ChannelTest_OnSessionChannelWindowAdjustReceived_OnWindowAdjust_Exception.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using Renci.SshNet.Common;
  5. using Renci.SshNet.Messages.Connection;
  6. namespace Renci.SshNet.Tests.Classes.Channels
  7. {
  8. [TestClass]
  9. public class ChannelTest_OnSessionChannelWindowAdjustReceived_OnWindowAdjust_Exception : ChannelTestBase
  10. {
  11. private uint _localChannelNumber;
  12. private uint _localWindowSize;
  13. private uint _localPacketSize;
  14. private uint _remoteChannelNumber;
  15. private uint _remoteWindowSize;
  16. private uint _remotePacketSize;
  17. private ChannelStub _channel;
  18. private IList<ExceptionEventArgs> _channelExceptionRegister;
  19. private Exception _onWindowAdjustException;
  20. private uint _bytesToAdd;
  21. protected override void SetupData()
  22. {
  23. var random = new Random();
  24. _localChannelNumber = (uint)random.Next(0, int.MaxValue);
  25. _localWindowSize = (uint)random.Next(1000, int.MaxValue);
  26. _localPacketSize = _localWindowSize - 1;
  27. _remoteChannelNumber = (uint)random.Next(0, int.MaxValue);
  28. _remoteWindowSize = (uint)random.Next(1000, int.MaxValue);
  29. _remotePacketSize = _localWindowSize - 1;
  30. _bytesToAdd = (uint)random.Next(0, int.MaxValue);
  31. _onWindowAdjustException = new SystemException();
  32. _channelExceptionRegister = new List<ExceptionEventArgs>();
  33. }
  34. protected override void SetupMocks()
  35. {
  36. }
  37. protected override void Arrange()
  38. {
  39. base.Arrange();
  40. _channel = new ChannelStub(SessionMock.Object, _localChannelNumber, _localWindowSize, _localPacketSize);
  41. _channel.InitializeRemoteChannelInfo(_remoteChannelNumber, _remoteWindowSize, _remotePacketSize);
  42. _channel.Exception += (sender, args) => _channelExceptionRegister.Add(args);
  43. _channel.OnWindowAdjustException = _onWindowAdjustException;
  44. }
  45. protected override void Act()
  46. {
  47. SessionMock.Raise(s => s.ChannelWindowAdjustReceived += null,
  48. new MessageEventArgs<ChannelWindowAdjustMessage>(new ChannelWindowAdjustMessage(_localChannelNumber, _bytesToAdd)));
  49. }
  50. [TestMethod]
  51. public void ExceptionEventShouldHaveFiredOnce()
  52. {
  53. Assert.AreEqual(1, _channelExceptionRegister.Count);
  54. Assert.AreSame(_onWindowAdjustException, _channelExceptionRegister[0].Exception);
  55. }
  56. [TestMethod]
  57. public void OnErrorOccurredShouldBeInvokedOnce()
  58. {
  59. Assert.AreEqual(1, _channel.OnErrorOccurredInvocations.Count);
  60. Assert.AreSame(_onWindowAdjustException, _channel.OnErrorOccurredInvocations[0]);
  61. }
  62. }
  63. }