DnsTest.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 Ximian.Mono.Tests.System {
  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("Ximian.Mono.Tests.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. }
  42. public void TestAsyncResolve() {
  43. IAsyncResult r;
  44. r = Dns.BeginResolve(site1Name, new AsyncCallback(Callback), null);
  45. }
  46. public void TestGetHostName() {
  47. string hostName = Dns.GetHostName();
  48. Assert(hostName != null);
  49. }
  50. private void SubTestGetHostByName(string siteName, string siteDot) {
  51. IPHostEntry h = Dns.GetHostByName(siteName);
  52. SubTestValidIPHostEntry(h);
  53. Assert(h.HostName.Equals(siteName));
  54. Assert(h.AddressList[0].ToString() == siteDot);
  55. }
  56. public void TestGetHostByName() {
  57. SubTestGetHostByName(site1Name, site1Dot);
  58. SubTestGetHostByName(site2Name, site2Dot);
  59. try {
  60. Dns.GetHostByName(noneExistingSite);
  61. Fail("Should raise a SocketException (assuming that '" + noneExistingSite + "' does not exist)");
  62. } catch (SocketException) {
  63. }
  64. try {
  65. Dns.GetHostByName(null);
  66. Fail("Should raise an ArgumentNullException");
  67. } catch (ArgumentNullException) {
  68. }
  69. }
  70. private void SubTestGetHostByAddressStringFormatException(string addr) {
  71. try {
  72. Dns.GetHostByAddress(addr);
  73. Fail("Should raise a FormatException");
  74. } catch (FormatException) {
  75. }
  76. }
  77. private void SubTestGetHostByAddressString(string addr) {
  78. IPHostEntry h = Dns.GetHostByAddress(addr);
  79. SubTestValidIPHostEntry(h);
  80. }
  81. public void TestGetHostByAddressString() {
  82. try {
  83. String addr = null;
  84. Dns.GetHostByAddress(addr);
  85. Fail("Should raise an ArgumentNullException");
  86. } catch (ArgumentNullException) {
  87. }
  88. SubTestGetHostByAddressStringFormatException("123.255.23");
  89. SubTestGetHostByAddressStringFormatException("123.256.34.10");
  90. SubTestGetHostByAddressStringFormatException("not an IP address");
  91. SubTestGetHostByAddressString(site1Dot);
  92. SubTestGetHostByAddressString(site2Dot);
  93. }
  94. private void SubTestGetHostByAddressIPAddress(IPAddress addr) {
  95. IPHostEntry h = Dns.GetHostByAddress(addr);
  96. SubTestValidIPHostEntry(h);
  97. Assert(h.AddressList[0].ToString() == addr.ToString());
  98. }
  99. public void TestGetHostByAddressIPAddress() {
  100. try {
  101. IPAddress addr = null;
  102. Dns.GetHostByAddress(addr);
  103. Fail("Should raise an ArgumentNullException");
  104. } catch (ArgumentNullException) {
  105. }
  106. SubTestGetHostByAddressIPAddress(new IPAddress(IPAddress.NetworkToHostOrder((int)site1IP)));
  107. SubTestGetHostByAddressIPAddress(new IPAddress(IPAddress.NetworkToHostOrder((int)site2IP)));
  108. }
  109. private void SubTestResolve(string addr) {
  110. IPHostEntry h = Dns.Resolve(addr);
  111. SubTestValidIPHostEntry(h);
  112. }
  113. public void TestResolve() {
  114. SubTestResolve(site1Name);
  115. SubTestResolve(site2Name);
  116. SubTestResolve(site1Dot);
  117. SubTestResolve(site2Dot);
  118. }
  119. private void SubTestValidIPHostEntry(IPHostEntry h) {
  120. Assert(h.HostName != null);
  121. Assert(h.AddressList != null);
  122. Assert(h.AddressList.Length > 0);
  123. }
  124. private static void printIPHostEntry(IPHostEntry h)
  125. {
  126. Console.WriteLine("----------------------------------------------------");
  127. Console.WriteLine("Host name:");
  128. Console.WriteLine(h.HostName);
  129. Console.WriteLine("IP addresses:");
  130. IPAddress[] list = h.AddressList;
  131. for(int i = 0; i < list.Length; ++i)
  132. Console.WriteLine(list[i]);
  133. Console.WriteLine("Aliases:");
  134. string[] aliases = h.Aliases;
  135. for(int i = 0; i < aliases.Length; ++i)
  136. Console.WriteLine(aliases[i]);
  137. Console.WriteLine("----------------------------------------------------");
  138. }
  139. }
  140. }