ChannelTest_OnSessionChannelWindowAdjustReceived_OnWindowAdjust_Exception.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 ChannelTest_OnSessionChannelWindowAdjustReceived_OnWindowAdjust_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 uint _remoteWindowSize;
  18. private uint _remotePacketSize;
  19. private ChannelStub _channel;
  20. private IList<ExceptionEventArgs> _channelExceptionRegister;
  21. private Exception _onWindowAdjustException;
  22. private uint _bytesToAdd;
  23. [TestInitialize]
  24. public void Initialize()
  25. {
  26. Arrange();
  27. Act();
  28. }
  29. private void Arrange()
  30. {
  31. var random = new Random();
  32. _localChannelNumber = (uint)random.Next(0, int.MaxValue);
  33. _localWindowSize = (uint)random.Next(1000, int.MaxValue);
  34. _localPacketSize = _localWindowSize - 1;
  35. _remoteChannelNumber = (uint)random.Next(0, int.MaxValue);
  36. _remoteWindowSize = (uint)random.Next(1000, int.MaxValue);
  37. _remotePacketSize = _localWindowSize - 1;
  38. _bytesToAdd = (uint)random.Next(0, int.MaxValue);
  39. _onWindowAdjustException = new SystemException();
  40. _channelExceptionRegister = new List<ExceptionEventArgs>();
  41. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  42. _channel = new ChannelStub(_sessionMock.Object, _localChannelNumber, _localWindowSize, _localPacketSize);
  43. _channel.InitializeRemoteChannelInfo(_remoteChannelNumber, _remoteWindowSize, _remotePacketSize);
  44. _channel.Exception += (sender, args) => _channelExceptionRegister.Add(args);
  45. _channel.OnWindowAdjustException = _onWindowAdjustException;
  46. }
  47. private void Act()
  48. {
  49. _sessionMock.Raise(s => s.ChannelWindowAdjustReceived += null,
  50. new MessageEventArgs<ChannelWindowAdjustMessage>(new ChannelWindowAdjustMessage(_localChannelNumber, _bytesToAdd)));
  51. }
  52. [TestMethod]
  53. public void ExceptionEventShouldHaveFiredOnce()
  54. {
  55. Assert.AreEqual(1, _channelExceptionRegister.Count);
  56. Assert.AreSame(_onWindowAdjustException, _channelExceptionRegister[0].Exception);
  57. }
  58. [TestMethod]
  59. public void OnErrorOccuredShouldBeInvokedOnce()
  60. {
  61. Assert.AreEqual(1, _channel.OnErrorOccurredInvocations.Count);
  62. Assert.AreSame(_onWindowAdjustException, _channel.OnErrorOccurredInvocations[0]);
  63. }
  64. }
  65. }