DnsTest.cs 17 KB

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