DnsAbstraction_GetHostAddresses.cs 2.9 KB

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