HttpConnectorTest_Connect_TimeoutReadingStatusLine.cs 4.7 KB

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