ChannelSessionTest_Open_OnOpenFailureReceived_RetriesAvalable.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Threading;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using Moq;
  7. using Renci.SshNet.Channels;
  8. using Renci.SshNet.Common;
  9. using Renci.SshNet.Messages.Connection;
  10. namespace Renci.SshNet.Tests.Classes.Channels
  11. {
  12. [TestClass]
  13. public class ChannelSessionTest_Open_OnOpenFailureReceived_RetriesAvalable : ChannelSessionTestBase
  14. {
  15. private ChannelSession _channel;
  16. private uint _localChannelNumber;
  17. private uint _localWindowSize;
  18. private uint _localPacketSize;
  19. private uint _remoteChannelNumber;
  20. private uint _remoteWindowSize;
  21. private uint _remotePacketSize;
  22. private IList<ChannelEventArgs> _channelClosedRegister;
  23. private List<ExceptionEventArgs> _channelExceptionRegister;
  24. private SemaphoreSlim _sessionSemaphore;
  25. private int _initialSessionSemaphoreCount;
  26. private uint _failureReasonCode;
  27. private string _failureDescription;
  28. private string _failureLanguage;
  29. protected override void SetupData()
  30. {
  31. var random = new Random();
  32. _localChannelNumber = (uint) random.Next(0, int.MaxValue);
  33. _localWindowSize = (uint) random.Next(2000, 3000);
  34. _localPacketSize = (uint) random.Next(1000, 2000);
  35. _remoteChannelNumber = (uint) random.Next(0, int.MaxValue);
  36. _remoteWindowSize = (uint) random.Next(0, int.MaxValue);
  37. _remotePacketSize = (uint) random.Next(0, int.MaxValue);
  38. _initialSessionSemaphoreCount = random.Next(10, 20);
  39. _sessionSemaphore = new SemaphoreSlim(_initialSessionSemaphoreCount);
  40. _channelClosedRegister = new List<ChannelEventArgs>();
  41. _channelExceptionRegister = new List<ExceptionEventArgs>();
  42. _failureReasonCode = (uint) random.Next(0, int.MaxValue);
  43. _failureDescription = random.Next().ToString(CultureInfo.InvariantCulture);
  44. _failureLanguage = random.Next().ToString(CultureInfo.InvariantCulture);
  45. }
  46. protected override void SetupMocks()
  47. {
  48. var sequence = new MockSequence();
  49. SessionMock.InSequence(sequence).Setup(p => p.ConnectionInfo).Returns(ConnectionInfoMock.Object);
  50. ConnectionInfoMock.InSequence(sequence).Setup(p => p.RetryAttempts).Returns(2);
  51. SessionMock.Setup(p => p.SessionSemaphore).Returns(_sessionSemaphore);
  52. SessionMock.InSequence(sequence)
  53. .Setup(
  54. p =>
  55. p.SendMessage(
  56. It.Is<ChannelOpenMessage>(
  57. m =>
  58. m.LocalChannelNumber == _localChannelNumber &&
  59. m.InitialWindowSize == _localWindowSize && m.MaximumPacketSize == _localPacketSize &&
  60. m.Info is SessionChannelOpenInfo)));
  61. SessionMock.InSequence(sequence)
  62. .Setup(p => p.WaitOnHandle(It.IsNotNull<WaitHandle>()))
  63. .Callback<WaitHandle>(
  64. w =>
  65. {
  66. SessionMock.Raise(
  67. s => s.ChannelOpenFailureReceived += null,
  68. new MessageEventArgs<ChannelOpenFailureMessage>(
  69. new ChannelOpenFailureMessage(
  70. _localChannelNumber,
  71. _failureDescription,
  72. _failureReasonCode,
  73. _failureLanguage
  74. )));
  75. w.WaitOne();
  76. });
  77. SessionMock.InSequence(sequence).Setup(p => p.ConnectionInfo).Returns(ConnectionInfoMock.Object);
  78. ConnectionInfoMock.InSequence(sequence).Setup(p => p.RetryAttempts).Returns(2);
  79. SessionMock.Setup(p => p.SessionSemaphore).Returns(_sessionSemaphore);
  80. SessionMock.InSequence(sequence)
  81. .Setup(
  82. p =>
  83. p.SendMessage(
  84. It.Is<ChannelOpenMessage>(
  85. m =>
  86. m.LocalChannelNumber == _localChannelNumber &&
  87. m.InitialWindowSize == _localWindowSize && m.MaximumPacketSize == _localPacketSize &&
  88. m.Info is SessionChannelOpenInfo)));
  89. SessionMock.InSequence(sequence)
  90. .Setup(p => p.WaitOnHandle(It.IsNotNull<WaitHandle>()))
  91. .Callback<WaitHandle>(
  92. w =>
  93. {
  94. SessionMock.Raise(
  95. s => s.ChannelOpenConfirmationReceived += null,
  96. new MessageEventArgs<ChannelOpenConfirmationMessage>(
  97. new ChannelOpenConfirmationMessage(
  98. _localChannelNumber,
  99. _remoteWindowSize,
  100. _remotePacketSize,
  101. _remoteChannelNumber)));
  102. w.WaitOne();
  103. });
  104. }
  105. protected override void Arrange()
  106. {
  107. base.Arrange();
  108. _channel = new ChannelSession(SessionMock.Object, _localChannelNumber, _localWindowSize, _localPacketSize);
  109. _channel.Closed += (sender, args) => _channelClosedRegister.Add(args);
  110. _channel.Exception += (sender, args) => _channelExceptionRegister.Add(args);
  111. }
  112. protected override void Act()
  113. {
  114. _channel.Open();
  115. }
  116. [TestMethod]
  117. public void CurrentCountOfSessionSemaphoreShouldBeOneLessThanToInitialCount()
  118. {
  119. Assert.AreEqual(_initialSessionSemaphoreCount - 1, _sessionSemaphore.CurrentCount);
  120. }
  121. [TestMethod]
  122. public void ExceptionShouldNeverHaveFired()
  123. {
  124. Assert.AreEqual(0, _channelExceptionRegister.Count);
  125. }
  126. [TestMethod]
  127. public void ClosedEventShouldNeverHaveFired()
  128. {
  129. Assert.AreEqual(0, _channelClosedRegister.Count);
  130. }
  131. [TestMethod]
  132. public void IsOpenShouldReturnTrue()
  133. {
  134. Assert.IsTrue(_channel.IsOpen);
  135. }
  136. }
  137. }