DnsTest.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. public class DnsTest: TestCase {
  21. private String site1Name = "www.go-mono.com",
  22. site1Dot = "129.250.184.233",
  23. site2Name = "info.diku.dk",
  24. site2Dot = "130.225.96.4",
  25. noneExistingSite = "www.hopefullydoesnotexist.dk";
  26. private uint site1IP = 2180692201, site2IP = 2195808260; // Big-Endian
  27. public DnsTest(String name): base(name) {
  28. }
  29. public static ITest Suite {
  30. get { return new TestSuite(typeof(DnsTest)); }
  31. }
  32. private void Callback(IAsyncResult ar) {
  33. IPHostEntry h;
  34. h = System.Net.Dns.EndGetHostByName(ar);
  35. SubTestValidIPHostEntry(h);
  36. }
  37. public void TestAsyncGetHostByName(){
  38. IAsyncResult r;
  39. r = System.Net.Dns.BeginGetHostByName(site1Name, new AsyncCallback(Callback), null);
  40. }
  41. public void TestAsyncResolve() {
  42. IAsyncResult r;
  43. r = System.Net.Dns.BeginResolve(site1Name, new AsyncCallback(Callback), null);
  44. }
  45. public void TestGetHostName() {
  46. string hostName = System.Net.Dns.GetHostName();
  47. Assert(hostName != null);
  48. }
  49. private void SubTestGetHostByName(string siteName, string siteDot) {
  50. IPHostEntry h = System.Net.Dns.GetHostByName(siteName);
  51. SubTestValidIPHostEntry(h);
  52. Assert(h.HostName.Equals(siteName));
  53. Assert(h.AddressList[0].ToString() == siteDot);
  54. }
  55. public void TestGetHostByName() {
  56. SubTestGetHostByName(site1Name, site1Dot);
  57. SubTestGetHostByName(site2Name, site2Dot);
  58. try {
  59. System.Net.Dns.GetHostByName(noneExistingSite);
  60. Fail("Should raise a SocketException (assuming that '" + noneExistingSite + "' does not exist)");
  61. } catch (SocketException) {
  62. }
  63. try {
  64. System.Net.Dns.GetHostByName(null);
  65. Fail("Should raise an ArgumentNullException");
  66. } catch (ArgumentNullException) {
  67. }
  68. }
  69. private void SubTestGetHostByAddressStringFormatException(string addr) {
  70. try {
  71. System.Net.Dns.GetHostByAddress(addr);
  72. Fail("Should raise a FormatException");
  73. } catch (FormatException) {
  74. }
  75. }
  76. private void SubTestGetHostByAddressString(string addr) {
  77. IPHostEntry h = System.Net.Dns.GetHostByAddress(addr);
  78. SubTestValidIPHostEntry(h);
  79. }
  80. public void TestGetHostByAddressString() {
  81. try {
  82. String addr = null;
  83. System.Net.Dns.GetHostByAddress(addr);
  84. Fail("Should raise an ArgumentNullException");
  85. } catch (ArgumentNullException) {
  86. }
  87. SubTestGetHostByAddressStringFormatException("123.255.23");
  88. SubTestGetHostByAddressStringFormatException("123.256.34.10");
  89. SubTestGetHostByAddressStringFormatException("not an IP address");
  90. SubTestGetHostByAddressString(site1Dot);
  91. SubTestGetHostByAddressString(site2Dot);
  92. }
  93. private void SubTestGetHostByAddressIPAddress(IPAddress addr) {
  94. IPHostEntry h = System.Net.Dns.GetHostByAddress(addr);
  95. SubTestValidIPHostEntry(h);
  96. Assert(h.AddressList[0].ToString() == addr.ToString());
  97. }
  98. public void TestGetHostByAddressIPAddress() {
  99. try {
  100. IPAddress addr = null;
  101. System.Net.Dns.GetHostByAddress(addr);
  102. Fail("Should raise an ArgumentNullException");
  103. } catch (ArgumentNullException) {
  104. }
  105. SubTestGetHostByAddressIPAddress(new IPAddress(IPAddress.NetworkToHostOrder((int)site1IP)));
  106. SubTestGetHostByAddressIPAddress(new IPAddress(IPAddress.NetworkToHostOrder((int)site2IP)));
  107. }
  108. private void SubTestResolve(string addr) {
  109. IPHostEntry h = System.Net.Dns.Resolve(addr);
  110. SubTestValidIPHostEntry(h);
  111. }
  112. public void TestResolve() {
  113. SubTestResolve(site1Name);
  114. SubTestResolve(site2Name);
  115. SubTestResolve(site1Dot);
  116. SubTestResolve(site2Dot);
  117. }
  118. private void SubTestValidIPHostEntry(IPHostEntry h) {
  119. Assert(h.HostName != null);
  120. Assert(h.AddressList != null);
  121. Assert(h.AddressList.Length > 0);
  122. }
  123. private static void printIPHostEntry(IPHostEntry h)
  124. {
  125. Console.WriteLine("----------------------------------------------------");
  126. Console.WriteLine("Host name:");
  127. Console.WriteLine(h.HostName);
  128. Console.WriteLine("IP addresses:");
  129. IPAddress[] list = h.AddressList;
  130. for(int i = 0; i < list.Length; ++i)
  131. Console.WriteLine(list[i]);
  132. Console.WriteLine("Aliases:");
  133. string[] aliases = h.Aliases;
  134. for(int i = 0; i < aliases.Length; ++i)
  135. Console.WriteLine(aliases[i]);
  136. Console.WriteLine("----------------------------------------------------");
  137. }
  138. }