DnsTest.cs 16 KB

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