* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.netconf.netty;
import static org.junit.Assert.assertEquals;
import org.opendaylight.netconf.ssh.SshProxyServer;
import org.opendaylight.netconf.ssh.SshProxyServerConfigurationBuilder;
import org.opendaylight.netconf.util.NetconfConfiguration;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
public class SSHTest {
- private static final Logger LOG = LoggerFactory.getLogger(SSHTest.class);
public static final String AHOJ = "ahoj\n";
private static EventLoopGroup nettyGroup;
new Thread(new EchoServer(), "EchoServer").start();
final InetSocketAddress addr = new InetSocketAddress("127.0.0.1", 10831);
- final SshProxyServer sshProxyServer = new SshProxyServer(minaTimerEx, nettyGroup, nioExec);
- sshProxyServer.bind(new SshProxyServerConfigurationBuilder()
+ try (var sshProxyServer = new SshProxyServer(minaTimerEx, nettyGroup, nioExec)) {
+ sshProxyServer.bind(new SshProxyServerConfigurationBuilder()
.setBindingAddress(addr).setLocalAddress(NetconfConfiguration.NETCONF_LOCAL_ADDRESS)
.setAuthenticator((username, password) -> true)
.setKeyPairProvider(SecurityUtils.createGeneratorHostKeyProvider(sshKeyPair.toPath()))
.setIdleTimeout(Integer.MAX_VALUE).createSshProxyServerConfiguration());
- final EchoClientHandler echoClientHandler = connectClient(addr);
+ final EchoClientHandler echoClientHandler = connectClient(addr);
- Stopwatch stopwatch = Stopwatch.createStarted();
- while (echoClientHandler.isConnected() == false && stopwatch.elapsed(TimeUnit.SECONDS) < 30) {
- Thread.sleep(500);
- }
- assertTrue(echoClientHandler.isConnected());
- LOG.info("connected, writing to client");
- echoClientHandler.write(AHOJ);
-
- // check that server sent back the same string
- stopwatch = stopwatch.reset().start();
- while (echoClientHandler.read().endsWith(AHOJ) == false && stopwatch.elapsed(TimeUnit.SECONDS) < 30) {
- Thread.sleep(500);
- }
+ Stopwatch stopwatch = Stopwatch.createStarted();
+ while (echoClientHandler.isConnected() == false && stopwatch.elapsed(TimeUnit.SECONDS) < 30) {
+ Thread.sleep(500);
+ }
+ assertTrue(echoClientHandler.isConnected());
+ echoClientHandler.write(AHOJ);
+
+ // check that server sent back the same string
+ stopwatch = stopwatch.reset().start();
+ while (echoClientHandler.read().endsWith(AHOJ) == false && stopwatch.elapsed(TimeUnit.SECONDS) < 30) {
+ Thread.sleep(500);
+ }
- try {
final String read = echoClientHandler.read();
assertTrue(read + " should end with " + AHOJ, read.endsWith(AHOJ));
- } finally {
- LOG.info("Closing socket");
- sshProxyServer.close();
}
}