2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.netconf.netty;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
14 import com.google.common.base.Stopwatch;
15 import io.netty.bootstrap.Bootstrap;
16 import io.netty.channel.ChannelInitializer;
17 import io.netty.channel.EventLoopGroup;
18 import io.netty.channel.nio.NioEventLoopGroup;
19 import io.netty.channel.socket.nio.NioSocketChannel;
20 import io.netty.util.HashedWheelTimer;
22 import java.net.InetSocketAddress;
23 import java.nio.file.Files;
24 import java.util.concurrent.ExecutorService;
25 import java.util.concurrent.Executors;
26 import java.util.concurrent.ScheduledExecutorService;
27 import java.util.concurrent.TimeUnit;
28 import org.junit.AfterClass;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 import org.opendaylight.netconf.netty.EchoClientHandler.State;
32 import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.LoginPasswordHandler;
33 import org.opendaylight.netconf.nettyutil.handler.ssh.client.AsyncSshHandler;
34 import org.opendaylight.netconf.shaded.sshd.common.util.security.SecurityUtils;
35 import org.opendaylight.netconf.ssh.SshProxyServer;
36 import org.opendaylight.netconf.ssh.SshProxyServerConfigurationBuilder;
37 import org.opendaylight.netconf.util.NetconfConfiguration;
39 public class SSHTest {
40 public static final String AHOJ = "ahoj\n";
42 private static EventLoopGroup nettyGroup;
43 private static HashedWheelTimer hashedWheelTimer;
44 private static ExecutorService nioExec;
45 private static ScheduledExecutorService minaTimerEx;
48 public static void setUp() throws Exception {
49 hashedWheelTimer = new HashedWheelTimer();
50 nettyGroup = new NioEventLoopGroup();
51 nioExec = Executors.newFixedThreadPool(1);
52 minaTimerEx = Executors.newScheduledThreadPool(1);
56 public static void tearDown() throws Exception {
57 hashedWheelTimer.stop();
58 nettyGroup.shutdownGracefully().await(5, TimeUnit.SECONDS);
59 minaTimerEx.shutdownNow();
60 nioExec.shutdownNow();
64 public void test() throws Exception {
65 File sshKeyPair = Files.createTempFile("sshKeyPair", ".pem").toFile();
66 sshKeyPair.deleteOnExit();
67 new Thread(new EchoServer(), "EchoServer").start();
69 final InetSocketAddress addr = new InetSocketAddress("127.0.0.1", 10831);
70 try (var sshProxyServer = new SshProxyServer(minaTimerEx, nettyGroup, nioExec)) {
71 sshProxyServer.bind(new SshProxyServerConfigurationBuilder()
72 .setBindingAddress(addr).setLocalAddress(NetconfConfiguration.NETCONF_LOCAL_ADDRESS)
73 .setAuthenticator((username, password) -> true)
74 .setKeyPairProvider(SecurityUtils.createGeneratorHostKeyProvider(sshKeyPair.toPath()))
75 .setIdleTimeout(Integer.MAX_VALUE).createSshProxyServerConfiguration());
77 final EchoClientHandler echoClientHandler = connectClient(addr);
79 Stopwatch stopwatch = Stopwatch.createStarted();
80 while (echoClientHandler.isConnected() == false && stopwatch.elapsed(TimeUnit.SECONDS) < 30) {
83 assertTrue(echoClientHandler.isConnected());
84 echoClientHandler.write(AHOJ);
86 // check that server sent back the same string
87 stopwatch = stopwatch.reset().start();
88 while (echoClientHandler.read().endsWith(AHOJ) == false && stopwatch.elapsed(TimeUnit.SECONDS) < 30) {
92 final String read = echoClientHandler.read();
93 assertTrue(read + " should end with " + AHOJ, read.endsWith(AHOJ));
97 public EchoClientHandler connectClient(final InetSocketAddress address) {
98 final EchoClientHandler echoClientHandler = new EchoClientHandler();
99 final ChannelInitializer<NioSocketChannel> channelInitializer = new ChannelInitializer<>() {
101 public void initChannel(final NioSocketChannel ch) throws Exception {
102 ch.pipeline().addFirst(AsyncSshHandler.createForNetconfSubsystem(new LoginPasswordHandler("a", "a")));
103 ch.pipeline().addLast(echoClientHandler);
106 final Bootstrap b = new Bootstrap();
109 .channel(NioSocketChannel.class)
110 .handler(channelInitializer);
113 b.connect(address).addListener(echoClientHandler);
114 return echoClientHandler;
118 public void testClientWithoutServer() throws Exception {
119 final InetSocketAddress address = new InetSocketAddress(12345);
120 final EchoClientHandler echoClientHandler = connectClient(address);
121 final Stopwatch stopwatch = Stopwatch.createStarted();
122 while (echoClientHandler.getState() == State.CONNECTING && stopwatch.elapsed(TimeUnit.SECONDS) < 5) {
125 assertFalse(echoClientHandler.isConnected());
126 assertEquals(State.FAILED_TO_CONNECT, echoClientHandler.getState());