コード例 #1
0
        protected void Arrange()
        {
            var random = new Random();

            _operationTimeout = TimeSpan.FromMilliseconds(random.Next(100, 500));
            _encoding         = Encoding.UTF8;

            _bAvail = (ulong)random.Next(0, int.MaxValue);

            _sessionMock        = new Mock <ISession>(MockBehavior.Strict);
            _channelSessionMock = new Mock <IChannelSession>(MockBehavior.Strict);

            var sequence = new MockSequence();

            _sessionMock.InSequence(sequence).Setup(p => p.CreateChannelSession()).Returns(_channelSessionMock.Object);
            _channelSessionMock.InSequence(sequence).Setup(p => p.Open());
            _channelSessionMock.InSequence(sequence).Setup(p => p.SendSubsystemRequest("sftp")).Returns(true);
            _channelSessionMock.InSequence(sequence).Setup(p => p.IsOpen).Returns(true);
            _channelSessionMock.InSequence(sequence).Setup(p => p.SendData(It.IsAny <byte[]>())).Callback(
                () =>
            {
                // generate response for SftpInitRequest
                var versionInfoResponse = SftpVersionResponseBuilder.Create(3)
                                          .AddExtension("*****@*****.**", "")
                                          .Build();
                _channelSessionMock.Raise(
                    c => c.DataReceived += null,
                    new ChannelDataEventArgs(0, versionInfoResponse));
            });
            _channelSessionMock.InSequence(sequence).Setup(p => p.IsOpen).Returns(true);
            _channelSessionMock.InSequence(sequence).Setup(p => p.SendData(It.IsAny <byte[]>())).Callback(
                () =>
            {
                var sftpNameResponse = CreateSftpNameResponse(1, _encoding, "ABC");

                _channelSessionMock.Raise(
                    c => c.DataReceived += null,
                    new ChannelDataEventArgs(0, sftpNameResponse));
            }
                );
            _channelSessionMock.InSequence(sequence).Setup(p => p.IsOpen).Returns(true);
            _channelSessionMock.InSequence(sequence).Setup(p => p.SendData(It.IsAny <byte[]>())).Callback(
                () =>
            {
                var statVfsReplyBuilder = StatVfsReplyBuilder.Create(2);
                statVfsReplyBuilder.WithBAvail(_bAvail);
                var statVfsReply = statVfsReplyBuilder.Build();

                _channelSessionMock.Raise(
                    c => c.DataReceived += null,
                    new ChannelDataEventArgs(0, statVfsReply));
            }
                );

            _sftpSession = new SftpSession(_sessionMock.Object, _operationTimeout, _encoding);
            _sftpSession.Connect();
        }
コード例 #2
0
        protected void Arrange()
        {
            var random = new Random();

            _operationTimeout = TimeSpan.FromMilliseconds(random.Next(100, 500));
            _expected         = new byte[random.Next(30, 50)];
            _encoding         = Encoding.UTF8;
            random.NextBytes(_expected);

            _sessionMock        = new Mock <ISession>(MockBehavior.Strict);
            _channelSessionMock = new Mock <IChannelSession>(MockBehavior.Strict);

            var sequence = new MockSequence();

            _sessionMock.InSequence(sequence).Setup(p => p.CreateChannelSession()).Returns(_channelSessionMock.Object);
            _channelSessionMock.InSequence(sequence).Setup(p => p.Open());
            _channelSessionMock.InSequence(sequence).Setup(p => p.SendSubsystemRequest("sftp")).Returns(true);
            _channelSessionMock.InSequence(sequence).Setup(p => p.IsOpen).Returns(true);
            _channelSessionMock.InSequence(sequence).Setup(p => p.SendData(It.IsAny <byte[]>())).Callback(
                () =>
            {
                // generate response for SftpInitRequest
                var versionInfoResponse = SftpVersionResponseBuilder.Create(3)
                                          .Build();
                _channelSessionMock.Raise(
                    c => c.DataReceived += null,
                    new ChannelDataEventArgs(0, versionInfoResponse));
            });
            _channelSessionMock.InSequence(sequence).Setup(p => p.IsOpen).Returns(true);
            _channelSessionMock.InSequence(sequence).Setup(p => p.SendData(It.IsAny <byte[]>())).Callback(
                () =>
            {
                var sftpNameResponse = CreateSftpNameResponse(1, _encoding, "ABC");

                _channelSessionMock.Raise(
                    c => c.DataReceived += null,
                    new ChannelDataEventArgs(0, sftpNameResponse));
            }
                );
            _channelSessionMock.InSequence(sequence).Setup(p => p.IsOpen).Returns(true);
            _channelSessionMock.InSequence(sequence).Setup(p => p.SendData(It.IsAny <byte[]>())).Callback(
                () =>
            {
                var sftpDataResponse = CreateSftpDataResponse(2, _expected);

                _channelSessionMock.Raise(
                    c => c.DataReceived += null,
                    new ChannelDataEventArgs(0, sftpDataResponse.Take(0, 20)));
                _channelSessionMock.Raise(
                    c => c.DataReceived += null,
                    new ChannelDataEventArgs(0, sftpDataResponse.Take(20, sftpDataResponse.Length - 20)));
            }
                );

            _sftpSession = new SftpSession(_sessionMock.Object, _operationTimeout, _encoding);
            _sftpSession.Connect();
        }