Socks4ConnectorTest_Connect_TimeoutReadingReplyVersion.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using Moq;
  3. using Renci.SshNet.Common;
  4. using Renci.SshNet.Connection;
  5. using Renci.SshNet.Tests.Common;
  6. using System;
  7. using System.Diagnostics;
  8. using System.Globalization;
  9. using System.Net;
  10. using System.Net.Sockets;
  11. namespace Renci.SshNet.Tests.Classes.Connection
  12. {
  13. [TestClass]
  14. public class Socks4ConnectorTest_Connect_TimeoutReadingReplyVersion : Socks4ConnectorTestBase
  15. {
  16. private ConnectionInfo _connectionInfo;
  17. private SshOperationTimeoutException _actualException;
  18. private Socket _clientSocket;
  19. private AsyncSocketListener _proxyServer;
  20. private Stopwatch _stopWatch;
  21. private AsyncSocketListener _server;
  22. private bool _disconnected;
  23. protected override void SetupData()
  24. {
  25. base.SetupData();
  26. var random = new Random();
  27. _connectionInfo = CreateConnectionInfo("proxyUser", "proxyPwd");
  28. _connectionInfo.Timeout = TimeSpan.FromMilliseconds(random.Next(50, 200));
  29. _stopWatch = new Stopwatch();
  30. _actualException = null;
  31. _clientSocket = SocketFactory.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  32. _proxyServer = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, _connectionInfo.ProxyPort));
  33. _proxyServer.Disconnected += socket => _disconnected = true;
  34. _proxyServer.Start();
  35. _server = new AsyncSocketListener(new IPEndPoint(IPAddress.Loopback, _connectionInfo.Port));
  36. _server.Start();
  37. }
  38. protected override void SetupMocks()
  39. {
  40. SocketFactoryMock.Setup(p => p.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
  41. .Returns(_clientSocket);
  42. }
  43. protected override void TearDown()
  44. {
  45. base.TearDown();
  46. if (_server != null)
  47. {
  48. _server.Dispose();
  49. }
  50. if (_proxyServer != null)
  51. {
  52. _proxyServer.Dispose();
  53. }
  54. }
  55. protected override void Act()
  56. {
  57. _stopWatch.Start();
  58. try
  59. {
  60. Connector.Connect(_connectionInfo);
  61. Assert.Fail();
  62. }
  63. catch (SshOperationTimeoutException ex)
  64. {
  65. _actualException = ex;
  66. }
  67. finally
  68. {
  69. _stopWatch.Stop();
  70. }
  71. }
  72. [TestMethod]
  73. public void ConnectShouldHaveThrownSshOperationTimeoutException()
  74. {
  75. Assert.IsNull(_actualException.InnerException);
  76. Assert.AreEqual(string.Format(CultureInfo.InvariantCulture, "Socket read operation has timed out after {0:F0} milliseconds.", _connectionInfo.Timeout.TotalMilliseconds), _actualException.Message);
  77. }
  78. [TestMethod]
  79. public void ConnectShouldHaveRespectedTimeout()
  80. {
  81. var errorText = string.Format("Elapsed: {0}, Timeout: {1}",
  82. _stopWatch.ElapsedMilliseconds,
  83. _connectionInfo.Timeout.TotalMilliseconds);
  84. Assert.IsTrue(_stopWatch.ElapsedMilliseconds >= _connectionInfo.Timeout.TotalMilliseconds, errorText);
  85. Assert.IsTrue(_stopWatch.ElapsedMilliseconds < (_connectionInfo.Timeout.TotalMilliseconds + 100), errorText);
  86. }
  87. [TestMethod]
  88. public void ClientSocketShouldNotBeConnected()
  89. {
  90. Assert.IsTrue(_disconnected);
  91. Assert.IsFalse(_clientSocket.Connected);
  92. }
  93. [TestMethod]
  94. public void ClientSocketShouldHaveBeenDisposed()
  95. {
  96. try
  97. {
  98. _clientSocket.Receive(new byte[0]);
  99. Assert.Fail();
  100. }
  101. catch (ObjectDisposedException)
  102. {
  103. }
  104. }
  105. [TestMethod]
  106. public void CreateOnSocketFactoryShouldHaveBeenInvokedOnce()
  107. {
  108. SocketFactoryMock.Verify(p => p.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp),
  109. Times.Once());
  110. }
  111. }
  112. }