If the window manager does not support _NET_ACTIVE_WINDOW, fall back to XGetInputFocus
[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 = "www.go-mono.com",\r
32                         site1Dot = "130.57.21.18",\r
33                         site2Name = "info.diku.dk",\r
34                         site2Dot = "130.225.96.4",\r
35                         noneExistingSite = "www.unlikely.novell.com";\r
36                 private uint site1IP = 2180692201, site2IP = 2195808260; // Big-Endian\r
37 \r
38                 [Test]\r
39                 public void AsyncGetHostByName ()\r
40                 {\r
41                         IAsyncResult r;\r
42                         r = Dns.BeginGetHostByName (site1Name, new AsyncCallback (GetHostByNameCallback), null);\r
43 \r
44                         IAsyncResult async = Dns.BeginGetHostByName (site1Name, null, null);\r
45                         IPHostEntry entry = Dns.EndGetHostByName (async);\r
46                         SubTestValidIPHostEntry (entry);\r
47                         Assert.AreEqual ("www.go-mono.com", entry.HostName);\r
48                 }\r
49 \r
50                 void GetHostByNameCallback (IAsyncResult ar)\r
51                 {\r
52                         IPHostEntry h;\r
53                         h = Dns.EndGetHostByName (ar);\r
54                         SubTestValidIPHostEntry (h);\r
55                 }\r
56 \r
57                 [Test]\r
58                 public void AsyncResolve ()\r
59                 {\r
60                         IAsyncResult r;\r
61                         r = Dns.BeginResolve (site1Name, new AsyncCallback (ResolveCallback), null);\r
62 \r
63                         IAsyncResult async = Dns.BeginResolve (site1Dot, null, null);\r
64                         IPHostEntry entry = Dns.EndResolve (async);\r
65                         SubTestValidIPHostEntry (entry);\r
66                         Assert.AreEqual (site1Dot, entry.AddressList [0].ToString ());\r
67                 }\r
68 \r
69                 void ResolveCallback (IAsyncResult ar)\r
70                 {\r
71                         IPHostEntry h = Dns.EndResolve (ar);\r
72                         SubTestValidIPHostEntry (h);\r
73                 }\r
74 \r
75 #if NET_2_0\r
76                 [Test]\r
77                 public void BeginGetHostAddresses_HostNameOrAddress_Null ()\r
78                 {\r
79                         try {\r
80                                 Dns.BeginGetHostAddresses (\r
81                                         (string) null,\r
82                                         new AsyncCallback (GetHostAddressesCallback),\r
83                                         null);\r
84                                 Assert.Fail ("#1");\r
85                         } catch (ArgumentNullException ex) {\r
86                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
87                                 Assert.IsNull (ex.InnerException, "#3");\r
88                                 Assert.IsNotNull (ex.Message, "#4");\r
89                                 Assert.AreEqual ("hostName", ex.ParamName, "#5");\r
90                         }\r
91                 }\r
92 \r
93                 [Test]\r
94                 public void BeginGetHostAddresses_HostNameOrAddress_UnspecifiedAddress ()\r
95                 {\r
96                         // IPv4\r
97                         try {\r
98                                 Dns.BeginGetHostAddresses (\r
99                                         "0.0.0.0",\r
100                                         new AsyncCallback (GetHostAddressesCallback),\r
101                                         null);\r
102                                 Assert.Fail ("#A1");\r
103                         } catch (ArgumentException ex) {\r
104                                 // IPv4 address 0.0.0.0 and IPv6 address ::0 are\r
105                                 // unspecified addresses that cannot be used as\r
106                                 // a target address\r
107                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");\r
108                                 Assert.IsNull (ex.InnerException, "#A3");\r
109                                 Assert.IsNotNull (ex.Message, "#A4");\r
110                                 Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#A5");\r
111                         }\r
112 \r
113                         // IPv6\r
114                         try {\r
115                                 Dns.BeginGetHostAddresses (\r
116                                         "::0",\r
117                                         new AsyncCallback (GetHostAddressesCallback),\r
118                                         null);\r
119                                 Assert.Fail ("#B1");\r
120                         } catch (ArgumentException ex) {\r
121                                 // IPv4 address 0.0.0.0 and IPv6 address ::0 are\r
122                                 // unspecified addresses that cannot be used as\r
123                                 // a target address\r
124                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");\r
125                                 Assert.IsNull (ex.InnerException, "#B3");\r
126                                 Assert.IsNotNull (ex.Message, "#B4");\r
127                                 Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#B5");\r
128                         }\r
129                 }\r
130 \r
131                 void GetHostAddressesCallback (IAsyncResult ar)\r
132                 {\r
133                         IPAddress [] addresses = Dns.EndGetHostAddresses (ar);\r
134                         Assert.IsNotNull (addresses);\r
135                 }\r
136 \r
137                 [Test]\r
138                 public void GetHostAddresses_HostNameOrAddress_Null ()\r
139                 {\r
140                         try {\r
141                                 Dns.GetHostAddresses ((string) null);\r
142                                 Assert.Fail ("#1");\r
143                         } catch (ArgumentNullException ex) {\r
144                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
145                                 Assert.IsNull (ex.InnerException, "#3");\r
146                                 Assert.IsNotNull (ex.Message, "#4");\r
147                                 Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#5");\r
148                         }\r
149                 }\r
150 \r
151                 [Test]\r
152                 public void GetHostAddresses_HostNameOrAddress_UnspecifiedAddress ()\r
153                 {\r
154                         // IPv4\r
155                         try {\r
156                                 Dns.GetHostAddresses ("0.0.0.0");\r
157                                 Assert.Fail ("#A1");\r
158                         } catch (ArgumentException ex) {\r
159                                 // IPv4 address 0.0.0.0 and IPv6 address ::0 are\r
160                                 // unspecified addresses that cannot be used as\r
161                                 // a target address\r
162                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");\r
163                                 Assert.IsNull (ex.InnerException, "#A3");\r
164                                 Assert.IsNotNull (ex.Message, "#A4");\r
165                                 Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#A5");\r
166                         }\r
167 \r
168                         // IPv6\r
169                         try {\r
170                                 Dns.GetHostAddresses ("::0");\r
171                                 Assert.Fail ("#B1");\r
172                         } catch (ArgumentException ex) {\r
173                                 // IPv4 address 0.0.0.0 and IPv6 address ::0 are\r
174                                 // unspecified addresses that cannot be used as\r
175                                 // a target address\r
176                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");\r
177                                 Assert.IsNull (ex.InnerException, "#B3");\r
178                                 Assert.IsNotNull (ex.Message, "#B4");\r
179                                 Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#B5");\r
180                         }\r
181                 }\r
182 #endif\r
183 \r
184                 [Test]\r
185                 public void GetHostName ()\r
186                 {\r
187                         string hostName = Dns.GetHostName ();\r
188                         Assert.IsNotNull (hostName);\r
189                 }\r
190 \r
191                 [Test]\r
192                 public void GetHostByName ()\r
193                 {\r
194                         SubTestGetHostByName (site1Name, site1Dot);\r
195                         SubTestGetHostByName (site2Name, site2Dot);\r
196                         try {\r
197                                 Dns.GetHostByName (noneExistingSite);\r
198                                 Assert.Fail ("Should raise a SocketException (assuming that '" + noneExistingSite + "' does not exist)");\r
199                         } catch (SocketException) {\r
200                         }\r
201                 }\r
202 \r
203                 void SubTestGetHostByName (string siteName, string siteDot)\r
204                 {\r
205                         IPHostEntry h = Dns.GetHostByName (siteName);\r
206                         SubTestValidIPHostEntry (h);\r
207                         Assert.AreEqual (siteName, h.HostName, "siteName");\r
208                         Assert.AreEqual (siteDot, h.AddressList [0].ToString (), "siteDot");\r
209                 }\r
210 \r
211                 [Test]\r
212                 public void GetHostByName_HostName_Null ()\r
213                 {\r
214                         try {\r
215                                 Dns.GetHostByName ((string) null);\r
216                                 Assert.Fail ("#1");\r
217                         } catch (ArgumentNullException ex) {\r
218                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
219                                 Assert.IsNull (ex.InnerException, "#3");\r
220                                 Assert.IsNotNull (ex.Message, "#4");\r
221                                 Assert.AreEqual ("hostName", ex.ParamName, "#5");\r
222                         }\r
223                 }\r
224 \r
225                 [Test]\r
226                 public void GetHostByAddressString_Address_Null ()\r
227                 {\r
228                         try {\r
229                                 Dns.GetHostByAddress ((string) null);\r
230                                 Assert.Fail ("#1");\r
231                         } catch (ArgumentNullException ex) {\r
232                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
233                                 Assert.IsNull (ex.InnerException, "#3");\r
234                                 Assert.IsNotNull (ex.Message, "#4");\r
235                                 Assert.AreEqual ("address", ex.ParamName, "#5");\r
236                         }\r
237                 }\r
238 \r
239                 [Test]\r
240                 [ExpectedException (typeof (SocketException))]\r
241 #if TARGET_JVM\r
242                 [Ignore ("Ignore failures in Sys.Net")]\r
243 #endif\r
244                 public void GetHostByAddressString2 ()\r
245                 {\r
246                         Dns.GetHostByAddress ("123.255.23");\r
247                 }\r
248 \r
249                 [Test]\r
250                 [ExpectedException (typeof (FormatException))]\r
251                 public void GetHostByAddressString3 ()\r
252                 {\r
253                         Dns.GetHostByAddress ("123.256.34.10");\r
254                 }\r
255 \r
256                 [Test, ExpectedException (typeof (FormatException))]\r
257                 public void GetHostByAddressString4 ()\r
258                 {\r
259                         Dns.GetHostByAddress ("not an IP address");\r
260                 }\r
261 \r
262                 [Test]\r
263 /*** Current go-mono.com IP works fine here***\r
264 #if ONLY_1_1\r
265                 [ExpectedException (typeof (SocketException))]\r
266 #endif\r
267 ********/\r
268                 public void GetHostByAddressString5 ()\r
269                 {\r
270                         Dns.GetHostByAddress (site1Dot);\r
271                 }\r
272 \r
273                 [Test]\r
274                 public void GetHostByAddressIPAddress_Address_Null ()\r
275                 {\r
276                         try {\r
277                                 Dns.GetHostByAddress ((IPAddress) null);\r
278                                 Assert.Fail ("#1");\r
279                         } catch (ArgumentNullException ex) {\r
280                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
281                                 Assert.IsNull (ex.InnerException, "#3");\r
282                                 Assert.IsNotNull (ex.Message, "#4");\r
283                                 Assert.AreEqual ("address", ex.ParamName, "#5");\r
284                         }\r
285                 }\r
286 \r
287                 [Test]\r
288                 [Ignore ("Fails on both Mono and MS")]\r
289                 public void GetHostByAddressIPAddress2 ()\r
290                 {\r
291                         IPAddress addr = new IPAddress (IPAddress.NetworkToHostOrder ((int) site1IP));\r
292                         IPHostEntry h = Dns.GetHostByAddress (addr);\r
293                         SubTestValidIPHostEntry (h);\r
294                         Assert.AreEqual (addr.ToString (), h.AddressList [0].ToString ());\r
295                 }\r
296 \r
297                 [Test]\r
298                 public void GetHostByAddressIPAddress3 ()\r
299                 {\r
300                         IPAddress addr = new IPAddress (IPAddress.NetworkToHostOrder ((int) site2IP));\r
301                         IPHostEntry h = Dns.GetHostByAddress (addr);\r
302                         SubTestValidIPHostEntry (h);\r
303                         Assert.AreEqual (addr.ToString (), h.AddressList [0].ToString ());\r
304                 }\r
305 \r
306                 [Test]\r
307                 public void BeginResolve_HostName_Null ()\r
308                 {\r
309                         try {\r
310                                 Dns.BeginResolve ((string) null,\r
311                                         new AsyncCallback (ResolveCallback),\r
312                                         null);\r
313                                 Assert.Fail ("#1");\r
314                         } catch (ArgumentNullException ex) {\r
315                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
316                                 Assert.IsNull (ex.InnerException, "#3");\r
317                                 Assert.IsNotNull (ex.Message, "#4");\r
318                                 Assert.AreEqual ("hostName", ex.ParamName, "#5");\r
319                         }\r
320                 }\r
321 \r
322                 [Test]\r
323                 public void Resolve ()\r
324                 {\r
325                         SubTestResolve (site1Name);\r
326                         SubTestResolve (site2Name);\r
327                         SubTestResolve (site1Dot);\r
328                         SubTestResolve (site2Dot);\r
329                 }\r
330 \r
331                 void SubTestResolve (string addr)\r
332                 {\r
333                         IPHostEntry h = Dns.Resolve (addr);\r
334                         SubTestValidIPHostEntry (h);\r
335                 }\r
336 \r
337                 [Test]\r
338                 public void Resolve_HostName_Null ()\r
339                 {\r
340                         try {\r
341                                 Dns.Resolve ((string) null);\r
342                                 Assert.Fail ("#1");\r
343                         } catch (ArgumentNullException ex) {\r
344                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
345                                 Assert.IsNull (ex.InnerException, "#3");\r
346                                 Assert.IsNotNull (ex.Message, "#4");\r
347                                 Assert.AreEqual ("hostName", ex.ParamName, "#5");\r
348                         }\r
349                 }\r
350 \r
351 #if NET_2_0\r
352                 [Test] // BeginGetHostEntry (IPAddress, AsyncCallback, Object)\r
353                 public void BeginGetHostEntry1_Address_Null ()\r
354                 {\r
355                         try {\r
356                                 Dns.BeginGetHostEntry (\r
357                                         (IPAddress) null,\r
358                                         new AsyncCallback (GetHostAddressesCallback),\r
359                                         null);\r
360                                 Assert.Fail ("#1");\r
361                         } catch (ArgumentNullException ex) {\r
362                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
363                                 Assert.IsNull (ex.InnerException, "#3");\r
364                                 Assert.IsNotNull (ex.Message, "#4");\r
365                                 Assert.AreEqual ("address", ex.ParamName, "#5");\r
366                         }\r
367                 }\r
368 \r
369                 [Test] // BeginGetHostEntry (String, AsyncCallback, Object)\r
370                 public void BeginGetHostEntry2_HostNameOrAddress_Null ()\r
371                 {\r
372                         try {\r
373                                 Dns.BeginGetHostEntry (\r
374                                         (string) null,\r
375                                         new AsyncCallback (GetHostAddressesCallback),\r
376                                         null);\r
377                                 Assert.Fail ("#1");\r
378                         } catch (ArgumentNullException ex) {\r
379                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
380                                 Assert.IsNull (ex.InnerException, "#3");\r
381                                 Assert.IsNotNull (ex.Message, "#4");\r
382                                 Assert.AreEqual ("hostName", ex.ParamName, "#5");\r
383                         }\r
384                 }\r
385 \r
386                 [Test] // BeginGetHostEntry (String, AsyncCallback, Object)\r
387                 public void BeginGetHostEntry2_HostNameOrAddress_UnspecifiedAddress ()\r
388                 {\r
389                         // IPv4\r
390                         try {\r
391                                 Dns.BeginGetHostEntry (\r
392                                         "0.0.0.0",\r
393                                         new AsyncCallback (GetHostEntryCallback),\r
394                                         null);\r
395                                 Assert.Fail ("#1");\r
396                         } catch (ArgumentException ex) {\r
397                                 // IPv4 address 0.0.0.0 and IPv6 address ::0 are\r
398                                 // unspecified addresses that cannot be used as\r
399                                 // a target address\r
400                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");\r
401                                 Assert.IsNull (ex.InnerException, "#3");\r
402                                 Assert.IsNotNull (ex.Message, "#4");\r
403                                 Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#5");\r
404                         }\r
405 \r
406                         // IPv6\r
407                         try {\r
408                                 Dns.BeginGetHostEntry (\r
409                                         "::0",\r
410                                         new AsyncCallback (GetHostEntryCallback),\r
411                                         null);\r
412                                 Assert.Fail ("#1");\r
413                         } catch (ArgumentException ex) {\r
414                                 // IPv4 address 0.0.0.0 and IPv6 address ::0 are\r
415                                 // unspecified addresses that cannot be used as\r
416                                 // a target address\r
417                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");\r
418                                 Assert.IsNull (ex.InnerException, "#3");\r
419                                 Assert.IsNotNull (ex.Message, "#4");\r
420                                 Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#5");\r
421                         }\r
422                 }\r
423 \r
424                 void GetHostEntryCallback (IAsyncResult ar)\r
425                 {\r
426                         IPHostEntry hostEntry = Dns.EndGetHostEntry (ar);\r
427                         Assert.IsNotNull (hostEntry);\r
428                 }\r
429 \r
430                 [Test] // GetHostEntry (IPAddress)\r
431                 public void GetHostEntry1_Address_Null ()\r
432                 {\r
433                         try {\r
434                                 Dns.GetHostEntry ((IPAddress) null);\r
435                                 Assert.Fail ("#1");\r
436                         } catch (ArgumentNullException ex) {\r
437                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
438                                 Assert.IsNull (ex.InnerException, "#3");\r
439                                 Assert.IsNotNull (ex.Message, "#4");\r
440                                 Assert.AreEqual ("address", ex.ParamName, "#5");\r
441                         }\r
442                 }\r
443 \r
444                 [Test] // GetHostEntry (String)\r
445                 public void GetHostEntry2 ()\r
446                 {\r
447                         Dns.GetHostEntry (site1Name); // hostname\r
448                         Dns.GetHostEntry (site1Dot); // IP address\r
449                 }\r
450 \r
451                 [Test] // GetHostEntry (String)\r
452                 public void GetHostEntry2_HostNameOrAddress_Null ()\r
453                 {\r
454                         try {\r
455                                 Dns.GetHostEntry ((string) null);\r
456                                 Assert.Fail ("#1");\r
457                         } catch (ArgumentNullException ex) {\r
458                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
459                                 Assert.IsNull (ex.InnerException, "#3");\r
460                                 Assert.IsNotNull (ex.Message, "#4");\r
461                                 Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#5");\r
462                         }\r
463                 }\r
464 \r
465                 [Test] // GetHostEntry (String)\r
466                 public void GetHostEntry2_HostNameOrAddress_UnspecifiedAddress ()\r
467                 {\r
468                         // IPv4\r
469                         try {\r
470                                 Dns.GetHostEntry ("0.0.0.0");\r
471                                 Assert.Fail ("#A1");\r
472                         } catch (ArgumentException ex) {\r
473                                 // IPv4 address 0.0.0.0 and IPv6 address ::0 are\r
474                                 // unspecified addresses that cannot be used as\r
475                                 // a target address\r
476                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");\r
477                                 Assert.IsNull (ex.InnerException, "#A3");\r
478                                 Assert.IsNotNull (ex.Message, "#A4");\r
479                                 Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#A5");\r
480                         }\r
481 \r
482                         // IPv6\r
483                         try {\r
484                                 Dns.GetHostEntry ("::0");\r
485                                 Assert.Fail ("#B1");\r
486                         } catch (ArgumentException ex) {\r
487                                 // IPv4 address 0.0.0.0 and IPv6 address ::0 are\r
488                                 // unspecified addresses that cannot be used as\r
489                                 // a target address\r
490                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");\r
491                                 Assert.IsNull (ex.InnerException, "#B3");\r
492                                 Assert.IsNotNull (ex.Message, "#B4");\r
493                                 Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#B5");\r
494                         }\r
495                 }\r
496 #endif\r
497 \r
498                 void SubTestValidIPHostEntry (IPHostEntry h)\r
499                 {\r
500                         Assert.IsNotNull (h.HostName, "HostName not null");\r
501                         Assert.IsNotNull (h.AddressList, "AddressList not null");\r
502                         Assert.IsTrue (h.AddressList.Length > 0, "AddressList.Length");\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