public void ReadByteShouldStartReadingAtBeginningOfFile()
        {
            var data = GenerateRandom(5, _random);

            SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true);
            SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize))
            .Returns(data);

            var actual = _target.ReadByte();

            Assert.AreEqual(data[0], actual);

            SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1));
            SftpSessionMock.Verify(p => p.RequestRead(_handle, 0UL, _readBufferSize), Times.Once);
        }
        public void DisposeShouldCloseHandleAndCompleteImmediately()
        {
            SftpSessionMock.InSequence(_seq).Setup(p => p.BeginClose(_handle, null, null)).Returns(_closeAsyncResult);
            SftpSessionMock.InSequence(_seq).Setup(p => p.EndClose(_closeAsyncResult));

            var stopwatch = Stopwatch.StartNew();

            _reader.Dispose();
            stopwatch.Stop();

            Assert.IsTrue(stopwatch.ElapsedMilliseconds < 200, "Dispose took too long to complete: " + stopwatch.ElapsedMilliseconds);

            SftpSessionMock.Verify(p => p.BeginClose(_handle, null, null), Times.Once);
            SftpSessionMock.Verify(p => p.EndClose(_closeAsyncResult), Times.Once);
        }
        public void SubsequentReadShouldReadAgainFromCurrentPositionFromServerAndNotUpdatePositionWhenServerReturnsZeroBytes()
        {
            SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true);
            SftpSessionMock.InSequence(MockSequence)
            .Setup(p => p.RequestRead(_handle, (ulong)_actual, _readBufferSize))
            .Returns(Array <byte> .Empty);
            SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true);

            _target.Read(new byte[10], 0, 10);

            Assert.AreEqual(_actual, _target.Position);

            SftpSessionMock.Verify(p => p.RequestRead(_handle, (ulong)_actual, _readBufferSize), Times.Once);
            SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3));
        }
Esempio n. 4
0
        public void WriteByteShouldThrowNotSupportedException()
        {
            SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true);

            try
            {
                _target.WriteByte(0x0a);
            }
            catch (NotSupportedException ex)
            {
                Assert.IsNull(ex.InnerException);
                Assert.AreEqual("Write not supported.", ex.Message);
            }

            SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1));
        }
        public void SubsequentReadShouldReadAgainFromCurrentPositionFromServerAndReturnZeroWhenServerReturnsZeroBytes()
        {
            SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true);
            SftpSessionMock.InSequence(MockSequence)
            .Setup(p => p.RequestRead(_handle, (ulong)_actual, _readBufferSize))
            .Returns(Array <byte> .Empty);

            var buffer = _originalBuffer.Copy();
            var actual = _target.Read(buffer, 0, buffer.Length);

            Assert.AreEqual(0, actual);
            Assert.IsTrue(_originalBuffer.IsEqualTo(buffer));

            SftpSessionMock.Verify(p => p.RequestRead(_handle, (ulong)_actual, _readBufferSize), Times.Once);
            SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(2));
        }
        public void SubsequentReadShouldReturnAllRemaningBytesFromReadBufferAndReadAgainWhenCountIsGreaterThanNumberOfRemainingBytesAndNewReadReturnsZeroBytes()
        {
            SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true);
            SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestRead(_handle, (ulong)(_serverData.Length), _readBufferSize)).Returns(Array <byte> .Empty);

            var buffer = new byte[_numberOfBytesToWriteToReadBuffer + 1];

            var actual = _target.Read(buffer, 0, buffer.Length);

            Assert.AreEqual(_numberOfBytesToWriteToReadBuffer, actual);
            Assert.IsTrue(_serverData.Take(_numberOfBytesToRead, _numberOfBytesToWriteToReadBuffer).IsEqualTo(buffer.Take(_numberOfBytesToWriteToReadBuffer)));
            Assert.AreEqual(0, buffer[_numberOfBytesToWriteToReadBuffer]);

            SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(2));
            SftpSessionMock.Verify(p => p.RequestRead(_handle, (ulong)(_serverData.Length), _readBufferSize));
        }
        public void FlushShouldFlushBuffer()
        {
            byte[] actualFlushedData = null;

            SftpSessionMock.InSequence(MockSequence)
            .Setup(p => p.IsOpen)
            .Returns(true);
            SftpSessionMock.InSequence(MockSequence)
            .Setup(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny <byte[]>(), 0, _expectedBufferedByteCount, It.IsAny <AutoResetEvent>(), null))
            .Callback <byte[], ulong, byte[], int, int, AutoResetEvent, Action <SftpStatusResponse> >((handle, serverFileOffset, data, offset, length, wait, writeCompleted) => actualFlushedData = data.Take(offset, length));

            _target.Flush();

            Assert.IsTrue(actualFlushedData.IsEqualTo(_expectedBufferedBytes));

            SftpSessionMock.Verify(p => p.RequestWrite(_handle, _expectedWrittenByteCount, It.IsAny <byte[]>(), 0, _expectedBufferedByteCount, It.IsAny <AutoResetEvent>(), null), Times.Once);
        }
