Socks4ConnectorTest_Connect_TimeoutReadingReplyVersion.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 Socks4ConnectorTest_Connect_TimeoutReadingReplyVersion : Socks4ConnectorTestBase
  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 = CreateConnectionInfo("proxyUser", "proxyPwd");
  28. _connectionInfo.Timeout = TimeSpan.FromMilliseconds(random.Next(50, 200));
  29. _stopWatch = new Stopwatch();
  30. _actualException = null;
  31. _clientSocket = SocketFactory.Create(SocketType.Stream, ProtocolType.Tcp);
  32. _proxyServer = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, _connectionInfo.ProxyPort));
  33. _proxyServer.Disconnected += socket => _disconnected = true;
  34. _proxyServer.Start();
  35. _server = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, _connectionInfo.Port));
  36. _server.Start();
  37. }
  38. protected override void SetupMocks()
  39. {
  40. _ = SocketFactoryMock.Setup(p => p.Create(SocketType.Stream, ProtocolType.Tcp))
  41. .Returns(_clientSocket);
  42. }
  43. protected override void TearDown()
  44. {
  45. base.TearDown();
  46. _server?.Dispose();
  47. _proxyServer?.Dispose();
  48. }
  49. protected override void Act()
  50. {
  51. _stopWatch.Start();
  52. try
  53. {
  54. _ = Connector.Connect(_connectionInfo);
  55. Assert.Fail();
  56. }
  57. catch (SshOperationTimeoutException ex)
  58. {
  59. _actualException = ex;
  60. }
  61. finally
  62. {
  63. _stopWatch.Stop();
  64. }
  65. // Give some time to process all messages
  66. Thread.Sleep(200);
  67. }
  68. [TestMethod]
  69. public void ConnectShouldHaveThrownSshOperationTimeoutException()
  70. {
  71. Assert.IsNull(_actualException.InnerException);
  72. Assert.AreEqual(string.Format(CultureInfo.InvariantCulture, "Socket read operation has timed out after {0:F0} milliseconds.", _connectionInfo.Timeout.TotalMilliseconds), _actualException.Message);
  73. }
  74. [TestMethod]
  75. public void ConnectShouldHaveRespectedTimeout()
  76. {
  77. var errorText = string.Format("Elapsed: {0}, Timeout: {1}",
  78. _stopWatch.ElapsedMilliseconds,
  79. _connectionInfo.Timeout.TotalMilliseconds);
  80. // Compare elapsed time with configured timeout, allowing for a margin of error
  81. Assert.IsTrue(_stopWatch.ElapsedMilliseconds >= _connectionInfo.Timeout.TotalMilliseconds - 10, errorText);
  82. Assert.IsTrue(_stopWatch.ElapsedMilliseconds < _connectionInfo.Timeout.TotalMilliseconds + 100, errorText);
  83. }
  84. [TestMethod]
  85. public void ClientSocketShouldNotBeConnected()
  86. {
  87. Assert.IsTrue(_disconnected);
  88. Assert.IsFalse(_clientSocket.Connected);
  89. }
  90. [TestMethod]
  91. public void ClientSocketShouldHaveBeenDisposed()
  92. {
  93. try
  94. {
  95. _ = _clientSocket.Receive(new byte[0]);
  96. Assert.Fail();
  97. }
  98. catch (ObjectDisposedException)
  99. {
  100. }
  101. }
  102. [TestMethod]
  103. public void CreateOnSocketFactoryShouldHaveBeenInvokedOnce()
  104. {
  105. SocketFactoryMock.Verify(p => p.Create(SocketType.Stream, ProtocolType.Tcp),
  106. Times.Once());
  107. }
  108. }
  109. }