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