Socks4ConnectorTest_Connect_TimeoutConnectingToProxy.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. if (_clientSocket != null)
  36. {
  37. _clientSocket.Dispose();
  38. }
  39. }
  40. protected override void Act()
  41. {
  42. _stopWatch.Start();
  43. try
  44. {
  45. Connector.Connect(_connectionInfo);
  46. Assert.Fail();
  47. }
  48. catch (SshOperationTimeoutException ex)
  49. {
  50. _actualException = ex;
  51. }
  52. finally
  53. {
  54. _stopWatch.Stop();
  55. }
  56. }
  57. [TestMethod]
  58. public void ConnectShouldHaveThrownSshOperationTimeoutException()
  59. {
  60. Assert.IsNull(_actualException.InnerException);
  61. Assert.AreEqual(string.Format(CultureInfo.InvariantCulture, "Connection failed to establish within {0} milliseconds.", _connectionInfo.Timeout.TotalMilliseconds), _actualException.Message);
  62. }
  63. [TestMethod]
  64. public void ConnectShouldHaveRespectedTimeout()
  65. {
  66. var errorText = string.Format("Elapsed: {0}, Timeout: {1}",
  67. _stopWatch.ElapsedMilliseconds,
  68. _connectionInfo.Timeout.TotalMilliseconds);
  69. // Compare elapsed time with configured timeout, allowing for a margin of error
  70. Assert.IsTrue(_stopWatch.ElapsedMilliseconds >= _connectionInfo.Timeout.TotalMilliseconds - 10, errorText);
  71. Assert.IsTrue(_stopWatch.ElapsedMilliseconds < _connectionInfo.Timeout.TotalMilliseconds + 100, errorText);
  72. }
  73. [TestMethod]
  74. public void ClientSocketShouldHaveBeenDisposed()
  75. {
  76. try
  77. {
  78. _clientSocket.Receive(new byte[0]);
  79. Assert.Fail();
  80. }
  81. catch (ObjectDisposedException)
  82. {
  83. }
  84. }
  85. [TestMethod]
  86. public void CreateOnSocketFactoryShouldHaveBeenInvokedOnce()
  87. {
  88. SocketFactoryMock.Verify(p => p.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp),
  89. Times.Once());
  90. }
  91. }
  92. }