|
|
@@ -12,7 +12,7 @@ namespace Renci.SshNet.Tests.Classes.Sftp
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// - In read mode
|
|
|
- /// - Bytes in (read) buffer
|
|
|
+ /// - Bytes read from (read) buffer
|
|
|
/// - New length greater than client position and greater than server position
|
|
|
/// </summary>
|
|
|
[TestClass]
|
|
|
@@ -30,7 +30,8 @@ namespace Renci.SshNet.Tests.Classes.Sftp
|
|
|
private SftpFileAttributes _fileAttributes;
|
|
|
private SftpFileAttributes _originalFileAttributes;
|
|
|
private SftpFileAttributes _newFileAttributes;
|
|
|
- private byte[] _readBytes;
|
|
|
+ private byte[] _readBytes1;
|
|
|
+ private byte[] _readBytes2;
|
|
|
private byte[] _actualReadBytes;
|
|
|
|
|
|
protected override void SetupData()
|
|
|
@@ -42,16 +43,17 @@ namespace Renci.SshNet.Tests.Classes.Sftp
|
|
|
_bufferSize = (uint) random.Next(1, 1000);
|
|
|
_readBufferSize = (uint) random.Next(1, 1000);
|
|
|
_writeBufferSize = (uint) random.Next(100, 1000);
|
|
|
- _readBytes = new byte[5];
|
|
|
- _actualReadBytes = GenerateRandom(_readBytes.Length, random);
|
|
|
- _length = _readBytes.Length + 2;
|
|
|
+ _readBytes1 = new byte[5];
|
|
|
+ _readBytes2 = new byte[random.Next(1, 3)];
|
|
|
+ _actualReadBytes = GenerateRandom(_readBytes1.Length + _readBytes2.Length + 2, random); // server returns more bytes than the caller requested
|
|
|
+ _length = _readBytes1.Length + _readBytes2.Length + 5;
|
|
|
|
|
|
_fileAttributes = new SftpFileAttributesBuilder().WithExtension("X", "ABC")
|
|
|
.WithExtension("V", "VValue")
|
|
|
.WithGroupId(random.Next())
|
|
|
.WithLastAccessTime(DateTime.Now.AddSeconds(random.Next()))
|
|
|
.WithLastWriteTime(DateTime.Now.AddSeconds(random.Next()))
|
|
|
- .WithPermissions((uint)random.Next())
|
|
|
+ .WithPermissions((uint) random.Next())
|
|
|
.WithSize(_length + 100)
|
|
|
.WithUserId(random.Next())
|
|
|
.Build();
|
|
|
@@ -80,6 +82,9 @@ namespace Renci.SshNet.Tests.Classes.Sftp
|
|
|
SftpSessionMock.InSequence(_sequence)
|
|
|
.Setup(p => p.IsOpen)
|
|
|
.Returns(true);
|
|
|
+ SftpSessionMock.InSequence(_sequence)
|
|
|
+ .Setup(p => p.IsOpen)
|
|
|
+ .Returns(true);
|
|
|
SftpSessionMock.InSequence(_sequence)
|
|
|
.Setup(p => p.RequestFStat(_handle, false))
|
|
|
.Returns(_fileAttributes);
|
|
|
@@ -93,7 +98,8 @@ namespace Renci.SshNet.Tests.Classes.Sftp
|
|
|
base.Arrange();
|
|
|
|
|
|
_sftpFileStream = new SftpFileStream(SftpSessionMock.Object, _path, FileMode.Open, FileAccess.ReadWrite, (int)_bufferSize);
|
|
|
- _sftpFileStream.Read(_readBytes, 0, _readBytes.Length);
|
|
|
+ _sftpFileStream.Read(_readBytes1, 0, _readBytes1.Length);
|
|
|
+ _sftpFileStream.Read(_readBytes2, 0, _readBytes2.Length); // this will return bytes from the buffer
|
|
|
}
|
|
|
|
|
|
protected override void Act()
|
|
|
@@ -106,9 +112,9 @@ namespace Renci.SshNet.Tests.Classes.Sftp
|
|
|
{
|
|
|
SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true);
|
|
|
|
|
|
- Assert.AreEqual(_readBytes.Length, _sftpFileStream.Position);
|
|
|
+ Assert.AreEqual(_readBytes1.Length + _readBytes2.Length, _sftpFileStream.Position);
|
|
|
|
|
|
- SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3));
|
|
|
+ SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4));
|
|
|
}
|
|
|
|
|
|
[TestMethod]
|
|
|
@@ -135,35 +141,38 @@ namespace Renci.SshNet.Tests.Classes.Sftp
|
|
|
{
|
|
|
SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true);
|
|
|
SftpSessionMock.InSequence(_sequence)
|
|
|
- .Setup(p => p.RequestRead(_handle, (uint) _readBytes.Length, _readBufferSize))
|
|
|
+ .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) _readBytes.Length, _readBufferSize), Times.Once);
|
|
|
- SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(3));
|
|
|
+ SftpSessionMock.Verify(p => p.RequestRead(_handle, (uint)(_readBytes1.Length + _readBytes2.Length), _readBufferSize), Times.Once);
|
|
|
+ SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4));
|
|
|
}
|
|
|
|
|
|
[TestMethod]
|
|
|
public void WriteShouldStartFromSamePositionAsBeforeSetLength()
|
|
|
{
|
|
|
- var bytesToWrite = GenerateRandom(5);
|
|
|
+ var bytesToWrite = GenerateRandom(_writeBufferSize);
|
|
|
+ byte[] bytesWritten = null;
|
|
|
|
|
|
- SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true);
|
|
|
SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true);
|
|
|
SftpSessionMock.InSequence(_sequence)
|
|
|
- .Setup(p => p.RequestWrite(_handle, (uint) _readBytes.Length, It.IsAny<byte[]>(), 0, bytesToWrite.Length, It.IsAny<AutoResetEvent>(), null))
|
|
|
+ .Setup(p => p.RequestWrite(_handle, (uint) (_readBytes1.Length + _readBytes2.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);
|
|
|
- _sftpFileStream.Flush();
|
|
|
|
|
|
- SftpSessionMock.Verify(p => p.RequestWrite(_handle, (uint) _readBytes.Length, It.IsAny<byte[]>(), 0, bytesToWrite.Length, It.IsAny<AutoResetEvent>(), null), Times.Once);
|
|
|
+ Assert.IsNotNull(bytesWritten);
|
|
|
+ CollectionAssert.AreEqual(bytesToWrite, bytesWritten);
|
|
|
+
|
|
|
+ SftpSessionMock.Verify(p => p.RequestWrite(_handle, (uint)(_readBytes1.Length + _readBytes2.Length), It.IsAny<byte[]>(), 0, bytesToWrite.Length, It.IsAny<AutoResetEvent>(), null), Times.Once);
|
|
|
SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4));
|
|
|
}
|
|
|
}
|