DnsTest.cs 16 KB

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