IPAddressTest.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. //
  11. using NUnit.Framework;
  12. using System;
  13. using System.Net;
  14. using System.Runtime.InteropServices;
  15. namespace MonoTests.System.Net
  16. {
  17. [TestFixture]
  18. public class IPAddressTest
  19. {
  20. static string[] ipv6AddressList = new string[] {
  21. "::", "0:0:0:0:0:0:0:0",
  22. "1::", "1:0:0:0:0:0:0:0",
  23. "2:2::", "2:2:0:0:0:0:0:0",
  24. "7:7:7:7:7:7:7:0", "7:7:7:7:7:7:7:0",
  25. "::1", "0:0:0:0:0:0:0:1",
  26. "0:7:7:7:7:7:7:7", "0:7:7:7:7:7:7:7",
  27. "E::1", "E:0:0:0:0:0:0:1",
  28. "E::2:2", "E:0:0:0:0:0:2:2",
  29. "E:0:6:6:6:6:6:6", "E:0:6:6:6:6:6:6",
  30. "E:E::1", "E:E:0:0:0:0:0:1",
  31. "E:E::2:2", "E:E:0:0:0:0:2:2",
  32. "E:E:0:5:5:5:5:5", "E:E:0:5:5:5:5:5",
  33. "E:E:E::1", "E:E:E:0:0:0:0:1",
  34. "E:E:E::2:2", "E:E:E:0:0:0:2:2",
  35. "E:E:E:0:4:4:4:4", "E:E:E:0:4:4:4:4",
  36. "E:E:E:E::1", "E:E:E:E:0:0:0:1",
  37. "E:E:E:E::2:2", "E:E:E:E:0:0:2:2",
  38. "E:E:E:E:0:3:3:3", "E:E:E:E:0:3:3:3",
  39. "E:E:E:E:E::1", "E:E:E:E:E:0:0:1",
  40. "E:E:E:E:E:0:2:2", "E:E:E:E:E:0:2:2",
  41. "E:E:E:E:E:E:0:1", "E:E:E:E:E:E:0:1",
  42. "::0.2.0.2", "0:0:0:0:0:0:2:2",
  43. "::FFFF:192.168.0.1", "::FFFF:192.168.0.1",
  44. "::FFFF:0.168.0.1", "::FFFF:0.168.0.1",
  45. "::FFFF", "::0.0.255.255",
  46. "::10.0.0.1", "::10.0.0.1",
  47. "1234::1234:0:0", "1234:0:0:0:0:1234:0:0",
  48. "1:0:1:0:1:0:1:0", "1:0:1:0:1:0:1:0",
  49. "1:1:1::1:1:0", "1:1:1:0:0:1:1:0",
  50. "::1234:0:0", "0:0:0:0:0:1234:0:0",
  51. "3ffe:38e1::100:1:1", "3ffe:38e1::0100:1:0001",
  52. "0:0:1:2::", "0:0:1:2:00:00:000:0000",
  53. "100:0:1:2::abcd", "100:0:1:2:0:0:000:abcd",
  54. "ffff::abcd", "ffff:0:0:0:0:0:00:abcd",
  55. "ffff:0:0:2::abcd", "ffff:0:0:2:0:0:00:abcd",
  56. "0:0:1:2::", "0:0:1:2:0:00:0000:0000",
  57. "::1:0:0", "0000:0000::1:0000:0000",
  58. "::111:234:5:6:789a:0", "0:0:111:234:5:6:789A:0",
  59. "11:22:33:44:55:66:77:8","11:22:33:44:55:66:77:8",
  60. "0:0:7711:ab42:1230::", "::7711:ab42:1230:0:0:0",
  61. };
  62. static string[] ipv4ParseOk = new string[] {
  63. "192.168.1.1", "192.168.1.1",
  64. "0xff.0x7f.0x20.0x01", "255.127.32.1",
  65. "0xff.0x7f.0x20.0xf", "255.127.32.15",
  66. "0.0.0.0", IPAddress.Any.ToString(),
  67. "255.255.255.255", IPAddress.Broadcast.ToString(),
  68. "12.1.1.3 ", "12.1.1.3",
  69. " 12.1.1.1", "0.0.0.0",
  70. "12.1 .1.2", "12.0.0.1",
  71. ".1.1.6", "0.1.1.6",
  72. "12.1.7", "12.1.0.7",
  73. "12.1.8. ", "12.1.8.0",
  74. "12", "0.0.0.12",
  75. "12.1 foo.1.2.3.4.5.bar", "12.0.0.1",
  76. "12.1.2. ", "12.1.2.0",
  77. "12.. .", "12.0.0.0",
  78. " ", "0.0.0.0",
  79. " foo", "0.0.0.0"
  80. };
  81. static object[] ipv4ParseWrong = new object[] {
  82. "12.+1.1.4", typeof(FormatException),
  83. "12.1.-1.5", typeof(FormatException),
  84. "257.1.1.9", typeof(FormatException),
  85. "12.", typeof(FormatException),
  86. "12.1.2.", typeof(FormatException),
  87. "12...", typeof(FormatException),
  88. null, typeof(ArgumentNullException),
  89. };
  90. [Test]
  91. public void PublicFields ()
  92. {
  93. Assertion.AssertEquals ("Any", IPAddress.Any.Address, (long) 0);
  94. Assertion.AssertEquals ("Broadcast", IPAddress.Broadcast.Address, (long) 0xFFFFFFFF);
  95. long loopback = IPAddress.HostToNetworkOrder (BitConverter.IsLittleEndian ?
  96. 0x7f000001 :
  97. 0x0100007f);
  98. Assertion.AssertEquals ("Loopback", IPAddress.Loopback.Address, loopback);
  99. Assertion.AssertEquals ("None", IPAddress.None.Address, (long) 0xFFFFFFFF);
  100. }
  101. [Test]
  102. public void ToStringTest ()
  103. {
  104. IPAddress ip = IPAddress.Parse ("192.168.1.1");
  105. Assertion.AssertEquals ("ToString #1", "192.168.1.1", ip.ToString ());
  106. Assertion.AssertEquals ("ToString #2", "0.0.0.0", IPAddress.Any.ToString ());
  107. Assertion.AssertEquals ("ToString #3", "255.255.255.255", IPAddress.Broadcast.ToString ());
  108. Assertion.AssertEquals ("ToString #4", "127.0.0.1", IPAddress.Loopback.ToString ());
  109. Assertion.AssertEquals ("ToString #5", "255.255.255.255", IPAddress.None.ToString ());
  110. #if NET_1_1
  111. for(int i=0; i<ipv6AddressList.Length/2; i++) {
  112. string addr = IPAddress.Parse (ipv6AddressList[i*2+1]).ToString().ToLower();
  113. Assertion.AssertEquals ("ToStringIPv6 #" + i, ipv6AddressList[i*2].ToLower(), addr);
  114. }
  115. #endif
  116. }
  117. [Test]
  118. public void IsLoopback ()
  119. {
  120. IPAddress ip = IPAddress.Parse ("127.0.0.1");
  121. Assertion.AssertEquals ("IsLoopback #1", true, IPAddress.IsLoopback (ip));
  122. ip = IPAddress.Parse ("::101");
  123. Assertion.AssertEquals ("IsLoopback #2", false, IPAddress.IsLoopback (ip));
  124. #if NET_1_1
  125. ip = IPAddress.IPv6Loopback;
  126. Assertion.AssertEquals ("IsLoopback #3", true, IPAddress.IsLoopback (ip));
  127. ip = IPAddress.IPv6None;
  128. Assertion.AssertEquals ("IsLoopback #7", false, IPAddress.IsLoopback (ip));
  129. #endif
  130. ip = IPAddress.Any;
  131. Assertion.AssertEquals ("IsLoopback #5", false, IPAddress.IsLoopback (ip));
  132. ip = IPAddress.Loopback;
  133. Assertion.AssertEquals ("IsLoopback #6", true, IPAddress.IsLoopback (ip));
  134. }
  135. #if NET_1_1
  136. [Test]
  137. public void GetAddressBytes()
  138. {
  139. byte[] dataIn = { 10, 11, 12, 13 };
  140. byte[] dataOut = IPAddress.Parse ("10.11.12.13").GetAddressBytes ();
  141. for(int i=0; i<dataIn.Length; i++)
  142. Assertion.AssertEquals ("GetAddressBytes #1", dataIn[i], dataOut[i]);
  143. dataIn = new byte[]{ 0x01, 0x23, 0x45, 0x67, 0x89, 0x98, 0x76, 0x54, 0x32, 0x10, 0x01, 0x23, 0x45, 0x67, 0x89, 0x98 };
  144. dataOut = IPAddress.Parse ("123:4567:8998:7654:3210:0123:4567:8998").GetAddressBytes ();
  145. for(int i=0; i<dataIn.Length; i++)
  146. Assertion.AssertEquals ("GetAddressBytes #2", dataIn[i], dataOut[i]);
  147. dataIn = new byte[]{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0x01 };
  148. dataOut = IPAddress.Parse ("::FFFF:127.0.0.1").GetAddressBytes ();
  149. for(int i=0; i<dataIn.Length; i++)
  150. Assertion.AssertEquals ("GetAddressBytes #3", dataIn[i], dataOut[i]);
  151. }
  152. #endif
  153. [Test]
  154. public void Address ()
  155. {
  156. // hm, lame, anything is accepted by ms.net
  157. /*
  158. try {
  159. IPAddress ip1 = new IPAddress (0x0000000100000000);
  160. Assertion.Fail ("#1");
  161. } catch (ArgumentOutOfRangeException) {}
  162. IPAddress ip = IPAddress.Parse ("127.0.0.1");
  163. ip.Address = 0;
  164. ip.Address = 0xffffffff;
  165. try {
  166. ip.Address = -1;
  167. Assertion.Fail ("#2");
  168. } catch (ArgumentOutOfRangeException) {}
  169. try {
  170. ip.Address = 0x0000000100000000;
  171. Assertion.Fail ("#3");
  172. } catch (ArgumentOutOfRangeException) {}
  173. */
  174. }
  175. [Test]
  176. public void ParseOk ()
  177. {
  178. for(int i=0; i<ipv4ParseOk.Length / 2; i++) {
  179. IPAddress ip = IPAddress.Parse (ipv4ParseOk [i*2]);
  180. Assertion.Assert ("ParseIPv4 #" + i, ip.ToString () == ipv4ParseOk [i*2+1]);
  181. }
  182. #if NET_1_1
  183. for(int i=0; i<ipv6AddressList.Length / 2; i++)
  184. {
  185. string source = ipv6AddressList [i*2].ToLower();
  186. IPAddress ip = IPAddress.Parse (source);
  187. Assertion.Assert (string.Format("ParseIPv6 #{0}-1: {1} != {2}", i,
  188. ip.ToString ().ToLower (), source), ip.ToString ().ToLower () == source);
  189. ip = IPAddress.Parse (ipv6AddressList [i*2+1].ToLower ());
  190. Assertion.Assert (string.Format("ParseIPv6 #{0}-2: {1} != {2}", i,
  191. ip.ToString ().ToLower (), source), ip.ToString ().ToLower () == source);
  192. }
  193. #endif
  194. }
  195. [Test]
  196. public void ParseWrong ()
  197. {
  198. for(int i=0; i<ipv4ParseWrong.Length/2; i++) {
  199. Type exception = ipv4ParseWrong[i*2+1] as Type;
  200. string ipAddress = ipv4ParseWrong[i*2] as string;
  201. try {
  202. IPAddress ip = IPAddress.Parse (ipAddress);
  203. Assertion.Fail ("IPv4: Should raise a " + exception + " #" + i);
  204. }
  205. catch (Exception e) {
  206. if(!e.GetType ().Equals (exception))
  207. Assertion.Fail ("ParseWrongIPv4 #" + i + ": " + e.ToString());
  208. }
  209. }
  210. }
  211. [Test]
  212. public void NetworkHost ()
  213. {
  214. long [] tested = new long [] { 0, 1, 1, 1};
  215. long [] expectedLE = new long [] {0, 256, 16777216, 72057594037927936 };
  216. long [] expected;
  217. expected = BitConverter.IsLittleEndian ? expectedLE : tested;
  218. short short0 = IPAddress.NetworkToHostOrder ((short) tested [0]);
  219. Assertion.AssertEquals ("NetworkToHostOrder #1", short0, (short) expected [0]);
  220. short0 = IPAddress.HostToNetworkOrder (short0);
  221. Assertion.AssertEquals ("HostToNetworkOrder #1", short0, (short) tested [0]);
  222. int int0 = IPAddress.NetworkToHostOrder ((int) tested [0]);
  223. Assertion.AssertEquals ("NetworkToHostOrder #2", int0, (int) expected [0]);
  224. int0 = IPAddress.HostToNetworkOrder (int0);
  225. Assertion.AssertEquals ("HostToNetworkOrder #2", int0, (int) tested [0]);
  226. long long0 = IPAddress.NetworkToHostOrder (tested [0]);
  227. Assertion.AssertEquals ("NetworkToHostOrder #3", long0, expected [0]);
  228. long0 = IPAddress.HostToNetworkOrder (long0);
  229. Assertion.AssertEquals ("HostToNetworkOrder #3", long0, tested [0]);
  230. short0 = IPAddress.NetworkToHostOrder ((short) tested [1]);
  231. Assertion.AssertEquals ("NetworkToHostOrder #4", short0, (short) expected [1]);
  232. short0 = IPAddress.HostToNetworkOrder (short0);
  233. Assertion.AssertEquals ("HostToNetworkOrder #4", short0, (short) tested [1]);
  234. int0 = IPAddress.NetworkToHostOrder ((int) tested [2]);
  235. Assertion.AssertEquals ("NetworkToHostOrder #5", int0, (int) expected [2]);
  236. int0 = IPAddress.HostToNetworkOrder (int0);
  237. Assertion.AssertEquals ("HostToNetworkOrder #5", int0, (int) tested [2]);
  238. long0 = IPAddress.NetworkToHostOrder (tested [3]);
  239. Assertion.AssertEquals ("NetworkToHostOrder #6", long0, expected [3]);
  240. long0 = IPAddress.HostToNetworkOrder (long0);
  241. Assertion.AssertEquals ("HostToNetworkOrder #6", long0, tested [3]);
  242. }
  243. }
  244. }