ChannelTest_OnSessionChannelRequestReceived_OnRequest_Exception.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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_OnSessionChannelRequestReceived_OnRequest_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 _onRequestException;
  17. private SignalRequestInfo _requestInfo;
  18. protected override void SetupData()
  19. {
  20. var random = new Random();
  21. _localWindowSize = (uint)random.Next(1000, int.MaxValue);
  22. _localPacketSize = _localWindowSize - 1;
  23. _localChannelNumber = (uint)random.Next(0, int.MaxValue);
  24. _onRequestException = new SystemException();
  25. _channelExceptionRegister = new List<ExceptionEventArgs>();
  26. _requestInfo = new SignalRequestInfo("ABC");
  27. }
  28. protected override void SetupMocks()
  29. {
  30. _ = SessionMock.Setup(p => p.ConnectionInfo)
  31. .Returns(new ConnectionInfo("host", "user", new PasswordAuthenticationMethod("user", "password")));
  32. }
  33. protected override void Arrange()
  34. {
  35. base.Arrange();
  36. _channel = new ChannelStub(SessionMock.Object, _localChannelNumber, _localWindowSize, _localPacketSize);
  37. _channel.Exception += (sender, args) => _channelExceptionRegister.Add(args);
  38. _channel.OnRequestException = _onRequestException;
  39. }
  40. protected override void Act()
  41. {
  42. SessionMock.Raise(s => s.ChannelRequestReceived += null,
  43. new MessageEventArgs<ChannelRequestMessage>(new ChannelRequestMessage(_localChannelNumber, _requestInfo)));
  44. }
  45. [TestMethod]
  46. public void ExceptionEventShouldHaveFiredOnce()
  47. {
  48. Assert.AreEqual(1, _channelExceptionRegister.Count);
  49. Assert.AreSame(_onRequestException, _channelExceptionRegister[0].Exception);
  50. }
  51. [TestMethod]
  52. public void OnErrorOccuredShouldBeInvokedOnce()
  53. {
  54. Assert.AreEqual(1, _channel.OnErrorOccurredInvocations.Count);
  55. Assert.AreSame(_onRequestException, _channel.OnErrorOccurredInvocations[0]);
  56. }
  57. }
  58. }