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