IPAddressTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. "0:7:7:7:7:7:7:7", "0:7:7:7:7:7:7:7",
  29. "E::1", "E:0:0:0:0:0:0:1",
  30. "E::2:2", "E:0:0:0:0:0:2:2",
  31. "E:0:6:6:6:6:6:6", "E:0:6:6:6:6:6:6",
  32. "E:E::1", "E:E:0:0:0:0:0:1",
  33. "E:E::2:2", "E:E:0:0:0:0:2:2",
  34. "E:E:0:5:5:5:5:5", "E:E:0:5:5:5:5:5",
  35. "E:E:E::1", "E:E:E:0:0:0:0:1",
  36. "E:E:E::2:2", "E:E:E:0:0:0:2:2",
  37. "E:E:E:0:4:4:4:4", "E:E:E:0:4:4:4:4",
  38. "E:E:E:E::1", "E:E:E:E:0:0:0:1",
  39. "E:E:E:E::2:2", "E:E:E:E:0:0:2:2",
  40. "E:E:E:E:0:3:3:3", "E:E:E:E:0:3:3:3",
  41. "E:E:E:E:E::1", "E:E:E:E:E:0:0:1",
  42. "E:E:E:E:E:0:2:2", "E:E:E:E:E:0:2:2",
  43. "E:E:E:E:E:E:0:1", "E:E:E:E:E:E:0:1",
  44. "::0.2.0.2", "0:0:0:0:0:0:2:2",
  45. "::FFFF:192.168.0.1", "::FFFF:192.168.0.1",
  46. "::FFFF:0.168.0.1", "::FFFF:0.168.0.1",
  47. "::FFFF", "::0.0.255.255",
  48. "::10.0.0.1", "::10.0.0.1",
  49. "1234::1234:0:0", "1234:0:0:0:0:1234:0:0",
  50. "1:0:1:0:1:0:1:0", "1:0:1:0:1:0:1:0",
  51. "1:1:1::1:1:0", "1:1:1:0:0:1:1:0",
  52. "::1234:0:0", "0:0:0:0:0:1234:0:0",
  53. "3ffe:38e1::100:1:1", "3ffe:38e1::0100:1:0001",
  54. "0:0:1:2::", "0:0:1:2:00:00:000:0000",
  55. "100:0:1:2::abcd", "100:0:1:2:0:0:000:abcd",
  56. "ffff::abcd", "ffff:0:0:0:0:0:00:abcd",
  57. "ffff:0:0:2::abcd", "ffff:0:0:2:0:0:00:abcd",
  58. "0:0:1:2::", "0:0:1:2:0:00:0000:0000",
  59. "::1:0:0", "0000:0000::1:0000:0000",
  60. "::111:234:5:6:789a:0", "0:0:111:234:5:6:789A:0",
  61. "11:22:33:44:55:66:77:8", "11:22:33:44:55:66:77:8",
  62. "0:0:7711:ab42:1230::", "::7711:ab42:1230:0:0:0",
  63. };
  64. static string[] ipv4ParseOk = new string[] {
  65. "192.168.1.1", "192.168.1.1",
  66. "0xff.0x7f.0x20.0x01", "255.127.32.1",
  67. "0xff.0x7f.0x20.0xf", "255.127.32.15",
  68. "0.0.0.0", IPAddress.Any.ToString(),
  69. "255.255.255.255", IPAddress.Broadcast.ToString(),
  70. "12.1.1.3 ", "12.1.1.3",
  71. "12.1 .1.2", "12.0.0.1",
  72. "12.1.7", "12.1.0.7",
  73. "12", "0.0.0.12",
  74. "12.1 foo.1.2.3.4.5.bar", "12.0.0.1",
  75. " ", "0.0.0.0"
  76. };
  77. static object[] ipv4ParseWrong = new object[] {
  78. " foo", typeof(FormatException),
  79. "12.. .", typeof(FormatException),
  80. "12.1.2. ", typeof(FormatException),
  81. "12.1.8. ", typeof(FormatException),
  82. ".1.1.6", typeof(FormatException),
  83. " 12.1.1.1", typeof(FormatException),
  84. "12.+1.1.4", typeof(FormatException),
  85. "12.1.-1.5", typeof(FormatException),
  86. "257.1.1.9", typeof(FormatException),
  87. "12.", typeof(FormatException),
  88. "12.1.2.", typeof(FormatException),
  89. "12...", typeof(FormatException),
  90. null, typeof(ArgumentNullException),
  91. };
  92. [Test]
  93. public void PublicFields ()
  94. {
  95. Assertion.AssertEquals ("Any", IPAddress.Any.Address, (long) 0);
  96. Assertion.AssertEquals ("Broadcast", IPAddress.Broadcast.Address, (long) 0xFFFFFFFF);
  97. long loopback = IPAddress.HostToNetworkOrder (BitConverter.IsLittleEndian ?
  98. 0x7f000001 :
  99. 0x0100007f);
  100. Assertion.AssertEquals ("Loopback", IPAddress.Loopback.Address, loopback);
  101. Assertion.AssertEquals ("None", IPAddress.None.Address, (long) 0xFFFFFFFF);
  102. }
  103. [Test]
  104. public void ToStringV4 ()
  105. {
  106. IPAddress ip = IPAddress.Parse ("192.168.1.1");
  107. Assertion.AssertEquals ("ToString #1", "192.168.1.1", ip.ToString ());
  108. Assertion.AssertEquals ("ToString #2", "0.0.0.0", IPAddress.Any.ToString ());
  109. Assertion.AssertEquals ("ToString #3", "255.255.255.255", IPAddress.Broadcast.ToString ());
  110. Assertion.AssertEquals ("ToString #4", "127.0.0.1", IPAddress.Loopback.ToString ());
  111. Assertion.AssertEquals ("ToString #5", "255.255.255.255", IPAddress.None.ToString ());
  112. }
  113. #if NET_1_1
  114. [Test]
  115. public void ToStringV6 ()
  116. {
  117. if (Socket.SupportsIPv6) {
  118. for(int i=0; i<ipv6AddressList.Length/2; i++) {
  119. string addr = IPAddress.Parse (ipv6AddressList[i*2+1]).ToString().ToLower();
  120. Assertion.AssertEquals ("ToStringIPv6 #" + i, ipv6AddressList[i*2].ToLower(), addr);
  121. }
  122. } else
  123. Assert.Ignore ("IPv6 must be enabled in machine.config");
  124. }
  125. #endif
  126. [Test]
  127. public void IsLoopbackV4 ()
  128. {
  129. IPAddress ip = IPAddress.Parse ("127.0.0.1");
  130. Assertion.AssertEquals ("IsLoopback #1", true, IPAddress.IsLoopback (ip));
  131. try {
  132. ip = IPAddress.Parse ("::101");
  133. Assertion.Fail ("#2 should have thrown a FormatException");
  134. } catch {
  135. }
  136. ip = IPAddress.Any;
  137. Assertion.AssertEquals ("IsLoopback #5", false, IPAddress.IsLoopback (ip));
  138. ip = IPAddress.Loopback;
  139. Assertion.AssertEquals ("IsLoopback #6", true, IPAddress.IsLoopback (ip));
  140. }
  141. #if NET_1_1
  142. [Test]
  143. public void IsLoopbackV6 ()
  144. {
  145. if (Socket.SupportsIPv6) {
  146. IPAddress ip = IPAddress.IPv6Loopback;
  147. Assertion.AssertEquals ("IsLoopback #3", true, IPAddress.IsLoopback (ip));
  148. ip = IPAddress.IPv6None;
  149. Assertion.AssertEquals ("IsLoopback #7", false, IPAddress.IsLoopback (ip));
  150. } else
  151. Assert.Ignore ("IPv6 must be enabled in machine.config");
  152. }
  153. [Test]
  154. public void GetAddressBytesV4 ()
  155. {
  156. byte[] dataIn = { 10, 11, 12, 13 };
  157. byte[] dataOut = IPAddress.Parse ("10.11.12.13").GetAddressBytes ();
  158. for(int i=0; i<dataIn.Length; i++)
  159. Assertion.AssertEquals ("GetAddressBytes #1", dataIn[i], dataOut[i]);
  160. }
  161. [Test]
  162. public void GetAddressBytesV6 ()
  163. {
  164. if (!Socket.SupportsIPv6) {
  165. Assert.Ignore ("IPv6 must be enabled in machine.config");
  166. return;
  167. }
  168. byte[] dataIn = new byte[]{ 0x01, 0x23, 0x45, 0x67, 0x89, 0x98, 0x76, 0x54, 0x32, 0x10, 0x01, 0x23, 0x45, 0x67, 0x89, 0x98 };
  169. byte[] dataOut = IPAddress.Parse ("123:4567:8998:7654:3210:0123:4567:8998").GetAddressBytes ();
  170. for(int i=0; i<dataIn.Length; i++)
  171. Assertion.AssertEquals ("GetAddressBytes #2", dataIn[i], dataOut[i]);
  172. dataIn = new byte[]{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0x01 };
  173. dataOut = IPAddress.Parse ("::FFFF:127.0.0.1").GetAddressBytes ();
  174. for(int i=0; i<dataIn.Length; i++)
  175. Assertion.AssertEquals ("GetAddressBytes #3", dataIn[i], dataOut[i]);
  176. }
  177. #endif
  178. [Test]
  179. public void Address ()
  180. {
  181. // hm, lame, anything is accepted by ms.net
  182. /*
  183. try {
  184. IPAddress ip1 = new IPAddress (0x0000000100000000);
  185. Assertion.Fail ("#1");
  186. } catch (ArgumentOutOfRangeException) {}
  187. IPAddress ip = IPAddress.Parse ("127.0.0.1");
  188. ip.Address = 0;
  189. ip.Address = 0xffffffff;
  190. try {
  191. ip.Address = -1;
  192. Assertion.Fail ("#2");
  193. } catch (ArgumentOutOfRangeException) {}
  194. try {
  195. ip.Address = 0x0000000100000000;
  196. Assertion.Fail ("#3");
  197. } catch (ArgumentOutOfRangeException) {}
  198. */
  199. }
  200. [Test]
  201. public void ParseOkV4 ()
  202. {
  203. for(int i=0; i<ipv4ParseOk.Length / 2; i++) {
  204. IPAddress ip;
  205. try
  206. {
  207. ip = IPAddress.Parse (ipv4ParseOk [i*2]);
  208. Assertion.Assert ("ParseIPv4 #" + i, ip.ToString () == ipv4ParseOk [i*2+1]);
  209. }
  210. catch
  211. {
  212. Assertion.Fail ("Cannot parse test i=" + i + ": '" + ipv4ParseOk [i*2] + "'");
  213. }
  214. }
  215. }
  216. #if NET_1_1
  217. [Test]
  218. public void ParseOkV6 ()
  219. {
  220. if (!Socket.SupportsIPv6) {
  221. Assert.Ignore ("IPv6 must be enabled in machine.config");
  222. return;
  223. }
  224. for(int i=0; i<ipv6AddressList.Length / 2; i++) {
  225. string source = ipv6AddressList [i*2].ToLower();
  226. IPAddress ip = IPAddress.Parse (source);
  227. Assertion.Assert (string.Format("ParseIPv6 #{0}-1: {1} != {2}", i,
  228. ip.ToString ().ToLower (), source), ip.ToString ().ToLower () == source);
  229. ip = IPAddress.Parse (ipv6AddressList [i*2+1].ToLower ());
  230. Assertion.Assert (string.Format("ParseIPv6 #{0}-2: {1} != {2}", i,
  231. ip.ToString ().ToLower (), source), ip.ToString ().ToLower () == source);
  232. }
  233. }
  234. #endif
  235. [Test]
  236. public void ParseWrong ()
  237. {
  238. for(int i=0; i<ipv4ParseWrong.Length/2; i++) {
  239. Type exception = ipv4ParseWrong[i*2+1] as Type;
  240. string ipAddress = ipv4ParseWrong[i*2] as string;
  241. try {
  242. IPAddress ip = IPAddress.Parse (ipAddress);
  243. Assertion.Fail ("IPv4: Should raise a " + exception + " #" + i);
  244. }
  245. catch (Exception e) {
  246. if(!e.GetType ().Equals (exception))
  247. Assertion.Fail ("ParseWrongIPv4 #" + i + ": " + e.ToString());
  248. }
  249. }
  250. }
  251. [Test]
  252. public void NetworkHost ()
  253. {
  254. long [] tested = new long [] { 0, 1, 1, 1};
  255. long [] expectedLE = new long [] {0, 256, 16777216, 72057594037927936 };
  256. long [] expected;
  257. expected = BitConverter.IsLittleEndian ? expectedLE : tested;
  258. short short0 = IPAddress.NetworkToHostOrder ((short) tested [0]);
  259. Assertion.AssertEquals ("NetworkToHostOrder #1", short0, (short) expected [0]);
  260. short0 = IPAddress.HostToNetworkOrder (short0);
  261. Assertion.AssertEquals ("HostToNetworkOrder #1", short0, (short) tested [0]);
  262. int int0 = IPAddress.NetworkToHostOrder ((int) tested [0]);
  263. Assertion.AssertEquals ("NetworkToHostOrder #2", int0, (int) expected [0]);
  264. int0 = IPAddress.HostToNetworkOrder (int0);
  265. Assertion.AssertEquals ("HostToNetworkOrder #2", int0, (int) tested [0]);
  266. long long0 = IPAddress.NetworkToHostOrder (tested [0]);
  267. Assertion.AssertEquals ("NetworkToHostOrder #3", long0, expected [0]);
  268. long0 = IPAddress.HostToNetworkOrder (long0);
  269. Assertion.AssertEquals ("HostToNetworkOrder #3", long0, tested [0]);
  270. short0 = IPAddress.NetworkToHostOrder ((short) tested [1]);
  271. Assertion.AssertEquals ("NetworkToHostOrder #4", short0, (short) expected [1]);
  272. short0 = IPAddress.HostToNetworkOrder (short0);
  273. Assertion.AssertEquals ("HostToNetworkOrder #4", short0, (short) tested [1]);
  274. int0 = IPAddress.NetworkToHostOrder ((int) tested [2]);
  275. Assertion.AssertEquals ("NetworkToHostOrder #5", int0, (int) expected [2]);
  276. int0 = IPAddress.HostToNetworkOrder (int0);
  277. Assertion.AssertEquals ("HostToNetworkOrder #5", int0, (int) tested [2]);
  278. long0 = IPAddress.NetworkToHostOrder (tested [3]);
  279. Assertion.AssertEquals ("NetworkToHostOrder #6", long0, expected [3]);
  280. long0 = IPAddress.HostToNetworkOrder (long0);
  281. Assertion.AssertEquals ("HostToNetworkOrder #6", long0, tested [3]);
  282. }
  283. [Test]
  284. public void LoopbackIPv6 ()
  285. {
  286. Assertion.AssertEquals ("#01", true, new Uri("http://[0:0:0:0::127.0.0.1]/").IsLoopback);
  287. Assertion.AssertEquals ("#02", false, new Uri("http://[0:0:0:0::127.1.2.3]/").IsLoopback);
  288. Assertion.AssertEquals ("#03", true, new Uri("http://[0:0:0:0::0.0.0.1]/").IsLoopback);
  289. }
  290. #if NET_2_0
  291. [Test]
  292. public void FromBytes3 ()
  293. {
  294. // This one works in 2.0
  295. new IPAddress (new byte [4]);
  296. }
  297. [Test]
  298. [ExpectedException (typeof (ArgumentException))]
  299. public void FromBytes4 ()
  300. {
  301. new IPAddress (new byte [4], 0);
  302. }
  303. #else
  304. [Test]
  305. [ExpectedException (typeof (ArgumentException))]
  306. public void FromBytes1 ()
  307. {
  308. new IPAddress (new byte [4]);
  309. }
  310. [Test]
  311. [ExpectedException (typeof (ArgumentException))]
  312. public void FromBytes2 ()
  313. {
  314. new IPAddress (new byte [4], 0);
  315. }
  316. #endif
  317. }
  318. }