DnsTest.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 NUnit.Framework;
  18. using System;
  19. using System.Net;
  20. using System.Net.Sockets;
  21. using System.Threading;
  22. using System.Collections;
  23. namespace MonoTests.System.Net {
  24. [TestFixture]
  25. public class DnsTest : Assertion {
  26. private String site1Name = "www.go-mono.com",
  27. site1Dot = "64.14.94.188",
  28. site2Name = "info.diku.dk",
  29. site2Dot = "130.225.96.4",
  30. noneExistingSite = "www.unlikely.novell.com";
  31. private uint site1IP = 2180692201, site2IP = 2195808260; // Big-Endian
  32. private void GetHostByNameCallback (IAsyncResult ar)
  33. {
  34. IPHostEntry h;
  35. h = Dns.EndGetHostByName(ar);
  36. SubTestValidIPHostEntry(h);
  37. }
  38. [Test]
  39. public void AsyncGetHostByName(){
  40. IAsyncResult r;
  41. r = Dns.BeginGetHostByName (site1Name, new AsyncCallback (GetHostByNameCallback), null);
  42. IAsyncResult async = Dns.BeginGetHostByName (site1Name, null, null);
  43. IPHostEntry entry = Dns.EndGetHostByName (async);
  44. SubTestValidIPHostEntry(entry);
  45. AssertEquals ("#1", "www.go-mono.com", entry.HostName);
  46. }
  47. private void ResolveCallback (IAsyncResult ar)
  48. {
  49. IPHostEntry h = Dns.EndResolve (ar);
  50. SubTestValidIPHostEntry (h);
  51. }
  52. [Test]
  53. public void AsyncResolve() {
  54. IAsyncResult r;
  55. r = Dns.BeginResolve (site1Name, new AsyncCallback (ResolveCallback), null);
  56. IAsyncResult async = Dns.BeginResolve (site1Dot, null, null);
  57. IPHostEntry entry = Dns.EndResolve (async);
  58. SubTestValidIPHostEntry(entry);
  59. AssertEquals ("#1", site1Dot, entry.HostName);
  60. }
  61. [Test]
  62. public void GetHostName() {
  63. string hostName = Dns.GetHostName();
  64. AssertNotNull (hostName);
  65. }
  66. private void SubTestGetHostByName(string siteName, string siteDot) {
  67. IPHostEntry h = Dns.GetHostByName(siteName);
  68. SubTestValidIPHostEntry(h);
  69. AssertEquals ("siteName", siteName, h.HostName);
  70. AssertEquals ("siteDot", siteDot, h.AddressList[0].ToString());
  71. }
  72. [Test]
  73. public void GetHostByName() {
  74. SubTestGetHostByName(site1Name, site1Dot);
  75. SubTestGetHostByName(site2Name, site2Dot);
  76. try {
  77. Dns.GetHostByName(noneExistingSite);
  78. Fail("Should raise a SocketException (assuming that '" + noneExistingSite + "' does not exist)");
  79. } catch (SocketException) {
  80. }
  81. try {
  82. Dns.GetHostByName(null);
  83. Fail("Should raise an ArgumentNullException");
  84. } catch (ArgumentNullException) {
  85. }
  86. }
  87. private void SubTestGetHostByAddressString(string addr) {
  88. IPHostEntry h = Dns.GetHostByAddress(addr);
  89. SubTestValidIPHostEntry(h);
  90. }
  91. [Test, ExpectedException (typeof (ArgumentNullException))]
  92. public void GetHostByAddressString1() {
  93. String addr = null;
  94. Dns.GetHostByAddress(addr);
  95. }
  96. [Test, ExpectedException (typeof (SocketException))]
  97. public void GetHostByAddressString2() {
  98. Dns.GetHostByAddress ("123.255.23");
  99. }
  100. [Test, ExpectedException (typeof (FormatException))]
  101. public void GetHostByAddressString3() {
  102. Dns.GetHostByAddress ("123.256.34.10");
  103. }
  104. [Test, ExpectedException (typeof (FormatException))]
  105. public void GetHostByAddressString4() {
  106. Dns.GetHostByAddress ("not an IP address");
  107. }
  108. [Test, ExpectedException (typeof (SocketException))]
  109. public void GetHostByAddressString5() {
  110. Dns.GetHostByAddress (site1Dot);
  111. }
  112. [Test, ExpectedException (typeof (ArgumentNullException))]
  113. public void GetHostByAddressIPAddress1() {
  114. IPAddress addr = null;
  115. Dns.GetHostByAddress(addr);
  116. }
  117. public void GetHostByAddressIPAddress2() {
  118. IPAddress addr = new IPAddress(IPAddress.NetworkToHostOrder((int)site1IP));
  119. IPHostEntry h = Dns.GetHostByAddress(addr);
  120. SubTestValidIPHostEntry(h);
  121. AssertEquals (addr.ToString(), h.AddressList[0].ToString());
  122. }
  123. public void GetHostByAddressIPAddress3() {
  124. IPAddress addr = new IPAddress(IPAddress.NetworkToHostOrder((int)site2IP));
  125. IPHostEntry h = Dns.GetHostByAddress(addr);
  126. SubTestValidIPHostEntry(h);
  127. AssertEquals (addr.ToString(), h.AddressList[0].ToString());
  128. }
  129. private void SubTestResolve(string addr) {
  130. IPHostEntry h = Dns.Resolve(addr);
  131. SubTestValidIPHostEntry(h);
  132. }
  133. [Test]
  134. public void Resolve() {
  135. SubTestResolve(site1Name);
  136. SubTestResolve(site2Name);
  137. SubTestResolve(site1Dot);
  138. SubTestResolve(site2Dot);
  139. }
  140. #if NET_2_0
  141. [Test]
  142. public void GetHostEntry ()
  143. {
  144. Dns.GetHostEntry (site1Name); // hostname
  145. Dns.GetHostEntry (site1Dot); // IP address
  146. }
  147. #endif
  148. private void SubTestValidIPHostEntry(IPHostEntry h) {
  149. AssertNotNull ("HostName not null", h.HostName);
  150. AssertNotNull ("AddressList not null", h.AddressList);
  151. Assert ("AddressList.Length", h.AddressList.Length > 0);
  152. }
  153. /* This isn't used anymore, but could be useful for debugging
  154. private static void printIPHostEntry(IPHostEntry h)
  155. {
  156. Console.WriteLine("----------------------------------------------------");
  157. Console.WriteLine("Host name:");
  158. Console.WriteLine(h.HostName);
  159. Console.WriteLine("IP addresses:");
  160. IPAddress[] list = h.AddressList;
  161. for(int i = 0; i < list.Length; ++i)
  162. Console.WriteLine(list[i]);
  163. Console.WriteLine("Aliases:");
  164. string[] aliases = h.Aliases;
  165. for(int i = 0; i < aliases.Length; ++i)
  166. Console.WriteLine(aliases[i]);
  167. Console.WriteLine("----------------------------------------------------");
  168. }
  169. */
  170. }
  171. }