1 // DnsTest.cs - NUnit Test Cases for the System.Net.Dns class
\r
3 // Author: Mads Pultz (mpultz@diku.dk)
\r
5 // (C) Mads Pultz, 2001
\r
7 // This test assumes the following:
\r
8 // 1) The following Internet sites exist:
\r
9 // www.go-mono.com with IP address 129.250.184.233
\r
10 // info.diku.dk with IP address 130.225.96.4
\r
11 // 2) The following DNS name does not exist:
\r
12 // www.hopefullydoesnotexist.dk
\r
15 using NUnit.Framework;
\r
18 using System.Net.Sockets;
\r
19 using System.Threading;
\r
20 using System.Collections;
\r
22 namespace Ximian.Mono.Tests.System {
\r
24 public class DnsTest: TestCase {
\r
26 private String site1Name = "www.go-mono.com",
\r
27 site1Dot = "129.250.184.233",
\r
28 site2Name = "info.diku.dk",
\r
29 site2Dot = "130.225.96.4",
\r
30 noneExistingSite = "www.hopefullydoesnotexist.dk";
\r
31 private uint site1IP = 2180692201, site2IP = 2195808260; // Big-Endian
\r
33 public DnsTest(): base("Ximian.Mono.Tests.System.Net.DnsTest testsuite") { }
\r
34 public DnsTest(String name): base(name) { }
\r
36 public static ITest Suite {
\r
37 get { return new TestSuite(typeof(DnsTest)); }
\r
40 private void Callback(IAsyncResult ar) {
\r
42 h = Dns.EndGetHostByName(ar);
\r
43 SubTestValidIPHostEntry(h);
\r
46 public void TestAsyncGetHostByName(){
\r
48 r = Dns.BeginGetHostByName(site1Name, new AsyncCallback(Callback), null);
\r
51 public void TestAsyncResolve() {
\r
53 r = Dns.BeginResolve(site1Name, new AsyncCallback(Callback), null);
\r
56 public void TestGetHostName() {
\r
57 string hostName = Dns.GetHostName();
\r
58 Assert(hostName != null);
\r
61 private void SubTestGetHostByName(string siteName, string siteDot) {
\r
62 IPHostEntry h = Dns.GetHostByName(siteName);
\r
63 SubTestValidIPHostEntry(h);
\r
64 Assert(h.HostName.Equals(siteName));
\r
65 Assert(h.AddressList[0].ToString() == siteDot);
\r
68 public void TestGetHostByName() {
\r
69 SubTestGetHostByName(site1Name, site1Dot);
\r
70 SubTestGetHostByName(site2Name, site2Dot);
\r
72 Dns.GetHostByName(noneExistingSite);
\r
73 Fail("Should raise a SocketException (assuming that '" + noneExistingSite + "' does not exist)");
\r
74 } catch (SocketException) {
\r
77 Dns.GetHostByName(null);
\r
78 Fail("Should raise an ArgumentNullException");
\r
79 } catch (ArgumentNullException) {
\r
83 private void SubTestGetHostByAddressStringFormatException(string addr) {
\r
85 Dns.GetHostByAddress(addr);
\r
86 Fail("Should raise a FormatException");
\r
87 } catch (FormatException) {
\r
91 private void SubTestGetHostByAddressString(string addr) {
\r
92 IPHostEntry h = Dns.GetHostByAddress(addr);
\r
93 SubTestValidIPHostEntry(h);
\r
96 public void TestGetHostByAddressString() {
\r
99 Dns.GetHostByAddress(addr);
\r
100 Fail("Should raise an ArgumentNullException");
\r
101 } catch (ArgumentNullException) {
\r
103 SubTestGetHostByAddressStringFormatException("123.255.23");
\r
104 SubTestGetHostByAddressStringFormatException("123.256.34.10");
\r
105 SubTestGetHostByAddressStringFormatException("not an IP address");
\r
106 SubTestGetHostByAddressString(site1Dot);
\r
107 SubTestGetHostByAddressString(site2Dot);
\r
110 private void SubTestGetHostByAddressIPAddress(IPAddress addr) {
\r
111 IPHostEntry h = Dns.GetHostByAddress(addr);
\r
112 SubTestValidIPHostEntry(h);
\r
113 Assert(h.AddressList[0].ToString() == addr.ToString());
\r
116 public void TestGetHostByAddressIPAddress() {
\r
118 IPAddress addr = null;
\r
119 Dns.GetHostByAddress(addr);
\r
120 Fail("Should raise an ArgumentNullException");
\r
121 } catch (ArgumentNullException) {
\r
123 SubTestGetHostByAddressIPAddress(new IPAddress(IPAddress.NetworkToHostOrder((int)site1IP)));
\r
124 SubTestGetHostByAddressIPAddress(new IPAddress(IPAddress.NetworkToHostOrder((int)site2IP)));
\r
127 private void SubTestResolve(string addr) {
\r
128 IPHostEntry h = Dns.Resolve(addr);
\r
129 SubTestValidIPHostEntry(h);
\r
132 public void TestResolve() {
\r
133 SubTestResolve(site1Name);
\r
134 SubTestResolve(site2Name);
\r
135 SubTestResolve(site1Dot);
\r
136 SubTestResolve(site2Dot);
\r
139 private void SubTestValidIPHostEntry(IPHostEntry h) {
\r
140 Assert(h.HostName != null);
\r
141 Assert(h.AddressList != null);
\r
142 Assert(h.AddressList.Length > 0);
\r
145 private static void printIPHostEntry(IPHostEntry h)
\r
147 Console.WriteLine("----------------------------------------------------");
\r
148 Console.WriteLine("Host name:");
\r
149 Console.WriteLine(h.HostName);
\r
150 Console.WriteLine("IP addresses:");
\r
151 IPAddress[] list = h.AddressList;
\r
152 for(int i = 0; i < list.Length; ++i)
\r
153 Console.WriteLine(list[i]);
\r
154 Console.WriteLine("Aliases:");
\r
155 string[] aliases = h.Aliases;
\r
156 for(int i = 0; i < aliases.Length; ++i)
\r
157 Console.WriteLine(aliases[i]);
\r
158 Console.WriteLine("----------------------------------------------------");
\r