protected override void Arrange()
        {
            base.Arrange();

            _sftpFileStream = new SftpFileStream(SftpSessionMock.Object, _path, FileMode.Open, FileAccess.ReadWrite, (int)_bufferSize);
            _sftpFileStream.Read(_readBytes1, 0, _readBytes1.Length);
            _sftpFileStream.Read(_readBytes2, 0, _readBytes2.Length); // this will return bytes from the buffer
        }
Esempio n. 2
0
        protected override void Arrange()
        {
            base.Arrange();

            _target = new SftpFileStream(SftpSessionMock.Object,
                                         _path,
                                         FileMode.Open,
                                         FileAccess.Read,
                                         (int)_bufferSize);
            _target.Read(_readBytes1, 0, _readBytes1.Length);
            _target.Read(_readBytes2, 0, _readBytes2.Length);
        }
Esempio n. 3
0
        protected override void Arrange()
        {
            base.Arrange();

            _target = new SftpFileStream(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize);
            _target.Read(_buffer, 0, _buffer.Length);
        }
Esempio n. 4
0
        protected override void Arrange()
        {
            base.Arrange();

            _sftpFileStream = new SftpFileStream(SftpSessionMock.Object, _path, FileMode.Open, FileAccess.ReadWrite, (int)_bufferSize);
            _sftpFileStream.Read(_readBytes, 0, _readBytes.Length);
        }
Esempio n. 5
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. 6
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. 7
0
        public void ReadTest()
        {
            SftpSession    session = null;                                    // TODO: Initialize to an appropriate value
            string         path    = string.Empty;                            // TODO: Initialize to an appropriate value
            FileMode       mode    = new FileMode();                          // TODO: Initialize to an appropriate value
            SftpFileStream target  = new SftpFileStream(session, path, mode); // TODO: Initialize to an appropriate value

            byte[] buffer   = null;                                           // TODO: Initialize to an appropriate value
            int    offset   = 0;                                              // TODO: Initialize to an appropriate value
            int    count    = 0;                                              // TODO: Initialize to an appropriate value
            int    expected = 0;                                              // TODO: Initialize to an appropriate value
            int    actual;

            actual = target.Read(buffer, offset, count);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }
Esempio n. 8
0
        public void ReadShouldThrowNotSupportedException()
        {
            var buffer = new byte[_readBufferSize];

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

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

            SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(1));
        }
Esempio n. 9
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));
        }
Esempio n. 10
0
        public void DownloadFile(string remotePath, string localPath, AsyncCallback callback)
        {
            SftpFileStream inSt  = null;
            FileStream     outSt = null;

            try
            {
                SftpFile file     = sftp.Get(remotePath);
                long     fileSize = file.Length;

                inSt  = sftp.OpenRead(remotePath);
                outSt = new FileStream(localPath, FileMode.OpenOrCreate, FileAccess.ReadWrite);

                byte[] buf   = new byte[8092];
                int    len   = 0;
                long   count = 0;
                while ((len = inSt.Read(buf, 0, 8092)) > 0)
                {
                    outSt.Write(buf, 0, len);
                    count += len;

                    if (callback != null) // 下载进度回调通知
                    {
                        DownloadAsyncResult result = new DownloadAsyncResult(count, fileSize);
                        callback.Invoke(result);
                    }
                }
            }
            finally
            {
                if (inSt != null)
                {
                    inSt.Close();
                }
                if (outSt != null)
                {
                    outSt.Close();
                }
            }
        }
        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. 12
0
        public NtStatus ReadFile(string fileName, byte[] buffer, out int bytesRead, long offset, DokanFileInfo info)
        {
            Debug.Print("read : {0} offset : {1}", fileName, offset);
            if (info.Context == null)
            {
                using (SftpFileStream stream = sftpClient.Open(ToUnixStylePath(fileName), FileMode.Open, System.IO.FileAccess.Read))
                {
                    stream.Position = offset;
                    bytesRead       = stream.Read(buffer, 0, buffer.Length);
                }
            }
            else
            {
                SftpFileStream stream = info.Context as SftpFileStream;
                lock (stream)
                {
                    stream.Position = offset;
                    bytesRead       = stream.Read(buffer, 0, buffer.Length);
                }
            }

            return(DokanResult.Success);
        }
 protected override void Act()
 {
     _actual = _target.Read(_buffer, 0, _numberOfBytesToRead);
 }
Esempio n. 14
0
 public override int Read(byte[] buffer, int offset, int count)
 {
     return(_fs.Read(buffer, offset, count));
 }