2
0

DirectConnectorTest_Connect_TimeoutConnectingToServer.cs 3.4 KB

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