mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-06 07:10:53 +03:00
Added integration test for append scenario (Fixes #390)
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user