Socks4ConnectorTest_Connect_TimeoutConnectingToProxy.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using Moq;
  3. using Renci.SshNet.Common;
  4. using System;
  5. using System.Diagnostics;
  6. using System.Globalization;
  7. using System.Net.Sockets;
  8. namespace Renci.SshNet.Tests.Classes.Connection
  9. {
  10. [TestClass]
  11. public class Socks4ConnectorTest_Connect_TimeoutConnectingToProxy : Socks4ConnectorTestBase
  12. {
  13. private ConnectionInfo _connectionInfo;
  14. private SshOperationTimeoutException _actualException;
  15. private Socket _clientSocket;
  16. private Stopwatch _stopWatch;
  17. protected override void SetupData()
  18. {
  19. base.SetupData();
  20. var random = new Random();
  21. _connectionInfo = CreateConnectionInfo("proxyUser", "proxyPwd");
  22. _connectionInfo.Timeout = TimeSpan.FromMilliseconds(random.Next(50, 200));
  23. _stopWatch = new Stopwatch();
  24. _clientSocket = SocketFactory.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  25. _actualException = null;
  26. }
  27. protected override void SetupMocks()
  28. {
  29. _ = SocketFactoryMock.Setup(p => p.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
  30. .Returns(_clientSocket);
  31. }
  32. protected override void TearDown()
  33. {
  34. base.TearDown();
  35. _clientSocket?.Dispose();
  36. }
  37. protected override void Act()
  38. {
  39. _stopWatch.Start();
  40. try
  41. {
  42. _ = Connector.Connect(_connectionInfo);
  43. Assert.Fail();
  44. }
  45. catch (SshOperationTimeoutException ex)
  46. {
  47. _actualException = ex;
  48. }
  49. finally
  50. {
  51. _stopWatch.Stop();
  52. }
  53. }
  54. [TestMethod]
  55. public void ConnectShouldHaveThrownSshOperationTimeoutException()
  56. {
  57. Assert.IsNull(_actualException.InnerException);
  58. Assert.AreEqual(string.Format(CultureInfo.InvariantCulture, "Connection failed to establish within {0} milliseconds.", _connectionInfo.Timeout.TotalMilliseconds), _actualException.Message);
  59. }
  60. [TestMethod]
  61. public void ConnectShouldHaveRespectedTimeout()
  62. {
  63. var errorText = string.Format("Elapsed: {0}, Timeout: {1}",
  64. _stopWatch.ElapsedMilliseconds,
  65. _connectionInfo.Timeout.TotalMilliseconds);
  66. // Compare elapsed time with configured timeout, allowing for a margin of error
  67. Assert.IsTrue(_stopWatch.ElapsedMilliseconds >= _connectionInfo.Timeout.TotalMilliseconds - 10, errorText);
  68. Assert.IsTrue(_stopWatch.ElapsedMilliseconds < _connectionInfo.Timeout.TotalMilliseconds + 100, errorText);
  69. }
  70. [TestMethod]
  71. public void ClientSocketShouldHaveBeenDisposed()
  72. {
  73. try
  74. {
  75. _ = _clientSocket.Receive(new byte[0]);
  76. Assert.Fail();
  77. }
  78. catch (ObjectDisposedException)
  79. {
  80. }
  81. }
  82. [TestMethod]
  83. public void CreateOnSocketFactoryShouldHaveBeenInvokedOnce()
  84. {
  85. SocketFactoryMock.Verify(p => p.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp),
  86. Times.Once());
  87. }
  88. }
  89. }