2 // IPAddressTest.cs - NUnit Test Cases for System.Net.IPAddress
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 // Martin Willemoes Hansen (mwh@sysrq.dk)
8 // (C) Ximian, Inc. http://www.ximian.com
9 // (C) 2003 Martin Willemoes Hansen
10 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
13 using NUnit.Framework;
16 using System.Net.Sockets;
17 using System.Runtime.InteropServices;
19 namespace MonoTests.System.Net
23 public class IPAddressTest
25 static string[] ipv6AddressList = new string[] {
26 "::", "0:0:0:0:0:0:0:0",
27 "1::", "1:0:0:0:0:0:0:0",
28 "2:2::", "2:2:0:0:0:0:0:0",
29 "7:7:7:7:7:7:7:0", "7:7:7:7:7:7:7:0",
30 "::1", "0:0:0:0:0:0:0:1",
31 "::2", "0:0:0:0:0:0:0:2",
32 "::F", "0:0:0:0:0:0:0:F",
33 "::10", "0:0:0:0:0:0:0:10",
34 "::A0", "0:0:0:0:0:0:0:A0",
35 "::F0", "0:0:0:0:0:0:0:F0",
36 "::FF", "0:0:0:0:0:0:0:FF",
37 "::0.1.0.0", "0:0:0:0:0:0:1:0",
38 "::0.2.0.0", "0:0:0:0:0:0:2:0",
39 "::0.15.0.0", "0:0:0:0:0:0:F:0",
40 "::0.16.0.0", "0:0:0:0:0:0:10:0",
41 "::0.160.0.0", "0:0:0:0:0:0:A0:0",
42 "::0.240.0.0", "0:0:0:0:0:0:F0:0",
43 "::0.255.0.0", "0:0:0:0:0:0:FF:0",
44 "::1001", "0:0:0:0:0:0:0:1001",
45 "::1002", "0:0:0:0:0:0:0:1002",
46 "::100F", "0:0:0:0:0:0:0:100F",
47 "::1010", "0:0:0:0:0:0:0:1010",
48 "::10A0", "0:0:0:0:0:0:0:10A0",
49 "::10F0", "0:0:0:0:0:0:0:10F0",
50 "::10FF", "0:0:0:0:0:0:0:10FF",
51 "::0.1.0.1", "0:0:0:0:0:0:1:1",
52 "::0.2.0.2", "0:0:0:0:0:0:2:2",
53 "::0.15.0.15", "0:0:0:0:0:0:F:F",
54 "::0.16.0.16", "0:0:0:0:0:0:10:10",
55 "::0.160.0.160", "0:0:0:0:0:0:A0:A0",
56 "::0.240.0.240", "0:0:0:0:0:0:F0:F0",
57 "::0.255.0.255", "0:0:0:0:0:0:FF:FF",
58 "::FFFF:0:1", "0:0:0:0:0:FFFF:0:1",
59 "::FFFF:0:2", "0:0:0:0:0:FFFF:0:2",
60 "::FFFF:0:F", "0:0:0:0:0:FFFF:0:F",
61 "::FFFF:0:10", "0:0:0:0:0:FFFF:0:10",
62 "::FFFF:0:A0", "0:0:0:0:0:FFFF:0:A0",
63 "::FFFF:0:F0", "0:0:0:0:0:FFFF:0:F0",
64 "::FFFF:0:FF", "0:0:0:0:0:FFFF:0:FF",
65 "::FFFF:0.1.0.0", "0:0:0:0:0:FFFF:1:0",
66 "::FFFF:0.2.0.0", "0:0:0:0:0:FFFF:2:0",
67 "::FFFF:0.15.0.0", "0:0:0:0:0:FFFF:F:0",
68 "::FFFF:0.16.0.0", "0:0:0:0:0:FFFF:10:0",
69 "::FFFF:0.160.0.0", "0:0:0:0:0:FFFF:A0:0",
70 "::FFFF:0.240.0.0", "0:0:0:0:0:FFFF:F0:0",
71 "::FFFF:0.255.0.0", "0:0:0:0:0:FFFF:FF:0",
72 "::FFFF:0:1001", "0:0:0:0:0:FFFF:0:1001",
73 "::FFFF:0:1002", "0:0:0:0:0:FFFF:0:1002",
74 "::FFFF:0:100F", "0:0:0:0:0:FFFF:0:100F",
75 "::FFFF:0:1010", "0:0:0:0:0:FFFF:0:1010",
76 "::FFFF:0:10A0", "0:0:0:0:0:FFFF:0:10A0",
77 "::FFFF:0:10F0", "0:0:0:0:0:FFFF:0:10F0",
78 "::FFFF:0:10FF", "0:0:0:0:0:FFFF:0:10FF",
79 "::FFFF:0.1.0.1", "0:0:0:0:0:FFFF:1:1",
80 "::FFFF:0.2.0.2", "0:0:0:0:0:FFFF:2:2",
81 "::FFFF:0.15.0.15", "0:0:0:0:0:FFFF:F:F",
82 "::FFFF:0.16.0.16", "0:0:0:0:0:FFFF:10:10",
83 "::FFFF:0.160.0.160", "0:0:0:0:0:FFFF:A0:A0",
84 "::FFFF:0.240.0.240", "0:0:0:0:0:FFFF:F0:F0",
85 "::FFFF:0.255.0.255", "0:0:0:0:0:FFFF:FF:FF",
86 "0:7:7:7:7:7:7:7", "0:7:7:7:7:7:7:7",
87 "E::1", "E:0:0:0:0:0:0:1",
88 "E::2:2", "E:0:0:0:0:0:2:2",
89 "E:0:6:6:6:6:6:6", "E:0:6:6:6:6:6:6",
90 "E:E::1", "E:E:0:0:0:0:0:1",
91 "E:E::2:2", "E:E:0:0:0:0:2:2",
92 "E:E:0:5:5:5:5:5", "E:E:0:5:5:5:5:5",
93 "E:E:E::1", "E:E:E:0:0:0:0:1",
94 "E:E:E::2:2", "E:E:E:0:0:0:2:2",
95 "E:E:E:0:4:4:4:4", "E:E:E:0:4:4:4:4",
96 "E:E:E:E::1", "E:E:E:E:0:0:0:1",
97 "E:E:E:E::2:2", "E:E:E:E:0:0:2:2",
98 "E:E:E:E:0:3:3:3", "E:E:E:E:0:3:3:3",
99 "E:E:E:E:E::1", "E:E:E:E:E:0:0:1",
100 "E:E:E:E:E:0:2:2", "E:E:E:E:E:0:2:2",
101 "E:E:E:E:E:E:0:1", "E:E:E:E:E:E:0:1",
102 "::0.2.0.2", "0:0:0:0:0:0:2:2",
103 "::FFFF:192.168.0.1", "::FFFF:192.168.0.1",
104 "::FFFF:0.168.0.1", "::FFFF:0.168.0.1",
105 "::FFFF", "::0.0.255.255",
106 "::EEEE:A00:1", "::EEEE:10.0.0.1",
107 "::10.0.0.1", "::10.0.0.1",
108 "1234::1234:0:0", "1234:0:0:0:0:1234:0:0",
109 "1:0:1:0:1:0:1:0", "1:0:1:0:1:0:1:0",
110 "1:1:1::1:1:0", "1:1:1:0:0:1:1:0",
111 "::1234:0:0", "0:0:0:0:0:1234:0:0",
112 "3ffe:38e1::100:1:1", "3ffe:38e1::0100:1:0001",
113 "0:0:1:2::", "0:0:1:2:00:00:000:0000",
114 "100:0:1:2::abcd", "100:0:1:2:0:0:000:abcd",
115 "ffff::abcd", "ffff:0:0:0:0:0:00:abcd",
116 "ffff:0:0:2::abcd", "ffff:0:0:2:0:0:00:abcd",
117 "0:0:1:2::", "0:0:1:2:0:00:0000:0000",
118 "::1:0:0", "0000:0000::1:0000:0000",
119 "::111:234:5:6:789a:0", "0:0:111:234:5:6:789A:0",
120 "11:22:33:44:55:66:77:8", "11:22:33:44:55:66:77:8",
121 "0:0:7711:ab42:1230::", "::7711:ab42:1230:0:0:0",
122 "fec0:0:0:ffff::1%1",
125 static string[] ipv6ParseWrong = new string[] {
142 static string[] ipv4ParseOk = new string[] {
143 "192.168.1.1", "192.168.1.1",
144 "0xff.0x7f.0x20.0x01", "255.127.32.1",
145 "0xff.0x7f.0x20.0xf", "255.127.32.15",
146 "0.0.0.0", IPAddress.Any.ToString(),
147 "255.255.255.255", IPAddress.Broadcast.ToString(),
148 "12.1.1.3 abc", "12.1.1.3",
149 "12.1 .1.2", "12.0.0.1",
150 "12.1 .zzzz.2", "12.0.0.1",
151 "12.1.7", "12.1.0.7",
153 "12.5.3 foo.67.test.test.7FFFFFFFFFfaFFF789FFFFFFFFFFFFFFF", "12.5.0.3",
154 "12.1 foo.bar.test.test.baf", "12.0.0.1",
155 "12.1.4.6 foo.bar.test.test.baf", "12.1.4.6",
156 "12.3 foo.bar.test.test.4", "12.0.0.3",
157 "12 foo.bar.test.test.baf", "0.0.0.12",
159 "65535", "0.0.255.255",
160 "20.65535", "20.0.255.255",
161 "0313.027035210", "203.92.58.136", // bug #411920
162 "0313.0134.035210", "203.92.58.136", // too
163 "7848198702", "211.202.2.46", // too
164 "1434328179", "85.126.28.115", // too
165 "3397943208", "202.136.127.168", // too
168 static string [] ipv4ParseWrong = new string [] {
180 "12.1 foo.1.2.3.4.5.bar",
181 "12.1 foo.1.2.3.4.5.",
185 "12.1.foo.1.2.3.4.5.bar",
193 public void PublicFields ()
195 Assert.AreEqual ((long) 0, IPAddress.Any.Address, "#1");
196 Assert.AreEqual ((long) 0xFFFFFFFF, IPAddress.Broadcast.Address, "#2");
197 long loopback = IPAddress.HostToNetworkOrder (BitConverter.IsLittleEndian ?
200 Assert.AreEqual (loopback, IPAddress.Loopback.Address, "#3");
201 Assert.AreEqual ((long) 0xFFFFFFFF, IPAddress.None.Address, "#4");
205 public void ToStringV4 ()
207 IPAddress ip = IPAddress.Parse ("192.168.1.1");
208 Assert.AreEqual ("192.168.1.1", ip.ToString (), "#1");
209 Assert.AreEqual ("0.0.0.0", IPAddress.Any.ToString (), "#2");
210 Assert.AreEqual ("255.255.255.255", IPAddress.Broadcast.ToString (), "#3");
211 Assert.AreEqual ("127.0.0.1", IPAddress.Loopback.ToString (), "#4");
212 Assert.AreEqual ("255.255.255.255", IPAddress.None.ToString (), "#5");
216 public void ToStringV6 ()
218 for(int i=0; i<ipv6AddressList.Length/2; i++) {
219 string addr = IPAddress.Parse (ipv6AddressList[i*2+1]).ToString().ToLower();
220 Assert.AreEqual (ipv6AddressList[i*2].ToLower(), addr, "ToStringIPv6 #" + i);
225 public void IsLoopbackV4 ()
229 ip = IPAddress.Parse ("127.0.0.1");
230 Assert.IsTrue (IPAddress.IsLoopback (ip), "#1");
232 Assert.IsFalse (IPAddress.IsLoopback (ip), "#2");
233 ip = IPAddress.Loopback;
234 Assert.IsTrue (IPAddress.IsLoopback (ip), "#3");
235 ip = IPAddress.Parse ("::101");
236 Assert.IsFalse (IPAddress.IsLoopback (ip), "#4");
240 public void IsLoopbackV6 ()
242 if (!Socket.SupportsIPv6)
243 Assert.Ignore ("IPv6 must be enabled in machine.config");
245 IPAddress ip = IPAddress.IPv6Loopback;
246 Assert.IsTrue (IPAddress.IsLoopback (ip), "#1");
248 ip = IPAddress.IPv6None;
249 Assert.IsFalse (IPAddress.IsLoopback (ip), "#2");
253 public void GetAddressBytesV4 ()
255 byte[] dataIn = { 10, 11, 12, 13 };
256 byte[] dataOut = IPAddress.Parse ("10.11.12.13").GetAddressBytes ();
257 for (int i = 0; i < dataIn.Length; i++)
258 Assert.AreEqual (dataOut [i], dataIn [i], "GetAddressBytesV4");
262 public void GetAddressBytesV6 ()
264 byte[] dataIn = new byte[]{ 0x01, 0x23, 0x45, 0x67, 0x89, 0x98, 0x76, 0x54, 0x32, 0x10, 0x01, 0x23, 0x45, 0x67, 0x89, 0x98 };
265 byte[] dataOut = IPAddress.Parse ("123:4567:8998:7654:3210:0123:4567:8998").GetAddressBytes ();
266 for (int i = 0; i < dataIn.Length; i++)
267 Assert.AreEqual (dataOut [i], dataIn [i], "GetAddressBytesV6 #1");
269 dataIn = new byte[]{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0x01 };
270 dataOut = IPAddress.Parse ("::FFFF:127.0.0.1").GetAddressBytes ();
271 for (int i = 0; i < dataIn.Length; i++)
272 Assert.AreEqual (dataOut [i], dataIn [i], "GetAddressBytesV6 #2");
276 public void Address ()
278 // hm, lame, anything is accepted by ms.net
281 IPAddress ip1 = new IPAddress (0x0000000100000000);
282 Assertion.Fail ("#1");
283 } catch (ArgumentOutOfRangeException) {}
284 IPAddress ip = IPAddress.Parse ("127.0.0.1");
286 ip.Address = 0xffffffff;
289 Assertion.Fail ("#2");
290 } catch (ArgumentOutOfRangeException) {}
292 ip.Address = 0x0000000100000000;
293 Assertion.Fail ("#3");
294 } catch (ArgumentOutOfRangeException) {}
299 public void ParseOkV4 ()
301 for (int i = 0; i < ipv4ParseOk.Length / 2; i++) {
304 ip = IPAddress.Parse (ipv4ParseOk [i*2]);
305 Assert.AreEqual (ipv4ParseOk [i * 2 + 1], ip.ToString (), "ParseOkV4:" + i);
306 } catch (FormatException) {
307 Assert.Fail ("Cannot parse test i=" + i + ": '" + ipv4ParseOk [i*2] + "'");
313 public void ParseOkV6 ()
315 for (int i = 0; i < ipv6AddressList.Length / 2; i++) {
316 string source = ipv6AddressList [i*2].ToLower();
318 IPAddress ip = IPAddress.Parse (source);
319 Assert.AreEqual (ip.ToString ().ToLower (), source,
320 string.Format("ParseIPv6 #{0}-1: {1} != {2}", i,
321 ip.ToString ().ToLower (), source));
323 ip = IPAddress.Parse (ipv6AddressList [i*2+1].ToLower ());
324 Assert.AreEqual (ip.ToString ().ToLower (), source,
325 string.Format("ParseIPv6 #{0}-2: {1} != {2}", i,
326 ip.ToString ().ToLower (), source));
331 public void ParseWrongV4 ()
333 for (int i = 0; i < ipv4ParseWrong.Length; i++) {
334 string ipAddress = ipv4ParseWrong [i];
337 IPAddress ip = IPAddress.Parse (ipAddress);
338 Assert.Fail ("#1:" + i + " (" + ipAddress + ")");
339 } catch (FormatException ex) {
340 Assert.AreEqual (typeof (FormatException), ex.GetType (), "#2:" + i);
341 Assert.IsNull (ex.InnerException, "#3:" + i);
342 Assert.IsNotNull (ex.Message, "#4:" + i);
348 public void Parse_IpString_Null ()
351 IPAddress.Parse ((string) null);
353 } catch (ArgumentNullException ex) {
354 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
355 Assert.IsNull (ex.InnerException, "#3");
356 Assert.IsNotNull (ex.Message, "#4");
357 Assert.AreEqual ("ipString", ex.ParamName, "#5");
362 public void NetworkHost ()
364 long [] tested = new long [] { 0, 1, 1, 1};
365 long [] expectedLE = new long [] {0, 256, 16777216, 72057594037927936 };
368 expected = BitConverter.IsLittleEndian ? expectedLE : tested;
370 short short0 = IPAddress.NetworkToHostOrder ((short) tested [0]);
371 Assert.AreEqual ((short) expected [0], short0, "#A1");
372 short0 = IPAddress.HostToNetworkOrder (short0);
373 Assert.AreEqual ((short) tested [0], short0, "#A2");
375 int int0 = IPAddress.NetworkToHostOrder ((int) tested [0]);
376 Assert.AreEqual ((int) expected [0], int0, "#B1");
377 int0 = IPAddress.HostToNetworkOrder (int0);
378 Assert.AreEqual ((int) tested [0], int0, "#B2");
380 long long0 = IPAddress.NetworkToHostOrder (tested [0]);
381 Assert.AreEqual (expected [0], long0, "#C1");
382 long0 = IPAddress.HostToNetworkOrder (long0);
383 Assert.AreEqual (tested [0], long0, "#C2");
385 short0 = IPAddress.NetworkToHostOrder ((short) tested [1]);
386 Assert.AreEqual ((short) expected [1], short0, "#D1");
387 short0 = IPAddress.HostToNetworkOrder (short0);
388 Assert.AreEqual ((short) tested [1], short0, "#D2");
390 int0 = IPAddress.NetworkToHostOrder ((int) tested [2]);
391 Assert.AreEqual ((int) expected [2], int0, "#E1");
392 int0 = IPAddress.HostToNetworkOrder (int0);
393 Assert.AreEqual ((int) tested [2], int0, "#E2");
395 long0 = IPAddress.NetworkToHostOrder (tested [3]);
396 Assert.AreEqual (expected [3], long0, "#F1");
397 long0 = IPAddress.HostToNetworkOrder (long0);
398 Assert.AreEqual (tested [3], long0, "#F2");
402 public void LoopbackIPv6 ()
404 Assert.IsTrue (new Uri("http://[0:0:0:0::127.0.0.1]/").IsLoopback, "#1");
405 Assert.IsFalse (new Uri ("http://[0:0:0:0::127.1.2.3]/").IsLoopback, "#2");
406 Assert.IsTrue (new Uri ("http://[0:0:0:0::0.0.0.1]/").IsLoopback, "#3");
410 public void Constructor0_Address_4Byte ()
412 byte[] bytes = new byte[4] { 192, 202, 112, 37 };
413 IPAddress i = new IPAddress (bytes);
414 Assert.AreEqual (bytes, i.GetAddressBytes (), "#1");
415 Assert.AreEqual ("192.202.112.37", i.ToString (), "#2");
419 public void Constructor0_Address_Invalid ()
422 new IPAddress (new byte [0]);
424 } catch (ArgumentException ex) {
425 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
426 Assert.IsNull (ex.InnerException, "#A3");
427 Assert.IsNotNull (ex.Message, "#A4");
428 Assert.IsFalse (ex.Message == "address", ex.Message, "#A5");
429 Assert.IsNotNull (ex.ParamName, "#A6");
430 Assert.AreEqual ("address", ex.ParamName, "#A7");
434 new IPAddress (new byte [3] { 192, 202, 112 });
436 } catch (ArgumentException ex) {
437 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
438 Assert.IsNull (ex.InnerException, "#B3");
439 Assert.IsNotNull (ex.Message, "#B4");
440 Assert.IsFalse (ex.Message == "address", ex.Message, "#B5");
441 Assert.IsNotNull (ex.ParamName, "#B6");
442 Assert.AreEqual ("address", ex.ParamName, "#B7");
446 new IPAddress (new byte [5] { 192, 202, 112, 142, 25 });
448 } catch (ArgumentException ex) {
449 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
450 Assert.IsNull (ex.InnerException, "#C3");
451 Assert.IsNotNull (ex.Message, "#C4");
452 Assert.IsFalse (ex.Message == "address", "#C5");
453 Assert.IsNotNull (ex.ParamName, "#C6");
454 Assert.AreEqual ("address", ex.ParamName, "#C7");
459 public void Constructor0_Address_Null ()
462 new IPAddress ((byte []) null);
464 } catch (ArgumentNullException ex) {
465 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
466 Assert.IsNull (ex.InnerException, "#3");
467 Assert.IsNotNull (ex.Message, "#4");
468 Assert.AreEqual ("address", ex.ParamName, "#5");
473 public void Constructor1_Address_4Byte ()
475 byte [] bytes = new byte [4] { 192, 202, 112, 37 };
477 new IPAddress (bytes, 0);
479 } catch (ArgumentException ex) {
480 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
481 Assert.IsNull (ex.InnerException, "#3");
482 Assert.IsNotNull (ex.Message, "#4");
483 Assert.IsFalse (ex.Message == "address", "#5");
484 Assert.IsNotNull (ex.ParamName, "#6");
485 Assert.AreEqual ("address", ex.ParamName, "#7");
490 public void Constructor1_Address_Null ()
493 new IPAddress ((byte []) null, 5);
495 } catch (ArgumentNullException ex) {
496 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
497 Assert.IsNull (ex.InnerException, "#3");
498 Assert.IsNotNull (ex.Message, "#4");
499 Assert.AreEqual ("address", ex.ParamName, "#5");
504 public void FromBytes1 ()
506 byte[] val1 = new byte[4];
511 CompareIPs (val1, "82.165.240.134");
513 byte[] val2 = new byte[4];
518 CompareIPs (val2, "123.124.125.126");
521 void CompareIPs (byte [] bytes, string address)
523 IPAddress ip = new IPAddress (bytes);
524 IPAddress ip2 = IPAddress.Parse (address);
525 Assert.IsTrue (ip2.Equals (ip), "#A-" + address);
526 Assert.IsTrue (ip.Equals (ip2), "#B-" + address);
530 public void TryParse_IpString_Null ()
535 IPAddress.TryParse ((string) null, out i);
537 } catch (ArgumentNullException ex) {
538 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
539 Assert.IsNull (ex.InnerException, "#3");
540 Assert.IsNotNull (ex.Message, "#4");
541 Assert.AreEqual ("ipString", ex.ParamName, "#5");
546 public void TryParse ()
549 Assert.IsTrue (IPAddress.TryParse ("0.0.0.0", out i), "#1");
550 Assert.IsTrue (IPAddress.TryParse ("127.0.0.1", out i), "#2");
551 Assert.IsFalse (IPAddress.TryParse ("www.mono-project.com", out i), "#3");
552 Assert.IsTrue (IPAddress.TryParse ("0001:0002:0003:0004:0005:0006:0007:0008", out i), "#4");
553 Assert.IsTrue (IPAddress.TryParse ("1:2:3:4:5:6:7:8", out i), "#5");
554 Assert.IsTrue (IPAddress.TryParse ("1::8", out i), "#6");
555 Assert.IsTrue (IPAddress.TryParse ("1::3:4:5:6:7:8", out i), "#7");
556 Assert.IsFalse (IPAddress.TryParse ("1::2:3:4:5::6:7:8", out i), "#8"); // :: ::
557 Assert.IsFalse (IPAddress.TryParse ("1::2:3:4:5:6:7:8", out i), "#9");
558 Assert.IsFalse (IPAddress.TryParse ("1;2:3:4:5:6:7:8", out i), "#10"); // ;
560 // Assert.IsFalse (IPAddress.TryParse ("1:2:3:4:5:6:7:8/10", out i), "#11"); // subnet
564 public void TryParseOkV4 ()
566 for (int i = 0; i < ipv4ParseOk.Length / 2; i++) {
568 Assert.IsTrue (IPAddress.TryParse (ipv4ParseOk [i * 2], out ip), "#1:" + i);
569 Assert.AreEqual (ipv4ParseOk [i * 2 + 1], ip.ToString (), "#2:" + i);
574 public void TryParseWrongV4 ()
576 for (int i = 0; i < ipv4ParseWrong.Length; i++) {
578 string ipAddress = ipv4ParseWrong [i];
580 Assert.IsFalse (IPAddress.TryParse (ipAddress, out ip), "#1:" + i);
581 Assert.IsNull (ip, "#2:" + i);
586 public void IsIPv6LinkLocal ()
588 Assert.IsTrue (IPAddress.Parse ("FE80::1").IsIPv6LinkLocal, "#1");
589 Assert.IsTrue (IPAddress.Parse ("FE81::1").IsIPv6LinkLocal, "#2");
590 Assert.IsFalse (IPAddress.Parse ("FD81::1").IsIPv6LinkLocal, "#3");
591 Assert.IsFalse (IPAddress.Parse ("FF80::1").IsIPv6LinkLocal, "#4");
592 Assert.IsTrue (IPAddress.Parse ("FE91::1").IsIPv6LinkLocal, "#5");
593 Assert.IsTrue (IPAddress.Parse ("FEA0::1").IsIPv6LinkLocal, "#6");
594 Assert.IsFalse (IPAddress.Parse ("FEC0::1").IsIPv6LinkLocal, "#7");
598 public void IsIPv6SiteLocal ()
600 Assert.IsTrue (IPAddress.Parse ("FEC0::1").IsIPv6SiteLocal, "#1");
601 Assert.IsTrue (IPAddress.Parse ("FEC1::1").IsIPv6SiteLocal, "#2");
602 Assert.IsFalse (IPAddress.Parse ("FE81::1").IsIPv6SiteLocal, "#3");
603 Assert.IsFalse (IPAddress.Parse ("FFC0::1").IsIPv6SiteLocal, "#4");
604 Assert.IsTrue (IPAddress.Parse ("FED1::1").IsIPv6SiteLocal, "#5");
605 Assert.IsTrue (IPAddress.Parse ("FEE1::1").IsIPv6SiteLocal, "#6");
609 public void IsIPv6Multicast ()
611 Assert.IsTrue (IPAddress.Parse ("FF00::1").IsIPv6Multicast, "#1");
612 Assert.IsTrue (IPAddress.Parse ("FF01::1").IsIPv6Multicast, "#2");
613 Assert.IsFalse (IPAddress.Parse ("FE00::1").IsIPv6Multicast, "#3");
617 public void IsIPv6Teredo ()
619 Assert.IsTrue (IPAddress.Parse ("2001::1").IsIPv6Teredo, "#1");
620 Assert.IsFalse (IPAddress.Parse ("2002::1").IsIPv6Teredo, "#2");
624 public void ParseWrongV6 ()
626 if (!Socket.SupportsIPv6)
627 Assert.Ignore ("IPv6 must be enabled in machine.config");
629 for (int i = 0; i < ipv6ParseWrong.Length; i++) {
630 string ipAddress = ipv6ParseWrong [i];
633 IPAddress ip = IPAddress.Parse (ipAddress);
634 Assert.Fail ("#1:" + i + " (" + ipAddress + ")");
635 } catch (FormatException ex) {
636 Assert.AreEqual (typeof (FormatException), ex.GetType (), "#2:" + i);
637 Assert.IsNull (ex.InnerException, "#3:" + i);
638 Assert.IsNotNull (ex.Message, "#4:" + i);