Merge pull request #385 from Igerly/ssh-with-docker-tests

Integration test(s) with OpenSSH server in Docker
This commit is contained in:
Jeroen van Erp
2017-12-04 00:23:44 +01:00
committed by GitHub
5 changed files with 78 additions and 8 deletions

View File

@@ -1,7 +1,10 @@
language: java
dist: trusty
sudo: false
sudo: required
services:
- docker
before_cache:
- rm -f $HOME/.gradle/caches/modules-2/modules-2.lock
@@ -17,6 +20,8 @@ jdk:
before_install:
- pip install --user codecov
- docker build -t sshj/test-sshd ./src/test/resources/
- docker run -d -p 127.0.0.1:2222:22 sshj/test-sshd
after_success:
- codecov

View File

@@ -15,25 +15,68 @@
*/
package com.hierynomus.sshj;
import net.schmizz.sshj.DefaultConfig;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.transport.verification.OpenSSHKnownHosts;
import org.junit.Ignore;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import java.io.File;
import java.io.IOException;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.Ignore;
import org.junit.Test;
import net.schmizz.sshj.DefaultConfig;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.transport.TransportException;
import net.schmizz.sshj.transport.verification.OpenSSHKnownHosts;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;
import net.schmizz.sshj.userauth.UserAuthException;
public class IntegrationTest {
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");
@Test @Ignore // Should only be enabled for testing against VM
public void shouldConnect() throws IOException {
public void shouldConnectVM() throws IOException {
SSHClient sshClient = new SSHClient(new DefaultConfig());
sshClient.addHostKeyVerifier(new OpenSSHKnownHosts(new File("/Users/ajvanerp/.ssh/known_hosts")));
sshClient.connect("172.16.37.147");
sshClient.authPublickey("jeroen");
assertThat("Is connected", sshClient.isAuthenticated());
}
@Test
public void shouldAcceptCorrectKey() throws IOException {
SSHClient sshClient = new SSHClient(new DefaultConfig());
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
sshClient.connect(SERVER_IP, DOCKER_PORT);
assertThat("Is connected", sshClient.isConnected());
}
@Test(expected = TransportException.class)
public void shouldDeclineWrongKey() throws IOException {
SSHClient sshClient = new SSHClient(new DefaultConfig());
sshClient.addHostKeyVerifier("d4:6a:a9:52:05:ab:b5:48:dd:73:60:18:0c:3a:f0:a3");
sshClient.connect(SERVER_IP, DOCKER_PORT);
}
@Test
public void shouldConnect() throws IOException {
SSHClient sshClient = getConnectedClient();
sshClient.authPublickey(USERNAME, "src/test/resources/id_rsa");
assertThat("Is authenitcated", sshClient.isAuthenticated());
}
@Test(expected = UserAuthException.class)
public void shouldFailWithWrongKey() throws IOException {
getConnectedClient().authPublickey(USERNAME, "src/test/resources/id_dsa");
}
private SSHClient getConnectedClient() throws IOException {
SSHClient sshClient = new SSHClient(new DefaultConfig());
sshClient.addHostKeyVerifier(new PromiscuousVerifier());
sshClient.connect(SERVER_IP, DOCKER_PORT);
return sshClient;
}
}

View File

@@ -0,0 +1,16 @@
FROM sickp/alpine-sshd:7.5
ADD id_rsa.pub /home/sshj/.ssh/authorized_keys
ADD test-container/ssh_host_ecdsa_key /etc/ssh/ssh_host_ecdsa_key
ADD test-container/ssh_host_ecdsa_key.pub /etc/ssh/ssh_host_ecdsa_key.pub
RUN \
echo "root:smile" | chpasswd && \
adduser -D -s /bin/ash sshj && \
passwd -u sshj && \
chmod 600 /home/sshj/.ssh/authorized_keys && \
chmod 600 /etc/ssh/ssh_host_ecdsa_key && \
chmod 644 /etc/ssh/ssh_host_ecdsa_key.pub && \
chown -R sshj:sshj /home/sshj

View File

@@ -0,0 +1,5 @@
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIOpOBFjqe0hjK/hs4WZ3dZqnzanq1L3/JbvV1TCkbe4ToAoGCCqGSM49
AwEHoUQDQgAEVzkrS7Yj0nXML7A3mE08YDthfBR/ZbyYJDIq1vTzcqs6KTaCT529
swNXWLHO+mbHviZcRiI57ULXHZ1emom/Jw==
-----END EC PRIVATE KEY-----

View File

@@ -0,0 +1 @@
ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBFc5K0u2I9J1zC+wN5hNPGA7YXwUf2W8mCQyKtb083KrOik2gk+dvbMDV1ixzvpmx74mXEYiOe1C1x2dXpqJvyc= root@404b27be2bf4