Merge pull request #2996 from UCIS/patch-7
[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 \r
11 using System;\r
12 using System.Linq;\r
13 using System.Net;\r
14 using System.Net.Sockets;\r
15 using System.Threading;\r
16 \r
17 using NUnit.Framework;\r
18 \r
19 namespace MonoTests.System.Net\r
20 {\r
21         [TestFixture]\r
22         public class DnsTest\r
23         {\r
24                 private String site1Name = "google-public-dns-a.google.com",\r
25                         site1Dot = "8.8.8.8",\r
26                         site2Name = "google-public-dns-b.google.com",\r
27                         site2Dot = "8.8.4.4",\r
28                         noneExistingSite = "unlikely.xamarin.com";\r
29                 private uint site1IP = 134744072, site2IP = 134743044; // Big-Endian\r
30 \r
31                 [Test]\r
32                 public void AsyncGetHostByName ()\r
33                 {\r
34                         IAsyncResult r;\r
35                         r = Dns.BeginGetHostByName (site1Name, new AsyncCallback (GetHostByNameCallback), null);\r
36 \r
37                         IAsyncResult async = Dns.BeginGetHostByName (site1Name, null, null);\r
38                         IPHostEntry entry = Dns.EndGetHostByName (async);\r
39                         SubTestValidIPHostEntry (entry);\r
40                         Assert.IsTrue (entry.HostName == "google-public-dns-a.google.com");\r
41                 }\r
42 \r
43                 void GetHostByNameCallback (IAsyncResult ar)\r
44                 {\r
45                         IPHostEntry h;\r
46                         h = Dns.EndGetHostByName (ar);\r
47                         SubTestValidIPHostEntry (h);\r
48                 }\r
49 \r
50                 [Test]\r
51                 public void AsyncResolve ()\r
52                 {\r
53                         IAsyncResult r;\r
54                         r = Dns.BeginResolve (site1Name, new AsyncCallback (ResolveCallback), null);\r
55 \r
56                         IAsyncResult async = Dns.BeginResolve (site1Dot, null, null);\r
57                         IPHostEntry entry = Dns.EndResolve (async);\r
58                         SubTestValidIPHostEntry (entry);
59                         var ip = GetIPv4Address (entry);\r
60                         Assert.AreEqual (site1Dot, ip.ToString ());\r
61                 }\r
62 \r
63                 void ResolveCallback (IAsyncResult ar)\r
64                 {\r
65                         IPHostEntry h = Dns.EndResolve (ar);\r
66                         SubTestValidIPHostEntry (h);\r
67                 }\r
68 \r
69                 [Test]\r
70                 public void BeginGetHostAddresses_HostNameOrAddress_Null ()\r
71                 {\r
72                         try {\r
73                                 Dns.BeginGetHostAddresses (\r
74                                         (string) null,\r
75                                         new AsyncCallback (GetHostAddressesCallback),\r
76                                         null);\r
77                                 Assert.Fail ("#1");\r
78                         } catch (ArgumentNullException ex) {\r
79                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
80                                 Assert.IsNull (ex.InnerException, "#3");\r
81                                 Assert.IsNotNull (ex.Message, "#4");\r
82                                 Assert.AreEqual ("hostName", ex.ParamName, "#5");\r
83                         }\r
84                 }\r
85 \r
86                 [Test]\r
87                 public void BeginGetHostAddresses_HostNameOrAddress_UnspecifiedAddress ()\r
88                 {\r
89                         // IPv4\r
90                         try {\r
91                                 Dns.BeginGetHostAddresses (\r
92                                         "0.0.0.0",\r
93                                         new AsyncCallback (GetHostAddressesCallback),\r
94                                         null);\r
95                                 Assert.Fail ("#A1");\r
96                         } catch (ArgumentException ex) {\r
97                                 // IPv4 address 0.0.0.0 and IPv6 address ::0 are\r
98                                 // unspecified addresses that cannot be used as\r
99                                 // a target address\r
100                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");\r
101                                 Assert.IsNull (ex.InnerException, "#A3");\r
102                                 Assert.IsNotNull (ex.Message, "#A4");\r
103                                 Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#A5");\r
104                         }\r
105 \r
106                         // IPv6\r
107                         try {\r
108                                 Dns.BeginGetHostAddresses (\r
109                                         "::0",\r
110                                         new AsyncCallback (GetHostAddressesCallback),\r
111                                         null);\r
112                                 Assert.Fail ("#B1");\r
113                         } catch (ArgumentException ex) {\r
114                                 // IPv4 address 0.0.0.0 and IPv6 address ::0 are\r
115                                 // unspecified addresses that cannot be used as\r
116                                 // a target address\r
117                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");\r
118                                 Assert.IsNull (ex.InnerException, "#B3");\r
119                                 Assert.IsNotNull (ex.Message, "#B4");\r
120                                 Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#B5");\r
121                         }\r
122                 }\r
123 \r
124                 void GetHostAddressesCallback (IAsyncResult ar)\r
125                 {\r
126                         IPAddress [] addresses = Dns.EndGetHostAddresses (ar);\r
127                         Assert.IsNotNull (addresses);\r
128                 }\r
129 \r
130                 [Test]\r
131                 public void GetHostAddresses_HostNameOrAddress_Null ()\r
132                 {\r
133                         try {\r
134                                 Dns.GetHostAddresses ((string) null);\r
135                                 Assert.Fail ("#1");\r
136                         } catch (ArgumentNullException ex) {\r
137                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
138                                 Assert.IsNull (ex.InnerException, "#3");\r
139                                 Assert.IsNotNull (ex.Message, "#4");\r
140                                 Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#5");\r
141                         }\r
142                 }\r
143 \r
144                 [Test]\r
145                 public void GetHostAddresses_HostNameOrAddress_UnspecifiedAddress ()\r
146                 {\r
147                         // IPv4\r
148                         try {\r
149                                 Dns.GetHostAddresses ("0.0.0.0");\r
150                                 Assert.Fail ("#A1");\r
151                         } catch (ArgumentException ex) {\r
152                                 // IPv4 address 0.0.0.0 and IPv6 address ::0 are\r
153                                 // unspecified addresses that cannot be used as\r
154                                 // a target address\r
155                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");\r
156                                 Assert.IsNull (ex.InnerException, "#A3");\r
157                                 Assert.IsNotNull (ex.Message, "#A4");\r
158                                 Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#A5");\r
159                         }\r
160 \r
161                         // IPv6\r
162                         try {\r
163                                 Dns.GetHostAddresses ("::0");\r
164                                 Assert.Fail ("#B1");\r
165                         } catch (ArgumentException ex) {\r
166                                 // IPv4 address 0.0.0.0 and IPv6 address ::0 are\r
167                                 // unspecified addresses that cannot be used as\r
168                                 // a target address\r
169                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");\r
170                                 Assert.IsNull (ex.InnerException, "#B3");\r
171                                 Assert.IsNotNull (ex.Message, "#B4");\r
172                                 Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#B5");\r
173                         }\r
174                 }\r
175 \r
176                 [Test]\r
177                 public void GetHostName ()\r
178                 {\r
179                         string hostName = Dns.GetHostName ();\r
180                         Assert.IsNotNull (hostName);\r
181                 }\r
182 \r
183                 [Test]\r
184                 public void GetHostByName ()\r
185                 {\r
186                         SubTestGetHostByName (site1Name, site1Dot);\r
187                         SubTestGetHostByName (site2Name, site2Dot);\r
188                         try {\r
189                                 var entry = Dns.GetHostByName (noneExistingSite);\r
190                                 /*\r
191                                  * Work around broken t-online.de DNS Server.\r
192                                  * \r
193                                  * T-Online's DNS Server for DSL Customers resolves\r
194                                  * non-exisitng domain names to\r
195                                  * http://navigationshilfe1.t-online.de/dnserror?url=....\r
196                                  * instead of reporting an error.\r
197                                  */\r
198                                 var navigationshilfe1 = IPAddress.Parse ("80.156.86.78");\r
199                                 var navigationshilfe2 = IPAddress.Parse ("62.157.140.133");\r
200                                 foreach (var addr in entry.AddressList) {\r
201                                         if (addr.Equals (navigationshilfe1) || addr.Equals (navigationshilfe2))\r
202                                                 return;\r
203                                 }\r
204                                 Assert.Fail ("Should raise a SocketException (assuming that '" + noneExistingSite + "' does not exist)");\r
205                         } catch (SocketException) {\r
206                         }\r
207                 }\r
208 \r
209                 static IPAddress GetIPv4Address (IPHostEntry h)\r
210                 {\r
211                         var al = h.AddressList.FirstOrDefault (x => x.AddressFamily == AddressFamily.InterNetwork);\r
212                         if (al == null)\r
213                                 Assert.Ignore ("Could not resolve an IPv4 address as required by this test case, e.g. running on an IPv6 only network");\r
214                         return al;\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                         var ip = GetIPv4Address (h);\r
223                         Assert.AreEqual (siteDot, ip.ToString (), "siteDot");\r
224                 }\r
225 \r
226                 [Test]\r
227                 public void GetHostByName_HostName_Null ()\r
228                 {\r
229                         try {\r
230                                 Dns.GetHostByName ((string) null);\r
231                                 Assert.Fail ("#1");\r
232                         } catch (ArgumentNullException ex) {\r
233                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
234                                 Assert.IsNull (ex.InnerException, "#3");\r
235                                 Assert.IsNotNull (ex.Message, "#4");\r
236                                 Assert.AreEqual ("hostName", ex.ParamName, "#5");\r
237                         }\r
238                 }\r
239 \r
240                 [Test]\r
241                 public void GetHostByAddressString_Address_Null ()\r
242                 {\r
243                         try {\r
244                                 Dns.GetHostByAddress ((string) null);\r
245                                 Assert.Fail ("#1");\r
246                         } catch (ArgumentNullException ex) {\r
247                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
248                                 Assert.IsNull (ex.InnerException, "#3");\r
249                                 Assert.IsNotNull (ex.Message, "#4");\r
250                                 Assert.AreEqual ("address", ex.ParamName, "#5");\r
251                         }\r
252                 }\r
253 \r
254                 [Test]\r
255                 [ExpectedException (typeof (SocketException))]\r
256                 public void GetHostByAddressString2 ()\r
257                 {\r
258                         Dns.GetHostByAddress ("123.255.23");\r
259                 }\r
260 \r
261                 [Test]\r
262                 [ExpectedException (typeof (FormatException))]\r
263                 public void GetHostByAddressString3 ()\r
264                 {\r
265                         Dns.GetHostByAddress ("123.256.34.10");\r
266                 }\r
267 \r
268                 [Test, ExpectedException (typeof (FormatException))]\r
269                 public void GetHostByAddressString4 ()\r
270                 {\r
271                         Dns.GetHostByAddress ("not an IP address");\r
272                 }\r
273 \r
274                 [Test]\r
275                 public void GetHostByAddressString5 ()\r
276                 {\r
277                         Dns.GetHostByAddress (site1Dot);\r
278                 }\r
279 \r
280                 [Test]\r
281                 public void GetHostByAddressIPAddress_Address_Null ()\r
282                 {\r
283                         try {\r
284                                 Dns.GetHostByAddress ((IPAddress) null);\r
285                                 Assert.Fail ("#1");\r
286                         } catch (ArgumentNullException ex) {\r
287                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
288                                 Assert.IsNull (ex.InnerException, "#3");\r
289                                 Assert.IsNotNull (ex.Message, "#4");\r
290                                 Assert.AreEqual ("address", ex.ParamName, "#5");\r
291                         }\r
292                 }\r
293 \r
294                 [Test]\r
295                 public void GetHostByAddressIPAddress2 ()\r
296                 {\r
297                         IPAddress addr = new IPAddress (IPAddress.NetworkToHostOrder ((int) site1IP));\r
298                         IPHostEntry h = Dns.GetHostByAddress (addr);\r
299                         SubTestValidIPHostEntry (h);\r
300                         var ip = GetIPv4Address (h);\r
301                         Assert.AreEqual (addr.ToString (), ip.ToString ());\r
302                 }\r
303 \r
304                 [Test]\r
305                 public void GetHostByAddressIPAddress3 ()\r
306                 {\r
307                         IPAddress addr = new IPAddress (IPAddress.NetworkToHostOrder ((int) site2IP));\r
308                         IPHostEntry h = Dns.GetHostByAddress (addr);\r
309                         SubTestValidIPHostEntry (h);
310                         var ip = GetIPv4Address (h);\r
311                         Assert.AreEqual (addr.ToString (), ip.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