This commit is contained in:
Iger
2017-12-03 22:40:41 +02:00
parent 9d697ede12
commit 73bc785ab4
2 changed files with 31 additions and 19 deletions

View File

@@ -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;
}
}

View File

@@ -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