Skip to content

Commit 09fb2b9

Browse files
authored
Merge pull request #385 from Igerly/ssh-with-docker-tests
Integration test(s) with OpenSSH server in Docker
2 parents 661f63e + 4045d5a commit 09fb2b9

File tree

5 files changed

+78
-8
lines changed

5 files changed

+78
-8
lines changed

.travis.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
language: java
22
dist: trusty
3-
sudo: false
3+
sudo: required
44

5+
services:
6+
- docker
7+
58
before_cache:
69
- rm -f $HOME/.gradle/caches/modules-2/modules-2.lock
710

@@ -17,6 +20,8 @@ jdk:
1720

1821
before_install:
1922
- pip install --user codecov
23+
- docker build -t sshj/test-sshd ./src/test/resources/
24+
- docker run -d -p 127.0.0.1:2222:22 sshj/test-sshd
2025

2126
after_success:
2227
- codecov

src/test/java/com/hierynomus/sshj/IntegrationTest.java

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,68 @@
1515
*/
1616
package com.hierynomus.sshj;
1717

18-
import net.schmizz.sshj.DefaultConfig;
19-
import net.schmizz.sshj.SSHClient;
20-
import net.schmizz.sshj.transport.verification.OpenSSHKnownHosts;
21-
import org.junit.Ignore;
22-
import org.junit.Test;
18+
import static org.hamcrest.MatcherAssert.assertThat;
2319

2420
import java.io.File;
2521
import java.io.IOException;
2622

27-
import static org.hamcrest.MatcherAssert.assertThat;
23+
import org.junit.Ignore;
24+
import org.junit.Test;
25+
26+
import net.schmizz.sshj.DefaultConfig;
27+
import net.schmizz.sshj.SSHClient;
28+
import net.schmizz.sshj.transport.TransportException;
29+
import net.schmizz.sshj.transport.verification.OpenSSHKnownHosts;
30+
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;
31+
import net.schmizz.sshj.userauth.UserAuthException;
2832

2933
public class IntegrationTest {
3034

35+
private static final int DOCKER_PORT = 2222;
36+
private static final String USERNAME = "sshj";
37+
private final static String SERVER_IP = System.getProperty("serverIP", "127.0.0.1");
38+
3139
@Test @Ignore // Should only be enabled for testing against VM
32-
public void shouldConnect() throws IOException {
40+
public void shouldConnectVM() throws IOException {
3341
SSHClient sshClient = new SSHClient(new DefaultConfig());
3442
sshClient.addHostKeyVerifier(new OpenSSHKnownHosts(new File("/Users/ajvanerp/.ssh/known_hosts")));
3543
sshClient.connect("172.16.37.147");
3644
sshClient.authPublickey("jeroen");
3745
assertThat("Is connected", sshClient.isAuthenticated());
3846
}
47+
48+
@Test
49+
public void shouldAcceptCorrectKey() throws IOException {
50+
SSHClient sshClient = new SSHClient(new DefaultConfig());
51+
sshClient.addHostKeyVerifier("d3:6a:a9:52:05:ab:b5:48:dd:73:60:18:0c:3a:f0:a3"); // test-containers/ssh_host_ecdsa_key's fingerprint
52+
sshClient.connect(SERVER_IP, DOCKER_PORT);
53+
assertThat("Is connected", sshClient.isConnected());
54+
}
55+
56+
@Test(expected = TransportException.class)
57+
public void shouldDeclineWrongKey() throws IOException {
58+
SSHClient sshClient = new SSHClient(new DefaultConfig());
59+
sshClient.addHostKeyVerifier("d4:6a:a9:52:05:ab:b5:48:dd:73:60:18:0c:3a:f0:a3");
60+
sshClient.connect(SERVER_IP, DOCKER_PORT);
61+
}
62+
63+
@Test
64+
public void shouldConnect() throws IOException {
65+
SSHClient sshClient = getConnectedClient();
66+
sshClient.authPublickey(USERNAME, "src/test/resources/id_rsa");
67+
assertThat("Is authenitcated", sshClient.isAuthenticated());
68+
}
69+
70+
@Test(expected = UserAuthException.class)
71+
public void shouldFailWithWrongKey() throws IOException {
72+
getConnectedClient().authPublickey(USERNAME, "src/test/resources/id_dsa");
73+
}
74+
75+
private SSHClient getConnectedClient() throws IOException {
76+
SSHClient sshClient = new SSHClient(new DefaultConfig());
77+
sshClient.addHostKeyVerifier(new PromiscuousVerifier());
78+
sshClient.connect(SERVER_IP, DOCKER_PORT);
79+
80+
return sshClient;
81+
}
3982
}

src/test/resources/Dockerfile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM sickp/alpine-sshd:7.5
2+
3+
ADD id_rsa.pub /home/sshj/.ssh/authorized_keys
4+
5+
ADD test-container/ssh_host_ecdsa_key /etc/ssh/ssh_host_ecdsa_key
6+
ADD test-container/ssh_host_ecdsa_key.pub /etc/ssh/ssh_host_ecdsa_key.pub
7+
8+
RUN \
9+
echo "root:smile" | chpasswd && \
10+
adduser -D -s /bin/ash sshj && \
11+
passwd -u sshj && \
12+
chmod 600 /home/sshj/.ssh/authorized_keys && \
13+
chmod 600 /etc/ssh/ssh_host_ecdsa_key && \
14+
chmod 644 /etc/ssh/ssh_host_ecdsa_key.pub && \
15+
chown -R sshj:sshj /home/sshj
16+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-----BEGIN EC PRIVATE KEY-----
2+
MHcCAQEEIOpOBFjqe0hjK/hs4WZ3dZqnzanq1L3/JbvV1TCkbe4ToAoGCCqGSM49
3+
AwEHoUQDQgAEVzkrS7Yj0nXML7A3mE08YDthfBR/ZbyYJDIq1vTzcqs6KTaCT529
4+
swNXWLHO+mbHviZcRiI57ULXHZ1emom/Jw==
5+
-----END EC PRIVATE KEY-----
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBFc5K0u2I9J1zC+wN5hNPGA7YXwUf2W8mCQyKtb083KrOik2gk+dvbMDV1ixzvpmx74mXEYiOe1C1x2dXpqJvyc= root@404b27be2bf4

0 commit comments

Comments
 (0)