ClientChannelTest_OnSessionChannelOpenConfirmationReceived_OnOpenConfirmation_Exception.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Extensions.Logging.Abstractions;
  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 ClientChannelTest_OnSessionChannelOpenConfirmationReceived_OnOpenConfirmation_Exception
  12. {
  13. private Mock<ISession> _sessionMock;
  14. private uint _localChannelNumber;
  15. private uint _localWindowSize;
  16. private uint _localPacketSize;
  17. private uint _remoteChannelNumber;
  18. private ClientChannelStub _channel;
  19. private IList<ExceptionEventArgs> _channelExceptionRegister;
  20. private Exception _onOpenConfirmationException;
  21. [TestInitialize]
  22. public void Initialize()
  23. {
  24. Arrange();
  25. Act();
  26. }
  27. private void Arrange()
  28. {
  29. var random = new Random();
  30. _localChannelNumber = (uint)random.Next(0, int.MaxValue);
  31. _localWindowSize = (uint)random.Next(1000, int.MaxValue);
  32. _localPacketSize = _localWindowSize - 1;
  33. _remoteChannelNumber = (uint)random.Next(0, int.MaxValue);
  34. _onOpenConfirmationException = new SystemException();
  35. _channelExceptionRegister = new List<ExceptionEventArgs>();
  36. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  37. _sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
  38. _channel = new ClientChannelStub(_sessionMock.Object, _localChannelNumber, _localWindowSize, _localPacketSize);
  39. _channel.Exception += (sender, args) => _channelExceptionRegister.Add(args);
  40. _channel.OnOpenConfirmationException = _onOpenConfirmationException;
  41. }
  42. private void Act()
  43. {
  44. _sessionMock.Raise(s => s.ChannelOpenConfirmationReceived += null,
  45. new MessageEventArgs<ChannelOpenConfirmationMessage>(
  46. new ChannelOpenConfirmationMessage(_localChannelNumber, _localWindowSize, _localPacketSize,
  47. _remoteChannelNumber)));
  48. }
  49. [TestMethod]
  50. public void ExceptionEventShouldHaveFiredOnce()
  51. {
  52. Assert.AreEqual(1, _channelExceptionRegister.Count);
  53. Assert.AreSame(_onOpenConfirmationException, _channelExceptionRegister[0].Exception);
  54. }
  55. [TestMethod]
  56. public void OnErrorOccurredShouldBeInvokedOnce()
  57. {
  58. Assert.AreEqual(1, _channel.OnErrorOccurredInvocations.Count);
  59. Assert.AreSame(_onOpenConfirmationException, _channel.OnErrorOccurredInvocations[0]);
  60. }
  61. }
  62. }