ClientChannelTest_OnSessionChannelOpenFailureReceived_OnOpenFailure_Exception.cs 2.5 KB

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