ForwardedPortLocalTest_Stop_PortStarted_ChannelBound.cs 7.0 KB

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