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