1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
|
// DnsTest.cs - NUnit Test Cases for the System.Net.Dns class
//
// Authors:
// Mads Pultz (mpultz@diku.dk)
// Martin Willemoes Hansen (mwh@sysrq.dk)
//
// (C) 2001 Mads Pultz
// (C) 2003 Martin Willemoes Hansen
//
// This test assumes the following:
// 1) The following Internet sites exist:
// www.go-mono.com with IP address 64.14.94.188
// info.diku.dk with IP address 130.225.96.4
// 2) The following DNS name does not exist:
// www.hopefullydoesnotexist.dk
//
using System;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using NUnit.Framework;
namespace MonoTests.System.Net
{
[TestFixture]
public class DnsTest
{
private String site1Name = "www.go-mono.com",
site1Dot = "130.57.21.18",
site2Name = "info.diku.dk",
site2Dot = "130.225.96.4",
noneExistingSite = "www.unlikely.novell.com";
private uint site1IP = 2180692201, site2IP = 2195808260; // Big-Endian
[Test]
public void AsyncGetHostByName ()
{
IAsyncResult r;
r = Dns.BeginGetHostByName (site1Name, new AsyncCallback (GetHostByNameCallback), null);
IAsyncResult async = Dns.BeginGetHostByName (site1Name, null, null);
IPHostEntry entry = Dns.EndGetHostByName (async);
SubTestValidIPHostEntry (entry);
Assert.AreEqual ("www.go-mono.com", entry.HostName);
}
void GetHostByNameCallback (IAsyncResult ar)
{
IPHostEntry h;
h = Dns.EndGetHostByName (ar);
SubTestValidIPHostEntry (h);
}
[Test]
public void AsyncResolve ()
{
IAsyncResult r;
r = Dns.BeginResolve (site1Name, new AsyncCallback (ResolveCallback), null);
IAsyncResult async = Dns.BeginResolve (site1Dot, null, null);
IPHostEntry entry = Dns.EndResolve (async);
SubTestValidIPHostEntry (entry);
Assert.AreEqual (site1Dot, entry.AddressList [0].ToString ());
}
void ResolveCallback (IAsyncResult ar)
{
IPHostEntry h = Dns.EndResolve (ar);
SubTestValidIPHostEntry (h);
}
#if NET_2_0
[Test]
public void BeginGetHostAddresses_HostNameOrAddress_Null ()
{
try {
Dns.BeginGetHostAddresses (
(string) null,
new AsyncCallback (GetHostAddressesCallback),
null);
Assert.Fail ("#1");
} catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.AreEqual ("hostName", ex.ParamName, "#5");
}
}
[Test]
public void BeginGetHostAddresses_HostNameOrAddress_UnspecifiedAddress ()
{
// IPv4
try {
Dns.BeginGetHostAddresses (
"0.0.0.0",
new AsyncCallback (GetHostAddressesCallback),
null);
Assert.Fail ("#A1");
} catch (ArgumentException ex) {
// IPv4 address 0.0.0.0 and IPv6 address ::0 are
// unspecified addresses that cannot be used as
// a target address
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
Assert.IsNull (ex.InnerException, "#A3");
Assert.IsNotNull (ex.Message, "#A4");
Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#A5");
}
// IPv6
try {
Dns.BeginGetHostAddresses (
"::0",
new AsyncCallback (GetHostAddressesCallback),
null);
Assert.Fail ("#B1");
} catch (ArgumentException ex) {
// IPv4 address 0.0.0.0 and IPv6 address ::0 are
// unspecified addresses that cannot be used as
// a target address
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
Assert.IsNull (ex.InnerException, "#B3");
Assert.IsNotNull (ex.Message, "#B4");
Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#B5");
}
}
void GetHostAddressesCallback (IAsyncResult ar)
{
IPAddress [] addresses = Dns.EndGetHostAddresses (ar);
Assert.IsNotNull (addresses);
}
[Test]
public void GetHostAddresses_HostNameOrAddress_Null ()
{
try {
Dns.GetHostAddresses ((string) null);
Assert.Fail ("#1");
} catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#5");
}
}
[Test]
public void GetHostAddresses_HostNameOrAddress_UnspecifiedAddress ()
{
// IPv4
try {
Dns.GetHostAddresses ("0.0.0.0");
Assert.Fail ("#A1");
} catch (ArgumentException ex) {
// IPv4 address 0.0.0.0 and IPv6 address ::0 are
// unspecified addresses that cannot be used as
// a target address
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
Assert.IsNull (ex.InnerException, "#A3");
Assert.IsNotNull (ex.Message, "#A4");
Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#A5");
}
// IPv6
try {
Dns.GetHostAddresses ("::0");
Assert.Fail ("#B1");
} catch (ArgumentException ex) {
// IPv4 address 0.0.0.0 and IPv6 address ::0 are
// unspecified addresses that cannot be used as
// a target address
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
Assert.IsNull (ex.InnerException, "#B3");
Assert.IsNotNull (ex.Message, "#B4");
Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#B5");
}
}
#endif
[Test]
public void GetHostName ()
{
string hostName = Dns.GetHostName ();
Assert.IsNotNull (hostName);
}
[Test]
public void GetHostByName ()
{
SubTestGetHostByName (site1Name, site1Dot);
SubTestGetHostByName (site2Name, site2Dot);
try {
Dns.GetHostByName (noneExistingSite);
Assert.Fail ("Should raise a SocketException (assuming that '" + noneExistingSite + "' does not exist)");
} catch (SocketException) {
}
}
void SubTestGetHostByName (string siteName, string siteDot)
{
IPHostEntry h = Dns.GetHostByName (siteName);
SubTestValidIPHostEntry (h);
Assert.AreEqual (siteName, h.HostName, "siteName");
Assert.AreEqual (siteDot, h.AddressList [0].ToString (), "siteDot");
}
[Test]
public void GetHostByName_HostName_Null ()
{
try {
Dns.GetHostByName ((string) null);
Assert.Fail ("#1");
} catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.AreEqual ("hostName", ex.ParamName, "#5");
}
}
[Test]
public void GetHostByAddressString_Address_Null ()
{
try {
Dns.GetHostByAddress ((string) null);
Assert.Fail ("#1");
} catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.AreEqual ("address", ex.ParamName, "#5");
}
}
[Test]
[ExpectedException (typeof (SocketException))]
#if TARGET_JVM
[Ignore ("Ignore failures in Sys.Net")]
#endif
public void GetHostByAddressString2 ()
{
Dns.GetHostByAddress ("123.255.23");
}
[Test]
[ExpectedException (typeof (FormatException))]
public void GetHostByAddressString3 ()
{
Dns.GetHostByAddress ("123.256.34.10");
}
[Test, ExpectedException (typeof (FormatException))]
public void GetHostByAddressString4 ()
{
Dns.GetHostByAddress ("not an IP address");
}
[Test]
/*** Current go-mono.com IP works fine here***
#if ONLY_1_1
[ExpectedException (typeof (SocketException))]
#endif
********/
public void GetHostByAddressString5 ()
{
Dns.GetHostByAddress (site1Dot);
}
[Test]
public void GetHostByAddressIPAddress_Address_Null ()
{
try {
Dns.GetHostByAddress ((IPAddress) null);
Assert.Fail ("#1");
} catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.AreEqual ("address", ex.ParamName, "#5");
}
}
[Test]
[Ignore ("Fails on both Mono and MS")]
public void GetHostByAddressIPAddress2 ()
{
IPAddress addr = new IPAddress (IPAddress.NetworkToHostOrder ((int) site1IP));
IPHostEntry h = Dns.GetHostByAddress (addr);
SubTestValidIPHostEntry (h);
Assert.AreEqual (addr.ToString (), h.AddressList [0].ToString ());
}
[Test]
public void GetHostByAddressIPAddress3 ()
{
IPAddress addr = new IPAddress (IPAddress.NetworkToHostOrder ((int) site2IP));
IPHostEntry h = Dns.GetHostByAddress (addr);
SubTestValidIPHostEntry (h);
Assert.AreEqual (addr.ToString (), h.AddressList [0].ToString ());
}
[Test]
public void BeginResolve_HostName_Null ()
{
try {
Dns.BeginResolve ((string) null,
new AsyncCallback (ResolveCallback),
null);
Assert.Fail ("#1");
} catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.AreEqual ("hostName", ex.ParamName, "#5");
}
}
[Test]
public void Resolve ()
{
SubTestResolve (site1Name);
SubTestResolve (site2Name);
SubTestResolve (site1Dot);
SubTestResolve (site2Dot);
}
void SubTestResolve (string addr)
{
IPHostEntry h = Dns.Resolve (addr);
SubTestValidIPHostEntry (h);
}
[Test]
public void Resolve_HostName_Null ()
{
try {
Dns.Resolve ((string) null);
Assert.Fail ("#1");
} catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.AreEqual ("hostName", ex.ParamName, "#5");
}
}
#if NET_2_0
[Test] // BeginGetHostEntry (IPAddress, AsyncCallback, Object)
public void BeginGetHostEntry1_Address_Null ()
{
try {
Dns.BeginGetHostEntry (
(IPAddress) null,
new AsyncCallback (GetHostAddressesCallback),
null);
Assert.Fail ("#1");
} catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.AreEqual ("address", ex.ParamName, "#5");
}
}
[Test] // BeginGetHostEntry (String, AsyncCallback, Object)
public void BeginGetHostEntry2_HostNameOrAddress_Null ()
{
try {
Dns.BeginGetHostEntry (
(string) null,
new AsyncCallback (GetHostAddressesCallback),
null);
Assert.Fail ("#1");
} catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.AreEqual ("hostName", ex.ParamName, "#5");
}
}
[Test] // BeginGetHostEntry (String, AsyncCallback, Object)
public void BeginGetHostEntry2_HostNameOrAddress_UnspecifiedAddress ()
{
// IPv4
try {
Dns.BeginGetHostEntry (
"0.0.0.0",
new AsyncCallback (GetHostEntryCallback),
null);
Assert.Fail ("#1");
} catch (ArgumentException ex) {
// IPv4 address 0.0.0.0 and IPv6 address ::0 are
// unspecified addresses that cannot be used as
// a target address
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#5");
}
// IPv6
try {
Dns.BeginGetHostEntry (
"::0",
new AsyncCallback (GetHostEntryCallback),
null);
Assert.Fail ("#1");
} catch (ArgumentException ex) {
// IPv4 address 0.0.0.0 and IPv6 address ::0 are
// unspecified addresses that cannot be used as
// a target address
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#5");
}
}
void GetHostEntryCallback (IAsyncResult ar)
{
IPHostEntry hostEntry = Dns.EndGetHostEntry (ar);
Assert.IsNotNull (hostEntry);
}
[Test] // GetHostEntry (IPAddress)
public void GetHostEntry1_Address_Null ()
{
try {
Dns.GetHostEntry ((IPAddress) null);
Assert.Fail ("#1");
} catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.AreEqual ("address", ex.ParamName, "#5");
}
}
[Test] // GetHostEntry (String)
public void GetHostEntry2 ()
{
Dns.GetHostEntry (site1Name); // hostname
Dns.GetHostEntry (site1Dot); // IP address
}
[Test] // GetHostEntry (String)
public void GetHostEntry2_HostNameOrAddress_Null ()
{
try {
Dns.GetHostEntry ((string) null);
Assert.Fail ("#1");
} catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#5");
}
}
[Test] // GetHostEntry (String)
public void GetHostEntry2_HostNameOrAddress_UnspecifiedAddress ()
{
// IPv4
try {
Dns.GetHostEntry ("0.0.0.0");
Assert.Fail ("#A1");
} catch (ArgumentException ex) {
// IPv4 address 0.0.0.0 and IPv6 address ::0 are
// unspecified addresses that cannot be used as
// a target address
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
Assert.IsNull (ex.InnerException, "#A3");
Assert.IsNotNull (ex.Message, "#A4");
Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#A5");
}
// IPv6
try {
Dns.GetHostEntry ("::0");
Assert.Fail ("#B1");
} catch (ArgumentException ex) {
// IPv4 address 0.0.0.0 and IPv6 address ::0 are
// unspecified addresses that cannot be used as
// a target address
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
Assert.IsNull (ex.InnerException, "#B3");
Assert.IsNotNull (ex.Message, "#B4");
Assert.AreEqual ("hostNameOrAddress", ex.ParamName, "#B5");
}
}
#endif
void SubTestValidIPHostEntry (IPHostEntry h)
{
Assert.IsNotNull (h.HostName, "HostName not null");
Assert.IsNotNull (h.AddressList, "AddressList not null");
Assert.IsTrue (h.AddressList.Length > 0, "AddressList.Length");
}
/* This isn't used anymore, but could be useful for debugging
static void printIPHostEntry(IPHostEntry h)
{
Console.WriteLine("----------------------------------------------------");
Console.WriteLine("Host name:");
Console.WriteLine(h.HostName);
Console.WriteLine("IP addresses:");
IPAddress[] list = h.AddressList;
for(int i = 0; i < list.Length; ++i)
Console.WriteLine(list[i]);
Console.WriteLine("Aliases:");
string[] aliases = h.Aliases;
for(int i = 0; i < aliases.Length; ++i)
Console.WriteLine(aliases[i]);
Console.WriteLine("----------------------------------------------------");
}
*/
}
}
|