HttpConnectorTest_Connect_TimeoutConnectingToProxy.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Diagnostics;
  3. using System.Globalization;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Runtime.InteropServices;
  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 HttpConnectorTest_Connect_TimeoutConnectingToProxy : HttpConnectorTestBase
  15. {
  16. private ConnectionInfo _connectionInfo;
  17. private Exception _actualException;
  18. private Socket _clientSocket;
  19. private Stopwatch _stopWatch;
  20. protected override void SetupData()
  21. {
  22. base.SetupData();
  23. var random = new Random();
  24. _connectionInfo = new ConnectionInfo(IPAddress.Loopback.ToString(),
  25. 777,
  26. "user",
  27. ProxyTypes.Http,
  28. IPAddress.Loopback.ToString(),
  29. 8122,
  30. "proxyUser",
  31. "proxyPwd",
  32. new KeyboardInteractiveAuthenticationMethod("user"))
  33. {
  34. Timeout = TimeSpan.FromMilliseconds(random.Next(50, 200))
  35. };
  36. _stopWatch = new Stopwatch();
  37. _actualException = null;
  38. _clientSocket = SocketFactory.Create(SocketType.Stream, ProtocolType.Tcp);
  39. }
  40. protected override void SetupMocks()
  41. {
  42. _ = SocketFactoryMock.Setup(p => p.Create(SocketType.Stream, ProtocolType.Tcp))
  43. .Returns(_clientSocket);
  44. }
  45. protected override void TearDown()
  46. {
  47. base.TearDown();
  48. _clientSocket?.Dispose();
  49. }
  50. protected override void Act()
  51. {
  52. _stopWatch.Start();
  53. try
  54. {
  55. _ = Connector.Connect(_connectionInfo);
  56. Assert.Fail();
  57. }
  58. catch (SshOperationTimeoutException ex)
  59. {
  60. _actualException = ex;
  61. }
  62. catch (SocketException ex)
  63. {
  64. _actualException = ex;
  65. }
  66. finally
  67. {
  68. _stopWatch.Stop();
  69. }
  70. }
  71. [TestMethodForPlatform(nameof(OSPlatform.Windows))]
  72. public void ConnectShouldHaveThrownSshOperationTimeoutExceptionOnWindows()
  73. {
  74. Assert.IsNull(_actualException.InnerException);
  75. Assert.IsInstanceOfType<SshOperationTimeoutException>(_actualException);
  76. Assert.AreEqual(string.Format(CultureInfo.InvariantCulture, "Connection failed to establish within {0} milliseconds.", _connectionInfo.Timeout.TotalMilliseconds), _actualException.Message);
  77. }
  78. [TestMethodForPlatform(nameof(OSPlatform.Linux))]
  79. public void ConnectShouldHaveThrownSshOperationTimeoutExceptionOnLinux()
  80. {
  81. Assert.IsNull(_actualException.InnerException);
  82. Assert.IsInstanceOfType<SocketException>(_actualException);
  83. Assert.AreEqual("Connection refused", _actualException.Message);
  84. }
  85. [TestMethodForPlatform(nameof(OSPlatform.Windows))]
  86. public void ConnectShouldHaveRespectedTimeoutOnWindows()
  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 ClientSocketShouldHaveBeenDisposed()
  97. {
  98. try
  99. {
  100. _ = _clientSocket.Receive(new byte[0]);
  101. Assert.Fail();
  102. }
  103. catch (ObjectDisposedException)
  104. {
  105. }
  106. }
  107. [TestMethod]
  108. public void CreateOnSocketFactoryShouldHaveBeenInvokedOnce()
  109. {
  110. SocketFactoryMock.Verify(p => p.Create(SocketType.Stream, ProtocolType.Tcp),
  111. Times.Once());
  112. }
  113. }
  114. }