|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 4 | +using Moq; |
| 5 | +using Renci.SshNet.Common; |
| 6 | +using Renci.SshNet.Sftp; |
| 7 | + |
| 8 | +namespace Renci.SshNet.Tests.Classes.Sftp |
| 9 | +{ |
| 10 | + [TestClass] |
| 11 | + public class SftpFileStreamTest_Read_ReadMode_NoDataInReaderBufferAndReadLessBytesFromServerThanCount : SftpFileStreamTestBase |
| 12 | + { |
| 13 | + private string _path; |
| 14 | + private SftpFileStream _target; |
| 15 | + private byte[] _handle; |
| 16 | + private uint _bufferSize; |
| 17 | + private uint _readBufferSize; |
| 18 | + private uint _writeBufferSize; |
| 19 | + private int _actual; |
| 20 | + private byte[] _buffer; |
| 21 | + private byte[] _serverData1; |
| 22 | + private byte[] _serverData2; |
| 23 | + private int _serverData1Length; |
| 24 | + private int _serverData2Length; |
| 25 | + private int _numberOfBytesToRead; |
| 26 | + |
| 27 | + protected override void SetupData() |
| 28 | + { |
| 29 | + base.SetupData(); |
| 30 | + |
| 31 | + var random = new Random(); |
| 32 | + _path = random.Next().ToString(); |
| 33 | + _handle = GenerateRandom(5, random); |
| 34 | + _bufferSize = (uint)random.Next(1, 1000); |
| 35 | + _readBufferSize = 20; |
| 36 | + _writeBufferSize = 500; |
| 37 | + |
| 38 | + _numberOfBytesToRead = 20; |
| 39 | + _buffer = new byte[_numberOfBytesToRead]; |
| 40 | + _serverData1Length = _numberOfBytesToRead - 5; |
| 41 | + _serverData1 = GenerateRandom(_serverData1Length, random); |
| 42 | + _serverData2Length = _numberOfBytesToRead - _serverData1Length + 3; |
| 43 | + _serverData2 = GenerateRandom(_serverData2Length, random); |
| 44 | + } |
| 45 | + |
| 46 | + protected override void SetupMocks() |
| 47 | + { |
| 48 | + SftpSessionMock.InSequence(MockSequence) |
| 49 | + .Setup(p => p.RequestOpen(_path, Flags.Read, false)) |
| 50 | + .Returns(_handle); |
| 51 | + SftpSessionMock.InSequence(MockSequence) |
| 52 | + .Setup(p => p.CalculateOptimalReadLength(_bufferSize)) |
| 53 | + .Returns(_readBufferSize); |
| 54 | + SftpSessionMock.InSequence(MockSequence) |
| 55 | + .Setup(p => p.CalculateOptimalWriteLength(_bufferSize, _handle)) |
| 56 | + .Returns(_writeBufferSize); |
| 57 | + SftpSessionMock.InSequence(MockSequence) |
| 58 | + .Setup(p => p.IsOpen) |
| 59 | + .Returns(true); |
| 60 | + SftpSessionMock.InSequence(MockSequence) |
| 61 | + .Setup(p => p.RequestRead(_handle, 0UL, _readBufferSize)) |
| 62 | + .Returns(_serverData1); |
| 63 | + SftpSessionMock.InSequence(MockSequence) |
| 64 | + .Setup(p => p.RequestRead(_handle, (ulong) _serverData1.Length, _readBufferSize)) |
| 65 | + .Returns(_serverData2); |
| 66 | + } |
| 67 | + |
| 68 | + [TestCleanup] |
| 69 | + public void TearDown() |
| 70 | + { |
| 71 | + SftpSessionMock.InSequence(MockSequence) |
| 72 | + .Setup(p => p.RequestClose(_handle)); |
| 73 | + } |
| 74 | + |
| 75 | + protected override void Arrange() |
| 76 | + { |
| 77 | + base.Arrange(); |
| 78 | + |
| 79 | + _target = new SftpFileStream(SftpSessionMock.Object, |
| 80 | + _path, |
| 81 | + FileMode.Open, |
| 82 | + FileAccess.Read, |
| 83 | + (int)_bufferSize); |
| 84 | + } |
| 85 | + |
| 86 | + protected override void Act() |
| 87 | + { |
| 88 | + _actual = _target.Read(_buffer, 0, _numberOfBytesToRead); |
| 89 | + } |
| 90 | + |
| 91 | + [TestMethod] |
| 92 | + public void ReadShouldHaveReturnedTheNumberOfBytesRequested() |
| 93 | + { |
| 94 | + Assert.AreEqual(_buffer.Length, _actual); |
| 95 | + } |
| 96 | + |
| 97 | + [TestMethod] |
| 98 | + public void ReadShouldHaveWrittenBytesToTheCallerSuppliedBuffer() |
| 99 | + { |
| 100 | + Assert.IsTrue(_serverData1.IsEqualTo(_buffer.Take(_serverData1Length))); |
| 101 | + |
| 102 | + var bytesWrittenFromSecondRead = _numberOfBytesToRead - _serverData1Length; |
| 103 | + Assert.IsTrue(_serverData2.Take(bytesWrittenFromSecondRead).IsEqualTo(_buffer.Take(_serverData1Length, bytesWrittenFromSecondRead))); |
| 104 | + } |
| 105 | + |
| 106 | + [TestMethod] |
| 107 | + public void PositionShouldReturnNumberOfBytesWrittenToBuffer() |
| 108 | + { |
| 109 | + SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); |
| 110 | + |
| 111 | + Assert.AreEqual(_buffer.Length, _target.Position); |
| 112 | + |
| 113 | + SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(2)); |
| 114 | + } |
| 115 | + |
| 116 | + [TestMethod] |
| 117 | + public void ReadShouldReturnAllRemaningBytesFromReadBufferWhenCountIsEqualToNumberOfRemainingBytes() |
| 118 | + { |
| 119 | + SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); |
| 120 | + |
| 121 | + var numberOfBytesRemainingInReadBuffer = _serverData1Length + _serverData2Length - _numberOfBytesToRead; |
| 122 | + |
| 123 | + _buffer = new byte[numberOfBytesRemainingInReadBuffer]; |
| 124 | + |
| 125 | + var actual = _target.Read(_buffer, 0, _buffer.Length); |
| 126 | + |
| 127 | + Assert.AreEqual(_buffer.Length, actual); |
| 128 | + Assert.IsTrue(_serverData2.Take(_numberOfBytesToRead - _serverData1Length, _buffer.Length).IsEqualTo(_buffer)); |
| 129 | + |
| 130 | + SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(2)); |
| 131 | + } |
| 132 | + |
| 133 | + [TestMethod] |
| 134 | + public void ReadShouldReturnAllRemaningBytesFromReadBufferAndReadAgainWhenCountIsGreaterThanNumberOfRemainingBytesAndNewReadReturnsZeroBytes() |
| 135 | + { |
| 136 | + SftpSessionMock.InSequence(MockSequence).Setup(p => p.IsOpen).Returns(true); |
| 137 | + SftpSessionMock.InSequence(MockSequence).Setup(p => p.RequestRead(_handle, (ulong) (_serverData1Length + _serverData2Length), _readBufferSize)).Returns(Array<byte>.Empty); |
| 138 | + |
| 139 | + var numberOfBytesRemainingInReadBuffer = _serverData1Length + _serverData2Length - _numberOfBytesToRead; |
| 140 | + |
| 141 | + _buffer = new byte[numberOfBytesRemainingInReadBuffer + 1]; |
| 142 | + |
| 143 | + var actual = _target.Read(_buffer, 0, _buffer.Length); |
| 144 | + |
| 145 | + Assert.AreEqual(numberOfBytesRemainingInReadBuffer, actual); |
| 146 | + Assert.IsTrue(_serverData2.Take(_numberOfBytesToRead - _serverData1Length, numberOfBytesRemainingInReadBuffer).IsEqualTo(_buffer.Take(numberOfBytesRemainingInReadBuffer))); |
| 147 | + Assert.AreEqual(0, _buffer[numberOfBytesRemainingInReadBuffer]); |
| 148 | + |
| 149 | + SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(2)); |
| 150 | + SftpSessionMock.Verify(p => p.RequestRead(_handle, (ulong) (_serverData1Length + _serverData2Length), _readBufferSize)); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments