DnsTest.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. using NUnit.Framework;
  7. using System;
  8. using System.Net;
  9. using System.Net.Sockets;
  10. using System.Collections;
  11. public class DnsTest: TestCase {
  12. private String site1Name = "www.go-mono.com",
  13. site1Dot = "129.250.184.233",
  14. site2Name = "info.diku.dk",
  15. site2Dot = "130.225.96.4",
  16. noneExistingSite = "www.hopefullydoesnotexist.dk";
  17. private uint site1IP = 2180692201, site2IP = 2195808260;
  18. public DnsTest(String name): base(name) {
  19. Console.WriteLine("Assuming the following sites exist for this test suite:");
  20. Console.WriteLine("'{0}' with IP address '{1}' ({2})", site1Name, site1Dot, site1IP);
  21. Console.WriteLine("'{0}' with IP address '{1}' ({2})", site2Name, site2Dot, site2IP);
  22. Console.WriteLine("Assuming that '{0}' does not exist", noneExistingSite);
  23. }
  24. public static ITest Suite {
  25. get { return new TestSuite(typeof(DnsTest)); }
  26. }
  27. public void TestGetHostName() {
  28. }
  29. private void SubTestGetHostByName(string siteName, string siteDot) {
  30. IPHostEntry h = System.Net.Dns.GetHostByName(siteName);
  31. Assert(h.HostName.Equals(siteName));
  32. Assert(h.AddressList.Length > 0);
  33. Assert(h.AddressList[0].ToString() == siteDot);
  34. printIPHostEntry(h);
  35. }
  36. public void TestGetHostByName() {
  37. SubTestGetHostByName(site1Name, site1Dot);
  38. SubTestGetHostByName(site2Name, site2Dot);
  39. try {
  40. System.Net.Dns.GetHostByName(noneExistingSite);
  41. Fail("Should raise a SocketException (assuming that '" + noneExistingSite + "' does not exist)");
  42. } catch (SocketException) {
  43. }
  44. try {
  45. System.Net.Dns.GetHostByName(null);
  46. Fail("Should raise an ArgumentNullException");
  47. } catch (ArgumentNullException) {
  48. }
  49. }
  50. private void SubTestGetHostByAddressStringFormatException(string addr) {
  51. try {
  52. System.Net.Dns.GetHostByAddress(addr);
  53. Fail("Should raise a FormatException");
  54. } catch (FormatException) {
  55. }
  56. }
  57. private void SubTestGetHostByAddressString(string addr) {
  58. IPHostEntry h = System.Net.Dns.GetHostByAddress(addr);
  59. Assert(h.HostName != null);
  60. Assert(h.AddressList.Length > 0);
  61. printIPHostEntry(h);
  62. }
  63. public void TestGetHostByAddressString() {
  64. try {
  65. String addr = null;
  66. System.Net.Dns.GetHostByAddress(addr);
  67. Fail("Should raise an ArgumentNullException");
  68. } catch (ArgumentNullException) {
  69. }
  70. SubTestGetHostByAddressStringFormatException("123.255.23");
  71. SubTestGetHostByAddressStringFormatException("123.256.34.10");
  72. SubTestGetHostByAddressStringFormatException("not an IP address");
  73. SubTestGetHostByAddressString(site1Dot);
  74. SubTestGetHostByAddressString(site2Dot);
  75. }
  76. private void SubTestGetHostByAddressIPAddress(IPAddress addr) {
  77. IPHostEntry h = System.Net.Dns.GetHostByAddress(addr);
  78. Assert(h.HostName != null);
  79. Assert(h.AddressList.Length > 0);
  80. Assert(h.AddressList[0] == addr);
  81. printIPHostEntry(h);
  82. }
  83. public void TestGetHostByAddressIPAddress() {
  84. try {
  85. IPAddress addr = null;
  86. System.Net.Dns.GetHostByAddress(addr);
  87. Fail("Should raise an ArgumentNullException");
  88. } catch (ArgumentNullException) {
  89. }
  90. SubTestGetHostByAddressIPAddress(new IPAddress(site1IP));
  91. SubTestGetHostByAddressIPAddress(new IPAddress(site2IP));
  92. }
  93. private void SubTestIpToString(int IpAddr) {
  94. String addr = System.Net.Dns.IpToString(IpAddr);
  95. Assert(addr != null);
  96. }
  97. public void TestIpToString() {
  98. SubTestIpToString((int)site1IP);
  99. SubTestIpToString((int)site2IP);
  100. }
  101. private void SubTestResolve(string addr) {
  102. IPHostEntry h = System.Net.Dns.Resolve(addr);
  103. Assert(h.HostName != null);
  104. Assert(h.AddressList.Length > 0);
  105. printIPHostEntry(h);
  106. }
  107. public void TestResolve() {
  108. SubTestResolve(site1Name);
  109. SubTestResolve(site2Name);
  110. SubTestResolve(site1Dot);
  111. SubTestResolve(site2Dot);
  112. }
  113. private static void printIPHostEntry(IPHostEntry h)
  114. {
  115. Console.WriteLine("----------------------------------------------------");
  116. Console.WriteLine("Host name:");
  117. Console.WriteLine(h.HostName);
  118. Console.WriteLine("IP addresses:");
  119. IPAddress[] list = h.AddressList;
  120. for(int i = 0; i < list.Length; ++i)
  121. Console.WriteLine(list[i]);
  122. Console.WriteLine("Aliases:");
  123. string[] aliases = h.Aliases;
  124. for(int i = 0; i < aliases.Length; ++i)
  125. Console.WriteLine(aliases[i]);
  126. Console.WriteLine("----------------------------------------------------");
  127. }
  128. }