|
| 1 | +package com.baeldung.java.io.jsch; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | + |
| 6 | +import java.nio.file.Paths; |
| 7 | + |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.testcontainers.containers.GenericContainer; |
| 10 | +import org.testcontainers.junit.jupiter.Container; |
| 11 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 12 | +import org.testcontainers.utility.MountableFile; |
| 13 | + |
| 14 | +import com.jcraft.jsch.JSch; |
| 15 | +import com.jcraft.jsch.JSchException; |
| 16 | +import com.jcraft.jsch.JSchUnknownHostKeyException; |
| 17 | +import com.jcraft.jsch.Session; |
| 18 | + |
| 19 | +@Testcontainers |
| 20 | +class ConnectIntegrationTest { |
| 21 | + private static final int SFTP_PORT = 22; |
| 22 | + |
| 23 | + private static final String USERNAME = "baeldung"; |
| 24 | + private static final String PASSWORD = "test"; |
| 25 | + private static final String DIRECTORY = "upload"; |
| 26 | + |
| 27 | + // atmoz/sftp is a Docker container for an SFTP server that can easily support password and key authentication. |
| 28 | + @Container |
| 29 | + public static GenericContainer<?> sftpContainer = new GenericContainer<>("atmoz/sftp:latest") |
| 30 | + .withExposedPorts(SFTP_PORT) |
| 31 | + .withCommand(USERNAME + ":" + PASSWORD + ":::" + DIRECTORY) |
| 32 | + .withCopyFileToContainer( |
| 33 | + MountableFile.forHostPath(Paths.get("src/test/resources/com/baeldung/java/io/jsch/nopassphrase/id_rsa.pub")), |
| 34 | + "/home/" + USERNAME + "/.ssh/keys/id_rsa.pub" |
| 35 | + ) |
| 36 | + .withCopyFileToContainer( |
| 37 | + MountableFile.forHostPath(Paths.get("src/test/resources/com/baeldung/java/io/jsch/passphrase/id_rsa.pub")), |
| 38 | + "/home/" + USERNAME + "/.ssh/keys/id_rsa2.pub" |
| 39 | + ); |
| 40 | + |
| 41 | + @Test |
| 42 | + void whenTheHostKeyIsUnknown_thenTheConnectionFails() throws Exception { |
| 43 | + JSch jSch = new JSch(); |
| 44 | + |
| 45 | + Session session = jSch.getSession(USERNAME, sftpContainer.getHost(), sftpContainer.getMappedPort(SFTP_PORT)); |
| 46 | + |
| 47 | + session.setUserInfo(new BaseUserInfo() {}); |
| 48 | + |
| 49 | + assertThrows(JSchUnknownHostKeyException.class, () -> session.connect()); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void whenNoAuthenticationIsProvided_thenTheConnectionFails() throws Exception { |
| 54 | + JSch jSch = new JSch(); |
| 55 | + |
| 56 | + Session session = jSch.getSession(USERNAME, sftpContainer.getHost(), sftpContainer.getMappedPort(SFTP_PORT)); |
| 57 | + |
| 58 | + session.setUserInfo(new BaseUserInfo() { |
| 59 | + |
| 60 | + @Override |
| 61 | + public boolean promptYesNo(String message) { |
| 62 | + if (message.startsWith("The authenticity of host")) { |
| 63 | + return true; |
| 64 | + } |
| 65 | + |
| 66 | + return false; |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + JSchException exception = assertThrows(JSchException.class, () -> session.connect()); |
| 71 | + assertEquals("Auth cancel for methods 'publickey,password,keyboard-interactive'", exception.getMessage()); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void whenPasswordAuthIsProvided_thenTheConnectionSucceeds() throws Exception { |
| 76 | + JSch jSch = new JSch(); |
| 77 | + |
| 78 | + Session session = jSch.getSession(USERNAME, sftpContainer.getHost(), sftpContainer.getMappedPort(SFTP_PORT)); |
| 79 | + |
| 80 | + session.setUserInfo(new BaseUserInfo() { |
| 81 | + @Override |
| 82 | + public String getPassword() { |
| 83 | + return PASSWORD; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public boolean promptPassword(String message) { |
| 88 | + return true; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public boolean promptYesNo(String message) { |
| 93 | + if (message.startsWith("The authenticity of host")) { |
| 94 | + return true; |
| 95 | + } |
| 96 | + |
| 97 | + return false; |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | + session.connect(); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void givenAnOpenSSHKey_whenKeyAuthIsProvided_thenTheConnectionSucceeds() throws Exception { |
| 106 | + JSch jSch = new JSch(); |
| 107 | + jSch.addIdentity("src/test/resources/com/baeldung/java/io/jsch/nopassphrase/id_rsa"); |
| 108 | + |
| 109 | + Session session = jSch.getSession(USERNAME, sftpContainer.getHost(), sftpContainer.getMappedPort(SFTP_PORT)); |
| 110 | + |
| 111 | + session.setUserInfo(new BaseUserInfo() { |
| 112 | + @Override |
| 113 | + public boolean promptYesNo(String message) { |
| 114 | + if (message.startsWith("The authenticity of host")) { |
| 115 | + return true; |
| 116 | + } |
| 117 | + |
| 118 | + return false; |
| 119 | + } |
| 120 | + }); |
| 121 | + |
| 122 | + session.connect(); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + void givenAPuttyKey_whenKeyAuthIsProvided_thenTheConnectionSucceeds() throws Exception { |
| 127 | + JSch jSch = new JSch(); |
| 128 | + jSch.addIdentity("src/test/resources/com/baeldung/java/io/jsch/nopassphrase/id_rsa.ppk"); |
| 129 | + |
| 130 | + Session session = jSch.getSession(USERNAME, sftpContainer.getHost(), sftpContainer.getMappedPort(SFTP_PORT)); |
| 131 | + |
| 132 | + session.setUserInfo(new BaseUserInfo() { |
| 133 | + @Override |
| 134 | + public boolean promptYesNo(String message) { |
| 135 | + if (message.startsWith("The authenticity of host")) { |
| 136 | + return true; |
| 137 | + } |
| 138 | + |
| 139 | + return false; |
| 140 | + } |
| 141 | + }); |
| 142 | + |
| 143 | + session.connect(); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + void givenAnOpenSSHKeyWithPassphrase_whenKeyAuthIsProvided_thenTheConnectionSucceeds() throws Exception { |
| 148 | + JSch jSch = new JSch(); |
| 149 | + jSch.addIdentity("src/test/resources/com/baeldung/java/io/jsch/passphrase/id_rsa"); |
| 150 | + |
| 151 | + Session session = jSch.getSession(USERNAME, sftpContainer.getHost(), sftpContainer.getMappedPort(SFTP_PORT)); |
| 152 | + |
| 153 | + session.setUserInfo(new BaseUserInfo() { |
| 154 | + @Override |
| 155 | + public String getPassphrase() { |
| 156 | + return "te5tPa55word"; |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public boolean promptPassphrase(String message) { |
| 161 | + return true; |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public boolean promptYesNo(String message) { |
| 166 | + if (message.startsWith("The authenticity of host")) { |
| 167 | + return true; |
| 168 | + } |
| 169 | + |
| 170 | + return false; |
| 171 | + } |
| 172 | + }); |
| 173 | + |
| 174 | + session.connect(); |
| 175 | + } |
| 176 | +} |
0 commit comments