ForwardedPortDynamicTest_Stop_PortStarted_ChannelBound.cs 8.1 KB

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