ChannelSessionTest_Open_ExceptionWaitingOnOpenConfirmation.cs 4.2 KB

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