Added reproducing testcase

This commit is contained in:
Jeroen van Erp
2016-03-18 10:53:15 +01:00
parent ac2ffbc367
commit ca49ca324f
3 changed files with 91 additions and 0 deletions

View File

@@ -57,6 +57,8 @@ dependencies {
testCompile "org.mockito:mockito-core:1.9.5"
testCompile "org.apache.sshd:sshd-core:1.0.0"
testRuntime "ch.qos.logback:logback-classic:1.1.2"
testCompile 'org.glassfish.grizzly:grizzly-http-server:2.3.17'
}
jar {

View File

@@ -0,0 +1,44 @@
package com.hierynomus.sshj.connection.channel.forwarded;
import com.hierynomus.sshj.test.HttpServer;
import com.hierynomus.sshj.test.SshFixture;
import com.hierynomus.sshj.test.util.FileUtil;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.connection.channel.forwarded.RemotePortForwarder;
import net.schmizz.sshj.connection.channel.forwarded.SocketForwardingConnectListener;
import org.apache.sshd.server.forward.AcceptAllForwardingFilter;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import static org.junit.Assert.*;
public class RemotePortForwarderTest {
@Rule
public SshFixture fixture = new SshFixture();
@Rule
public HttpServer httpServer = new HttpServer();
@Test
public void shouldDynamicallyForwardPort() throws IOException {
fixture.getServer().setTcpipForwardingFilter(new AcceptAllForwardingFilter());
File file = httpServer.getDocRoot().newFile("index.html");
FileUtil.writeToFile(file, "<html><head/><body><h1>Hi!</h1></body></html>");
SSHClient sshClient = fixture.setupConnectedDefaultClient();
sshClient.authPassword("jeroen", "jeroen");
sshClient.getRemotePortForwarder().bind(
// where the server should listen
new RemotePortForwarder.Forward(0),
// what we do with incoming connections that are forwarded to us
new SocketForwardingConnectListener(new InetSocketAddress("127.0.0.1", 8080)));
}
}

View File

@@ -0,0 +1,45 @@
package com.hierynomus.sshj.test;
import org.junit.rules.ExternalResource;
import org.junit.rules.TemporaryFolder;
import java.io.File;
/**
* Can be used to setup a test HTTP server
*/
public class HttpServer extends ExternalResource {
private org.glassfish.grizzly.http.server.HttpServer httpServer;
private TemporaryFolder docRoot = new TemporaryFolder();
public HttpServer() {
}
@Override
protected void before() throws Throwable {
docRoot.create();
httpServer = org.glassfish.grizzly.http.server.HttpServer.createSimpleServer(docRoot.getRoot().getAbsolutePath());
httpServer.start();
}
@Override
protected void after() {
try {
httpServer.shutdownNow();
} catch (Exception e) {}
try {
docRoot.delete();
} catch (Exception e) {}
}
public org.glassfish.grizzly.http.server.HttpServer getHttpServer() {
return httpServer;
}
public TemporaryFolder getDocRoot() {
return docRoot;
}
}