ChannelTest_OnSessionChannelRequestReceived_OnRequest_Exception.cs 2.5 KB

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