Skip to content

Commit

Permalink
Added reproducing testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
hierynomus committed Mar 18, 2016
1 parent ac2ffbc commit ca49ca3
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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)));

}
}
45 changes: 45 additions & 0 deletions src/test/java/com/hierynomus/sshj/test/HttpServer.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit ca49ca3

Please sign in to comment.