IPAddressTest.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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. IPAddress ip = IPAddress.IPv6Loopback;
  232. Assert.IsTrue (IPAddress.IsLoopback (ip), "#1");
  233. ip = IPAddress.IPv6None;
  234. Assert.IsFalse (IPAddress.IsLoopback (ip), "#2");
  235. }
  236. [Test]
  237. public void GetAddressBytesV4 ()
  238. {
  239. byte[] dataIn = { 10, 11, 12, 13 };
  240. byte[] dataOut = IPAddress.Parse ("10.11.12.13").GetAddressBytes ();
  241. for (int i = 0; i < dataIn.Length; i++)
  242. Assert.AreEqual (dataOut [i], dataIn [i], "GetAddressBytesV4");
  243. }
  244. [Test]
  245. public void GetAddressBytesV6 ()
  246. {
  247. byte[] dataIn = new byte[]{ 0x01, 0x23, 0x45, 0x67, 0x89, 0x98, 0x76, 0x54, 0x32, 0x10, 0x01, 0x23, 0x45, 0x67, 0x89, 0x98 };
  248. byte[] dataOut = IPAddress.Parse ("123:4567:8998:7654:3210:0123:4567:8998").GetAddressBytes ();
  249. for (int i = 0; i < dataIn.Length; i++)
  250. Assert.AreEqual (dataOut [i], dataIn [i], "GetAddressBytesV6 #1");
  251. dataIn = new byte[]{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0x01 };
  252. dataOut = IPAddress.Parse ("::FFFF:127.0.0.1").GetAddressBytes ();
  253. for (int i = 0; i < dataIn.Length; i++)
  254. Assert.AreEqual (dataOut [i], dataIn [i], "GetAddressBytesV6 #2");
  255. }
  256. [Test]
  257. public void Address ()
  258. {
  259. try {
  260. IPAddress ip1 = new IPAddress (0x0000000100000000);
  261. Assert.Fail ("#1");
  262. } catch (ArgumentOutOfRangeException) {}
  263. IPAddress ip = IPAddress.Parse ("127.0.0.1");
  264. ip.Address = 0;
  265. ip.Address = 0xffffffff;
  266. ip.Address = -1;
  267. ip.Address = 0x0000000100000000;
  268. }
  269. [Test]
  270. public void ParseOkV4 ()
  271. {
  272. for (int i = 0; i < ipv4ParseOk.Length / 2; i++) {
  273. IPAddress ip;
  274. try {
  275. ip = IPAddress.Parse (ipv4ParseOk [i*2]);
  276. Assert.AreEqual (ipv4ParseOk [i * 2 + 1], ip.ToString (), "ParseOkV4:" + i);
  277. } catch (FormatException) {
  278. Assert.Fail ("Cannot parse test i=" + i + ": '" + ipv4ParseOk [i*2] + "'");
  279. }
  280. }
  281. }
  282. [Test]
  283. public void ParseOkV6 ()
  284. {
  285. for (int i = 0; i < ipv6AddressList.Length / 2; i++) {
  286. string source = ipv6AddressList [i*2].ToLower();
  287. IPAddress ip = IPAddress.Parse (source);
  288. Assert.AreEqual (ip.ToString ().ToLower (), source,
  289. string.Format("ParseIPv6 #{0}-1: {1} != {2}", i,
  290. ip.ToString ().ToLower (), source));
  291. ip = IPAddress.Parse (ipv6AddressList [i*2+1].ToLower ());
  292. Assert.AreEqual (ip.ToString ().ToLower (), source,
  293. string.Format("ParseIPv6 #{0}-2: {1} != {2}", i,
  294. ip.ToString ().ToLower (), source));
  295. }
  296. }
  297. [Test]
  298. public void ParseWrongV4 ()
  299. {
  300. for (int i = 0; i < ipv4ParseWrong.Length; i++) {
  301. string ipAddress = ipv4ParseWrong [i];
  302. try {
  303. IPAddress ip = IPAddress.Parse (ipAddress);
  304. Assert.Fail ("#1:" + i + " (" + ipAddress + ")");
  305. } catch (FormatException ex) {
  306. Assert.AreEqual (typeof (FormatException), ex.GetType (), "#2:" + i);
  307. Assert.IsNotNull (ex.InnerException, "#3:" + i);
  308. Assert.IsNotNull (ex.Message, "#4:" + i);
  309. }
  310. }
  311. }
  312. [Test]
  313. public void Parse_IpString_Null ()
  314. {
  315. try {
  316. IPAddress.Parse ((string) null);
  317. Assert.Fail ("#1");
  318. } catch (ArgumentNullException ex) {
  319. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  320. Assert.IsNull (ex.InnerException, "#3");
  321. Assert.IsNotNull (ex.Message, "#4");
  322. Assert.AreEqual ("ipString", ex.ParamName, "#5");
  323. }
  324. }
  325. [Test]
  326. public void NetworkHost ()
  327. {
  328. long [] tested = new long [] { 0, 1, 1, 1};
  329. long [] expectedLE = new long [] {0, 256, 16777216, 72057594037927936 };
  330. long [] expected;
  331. expected = BitConverter.IsLittleEndian ? expectedLE : tested;
  332. short short0 = IPAddress.NetworkToHostOrder ((short) tested [0]);
  333. Assert.AreEqual ((short) expected [0], short0, "#A1");
  334. short0 = IPAddress.HostToNetworkOrder (short0);
  335. Assert.AreEqual ((short) tested [0], short0, "#A2");
  336. int int0 = IPAddress.NetworkToHostOrder ((int) tested [0]);
  337. Assert.AreEqual ((int) expected [0], int0, "#B1");
  338. int0 = IPAddress.HostToNetworkOrder (int0);
  339. Assert.AreEqual ((int) tested [0], int0, "#B2");
  340. long long0 = IPAddress.NetworkToHostOrder (tested [0]);
  341. Assert.AreEqual (expected [0], long0, "#C1");
  342. long0 = IPAddress.HostToNetworkOrder (long0);
  343. Assert.AreEqual (tested [0], long0, "#C2");
  344. short0 = IPAddress.NetworkToHostOrder ((short) tested [1]);
  345. Assert.AreEqual ((short) expected [1], short0, "#D1");
  346. short0 = IPAddress.HostToNetworkOrder (short0);
  347. Assert.AreEqual ((short) tested [1], short0, "#D2");
  348. int0 = IPAddress.NetworkToHostOrder ((int) tested [2]);
  349. Assert.AreEqual ((int) expected [2], int0, "#E1");
  350. int0 = IPAddress.HostToNetworkOrder (int0);
  351. Assert.AreEqual ((int) tested [2], int0, "#E2");
  352. long0 = IPAddress.NetworkToHostOrder (tested [3]);
  353. Assert.AreEqual (expected [3], long0, "#F1");
  354. long0 = IPAddress.HostToNetworkOrder (long0);
  355. Assert.AreEqual (tested [3], long0, "#F2");
  356. }
  357. [Test]
  358. public void LoopbackIPv6 ()
  359. {
  360. Assert.IsTrue (new Uri("http://[0:0:0:0::127.0.0.1]/").IsLoopback, "#1");
  361. Assert.IsFalse (new Uri ("http://[0:0:0:0::127.1.2.3]/").IsLoopback, "#2");
  362. Assert.IsTrue (new Uri ("http://[0:0:0:0::0.0.0.1]/").IsLoopback, "#3");
  363. }
  364. [Test] // bug #76792
  365. public void Constructor0_Address_4Byte ()
  366. {
  367. byte[] bytes = new byte[4] { 192, 202, 112, 37 };
  368. IPAddress i = new IPAddress (bytes);
  369. Assert.AreEqual (bytes, i.GetAddressBytes (), "#1");
  370. Assert.AreEqual ("192.202.112.37", i.ToString (), "#2");
  371. }
  372. [Test]
  373. public void Constructor0_Address_Invalid ()
  374. {
  375. try {
  376. new IPAddress (new byte [0]);
  377. Assert.Fail ("#A1");
  378. } catch (ArgumentException ex) {
  379. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  380. Assert.IsNull (ex.InnerException, "#A3");
  381. Assert.IsNotNull (ex.Message, "#A4");
  382. Assert.IsFalse (ex.Message == "address", ex.Message, "#A5");
  383. Assert.IsNotNull (ex.ParamName, "#A6");
  384. Assert.AreEqual ("address", ex.ParamName, "#A7");
  385. }
  386. try {
  387. new IPAddress (new byte [3] { 192, 202, 112 });
  388. Assert.Fail ("#B1");
  389. } catch (ArgumentException ex) {
  390. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  391. Assert.IsNull (ex.InnerException, "#B3");
  392. Assert.IsNotNull (ex.Message, "#B4");
  393. Assert.IsFalse (ex.Message == "address", ex.Message, "#B5");
  394. Assert.IsNotNull (ex.ParamName, "#B6");
  395. Assert.AreEqual ("address", ex.ParamName, "#B7");
  396. }
  397. try {
  398. new IPAddress (new byte [5] { 192, 202, 112, 142, 25 });
  399. Assert.Fail ("#C1");
  400. } catch (ArgumentException ex) {
  401. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
  402. Assert.IsNull (ex.InnerException, "#C3");
  403. Assert.IsNotNull (ex.Message, "#C4");
  404. Assert.IsFalse (ex.Message == "address", "#C5");
  405. Assert.IsNotNull (ex.ParamName, "#C6");
  406. Assert.AreEqual ("address", ex.ParamName, "#C7");
  407. }
  408. }
  409. [Test]
  410. public void Constructor0_Address_Null ()
  411. {
  412. try {
  413. new IPAddress ((byte []) null);
  414. Assert.Fail ("#1");
  415. } catch (ArgumentNullException ex) {
  416. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  417. Assert.IsNull (ex.InnerException, "#3");
  418. Assert.IsNotNull (ex.Message, "#4");
  419. Assert.AreEqual ("address", ex.ParamName, "#5");
  420. }
  421. }
  422. [Test]
  423. public void Constructor1_Address_4Byte ()
  424. {
  425. byte [] bytes = new byte [4] { 192, 202, 112, 37 };
  426. try {
  427. new IPAddress (bytes, 0);
  428. Assert.Fail ("#1");
  429. } catch (ArgumentException ex) {
  430. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  431. Assert.IsNull (ex.InnerException, "#3");
  432. Assert.IsNotNull (ex.Message, "#4");
  433. Assert.IsFalse (ex.Message == "address", "#5");
  434. Assert.IsNotNull (ex.ParamName, "#6");
  435. Assert.AreEqual ("address", ex.ParamName, "#7");
  436. }
  437. }
  438. [Test]
  439. public void Constructor1_Address_Null ()
  440. {
  441. try {
  442. new IPAddress ((byte []) null, 5);
  443. Assert.Fail ("#1");
  444. } catch (ArgumentNullException ex) {
  445. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  446. Assert.IsNull (ex.InnerException, "#3");
  447. Assert.IsNotNull (ex.Message, "#4");
  448. Assert.AreEqual ("address", ex.ParamName, "#5");
  449. }
  450. }
  451. [Test]
  452. public void FromBytes1 ()
  453. {
  454. byte[] val1 = new byte[4];
  455. val1[0] = 82;
  456. val1[1] = 165;
  457. val1[2] = 240;
  458. val1[3] = 134;
  459. CompareIPs (val1, "82.165.240.134");
  460. byte[] val2 = new byte[4];
  461. val2[0] = 123;
  462. val2[1] = 124;
  463. val2[2] = 125;
  464. val2[3] = 126;
  465. CompareIPs (val2, "123.124.125.126");
  466. }
  467. void CompareIPs (byte [] bytes, string address)
  468. {
  469. IPAddress ip = new IPAddress (bytes);
  470. IPAddress ip2 = IPAddress.Parse (address);
  471. Assert.IsTrue (ip2.Equals (ip), "#A-" + address);
  472. Assert.IsTrue (ip.Equals (ip2), "#B-" + address);
  473. }
  474. [Test]
  475. public void TryParse_IpString_Null ()
  476. {
  477. IPAddress i;
  478. bool val1 = IPAddress.TryParse ((string) null, out i);
  479. Assert.IsFalse (val1, "#1");
  480. Assert.IsNull (i, "#2");
  481. }
  482. [Test]
  483. public void TryParse ()
  484. {
  485. IPAddress i;
  486. Assert.IsTrue (IPAddress.TryParse ("0.0.0.0", out i), "#1");
  487. Assert.IsTrue (IPAddress.TryParse ("127.0.0.1", out i), "#2");
  488. Assert.IsFalse (IPAddress.TryParse ("www.mono-project.com", out i), "#3");
  489. Assert.IsTrue (IPAddress.TryParse ("0001:0002:0003:0004:0005:0006:0007:0008", out i), "#4");
  490. Assert.IsTrue (IPAddress.TryParse ("1:2:3:4:5:6:7:8", out i), "#5");
  491. Assert.IsTrue (IPAddress.TryParse ("1::8", out i), "#6");
  492. Assert.IsTrue (IPAddress.TryParse ("1::3:4:5:6:7:8", out i), "#7");
  493. Assert.IsFalse (IPAddress.TryParse ("1::2:3:4:5::6:7:8", out i), "#8"); // :: ::
  494. Assert.IsFalse (IPAddress.TryParse ("1::2:3:4:5:6:7:8", out i), "#9");
  495. Assert.IsFalse (IPAddress.TryParse ("1;2:3:4:5:6:7:8", out i), "#10"); // ;
  496. // FIXME:
  497. // Assert.IsFalse (IPAddress.TryParse ("1:2:3:4:5:6:7:8/10", out i), "#11"); // subnet
  498. }
  499. [Test]
  500. public void TryParseOkV4 ()
  501. {
  502. for (int i = 0; i < ipv4ParseOk.Length / 2; i++) {
  503. IPAddress ip;
  504. Assert.IsTrue (IPAddress.TryParse (ipv4ParseOk [i * 2], out ip), "#1:" + i);
  505. Assert.AreEqual (ipv4ParseOk [i * 2 + 1], ip.ToString (), "#2:" + i);
  506. }
  507. }
  508. [Test]
  509. public void TryParseWrongV4 ()
  510. {
  511. for (int i = 0; i < ipv4ParseWrong.Length; i++) {
  512. IPAddress ip;
  513. string ipAddress = ipv4ParseWrong [i];
  514. Assert.IsFalse (IPAddress.TryParse (ipAddress, out ip), "#1:" + i);
  515. Assert.IsNull (ip, "#2:" + i);
  516. }
  517. }
  518. [Test]
  519. public void IsIPv6LinkLocal ()
  520. {
  521. Assert.IsTrue (IPAddress.Parse ("FE80::1").IsIPv6LinkLocal, "#1");
  522. Assert.IsTrue (IPAddress.Parse ("FE81::1").IsIPv6LinkLocal, "#2");
  523. Assert.IsFalse (IPAddress.Parse ("FD81::1").IsIPv6LinkLocal, "#3");
  524. Assert.IsFalse (IPAddress.Parse ("FF80::1").IsIPv6LinkLocal, "#4");
  525. Assert.IsTrue (IPAddress.Parse ("FE91::1").IsIPv6LinkLocal, "#5");
  526. Assert.IsTrue (IPAddress.Parse ("FEA0::1").IsIPv6LinkLocal, "#6");
  527. Assert.IsFalse (IPAddress.Parse ("FEC0::1").IsIPv6LinkLocal, "#7");
  528. }
  529. [Test]
  530. public void IsIPv6SiteLocal ()
  531. {
  532. Assert.IsTrue (IPAddress.Parse ("FEC0::1").IsIPv6SiteLocal, "#1");
  533. Assert.IsTrue (IPAddress.Parse ("FEC1::1").IsIPv6SiteLocal, "#2");
  534. Assert.IsFalse (IPAddress.Parse ("FE81::1").IsIPv6SiteLocal, "#3");
  535. Assert.IsFalse (IPAddress.Parse ("FFC0::1").IsIPv6SiteLocal, "#4");
  536. Assert.IsTrue (IPAddress.Parse ("FED1::1").IsIPv6SiteLocal, "#5");
  537. Assert.IsTrue (IPAddress.Parse ("FEE1::1").IsIPv6SiteLocal, "#6");
  538. }
  539. [Test]
  540. public void IsIPv6Multicast ()
  541. {
  542. Assert.IsTrue (IPAddress.Parse ("FF00::1").IsIPv6Multicast, "#1");
  543. Assert.IsTrue (IPAddress.Parse ("FF01::1").IsIPv6Multicast, "#2");
  544. Assert.IsFalse (IPAddress.Parse ("FE00::1").IsIPv6Multicast, "#3");
  545. }
  546. [Test]
  547. public void IsIPv6Teredo ()
  548. {
  549. Assert.IsTrue (IPAddress.Parse ("2001::1").IsIPv6Teredo, "#1");
  550. Assert.IsFalse (IPAddress.Parse ("2002::1").IsIPv6Teredo, "#2");
  551. }
  552. [Test]
  553. public void ParseWrongV6 ()
  554. {
  555. for (int i = 0; i < ipv6ParseWrong.Length; i++) {
  556. string ipAddress = ipv6ParseWrong [i];
  557. try {
  558. IPAddress ip = IPAddress.Parse (ipAddress);
  559. Assert.Fail ("#1:" + i + " (" + ipAddress + ")");
  560. } catch (FormatException ex) {
  561. Assert.AreEqual (typeof (FormatException), ex.GetType (), "#2:" + i);
  562. Assert.AreEqual(typeof(SocketException), ex.InnerException.GetType (), "#3:" + i);
  563. Assert.IsNotNull (ex.Message, "#4:" + i);
  564. }
  565. }
  566. }
  567. [Test]
  568. public void MapToIPv6 ()
  569. {
  570. for (int i = 0; i < ipv4ParseOk.Length / 2; i++) {
  571. IPAddress v4 = IPAddress.Parse (ipv4ParseOk [i * 2]);
  572. byte [] v4bytes = v4.GetAddressBytes ();
  573. IPAddress v6 = v4.MapToIPv6 ();
  574. byte [] v6bytes = v6.GetAddressBytes ();
  575. IPAddress v4back = v6.MapToIPv4 ();
  576. Assert.IsTrue (StartsWith (v6bytes, ipv4MappedIPv6Prefix), "MapToIPv6 #" + i + ".1");
  577. Assert.IsTrue (v6bytes [12] == v4bytes [0], "MapToIPv6 #" + i + ".2");
  578. Assert.IsTrue (v6bytes [13] == v4bytes [1], "MapToIPv6 #" + i + ".3");
  579. Assert.IsTrue (v6bytes [14] == v4bytes [2], "MapToIPv6 #" + i + ".4");
  580. Assert.IsTrue (v6bytes [15] == v4bytes [3], "MapToIPv6 #" + i + ".5");
  581. Assert.IsTrue (v4.Equals (v4back), "MapToIPv4 #" + i);
  582. }
  583. //TODO: Test using MapToIPv4/6 with anything other than IPv4/6 addresses.
  584. //Currently it is not possible to do with the IPAddress implementation.
  585. }
  586. static bool StartsWith (byte [] a, byte [] b)
  587. {
  588. if (a.Length < b.Length)
  589. return false;
  590. for (int i = 0; i < b.Length; i++)
  591. {
  592. if (a [i] != b [i])
  593. return false;
  594. }
  595. return true;
  596. }
  597. [Test]
  598. public void EqualsFromBytes ()
  599. {
  600. for (int i = 0; i < ipv4ParseOk.Length / 2; i++) {
  601. IPAddress ip = IPAddress.Parse (ipv4ParseOk [i * 2]);
  602. IPAddress ipFromBytes = new IPAddress (ip.GetAddressBytes ());
  603. Assert.IsTrue (ip.Equals (ipFromBytes), "EqualsFromBytes #" + i);
  604. }
  605. }
  606. }
  607. }