private static void TestParseOne(string ipString, IPv4 expectedIP, bool expectedSuccess) { bool success = false; IPv4 testIP = IPv4.Zero; try { testIP = IPv4.Parse(ipString); success = (testIP == expectedIP); } catch (FormatException) { } if (success != expectedSuccess) { throw new TestFailedException(ipString); } IPv4 dup = IPv4.Parse(testIP.ToString()); if (testIP != dup) { throw new TestFailedException( String.Format("ToString {0}", testIP) ); } }
private static void TestBasics() { IPv4 a = new IPv4(0x1a2b3c4d); IPv4 b = IPv4.Parse("0x1a.0x2b.0x3c.0x4d"); byte [] ipv4Bytes = new byte [4] { 0x1a, 0x2b, 0x3c, 0x4d }; IPv4 c = IPv4.ParseBytes(ipv4Bytes); if ((uint)a != 0x1a2b3c4d) { throw new TestFailedException("a"); } if ((uint)b != 0x1a2b3c4d) { throw new TestFailedException("b"); } if ((uint)c != 0x1a2b3c4d) { throw new TestFailedException("c"); } if (a.Equals(b) == false) { throw new TestFailedException("a Equals b"); } if ((a == c) == false || (a != c)) { throw new TestFailedException("a c == !="); } for (int i = 0; i < ipv4Bytes.Length; i++) { if (ipv4Bytes[i] != c.GetAddressBytes()[i]) { throw new TestFailedException("GetAddressBytes"); } } byte [] ipAddrBytes = ((IPAddress)c).GetAddressBytes(); for (int i = 0; i < ipv4Bytes.Length; i++) { if (ipv4Bytes[i] != ipAddrBytes[i]) { throw new TestFailedException("IPAddress"); } } IPAddress ipa = IPAddress.Parse(c.ToString()); if (new IPv4(ipa) != c) { throw new TestFailedException("IPv4(IPAddress) Constructor"); } }
/// <summary> /// /// Parse an IPv4 network string representation into an IPv4Network. /// /// <para> The representation should either be <ipAddress/> or /// <ipAddress/>/<masklength/>. /// </para> /// /// <para> Example forms of IPv4 Networks are: 10.0.2.0/24, /// 10.0.0.1. /// </para> /// /// </summary> /// /// <exception cref="ArgumentNullException"></exception> /// /// <exception cref="FormatException"> /// Thrown when IP address component of format is invalid or too many /// slashes appear in string argument, or netmask length is not a valid /// integer. /// </exception> /// /// <exception cref="ArgumentException"> /// Thrown when specified mask length is greater than /// <c>IPv4.BitCount</c>or less than zero. /// </exception> /// /// <exception cref="OverflowException"> /// Netmask length overflows valid values for integers /// </exception> /// public static IPv4Network Parse(string ipNetwork) { if (ipNetwork == null) throw new ArgumentNullException("ipNetwork"); string [] pieces = ipNetwork.Split('/'); if (pieces.Length > 2) throw new FormatException("slash overload"); int maskLength = IPv4.BitCount; if (pieces.Length == 2) maskLength = Int32.Parse(pieces[1]); return new IPv4Network(IPv4.Parse(pieces[0]), maskLength); }
public static void TestCount() { for (int i = IPv4.BitCount; i >= 0; i--) { IPv4 addr = IPv4.NetMask(i); if (IPv4.GetMaskLength(addr) != i) { throw new TestFailedException( String.Format("TestCount {0}", i) ); } IPv4 addrDup = IPv4.Parse(addr.ToString()); if (addrDup != addr) { throw new TestFailedException( String.Format("TestCountDup {0}", i) ); } } }
public static void TestCompare() { IPv4 address = IPv4.Parse("10.1.1.0"); IPv4Network outer = new IPv4Network(address, 24); IPv4Network inner = new IPv4Network(address, 26); if (outer.Contains(outer) == false) { throw new TestFailedException("outer.Contains(outer)"); } if (outer.Contains(inner) == false) { throw new TestFailedException("outer.Contains(inner)"); } if (inner.Contains(outer) == true) { throw new TestFailedException("inner.Contains(outer)"); } if (outer.Contains(address) == false) { throw new TestFailedException("outer.Contains(address)"); } if (inner.Contains(address) == false) { throw new TestFailedException("inner.Contains(address)"); } if (outer.IsMoreSpecificThan(inner) == true) { throw new TestFailedException("outer.IsMoreSpecificThan(inner)"); } if (outer.IsLessSpecificThan(inner) == false) { throw new TestFailedException("outer.IsLessSpecificThan(inner)"); } if ((outer == outer) == false) { throw new TestFailedException("operator=="); } if ((outer != inner) == false) { throw new TestFailedException("operator!="); } if (outer.Contains(outer.UpperBound) == false) { throw new TestFailedException("outer.Contains(outer.UpperBound)"); } if (outer.Contains(outer.LowerBound) == false) { throw new TestFailedException("outer.Contains(outer.LowerBound)"); } }
private static void TestBasics() { IPv4 address = IPv4.Parse("192.168.0.100"); IPv4 mask = IPv4.Parse("255.255.255.254"); int masklen = 31; if (new IPv4Network(address, masklen) != new IPv4Network(address, mask)) { throw new TestFailedException("Constructors"); } if (IPv4Network.Parse("10.0.0.0/24") != new IPv4Network(new IPv4(0x0a000000), 24)) { throw new TestFailedException("10/24"); } if (IPv4Network.Parse("10.0.0.0/32") != new IPv4Network(new IPv4(0x0a000000), 32)) { throw new TestFailedException("10/32"); } if (IPv4Network.Parse("10.0.0.0/1") != new IPv4Network(new IPv4(0x0a000000), 1)) { throw new TestFailedException("10/1"); } try { IPv4Network.Parse("10.0.0.1//2"); throw new TestFailedException("double slash"); } catch (FormatException) { } try { IPv4Network.Parse("10.0.0.0/33"); throw new TestFailedException("netmask length"); } catch (ArgumentException) { } try { IPv4Network.Parse("10.0.0.0/xx"); throw new TestFailedException("netmask content"); } catch (FormatException) { } try { IPv4Network.Parse("10.x.0.0/10"); throw new TestFailedException("network content"); } catch (FormatException) { } try { IPv4Network.Parse("10.0.0.0/3333333333333333333333333333"); throw new TestFailedException("netmask overflow"); } catch (OverflowException) { } }