ChannelTest_OnSessionChannelEofReceived_OnEof_Exception.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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_OnSessionChannelEofReceived_Exception
  11. {
  12. private Mock<ISession> _sessionMock;
  13. private uint _localWindowSize;
  14. private uint _localPacketSize;
  15. private uint _localChannelNumber;
  16. private uint _remoteChannelNumber;
  17. private uint _remoteWindowSize;
  18. private uint _remotePacketSize;
  19. private ChannelStub _channel;
  20. private IList<ExceptionEventArgs> _channelExceptionRegister;
  21. private Exception _onEofException;
  22. [TestInitialize]
  23. public void Initialize()
  24. {
  25. Arrange();
  26. Act();
  27. }
  28. private void Arrange()
  29. {
  30. var random = new Random();
  31. _localWindowSize = (uint)random.Next(0, 1000);
  32. _localPacketSize = (uint)random.Next(1001, int.MaxValue);
  33. _localChannelNumber = (uint)random.Next(0, int.MaxValue);
  34. _remoteChannelNumber = (uint)random.Next(0, int.MaxValue);
  35. _remoteWindowSize = (uint)random.Next(0, int.MaxValue);
  36. _remotePacketSize = (uint)random.Next(0, int.MaxValue);
  37. _onEofException = new SystemException();
  38. _channelExceptionRegister = new List<ExceptionEventArgs>();
  39. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  40. _channel = new ChannelStub(_sessionMock.Object, _localChannelNumber, _localWindowSize, _localPacketSize);
  41. _channel.Exception += (sender, args) => _channelExceptionRegister.Add(args);
  42. _channel.InitializeRemoteChannelInfo(_remoteChannelNumber, _remoteWindowSize, _remotePacketSize);
  43. _channel.SetIsOpen(true);
  44. _channel.OnEofException = _onEofException;
  45. }
  46. private void Act()
  47. {
  48. _sessionMock.Raise(s => s.ChannelEofReceived += null,
  49. new MessageEventArgs<ChannelEofMessage>(new ChannelEofMessage(_localChannelNumber)));
  50. }
  51. [TestMethod]
  52. public void ExceptionEventShouldHaveFiredOnce()
  53. {
  54. Assert.AreEqual(1, _channelExceptionRegister.Count);
  55. Assert.AreSame(_onEofException, _channelExceptionRegister[0].Exception);
  56. }
  57. [TestMethod]
  58. public void OnErrorOccuredShouldBeInvokedOnce()
  59. {
  60. Assert.AreEqual(1, _channel.OnErrorOccurredInvocations.Count);
  61. Assert.AreSame(_onEofException, _channel.OnErrorOccurredInvocations[0]);
  62. }
  63. }
  64. }