DnsAbstraction_GetHostAddresses.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 ex)
  25. {
  26. Assert.IsNull(ex.InnerException);
  27. Assert.AreEqual("hostNameOrAddress", ex.ParamName);
  28. }
  29. }
  30. [TestMethod]
  31. public void ShouldThrowSocketExceptionWhenHostIsNotFound()
  32. {
  33. const string hostNameOrAddress = "surelydoesnotexist.OrAmIWrong";
  34. try
  35. {
  36. var addresses = DnsAbstraction.GetHostAddresses(hostNameOrAddress);
  37. Assert.Fail(addresses.ToString());
  38. }
  39. catch (SocketException ex)
  40. {
  41. Assert.IsNull(ex.InnerException);
  42. Assert.AreEqual(SocketError.HostNotFound, ex.SocketErrorCode);
  43. }
  44. }
  45. [TestMethod]
  46. public void ShouldReturnHostAddressesOfLocalHostWhenHostNameOrAddressIsEmpty()
  47. {
  48. const string hostNameOrAddress = "";
  49. var addresses = DnsAbstraction.GetHostAddresses(hostNameOrAddress);
  50. Assert.IsNotNull(addresses);
  51. #if !SILVERLIGHT
  52. var hostEntry = Dns.GetHostEntry(Dns.GetHostName());
  53. Assert.IsNotNull(hostEntry);
  54. Assert.AreEqual(hostEntry.AddressList.Length, addresses.Length);
  55. for (var i = 0; i < hostEntry.AddressList.Length; i++)
  56. Assert.AreEqual(hostEntry.AddressList[i], addresses[i]);
  57. #endif
  58. }
  59. [TestMethod]
  60. public void ShouldReturnSingleIpv4AddressWhenHostNameOrAddressIsValidIpv4Address()
  61. {
  62. const string hostNameOrAddress = "1.2.3.4";
  63. var addresses = DnsAbstraction.GetHostAddresses(hostNameOrAddress);
  64. Assert.IsNotNull(addresses);
  65. Assert.AreEqual(1, addresses.Length);
  66. Assert.AreEqual(AddressFamily.InterNetwork, addresses[0].AddressFamily);
  67. Assert.AreEqual(IPAddress.Parse(hostNameOrAddress), addresses[0]);
  68. }
  69. [TestMethod]
  70. public void ShouldReturnSingleIpv6AddressWhenHostNameOrAddressIsValidIpv6Address()
  71. {
  72. const string hostNameOrAddress = "2001:0:9d38:90d7:384f:2133:ab3d:d152";
  73. var addresses = DnsAbstraction.GetHostAddresses(hostNameOrAddress);
  74. Assert.IsNotNull(addresses);
  75. Assert.AreEqual(1, addresses.Length);
  76. Assert.AreEqual(AddressFamily.InterNetworkV6, addresses[0].AddressFamily);
  77. Assert.AreEqual(IPAddress.Parse(hostNameOrAddress), addresses[0]);
  78. }
  79. }
  80. }