1 // DnsTest.cs - NUnit Test Cases for the System.Net.Dns class
\r
4 // Mads Pultz (mpultz@diku.dk)
\r
5 // Martin Willemoes Hansen (mwh@sysrq.dk)
\r
7 // (C) 2001 Mads Pultz
\r
8 // (C) 2003 Martin Willemoes Hansen
\r
14 using System.Net.Sockets;
\r
15 using System.Threading;
\r
17 using NUnit.Framework;
\r
19 namespace MonoTests.System.Net
\r
22 [Category ("RequiresBSDSockets")]
\r
23 public class DnsTest
\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
33 public void AsyncGetHostByName ()
\r
36 r = Dns.BeginGetHostByName (site1Name, new AsyncCallback (GetHostByNameCallback), null);
\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
44 void GetHostByNameCallback (IAsyncResult ar)
\r
47 h = Dns.EndGetHostByName (ar);
\r
48 SubTestValidIPHostEntry (h);
\r
52 public void AsyncResolve ()
\r
55 r = Dns.BeginResolve (site1Name, new AsyncCallback (ResolveCallback), null);
\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
64 void ResolveCallback (IAsyncResult ar)
\r
66 IPHostEntry h = Dns.EndResolve (ar);
\r
67 SubTestValidIPHostEntry (h);
\r
71 public void BeginGetHostAddresses_HostNameOrAddress_Null ()
\r
74 Dns.BeginGetHostAddresses (
\r
76 new AsyncCallback (GetHostAddressesCallback),
\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
88 public void BeginGetHostAddresses_HostNameOrAddress_UnspecifiedAddress ()
\r
92 Dns.BeginGetHostAddresses (
\r
94 new AsyncCallback (GetHostAddressesCallback),
\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
109 Dns.BeginGetHostAddresses (
\r
111 new AsyncCallback (GetHostAddressesCallback),
\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
125 void GetHostAddressesCallback (IAsyncResult ar)
\r
127 IPAddress [] addresses = Dns.EndGetHostAddresses (ar);
\r
128 Assert.IsNotNull (addresses);
\r
132 public void GetHostAddresses_HostNameOrAddress_Null ()
\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
146 public void GetHostAddresses_HostNameOrAddress_UnspecifiedAddress ()
\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
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
178 public void GetHostName ()
\r
180 string hostName = Dns.GetHostName ();
\r
181 Assert.IsNotNull (hostName);
\r
185 public void GetHostByName ()
\r
187 SubTestGetHostByName (site1Name, site1Dot);
\r
188 SubTestGetHostByName (site2Name, site2Dot);
\r
190 var entry = Dns.GetHostByName (noneExistingSite);
\r
192 * Work around broken t-online.de DNS Server.
\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
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
205 Assert.Fail ("Should raise a SocketException (assuming that '" + noneExistingSite + "' does not exist)");
\r
206 } catch (SocketException) {
\r
210 static IPAddress GetIPv4Address (IPHostEntry h)
\r
212 var al = h.AddressList.FirstOrDefault (x => x.AddressFamily == AddressFamily.InterNetwork);
\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
218 void SubTestGetHostByName (string siteName, string siteDot)
\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
228 public void GetHostByName_HostName_Null ()
\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
242 public void GetHostByAddressString_Address_Null ()
\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
256 [ExpectedException (typeof (SocketException))]
\r
257 public void GetHostByAddressString2 ()
\r
259 Dns.GetHostByAddress ("123.255.23");
\r
263 [ExpectedException (typeof (FormatException))]
\r
264 public void GetHostByAddressString3 ()
\r
266 Dns.GetHostByAddress ("123.256.34.10");
\r
269 [Test, ExpectedException (typeof (FormatException))]
\r
270 public void GetHostByAddressString4 ()
\r
272 Dns.GetHostByAddress ("not an IP address");
\r
276 public void GetHostByAddressString5 ()
\r
278 Dns.GetHostByAddress (site1Dot);
\r
282 public void GetHostByAddressIPAddress_Address_Null ()
\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
296 public void GetHostByAddressIPAddress2 ()
\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
306 public void GetHostByAddressIPAddress3 ()
\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
316 public void BeginResolve_HostName_Null ()
\r
319 Dns.BeginResolve ((string) null,
\r
320 new AsyncCallback (ResolveCallback),
\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
332 public void Resolve ()
\r
334 SubTestResolve (site1Name);
\r
335 SubTestResolve (site2Name);
\r
336 SubTestResolve (site1Dot);
\r
337 SubTestResolve (site2Dot);
\r
340 void SubTestResolve (string addr)
\r
342 IPHostEntry h = Dns.Resolve (addr);
\r
343 SubTestValidIPHostEntry (h);
\r
347 public void Resolve_HostName_Null ()
\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
360 [Test] // BeginGetHostEntry (IPAddress, AsyncCallback, Object)
\r
361 public void BeginGetHostEntry1_Address_Null ()
\r
364 Dns.BeginGetHostEntry (
\r
366 new AsyncCallback (GetHostAddressesCallback),
\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
377 [Test] // BeginGetHostEntry (String, AsyncCallback, Object)
\r
378 public void BeginGetHostEntry2_HostNameOrAddress_Null ()
\r
381 Dns.BeginGetHostEntry (
\r
383 new AsyncCallback (GetHostAddressesCallback),
\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
394 [Test] // BeginGetHostEntry (String, AsyncCallback, Object)
\r
395 public void BeginGetHostEntry2_HostNameOrAddress_UnspecifiedAddress ()
\r
399 Dns.BeginGetHostEntry (
\r
401 new AsyncCallback (GetHostEntryCallback),
\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
416 Dns.BeginGetHostEntry (
\r
418 new AsyncCallback (GetHostEntryCallback),
\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
432 void GetHostEntryCallback (IAsyncResult ar)
\r
434 IPHostEntry hostEntry = Dns.EndGetHostEntry (ar);
\r
435 Assert.IsNotNull (hostEntry);
\r
438 [Test] // GetHostEntry (IPAddress)
\r
439 public void GetHostEntry1_Address_Null ()
\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
452 [Test] // GetHostEntry (String)
\r
453 public void GetHostEntry2 ()
\r
455 Dns.GetHostEntry (site1Name); // hostname
\r
456 Dns.GetHostEntry (site1Dot); // IP address
\r
459 [Test] // GetHostEntry (String)
\r
460 public void GetHostEntry2_HostNameOrAddress_Null ()
\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
473 [Test] // GetHostEntry (String)
\r
474 public void GetHostEntry2_HostNameOrAddress_UnspecifiedAddress ()
\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
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
505 void SubTestValidIPHostEntry (IPHostEntry h)
\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
513 public void GetHostEntry_StringEmpty ()
\r
515 Dns.GetHostEntry (string.Empty);
\r
518 /* This isn't used anymore, but could be useful for debugging
\r
519 static void printIPHostEntry(IPHostEntry h)
\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