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