forked from testcontainers/testcontainers-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSftpContainerTest.java
More file actions
105 lines (99 loc) · 4.78 KB
/
SftpContainerTest.java
File metadata and controls
105 lines (99 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package org.example;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.HostKey;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.MountableFile;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
class SftpContainerTest {
@Test
void test() throws Exception {
try (
GenericContainer<?> sftp = new GenericContainer<>("atmoz/sftp:alpine-3.7")
.withCopyFileToContainer(
MountableFile.forClasspathResource("testcontainers/", 0777),
"/home/foo/upload/testcontainers"
)
.withExposedPorts(22)
.withCommand("foo:pass:::upload")
) {
sftp.start();
JSch jsch = new JSch();
Session jschSession = jsch.getSession("foo", sftp.getHost(), sftp.getMappedPort(22));
jschSession.setPassword("pass");
jschSession.setConfig("StrictHostKeyChecking", "no");
jschSession.connect();
ChannelSftp channel = (ChannelSftp) jschSession.openChannel("sftp");
channel.connect();
assertThat(channel.ls("/upload/testcontainers")).anyMatch(item -> item.toString().contains("file.txt"));
assertThat(
new BufferedReader(
new InputStreamReader(channel.get("/upload/testcontainers/file.txt"), StandardCharsets.UTF_8)
)
.lines()
.collect(Collectors.joining("\n"))
)
.contains("Testcontainers");
channel.rm("/upload/testcontainers/file.txt");
assertThat(channel.ls("/upload/testcontainers/"))
.noneMatch(item -> item.toString().contains("testcontainers/file.txt"));
}
}
@Test
void testHostKeyCheck() throws Exception {
try (
GenericContainer<?> sftp = new GenericContainer<>("atmoz/sftp:alpine-3.7")
.withCopyFileToContainer(
MountableFile.forClasspathResource("testcontainers/", 0777),
"/home/foo/upload/testcontainers"
)
.withCopyFileToContainer(
MountableFile.forClasspathResource("./ssh_host_rsa_key", 0400),
"/etc/ssh/ssh_host_rsa_key"
)
.withExposedPorts(22)
.withCommand("foo:pass:::upload")
) {
sftp.start();
JSch jsch = new JSch();
Session jschSession = jsch.getSession("foo", sftp.getHost(), sftp.getMappedPort(22));
jschSession.setPassword("pass");
// hostKeyString is string starting with AAAA from file known_hosts or ssh_host_*_key.pub
// generate the files with:
// ssh-keygen -t rsa -b 3072 -f ssh_host_rsa_key < /dev/null
String hostKeyString =
"AAAAB3NzaC1yc2EAAAADAQABAAABgQCXMxVRzmFWxfrRB9XiZ/3HNM+xkYYE+IMGuOZD" +
"04M2ezU25XjT6cPajzpFmzTxR2qEpRCKHeVnSG5nT6UXQp7760brTN7m5sDasbMnHgYh" +
"fC/3of2k6qTR9X/JHRpgwzq5+6FtEe41w1H1dXoNIr4YTKnLijSp8MKqBtPPNUpzEVb9" +
"5YKZGdCDoCbbYOyS/Dc8azUDo0mqM542J3nA2Sq9HCP0BAv43hrTAtCZodkB5wo18exb" +
"fPKsjGtA3de2npybFoSRbavZmT8L/b2iHZX6FRaqLsbYGKtszCWu5OU7WBX5g5QVlLfO" +
"nGQ+LsF6d6pX5LlMwEU14uu4gNPvZFOaZXtHNHZqnBcjd/sMaw5N/atFsPgtQ0vYnrEA" +
"D6oDjj0uXMsnmgUWTZBi3q2GBWWPqhE+0ASb2xBQGa+tWWTVYbuuYlA7hUX0URK8FcLw" +
"4UOYJjscDjnjlvQkghd2esP5NxV1NXkG2XYNHnf1E/tH4+AHJzy+qOQom7ehda96FZ8=";
HostKey hostKey = new HostKey(sftp.getHost(), Base64.getDecoder().decode(hostKeyString));
jschSession.getHostKeyRepository().add(hostKey, null);
jschSession.connect();
ChannelSftp channel = (ChannelSftp) jschSession.openChannel("sftp");
channel.connect();
assertThat(channel.ls("/upload/testcontainers")).anyMatch(item -> item.toString().contains("file.txt"));
assertThat(
new BufferedReader(
new InputStreamReader(channel.get("/upload/testcontainers/file.txt"), StandardCharsets.UTF_8)
)
.lines()
.collect(Collectors.joining("\n"))
)
.contains("Testcontainers");
channel.rm("/upload/testcontainers/file.txt");
assertThat(channel.ls("/upload/testcontainers/"))
.noneMatch(item -> item.toString().contains("testcontainers/file.txt"));
}
}
}