From 73bc785ab4328ad7a35c9e9c64ad9b9d165914cb Mon Sep 17 00:00:00 2001 From: Iger Date: Sun, 3 Dec 2017 22:40:41 +0200 Subject: [PATCH] - eh? --- .../com/hierynomus/sshj/IntegrationTest.java | 33 +++++++++++++++---- src/test/resources/Dockerfile | 17 +++------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/test/java/com/hierynomus/sshj/IntegrationTest.java b/src/test/java/com/hierynomus/sshj/IntegrationTest.java index 89f66c7f..b0adfddd 100644 --- a/src/test/java/com/hierynomus/sshj/IntegrationTest.java +++ b/src/test/java/com/hierynomus/sshj/IntegrationTest.java @@ -25,13 +25,15 @@ 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 static final String USERNAME = "sickp"; private final static String SERVER_IP = System.getProperty("serverIP", "127.0.0.1"); @Test @Ignore // Should only be enabled for testing against VM @@ -44,20 +46,37 @@ public class IntegrationTest { } @Test - public void shouldConnect() throws IOException { + 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 connected", sshClient.isAuthenticated()); + assertThat("Is authenitcated", sshClient.isAuthenticated()); } @Test(expected = UserAuthException.class) public void shouldFailWithWrongKey() 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); - sshClient.authPublickey(USERNAME, "src/test/resources/id_dsa"); + 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 index ab836e27..0c0867ec 100644 --- a/src/test/resources/Dockerfile +++ b/src/test/resources/Dockerfile @@ -1,15 +1,8 @@ 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 - +ADD https://raw.githubusercontent.com/Igerly/sshj/master/src/test/resources/id_rsa.pub /home/sickp/.ssh/authorized_keys RUN \ - echo "root:smile" | chpasswd && \ - adduser -D -s /bin/ash sshj && \ - passwd -u sshj && \ - chmod 600 /etc/ssh/ssh_host_ecdsa_key && \ - chmod 644 /etc/ssh/ssh_host_ecdsa_key.pub && \ - chown -R sshj:sshj /home/sshj + echo "root:sunshine" | chpasswd && \ + adduser -D -s /bin/ash sickp && \ + passwd -u sickp && \ + chown -R sickp:sickp /home/sickp