diff --git a/.travis.yml b/.travis.yml index 8300ef05..a85a50e9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/src/test/java/com/hierynomus/sshj/IntegrationTest.java b/src/test/java/com/hierynomus/sshj/IntegrationTest.java index 8ee1e709..861b1a46 100644 --- a/src/test/java/com/hierynomus/sshj/IntegrationTest.java +++ b/src/test/java/com/hierynomus/sshj/IntegrationTest.java @@ -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; + } } diff --git a/src/test/resources/Dockerfile b/src/test/resources/Dockerfile new file mode 100644 index 00000000..b306ac8c --- /dev/null +++ b/src/test/resources/Dockerfile @@ -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 + diff --git a/src/test/resources/test-container/ssh_host_ecdsa_key b/src/test/resources/test-container/ssh_host_ecdsa_key new file mode 100644 index 00000000..cac0cbe7 --- /dev/null +++ b/src/test/resources/test-container/ssh_host_ecdsa_key @@ -0,0 +1,5 @@ +-----BEGIN EC PRIVATE KEY----- +MHcCAQEEIOpOBFjqe0hjK/hs4WZ3dZqnzanq1L3/JbvV1TCkbe4ToAoGCCqGSM49 +AwEHoUQDQgAEVzkrS7Yj0nXML7A3mE08YDthfBR/ZbyYJDIq1vTzcqs6KTaCT529 +swNXWLHO+mbHviZcRiI57ULXHZ1emom/Jw== +-----END EC PRIVATE KEY----- diff --git a/src/test/resources/test-container/ssh_host_ecdsa_key.pub b/src/test/resources/test-container/ssh_host_ecdsa_key.pub new file mode 100644 index 00000000..9b7f7995 --- /dev/null +++ b/src/test/resources/test-container/ssh_host_ecdsa_key.pub @@ -0,0 +1 @@ +ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBFc5K0u2I9J1zC+wN5hNPGA7YXwUf2W8mCQyKtb083KrOik2gk+dvbMDV1ixzvpmx74mXEYiOe1C1x2dXpqJvyc= root@404b27be2bf4 \ No newline at end of file