DnsTest.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 129.250.184.233
  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 {
  26. private String site1Name = "www.go-mono.com",
  27. site1Dot = "129.250.184.233",
  28. site2Name = "info.diku.dk",
  29. site2Dot = "130.225.96.4",
  30. noneExistingSite = "www.hopefullydoesnotexist.dk";
  31. private uint site1IP = 2180692201, site2IP = 2195808260; // Big-Endian
  32. private void Callback(IAsyncResult ar) {
  33. IPHostEntry h;
  34. h = Dns.EndGetHostByName(ar);
  35. SubTestValidIPHostEntry(h);
  36. }
  37. [Test]
  38. public void AsyncGetHostByName(){
  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. Assertion.AssertEquals ("#1", "www.go-mono.com", entry.HostName);
  45. }
  46. [Test]
  47. public void AsyncResolve() {
  48. IAsyncResult r;
  49. r = Dns.BeginResolve(site1Name, new AsyncCallback(Callback), null);
  50. IAsyncResult async = Dns.BeginResolve (site1Dot, null, null);
  51. IPHostEntry entry = Dns.EndResolve (async);
  52. SubTestValidIPHostEntry(entry);
  53. Assertion.AssertEquals ("#1", "129.250.184.233", entry.HostName);
  54. }
  55. [Test]
  56. public void GetHostName() {
  57. string hostName = Dns.GetHostName();
  58. Assertion.Assert(hostName != null);
  59. }
  60. private void SubTestGetHostByName(string siteName, string siteDot) {
  61. IPHostEntry h = Dns.GetHostByName(siteName);
  62. SubTestValidIPHostEntry(h);
  63. Assertion.Assert(h.HostName.Equals(siteName));
  64. Assertion.Assert(h.AddressList[0].ToString() == siteDot);
  65. }
  66. [Test]
  67. public void GetHostByName() {
  68. SubTestGetHostByName(site1Name, site1Dot);
  69. SubTestGetHostByName(site2Name, site2Dot);
  70. try {
  71. Dns.GetHostByName(noneExistingSite);
  72. Assertion.Fail("Should raise a SocketException (assuming that '" + noneExistingSite + "' does not exist)");
  73. } catch (SocketException) {
  74. }
  75. try {
  76. Dns.GetHostByName(null);
  77. Assertion.Fail("Should raise an ArgumentNullException");
  78. } catch (ArgumentNullException) {
  79. }
  80. }
  81. private void SubTestGetHostByAddressStringFormatException(string addr) {
  82. try {
  83. Dns.GetHostByAddress(addr);
  84. Assertion.Fail("Should raise a FormatException");
  85. } catch (FormatException) {
  86. }
  87. }
  88. private void SubTestGetHostByAddressString(string addr) {
  89. IPHostEntry h = Dns.GetHostByAddress(addr);
  90. SubTestValidIPHostEntry(h);
  91. }
  92. [Test]
  93. public void GetHostByAddressString() {
  94. try {
  95. String addr = null;
  96. Dns.GetHostByAddress(addr);
  97. Assertion.Fail("Should raise an ArgumentNullException");
  98. } catch (ArgumentNullException) {
  99. }
  100. SubTestGetHostByAddressStringFormatException("123.255.23");
  101. SubTestGetHostByAddressStringFormatException("123.256.34.10");
  102. SubTestGetHostByAddressStringFormatException("not an IP address");
  103. SubTestGetHostByAddressString(site1Dot);
  104. SubTestGetHostByAddressString(site2Dot);
  105. }
  106. private void SubTestGetHostByAddressIPAddress(IPAddress addr) {
  107. IPHostEntry h = Dns.GetHostByAddress(addr);
  108. SubTestValidIPHostEntry(h);
  109. Assertion.Assert(h.AddressList[0].ToString() == addr.ToString());
  110. }
  111. [Test]
  112. public void GetHostByAddressIPAddress() {
  113. try {
  114. IPAddress addr = null;
  115. Dns.GetHostByAddress(addr);
  116. Assertion.Fail("Should raise an ArgumentNullException");
  117. } catch (ArgumentNullException) {
  118. }
  119. SubTestGetHostByAddressIPAddress(new IPAddress(IPAddress.NetworkToHostOrder((int)site1IP)));
  120. SubTestGetHostByAddressIPAddress(new IPAddress(IPAddress.NetworkToHostOrder((int)site2IP)));
  121. }
  122. private void SubTestResolve(string addr) {
  123. IPHostEntry h = Dns.Resolve(addr);
  124. SubTestValidIPHostEntry(h);
  125. }
  126. [Test]
  127. public void Resolve() {
  128. SubTestResolve(site1Name);
  129. SubTestResolve(site2Name);
  130. SubTestResolve(site1Dot);
  131. SubTestResolve(site2Dot);
  132. }
  133. private void SubTestValidIPHostEntry(IPHostEntry h) {
  134. Assertion.Assert(h.HostName != null);
  135. Assertion.Assert(h.AddressList != null);
  136. Assertion.Assert(h.AddressList.Length > 0);
  137. }
  138. private static void printIPHostEntry(IPHostEntry h)
  139. {
  140. Console.WriteLine("----------------------------------------------------");
  141. Console.WriteLine("Host name:");
  142. Console.WriteLine(h.HostName);
  143. Console.WriteLine("IP addresses:");
  144. IPAddress[] list = h.AddressList;
  145. for(int i = 0; i < list.Length; ++i)
  146. Console.WriteLine(list[i]);
  147. Console.WriteLine("Aliases:");
  148. string[] aliases = h.Aliases;
  149. for(int i = 0; i < aliases.Length; ++i)
  150. Console.WriteLine(aliases[i]);
  151. Console.WriteLine("----------------------------------------------------");
  152. }
  153. }
  154. }