ForwardedPortLocalTest_Stop_PortStarted_ChannelBound.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. 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. _channelBound = 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. _channelBound.Set();
  79. Thread.Sleep(_bindSleepTime);
  80. _channelBindCompleted.Set();
  81. });
  82. _channelMock.Setup(p => p.Close());
  83. _channelMock.Setup(p => p.Dispose());
  84. _forwardedPort.Closing += (sender, args) => _closingRegister.Add(args);
  85. _forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
  86. _forwardedPort.Session = _sessionMock.Object;
  87. _forwardedPort.Start();
  88. _client = new Socket(_localEndpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
  89. {
  90. ReceiveTimeout = 500,
  91. SendTimeout = 500,
  92. SendBufferSize = 0
  93. };
  94. _client.Connect(_localEndpoint);
  95. // wait for SOCKS client to bind to channel
  96. Assert.IsTrue(_channelBound.WaitOne(TimeSpan.FromMilliseconds(200)));
  97. }
  98. protected void Act()
  99. {
  100. _forwardedPort.Stop();
  101. }
  102. [TestMethod]
  103. public void ShouldBlockUntilBindHasCompleted()
  104. {
  105. Assert.IsTrue(_channelBindCompleted.WaitOne(0));
  106. }
  107. [TestMethod]
  108. public void IsStartedShouldReturnFalse()
  109. {
  110. Assert.IsFalse(_forwardedPort.IsStarted);
  111. }
  112. [TestMethod]
  113. public void ForwardedPortShouldRefuseNewConnections()
  114. {
  115. using (var client = new Socket(_localEndpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
  116. {
  117. try
  118. {
  119. client.Connect(_localEndpoint);
  120. Assert.Fail();
  121. }
  122. catch (SocketException ex)
  123. {
  124. Assert.AreEqual(SocketError.ConnectionRefused, ex.SocketErrorCode);
  125. }
  126. }
  127. }
  128. [TestMethod]
  129. public void BoundClientShouldNotBeClosed()
  130. {
  131. // the forwarded port itself does not close the client connection when the channel is closed properly
  132. // it's the channel that will take care of closing the client connection
  133. _client.Send(new byte[] { 0x0a }, 0, 1, SocketFlags.None);
  134. }
  135. [TestMethod]
  136. public void ClosingShouldHaveFiredOnce()
  137. {
  138. Assert.AreEqual(1, _closingRegister.Count);
  139. }
  140. [TestMethod]
  141. public void ExceptionShouldNotHaveFired()
  142. {
  143. Assert.AreEqual(0, _exceptionRegister.Count);
  144. }
  145. [TestMethod]
  146. public void OpenOnChannelShouldBeInvokedOnce()
  147. {
  148. _channelMock.Verify(p => p.Open(_forwardedPort.Host, _forwardedPort.Port, _forwardedPort, It.IsAny<Socket>()), Times.Once);
  149. }
  150. [TestMethod]
  151. public void BindOnChannelShouldBeInvokedOnce()
  152. {
  153. _channelMock.Verify(p => p.Bind(), Times.Once);
  154. }
  155. [TestMethod]
  156. public void CloseOnChannelShouldBeInvokedOnce()
  157. {
  158. _channelMock.Verify(p => p.Close(), Times.Once);
  159. }
  160. [TestMethod]
  161. public void DisposeOnChannelShouldBeInvokedOnce()
  162. {
  163. _channelMock.Verify(p => p.Dispose(), Times.Once);
  164. }
  165. }
  166. }