|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | + |
| 4 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 5 | + |
| 6 | +using Moq; |
| 7 | + |
| 8 | +using Renci.SshNet.Common; |
| 9 | +using Renci.SshNet.Messages; |
| 10 | +using Renci.SshNet.Messages.Connection; |
| 11 | + |
| 12 | +namespace Renci.SshNet.Tests.Classes.Channels |
| 13 | +{ |
| 14 | + [TestClass] |
| 15 | + public class ChannelTest_OnSessionChannelRequestReceived_HandleUnknownMessage : ChannelTestBase |
| 16 | + { |
| 17 | + private uint _localWindowSize; |
| 18 | + private uint _localPacketSize; |
| 19 | + private uint _localChannelNumber; |
| 20 | + private uint _remoteChannelNumber; |
| 21 | + private uint _remoteWindowSize; |
| 22 | + private uint _remotePacketSize; |
| 23 | + private ChannelStub _channel; |
| 24 | + private IList<ExceptionEventArgs> _channelExceptionRegister; |
| 25 | + private UnknownRequestInfoWithWantReply _requestInfo; |
| 26 | + |
| 27 | + protected override void SetupData() |
| 28 | + { |
| 29 | + var random = new Random(); |
| 30 | + |
| 31 | + _localWindowSize = (uint) random.Next(1000, int.MaxValue); |
| 32 | + _localPacketSize = _localWindowSize - 1; |
| 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 | + _channelExceptionRegister = new List<ExceptionEventArgs>(); |
| 38 | + _requestInfo = new UnknownRequestInfoWithWantReply(); |
| 39 | + } |
| 40 | + |
| 41 | + protected override void SetupMocks() |
| 42 | + { |
| 43 | + _ = SessionMock.Setup(p => p.ConnectionInfo) |
| 44 | + .Returns(new ConnectionInfo("host", "user", new PasswordAuthenticationMethod("user", "password"))); |
| 45 | + _ = SessionMock.Setup(p => p.SendMessage(It.IsAny<Message>())); |
| 46 | + } |
| 47 | + |
| 48 | + protected override void Arrange() |
| 49 | + { |
| 50 | + base.Arrange(); |
| 51 | + |
| 52 | + _channel = new ChannelStub(SessionMock.Object, _localChannelNumber, _localWindowSize, _localPacketSize); |
| 53 | + _channel.InitializeRemoteChannelInfo(_remoteChannelNumber, _remoteWindowSize, _remotePacketSize); |
| 54 | + _channel.SetIsOpen(true); |
| 55 | + _channel.Exception += (sender, args) => _channelExceptionRegister.Add(args); |
| 56 | + } |
| 57 | + |
| 58 | + protected override void Act() |
| 59 | + { |
| 60 | + SessionMock.Raise(s => s.ChannelRequestReceived += null, |
| 61 | + new MessageEventArgs<ChannelRequestMessage>(new ChannelRequestMessage(_localChannelNumber, _requestInfo))); |
| 62 | + } |
| 63 | + |
| 64 | + [TestMethod] |
| 65 | + public void FailureMessageWasSent() |
| 66 | + { |
| 67 | + SessionMock.Verify(p => p.SendMessage(It.Is<ChannelFailureMessage>(m => m.LocalChannelNumber == _channel.RemoteChannelNumber)), Times.Once); |
| 68 | + } |
| 69 | + |
| 70 | + [TestMethod] |
| 71 | + public void NoExceptionShouldHaveFired() |
| 72 | + { |
| 73 | + Assert.AreEqual(0, _channelExceptionRegister.Count); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + internal class UnknownRequestInfoWithWantReply : RequestInfo |
| 78 | + { |
| 79 | + public override string RequestName |
| 80 | + { |
| 81 | + get |
| 82 | + { |
| 83 | + return nameof(UnknownRequestInfoWithWantReply); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + internal UnknownRequestInfoWithWantReply() |
| 88 | + { |
| 89 | + WantReply = true; |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments