Socks5ConnectorTest_Connect_TimeoutConnectingToProxy.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Diagnostics;
  3. using System.Globalization;
  4. using System.Net.Sockets;
  5. using System.Runtime.InteropServices;
  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_TimeoutConnectingToProxy : Socks5ConnectorTestBase
  14. {
  15. private ConnectionInfo _connectionInfo;
  16. private Exception _actualException;
  17. private Socket _clientSocket;
  18. private Stopwatch _stopWatch;
  19. protected override void SetupData()
  20. {
  21. base.SetupData();
  22. var random = new Random();
  23. _connectionInfo = CreateConnectionInfo("proxyUser", "proxyPwd");
  24. _connectionInfo.Timeout = TimeSpan.FromMilliseconds(random.Next(50, 200));
  25. _stopWatch = new Stopwatch();
  26. _actualException = null;
  27. _clientSocket = SocketFactory.Create(SocketType.Stream, ProtocolType.Tcp);
  28. }
  29. protected override void SetupMocks()
  30. {
  31. _ = SocketFactoryMock.Setup(p => p.Create(SocketType.Stream, ProtocolType.Tcp))
  32. .Returns(_clientSocket);
  33. }
  34. protected override void TearDown()
  35. {
  36. base.TearDown();
  37. _clientSocket?.Dispose();
  38. }
  39. protected override void Act()
  40. {
  41. _stopWatch.Start();
  42. try
  43. {
  44. _ = Connector.Connect(_connectionInfo);
  45. Assert.Fail();
  46. }
  47. catch (SshOperationTimeoutException ex)
  48. {
  49. _actualException = ex;
  50. }
  51. catch (SocketException ex)
  52. {
  53. _actualException = ex;
  54. }
  55. finally
  56. {
  57. _stopWatch.Stop();
  58. }
  59. }
  60. [TestMethodForPlatform(nameof(OSPlatform.Windows))]
  61. public void ConnectShouldHaveThrownSshOperationTimeoutExceptionOnWindows()
  62. {
  63. Assert.IsNull(_actualException.InnerException);
  64. Assert.IsInstanceOfType<SshOperationTimeoutException>(_actualException);
  65. Assert.AreEqual(string.Format(CultureInfo.InvariantCulture, "Connection failed to establish within {0} milliseconds.", _connectionInfo.Timeout.TotalMilliseconds), _actualException.Message);
  66. }
  67. [TestMethodForPlatform(nameof(OSPlatform.Linux))]
  68. public void ConnectShouldHaveThrownSshOperationTimeoutExceptionOnLinux()
  69. {
  70. Assert.IsNull(_actualException.InnerException);
  71. Assert.IsInstanceOfType<SocketException>(_actualException);
  72. Assert.AreEqual("Connection refused", _actualException.Message);
  73. }
  74. [TestMethodForPlatform(nameof(OSPlatform.Windows))]
  75. public void ConnectShouldHaveRespectedTimeoutOnWindows()
  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 ClientSocketShouldHaveBeenDisposed()
  86. {
  87. try
  88. {
  89. _ = _clientSocket.Receive(new byte[0]);
  90. Assert.Fail();
  91. }
  92. catch (ObjectDisposedException)
  93. {
  94. }
  95. }
  96. [TestMethod]
  97. public void CreateOnSocketFactoryShouldHaveBeenInvokedOnce()
  98. {
  99. SocketFactoryMock.Verify(p => p.Create(SocketType.Stream, ProtocolType.Tcp),
  100. Times.Once());
  101. }
  102. }
  103. }