Socks4ConnectorTest_Connect_TimeoutReadingReplyVersion.cs 4.3 KB

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