Esempio n. 8
0
        public void ReadShouldStartReadingAtBeginningOfFile()
        {
            var buffer   = new byte[8];
            var data     = new byte[] { 5, 4, 3, 2, 1 };
            var expected = new byte[] { 0, 5, 4, 3, 2, 1, 0, 0 };

            SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true);
            SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize)).Returns(data);

            var actual = _target.Read(buffer, 1, data.Length);

            Assert.AreEqual(data.Length, actual);
            Assert.IsTrue(buffer.IsEqualTo(expected));

            SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1));
            SftpSessionMock.Verify(p => p.RequestRead(_handle, 0UL, _readBufferSize), Times.Once);
        }
Esempio n. 9
0
        public void ReadBytesThatWereNotBufferedBeforeSeekShouldReadBytesFromServer()
        {
            SftpSessionMock.InSequence(MockSequence)
            .Setup(p => p.IsOpen)
            .Returns(true);
            SftpSessionMock.InSequence(MockSequence)
            .Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize))
            .Returns(_serverData2);

            var bytesRead = _target.Read(_buffer, 0, _buffer.Length);

            Assert.AreEqual(_buffer.Length, bytesRead);
            Assert.IsTrue(_serverData2.Take(_buffer.Length).IsEqualTo(_buffer));

            SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3));
            SftpSessionMock.Verify(p => p.RequestRead(_handle, 0UL, _readBufferSize), Times.Exactly(2));
        }
Esempio n. 10
0
        public void WriteShouldThrowNotSupportedException()
        {
            var buffer = new byte[_writeBufferSize];

            SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true);

            try
            {
                _target.Write(buffer, 0, buffer.Length);
            }
            catch (NotSupportedException ex)
            {
                Assert.IsNull(ex.InnerException);
                Assert.AreEqual("Write not supported.", ex.Message);
            }

            SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1));
        }
Esempio n. 11
0
        public void ReadShouldReadStartFromSamePositionAsBeforeSetLength()
        {
            SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true);
            SftpSessionMock.InSequence(_sequence)
            .Setup(p => p.RequestRead(_handle,
                                      (uint)(_readBytes1.Length + _readBytes2.Length),
                                      _readBufferSize))
            .Returns(new byte[] { 0x0f });

            var byteRead = _sftpFileStream.ReadByte();

            Assert.AreEqual(0x0f, byteRead);

            SftpSessionMock.Verify(p => p.RequestRead(_handle,
                                                      (uint)(_readBytes1.Length + _readBytes2.Length),
                                                      _readBufferSize),
                                   Times.Once);
            SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4));
        }
Esempio n. 12
0
        public void ReadShouldReturnReadBytesFromServer()
        {
            SftpSessionMock.InSequence(MockSequence)
            .Setup(p => p.IsOpen)
            .Returns(true);
            SftpSessionMock.InSequence(MockSequence)
            .Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize))
            .Returns(new byte[] { 0x05, 0x04 });

            var buffer = new byte[1];

            var bytesRead = _target.Read(buffer, 0, buffer.Length);

            Assert.AreEqual(buffer.Length, bytesRead);
            Assert.AreEqual(0x05, buffer[0]);

            SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3));
            SftpSessionMock.Verify(p => p.RequestRead(_handle, 0UL, _readBufferSize), Times.Exactly(2));
        }
        public void ReadShouldReadFromServer()
        {
            var serverBytes2      = GenerateRandom(5);
            var readBytes2        = new byte[5];
            var expectedReadBytes = new ArrayBuilder <byte>().Add(new byte[2])
                                    .Add(serverBytes2.Take(0, 3))
                                    .Build();

            SftpSessionMock.InSequence(MockSequence)
            .Setup(p => p.IsOpen)
            .Returns(true);
            SftpSessionMock.InSequence(MockSequence)
            .Setup(p => p.RequestRead(_handle, (ulong)_readBytes.Length, _readBufferSize))
            .Returns(serverBytes2);

            var bytesRead = _target.Read(readBytes2, 2, 3);

            Assert.AreEqual(3, bytesRead);
            CollectionAssert.AreEqual(expectedReadBytes, readBytes2);

            SftpSessionMock.Verify(p => p.RequestRead(_handle, (ulong)_readBytes.Length, _readBufferSize), Times.Once);
            SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3));
        }
