Update mcs/class/Commons.Xml.Relaxng/Commons.Xml.Relaxng/RelaxngPattern.cs
[mono.git] / mcs / class / System / Test / System.Net / DnsTest.cs
1 // DnsTest.cs - NUnit Test Cases for the System.Net.Dns class\r
2 //\r
3 // Authors: \r
4 //   Mads Pultz (mpultz@diku.dk)\r
5 //   Martin Willemoes Hansen (mwh@sysrq.dk)\r
6 //\r
7 // (C) 2001 Mads Pultz\r
8 // (C) 2003 Martin Willemoes Hansen\r
9 // \r
10 // This test assumes the following:\r
11 // 1) The following Internet sites exist:\r
12 //        www.go-mono.com with IP address 64.14.94.188\r
13 //        info.diku.dk with IP address 130.225.96.4\r
14 // 2) The following DNS name does not exist:\r
15 //        www.hopefullydoesnotexist.dk\r
16 //\r
17 \r
18 using System;\r
19 using System.Collections;\r
20 using System.Net;\r
21 using System.Net.Sockets;\r
22 using System.Threading;\r
23 \r
24 using NUnit.Framework;\r
25 \r
26 namespace MonoTests.System.Net\r
27 {\r
28         [TestFixture]\r
29         public class DnsTest\r
30         {\r
31                 private String site1Name = "mono-project.com",\r
32                         site1Dot = "96.126.105.110",\r
33                         site2Name = "info.diku.dk",\r
34                         site2Dot = "130.225.96.4",\r
35                         noneExistingSite = "www.unlikely.novell.com";\r
36                 private uint site1IP = 1852407392, site2IP = 2195808260; // Big-Endian\r
37 \r
38                 [Test]\r
39                 public void AsyncGetHostByName ()\r
40                 {\r
41                         IAsyncResult r;\r
42                         r = Dns.BeginGetHostByName (site1Name, new AsyncCallback (GetHostByNameCallback), null);\r
43 \r
44                         IAsyncResult async = Dns.BeginGetHostByName (site1Name, null, null);\r
45                         IPHostEntry entry = Dns.EndGetHostByName (async);\r
46                         SubTestValidIPHostEntry (entry);\r
47                         Assert.AreEqual ("www.mono-project.com", entry.HostName);\r
48                 }\r
49 \r
50                 void GetHostByNameCallback (IAsyncResult ar)\r
51                 {\r
52                         IPHostEntry h;\r
53                         h = Dns.EndGetHostByName (ar);\r
54                         SubTestValidIPHostEntry (h);\r
55                 }\r
56 \r
57                 [Test]\r
58                 public void AsyncResolve ()\r
59                 {\r
60                         IAsyncResult r;\r
61                         r = Dns.BeginResolve (site1Name, new AsyncCallback (ResolveCallback), null);\r
62 \r
63                         IAsyncResult async = Dns.BeginResolve (site1Dot, null, null);\r
64                         IPHostEntry entry = Dns.EndResolve (async);\r
65                         SubTestValidIPHostEntry (entry);\r
66                         Assert.AreEqual (site1Dot, entry.AddressList [0].ToString ());\r
67                 }\r
68 \r
69                 void ResolveCallback (IAsyncResult ar)\r
70                 {\r
71                         IPHostEntry h = Dns.EndResolve (ar);\r
72                         SubTestValidIPHostEntry (h);\r
73                 }\r
74 \r
75 #if NET_2_0\r
76                 [Test]\r
77                 public void BeginGetHostAddresses_HostNameOrAddress_Null ()\r
78                 {\r
79                         try {\r
80                                 Dns.BeginGetHostAddresses (\r
81                                         (string) null,\r
82                                         new AsyncCallback (GetHostAddressesCallback),\r
83                                         null);\r
84                                 Assert.Fail ("#1");\r
85                         } catch (ArgumentNullException ex) {\r
86                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
87                                 Assert.IsNull (ex.InnerException, "#3");\r
88                                 Assert.IsNotNull (ex.Message, "#4");\r
89                                 Assert.AreEqual ("hostName", ex.ParamName, "#5");\r
90                         }\r
91                 }\r
92 \r
93                 [Test]\r
94                 public void BeginGetHostAddresses_HostNameOrAddress_UnspecifiedAddress ()\r
95                 {\r
96                         // IPv4\r
97                         try {\r
98                                 Dns.BeginGetHostAddresses (\r
99                                         "0.0.0.0",\r
100                                         new AsyncCallback (GetHostAddressesCallback),\r
101                                         null);\r
102                                 Assert.Fail ("#A1");\r
103                         } catch (ArgumentException ex) {\r
104                                 // IPv4 address 0.0.0.0 and IPv6 address ::0 are\r
105                                 // unspecified addresses that cannot be used as\r
106                                 // a target address\r
107                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");\r
108                                 Assert.IsNull (ex.InnerException, "#A3");\r
109                                 Assert.IsNotNull (ex.Message, "#A4");\r
110                                 Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#A5");\r
111                         }\r
112 \r
113                         // IPv6\r
114                         try {\r
115                                 Dns.BeginGetHostAddresses (\r
116                                         "::0",\r
117                                         new AsyncCallback (GetHostAddressesCallback),\r
118                                         null);\r
119                                 Assert.Fail ("#B1");\r
120                         } catch (ArgumentException ex) {\r
121                                 // IPv4 address 0.0.0.0 and IPv6 address ::0 are\r
122                                 // unspecified addresses that cannot be used as\r
123                                 // a target address\r
124                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");\r
125                                 Assert.IsNull (ex.InnerException, "#B3");\r
126                                 Assert.IsNotNull (ex.Message, "#B4");\r
127                                 Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#B5");\r
128                         }\r
129                 }\r
130 \r
131                 void GetHostAddressesCallback (IAsyncResult ar)\r
132                 {\r
133                         IPAddress [] addresses = Dns.EndGetHostAddresses (ar);\r
134                         Assert.IsNotNull (addresses);\r
135                 }\r
136 \r
137                 [Test]\r
138                 public void GetHostAddresses_HostNameOrAddress_Null ()\r
139                 {\r
140                         try {\r
141                                 Dns.GetHostAddresses ((string) null);\r
142                                 Assert.Fail ("#1");\r
143                         } catch (ArgumentNullException ex) {\r
144                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
145                                 Assert.IsNull (ex.InnerException, "#3");\r
146                                 Assert.IsNotNull (ex.Message, "#4");\r
147                                 Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#5");\r
148                         }\r
149                 }\r
150 \r
151                 [Test]\r
152                 public void GetHostAddresses_HostNameOrAddress_UnspecifiedAddress ()\r
153                 {\r
154                         // IPv4\r
155                         try {\r
156                                 Dns.GetHostAddresses ("0.0.0.0");\r
157                                 Assert.Fail ("#A1");\r
158                         } catch (ArgumentException ex) {\r
159                                 // IPv4 address 0.0.0.0 and IPv6 address ::0 are\r
160                                 // unspecified addresses that cannot be used as\r
161                                 // a target address\r
162                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");\r
163                                 Assert.IsNull (ex.InnerException, "#A3");\r
164                                 Assert.IsNotNull (ex.Message, "#A4");\r
165                                 Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#A5");\r
166                         }\r
167 \r
168                         // IPv6\r
169                         try {\r
170                                 Dns.GetHostAddresses ("::0");\r
171                                 Assert.Fail ("#B1");\r
172                         } catch (ArgumentException ex) {\r
173                                 // IPv4 address 0.0.0.0 and IPv6 address ::0 are\r
174                                 // unspecified addresses that cannot be used as\r
175                                 // a target address\r
176                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");\r
177                                 Assert.IsNull (ex.InnerException, "#B3");\r
178                                 Assert.IsNotNull (ex.Message, "#B4");\r
179                                 Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#B5");\r
180                         }\r
181                 }\r
182 #endif\r
183 \r
184                 [Test]\r
185                 public void GetHostName ()\r
186                 {\r
187                         string hostName = Dns.GetHostName ();\r
188                         Assert.IsNotNull (hostName);\r
189                 }\r
190 \r
191                 [Test]\r
192                 public void GetHostByName ()\r
193                 {\r
194                         SubTestGetHostByName ("www.mono-project.com", site1Dot);\r
195                         SubTestGetHostByName (site2Name, site2Dot);\r
196                         try {\r
197                                 var entry = Dns.GetHostByName (noneExistingSite);\r
198                                 /*\r
199                                  * Work around broken t-online.de DNS Server.\r
200                                  * \r
201                                  * T-Online's DNS Server for DSL Customers resolves\r
202                                  * non-exisitng domain names to\r
203                                  * http://navigationshilfe1.t-online.de/dnserror?url=....\r
204                                  * instead of reporting an error.\r
205                                  */\r
206                                 var navigationshilfe1 = IPAddress.Parse ("80.156.86.78");\r
207                                 var navigationshilfe2 = IPAddress.Parse ("62.157.140.133");\r
208                                 foreach (var addr in entry.AddressList) {\r
209                                         if (addr.Equals (navigationshilfe1) || addr.Equals (navigationshilfe2))\r
210                                                 return;\r
211                                 }\r
212                                 Assert.Fail ("Should raise a SocketException (assuming that '" + noneExistingSite + "' does not exist)");\r
213                         } catch (SocketException) {\r
214                         }\r
215                 }\r
216 \r
217                 void SubTestGetHostByName (string siteName, string siteDot)\r
218                 {\r
219                         IPHostEntry h = Dns.GetHostByName (siteName);\r
220                         SubTestValidIPHostEntry (h);\r
221                         Assert.AreEqual (siteName, h.HostName, "siteName");\r
222                         Assert.AreEqual (siteDot, h.AddressList [0].ToString (), "siteDot");\r
223                 }\r
224 \r
225                 [Test]\r
226                 public void GetHostByName_HostName_Null ()\r
227                 {\r
228                         try {\r
229                                 Dns.GetHostByName ((string) null);\r
230                                 Assert.Fail ("#1");\r
231                         } catch (ArgumentNullException ex) {\r
232                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
233                                 Assert.IsNull (ex.InnerException, "#3");\r
234                                 Assert.IsNotNull (ex.Message, "#4");\r
235                                 Assert.AreEqual ("hostName", ex.ParamName, "#5");\r
236                         }\r
237                 }\r
238 \r
239                 [Test]\r
240                 public void GetHostByAddressString_Address_Null ()\r
241                 {\r
242                         try {\r
243                                 Dns.GetHostByAddress ((string) null);\r
244                                 Assert.Fail ("#1");\r
245                         } catch (ArgumentNullException ex) {\r
246                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
247                                 Assert.IsNull (ex.InnerException, "#3");\r
248                                 Assert.IsNotNull (ex.Message, "#4");\r
249                                 Assert.AreEqual ("address", ex.ParamName, "#5");\r
250                         }\r
251                 }\r
252 \r
253                 [Test]\r
254                 [ExpectedException (typeof (SocketException))]\r
255 #if TARGET_JVM\r
256                 [Ignore ("Ignore failures in Sys.Net")]\r
257 #endif\r
258                 public void GetHostByAddressString2 ()\r
259                 {\r
260                         Dns.GetHostByAddress ("123.255.23");\r
261                 }\r
262 \r
263                 [Test]\r
264                 [ExpectedException (typeof (FormatException))]\r
265                 public void GetHostByAddressString3 ()\r
266                 {\r
267                         Dns.GetHostByAddress ("123.256.34.10");\r
268                 }\r
269 \r
270                 [Test, ExpectedException (typeof (FormatException))]\r
271                 public void GetHostByAddressString4 ()\r
272                 {\r
273                         Dns.GetHostByAddress ("not an IP address");\r
274                 }\r
275 \r
276                 [Test]\r
277                 public void GetHostByAddressString5 ()\r
278                 {\r
279                         Dns.GetHostByAddress (site1Dot);\r
280                 }\r
281 \r
282                 [Test]\r
283                 public void GetHostByAddressIPAddress_Address_Null ()\r
284                 {\r
285                         try {\r
286                                 Dns.GetHostByAddress ((IPAddress) null);\r
287                                 Assert.Fail ("#1");\r
288                         } catch (ArgumentNullException ex) {\r
289                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
290                                 Assert.IsNull (ex.InnerException, "#3");\r
291                                 Assert.IsNotNull (ex.Message, "#4");\r
292                                 Assert.AreEqual ("address", ex.ParamName, "#5");\r
293                         }\r
294                 }\r
295 \r
296                 [Test]\r
297                 public void GetHostByAddressIPAddress2 ()\r
298                 {\r
299                         IPAddress addr = new IPAddress (IPAddress.NetworkToHostOrder ((int) site1IP));\r
300                         IPHostEntry h = Dns.GetHostByAddress (addr);\r
301                         SubTestValidIPHostEntry (h);\r
302                         Assert.AreEqual (addr.ToString (), h.AddressList [0].ToString ());\r
303                 }\r
304 \r
305                 [Test]\r
306                 public void GetHostByAddressIPAddress3 ()\r
307                 {\r
308                         IPAddress addr = new IPAddress (IPAddress.NetworkToHostOrder ((int) site2IP));\r
309                         IPHostEntry h = Dns.GetHostByAddress (addr);\r
310                         SubTestValidIPHostEntry (h);\r
311                         Assert.AreEqual (addr.ToString (), h.AddressList [0].ToString ());\r
312                 }\r
313 \r
314                 [Test]\r
315                 public void BeginResolve_HostName_Null ()\r
316                 {\r
317                         try {\r
318                                 Dns.BeginResolve ((string) null,\r
319                                         new AsyncCallback (ResolveCallback),\r
320                                         null);\r
321                                 Assert.Fail ("#1");\r
322                         } catch (ArgumentNullException ex) {\r
323                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
324                                 Assert.IsNull (ex.InnerException, "#3");\r
325                                 Assert.IsNotNull (ex.Message, "#4");\r
326                                 Assert.AreEqual ("hostName", ex.ParamName, "#5");\r
327                         }\r
328                 }\r
329 \r
330                 [Test]\r
331                 public void Resolve ()\r
332                 {\r
333                         SubTestResolve (site1Name);\r
334                         SubTestResolve (site2Name);\r
335                         SubTestResolve (site1Dot);\r
336                         SubTestResolve (site2Dot);\r
337                 }\r
338 \r
339                 void SubTestResolve (string addr)\r
340                 {\r
341                         IPHostEntry h = Dns.Resolve (addr);\r
342                         SubTestValidIPHostEntry (h);\r
343                 }\r
344 \r
345                 [Test]\r
346                 public void Resolve_HostName_Null ()\r
347                 {\r
348                         try {\r
349                                 Dns.Resolve ((string) null);\r
350                                 Assert.Fail ("#1");\r
351                         } catch (ArgumentNullException ex) {\r
352                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
353                                 Assert.IsNull (ex.InnerException, "#3");\r
354                                 Assert.IsNotNull (ex.Message, "#4");\r
355                                 Assert.AreEqual ("hostName", ex.ParamName, "#5");\r
356                         }\r
357                 }\r
358 \r
359                 [Test] // BeginGetHostEntry (IPAddress, AsyncCallback, Object)\r
360                 public void BeginGetHostEntry1_Address_Null ()\r
361                 {\r
362                         try {\r
363                                 Dns.BeginGetHostEntry (\r
364                                         (IPAddress) null,\r
365                                         new AsyncCallback (GetHostAddressesCallback),\r
366                                         null);\r
367                                 Assert.Fail ("#1");\r
368                         } catch (ArgumentNullException ex) {\r
369                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
370                                 Assert.IsNull (ex.InnerException, "#3");\r
371                                 Assert.IsNotNull (ex.Message, "#4");\r
372                                 Assert.AreEqual ("address", ex.ParamName, "#5");\r
373                         }\r
374                 }\r
375 \r
376                 [Test] // BeginGetHostEntry (String, AsyncCallback, Object)\r
377                 public void BeginGetHostEntry2_HostNameOrAddress_Null ()\r
378                 {\r
379                         try {\r
380                                 Dns.BeginGetHostEntry (\r
381                                         (string) null,\r
382                                         new AsyncCallback (GetHostAddressesCallback),\r
383                                         null);\r
384                                 Assert.Fail ("#1");\r
385                         } catch (ArgumentNullException ex) {\r
386                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
387                                 Assert.IsNull (ex.InnerException, "#3");\r
388                                 Assert.IsNotNull (ex.Message, "#4");\r
389                                 Assert.AreEqual ("hostName", ex.ParamName, "#5");\r
390                         }\r
391                 }\r
392 \r
393                 [Test] // BeginGetHostEntry (String, AsyncCallback, Object)\r
394                 public void BeginGetHostEntry2_HostNameOrAddress_UnspecifiedAddress ()\r
395                 {\r
396                         // IPv4\r
397                         try {\r
398                                 Dns.BeginGetHostEntry (\r
399                                         "0.0.0.0",\r
400                                         new AsyncCallback (GetHostEntryCallback),\r
401                                         null);\r
402                                 Assert.Fail ("#1");\r
403                         } catch (ArgumentException ex) {\r
404                                 // IPv4 address 0.0.0.0 and IPv6 address ::0 are\r
405                                 // unspecified addresses that cannot be used as\r
406                                 // a target address\r
407                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");\r
408                                 Assert.IsNull (ex.InnerException, "#3");\r
409                                 Assert.IsNotNull (ex.Message, "#4");\r
410                                 Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#5");\r
411                         }\r
412 \r
413                         // IPv6\r
414                         try {\r
415                                 Dns.BeginGetHostEntry (\r
416                                         "::0",\r
417                                         new AsyncCallback (GetHostEntryCallback),\r
418                                         null);\r
419                                 Assert.Fail ("#1");\r
420                         } catch (ArgumentException ex) {\r
421                                 // IPv4 address 0.0.0.0 and IPv6 address ::0 are\r
422                                 // unspecified addresses that cannot be used as\r
423                                 // a target address\r
424                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");\r
425                                 Assert.IsNull (ex.InnerException, "#3");\r
426                                 Assert.IsNotNull (ex.Message, "#4");\r
427                                 Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#5");\r
428                         }\r
429                 }\r
430 \r
431                 void GetHostEntryCallback (IAsyncResult ar)\r
432                 {\r
433                         IPHostEntry hostEntry = Dns.EndGetHostEntry (ar);\r
434                         Assert.IsNotNull (hostEntry);\r
435                 }\r
436 \r
437                 [Test] // GetHostEntry (IPAddress)\r
438                 public void GetHostEntry1_Address_Null ()\r
439                 {\r
440                         try {\r
441                                 Dns.GetHostEntry ((IPAddress) null);\r
442                                 Assert.Fail ("#1");\r
443                         } catch (ArgumentNullException ex) {\r
444                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
445                                 Assert.IsNull (ex.InnerException, "#3");\r
446                                 Assert.IsNotNull (ex.Message, "#4");\r
447                                 Assert.AreEqual ("address", ex.ParamName, "#5");\r
448                         }\r
449                 }\r
450 \r
451                 [Test] // GetHostEntry (String)\r
452                 public void GetHostEntry2 ()\r
453                 {\r
454                         Dns.GetHostEntry (site1Name); // hostname\r
455                         Dns.GetHostEntry (site1Dot); // IP address\r
456                 }\r
457 \r
458                 [Test] // GetHostEntry (String)\r
459                 public void GetHostEntry2_HostNameOrAddress_Null ()\r
460                 {\r
461                         try {\r
462                                 Dns.GetHostEntry ((string) null);\r
463                                 Assert.Fail ("#1");\r
464                         } catch (ArgumentNullException ex) {\r
465                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
466                                 Assert.IsNull (ex.InnerException, "#3");\r
467                                 Assert.IsNotNull (ex.Message, "#4");\r
468                                 Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#5");\r
469                         }\r
470                 }\r
471 \r
472                 [Test] // GetHostEntry (String)\r
473                 public void GetHostEntry2_HostNameOrAddress_UnspecifiedAddress ()\r
474                 {\r
475                         // IPv4\r
476                         try {\r
477                                 Dns.GetHostEntry ("0.0.0.0");\r
478                                 Assert.Fail ("#A1");\r
479                         } catch (ArgumentException ex) {\r
480                                 // IPv4 address 0.0.0.0 and IPv6 address ::0 are\r
481                                 // unspecified addresses that cannot be used as\r
482                                 // a target address\r
483                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");\r
484                                 Assert.IsNull (ex.InnerException, "#A3");\r
485                                 Assert.IsNotNull (ex.Message, "#A4");\r
486                                 Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#A5");\r
487                         }\r
488 \r
489                         // IPv6\r
490                         try {\r
491                                 Dns.GetHostEntry ("::0");\r
492                                 Assert.Fail ("#B1");\r
493                         } catch (ArgumentException ex) {\r
494                                 // IPv4 address 0.0.0.0 and IPv6 address ::0 are\r
495                                 // unspecified addresses that cannot be used as\r
496                                 // a target address\r
497                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");\r
498                                 Assert.IsNull (ex.InnerException, "#B3");\r
499                                 Assert.IsNotNull (ex.Message, "#B4");\r
500                                 Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#B5");\r
501                         }\r
502                 }\r
503 \r
504                 void SubTestValidIPHostEntry (IPHostEntry h)\r
505                 {\r
506                         Assert.IsNotNull (h.HostName, "HostName not null");\r
507                         Assert.IsNotNull (h.AddressList, "AddressList not null");\r
508                         Assert.IsTrue (h.AddressList.Length > 0, "AddressList.Length");\r
509                 }\r
510 \r
511                 [Test]\r
512                 public void GetHostEntry_StringEmpty ()\r
513                 {\r
514                         Dns.GetHostEntry (string.Empty);\r
515                 }\r
516 \r
517                 /* This isn't used anymore, but could be useful for debugging\r
518                 static void printIPHostEntry(IPHostEntry h)\r
519                 {\r
520                         Console.WriteLine("----------------------------------------------------");\r
521                         Console.WriteLine("Host name:");\r
522                         Console.WriteLine(h.HostName);\r
523                         Console.WriteLine("IP addresses:");\r
524                         IPAddress[] list = h.AddressList;\r
525                         for(int i = 0; i < list.Length; ++i)\r
526                                 Console.WriteLine(list[i]);\r
527                         Console.WriteLine("Aliases:");\r
528                         string[] aliases = h.Aliases;\r
529                         for(int i = 0; i < aliases.Length; ++i)\r
530                                 Console.WriteLine(aliases[i]);\r
531                         Console.WriteLine("----------------------------------------------------");\r
532                 }\r
533                 */\r
534         }\r
535 }\r