2
0

ChannelSessionTest_Open_ExceptionWaitingOnOpenConfirmation.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_Open_ExceptionWaitingOnOpenConfirmation : ChannelSessionTestBase
  13. {
  14. private ChannelSession _channel;
  15. private uint _localChannelNumber;
  16. private uint _localWindowSize;
  17. private uint _localPacketSize;
  18. private IList<ChannelEventArgs> _channelClosedRegister;
  19. private List<ExceptionEventArgs> _channelExceptionRegister;
  20. private SemaphoreSlim _sessionSemaphore;
  21. private int _initialSessionSemaphoreCount;
  22. private Exception _waitOnConfirmationException;
  23. private SystemException _actualException;
  24. protected override void SetupData()
  25. {
  26. var random = new Random();
  27. _localChannelNumber = (uint)random.Next(0, int.MaxValue);
  28. _localWindowSize = (uint)random.Next(2000, 3000);
  29. _localPacketSize = (uint)random.Next(1000, 2000);
  30. _initialSessionSemaphoreCount = random.Next(10, 20);
  31. _sessionSemaphore = new SemaphoreSlim(_initialSessionSemaphoreCount);
  32. _channelClosedRegister = new List<ChannelEventArgs>();
  33. _channelExceptionRegister = new List<ExceptionEventArgs>();
  34. _waitOnConfirmationException = new SystemException();
  35. }
  36. protected override void SetupMocks()
  37. {
  38. var sequence = new MockSequence();
  39. SessionMock.InSequence(sequence).Setup(p => p.ConnectionInfo).Returns(ConnectionInfoMock.Object);
  40. ConnectionInfoMock.InSequence(sequence).Setup(p => p.RetryAttempts).Returns(2);
  41. SessionMock.Setup(p => p.SessionSemaphore).Returns(_sessionSemaphore);
  42. SessionMock.InSequence(sequence)
  43. .Setup(
  44. p =>
  45. p.SendMessage(
  46. It.Is<ChannelOpenMessage>(
  47. m =>
  48. m.LocalChannelNumber == _localChannelNumber &&
  49. m.InitialWindowSize == _localWindowSize && m.MaximumPacketSize == _localPacketSize &&
  50. m.Info is SessionChannelOpenInfo)));
  51. SessionMock.InSequence(sequence)
  52. .Setup(p => p.WaitOnHandle(It.IsNotNull<WaitHandle>()))
  53. .Throws(_waitOnConfirmationException);
  54. }
  55. protected override void Arrange()
  56. {
  57. base.Arrange();
  58. _channel = new ChannelSession(SessionMock.Object, _localChannelNumber, _localWindowSize, _localPacketSize);
  59. _channel.Closed += (sender, args) => _channelClosedRegister.Add(args);
  60. _channel.Exception += (sender, args) => _channelExceptionRegister.Add(args);
  61. }
  62. protected override void Act()
  63. {
  64. try
  65. {
  66. _channel.Open();
  67. }
  68. catch (SystemException ex)
  69. {
  70. _actualException = ex;
  71. }
  72. }
  73. [TestMethod]
  74. public void OpenShouldHaveRethrownExceptionThrownByWaitOnHandle()
  75. {
  76. Assert.IsNotNull(_actualException);
  77. Assert.AreSame(_waitOnConfirmationException, _actualException);
  78. }
  79. [TestMethod]
  80. public void CurrentCountOfSessionSemaphoreShouldBeEqualToInitialCount()
  81. {
  82. Assert.AreEqual(_initialSessionSemaphoreCount, _sessionSemaphore.CurrentCount);
  83. }
  84. [TestMethod]
  85. public void ExceptionShouldNeverHaveFired()
  86. {
  87. Assert.AreEqual(0, _channelExceptionRegister.Count);
  88. }
  89. [TestMethod]
  90. public void ClosedEventShouldNeverHaveFired()
  91. {
  92. Assert.AreEqual(0, _channelClosedRegister.Count);
  93. }
  94. [TestMethod]
  95. public void IsOpenShouldReturnFalse()
  96. {
  97. Assert.IsFalse(_channel.IsOpen);
  98. }
  99. }
  100. }