From 0f67fa2541b8252d7c25d00d49b9ec8ac0654202 Mon Sep 17 00:00:00 2001 From: Jeroen van Erp Date: Thu, 28 Dec 2017 13:00:36 +0100 Subject: [PATCH] Added integration test for append scenario (Fixes #390) --- .../sshj/IntegrationBaseSpec.groovy | 21 ++++++++ .../hierynomus/sshj/IntegrationSpec.groovy | 17 +----- .../hierynomus/sshj/sftp/FileWriteSpec.groovy | 53 +++++++++++++++++++ 3 files changed, 75 insertions(+), 16 deletions(-) create mode 100644 src/itest/groovy/com/hierynomus/sshj/IntegrationBaseSpec.groovy create mode 100644 src/itest/groovy/com/hierynomus/sshj/sftp/FileWriteSpec.groovy diff --git a/src/itest/groovy/com/hierynomus/sshj/IntegrationBaseSpec.groovy b/src/itest/groovy/com/hierynomus/sshj/IntegrationBaseSpec.groovy new file mode 100644 index 00000000..0e469bc8 --- /dev/null +++ b/src/itest/groovy/com/hierynomus/sshj/IntegrationBaseSpec.groovy @@ -0,0 +1,21 @@ +package com.hierynomus.sshj + +import net.schmizz.sshj.DefaultConfig +import net.schmizz.sshj.SSHClient +import net.schmizz.sshj.transport.verification.PromiscuousVerifier +import spock.lang.Specification + +class IntegrationBaseSpec extends Specification { + protected static final int DOCKER_PORT = 2222; + protected static final String USERNAME = "sshj"; + protected final static String SERVER_IP = System.getProperty("serverIP", "127.0.0.1"); + + protected static SSHClient getConnectedClient() throws IOException { + SSHClient sshClient = new SSHClient(new DefaultConfig()); + sshClient.addHostKeyVerifier(new PromiscuousVerifier()); + sshClient.connect(SERVER_IP, DOCKER_PORT); + + return sshClient; + } + +} diff --git a/src/itest/groovy/com/hierynomus/sshj/IntegrationSpec.groovy b/src/itest/groovy/com/hierynomus/sshj/IntegrationSpec.groovy index 6675584f..16d3e004 100644 --- a/src/itest/groovy/com/hierynomus/sshj/IntegrationSpec.groovy +++ b/src/itest/groovy/com/hierynomus/sshj/IntegrationSpec.groovy @@ -18,14 +18,9 @@ package com.hierynomus.sshj import net.schmizz.sshj.DefaultConfig import net.schmizz.sshj.SSHClient import net.schmizz.sshj.transport.TransportException -import net.schmizz.sshj.transport.verification.PromiscuousVerifier import net.schmizz.sshj.userauth.UserAuthException -import spock.lang.Specification -class IntegrationSpec extends Specification { - private static final int DOCKER_PORT = 2222; - private static final String USERNAME = "sshj"; - private final static String SERVER_IP = System.getProperty("serverIP", "127.0.0.1"); +class IntegrationSpec extends IntegrationBaseSpec { def "should accept correct key"() { given: @@ -73,14 +68,4 @@ class IntegrationSpec extends Specification { thrown(UserAuthException.class) !client.isAuthenticated() } - - private static SSHClient getConnectedClient() throws IOException { - SSHClient sshClient = new SSHClient(new DefaultConfig()); - sshClient.addHostKeyVerifier(new PromiscuousVerifier()); - sshClient.connect(SERVER_IP, DOCKER_PORT); - - return sshClient; - } - - } diff --git a/src/itest/groovy/com/hierynomus/sshj/sftp/FileWriteSpec.groovy b/src/itest/groovy/com/hierynomus/sshj/sftp/FileWriteSpec.groovy new file mode 100644 index 00000000..f6ea07ac --- /dev/null +++ b/src/itest/groovy/com/hierynomus/sshj/sftp/FileWriteSpec.groovy @@ -0,0 +1,53 @@ +package com.hierynomus.sshj.sftp + +import com.hierynomus.sshj.IntegrationBaseSpec +import net.schmizz.sshj.SSHClient +import net.schmizz.sshj.sftp.OpenMode +import net.schmizz.sshj.sftp.RemoteFile +import net.schmizz.sshj.sftp.SFTPClient + +import java.nio.charset.StandardCharsets + +import static org.codehaus.groovy.runtime.IOGroovyMethods.withCloseable + +class FileWriteSpec extends IntegrationBaseSpec { + + def "should append to file (GH issue #390)"() { + given: + SSHClient client = getConnectedClient() + client.authPublickey("sshj", "src/test/resources/id_rsa") + SFTPClient sftp = client.newSFTPClient() + def file = "/home/sshj/test.txt" + def initialText = "This is the initial text.\n".getBytes(StandardCharsets.UTF_16) + def appendText = "And here's the appended text.\n".getBytes(StandardCharsets.UTF_16) + + when: + withCloseable(sftp.open(file, EnumSet.of(OpenMode.WRITE, OpenMode.CREAT))) { RemoteFile initial -> + initial.write(0, initialText, 0, initialText.length) + } + + then: + withCloseable(sftp.open(file, EnumSet.of(OpenMode.READ))) { RemoteFile read -> + def bytes = new byte[initialText.length] + read.read(0, bytes, 0, bytes.length) + bytes == initialText + } + + when: + withCloseable(sftp.open(file, EnumSet.of(OpenMode.WRITE, OpenMode.APPEND))) { RemoteFile append -> + append.write(0, appendText, 0, appendText.length) + } + + then: + withCloseable(sftp.open(file, EnumSet.of(OpenMode.READ))) { RemoteFile read -> + def bytes = new byte[initialText.length + appendText.length] + read.read(0, bytes, 0, bytes.length) + Arrays.copyOfRange(bytes, 0, initialText.length) == initialText + Arrays.copyOfRange(bytes, initialText.length, initialText.length + appendText.length) == appendText + } + + cleanup: + sftp.close() + client.close() + } +}