Socks5ConnectorTest_Connect_TimeoutConnectionReply.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using Microsoft.VisualStudio.TestTools.UnitTesting;
  7. using Moq;
  8. using Renci.SshNet.Common;
  9. using Renci.SshNet.Tests.Common;
  10. namespace Renci.SshNet.Tests.Classes.Connection
  11. {
  12. [TestClass]
  13. public class Socks5ConnectorTest_Connect_TimeoutConnectionReply : Socks5ConnectorTestBase
  14. {
  15. private ConnectionInfo _connectionInfo;
  16. private Exception _actualException;
  17. private AsyncSocketListener _proxyServer;
  18. private Socket _clientSocket;
  19. private List<byte> _bytesReceivedByProxy;
  20. private Stopwatch _stopWatch;
  21. protected override void SetupData()
  22. {
  23. base.SetupData();
  24. var random = new Random();
  25. _connectionInfo = CreateConnectionInfo("proxyUser", "proxyPwd");
  26. _connectionInfo.Timeout = TimeSpan.FromMilliseconds(random.Next(50, 200));
  27. _stopWatch = new Stopwatch();
  28. _bytesReceivedByProxy = new List<byte>();
  29. _clientSocket = SocketFactory.Create(SocketType.Stream, ProtocolType.Tcp);
  30. _proxyServer = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, _connectionInfo.ProxyPort));
  31. _proxyServer.BytesReceived += (bytesReceived, socket) =>
  32. {
  33. _bytesReceivedByProxy.AddRange(bytesReceived);
  34. if (_bytesReceivedByProxy.Count == 4)
  35. {
  36. _ = socket.Send(new byte[]
  37. {
  38. // SOCKS version
  39. 0x05,
  40. // Require no authentication
  41. 0x00
  42. });
  43. }
  44. };
  45. _proxyServer.Start();
  46. }
  47. protected override void SetupMocks()
  48. {
  49. _ = SocketFactoryMock.Setup(p => p.Create(SocketType.Stream, ProtocolType.Tcp))
  50. .Returns(_clientSocket);
  51. }
  52. protected override void TearDown()
  53. {
  54. base.TearDown();
  55. _proxyServer?.Dispose();
  56. _clientSocket?.Dispose();
  57. }
  58. protected override void Act()
  59. {
  60. _stopWatch.Start();
  61. try
  62. {
  63. _ = Connector.Connect(_connectionInfo);
  64. Assert.Fail();
  65. }
  66. catch (SocketException ex)
  67. {
  68. _actualException = ex;
  69. }
  70. catch (SshOperationTimeoutException ex)
  71. {
  72. _actualException = ex;
  73. }
  74. finally
  75. {
  76. _stopWatch.Stop();
  77. }
  78. }
  79. [TestMethod]
  80. public void ConnectShouldHaveThrownSshOperationTimeoutException()
  81. {
  82. Assert.IsNull(_actualException.InnerException);
  83. Assert.IsInstanceOfType<SshOperationTimeoutException>(_actualException);
  84. }
  85. [TestMethod]
  86. public void ConnectShouldHaveRespectedTimeout()
  87. {
  88. var errorText = string.Format("Elapsed: {0}, Timeout: {1}",
  89. _stopWatch.ElapsedMilliseconds,
  90. _connectionInfo.Timeout.TotalMilliseconds);
  91. // Compare elapsed time with configured timeout, allowing for a margin of error
  92. Assert.IsTrue(_stopWatch.ElapsedMilliseconds >= _connectionInfo.Timeout.TotalMilliseconds - 10, errorText);
  93. Assert.IsTrue(_stopWatch.ElapsedMilliseconds < _connectionInfo.Timeout.TotalMilliseconds + 100, errorText);
  94. }
  95. [TestMethod]
  96. public void CreateOnSocketFactoryShouldHaveBeenInvokedOnce()
  97. {
  98. SocketFactoryMock.Verify(p => p.Create(SocketType.Stream, ProtocolType.Tcp),
  99. Times.Once());
  100. }
  101. }
  102. }