ForwardedPortDynamicTest_Dispose_PortStarted_ChannelBound.cs 8.6 KB

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