use of io.cdap.cdap.runtime.spi.ssh.RemotePortForwarding in project cdap by caskdata.
the class SSHSessionTest method testRemotePortForwarding.
@Test
public void testRemotePortForwarding() throws Exception {
EchoServer echoServer = new EchoServer();
echoServer.startAndWait();
try {
SSHConfig sshConfig = getSSHConfig();
try (SSHSession session = new DefaultSSHSession(sshConfig)) {
InetSocketAddress echoServerAddr = echoServer.getBindAddress();
try (RemotePortForwarding forwarding = session.createRemotePortForward(0, echoServerAddr.getPort())) {
// Send data to the remotePort, it should get forwarded to the echoServer
try (Socket socket = new Socket(session.getAddress().getAddress(), forwarding.getRemotePort())) {
PrintStream printer = new PrintStream(socket.getOutputStream(), true, "UTF-8");
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
String msg = "Testing message";
printer.println(msg);
Assert.assertEquals(msg, reader.readLine());
}
}
}
} finally {
echoServer.stopAndWait();
}
}
Aggregations