ForwardedPortLocalTest_Dispose_PortStarted_ChannelBound.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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_Dispose_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 _channelBindStarted;
  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 (_channelBindStarted != null)
  47. {
  48. _channelBindStarted.Dispose();
  49. _channelBindStarted = null;
  50. }
  51. if (_channelBindCompleted != null)
  52. {
  53. _channelBindCompleted.Dispose();
  54. _channelBindCompleted = null;
  55. }
  56. }
  57. protected void Arrange()
  58. {
  59. var random = new Random();
  60. _closingRegister = new List<EventArgs>();
  61. _exceptionRegister = new List<ExceptionEventArgs>();
  62. _localEndpoint = new IPEndPoint(IPAddress.Loopback, 8122);
  63. _remoteEndpoint = new IPEndPoint(IPAddress.Parse("193.168.1.5"), random.Next(IPEndPoint.MinPort, IPEndPoint.MaxPort));
  64. _bindSleepTime = TimeSpan.FromMilliseconds(random.Next(100, 500));
  65. _forwardedPort = new ForwardedPortLocal(_localEndpoint.Address.ToString(), (uint)_localEndpoint.Port, _remoteEndpoint.Address.ToString(), (uint)_remoteEndpoint.Port);
  66. _channelBindStarted = new ManualResetEvent(false);
  67. _channelBindCompleted = new ManualResetEvent(false);
  68. _connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);
  69. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  70. _channelMock = new Mock<IChannelDirectTcpip>(MockBehavior.Strict);
  71. _connectionInfoMock.Setup(p => p.Timeout).Returns(TimeSpan.FromSeconds(15));
  72. _sessionMock.Setup(p => p.IsConnected).Returns(true);
  73. _sessionMock.Setup(p => p.ConnectionInfo).Returns(_connectionInfoMock.Object);
  74. _sessionMock.Setup(p => p.CreateChannelDirectTcpip()).Returns(_channelMock.Object);
  75. _channelMock.Setup(p => p.Open(_forwardedPort.Host, _forwardedPort.Port, _forwardedPort, It.IsAny<Socket>()));
  76. _channelMock.Setup(p => p.Bind()).Callback(() =>
  77. {
  78. _channelBindStarted.Set();
  79. Thread.Sleep(_bindSleepTime);
  80. _channelBindCompleted.Set();
  81. });
  82. _channelMock.Setup(p => p.Dispose());
  83. _forwardedPort.Closing += (sender, args) => _closingRegister.Add(args);
  84. _forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
  85. _forwardedPort.Session = _sessionMock.Object;
  86. _forwardedPort.Start();
  87. _client = new Socket(_localEndpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
  88. {
  89. ReceiveTimeout = 100,
  90. SendTimeout = 500,
  91. SendBufferSize = 0
  92. };
  93. _client.Connect(_localEndpoint);
  94. // wait for SOCKS client to bind to channel
  95. Assert.IsTrue(_channelBindStarted.WaitOne(TimeSpan.FromMilliseconds(200)));
  96. }
  97. protected void Act()
  98. {
  99. _forwardedPort.Dispose();
  100. }
  101. [TestMethod]
  102. public void ShouldBlockUntilBindHasCompleted()
  103. {
  104. Assert.IsTrue(_channelBindCompleted.WaitOne(0));
  105. }
  106. [TestMethod]
  107. public void IsStartedShouldReturnFalse()
  108. {
  109. Assert.IsFalse(_forwardedPort.IsStarted);
  110. }
  111. [TestMethod]
  112. public void ForwardedPortShouldRefuseNewConnections()
  113. {
  114. using (var client = new Socket(_localEndpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
  115. {
  116. try
  117. {
  118. client.Connect(_localEndpoint);
  119. Assert.Fail();
  120. }
  121. catch (SocketException ex)
  122. {
  123. Assert.AreEqual(SocketError.ConnectionRefused, ex.SocketErrorCode);
  124. }
  125. }
  126. }
  127. [TestMethod]
  128. public void BoundClientShouldNotBeClosed()
  129. {
  130. // the forwarded port itself does not close the client connection; when the channel is closed properly
  131. // it's the channel that will take care of closing the client connection
  132. //
  133. // we'll check if the client connection is still alive by attempting to receive, which should time out
  134. // as the forwarded port (or its channel) are not sending anything
  135. var buffer = new byte[1];
  136. try
  137. {
  138. _client.Receive(buffer);
  139. Assert.Fail();
  140. }
  141. catch (SocketException ex)
  142. {
  143. Assert.AreEqual(SocketError.TimedOut, ex.SocketErrorCode);
  144. }
  145. }
  146. [TestMethod]
  147. public void ClosingShouldHaveFiredOnce()
  148. {
  149. Assert.AreEqual(1, _closingRegister.Count);
  150. }
  151. [TestMethod]
  152. public void ExceptionShouldNotHaveFired()
  153. {
  154. Assert.AreEqual(0, _exceptionRegister.Count);
  155. }
  156. [TestMethod]
  157. public void OpenOnChannelShouldBeInvokedOnce()
  158. {
  159. _channelMock.Verify(p => p.Open(_forwardedPort.Host, _forwardedPort.Port, _forwardedPort, It.IsAny<Socket>()), Times.Once);
  160. }
  161. [TestMethod]
  162. public void BindOnChannelShouldBeInvokedOnce()
  163. {
  164. _channelMock.Verify(p => p.Bind(), Times.Once);
  165. }
  166. [TestMethod]
  167. public void DisposeOnChannelShouldBeInvokedOnce()
  168. {
  169. _channelMock.Verify(p => p.Dispose(), Times.Once);
  170. }
  171. }
  172. }