ChannelTest_OnSessionChannelDataReceived_OnData_Exception.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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_OnSessionChannelDataReceived_OnData_Exception : ChannelTestBase
  10. {
  11. private uint _localWindowSize;
  12. private uint _localPacketSize;
  13. private uint _localChannelNumber;
  14. private ChannelStub _channel;
  15. private IList<ExceptionEventArgs> _channelExceptionRegister;
  16. private Exception _onDataException;
  17. protected override void SetupData()
  18. {
  19. var random = new Random();
  20. _localWindowSize = (uint)random.Next(1000, int.MaxValue);
  21. _localPacketSize = _localWindowSize - 1;
  22. _localChannelNumber = (uint)random.Next(0, int.MaxValue);
  23. _onDataException = new SystemException();
  24. _channelExceptionRegister = new List<ExceptionEventArgs>();
  25. }
  26. protected override void SetupMocks()
  27. {
  28. }
  29. protected override void Arrange()
  30. {
  31. base.Arrange();
  32. _channel = new ChannelStub(SessionMock.Object, _localChannelNumber, _localWindowSize, _localPacketSize);
  33. _channel.Exception += (sender, args) => _channelExceptionRegister.Add(args);
  34. _channel.OnDataException = _onDataException;
  35. }
  36. protected override void Act()
  37. {
  38. SessionMock.Raise(s => s.ChannelDataReceived += null,
  39. new MessageEventArgs<ChannelDataMessage>(new ChannelDataMessage(_localChannelNumber, new byte[0])));
  40. }
  41. [TestMethod]
  42. public void ExceptionEventShouldHaveFiredOnce()
  43. {
  44. Assert.AreEqual(1, _channelExceptionRegister.Count);
  45. Assert.AreSame(_onDataException, _channelExceptionRegister[0].Exception);
  46. }
  47. [TestMethod]
  48. public void OnErrorOccuredShouldBeInvokedOnce()
  49. {
  50. Assert.AreEqual(1, _channel.OnErrorOccurredInvocations.Count);
  51. Assert.AreSame(_onDataException, _channel.OnErrorOccurredInvocations[0]);
  52. }
  53. }
  54. }