ForwardedPortRemoteTest_Dispose_PortStarted_ChannelBound.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Net;
  5. using System.Threading;
  6. using Microsoft.Extensions.Logging.Abstractions;
  7. using Microsoft.VisualStudio.TestTools.UnitTesting;
  8. using Moq;
  9. using Renci.SshNet.Channels;
  10. using Renci.SshNet.Common;
  11. using Renci.SshNet.Messages.Connection;
  12. namespace Renci.SshNet.Tests.Classes
  13. {
  14. [TestClass]
  15. public class ForwardedPortRemoteTest_Dispose_PortStarted_ChannelBound
  16. {
  17. private Mock<ISession> _sessionMock;
  18. private Mock<IConnectionInfo> _connectionInfoMock;
  19. private Mock<IChannelForwardedTcpip> _channelMock;
  20. private IList<EventArgs> _closingRegister;
  21. private IList<ExceptionEventArgs> _exceptionRegister;
  22. private IPEndPoint _bindEndpoint;
  23. private IPEndPoint _remoteEndpoint;
  24. private TimeSpan _bindSleepTime;
  25. private uint _remoteChannelNumberWhileClosing;
  26. private uint _remoteWindowSizeWhileClosing;
  27. private uint _remotePacketSizeWhileClosing;
  28. private uint _remoteChannelNumberStarted;
  29. private uint _remoteWindowSizeStarted;
  30. private uint _remotePacketSizeStarted;
  31. private string _originatorAddress;
  32. private uint _originatorPort;
  33. private ManualResetEvent _channelBindStarted;
  34. private ManualResetEvent _channelBindCompleted;
  35. protected ForwardedPortRemote ForwardedPort { get; private set; }
  36. [TestInitialize]
  37. public void Setup()
  38. {
  39. Arrange();
  40. Act();
  41. }
  42. [TestCleanup]
  43. public void Cleanup()
  44. {
  45. if (ForwardedPort != null)
  46. {
  47. ForwardedPort.Dispose();
  48. ForwardedPort = null;
  49. }
  50. if (_channelBindStarted != null)
  51. {
  52. _channelBindStarted.Dispose();
  53. _channelBindStarted = null;
  54. }
  55. if (_channelBindCompleted != null)
  56. {
  57. _channelBindCompleted.Dispose();
  58. _channelBindCompleted = null;
  59. }
  60. }
  61. private void CreateMocks()
  62. {
  63. _connectionInfoMock = new Mock<IConnectionInfo>(MockBehavior.Strict);
  64. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  65. _sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
  66. _channelMock = new Mock<IChannelForwardedTcpip>(MockBehavior.Strict);
  67. }
  68. private void SetUpData()
  69. {
  70. var random = new Random();
  71. _closingRegister = new List<EventArgs>();
  72. _exceptionRegister = new List<ExceptionEventArgs>();
  73. _bindEndpoint = new IPEndPoint(IPAddress.Any, random.Next(IPEndPoint.MinPort, IPEndPoint.MaxPort));
  74. _remoteEndpoint = new IPEndPoint(IPAddress.Parse("193.168.1.5"), random.Next(IPEndPoint.MinPort, IPEndPoint.MaxPort));
  75. _bindSleepTime = TimeSpan.FromMilliseconds(random.Next(100, 500));
  76. _remoteChannelNumberWhileClosing = (uint)random.Next(0, 1000);
  77. _remoteWindowSizeWhileClosing = (uint)random.Next(0, int.MaxValue);
  78. _remotePacketSizeWhileClosing = (uint)random.Next(0, int.MaxValue);
  79. _remoteChannelNumberStarted = (uint)random.Next(0, 1000);
  80. _remoteWindowSizeStarted = (uint)random.Next(0, int.MaxValue);
  81. _remotePacketSizeStarted = (uint)random.Next(0, int.MaxValue);
  82. _originatorAddress = random.Next().ToString(CultureInfo.InvariantCulture);
  83. _originatorPort = (uint)random.Next(0, int.MaxValue);
  84. _channelBindStarted = new ManualResetEvent(false);
  85. _channelBindCompleted = new ManualResetEvent(false);
  86. ForwardedPort = new ForwardedPortRemote(_bindEndpoint.Address, (uint)_bindEndpoint.Port, _remoteEndpoint.Address, (uint)_remoteEndpoint.Port);
  87. ForwardedPort.Closing += (sender, args) =>
  88. {
  89. _closingRegister.Add(args);
  90. _sessionMock.Raise(p => p.ChannelOpenReceived += null, new MessageEventArgs<ChannelOpenMessage>(new ChannelOpenMessage(_remoteChannelNumberWhileClosing, _remoteWindowSizeWhileClosing, _remotePacketSizeWhileClosing, new ForwardedTcpipChannelInfo(ForwardedPort.BoundHost, ForwardedPort.BoundPort, _originatorAddress, _originatorPort))));
  91. };
  92. ForwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
  93. ForwardedPort.Session = _sessionMock.Object;
  94. }
  95. private void SetupMocks()
  96. {
  97. _connectionInfoMock.Setup(p => p.Timeout).Returns(TimeSpan.FromSeconds(15));
  98. _sessionMock.Setup(p => p.IsConnected).Returns(true);
  99. _sessionMock.Setup(p => p.ConnectionInfo).Returns(_connectionInfoMock.Object);
  100. _sessionMock.Setup(p => p.RegisterMessage("SSH_MSG_REQUEST_FAILURE"));
  101. _sessionMock.Setup(p => p.RegisterMessage("SSH_MSG_REQUEST_SUCCESS"));
  102. _sessionMock.Setup(p => p.RegisterMessage("SSH_MSG_CHANNEL_OPEN"));
  103. _sessionMock.Setup(
  104. p =>
  105. p.SendMessage(
  106. It.Is<TcpIpForwardGlobalRequestMessage>(
  107. g =>
  108. g.AddressToBind == ForwardedPort.BoundHost &&
  109. g.PortToBind == ForwardedPort.BoundPort)))
  110. .Callback(
  111. () =>
  112. _sessionMock.Raise(s => s.RequestSuccessReceived += null,
  113. new MessageEventArgs<RequestSuccessMessage>(new RequestSuccessMessage())));
  114. _sessionMock.Setup(p => p.WaitOnHandle(It.IsAny<WaitHandle>()));
  115. _sessionMock.Setup(p => p.SendMessage(It.Is<ChannelOpenFailureMessage>(c => c.LocalChannelNumber == _remoteChannelNumberWhileClosing && c.ReasonCode == ChannelOpenFailureMessage.AdministrativelyProhibited && c.Description == string.Empty && c.Language == "en")));
  116. _sessionMock.Setup(p => p.CreateChannelForwardedTcpip(_remoteChannelNumberStarted, _remoteWindowSizeStarted, _remotePacketSizeStarted)).Returns(_channelMock.Object);
  117. _channelMock.Setup(
  118. p =>
  119. p.Bind(
  120. It.Is<IPEndPoint>(
  121. ep => ep.Address.Equals(_remoteEndpoint.Address) && ep.Port == _remoteEndpoint.Port),
  122. ForwardedPort)).Callback(() =>
  123. {
  124. _channelBindStarted.Set();
  125. Thread.Sleep(_bindSleepTime);
  126. _channelBindCompleted.Set();
  127. });
  128. _channelMock.Setup(p => p.Dispose());
  129. _sessionMock.Setup(
  130. p =>
  131. p.SendMessage(
  132. It.Is<CancelTcpIpForwardGlobalRequestMessage>(
  133. g =>
  134. g.AddressToBind == ForwardedPort.BoundHost && g.PortToBind == ForwardedPort.BoundPort))).Callback(
  135. () =>
  136. {
  137. // raise event confirming that forwarded port was cancelled
  138. _sessionMock.Raise(p => p.RequestSuccessReceived += null, new MessageEventArgs<RequestSuccessMessage>(new RequestSuccessMessage()));
  139. });
  140. _sessionMock.Setup(p => p.MessageListenerCompleted).Returns(new ManualResetEvent(false));
  141. }
  142. protected void Arrange()
  143. {
  144. CreateMocks();
  145. SetUpData();
  146. SetupMocks();
  147. ForwardedPort.Start();
  148. _sessionMock.Raise(p => p.ChannelOpenReceived += null, new MessageEventArgs<ChannelOpenMessage>(new ChannelOpenMessage(_remoteChannelNumberStarted, _remoteWindowSizeStarted, _remotePacketSizeStarted, new ForwardedTcpipChannelInfo(ForwardedPort.BoundHost, ForwardedPort.BoundPort, _originatorAddress, _originatorPort))));
  149. // wait until channel is bound
  150. Assert.IsTrue(_channelBindStarted.WaitOne(TimeSpan.FromMilliseconds(200)));
  151. }
  152. protected virtual void Act()
  153. {
  154. ForwardedPort.Dispose();
  155. }
  156. [TestMethod]
  157. public void ShouldBlockUntilBindHasCompleted()
  158. {
  159. Assert.IsTrue(_channelBindCompleted.WaitOne(0));
  160. }
  161. [TestMethod]
  162. public void IsStartedShouldReturnFalse()
  163. {
  164. Assert.IsFalse(ForwardedPort.IsStarted);
  165. }
  166. [TestMethod]
  167. public void ForwardedPortShouldRejectChannelOpenMessagesThatAreReceivedWhileTheSuccessMessageForTheCancelOfTheForwardedPortIsNotReceived()
  168. {
  169. _sessionMock.Verify(p => p.SendMessage(new ChannelOpenFailureMessage(_remoteChannelNumberWhileClosing, string.Empty,
  170. ChannelOpenFailureMessage.AdministrativelyProhibited)), Times.Never);
  171. }
  172. [TestMethod]
  173. public void ForwardedPortShouldIgnoreChannelOpenMessagesWhenDisposed()
  174. {
  175. var channelNumberDisposed = (uint)new Random().Next(1001, int.MaxValue);
  176. var initialWindowSizeDisposed = (uint)new Random().Next(0, int.MaxValue);
  177. var maximumPacketSizeDisposed = (uint)new Random().Next(0, int.MaxValue);
  178. var originatorAddressDisposed = new Random().Next().ToString(CultureInfo.InvariantCulture);
  179. var originatorPortDisposed = (uint)new Random().Next(0, int.MaxValue);
  180. var channelMock = new Mock<IChannelForwardedTcpip>(MockBehavior.Strict);
  181. _sessionMock.Setup(
  182. p =>
  183. p.CreateChannelForwardedTcpip(channelNumberDisposed, initialWindowSizeDisposed, maximumPacketSizeDisposed)).Returns(channelMock.Object);
  184. _sessionMock.Setup(
  185. p =>
  186. p.SendMessage(new ChannelOpenFailureMessage(channelNumberDisposed, string.Empty,
  187. ChannelOpenFailureMessage.AdministrativelyProhibited)));
  188. _sessionMock.Raise(p => p.ChannelOpenReceived += null,
  189. new MessageEventArgs<ChannelOpenMessage>(new ChannelOpenMessage(channelNumberDisposed,
  190. initialWindowSizeDisposed, maximumPacketSizeDisposed,
  191. new ForwardedTcpipChannelInfo(ForwardedPort.BoundHost, ForwardedPort.BoundPort,
  192. originatorAddressDisposed, originatorPortDisposed))));
  193. _sessionMock.Verify(p => p.CreateChannelForwardedTcpip(channelNumberDisposed, initialWindowSizeDisposed, maximumPacketSizeDisposed), Times.Never);
  194. _sessionMock.Verify(p => p.SendMessage(It.Is<ChannelOpenFailureMessage>(c => c.LocalChannelNumber == channelNumberDisposed && c.ReasonCode == ChannelOpenFailureMessage.AdministrativelyProhibited && c.Description == string.Empty && c.Language == null)), Times.Never);
  195. }
  196. [TestMethod]
  197. public void ClosingShouldHaveFiredOnce()
  198. {
  199. Assert.AreEqual(1, _closingRegister.Count);
  200. }
  201. [TestMethod]
  202. public void ExceptionShouldNotHaveFired()
  203. {
  204. Assert.AreEqual(0, _exceptionRegister.Count);
  205. }
  206. [TestMethod]
  207. public void BindOnChannelShouldBeInvokedOnceForChannelOpenedWhileStarted()
  208. {
  209. _channelMock.Verify(
  210. c =>
  211. c.Bind(
  212. It.Is<IPEndPoint>(
  213. ep => ep.Address.Equals(_remoteEndpoint.Address) && ep.Port == _remoteEndpoint.Port),
  214. ForwardedPort), Times.Once);
  215. }
  216. [TestMethod]
  217. public void DisposeOnChannelShouldBeInvokedOnce()
  218. {
  219. _channelMock.Verify(p => p.Dispose(), Times.Once);
  220. }
  221. }
  222. }