Skip to content

Commit 13d1164

Browse files
committed
Added unit tests
1 parent 8989180 commit 13d1164

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using System;
3+
using TechnitiumLibrary.Net.Firewall;
4+
5+
namespace TechnitiumLibrary.UnitTests.TechnitiumLibrary.Net.Firewall
6+
{
7+
[TestClass]
8+
public sealed class WindowsFirewallPublicTests
9+
{
10+
[TestMethod]
11+
public void AddPort_ShouldThrow_WhenUnsupportedProtocol()
12+
{
13+
// Protocol ICMPv4 cannot be added using AddPort
14+
Assert.ThrowsExactly<Exception>(() => WindowsFirewall.AddPort("bad", Protocol.ICMPv4, port: 55, enable: true));
15+
}
16+
17+
[TestMethod]
18+
public void RemovePort_ShouldThrow_WhenUnsupportedProtocol()
19+
{
20+
// RemovePort validates only TCP, UDP, ANY
21+
Assert.ThrowsExactly<Exception>(() => WindowsFirewall.RemovePort(Protocol.IGMP, 123));
22+
}
23+
24+
[TestMethod]
25+
public void PortExists_ShouldThrow_WhenUnsupportedProtocol()
26+
{
27+
Assert.ThrowsExactly<Exception>(() => WindowsFirewall.PortExists(Protocol.IGMP, 44));
28+
}
29+
30+
[TestMethod]
31+
public void RuleExistsVista_ShouldReturnDoesNotExist_WhenInputsClearlyNotMatchingAnything()
32+
{
33+
// Since firewall is not guaranteed to have this rule,
34+
// safest expected response is DoesNotExists.
35+
RuleStatus result = WindowsFirewall.RuleExistsVista(
36+
name: "__Definitely_Not_A_Real_Rule__",
37+
applicationPath: "__Fake__");
38+
39+
Assert.AreEqual(RuleStatus.DoesNotExists, result);
40+
}
41+
42+
[TestMethod]
43+
public void ApplicationExists_ShouldReturnDoesNotExist_WhenApplicationIsNotRegistered()
44+
{
45+
// Public observable guarantee:
46+
// if the system has no such application entry → DoesNotExists
47+
48+
const string fakePath = "C:\\DefinitelyNotExisting\\app.exe";
49+
50+
RuleStatus status = WindowsFirewall.ApplicationExists(fakePath);
51+
52+
Assert.AreEqual(RuleStatus.DoesNotExists, status);
53+
}
54+
}
55+
}

TechnitiumLibrary.UnitTests/TechnitiumLibrary.UnitTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<ProjectReference Include="..\TechnitiumLibrary\TechnitiumLibrary.csproj" />
12+
<ProjectReference Include="..\TechnitiumLibrary.Net.Firewall\TechnitiumLibrary.Net.Firewall.csproj" />
1313
</ItemGroup>
1414

1515
</Project>

0 commit comments

Comments
 (0)