public static void TestCount() { for (int i = 0; i < IPv6.BitCount; i++) { IPv6 addr = IPv6.NetMask(i); if (IPv6.GetMaskLength(addr) != i) { throw new TestFailedException( String.Format("TestCount {0}", i) ); } IPv6 addrDup = IPv6.Parse(addr.ToString()); if (addrDup != addr) { throw new TestFailedException( String.Format("TestCountDup {0}", i) ); } } }
/// <summary> /// /// Parse an IPv6 network string representation into an IPv6Network. /// /// <para> The representation should either be <ipAddress/> or /// <ipAddress/>/<masklength/>. /// </para> /// /// <para> Example forms of IPv6 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>IPv6.BitCount</c> or less than zero. /// </exception> /// /// <exception cref="OverflowException"> /// Netmask length overflows valid values for integers /// </exception> /// public static IPv6Network 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 = IPv6.BitCount; if (pieces.Length == 2) { maskLength = Int32.Parse(pieces[1]); } return(new IPv6Network(IPv6.Parse(pieces[0]), maskLength)); }
private static void TestParse() { // // grumble, grumble, no clean struct array initializer syntax !?! // // Okay, these are pairs of Valid IPv6 string representations // and their manually parsed IPv6 representations. // SrepV6Pair [] pairs = new SrepV6Pair[] { new SrepV6Pair( "FfEe:DdCc:BbAa:9876:5432:1001:2345:6789", new IPv6(0xffeeddcc, 0xbbaa9876, 0x54321001, 0x23456789) ), new SrepV6Pair( "ffe8:c181::192:101", new IPv6(0xffe8c181U, 0x0U, 0x0U, 0x01920101U) ), new SrepV6Pair( "::", IPv6.Zero ), new SrepV6Pair( "0:0:0:0:0:0:0:0", IPv6.Zero ), new SrepV6Pair( "1::", new IPv6(0x00010000U, 0x0U, 0x0U, 0x0U) ), new SrepV6Pair( "12::", new IPv6(0x00120000U, 0x0U, 0x0U, 0x0U) ), new SrepV6Pair( "123::", new IPv6(0x01230000U, 0x0U, 0x0U, 0x0U) ), new SrepV6Pair( "1234::", new IPv6(0x12340000U, 0x0U, 0x0U, 0x0U) ), new SrepV6Pair( "1234:5678:9abc:def0::", new IPv6(0x12345678U, 0x9abcdef0U, 0x0U, 0x0U) ), new SrepV6Pair( "::1", new IPv6(0x0U, 0x0U, 0x0U, 0x1U) ), new SrepV6Pair( "::12", new IPv6(0x0U, 0x0U, 0x0U, 0x12U) ), new SrepV6Pair( "::123", new IPv6(0x0U, 0x0U, 0x0U, 0x123U) ), new SrepV6Pair( "::1234", new IPv6(0x0U, 0x0U, 0x0U, 0x1234U) ), new SrepV6Pair( "::1234:5678:9abc:def0", new IPv6(0x0U, 0x0U, 0x12345678U, 0x9abcdef0U) ), }; for (int i = 0; i < pairs.Length; i++) { IPv6 a = IPv6.Parse(pairs[i].srep); if (a != pairs[i].v6rep) { Console.WriteLine("{0} {1}", a, pairs[i].v6rep); throw new TestFailedException( String.Format("Parse {0}", i) ); } IPv6 dup = IPv6.Parse(a.ToString()); if (dup != a) { throw new TestFailedException( String.Format("ToString {0}", i) ); } } // // Test bogus strings // string [] badSreps = new string [] { "Frank", "00000::", "0:1:2:3:4:5:6:7:8", "0.1.2.3.4::0.1.2.3.4.", "0:1:2:3:4:5:6:7:trailing junk", }; for (int i = 0; i < badSreps.Length; i++) { try { IPv6 bad = IPv6.Parse(badSreps[i]); throw new TestFailedException(badSreps[i]); } catch (FormatException) { } } }
private static void TestBasics() { // Create an IPv6 address from a set of uints IPv6 a = new IPv6(0xf0e1d2c3, 0xb4a59687, 0x78695a4b, 0x3c2d1e0f); // Then check it against equivalent array of Bytes byte[] xBytes = { 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87, 0x78, 0x69, 0x5a, 0x4b, 0x3c, 0x2d, 0x1e, 0x0f }; byte[] aBytes = a.GetAddressBytes(); if (aBytes.Length != 16) { throw new TestFailedException("GetAddressBytes Length"); } for (int i = 0; i < aBytes.Length; i++) { if (aBytes[i] != xBytes[i]) { throw new TestFailedException("GetAddressBytes Values"); } } // Instantiate from array of bytes and then check a = IPv6.ParseBytes(xBytes); aBytes = a.GetAddressBytes(); if (aBytes.Length != 16) { throw new TestFailedException("GetAddressBytes Length"); } for (int i = 0; i < aBytes.Length; i++) { if (aBytes[i] != xBytes[i]) { throw new TestFailedException("GetAddressBytes Values"); } } // Test System.Net.IPAddress Constructor IPAddress ipa = new IPAddress(aBytes); if (new IPv6(ipa) != a) { throw new TestFailedException("IPv6(IPAddress) Constructor"); } // Test basic string parsing a = IPv6.Parse("f0e1:d2c3:b4a5:9687:7869:5a4b:3c2d:1e0f"); aBytes = a.GetAddressBytes(); if (aBytes.Length != 16) { throw new TestFailedException("GetAddressBytes Length"); } for (int i = 0; i < aBytes.Length; i++) { if (aBytes[i] != xBytes[i]) { throw new TestFailedException("GetAddressBytes Values"); } } }
public static void TestCompare() { IPv6 address = IPv6.Parse("dead:beef:cafe:babe:5ca1:ab1e:b01d:face"); IPv6Network outer = new IPv6Network(address, 124); IPv6Network inner = new IPv6Network(address, 126); 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() { IPv6 address = IPv6.Parse("ab1e:d00d::1057:d05"); IPv6 mask = IPv6.Parse("ffff:fffe::"); int masklen = 31; if (new IPv6Network(address, masklen) != new IPv6Network(address, mask)) { throw new TestFailedException("Constructors"); } if (IPv6Network.Parse("10::/24") != new IPv6Network(new IPv6(0x00100000, 0x0, 0x0, 0x0), 24)) { Console.WriteLine(IPv6Network.Parse("10::/24")); Console.WriteLine(new IPv6Network(new IPv6(0x00100000, 0x0, 0x0, 0x0), 24)); throw new TestFailedException("10::/24"); } if (IPv6Network.Parse("10::/32") != new IPv6Network(new IPv6(0x00100000, 0x0, 0x0, 0x0), 32)) { throw new TestFailedException("10::/32"); } if (IPv6Network.Parse("10::/1") != new IPv6Network(new IPv6(0x00100000, 0x0, 0x0, 0x0), 1)) { throw new TestFailedException("10::/1"); } try { IPv6Network.Parse("10:://12"); throw new TestFailedException("double slash"); } catch (FormatException) { } try { IPv6Network.Parse("10::/129"); throw new TestFailedException("netmask length"); } catch (ArgumentException) { } try { IPv6Network.Parse("10::/xx"); throw new TestFailedException("netmask content"); } catch (FormatException) { } try { IPv6Network.Parse("10::x/10"); throw new TestFailedException("network content"); } catch (FormatException) { } try { IPv6Network.Parse("10::/3333333333333333333333333333"); throw new TestFailedException("netmask overflow"); } catch (OverflowException) { } }