Socks4ConnectorTest_Connect_TimeoutReadingReplyCode.cs 4.4 KB

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