DnsTest.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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 = "jenkins.mono-project.com",
  29. site1Dot = "162.253.133.196",
  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 == "jenkins.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 ("jenkins.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. 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. Assert.AreEqual (addr.ToString (), h.AddressList [0].ToString ());
  273. }
  274. [Test]
  275. public void GetHostByAddressIPAddress3 ()
  276. {
  277. IPAddress addr = new IPAddress (IPAddress.NetworkToHostOrder ((int) site2IP));
  278. IPHostEntry h = Dns.GetHostByAddress (addr);
  279. SubTestValidIPHostEntry (h);
  280. Assert.AreEqual (addr.ToString (), h.AddressList [0].ToString ());
  281. }
  282. [Test]
  283. public void BeginResolve_HostName_Null ()
  284. {
  285. try {
  286. Dns.BeginResolve ((string) null,
  287. new AsyncCallback (ResolveCallback),
  288. null);
  289. Assert.Fail ("#1");
  290. } catch (ArgumentNullException ex) {
  291. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  292. Assert.IsNull (ex.InnerException, "#3");
  293. Assert.IsNotNull (ex.Message, "#4");
  294. Assert.AreEqual ("hostName", ex.ParamName, "#5");
  295. }
  296. }
  297. [Test]
  298. public void Resolve ()
  299. {
  300. SubTestResolve (site1Name);
  301. SubTestResolve (site2Name);
  302. SubTestResolve (site1Dot);
  303. SubTestResolve (site2Dot);
  304. }
  305. void SubTestResolve (string addr)
  306. {
  307. IPHostEntry h = Dns.Resolve (addr);
  308. SubTestValidIPHostEntry (h);
  309. }
  310. [Test]
  311. public void Resolve_HostName_Null ()
  312. {
  313. try {
  314. Dns.Resolve ((string) null);
  315. Assert.Fail ("#1");
  316. } catch (ArgumentNullException ex) {
  317. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  318. Assert.IsNull (ex.InnerException, "#3");
  319. Assert.IsNotNull (ex.Message, "#4");
  320. Assert.AreEqual ("hostName", ex.ParamName, "#5");
  321. }
  322. }
  323. [Test] // BeginGetHostEntry (IPAddress, AsyncCallback, Object)
  324. public void BeginGetHostEntry1_Address_Null ()
  325. {
  326. try {
  327. Dns.BeginGetHostEntry (
  328. (IPAddress) null,
  329. new AsyncCallback (GetHostAddressesCallback),
  330. null);
  331. Assert.Fail ("#1");
  332. } catch (ArgumentNullException ex) {
  333. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  334. Assert.IsNull (ex.InnerException, "#3");
  335. Assert.IsNotNull (ex.Message, "#4");
  336. Assert.AreEqual ("address", ex.ParamName, "#5");
  337. }
  338. }
  339. [Test] // BeginGetHostEntry (String, AsyncCallback, Object)
  340. public void BeginGetHostEntry2_HostNameOrAddress_Null ()
  341. {
  342. try {
  343. Dns.BeginGetHostEntry (
  344. (string) null,
  345. new AsyncCallback (GetHostAddressesCallback),
  346. null);
  347. Assert.Fail ("#1");
  348. } catch (ArgumentNullException ex) {
  349. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  350. Assert.IsNull (ex.InnerException, "#3");
  351. Assert.IsNotNull (ex.Message, "#4");
  352. Assert.AreEqual ("hostName", ex.ParamName, "#5");
  353. }
  354. }
  355. [Test] // BeginGetHostEntry (String, AsyncCallback, Object)
  356. public void BeginGetHostEntry2_HostNameOrAddress_UnspecifiedAddress ()
  357. {
  358. // IPv4
  359. try {
  360. Dns.BeginGetHostEntry (
  361. "0.0.0.0",
  362. new AsyncCallback (GetHostEntryCallback),
  363. null);
  364. Assert.Fail ("#1");
  365. } catch (ArgumentException ex) {
  366. // IPv4 address 0.0.0.0 and IPv6 address ::0 are
  367. // unspecified addresses that cannot be used as
  368. // a target address
  369. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  370. Assert.IsNull (ex.InnerException, "#3");
  371. Assert.IsNotNull (ex.Message, "#4");
  372. Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#5");
  373. }
  374. // IPv6
  375. try {
  376. Dns.BeginGetHostEntry (
  377. "::0",
  378. new AsyncCallback (GetHostEntryCallback),
  379. null);
  380. Assert.Fail ("#1");
  381. } catch (ArgumentException ex) {
  382. // IPv4 address 0.0.0.0 and IPv6 address ::0 are
  383. // unspecified addresses that cannot be used as
  384. // a target address
  385. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  386. Assert.IsNull (ex.InnerException, "#3");
  387. Assert.IsNotNull (ex.Message, "#4");
  388. Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#5");
  389. }
  390. }
  391. void GetHostEntryCallback (IAsyncResult ar)
  392. {
  393. IPHostEntry hostEntry = Dns.EndGetHostEntry (ar);
  394. Assert.IsNotNull (hostEntry);
  395. }
  396. [Test] // GetHostEntry (IPAddress)
  397. public void GetHostEntry1_Address_Null ()
  398. {
  399. try {
  400. Dns.GetHostEntry ((IPAddress) null);
  401. Assert.Fail ("#1");
  402. } catch (ArgumentNullException ex) {
  403. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  404. Assert.IsNull (ex.InnerException, "#3");
  405. Assert.IsNotNull (ex.Message, "#4");
  406. Assert.AreEqual ("address", ex.ParamName, "#5");
  407. }
  408. }
  409. [Test] // GetHostEntry (String)
  410. public void GetHostEntry2 ()
  411. {
  412. Dns.GetHostEntry (site1Name); // hostname
  413. Dns.GetHostEntry (site1Dot); // IP address
  414. }
  415. [Test] // GetHostEntry (String)
  416. public void GetHostEntry2_HostNameOrAddress_Null ()
  417. {
  418. try {
  419. Dns.GetHostEntry ((string) null);
  420. Assert.Fail ("#1");
  421. } catch (ArgumentNullException ex) {
  422. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  423. Assert.IsNull (ex.InnerException, "#3");
  424. Assert.IsNotNull (ex.Message, "#4");
  425. Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#5");
  426. }
  427. }
  428. [Test] // GetHostEntry (String)
  429. public void GetHostEntry2_HostNameOrAddress_UnspecifiedAddress ()
  430. {
  431. // IPv4
  432. try {
  433. Dns.GetHostEntry ("0.0.0.0");
  434. Assert.Fail ("#A1");
  435. } catch (ArgumentException ex) {
  436. // IPv4 address 0.0.0.0 and IPv6 address ::0 are
  437. // unspecified addresses that cannot be used as
  438. // a target address
  439. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  440. Assert.IsNull (ex.InnerException, "#A3");
  441. Assert.IsNotNull (ex.Message, "#A4");
  442. Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#A5");
  443. }
  444. // IPv6
  445. try {
  446. Dns.GetHostEntry ("::0");
  447. Assert.Fail ("#B1");
  448. } catch (ArgumentException ex) {
  449. // IPv4 address 0.0.0.0 and IPv6 address ::0 are
  450. // unspecified addresses that cannot be used as
  451. // a target address
  452. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  453. Assert.IsNull (ex.InnerException, "#B3");
  454. Assert.IsNotNull (ex.Message, "#B4");
  455. Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#B5");
  456. }
  457. }
  458. void SubTestValidIPHostEntry (IPHostEntry h)
  459. {
  460. Assert.IsNotNull (h.HostName, "HostName not null");
  461. Assert.IsNotNull (h.AddressList, "AddressList not null");
  462. Assert.IsTrue (h.AddressList.Length > 0, "AddressList.Length");
  463. }
  464. [Test]
  465. public void GetHostEntry_StringEmpty ()
  466. {
  467. Dns.GetHostEntry (string.Empty);
  468. }
  469. /* This isn't used anymore, but could be useful for debugging
  470. static void printIPHostEntry(IPHostEntry h)
  471. {
  472. Console.WriteLine("----------------------------------------------------");
  473. Console.WriteLine("Host name:");
  474. Console.WriteLine(h.HostName);
  475. Console.WriteLine("IP addresses:");
  476. IPAddress[] list = h.AddressList;
  477. for(int i = 0; i < list.Length; ++i)
  478. Console.WriteLine(list[i]);
  479. Console.WriteLine("Aliases:");
  480. string[] aliases = h.Aliases;
  481. for(int i = 0; i < aliases.Length; ++i)
  482. Console.WriteLine(aliases[i]);
  483. Console.WriteLine("----------------------------------------------------");
  484. }
  485. */
  486. }
  487. }