Skip to content

Commit a353f95

Browse files
committed
Ensure read buffer is large enough.
1 parent c3d43b6 commit a353f95

File tree

1 file changed

+37
-14
lines changed

1 file changed

+37
-14
lines changed

src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_DataInReadBuffer_NewLengthGreatherThanPosition.cs

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ protected override void SetupData()
4343
_path = random.Next().ToString(CultureInfo.InvariantCulture);
4444
_handle = GenerateRandom(random.Next(2, 6), random);
4545
_bufferSize = (uint) random.Next(1, 1000);
46-
_readBufferSize = (uint) random.Next(1, 1000);
47-
_writeBufferSize = (uint) random.Next(100, 1000);
4846
_readBytes1 = new byte[5];
4947
_readBytes2 = new byte[random.Next(1, 3)];
5048
_actualReadBytes = GenerateRandom(_readBytes1.Length + _readBytes2.Length + 2, random); // server returns more bytes than the caller requested
49+
_readBufferSize = (uint) random.Next(_actualReadBytes.Length, _actualReadBytes.Length * 2);
50+
_writeBufferSize = (uint) random.Next(100, 1000);
5151
_length = _readBytes1.Length + _readBytes2.Length + 5;
5252

5353
_fileAttributes = new SftpFileAttributesBuilder().WithExtension("X", "ABC")
@@ -99,7 +99,11 @@ protected override void Arrange()
9999
{
100100
base.Arrange();
101101

102-
_sftpFileStream = new SftpFileStream(SftpSessionMock.Object, _path, FileMode.Open, FileAccess.ReadWrite, (int)_bufferSize);
102+
_sftpFileStream = new SftpFileStream(SftpSessionMock.Object,
103+
_path,
104+
FileMode.Open,
105+
FileAccess.ReadWrite,
106+
(int) _bufferSize);
103107
_sftpFileStream.Read(_readBytes1, 0, _readBytes1.Length);
104108
_sftpFileStream.Read(_readBytes2, 0, _readBytes2.Length); // this will return bytes from the buffer
105109
}
@@ -143,14 +147,19 @@ public void ReadShouldReadStartFromSamePositionAsBeforeSetLength()
143147
{
144148
SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true);
145149
SftpSessionMock.InSequence(_sequence)
146-
.Setup(p => p.RequestRead(_handle, (uint) (_readBytes1.Length + _readBytes2.Length), _readBufferSize))
147-
.Returns(new byte[] { 0x0f });
150+
.Setup(p => p.RequestRead(_handle,
151+
(uint) (_readBytes1.Length + _readBytes2.Length),
152+
_readBufferSize))
153+
.Returns(new byte[] {0x0f});
148154

149155
var byteRead = _sftpFileStream.ReadByte();
150156

151157
Assert.AreEqual(0x0f, byteRead);
152158

153-
SftpSessionMock.Verify(p => p.RequestRead(_handle, (uint)(_readBytes1.Length + _readBytes2.Length), _readBufferSize), Times.Once);
159+
SftpSessionMock.Verify(p => p.RequestRead(_handle,
160+
(uint) (_readBytes1.Length + _readBytes2.Length),
161+
_readBufferSize),
162+
Times.Once);
154163
SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4));
155164
}
156165

@@ -162,20 +171,34 @@ public void WriteShouldStartFromSamePositionAsBeforeSetLength()
162171

163172
SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true);
164173
SftpSessionMock.InSequence(_sequence)
165-
.Setup(p => p.RequestWrite(_handle, (uint) (_readBytes1.Length + _readBytes2.Length), It.IsAny<byte[]>(), 0, bytesToWrite.Length, It.IsAny<AutoResetEvent>(), null))
166-
.Callback<byte[], ulong, byte[], int, int, AutoResetEvent, Action<SftpStatusResponse>>((handle, serverOffset, data, offset, length, wait, writeCompleted) =>
167-
{
168-
bytesWritten = data.Take(offset, length);
169-
wait.Set();
170-
});
174+
.Setup(p => p.RequestWrite(_handle,
175+
(uint) (_readBytes1.Length + _readBytes2.Length),
176+
It.IsAny<byte[]>(),
177+
0,
178+
bytesToWrite.Length,
179+
It.IsAny<AutoResetEvent>(),
180+
null))
181+
.Callback<byte[], ulong, byte[], int, int, AutoResetEvent, Action<SftpStatusResponse>>(
182+
(handle, serverOffset, data, offset, length, wait, writeCompleted) =>
183+
{
184+
bytesWritten = data.Take(offset, length);
185+
wait.Set();
186+
});
171187

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

174190
Assert.IsNotNull(bytesWritten);
175191
CollectionAssert.AreEqual(bytesToWrite, bytesWritten);
176192

177-
SftpSessionMock.Verify(p => p.RequestWrite(_handle, (uint)(_readBytes1.Length + _readBytes2.Length), It.IsAny<byte[]>(), 0, bytesToWrite.Length, It.IsAny<AutoResetEvent>(), null), Times.Once);
193+
SftpSessionMock.Verify(p => p.RequestWrite(_handle,
194+
(uint) (_readBytes1.Length + _readBytes2.Length),
195+
It.IsAny<byte[]>(),
196+
0,
197+
bytesToWrite.Length,
198+
It.IsAny<AutoResetEvent>(),
199+
null),
200+
Times.Once);
178201
SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4));
179202
}
180203
}
181-
}
204+
}

0 commit comments

Comments
 (0)