2
0

HttpConnectorTest_Connect_ConnectionToProxyRefused.cs 3.5 KB

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