diff --git a/src/test/java/com/hierynomus/sshj/SshIntegrationTestBase.java b/src/test/java/com/hierynomus/sshj/SshIntegrationTestBase.java new file mode 100644 index 00000000..f37e5517 --- /dev/null +++ b/src/test/java/com/hierynomus/sshj/SshIntegrationTestBase.java @@ -0,0 +1,60 @@ +package com.hierynomus.sshj; + +import org.apache.sshd.SshServer; +import org.apache.sshd.common.keyprovider.FileKeyPairProvider; +import org.apache.sshd.server.PasswordAuthenticator; +import org.apache.sshd.server.session.ServerSession; +import org.junit.After; +import org.junit.Before; + +import java.io.IOException; +import java.net.ServerSocket; + +public class SshIntegrationTestBase { + public static final String hostkey = "src/test/resources/hostkey.pem"; + public static final String fingerprint = "ce:a7:c1:cf:17:3f:96:49:6a:53:1a:05:0b:ba:90:db"; + + public static final String hostname = "localhost"; + + protected SshServer server = null; + + @Before + public void setupSshServer() throws IOException { + server = SshServer.setUpDefaultServer(); + server.setPort(randomPort()); + configureSshServer(); + server.start(); + } + + @After + public void stopSshServer() throws Exception { + server.stop(); + } + + protected void configureSshServer() { + server.setKeyPairProvider(new FileKeyPairProvider(new String[]{hostkey})); + server.setPasswordAuthenticator(new PasswordAuthenticator() { + @Override + public boolean authenticate(String username, String password, ServerSession session) { + return username.equals(password); + } + }); + } + + private int randomPort() { + try { + ServerSocket s = null; + try { + s = new ServerSocket(0); + return s.getLocalPort(); + } finally { + if (s != null) + s.close(); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + +} + diff --git a/src/itest/java/nl/javadude/sshj/connection/channel/ChannelCloseEofTest.java b/src/test/java/com/hierynomus/sshj/connection/channel/ChannelCloseEofTest.java similarity index 81% rename from src/itest/java/nl/javadude/sshj/connection/channel/ChannelCloseEofTest.java rename to src/test/java/com/hierynomus/sshj/connection/channel/ChannelCloseEofTest.java index 2cad07a8..3d171188 100644 --- a/src/itest/java/nl/javadude/sshj/connection/channel/ChannelCloseEofTest.java +++ b/src/test/java/com/hierynomus/sshj/connection/channel/ChannelCloseEofTest.java @@ -1,5 +1,6 @@ -package nl.javadude.sshj.connection.channel; +package com.hierynomus.sshj.connection.channel; +import com.hierynomus.sshj.SshIntegrationTestBase; import net.schmizz.sshj.SSHClient; import net.schmizz.sshj.connection.channel.direct.Session; import net.schmizz.sshj.transport.verification.PromiscuousVerifier; @@ -11,8 +12,7 @@ import java.io.IOException; import static org.hamcrest.MatcherAssert.assertThat; -public class ChannelCloseEofTest { - +public class ChannelCloseEofTest extends SshIntegrationTestBase { private SSHClient sshClient; @Before @@ -28,7 +28,7 @@ public class ChannelCloseEofTest { @Test public void shouldCorrectlyHandleSessionChannelEof() throws IOException, InterruptedException { sshClient.addHostKeyVerifier(new PromiscuousVerifier()); - sshClient.connect("172.16.37.129"); + sshClient.connect(server.getHost(), server.getPort()); sshClient.authPassword("jeroen", "jeroen"); Session session = sshClient.startSession(); session.allocateDefaultPTY(); @@ -36,4 +36,5 @@ public class ChannelCloseEofTest { Thread.sleep(1000); assertThat("Should still be connected", sshClient.isConnected()); } + }