This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mina-sshd.git


The following commit(s) were added to refs/heads/master by this push:
     new cb04b3f  [SSHD-970] transferTo function of SftpRemotePathChannel will 
loop if count parameter is greater than file size
cb04b3f is described below

commit cb04b3f036db31f6010862a6e71889fcd1fe07e1
Author: Guillaume Nodet <gno...@apache.org>
AuthorDate: Thu Feb 27 10:16:17 2020 +0100

    [SSHD-970] transferTo function of SftpRemotePathChannel will loop if count 
parameter is greater than file size
---
 .../subsystem/sftp/SftpRemotePathChannel.java      |  2 +-
 .../subsystem/sftp/SftpRemotePathChannelTest.java  | 41 ++++++++++++++++++++++
 2 files changed, 42 insertions(+), 1 deletion(-)

diff --git 
a/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpRemotePathChannel.java
 
b/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpRemotePathChannel.java
index 52e3149..003f3ae 100644
--- 
a/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpRemotePathChannel.java
+++ 
b/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpRemotePathChannel.java
@@ -355,7 +355,7 @@ public class SftpRemotePathChannel extends FileChannel {
             try {
                 beginBlocking("transferTo");
 
-                while (totalRead < count) {
+                while (totalRead < count && !eof) {
                     int read = sftp.read(handle, curPos, buffer, 0,
                         (int) Math.min(count - totalRead, buffer.length));
                     if (read > 0) {
diff --git 
a/sshd-sftp/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpRemotePathChannelTest.java
 
b/sshd-sftp/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpRemotePathChannelTest.java
index 2a496a9..9d278f4 100644
--- 
a/sshd-sftp/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpRemotePathChannelTest.java
+++ 
b/sshd-sftp/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpRemotePathChannelTest.java
@@ -138,6 +138,47 @@ public class SftpRemotePathChannelTest extends 
AbstractSftpClientTestSupport {
         assertArrayEquals("Mismatched transferred data", expected, actual);
     }
 
+    @Test(timeout = 10000) // see SSHD-970
+    public void testTransferToFileChannelLoopFile() throws IOException {
+        Path targetPath = detectTargetFolder();
+        Path lclSftp = CommonTestSupportUtils.resolve(
+                targetPath, SftpConstants.SFTP_SUBSYSTEM_NAME, 
getClass().getSimpleName());
+        Path srcFile = 
assertHierarchyTargetFolderExists(lclSftp).resolve(getCurrentTestName() + 
"-src.txt");
+        Path parentPath = targetPath.getParent();
+
+        Files.deleteIfExists(srcFile);
+        try (Writer output = Files.newBufferedWriter(srcFile, 
StandardCharsets.UTF_8)) {
+            String seed = getClass().getName() + "#" + getCurrentTestName() + 
"(" + new Date() + ")";
+            output.append(seed).append(System.lineSeparator());
+        }
+
+        byte[] expected = Files.readAllBytes(srcFile);
+        Path dstFile = srcFile.getParent().resolve(getCurrentTestName() + 
"-dst.txt");
+        Files.deleteIfExists(dstFile);
+
+        String remFilePath = 
CommonTestSupportUtils.resolveRelativeRemotePath(parentPath, srcFile);
+        try (ClientSession session = client.connect(getCurrentTestName(), 
TEST_LOCALHOST, port)
+                .verify(7L, TimeUnit.SECONDS)
+                .getSession()) {
+            session.addPasswordIdentity(getCurrentTestName());
+            session.auth().verify(5L, TimeUnit.SECONDS);
+
+            try (SftpClient sftp = createSftpClient(session);
+                 FileChannel srcChannel = sftp.openRemotePathChannel(
+                         remFilePath, EnumSet.of(StandardOpenOption.READ));
+                 FileChannel dstChannel = FileChannel.open(dstFile,
+                         StandardOpenOption.CREATE, StandardOpenOption.WRITE)) 
{
+                //SftpRemotePathChannel.DEFAULT_TRANSFER_BUFFER_SIZE > 
expected.length => Infinite loop
+                long numXfered = srcChannel.transferTo(0L, 
SftpRemotePathChannel.DEFAULT_TRANSFER_BUFFER_SIZE, dstChannel);
+                assertEquals("Mismatched reported transfer count", 
expected.length, numXfered);
+            }
+        }
+
+        byte[] actual = Files.readAllBytes(dstFile);
+        assertEquals("Mismatched transfered size", expected.length, 
actual.length);
+        assertArrayEquals("Mismatched transferred data", expected, actual);
+    }
+
     @Test   // see SSHD-967
     public void testTransferFromFileChannel() throws IOException {
         Path targetPath = detectTargetFolder();

Reply via email to