2
0

DnsAbstraction_GetHostAddresses.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. #if SILVERLIGHT
  5. using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
  6. #else
  7. using Microsoft.VisualStudio.TestTools.UnitTesting;
  8. #endif
  9. using Renci.SshNet.Abstractions;
  10. namespace Renci.SshNet.Tests.Abstractions
  11. {
  12. [TestClass]
  13. public class DnsAbstraction_GetHostAddresses
  14. {
  15. [TestMethod]
  16. public void ShouldThrowArgumentNullExceptionWhenHostNameOrAddressIsNull()
  17. {
  18. const string hostNameOrAddress = null;
  19. try
  20. {
  21. DnsAbstraction.GetHostAddresses(hostNameOrAddress);
  22. Assert.Fail();
  23. }
  24. catch (ArgumentNullException)
  25. {
  26. }
  27. }
  28. [TestMethod]
  29. public void ShouldThrowSocketExceptionWhenHostIsNotFound()
  30. {
  31. const string hostNameOrAddress = "surelydoesnotexist.OrAmIWrong";
  32. try
  33. {
  34. var addresses = DnsAbstraction.GetHostAddresses(hostNameOrAddress);
  35. Assert.Fail(addresses.ToString());
  36. }
  37. catch (SocketException ex)
  38. {
  39. Assert.IsNull(ex.InnerException);
  40. Assert.AreEqual(SocketError.HostNotFound, ex.SocketErrorCode);
  41. }
  42. }
  43. [TestMethod]
  44. public void ShouldReturnHostAddressesOfLocalHostWhenHostNameOrAddressIsEmpty()
  45. {
  46. const string hostNameOrAddress = "";
  47. var addresses = DnsAbstraction.GetHostAddresses(hostNameOrAddress);
  48. Assert.IsNotNull(addresses);
  49. #if !SILVERLIGHT
  50. var hostEntry = Dns.GetHostEntry(Dns.GetHostName());
  51. Assert.IsNotNull(hostEntry);
  52. Assert.AreEqual(hostEntry.AddressList.Length, addresses.Length);
  53. for (var i = 0; i < hostEntry.AddressList.Length; i++)
  54. Assert.AreEqual(hostEntry.AddressList[i], addresses[i]);
  55. #endif
  56. }
  57. [TestMethod]
  58. public void ShouldReturnSingleIpv4AddressWhenHostNameOrAddressIsValidIpv4Address()
  59. {
  60. const string hostNameOrAddress = "1.2.3.4";
  61. var addresses = DnsAbstraction.GetHostAddresses(hostNameOrAddress);
  62. Assert.IsNotNull(addresses);
  63. Assert.AreEqual(1, addresses.Length);
  64. Assert.AreEqual(AddressFamily.InterNetwork, addresses[0].AddressFamily);
  65. Assert.AreEqual(IPAddress.Parse(hostNameOrAddress), addresses[0]);
  66. }
  67. [TestMethod]
  68. public void ShouldReturnSingleIpv6AddressWhenHostNameOrAddressIsValidIpv6Address()
  69. {
  70. const string hostNameOrAddress = "2001:0:9d38:90d7:384f:2133:ab3d:d152";
  71. var addresses = DnsAbstraction.GetHostAddresses(hostNameOrAddress);
  72. Assert.IsNotNull(addresses);
  73. Assert.AreEqual(1, addresses.Length);
  74. Assert.AreEqual(AddressFamily.InterNetworkV6, addresses[0].AddressFamily);
  75. Assert.AreEqual(IPAddress.Parse(hostNameOrAddress), addresses[0]);
  76. }
  77. }
  78. }