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