Skip to content

Commit 0f67fa2

Browse files
committed
Added integration test for append scenario (Fixes #390)
1 parent 54018a4 commit 0f67fa2

File tree

3 files changed

+75
-16
lines changed

3 files changed

+75
-16
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.hierynomus.sshj
2+
3+
import net.schmizz.sshj.DefaultConfig
4+
import net.schmizz.sshj.SSHClient
5+
import net.schmizz.sshj.transport.verification.PromiscuousVerifier
6+
import spock.lang.Specification
7+
8+
class IntegrationBaseSpec extends Specification {
9+
protected static final int DOCKER_PORT = 2222;
10+
protected static final String USERNAME = "sshj";
11+
protected final static String SERVER_IP = System.getProperty("serverIP", "127.0.0.1");
12+
13+
protected static SSHClient getConnectedClient() throws IOException {
14+
SSHClient sshClient = new SSHClient(new DefaultConfig());
15+
sshClient.addHostKeyVerifier(new PromiscuousVerifier());
16+
sshClient.connect(SERVER_IP, DOCKER_PORT);
17+
18+
return sshClient;
19+
}
20+
21+
}

src/itest/groovy/com/hierynomus/sshj/IntegrationSpec.groovy

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,9 @@ package com.hierynomus.sshj
1818
import net.schmizz.sshj.DefaultConfig
1919
import net.schmizz.sshj.SSHClient
2020
import net.schmizz.sshj.transport.TransportException
21-
import net.schmizz.sshj.transport.verification.PromiscuousVerifier
2221
import net.schmizz.sshj.userauth.UserAuthException
23-
import spock.lang.Specification
2422

25-
class IntegrationSpec extends Specification {
26-
private static final int DOCKER_PORT = 2222;
27-
private static final String USERNAME = "sshj";
28-
private final static String SERVER_IP = System.getProperty("serverIP", "127.0.0.1");
23+
class IntegrationSpec extends IntegrationBaseSpec {
2924

3025
def "should accept correct key"() {
3126
given:
@@ -73,14 +68,4 @@ class IntegrationSpec extends Specification {
7368
thrown(UserAuthException.class)
7469
!client.isAuthenticated()
7570
}
76-
77-
private static SSHClient getConnectedClient() throws IOException {
78-
SSHClient sshClient = new SSHClient(new DefaultConfig());
79-
sshClient.addHostKeyVerifier(new PromiscuousVerifier());
80-
sshClient.connect(SERVER_IP, DOCKER_PORT);
81-
82-
return sshClient;
83-
}
84-
85-
8671
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.hierynomus.sshj.sftp
2+
3+
import com.hierynomus.sshj.IntegrationBaseSpec
4+
import net.schmizz.sshj.SSHClient
5+
import net.schmizz.sshj.sftp.OpenMode
6+
import net.schmizz.sshj.sftp.RemoteFile
7+
import net.schmizz.sshj.sftp.SFTPClient
8+
9+
import java.nio.charset.StandardCharsets
10+
11+
import static org.codehaus.groovy.runtime.IOGroovyMethods.withCloseable
12+
13+
class FileWriteSpec extends IntegrationBaseSpec {
14+
15+
def "should append to file (GH issue #390)"() {
16+
given:
17+
SSHClient client = getConnectedClient()
18+
client.authPublickey("sshj", "src/test/resources/id_rsa")
19+
SFTPClient sftp = client.newSFTPClient()
20+
def file = "/home/sshj/test.txt"
21+
def initialText = "This is the initial text.\n".getBytes(StandardCharsets.UTF_16)
22+
def appendText = "And here's the appended text.\n".getBytes(StandardCharsets.UTF_16)
23+
24+
when:
25+
withCloseable(sftp.open(file, EnumSet.of(OpenMode.WRITE, OpenMode.CREAT))) { RemoteFile initial ->
26+
initial.write(0, initialText, 0, initialText.length)
27+
}
28+
29+
then:
30+
withCloseable(sftp.open(file, EnumSet.of(OpenMode.READ))) { RemoteFile read ->
31+
def bytes = new byte[initialText.length]
32+
read.read(0, bytes, 0, bytes.length)
33+
bytes == initialText
34+
}
35+
36+
when:
37+
withCloseable(sftp.open(file, EnumSet.of(OpenMode.WRITE, OpenMode.APPEND))) { RemoteFile append ->
38+
append.write(0, appendText, 0, appendText.length)
39+
}
40+
41+
then:
42+
withCloseable(sftp.open(file, EnumSet.of(OpenMode.READ))) { RemoteFile read ->
43+
def bytes = new byte[initialText.length + appendText.length]
44+
read.read(0, bytes, 0, bytes.length)
45+
Arrays.copyOfRange(bytes, 0, initialText.length) == initialText
46+
Arrays.copyOfRange(bytes, initialText.length, initialText.length + appendText.length) == appendText
47+
}
48+
49+
cleanup:
50+
sftp.close()
51+
client.close()
52+
}
53+
}

0 commit comments

Comments
 (0)