-
Notifications
You must be signed in to change notification settings - Fork 592
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac2ffbc
commit ca49ca3
Showing
3 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/test/java/com/hierynomus/sshj/connection/channel/forwarded/RemotePortForwarderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |