Przeglądaj źródła

Ensure read buffer is large enough.

Gert Driesen 8 lat temu
rodzic
commit
a353f95ebf

+ 37 - 14
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_SetLength_DataInReadBuffer_NewLengthGreatherThanPosition.cs

@@ -43,11 +43,11 @@ namespace Renci.SshNet.Tests.Classes.Sftp
             _path = random.Next().ToString(CultureInfo.InvariantCulture);
             _handle = GenerateRandom(random.Next(2, 6), random);
             _bufferSize = (uint) random.Next(1, 1000);
-            _readBufferSize = (uint) random.Next(1, 1000);
-            _writeBufferSize = (uint) random.Next(100, 1000);
             _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
+            _readBufferSize = (uint) random.Next(_actualReadBytes.Length, _actualReadBytes.Length * 2);
+            _writeBufferSize = (uint) random.Next(100, 1000);
             _length = _readBytes1.Length + _readBytes2.Length + 5;
 
             _fileAttributes = new SftpFileAttributesBuilder().WithExtension("X", "ABC")
@@ -99,7 +99,11 @@ namespace Renci.SshNet.Tests.Classes.Sftp
         {
             base.Arrange();
 
-            _sftpFileStream = new SftpFileStream(SftpSessionMock.Object, _path, FileMode.Open, FileAccess.ReadWrite, (int)_bufferSize);
+            _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
         }
@@ -143,14 +147,19 @@ namespace Renci.SshNet.Tests.Classes.Sftp
         {
             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 });
+                           .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.RequestRead(_handle,
+                                                      (uint) (_readBytes1.Length + _readBytes2.Length),
+                                                      _readBufferSize),
+                                   Times.Once);
             SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4));
         }
 
@@ -162,20 +171,34 @@ namespace Renci.SshNet.Tests.Classes.Sftp
 
             SftpSessionMock.InSequence(_sequence).Setup(p => p.IsOpen).Returns(true);
             SftpSessionMock.InSequence(_sequence)
-                           .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();
-                           });
+                           .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);
 
             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.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));
         }
     }
-}
+}