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
9 package org.opendaylight.netconf.netty;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertTrue;
15 import com.google.common.base.Stopwatch;
16 import io.netty.bootstrap.Bootstrap;
17 import io.netty.channel.ChannelInitializer;
18 import io.netty.channel.EventLoopGroup;
19 import io.netty.channel.nio.NioEventLoopGroup;
20 import io.netty.channel.socket.nio.NioSocketChannel;
21 import io.netty.util.HashedWheelTimer;
23 import java.net.InetSocketAddress;
24 import java.nio.file.Files;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.ScheduledExecutorService;
28 import java.util.concurrent.TimeUnit;
29 import org.apache.sshd.server.keyprovider.PEMGeneratorHostKeyProvider;
30 import org.junit.AfterClass;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.opendaylight.netconf.netty.EchoClientHandler.State;
34 import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.LoginPassword;
35 import org.opendaylight.netconf.nettyutil.handler.ssh.client.AsyncSshHandler;
36 import org.opendaylight.netconf.ssh.SshProxyServer;
37 import org.opendaylight.netconf.ssh.SshProxyServerConfigurationBuilder;
38 import org.opendaylight.netconf.util.osgi.NetconfConfiguration;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 public class SSHTest {
43 private static final Logger LOG = LoggerFactory.getLogger(SSHTest.class);
44 public static final String AHOJ = "ahoj\n";
46 private static EventLoopGroup nettyGroup;
47 private static HashedWheelTimer hashedWheelTimer;
48 private static ExecutorService nioExec;
49 private static ScheduledExecutorService minaTimerEx;
52 public static void setUp() throws Exception {
53 hashedWheelTimer = new HashedWheelTimer();
54 nettyGroup = new NioEventLoopGroup();
55 nioExec = Executors.newFixedThreadPool(1);
56 minaTimerEx = Executors.newScheduledThreadPool(1);
60 public static void tearDown() throws Exception {
61 hashedWheelTimer.stop();
62 nettyGroup.shutdownGracefully().await(5, TimeUnit.SECONDS);
63 minaTimerEx.shutdownNow();
64 nioExec.shutdownNow();
68 public void test() throws Exception {
69 File sshKeyPair = Files.createTempFile("sshKeyPair", ".pem").toFile();
70 sshKeyPair.deleteOnExit();
71 new Thread(new EchoServer(), "EchoServer").start();
73 final InetSocketAddress addr = new InetSocketAddress("127.0.0.1", 10831);
74 final SshProxyServer sshProxyServer = new SshProxyServer(minaTimerEx, nettyGroup, nioExec);
75 sshProxyServer.bind(new SshProxyServerConfigurationBuilder()
76 .setBindingAddress(addr).setLocalAddress(NetconfConfiguration.NETCONF_LOCAL_ADDRESS)
77 .setAuthenticator((username, password) -> true)
78 .setKeyPairProvider(new PEMGeneratorHostKeyProvider(sshKeyPair.toPath().toAbsolutePath().toString(),
80 .setIdleTimeout(Integer.MAX_VALUE).createSshProxyServerConfiguration());
82 final EchoClientHandler echoClientHandler = connectClient(addr);
84 Stopwatch stopwatch = Stopwatch.createStarted();
85 while (echoClientHandler.isConnected() == false && stopwatch.elapsed(TimeUnit.SECONDS) < 30) {
88 assertTrue(echoClientHandler.isConnected());
89 LOG.info("connected, writing to client");
90 echoClientHandler.write(AHOJ);
92 // check that server sent back the same string
93 stopwatch = stopwatch.reset().start();
94 while (echoClientHandler.read().endsWith(AHOJ) == false && stopwatch.elapsed(TimeUnit.SECONDS) < 30) {
99 final String read = echoClientHandler.read();
100 assertTrue(read + " should end with " + AHOJ, read.endsWith(AHOJ));
102 LOG.info("Closing socket");
103 sshProxyServer.close();
107 public EchoClientHandler connectClient(final InetSocketAddress address) {
108 final EchoClientHandler echoClientHandler = new EchoClientHandler();
109 final ChannelInitializer<NioSocketChannel> channelInitializer = new ChannelInitializer<NioSocketChannel>() {
111 public void initChannel(final NioSocketChannel ch) throws Exception {
112 ch.pipeline().addFirst(AsyncSshHandler.createForNetconfSubsystem(new LoginPassword("a", "a")));
113 ch.pipeline().addLast(echoClientHandler);
116 final Bootstrap b = new Bootstrap();
119 .channel(NioSocketChannel.class)
120 .handler(channelInitializer);
123 b.connect(address).addListener(echoClientHandler);
124 return echoClientHandler;
128 public void testClientWithoutServer() throws Exception {
129 final InetSocketAddress address = new InetSocketAddress(12345);
130 final EchoClientHandler echoClientHandler = connectClient(address);
131 final Stopwatch stopwatch = Stopwatch.createStarted();
132 while (echoClientHandler.getState() == State.CONNECTING && stopwatch.elapsed(TimeUnit.SECONDS) < 5) {
135 assertFalse(echoClientHandler.isConnected());
136 assertEquals(State.FAILED_TO_CONNECT, echoClientHandler.getState());