ForwardedPortLocalTest_Dispose_PortStarted_ChannelBound.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Threading;
  6. using Microsoft.Extensions.Logging.Abstractions;
  7. using Microsoft.VisualStudio.TestTools.UnitTesting;
  8. using Moq;
  9. using Renci.SshNet.Channels;
  10. using Renci.SshNet.Common;
  11. namespace Renci.SshNet.Tests.Classes
  12. {
  13. [TestClass]
  14. public class ForwardedPortLocalTest_Dispose_PortStarted_ChannelBound
  15. {
  16. private Mock<ISession> _sessionMock;
  17. private Mock<IConnectionInfo> _connectionInfoMock;
  18. private Mock<IChannelDirectTcpip> _channelMock;
  19. private ForwardedPortLocal _forwardedPort;
  20. private IList<EventArgs> _closingRegister;
  21. private IList<ExceptionEventArgs> _exceptionRegister;
  22. private IPEndPoint _localEndpoint;
  23. private IPEndPoint _remoteEndpoint;
  24. private Socket _client;
  25. private TimeSpan _bindSleepTime;
  26. private ManualResetEvent _channelBindStarted;
  27. private ManualResetEvent _channelBindCompleted;
  28. [TestInitialize]
  29. public void Setup()
  30. {
  31. Arrange();
  32. Act();
  33. }
  34. [TestCleanup]
  35. public void Cleanup()
  36. {
  37. if (_client != null)
  38. {
  39. _client.Dispose();
  40. _client = null;
  41. }
  42. if (_forwardedPort != null)
  43. {
  44. _forwardedPort.Dispose();
  45. _forwardedPort = null;
  46. }
  47. if (_channelBindStarted != null)
  48. {
  49. _channelBindStarted.Dispose();
  50. _channelBindStarted = null;
  51. }
  52. if (_channelBindCompleted != null)
  53. {
  54. _channelBindCompleted.Dispose();
  55. _channelBindCompleted = null;
  56. }
  57. }
  58. protected void Arrange()
  59. {
  60. var random = new Random();
  61. _closingRegister = new List<EventArgs>();
  62. _exceptionRegister = new List<ExceptionEventArgs>();
  63. _localEndpoint = new IPEndPoint(IPAddress.Loopback, 8122);
  64. _remoteEndpoint = new IPEndPoint(IPAddress.Parse("193.168.1.5"), random.Next(IPEndPoint.MinPort, IPEndPoint.MaxPort));
  65. _bindSleepTime = TimeSpan.FromMilliseconds(random.Next(100, 500));
  66. _forwardedPort = new ForwardedPortLocal(_localEndpoint.Address.ToString(), (uint)_localEndpoint.Port, _remoteEndpoint.Address.ToString(), (uint)_remoteEndpoint.Port);
  67. _channelBindStarted = new ManualResetEvent(false);
  68. _channelBindCompleted = new ManualResetEvent(false);
  69. _connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);
  70. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  71. _sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
  72. _channelMock = new Mock<IChannelDirectTcpip>(MockBehavior.Strict);
  73. _connectionInfoMock.Setup(p => p.Timeout).Returns(TimeSpan.FromSeconds(15));
  74. _sessionMock.Setup(p => p.IsConnected).Returns(true);
  75. _sessionMock.Setup(p => p.ConnectionInfo).Returns(_connectionInfoMock.Object);
  76. _sessionMock.Setup(p => p.CreateChannelDirectTcpip()).Returns(_channelMock.Object);
  77. _channelMock.Setup(p => p.Open(_forwardedPort.Host, _forwardedPort.Port, _forwardedPort, It.IsAny<Socket>()));
  78. _channelMock.Setup(p => p.Bind()).Callback(() =>
  79. {
  80. _channelBindStarted.Set();
  81. Thread.Sleep(_bindSleepTime);
  82. _channelBindCompleted.Set();
  83. });
  84. _channelMock.Setup(p => p.Dispose());
  85. _forwardedPort.Closing += (sender, args) => _closingRegister.Add(args);
  86. _forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
  87. _forwardedPort.Session = _sessionMock.Object;
  88. _forwardedPort.Start();
  89. _client = new Socket(_localEndpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
  90. {
  91. ReceiveTimeout = 100,
  92. SendTimeout = 500,
  93. SendBufferSize = 0
  94. };
  95. _client.Connect(_localEndpoint);
  96. // wait for SOCKS client to bind to channel
  97. Assert.IsTrue(_channelBindStarted.WaitOne(TimeSpan.FromMilliseconds(200)));
  98. }
  99. protected void Act()
  100. {
  101. _forwardedPort.Dispose();
  102. }
  103. [TestMethod]
  104. public void ShouldBlockUntilBindHasCompleted()
  105. {
  106. Assert.IsTrue(_channelBindCompleted.WaitOne(0));
  107. }
  108. [TestMethod]
  109. public void IsStartedShouldReturnFalse()
  110. {
  111. Assert.IsFalse(_forwardedPort.IsStarted);
  112. }
  113. [TestMethod]
  114. public void ForwardedPortShouldRefuseNewConnections()
  115. {
  116. using (var client = new Socket(_localEndpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
  117. {
  118. try
  119. {
  120. client.Connect(_localEndpoint);
  121. Assert.Fail();
  122. }
  123. catch (SocketException ex)
  124. {
  125. Assert.AreEqual(SocketError.ConnectionRefused, ex.SocketErrorCode);
  126. }
  127. }
  128. }
  129. [TestMethod]
  130. public void BoundClientShouldNotBeClosed()
  131. {
  132. // the forwarded port itself does not close the client connection; when the channel is closed properly
  133. // it's the channel that will take care of closing the client connection
  134. //
  135. // we'll check if the client connection is still alive by attempting to receive, which should time out
  136. // as the forwarded port (or its channel) are not sending anything
  137. var buffer = new byte[1];
  138. try
  139. {
  140. _client.Receive(buffer);
  141. Assert.Fail();
  142. }
  143. catch (SocketException ex)
  144. {
  145. Assert.AreEqual(SocketError.TimedOut, ex.SocketErrorCode);
  146. }
  147. }
  148. [TestMethod]
  149. public void ClosingShouldHaveFiredOnce()
  150. {
  151. Assert.AreEqual(1, _closingRegister.Count);
  152. }
  153. [TestMethod]
  154. public void ExceptionShouldNotHaveFired()
  155. {
  156. Assert.AreEqual(0, _exceptionRegister.Count);
  157. }
  158. [TestMethod]
  159. public void OpenOnChannelShouldBeInvokedOnce()
  160. {
  161. _channelMock.Verify(p => p.Open(_forwardedPort.Host, _forwardedPort.Port, _forwardedPort, It.IsAny<Socket>()), Times.Once);
  162. }
  163. [TestMethod]
  164. public void BindOnChannelShouldBeInvokedOnce()
  165. {
  166. _channelMock.Verify(p => p.Bind(), Times.Once);
  167. }
  168. [TestMethod]
  169. public void DisposeOnChannelShouldBeInvokedOnce()
  170. {
  171. _channelMock.Verify(p => p.Dispose(), Times.Once);
  172. }
  173. }
  174. }