public void ToStringTest() { Assert.AreEqual("ALL TRAFFIC", Port.AllTraffic().ToString()); Assert.AreEqual("ALL PORTS", Port.AllTcp().ToString()); Assert.AreEqual("UDP ALL PORTS", Port.AllUdp().ToString()); Assert.AreEqual("2000-3000", Port.TcpRange(2000, 3000).ToString()); Assert.AreEqual("2000", Port.Tcp(2000).ToString()); Assert.AreEqual("2000-3000", Port.TcpRange(2000, 3000).ToString()); Assert.AreEqual("UDP 2000", Port.Udp(2000).ToString()); Assert.AreEqual("UDP 2000-3000", Port.UdpRange(2000, 3000).ToString()); }
public static Port Parse(string s, int p1 = 0, int p2 = 0) { var fields = s.Split(' ', StringSplitOptions.RemoveEmptyEntries); if (1 == fields.Length) { return(ParseRange(fields[0], (p) => Port.Tcp(p), (b, e) => Port.TcpRange(b, e), p1, p2)); } else if (2 <= fields.Length) { switch (fields[0].ToUpper()) { case "UDP": if (3 == fields.Length && "ALL" == fields[1].ToUpper() && "PORTS" == fields[2].ToUpper()) { return(Port.AllUdp()); } return(ParseRange(fields[1], (p) => Port.Udp(p), (b, e) => Port.UdpRange(b, e), p1, p2)); case "ICMP": if ("TYPE" == fields[1].ToUpper()) { switch (fields.Length) { case 3: return(Port.IcmpType(0 < p1 ? p1 : int.Parse(fields[2]))); case 5: if ("CODE" == fields[3].ToUpper()) { return(Port.IcmpTypeAndCode(0 < p1 ? p1 : int.Parse(fields[2]), 0 < p2 ? p2 : int.Parse(fields[4]))); } break; } } break; case "ALL": switch (fields[1].ToUpper()) { case "TRAFFIC": return(Port.AllTraffic()); case "PORTS": return(Port.AllTcp()); } break; } } throw new FormatException($"Can't parse Port: '{s}'"); }