DnsTest.cs 17 KB

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