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