DnsTest.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // DnsTest.cs - NUnit Test Cases for the System.Net.Dns class
  2. //
  3. // Author: Mads Pultz ([email protected])
  4. //
  5. // (C) Mads Pultz, 2001
  6. //
  7. // This test assumes the following:
  8. // 1) The following Internet sites exist:
  9. // www.go-mono.com with IP address 129.250.184.233
  10. // info.diku.dk with IP address 130.225.96.4
  11. // 2) The following DNS name does not exist:
  12. // www.hopefullydoesnotexist.dk
  13. //
  14. using NUnit.Framework;
  15. using System;
  16. using System.Net;
  17. using System.Net.Sockets;
  18. using System.Threading;
  19. using System.Collections;
  20. namespace MonoTests.System.Net {
  21. public class DnsTest: TestCase {
  22. private String site1Name = "www.go-mono.com",
  23. site1Dot = "129.250.184.233",
  24. site2Name = "info.diku.dk",
  25. site2Dot = "130.225.96.4",
  26. noneExistingSite = "www.hopefullydoesnotexist.dk";
  27. private uint site1IP = 2180692201, site2IP = 2195808260; // Big-Endian
  28. public DnsTest(): base("MonoTests.System.Net.DnsTest testsuite") { }
  29. public DnsTest(String name): base(name) { }
  30. public static ITest Suite {
  31. get { return new TestSuite(typeof(DnsTest)); }
  32. }
  33. private void Callback(IAsyncResult ar) {
  34. IPHostEntry h;
  35. h = Dns.EndGetHostByName(ar);
  36. SubTestValidIPHostEntry(h);
  37. }
  38. public void TestAsyncGetHostByName(){
  39. IAsyncResult r;
  40. r = Dns.BeginGetHostByName(site1Name, new AsyncCallback(Callback), null);
  41. IAsyncResult async = Dns.BeginGetHostByName (site1Name, null, null);
  42. IPHostEntry entry = Dns.EndGetHostByName (async);
  43. SubTestValidIPHostEntry(entry);
  44. AssertEquals ("#1", "www.go-mono.com", entry.HostName);
  45. }
  46. public void TestAsyncResolve() {
  47. IAsyncResult r;
  48. r = Dns.BeginResolve(site1Name, new AsyncCallback(Callback), null);
  49. IAsyncResult async = Dns.BeginResolve (site1Dot, null, null);
  50. IPHostEntry entry = Dns.EndResolve (async);
  51. SubTestValidIPHostEntry(entry);
  52. AssertEquals ("#1", "129.250.184.233", entry.HostName);
  53. }
  54. public void TestGetHostName() {
  55. string hostName = Dns.GetHostName();
  56. Assert(hostName != null);
  57. }
  58. private void SubTestGetHostByName(string siteName, string siteDot) {
  59. IPHostEntry h = Dns.GetHostByName(siteName);
  60. SubTestValidIPHostEntry(h);
  61. Assert(h.HostName.Equals(siteName));
  62. Assert(h.AddressList[0].ToString() == siteDot);
  63. }
  64. public void TestGetHostByName() {
  65. SubTestGetHostByName(site1Name, site1Dot);
  66. SubTestGetHostByName(site2Name, site2Dot);
  67. try {
  68. Dns.GetHostByName(noneExistingSite);
  69. Fail("Should raise a SocketException (assuming that '" + noneExistingSite + "' does not exist)");
  70. } catch (SocketException) {
  71. }
  72. try {
  73. Dns.GetHostByName(null);
  74. Fail("Should raise an ArgumentNullException");
  75. } catch (ArgumentNullException) {
  76. }
  77. }
  78. private void SubTestGetHostByAddressStringFormatException(string addr) {
  79. try {
  80. Dns.GetHostByAddress(addr);
  81. Fail("Should raise a FormatException");
  82. } catch (FormatException) {
  83. }
  84. }
  85. private void SubTestGetHostByAddressString(string addr) {
  86. IPHostEntry h = Dns.GetHostByAddress(addr);
  87. SubTestValidIPHostEntry(h);
  88. }
  89. public void TestGetHostByAddressString() {
  90. try {
  91. String addr = null;
  92. Dns.GetHostByAddress(addr);
  93. Fail("Should raise an ArgumentNullException");
  94. } catch (ArgumentNullException) {
  95. }
  96. SubTestGetHostByAddressStringFormatException("123.255.23");
  97. SubTestGetHostByAddressStringFormatException("123.256.34.10");
  98. SubTestGetHostByAddressStringFormatException("not an IP address");
  99. SubTestGetHostByAddressString(site1Dot);
  100. SubTestGetHostByAddressString(site2Dot);
  101. }
  102. private void SubTestGetHostByAddressIPAddress(IPAddress addr) {
  103. IPHostEntry h = Dns.GetHostByAddress(addr);
  104. SubTestValidIPHostEntry(h);
  105. Assert(h.AddressList[0].ToString() == addr.ToString());
  106. }
  107. public void TestGetHostByAddressIPAddress() {
  108. try {
  109. IPAddress addr = null;
  110. Dns.GetHostByAddress(addr);
  111. Fail("Should raise an ArgumentNullException");
  112. } catch (ArgumentNullException) {
  113. }
  114. SubTestGetHostByAddressIPAddress(new IPAddress(IPAddress.NetworkToHostOrder((int)site1IP)));
  115. SubTestGetHostByAddressIPAddress(new IPAddress(IPAddress.NetworkToHostOrder((int)site2IP)));
  116. }
  117. private void SubTestResolve(string addr) {
  118. IPHostEntry h = Dns.Resolve(addr);
  119. SubTestValidIPHostEntry(h);
  120. }
  121. public void TestResolve() {
  122. SubTestResolve(site1Name);
  123. SubTestResolve(site2Name);
  124. SubTestResolve(site1Dot);
  125. SubTestResolve(site2Dot);
  126. }
  127. private void SubTestValidIPHostEntry(IPHostEntry h) {
  128. Assert(h.HostName != null);
  129. Assert(h.AddressList != null);
  130. Assert(h.AddressList.Length > 0);
  131. }
  132. private static void printIPHostEntry(IPHostEntry h)
  133. {
  134. Console.WriteLine("----------------------------------------------------");
  135. Console.WriteLine("Host name:");
  136. Console.WriteLine(h.HostName);
  137. Console.WriteLine("IP addresses:");
  138. IPAddress[] list = h.AddressList;
  139. for(int i = 0; i < list.Length; ++i)
  140. Console.WriteLine(list[i]);
  141. Console.WriteLine("Aliases:");
  142. string[] aliases = h.Aliases;
  143. for(int i = 0; i < aliases.Length; ++i)
  144. Console.WriteLine(aliases[i]);
  145. Console.WriteLine("----------------------------------------------------");
  146. }
  147. }
  148. }