ChannelTest_Dispose_SessionIsConnectedAndChannelIsOpen_EofReceived.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5. using Moq;
  6. using Renci.SshNet.Common;
  7. using Renci.SshNet.Messages.Connection;
  8. namespace Renci.SshNet.Tests.Classes.Channels
  9. {
  10. [TestClass]
  11. public class ChannelTest_Dispose_SessionIsConnectedAndChannelIsOpen_EofReceived : ChannelTestBase
  12. {
  13. private uint _localChannelNumber;
  14. private uint _localWindowSize;
  15. private uint _localPacketSize;
  16. private uint _remoteChannelNumber;
  17. private uint _remoteWindowSize;
  18. private uint _remotePacketSize;
  19. private TimeSpan _channelCloseTimeout;
  20. private ChannelStub _channel;
  21. private List<ChannelEventArgs> _channelClosedRegister;
  22. private List<ChannelEventArgs> _channelEndOfDataRegister;
  23. private IList<ExceptionEventArgs> _channelExceptionRegister;
  24. private ManualResetEvent _channelClosedReceived;
  25. private ManualResetEvent _channelClosedEventHandlerCompleted;
  26. private Thread _raiseChannelCloseReceivedThread;
  27. protected override void SetupData()
  28. {
  29. var random = new Random();
  30. _localChannelNumber = (uint)random.Next(0, int.MaxValue);
  31. _localWindowSize = (uint)random.Next(0, int.MaxValue);
  32. _localPacketSize = (uint)random.Next(0, int.MaxValue);
  33. _remoteChannelNumber = (uint)random.Next(0, int.MaxValue);
  34. _remoteWindowSize = (uint)random.Next(0, int.MaxValue);
  35. _remotePacketSize = (uint)random.Next(0, int.MaxValue);
  36. _channelCloseTimeout = TimeSpan.FromSeconds(random.Next(10, 20));
  37. _channelClosedRegister = new List<ChannelEventArgs>();
  38. _channelEndOfDataRegister = new List<ChannelEventArgs>();
  39. _channelExceptionRegister = new List<ExceptionEventArgs>();
  40. _channelClosedReceived = new ManualResetEvent(false);
  41. _channelClosedEventHandlerCompleted = new ManualResetEvent(false);
  42. _raiseChannelCloseReceivedThread = null;
  43. }
  44. protected override void SetupMocks()
  45. {
  46. var sequence = new MockSequence();
  47. SessionMock.InSequence(sequence).Setup(p => p.IsConnected).Returns(true);
  48. SessionMock.InSequence(sequence).Setup(p => p.TrySendMessage(It.Is<ChannelCloseMessage>(c => c.LocalChannelNumber == _remoteChannelNumber))).Returns(true);
  49. SessionMock.InSequence(sequence).Setup(p => p.ConnectionInfo).Returns(ConnectionInfoMock.Object);
  50. ConnectionInfoMock.InSequence(sequence).Setup(p => p.ChannelCloseTimeout).Returns(_channelCloseTimeout);
  51. SessionMock.InSequence(sequence)
  52. .Setup(p => p.TryWait(It.IsAny<EventWaitHandle>(), _channelCloseTimeout))
  53. .Callback<WaitHandle, TimeSpan>((waitHandle, channelCloseTimeout) =>
  54. {
  55. _raiseChannelCloseReceivedThread = new Thread(() =>
  56. {
  57. Thread.Sleep(100);
  58. // signal that the ChannelCloseMessage was received; we use this to verify whether we've actually
  59. // waited on the EventWaitHandle to be set; this needs to be set before we raise the ChannelCloseReceived
  60. // to make sure the waithandle is signaled when the Dispose method completes (or else the assert that
  61. // checks whether the handle has been signaled, will sometimes fail)
  62. _channelClosedReceived.Set();
  63. // raise ChannelCloseReceived event to set waithandle for receiving SSH_MSG_CHANNEL_CLOSE message
  64. // from server which is waited on after sending the SSH_MSG_CHANNEL_CLOSE message to the server
  65. //
  66. // this will cause a new invocation of Close() that will block until the Close() that was invoked
  67. // as part of Dispose() has released the lock; as such, this thread cannot be joined until that
  68. // lock is released
  69. //
  70. // we're mocking the wait on the ChannelCloseMessage, but we still want
  71. // to get the channel in the state that it would have after actually receiving
  72. // the ChannelCloseMessage
  73. SessionMock.Raise(s => s.ChannelCloseReceived += null, new MessageEventArgs<ChannelCloseMessage>(new ChannelCloseMessage(_localChannelNumber)));
  74. });
  75. _raiseChannelCloseReceivedThread.Start();
  76. waitHandle.WaitOne();
  77. })
  78. .Returns(WaitResult.Success);
  79. }
  80. [TestCleanup]
  81. public void TearDown()
  82. {
  83. if (_channelClosedReceived != null)
  84. {
  85. _channelClosedReceived.Dispose();
  86. _channelClosedReceived = null;
  87. }
  88. if (_raiseChannelCloseReceivedThread != null)
  89. {
  90. _raiseChannelCloseReceivedThread.Join();
  91. _raiseChannelCloseReceivedThread = null;
  92. }
  93. if (_channelClosedEventHandlerCompleted != null)
  94. {
  95. _channelClosedEventHandlerCompleted.Dispose();
  96. _channelClosedEventHandlerCompleted = null;
  97. }
  98. }
  99. protected override void Arrange()
  100. {
  101. base.Arrange();
  102. _channel = new ChannelStub(SessionMock.Object, _localChannelNumber, _localWindowSize, _localPacketSize);
  103. _channel.Closed += (sender, args) =>
  104. {
  105. _channelClosedRegister.Add(args);
  106. Thread.Sleep(50);
  107. _channelClosedEventHandlerCompleted.Set();
  108. };
  109. _channel.EndOfData += (sender, args) => _channelEndOfDataRegister.Add(args);
  110. _channel.Exception += (sender, args) => _channelExceptionRegister.Add(args);
  111. _channel.InitializeRemoteChannelInfo(_remoteChannelNumber, _remoteWindowSize, _remotePacketSize);
  112. _channel.SetIsOpen(true);
  113. SessionMock.Raise(
  114. s => s.ChannelEofReceived += null,
  115. new MessageEventArgs<ChannelEofMessage>(new ChannelEofMessage(_localChannelNumber)));
  116. }
  117. protected override void Act()
  118. {
  119. _channel.Dispose();
  120. }
  121. [TestMethod]
  122. public void IsOpenShouldReturnFalse()
  123. {
  124. Assert.IsFalse(_channel.IsOpen);
  125. }
  126. [TestMethod]
  127. public void TrySendMessageOnSessionShouldBeInvokedOnceForChannelCloseMessage()
  128. {
  129. SessionMock.Verify(
  130. p => p.TrySendMessage(It.Is<ChannelCloseMessage>(c => c.LocalChannelNumber == _remoteChannelNumber)),
  131. Times.Once);
  132. }
  133. [TestMethod]
  134. public void TrySendMessageOnSessionShouldNeverBeInvokedForChannelEofMessage()
  135. {
  136. SessionMock.Verify(
  137. p => p.TrySendMessage(It.Is<ChannelEofMessage>(c => c.LocalChannelNumber == _remoteChannelNumber)),
  138. Times.Never);
  139. }
  140. [TestMethod]
  141. public void TryWaitOnSessionShouldBeInvokedOnce()
  142. {
  143. SessionMock.Verify(p => p.TryWait(It.IsAny<EventWaitHandle>(), _channelCloseTimeout), Times.Once);
  144. }
  145. [TestMethod]
  146. public void WaitOnHandleOnSessionShouldWaitForChannelCloseMessageToBeReceived()
  147. {
  148. Assert.IsTrue(_channelClosedReceived.WaitOne(0));
  149. }
  150. [TestMethod]
  151. public void ClosedEventShouldHaveFiredOnce()
  152. {
  153. Assert.AreEqual(1, _channelClosedRegister.Count);
  154. Assert.AreEqual(_localChannelNumber, _channelClosedRegister[0].ChannelNumber);
  155. }
  156. [TestMethod]
  157. public void EndOfDataEventShouldNotHaveFired()
  158. {
  159. Assert.AreEqual(1, _channelEndOfDataRegister.Count);
  160. Assert.AreEqual(_localChannelNumber, _channelEndOfDataRegister[0].ChannelNumber);
  161. }
  162. [TestMethod]
  163. public void ExceptionShouldNeverHaveFired()
  164. {
  165. Assert.AreEqual(0, _channelExceptionRegister.Count);
  166. }
  167. [TestMethod]
  168. public void ThreadThatRaisedChannelCloseReceivedShouldComplete()
  169. {
  170. _raiseChannelCloseReceivedThread.Join();
  171. }
  172. }
  173. }