|
15 | 15 | */ |
16 | 16 | package com.hierynomus.sshj; |
17 | 17 |
|
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; |
23 | 19 |
|
24 | 20 | import java.io.File; |
25 | 21 | import java.io.IOException; |
26 | 22 |
|
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; |
28 | 32 |
|
29 | 33 | public class IntegrationTest { |
30 | 34 |
|
| 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 | + |
31 | 39 | @Test @Ignore // Should only be enabled for testing against VM |
32 | | - public void shouldConnect() throws IOException { |
| 40 | + public void shouldConnectVM() throws IOException { |
33 | 41 | SSHClient sshClient = new SSHClient(new DefaultConfig()); |
34 | 42 | sshClient.addHostKeyVerifier(new OpenSSHKnownHosts(new File("/Users/ajvanerp/.ssh/known_hosts"))); |
35 | 43 | sshClient.connect("172.16.37.147"); |
36 | 44 | sshClient.authPublickey("jeroen"); |
37 | 45 | assertThat("Is connected", sshClient.isAuthenticated()); |
38 | 46 | } |
| 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 | + } |
39 | 82 | } |
0 commit comments