IPAddressTest.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. //
  2. // IPAddressTest.cs - NUnit Test Cases for System.Net.IPAddress
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. // Martin Willemoes Hansen ([email protected])
  7. //
  8. // (C) Ximian, Inc. http://www.ximian.com
  9. // (C) 2003 Martin Willemoes Hansen
  10. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  11. //
  12. using NUnit.Framework;
  13. using System;
  14. using System.Net;
  15. using System.Net.Sockets;
  16. using System.Runtime.InteropServices;
  17. namespace MonoTests.System.Net
  18. {
  19. [TestFixture]
  20. public class IPAddressTest
  21. {
  22. static string[] ipv6AddressList = new string[] {
  23. "::", "0:0:0:0:0:0:0:0",
  24. "1::", "1:0:0:0:0:0:0:0",
  25. "2:2::", "2:2:0:0:0:0:0:0",
  26. "7:7:7:7:7:7:7:0", "7:7:7:7:7:7:7:0",
  27. "::1", "0:0:0:0:0:0:0:1",
  28. "::2", "0:0:0:0:0:0:0:2",
  29. "::F", "0:0:0:0:0:0:0:F",
  30. "::10", "0:0:0:0:0:0:0:10",
  31. "::A0", "0:0:0:0:0:0:0:A0",
  32. "::F0", "0:0:0:0:0:0:0:F0",
  33. "::FF", "0:0:0:0:0:0:0:FF",
  34. "::0.1.0.0", "0:0:0:0:0:0:1:0",
  35. "::0.2.0.0", "0:0:0:0:0:0:2:0",
  36. "::0.15.0.0", "0:0:0:0:0:0:F:0",
  37. "::0.16.0.0", "0:0:0:0:0:0:10:0",
  38. "::0.160.0.0", "0:0:0:0:0:0:A0:0",
  39. "::0.240.0.0", "0:0:0:0:0:0:F0:0",
  40. "::0.255.0.0", "0:0:0:0:0:0:FF:0",
  41. "::1001", "0:0:0:0:0:0:0:1001",
  42. "::1002", "0:0:0:0:0:0:0:1002",
  43. "::100F", "0:0:0:0:0:0:0:100F",
  44. "::1010", "0:0:0:0:0:0:0:1010",
  45. "::10A0", "0:0:0:0:0:0:0:10A0",
  46. "::10F0", "0:0:0:0:0:0:0:10F0",
  47. "::10FF", "0:0:0:0:0:0:0:10FF",
  48. "::0.1.0.1", "0:0:0:0:0:0:1:1",
  49. "::0.2.0.2", "0:0:0:0:0:0:2:2",
  50. "::0.15.0.15", "0:0:0:0:0:0:F:F",
  51. "::0.16.0.16", "0:0:0:0:0:0:10:10",
  52. "::0.160.0.160", "0:0:0:0:0:0:A0:A0",
  53. "::0.240.0.240", "0:0:0:0:0:0:F0:F0",
  54. "::0.255.0.255", "0:0:0:0:0:0:FF:FF",
  55. "::FFFF:0:1", "0:0:0:0:0:FFFF:0:1",
  56. "::FFFF:0:2", "0:0:0:0:0:FFFF:0:2",
  57. "::FFFF:0:F", "0:0:0:0:0:FFFF:0:F",
  58. "::FFFF:0:10", "0:0:0:0:0:FFFF:0:10",
  59. "::FFFF:0:A0", "0:0:0:0:0:FFFF:0:A0",
  60. "::FFFF:0:F0", "0:0:0:0:0:FFFF:0:F0",
  61. "::FFFF:0:FF", "0:0:0:0:0:FFFF:0:FF",
  62. "::FFFF:0.1.0.0", "0:0:0:0:0:FFFF:1:0",
  63. "::FFFF:0.2.0.0", "0:0:0:0:0:FFFF:2:0",
  64. "::FFFF:0.15.0.0", "0:0:0:0:0:FFFF:F:0",
  65. "::FFFF:0.16.0.0", "0:0:0:0:0:FFFF:10:0",
  66. "::FFFF:0.160.0.0", "0:0:0:0:0:FFFF:A0:0",
  67. "::FFFF:0.240.0.0", "0:0:0:0:0:FFFF:F0:0",
  68. "::FFFF:0.255.0.0", "0:0:0:0:0:FFFF:FF:0",
  69. "::FFFF:0:1001", "0:0:0:0:0:FFFF:0:1001",
  70. "::FFFF:0:1002", "0:0:0:0:0:FFFF:0:1002",
  71. "::FFFF:0:100F", "0:0:0:0:0:FFFF:0:100F",
  72. "::FFFF:0:1010", "0:0:0:0:0:FFFF:0:1010",
  73. "::FFFF:0:10A0", "0:0:0:0:0:FFFF:0:10A0",
  74. "::FFFF:0:10F0", "0:0:0:0:0:FFFF:0:10F0",
  75. "::FFFF:0:10FF", "0:0:0:0:0:FFFF:0:10FF",
  76. "::FFFF:0.1.0.1", "0:0:0:0:0:FFFF:1:1",
  77. "::FFFF:0.2.0.2", "0:0:0:0:0:FFFF:2:2",
  78. "::FFFF:0.15.0.15", "0:0:0:0:0:FFFF:F:F",
  79. "::FFFF:0.16.0.16", "0:0:0:0:0:FFFF:10:10",
  80. "::FFFF:0.160.0.160", "0:0:0:0:0:FFFF:A0:A0",
  81. "::FFFF:0.240.0.240", "0:0:0:0:0:FFFF:F0:F0",
  82. "::FFFF:0.255.0.255", "0:0:0:0:0:FFFF:FF:FF",
  83. "0:7:7:7:7:7:7:7", "0:7:7:7:7:7:7:7",
  84. "E::1", "E:0:0:0:0:0:0:1",
  85. "E::2:2", "E:0:0:0:0:0:2:2",
  86. "E:0:6:6:6:6:6:6", "E:0:6:6:6:6:6:6",
  87. "E:E::1", "E:E:0:0:0:0:0:1",
  88. "E:E::2:2", "E:E:0:0:0:0:2:2",
  89. "E:E:0:5:5:5:5:5", "E:E:0:5:5:5:5:5",
  90. "E:E:E::1", "E:E:E:0:0:0:0:1",
  91. "E:E:E::2:2", "E:E:E:0:0:0:2:2",
  92. "E:E:E:0:4:4:4:4", "E:E:E:0:4:4:4:4",
  93. "E:E:E:E::1", "E:E:E:E:0:0:0:1",
  94. "E:E:E:E::2:2", "E:E:E:E:0:0:2:2",
  95. "E:E:E:E:0:3:3:3", "E:E:E:E:0:3:3:3",
  96. "E:E:E:E:E::1", "E:E:E:E:E:0:0:1",
  97. "E:E:E:E:E:0:2:2", "E:E:E:E:E:0:2:2",
  98. "E:E:E:E:E:E:0:1", "E:E:E:E:E:E:0:1",
  99. "::0.2.0.2", "0:0:0:0:0:0:2:2",
  100. "::FFFF:192.168.0.1", "::FFFF:192.168.0.1",
  101. "::FFFF:0.168.0.1", "::FFFF:0.168.0.1",
  102. "::FFFF", "::0.0.255.255",
  103. "::EEEE:A00:1", "::EEEE:10.0.0.1",
  104. "::10.0.0.1", "::10.0.0.1",
  105. "1234::1234:0:0", "1234:0:0:0:0:1234:0:0",
  106. "1:0:1:0:1:0:1:0", "1:0:1:0:1:0:1:0",
  107. "1:1:1::1:1:0", "1:1:1:0:0:1:1:0",
  108. "::1234:0:0", "0:0:0:0:0:1234:0:0",
  109. "3ffe:38e1::100:1:1", "3ffe:38e1::0100:1:0001",
  110. "0:0:1:2::", "0:0:1:2:00:00:000:0000",
  111. "100:0:1:2::abcd", "100:0:1:2:0:0:000:abcd",
  112. "ffff::abcd", "ffff:0:0:0:0:0:00:abcd",
  113. "ffff:0:0:2::abcd", "ffff:0:0:2:0:0:00:abcd",
  114. "0:0:1:2::", "0:0:1:2:0:00:0000:0000",
  115. "::1:0:0", "0000:0000::1:0000:0000",
  116. "::111:234:5:6:789a:0", "0:0:111:234:5:6:789A:0",
  117. "11:22:33:44:55:66:77:8", "11:22:33:44:55:66:77:8",
  118. "0:0:7711:ab42:1230::", "::7711:ab42:1230:0:0:0",
  119. "fec0:0:0:ffff::1%1",
  120. };
  121. static string[] ipv6ParseWrong = new string[] {
  122. ":::4df",
  123. "4df:::",
  124. "0:::4df",
  125. "4df:::0",
  126. "::4df:::",
  127. "0::4df:::",
  128. " ::1",
  129. ":: 1",
  130. ":",
  131. "0:0:0:0:0:0:0:0:0",
  132. "0:0:0:0:0:0:0",
  133. "0FFFF::",
  134. "FFFF0::",
  135. "[::1",
  136. };
  137. static string[] ipv4ParseOk = new string[] {
  138. "192.168.1.1", "192.168.1.1",
  139. "0xff.0x7f.0x20.0x01", "255.127.32.1",
  140. "0xff.0x7f.0x20.0xf", "255.127.32.15",
  141. "0.0.0.0", IPAddress.Any.ToString(),
  142. "255.255.255.255", IPAddress.Broadcast.ToString(),
  143. "12.1.7", "12.1.0.7",
  144. "12", "0.0.0.12",
  145. "65536", "0.1.0.0",
  146. "65535", "0.0.255.255",
  147. "20.65535", "20.0.255.255",
  148. "0313.027035210", "203.92.58.136", // bug #411920
  149. "0313.0134.035210", "203.92.58.136", // too
  150. "1434328179", "85.126.28.115", // too
  151. "3397943208", "202.136.127.168", // too
  152. };
  153. static string [] ipv4ParseWrong = new string [] {
  154. " foo",
  155. "12.. .",
  156. "12.1.2. ",
  157. "12.1.8. ",
  158. ".1.1.6",
  159. " 12.1.1.1",
  160. "12.+1.1.4",
  161. "12.1.-1.5",
  162. "257.1.1.9",
  163. "255.1.1.256",
  164. "12.1.1.3 ",
  165. "12.1 foo.1.2.3.4.5.bar",
  166. "12.1 foo.1.2.3.4.5.",
  167. "12.1.1.3 g",
  168. " ",
  169. "",
  170. "12.1.foo.1.2.3.4.5.bar",
  171. "12.",
  172. "12.1.2.",
  173. "12...",
  174. " ",
  175. "7848198702",
  176. "12.1.1.3 abc",
  177. "12.1 .1.2",
  178. "12.1 .zzzz.2",
  179. "12.5.3 foo.67.test.test.7FFFFFFFFFfaFFF789FFFFFFFFFFFFFFF",
  180. "12.1 foo.bar.test.test.baf",
  181. "12.1.4.6 foo.bar.test.test.baf",
  182. "12.3 foo.bar.test.test.4",
  183. "12 foo.bar.test.test.baf",
  184. };
  185. static byte [] ipv4MappedIPv6Prefix = new byte [] { 0,0, 0,0, 0,0, 0,0, 0,0, 0xFF,0xFF };
  186. [Test]
  187. public void PublicFields ()
  188. {
  189. Assert.AreEqual ((long) 0, IPAddress.Any.Address, "#1");
  190. Assert.AreEqual ((long) 0xFFFFFFFF, IPAddress.Broadcast.Address, "#2");
  191. long loopback = IPAddress.HostToNetworkOrder (BitConverter.IsLittleEndian ?
  192. 0x7f000001 :
  193. 0x0100007f);
  194. Assert.AreEqual (loopback, IPAddress.Loopback.Address, "#3");
  195. Assert.AreEqual ((long) 0xFFFFFFFF, IPAddress.None.Address, "#4");
  196. }
  197. [Test]
  198. public void ToStringV4 ()
  199. {
  200. IPAddress ip = IPAddress.Parse ("192.168.1.1");
  201. Assert.AreEqual ("192.168.1.1", ip.ToString (), "#1");
  202. Assert.AreEqual ("0.0.0.0", IPAddress.Any.ToString (), "#2");
  203. Assert.AreEqual ("255.255.255.255", IPAddress.Broadcast.ToString (), "#3");
  204. Assert.AreEqual ("127.0.0.1", IPAddress.Loopback.ToString (), "#4");
  205. Assert.AreEqual ("255.255.255.255", IPAddress.None.ToString (), "#5");
  206. }
  207. [Test]
  208. public void ToStringV6 ()
  209. {
  210. for(int i=0; i<ipv6AddressList.Length/2; i++) {
  211. string addr = IPAddress.Parse (ipv6AddressList[i*2+1]).ToString().ToLower();
  212. Assert.AreEqual (ipv6AddressList[i*2].ToLower(), addr, "ToStringIPv6 #" + i);
  213. }
  214. }
  215. [Test]
  216. public void IsLoopbackV4 ()
  217. {
  218. IPAddress ip;
  219. ip = IPAddress.Parse ("127.0.0.1");
  220. Assert.IsTrue (IPAddress.IsLoopback (ip), "#1");
  221. ip = IPAddress.Any;
  222. Assert.IsFalse (IPAddress.IsLoopback (ip), "#2");
  223. ip = IPAddress.Loopback;
  224. Assert.IsTrue (IPAddress.IsLoopback (ip), "#3");
  225. ip = IPAddress.Parse ("::101");
  226. Assert.IsFalse (IPAddress.IsLoopback (ip), "#4");
  227. }
  228. [Test]
  229. public void IsLoopbackV6 ()
  230. {
  231. // if (!Socket.SupportsIPv6)
  232. // Assert.Ignore ("IPv6 must be enabled in machine.config");
  233. IPAddress ip = IPAddress.IPv6Loopback;
  234. Assert.IsTrue (IPAddress.IsLoopback (ip), "#1");
  235. ip = IPAddress.IPv6None;
  236. Assert.IsFalse (IPAddress.IsLoopback (ip), "#2");
  237. }
  238. [Test]
  239. public void GetAddressBytesV4 ()
  240. {
  241. byte[] dataIn = { 10, 11, 12, 13 };
  242. byte[] dataOut = IPAddress.Parse ("10.11.12.13").GetAddressBytes ();
  243. for (int i = 0; i < dataIn.Length; i++)
  244. Assert.AreEqual (dataOut [i], dataIn [i], "GetAddressBytesV4");
  245. }
  246. [Test]
  247. public void GetAddressBytesV6 ()
  248. {
  249. byte[] dataIn = new byte[]{ 0x01, 0x23, 0x45, 0x67, 0x89, 0x98, 0x76, 0x54, 0x32, 0x10, 0x01, 0x23, 0x45, 0x67, 0x89, 0x98 };
  250. byte[] dataOut = IPAddress.Parse ("123:4567:8998:7654:3210:0123:4567:8998").GetAddressBytes ();
  251. for (int i = 0; i < dataIn.Length; i++)
  252. Assert.AreEqual (dataOut [i], dataIn [i], "GetAddressBytesV6 #1");
  253. dataIn = new byte[]{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0x01 };
  254. dataOut = IPAddress.Parse ("::FFFF:127.0.0.1").GetAddressBytes ();
  255. for (int i = 0; i < dataIn.Length; i++)
  256. Assert.AreEqual (dataOut [i], dataIn [i], "GetAddressBytesV6 #2");
  257. }
  258. [Test]
  259. public void Address ()
  260. {
  261. try {
  262. IPAddress ip1 = new IPAddress (0x0000000100000000);
  263. Assert.Fail ("#1");
  264. } catch (ArgumentOutOfRangeException) {}
  265. IPAddress ip = IPAddress.Parse ("127.0.0.1");
  266. ip.Address = 0;
  267. ip.Address = 0xffffffff;
  268. ip.Address = -1;
  269. ip.Address = 0x0000000100000000;
  270. }
  271. [Test]
  272. public void ParseOkV4 ()
  273. {
  274. for (int i = 0; i < ipv4ParseOk.Length / 2; i++) {
  275. IPAddress ip;
  276. try {
  277. ip = IPAddress.Parse (ipv4ParseOk [i*2]);
  278. Assert.AreEqual (ipv4ParseOk [i * 2 + 1], ip.ToString (), "ParseOkV4:" + i);
  279. } catch (FormatException) {
  280. Assert.Fail ("Cannot parse test i=" + i + ": '" + ipv4ParseOk [i*2] + "'");
  281. }
  282. }
  283. }
  284. [Test]
  285. public void ParseOkV6 ()
  286. {
  287. for (int i = 0; i < ipv6AddressList.Length / 2; i++) {
  288. string source = ipv6AddressList [i*2].ToLower();
  289. IPAddress ip = IPAddress.Parse (source);
  290. Assert.AreEqual (ip.ToString ().ToLower (), source,
  291. string.Format("ParseIPv6 #{0}-1: {1} != {2}", i,
  292. ip.ToString ().ToLower (), source));
  293. ip = IPAddress.Parse (ipv6AddressList [i*2+1].ToLower ());
  294. Assert.AreEqual (ip.ToString ().ToLower (), source,
  295. string.Format("ParseIPv6 #{0}-2: {1} != {2}", i,
  296. ip.ToString ().ToLower (), source));
  297. }
  298. }
  299. [Test]
  300. public void ParseWrongV4 ()
  301. {
  302. for (int i = 0; i < ipv4ParseWrong.Length; i++) {
  303. string ipAddress = ipv4ParseWrong [i];
  304. try {
  305. IPAddress ip = IPAddress.Parse (ipAddress);
  306. Assert.Fail ("#1:" + i + " (" + ipAddress + ")");
  307. } catch (FormatException ex) {
  308. Assert.AreEqual (typeof (FormatException), ex.GetType (), "#2:" + i);
  309. Assert.IsNull (ex.InnerException, "#3:" + i);
  310. Assert.IsNotNull (ex.Message, "#4:" + i);
  311. }
  312. }
  313. }
  314. [Test]
  315. public void Parse_IpString_Null ()
  316. {
  317. try {
  318. IPAddress.Parse ((string) null);
  319. Assert.Fail ("#1");
  320. } catch (ArgumentNullException ex) {
  321. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  322. Assert.IsNull (ex.InnerException, "#3");
  323. Assert.IsNotNull (ex.Message, "#4");
  324. Assert.AreEqual ("ipString", ex.ParamName, "#5");
  325. }
  326. }
  327. [Test]
  328. public void NetworkHost ()
  329. {
  330. long [] tested = new long [] { 0, 1, 1, 1};
  331. long [] expectedLE = new long [] {0, 256, 16777216, 72057594037927936 };
  332. long [] expected;
  333. expected = BitConverter.IsLittleEndian ? expectedLE : tested;
  334. short short0 = IPAddress.NetworkToHostOrder ((short) tested [0]);
  335. Assert.AreEqual ((short) expected [0], short0, "#A1");
  336. short0 = IPAddress.HostToNetworkOrder (short0);
  337. Assert.AreEqual ((short) tested [0], short0, "#A2");
  338. int int0 = IPAddress.NetworkToHostOrder ((int) tested [0]);
  339. Assert.AreEqual ((int) expected [0], int0, "#B1");
  340. int0 = IPAddress.HostToNetworkOrder (int0);
  341. Assert.AreEqual ((int) tested [0], int0, "#B2");
  342. long long0 = IPAddress.NetworkToHostOrder (tested [0]);
  343. Assert.AreEqual (expected [0], long0, "#C1");
  344. long0 = IPAddress.HostToNetworkOrder (long0);
  345. Assert.AreEqual (tested [0], long0, "#C2");
  346. short0 = IPAddress.NetworkToHostOrder ((short) tested [1]);
  347. Assert.AreEqual ((short) expected [1], short0, "#D1");
  348. short0 = IPAddress.HostToNetworkOrder (short0);
  349. Assert.AreEqual ((short) tested [1], short0, "#D2");
  350. int0 = IPAddress.NetworkToHostOrder ((int) tested [2]);
  351. Assert.AreEqual ((int) expected [2], int0, "#E1");
  352. int0 = IPAddress.HostToNetworkOrder (int0);
  353. Assert.AreEqual ((int) tested [2], int0, "#E2");
  354. long0 = IPAddress.NetworkToHostOrder (tested [3]);
  355. Assert.AreEqual (expected [3], long0, "#F1");
  356. long0 = IPAddress.HostToNetworkOrder (long0);
  357. Assert.AreEqual (tested [3], long0, "#F2");
  358. }
  359. [Test]
  360. public void LoopbackIPv6 ()
  361. {
  362. Assert.IsTrue (new Uri("http://[0:0:0:0::127.0.0.1]/").IsLoopback, "#1");
  363. Assert.IsFalse (new Uri ("http://[0:0:0:0::127.1.2.3]/").IsLoopback, "#2");
  364. Assert.IsTrue (new Uri ("http://[0:0:0:0::0.0.0.1]/").IsLoopback, "#3");
  365. }
  366. [Test] // bug #76792
  367. public void Constructor0_Address_4Byte ()
  368. {
  369. byte[] bytes = new byte[4] { 192, 202, 112, 37 };
  370. IPAddress i = new IPAddress (bytes);
  371. Assert.AreEqual (bytes, i.GetAddressBytes (), "#1");
  372. Assert.AreEqual ("192.202.112.37", i.ToString (), "#2");
  373. }
  374. [Test]
  375. public void Constructor0_Address_Invalid ()
  376. {
  377. try {
  378. new IPAddress (new byte [0]);
  379. Assert.Fail ("#A1");
  380. } catch (ArgumentException ex) {
  381. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  382. Assert.IsNull (ex.InnerException, "#A3");
  383. Assert.IsNotNull (ex.Message, "#A4");
  384. Assert.IsFalse (ex.Message == "address", ex.Message, "#A5");
  385. Assert.IsNotNull (ex.ParamName, "#A6");
  386. Assert.AreEqual ("address", ex.ParamName, "#A7");
  387. }
  388. try {
  389. new IPAddress (new byte [3] { 192, 202, 112 });
  390. Assert.Fail ("#B1");
  391. } catch (ArgumentException ex) {
  392. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  393. Assert.IsNull (ex.InnerException, "#B3");
  394. Assert.IsNotNull (ex.Message, "#B4");
  395. Assert.IsFalse (ex.Message == "address", ex.Message, "#B5");
  396. Assert.IsNotNull (ex.ParamName, "#B6");
  397. Assert.AreEqual ("address", ex.ParamName, "#B7");
  398. }
  399. try {
  400. new IPAddress (new byte [5] { 192, 202, 112, 142, 25 });
  401. Assert.Fail ("#C1");
  402. } catch (ArgumentException ex) {
  403. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
  404. Assert.IsNull (ex.InnerException, "#C3");
  405. Assert.IsNotNull (ex.Message, "#C4");
  406. Assert.IsFalse (ex.Message == "address", "#C5");
  407. Assert.IsNotNull (ex.ParamName, "#C6");
  408. Assert.AreEqual ("address", ex.ParamName, "#C7");
  409. }
  410. }
  411. [Test]
  412. public void Constructor0_Address_Null ()
  413. {
  414. try {
  415. new IPAddress ((byte []) null);
  416. Assert.Fail ("#1");
  417. } catch (ArgumentNullException ex) {
  418. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  419. Assert.IsNull (ex.InnerException, "#3");
  420. Assert.IsNotNull (ex.Message, "#4");
  421. Assert.AreEqual ("address", ex.ParamName, "#5");
  422. }
  423. }
  424. [Test]
  425. public void Constructor1_Address_4Byte ()
  426. {
  427. byte [] bytes = new byte [4] { 192, 202, 112, 37 };
  428. try {
  429. new IPAddress (bytes, 0);
  430. Assert.Fail ("#1");
  431. } catch (ArgumentException ex) {
  432. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  433. Assert.IsNull (ex.InnerException, "#3");
  434. Assert.IsNotNull (ex.Message, "#4");
  435. Assert.IsFalse (ex.Message == "address", "#5");
  436. Assert.IsNotNull (ex.ParamName, "#6");
  437. Assert.AreEqual ("address", ex.ParamName, "#7");
  438. }
  439. }
  440. [Test]
  441. public void Constructor1_Address_Null ()
  442. {
  443. try {
  444. new IPAddress ((byte []) null, 5);
  445. Assert.Fail ("#1");
  446. } catch (ArgumentNullException ex) {
  447. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  448. Assert.IsNull (ex.InnerException, "#3");
  449. Assert.IsNotNull (ex.Message, "#4");
  450. Assert.AreEqual ("address", ex.ParamName, "#5");
  451. }
  452. }
  453. [Test]
  454. public void FromBytes1 ()
  455. {
  456. byte[] val1 = new byte[4];
  457. val1[0] = 82;
  458. val1[1] = 165;
  459. val1[2] = 240;
  460. val1[3] = 134;
  461. CompareIPs (val1, "82.165.240.134");
  462. byte[] val2 = new byte[4];
  463. val2[0] = 123;
  464. val2[1] = 124;
  465. val2[2] = 125;
  466. val2[3] = 126;
  467. CompareIPs (val2, "123.124.125.126");
  468. }
  469. void CompareIPs (byte [] bytes, string address)
  470. {
  471. IPAddress ip = new IPAddress (bytes);
  472. IPAddress ip2 = IPAddress.Parse (address);
  473. Assert.IsTrue (ip2.Equals (ip), "#A-" + address);
  474. Assert.IsTrue (ip.Equals (ip2), "#B-" + address);
  475. }
  476. [Test]
  477. public void TryParse_IpString_Null ()
  478. {
  479. IPAddress i;
  480. bool val1 = IPAddress.TryParse ((string) null, out i);
  481. Assert.IsFalse (val1, "#1");
  482. Assert.IsNull (i, "#2");
  483. }
  484. [Test]
  485. public void TryParse ()
  486. {
  487. IPAddress i;
  488. Assert.IsTrue (IPAddress.TryParse ("0.0.0.0", out i), "#1");
  489. Assert.IsTrue (IPAddress.TryParse ("127.0.0.1", out i), "#2");
  490. Assert.IsFalse (IPAddress.TryParse ("www.mono-project.com", out i), "#3");
  491. Assert.IsTrue (IPAddress.TryParse ("0001:0002:0003:0004:0005:0006:0007:0008", out i), "#4");
  492. Assert.IsTrue (IPAddress.TryParse ("1:2:3:4:5:6:7:8", out i), "#5");
  493. Assert.IsTrue (IPAddress.TryParse ("1::8", out i), "#6");
  494. Assert.IsTrue (IPAddress.TryParse ("1::3:4:5:6:7:8", out i), "#7");
  495. Assert.IsFalse (IPAddress.TryParse ("1::2:3:4:5::6:7:8", out i), "#8"); // :: ::
  496. Assert.IsFalse (IPAddress.TryParse ("1::2:3:4:5:6:7:8", out i), "#9");
  497. Assert.IsFalse (IPAddress.TryParse ("1;2:3:4:5:6:7:8", out i), "#10"); // ;
  498. // FIXME:
  499. // Assert.IsFalse (IPAddress.TryParse ("1:2:3:4:5:6:7:8/10", out i), "#11"); // subnet
  500. }
  501. [Test]
  502. public void TryParseOkV4 ()
  503. {
  504. for (int i = 0; i < ipv4ParseOk.Length / 2; i++) {
  505. IPAddress ip;
  506. Assert.IsTrue (IPAddress.TryParse (ipv4ParseOk [i * 2], out ip), "#1:" + i);
  507. Assert.AreEqual (ipv4ParseOk [i * 2 + 1], ip.ToString (), "#2:" + i);
  508. }
  509. }
  510. [Test]
  511. public void TryParseWrongV4 ()
  512. {
  513. for (int i = 0; i < ipv4ParseWrong.Length; i++) {
  514. IPAddress ip;
  515. string ipAddress = ipv4ParseWrong [i];
  516. Assert.IsFalse (IPAddress.TryParse (ipAddress, out ip), "#1:" + i);
  517. Assert.IsNull (ip, "#2:" + i);
  518. }
  519. }
  520. [Test]
  521. public void IsIPv6LinkLocal ()
  522. {
  523. Assert.IsTrue (IPAddress.Parse ("FE80::1").IsIPv6LinkLocal, "#1");
  524. Assert.IsTrue (IPAddress.Parse ("FE81::1").IsIPv6LinkLocal, "#2");
  525. Assert.IsFalse (IPAddress.Parse ("FD81::1").IsIPv6LinkLocal, "#3");
  526. Assert.IsFalse (IPAddress.Parse ("FF80::1").IsIPv6LinkLocal, "#4");
  527. Assert.IsTrue (IPAddress.Parse ("FE91::1").IsIPv6LinkLocal, "#5");
  528. Assert.IsTrue (IPAddress.Parse ("FEA0::1").IsIPv6LinkLocal, "#6");
  529. Assert.IsFalse (IPAddress.Parse ("FEC0::1").IsIPv6LinkLocal, "#7");
  530. }
  531. [Test]
  532. public void IsIPv6SiteLocal ()
  533. {
  534. Assert.IsTrue (IPAddress.Parse ("FEC0::1").IsIPv6SiteLocal, "#1");
  535. Assert.IsTrue (IPAddress.Parse ("FEC1::1").IsIPv6SiteLocal, "#2");
  536. Assert.IsFalse (IPAddress.Parse ("FE81::1").IsIPv6SiteLocal, "#3");
  537. Assert.IsFalse (IPAddress.Parse ("FFC0::1").IsIPv6SiteLocal, "#4");
  538. Assert.IsTrue (IPAddress.Parse ("FED1::1").IsIPv6SiteLocal, "#5");
  539. Assert.IsTrue (IPAddress.Parse ("FEE1::1").IsIPv6SiteLocal, "#6");
  540. }
  541. [Test]
  542. public void IsIPv6Multicast ()
  543. {
  544. Assert.IsTrue (IPAddress.Parse ("FF00::1").IsIPv6Multicast, "#1");
  545. Assert.IsTrue (IPAddress.Parse ("FF01::1").IsIPv6Multicast, "#2");
  546. Assert.IsFalse (IPAddress.Parse ("FE00::1").IsIPv6Multicast, "#3");
  547. }
  548. [Test]
  549. public void IsIPv6Teredo ()
  550. {
  551. Assert.IsTrue (IPAddress.Parse ("2001::1").IsIPv6Teredo, "#1");
  552. Assert.IsFalse (IPAddress.Parse ("2002::1").IsIPv6Teredo, "#2");
  553. }
  554. [Test]
  555. public void ParseWrongV6 ()
  556. {
  557. //if (!Socket.SupportsIPv6)
  558. // Assert.Ignore ("IPv6 must be enabled in machine.config");
  559. for (int i = 0; i < ipv6ParseWrong.Length; i++) {
  560. string ipAddress = ipv6ParseWrong [i];
  561. try {
  562. IPAddress ip = IPAddress.Parse (ipAddress);
  563. Assert.Fail ("#1:" + i + " (" + ipAddress + ")");
  564. } catch (FormatException ex) {
  565. Assert.AreEqual (typeof (FormatException), ex.GetType (), "#2:" + i);
  566. Assert.AreEqual(typeof(SocketException), ex.InnerException.GetType (), "#3:" + i);
  567. Assert.IsNotNull (ex.Message, "#4:" + i);
  568. }
  569. }
  570. }
  571. [Test]
  572. public void MapToIPv6 ()
  573. {
  574. for (int i = 0; i < ipv4ParseOk.Length / 2; i++) {
  575. IPAddress v4 = IPAddress.Parse (ipv4ParseOk [i * 2]);
  576. byte [] v4bytes = v4.GetAddressBytes ();
  577. IPAddress v6 = v4.MapToIPv6 ();
  578. byte [] v6bytes = v6.GetAddressBytes ();
  579. IPAddress v4back = v6.MapToIPv4 ();
  580. Assert.IsTrue (StartsWith (v6bytes, ipv4MappedIPv6Prefix), "MapToIPv6 #" + i + ".1");
  581. Assert.IsTrue (v6bytes [12] == v4bytes [0], "MapToIPv6 #" + i + ".2");
  582. Assert.IsTrue (v6bytes [13] == v4bytes [1], "MapToIPv6 #" + i + ".3");
  583. Assert.IsTrue (v6bytes [14] == v4bytes [2], "MapToIPv6 #" + i + ".4");
  584. Assert.IsTrue (v6bytes [15] == v4bytes [3], "MapToIPv6 #" + i + ".5");
  585. Assert.IsTrue (v4.Equals (v4back), "MapToIPv4 #" + i);
  586. }
  587. //TODO: Test using MapToIPv4/6 with anything other than IPv4/6 addresses.
  588. //Currently it is not possible to do with the IPAddress implementation.
  589. }
  590. static bool StartsWith (byte [] a, byte [] b)
  591. {
  592. if (a.Length < b.Length)
  593. return false;
  594. for (int i = 0; i < b.Length; i++)
  595. {
  596. if (a [i] != b [i])
  597. return false;
  598. }
  599. return true;
  600. }
  601. [Test]
  602. public void EqualsFromBytes ()
  603. {
  604. for (int i = 0; i < ipv4ParseOk.Length / 2; i++) {
  605. IPAddress ip = IPAddress.Parse (ipv4ParseOk [i * 2]);
  606. IPAddress ipFromBytes = new IPAddress (ip.GetAddressBytes ());
  607. Assert.IsTrue (ip.Equals (ipFromBytes), "EqualsFromBytes #" + i);
  608. }
  609. }
  610. [Test]
  611. [Category ("NotDotNet")]
  612. public void UnixInterfaceNameAsZoneIndex ()
  613. {
  614. var ip = IPAddress.Parse ("fe80::bae8:56ff:fe47:af7e%en0");
  615. // Should be en0 but it's of long type!
  616. Assert.AreEqual (0, ip.ScopeId);
  617. Assert.AreEqual ("fe80::bae8:56ff:fe47:af7e", ip.ToString ());
  618. }
  619. }
  620. }