DirectConnectorTest_Connect_TimeoutConnectingToServer.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 DirectConnectorTest_Connect_TimeoutConnectingToServer : DirectConnectorTestBase
  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 = CreateConnectionInfo(IPAddress.Loopback.ToString());
  25. _connectionInfo.Timeout = TimeSpan.FromMilliseconds(random.Next(50, 200));
  26. _stopWatch = new Stopwatch();
  27. _actualException = null;
  28. _clientSocket = SocketFactory.Create(SocketType.Stream, ProtocolType.Tcp);
  29. }
  30. protected override void SetupMocks()
  31. {
  32. _ = SocketFactoryMock.Setup(p => p.Create(SocketType.Stream, ProtocolType.Tcp))
  33. .Returns(_clientSocket);
  34. }
  35. protected override void TearDown()
  36. {
  37. base.TearDown();
  38. _clientSocket?.Dispose();
  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. catch (SocketException ex)
  53. {
  54. _actualException = ex;
  55. }
  56. finally
  57. {
  58. _stopWatch.Stop();
  59. }
  60. }
  61. [TestMethodForPlatform(nameof(OSPlatform.Windows))]
  62. public void ConnectShouldHaveThrownSshOperationTimeoutExceptionOnWindows()
  63. {
  64. Assert.IsNull(_actualException.InnerException);
  65. Assert.IsInstanceOfType<SshOperationTimeoutException>(_actualException);
  66. Assert.AreEqual(string.Format(CultureInfo.InvariantCulture, "Connection failed to establish within {0} milliseconds.", _connectionInfo.Timeout.TotalMilliseconds), _actualException.Message);
  67. }
  68. [TestMethodForPlatform(nameof(OSPlatform.Linux))]
  69. public void ConnectShouldHaveThrownSocketExceptionOnLinux()
  70. {
  71. Assert.IsNull(_actualException.InnerException);
  72. Assert.IsInstanceOfType<SocketException>(_actualException);
  73. Assert.AreEqual("Connection refused", _actualException.Message);
  74. }
  75. [TestMethodForPlatform(nameof(OSPlatform.Windows))]
  76. public void ConnectShouldHaveRespectedTimeoutOnWindows()
  77. {
  78. var errorText = string.Format("Elapsed: {0}, Timeout: {1}",
  79. _stopWatch.ElapsedMilliseconds,
  80. _connectionInfo.Timeout.TotalMilliseconds);
  81. // Compare elapsed time with configured timeout, allowing for a margin of error
  82. Assert.IsTrue(_stopWatch.ElapsedMilliseconds >= _connectionInfo.Timeout.TotalMilliseconds - 10, errorText);
  83. Assert.IsTrue(_stopWatch.ElapsedMilliseconds < _connectionInfo.Timeout.TotalMilliseconds + 100, errorText);
  84. }
  85. [TestMethod]
  86. public void ClientSocketShouldHaveBeenDisposed()
  87. {
  88. try
  89. {
  90. _ = _clientSocket.Receive(new byte[0]);
  91. Assert.Fail();
  92. }
  93. catch (ObjectDisposedException)
  94. {
  95. }
  96. }
  97. [TestMethod]
  98. public void CreateOnSocketFactoryShouldHaveBeenInvokedOnce()
  99. {
  100. SocketFactoryMock.Verify(p => p.Create(SocketType.Stream, ProtocolType.Tcp),
  101. Times.Once());
  102. }
  103. }
  104. }