ClientChannelTest_OnSessionChannelOpenConfirmationReceived_OnOpenConfirmation_Exception.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using Moq;
  5. using Renci.SshNet.Common;
  6. using Renci.SshNet.Messages.Connection;
  7. namespace Renci.SshNet.Tests.Classes.Channels
  8. {
  9. [TestClass]
  10. public class ClientChannelTest_OnSessionChannelOpenConfirmationReceived_OnOpenConfirmation_Exception
  11. {
  12. private Mock<ISession> _sessionMock;
  13. private uint _localChannelNumber;
  14. private uint _localWindowSize;
  15. private uint _localPacketSize;
  16. private uint _remoteChannelNumber;
  17. private ClientChannelStub _channel;
  18. private IList<ExceptionEventArgs> _channelExceptionRegister;
  19. private Exception _onOpenConfirmationException;
  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. _remoteChannelNumber = (uint)random.Next(0, int.MaxValue);
  33. _onOpenConfirmationException = new SystemException();
  34. _channelExceptionRegister = new List<ExceptionEventArgs>();
  35. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  36. _channel = new ClientChannelStub(_sessionMock.Object, _localChannelNumber, _localWindowSize, _localPacketSize);
  37. _channel.Exception += (sender, args) => _channelExceptionRegister.Add(args);
  38. _channel.OnOpenConfirmationException = _onOpenConfirmationException;
  39. }
  40. private void Act()
  41. {
  42. _sessionMock.Raise(s => s.ChannelOpenConfirmationReceived += null,
  43. new MessageEventArgs<ChannelOpenConfirmationMessage>(
  44. new ChannelOpenConfirmationMessage(_localChannelNumber, _localWindowSize, _localPacketSize,
  45. _remoteChannelNumber)));
  46. }
  47. [TestMethod]
  48. public void ExceptionEventShouldHaveFiredOnce()
  49. {
  50. Assert.AreEqual(1, _channelExceptionRegister.Count);
  51. Assert.AreSame(_onOpenConfirmationException, _channelExceptionRegister[0].Exception);
  52. }
  53. [TestMethod]
  54. public void OnErrorOccuredShouldBeInvokedOnce()
  55. {
  56. Assert.AreEqual(1, _channel.OnErrorOccurredInvocations.Count);
  57. Assert.AreSame(_onOpenConfirmationException, _channel.OnErrorOccurredInvocations[0]);
  58. }
  59. }
  60. }