public void TestParseRuleSingle() { int localPort = 80; IPAddress remoteAddress = IPAddress.Parse("192.168.1.1"); int remotePort = 128; string record = $"X\tYes\tAllow\t{localPort}\t{remoteAddress}\t{remotePort}\tTCP"; WindowsFirewallRule rule = WindowsFirewallRuleParser.ParseRecord( WindowsFirewallRuleParserTest.HeaderIndex, record, '\t'); Assert.AreEqual("X", rule.Name); Assert.IsTrue(rule.Enabled); Assert.IsTrue(rule.Allow); Assert.AreEqual(1, rule.LocalPorts.Ranges.Count); Assert.AreEqual(localPort, rule.LocalPorts.Ranges.Single().Low); Assert.AreEqual(localPort, rule.LocalPorts.Ranges.Single().High); Assert.AreEqual(1, rule.RemotePorts.Ranges.Count); Assert.AreEqual(remotePort, rule.RemotePorts.Ranges.Single().Low); Assert.AreEqual(remotePort, rule.RemotePorts.Ranges.Single().High); Assert.AreEqual(1, rule.RemoteAddresses.Ranges.Count); Assert.AreEqual(remoteAddress, rule.RemoteAddresses.Ranges.Single().Low); Assert.AreEqual(remoteAddress, rule.RemoteAddresses.Ranges.Single().High); Assert.AreEqual(6, rule.Protocol.ProtocolNumber); }
public void TestParseRuleRanges() { int localPortLow = 80; int localPortHigh = 8080; IPAddress remoteAddressLow = IPAddress.Parse("64.32.16.8"); IPAddress remoteAddressHigh = IPAddress.Parse("128.64.32.16"); int remotePortLow = 128; int remotePortHigh = 256; string record = $"X\tYes\tAllow\t{localPortLow}-{localPortHigh}\t" + $"{remoteAddressLow}-{remoteAddressHigh}\t" + $"{remotePortLow}-{remotePortHigh}\tUDP"; WindowsFirewallRule rule = WindowsFirewallRuleParser.ParseRecord( WindowsFirewallRuleParserTest.HeaderIndex, record, '\t'); Assert.AreEqual(1, rule.LocalPorts.Ranges.Count); Assert.AreEqual(localPortLow, rule.LocalPorts.Ranges.Single().Low); Assert.AreEqual(localPortHigh, rule.LocalPorts.Ranges.Single().High); Assert.AreEqual(1, rule.RemotePorts.Ranges.Count); Assert.AreEqual(remotePortLow, rule.RemotePorts.Ranges.Single().Low); Assert.AreEqual(remotePortHigh, rule.RemotePorts.Ranges.Single().High); Assert.AreEqual(1, rule.RemoteAddresses.Ranges.Count); Assert.AreEqual(remoteAddressLow, rule.RemoteAddresses.Ranges.Single().Low); Assert.AreEqual(remoteAddressHigh, rule.RemoteAddresses.Ranges.Single().High); Assert.AreEqual(17, rule.Protocol.ProtocolNumber); }
public void TestParseHeader() { Dictionary <string, int> indexed = WindowsFirewallRuleParser.ParseHeader( WindowsFirewallRuleParser.RequiredHeaders, WindowsFirewallRuleParserTest.HeaderText, '\t'); foreach (KeyValuePair <string, int> pair in WindowsFirewallRuleParserTest.HeaderIndex) { Assert.IsTrue(indexed.ContainsKey(pair.Key)); Assert.AreEqual(pair.Value, indexed[pair.Key]); } }
/// <summary> /// Parses and verifies a record. /// </summary> /// <param name="headerIndex">The column header index.</param> /// <param name="recordLine">The unparsed record.</param> /// <param name="separator">The character separating columns.</param> /// <returns>An instance of <see cref="WindowsFirewallRule"/>.</returns> public static WindowsFirewallRule ParseRecord(Dictionary <string, int> headerIndex, string recordLine, char separator) { if (string.IsNullOrEmpty(recordLine)) { throw new ArgumentNullException(recordLine); } string[] record = recordLine.Split(new[] { separator }, StringSplitOptions.None); return(new WindowsFirewallRule { Name = WindowsFirewallRuleParser.ParseName(record[headerIndex[RuleNameName]]), RemoteAddresses = WindowsFirewallRuleParser.ParseAddressSet(record[headerIndex[RemoteAddressHeaderName]]), RemotePorts = WindowsFirewallRuleParser.ParsePortSet(record[headerIndex[RemotePortHeaderName]]), LocalPorts = WindowsFirewallRuleParser.ParsePortSet(record[headerIndex[LocalPortHeaderName]]), Protocol = WindowsFirewallRuleParser.ParseNetworkProtocol(record[headerIndex[ProtocolHeaderName]]), Enabled = WindowsFirewallRuleParser.ParseEnabled(record[headerIndex[EnabledHeaderName]]), Allow = WindowsFirewallRuleParser.ParseAction(record[headerIndex[PermissionHeaderName]]) }); }