Esempio n. 14
0
        public void WriteShouldStartFromEndOfStream()
        {
            var bytesToWrite = GenerateRandom(_writeBufferSize);

            byte[] bytesWritten = null;

            SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true);
            SftpSessionMock.InSequence(_sequence)
            .Setup(p => p.RequestWrite(_handle, (uint)_length, It.IsAny <byte[]>(), 0, bytesToWrite.Length, It.IsAny <AutoResetEvent>(), null))
            .Callback <byte[], ulong, byte[], int, int, AutoResetEvent, Action <SftpStatusResponse> >((handle, serverOffset, data, offset, length, wait, writeCompleted) =>
            {
                bytesWritten = data.Take(offset, length);
                wait.Set();
            });

            _sftpFileStream.Write(bytesToWrite, 0, bytesToWrite.Length);

            Assert.IsNotNull(bytesWritten);
            CollectionAssert.AreEqual(bytesToWrite, bytesWritten);

            SftpSessionMock.Verify(p => p.RequestWrite(_handle, (uint)_length, It.IsAny <byte[]>(), 0, bytesToWrite.Length, It.IsAny <AutoResetEvent>(), null), Times.Once);
            SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4));
        }
Esempio n. 15
0
 public void IsOpenOnSftpSessionShouldBeInvokedOnce()
 {
     SftpSessionMock.Verify(p => p.IsOpen, Times.Once);
 }
 public void RequestCloseOnSftpSessionShouldNeverBeInvoked()
 {
     SftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Never);
 }
 public void RequestWriteOnSftpSessionShouldBeInvokedTwice()
 {
     SftpSessionMock.Verify(p => p.RequestWrite(_handle, 0, _data, _offset, (int)_writeBufferSize, It.IsAny <AutoResetEvent>(), null), Times.Once);
     SftpSessionMock.Verify(p => p.RequestWrite(_handle, _writeBufferSize, _data, _offset + (int)_writeBufferSize, (int)_writeBufferSize, It.IsAny <AutoResetEvent>(), null), Times.Once);
 }
 public void BeginCloseOnSftpSessionShouldHaveBeenInvokedOnce()
 {
     SftpSessionMock.Verify(p => p.BeginClose(_handle, null, null), Times.Once);
 }
 public void BeginCloseOnSftpSessionShouldNeverHaveBeenInvoked()
 {
     SftpSessionMock.Verify(p => p.BeginClose(_handle, null, null), Times.Never);
 }
Esempio n. 20
0
 public void RequestFStatOnSftpSessionShouldBeInvokedOnce()
 {
     SftpSessionMock.Verify(p => p.RequestFStat(_handle, false), Times.Once);
 }
Esempio n. 21
0
 public void RequestOpenOnSftpSessionShouldBeInvokedOnce()
 {
     SftpSessionMock.Verify(p => p.RequestOpen(_path, Flags.Write | Flags.Append | Flags.CreateNewOrOpen, false), Times.Once);
 }
 public void IsOpenOnSftpSessionShouldNeverBeInvoked()
 {
     SftpSessionMock.Verify(p => p.IsOpen, Times.Never);
 }
Esempio n. 23
0
 public void HandleShouldHaveBeenClosed()
 {
     SftpSessionMock.Verify(p => p.BeginClose(_handle, null, null), Times.Once);
     SftpSessionMock.Verify(p => p.EndClose(_closeAsyncResult), Times.Once);
 }
 public void RequestOpenOnSftpSessionShouldBeInvokedOnceWithTruncateAndOnceWithCreateNew()
 {
     SftpSessionMock.Verify(p => p.RequestOpen(_path, Flags.Write | Flags.Truncate, true), Times.Once);
     SftpSessionMock.Verify(p => p.RequestOpen(_path, Flags.Write | Flags.CreateNew, false), Times.Once);
 }
Esempio n. 25
0
 public void ExceptionInReadAheadShouldPreventFurtherReadAheads()
 {
     SftpSessionMock.Verify(p => p.BeginRead(_handle, 2 * ChunkLength, ChunkLength, It.IsNotNull <AsyncCallback>(), It.IsAny <BufferedRead>()), Times.Never);
 }
Esempio n. 26
0
 public void RequestCloseOnSftpSessionShouldBeInvokedOnce()
 {
     SftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Once);
 }
Esempio n. 27
0
 public void RequestOpenOnSftpSessionShouldBeInvokedOnce()
 {
     SftpSessionMock.Verify(p => p.RequestOpen(_path, Flags.Read | Flags.Write | Flags.Truncate, false), Times.Once);
 }
Esempio n. 28
0
 public void RequestFSetStatOnSftpSessionShouldBeInvokedOnce()
 {
     SftpSessionMock.Verify(p => p.RequestFSetStat(_handle, _fileAttributes), Times.Once);
 }
 public void EndCloseOnSftpSessionShouldHaveBeenInvokedOnce()
 {
     SftpSessionMock.Verify(p => p.EndClose(_closeAsyncResult), Times.Once);
 }
Esempio n. 30
0
 public void IsOpenOnSftpSessionShouldHaveBeenInvokedTwice()
 {
     SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(2));
 }