ChannelSessionTest_Dispose_SessionIsNotConnectedAndChannelIsOpen_NoChannelCloseOrChannelEofReceived.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.Channels;
  7. using Renci.SshNet.Common;
  8. using Renci.SshNet.Messages.Connection;
  9. namespace Renci.SshNet.Tests.Classes.Channels
  10. {
  11. [TestClass]
  12. public class ChannelSessionTest_Dispose_SessionIsNotConnectedAndChannelIsOpen_NoChannelCloseOrChannelEofReceived : ChannelSessionTestBase
  13. {
  14. private uint _localChannelNumber;
  15. private uint _localWindowSize;
  16. private uint _localPacketSize;
  17. private uint _remoteChannelNumber;
  18. private uint _remoteWindowSize;
  19. private uint _remotePacketSize;
  20. private IList<ChannelEventArgs> _channelClosedRegister;
  21. private List<ExceptionEventArgs> _channelExceptionRegister;
  22. private ChannelSession _channel;
  23. private MockSequence _sequence;
  24. private SemaphoreSlim _sessionSemaphore;
  25. private int _initialSessionSemaphoreCount;
  26. protected override void SetupData()
  27. {
  28. var random = new Random();
  29. _localChannelNumber = (uint)random.Next(0, int.MaxValue);
  30. _localWindowSize = (uint)random.Next(0, int.MaxValue);
  31. _localPacketSize = (uint)random.Next(0, int.MaxValue);
  32. _remoteChannelNumber = (uint)random.Next(0, int.MaxValue);
  33. _remoteWindowSize = (uint)random.Next(0, int.MaxValue);
  34. _remotePacketSize = (uint)random.Next(0, int.MaxValue);
  35. _channelClosedRegister = new List<ChannelEventArgs>();
  36. _channelExceptionRegister = new List<ExceptionEventArgs>();
  37. _initialSessionSemaphoreCount = random.Next(10, 20);
  38. _sessionSemaphore = new SemaphoreSlim(_initialSessionSemaphoreCount);
  39. }
  40. protected override void SetupMocks()
  41. {
  42. _sequence = new MockSequence();
  43. SessionMock.InSequence(_sequence).Setup(p => p.ConnectionInfo).Returns(ConnectionInfoMock.Object);
  44. ConnectionInfoMock.InSequence(_sequence).Setup(p => p.RetryAttempts).Returns(1);
  45. SessionMock.Setup(p => p.SessionSemaphore).Returns(_sessionSemaphore);
  46. SessionMock.InSequence(_sequence)
  47. .Setup(
  48. p =>
  49. p.SendMessage(
  50. It.Is<ChannelOpenMessage>(
  51. m =>
  52. m.LocalChannelNumber == _localChannelNumber &&
  53. m.InitialWindowSize == _localWindowSize && m.MaximumPacketSize == _localPacketSize &&
  54. m.Info is SessionChannelOpenInfo)));
  55. SessionMock.InSequence(_sequence)
  56. .Setup(p => p.WaitOnHandle(It.IsNotNull<WaitHandle>()))
  57. .Callback<WaitHandle>(
  58. w =>
  59. {
  60. SessionMock.Raise(
  61. s => s.ChannelOpenConfirmationReceived += null,
  62. new MessageEventArgs<ChannelOpenConfirmationMessage>(
  63. new ChannelOpenConfirmationMessage(
  64. _localChannelNumber,
  65. _remoteWindowSize,
  66. _remotePacketSize,
  67. _remoteChannelNumber)));
  68. w.WaitOne();
  69. });
  70. SessionMock.Setup(p => p.IsConnected).Returns(false);
  71. }
  72. protected override void Arrange()
  73. {
  74. base.Arrange();
  75. _channel = new ChannelSession(SessionMock.Object, _localChannelNumber, _localWindowSize, _localPacketSize);
  76. _channel.Closed += (sender, args) => _channelClosedRegister.Add(args);
  77. _channel.Exception += (sender, args) => _channelExceptionRegister.Add(args);
  78. _channel.Open();
  79. }
  80. protected override void Act()
  81. {
  82. _channel.Dispose();
  83. }
  84. [TestMethod]
  85. public void CurrentCountOfSessionSemaphoreShouldBeEqualToInitialCount()
  86. {
  87. Assert.AreEqual(_initialSessionSemaphoreCount, _sessionSemaphore.CurrentCount);
  88. }
  89. [TestMethod]
  90. public void ExceptionShouldNeverHaveFired()
  91. {
  92. Assert.AreEqual(0, _channelExceptionRegister.Count);
  93. }
  94. [TestMethod]
  95. public void ClosedEventShouldNotHaveFired()
  96. {
  97. Assert.AreEqual(0, _channelClosedRegister.Count);
  98. }
  99. [TestMethod]
  100. public void IsOpenShouldReturnFalse()
  101. {
  102. Assert.IsFalse(_channel.IsOpen);
  103. }
  104. }
  105. }