HttpConnectorTest_Connect_TimeoutConnectingToProxy.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Diagnostics;
  3. using System.Globalization;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using Microsoft.VisualStudio.TestTools.UnitTesting;
  7. using Moq;
  8. using Renci.SshNet.Common;
  9. namespace Renci.SshNet.Tests.Classes.Connection
  10. {
  11. [TestClass]
  12. public class HttpConnectorTest_Connect_TimeoutConnectingToProxy : HttpConnectorTestBase
  13. {
  14. private ConnectionInfo _connectionInfo;
  15. private SshOperationTimeoutException _actualException;
  16. private Socket _clientSocket;
  17. private Stopwatch _stopWatch;
  18. protected override void SetupData()
  19. {
  20. base.SetupData();
  21. var random = new Random();
  22. _connectionInfo = new ConnectionInfo(IPAddress.Loopback.ToString(),
  23. 777,
  24. "user",
  25. ProxyTypes.Http,
  26. IPAddress.Loopback.ToString(),
  27. 8122,
  28. "proxyUser",
  29. "proxyPwd",
  30. new KeyboardInteractiveAuthenticationMethod("user"))
  31. {
  32. Timeout = TimeSpan.FromMilliseconds(random.Next(50, 200))
  33. };
  34. _stopWatch = new Stopwatch();
  35. _actualException = null;
  36. _clientSocket = SocketFactory.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  37. }
  38. protected override void SetupMocks()
  39. {
  40. _ = SocketFactoryMock.Setup(p => p.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
  41. .Returns(_clientSocket);
  42. }
  43. protected override void TearDown()
  44. {
  45. base.TearDown();
  46. _clientSocket?.Dispose();
  47. }
  48. protected override void Act()
  49. {
  50. _stopWatch.Start();
  51. try
  52. {
  53. _ = Connector.Connect(_connectionInfo);
  54. Assert.Fail();
  55. }
  56. catch (SshOperationTimeoutException ex)
  57. {
  58. _actualException = ex;
  59. }
  60. finally
  61. {
  62. _stopWatch.Stop();
  63. }
  64. }
  65. [TestMethod]
  66. public void ConnectShouldHaveThrownSshOperationTimeoutException()
  67. {
  68. Assert.IsNull(_actualException.InnerException);
  69. Assert.AreEqual(string.Format(CultureInfo.InvariantCulture, "Connection failed to establish within {0} milliseconds.", _connectionInfo.Timeout.TotalMilliseconds), _actualException.Message);
  70. }
  71. [TestMethod]
  72. public void ConnectShouldHaveRespectedTimeout()
  73. {
  74. var errorText = string.Format("Elapsed: {0}, Timeout: {1}",
  75. _stopWatch.ElapsedMilliseconds,
  76. _connectionInfo.Timeout.TotalMilliseconds);
  77. // Compare elapsed time with configured timeout, allowing for a margin of error
  78. Assert.IsTrue(_stopWatch.ElapsedMilliseconds >= _connectionInfo.Timeout.TotalMilliseconds - 10, errorText);
  79. Assert.IsTrue(_stopWatch.ElapsedMilliseconds < _connectionInfo.Timeout.TotalMilliseconds + 100, errorText);
  80. }
  81. [TestMethod]
  82. public void ClientSocketShouldHaveBeenDisposed()
  83. {
  84. try
  85. {
  86. _ = _clientSocket.Receive(new byte[0]);
  87. Assert.Fail();
  88. }
  89. catch (ObjectDisposedException)
  90. {
  91. }
  92. }
  93. [TestMethod]
  94. public void CreateOnSocketFactoryShouldHaveBeenInvokedOnce()
  95. {
  96. SocketFactoryMock.Verify(p => p.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp),
  97. Times.Once());
  98. }
  99. }
  100. }