|
| 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 | +} |
0 commit comments