Socks5ConnectorTest_Connect_TimeoutConnectionReply.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  30. _proxyServer = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, _connectionInfo.ProxyPort));
  31. _proxyServer.BytesReceived += (bytesReceived, socket) => {
  32. _bytesReceivedByProxy.AddRange(bytesReceived);
  33. if (_bytesReceivedByProxy.Count == 4) {
  34. _ = socket.Send(new byte[]
  35. {
  36. // SOCKS version
  37. 0x05,
  38. // Require no authentication
  39. 0x00
  40. });
  41. }
  42. };
  43. _proxyServer.Start();
  44. }
  45. protected override void SetupMocks()
  46. {
  47. _ = SocketFactoryMock.Setup(p => p.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
  48. .Returns(_clientSocket);
  49. }
  50. protected override void TearDown()
  51. {
  52. base.TearDown();
  53. _proxyServer?.Dispose();
  54. _clientSocket?.Dispose();
  55. }
  56. protected override void Act()
  57. {
  58. _stopWatch.Start();
  59. try
  60. {
  61. _ = Connector.Connect(_connectionInfo);
  62. Assert.Fail();
  63. }
  64. catch (SocketException ex) {
  65. _actualException = ex;
  66. }
  67. catch (SshOperationTimeoutException ex) {
  68. _actualException = ex;
  69. }
  70. finally
  71. {
  72. _stopWatch.Stop();
  73. }
  74. }
  75. [TestMethod]
  76. public void ConnectShouldHaveThrownSshOperationTimeoutException() {
  77. Assert.IsNull(_actualException.InnerException);
  78. Assert.IsInstanceOfType<SshOperationTimeoutException>(_actualException);
  79. }
  80. [TestMethod]
  81. public void ConnectShouldHaveRespectedTimeout()
  82. {
  83. var errorText = string.Format("Elapsed: {0}, Timeout: {1}",
  84. _stopWatch.ElapsedMilliseconds,
  85. _connectionInfo.Timeout.TotalMilliseconds);
  86. // Compare elapsed time with configured timeout, allowing for a margin of error
  87. Assert.IsTrue(_stopWatch.ElapsedMilliseconds >= _connectionInfo.Timeout.TotalMilliseconds - 10, errorText);
  88. Assert.IsTrue(_stopWatch.ElapsedMilliseconds < _connectionInfo.Timeout.TotalMilliseconds + 100, errorText);
  89. }
  90. [TestMethod]
  91. public void CreateOnSocketFactoryShouldHaveBeenInvokedOnce()
  92. {
  93. SocketFactoryMock.Verify(p => p.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp),
  94. Times.Once());
  95. }
  96. }
  97. }