Convert mdsal-netconf-ssh to OSGi DS
[netconf.git] / netconf / mdsal-netconf-ssh / src / test / java / org / opendaylight / netconf / netty / SSHTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.netty;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13
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;
21 import java.io.File;
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;
38
39 public class SSHTest {
40     public static final String AHOJ = "ahoj\n";
41
42     private static EventLoopGroup nettyGroup;
43     private static HashedWheelTimer hashedWheelTimer;
44     private static ExecutorService nioExec;
45     private static ScheduledExecutorService minaTimerEx;
46
47     @BeforeClass
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);
53     }
54
55     @AfterClass
56     public static void tearDown() throws Exception {
57         hashedWheelTimer.stop();
58         nettyGroup.shutdownGracefully().await(5, TimeUnit.SECONDS);
59         minaTimerEx.shutdownNow();
60         nioExec.shutdownNow();
61     }
62
63     @Test
64     public void test() throws Exception {
65         File sshKeyPair = Files.createTempFile("sshKeyPair", ".pem").toFile();
66         sshKeyPair.deleteOnExit();
67         new Thread(new EchoServer(), "EchoServer").start();
68
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());
76
77             final EchoClientHandler echoClientHandler = connectClient(addr);
78
79             Stopwatch stopwatch = Stopwatch.createStarted();
80             while (echoClientHandler.isConnected() == false && stopwatch.elapsed(TimeUnit.SECONDS) < 30) {
81                 Thread.sleep(500);
82             }
83             assertTrue(echoClientHandler.isConnected());
84             echoClientHandler.write(AHOJ);
85
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) {
89                 Thread.sleep(500);
90             }
91
92             final String read = echoClientHandler.read();
93             assertTrue(read + " should end with " + AHOJ, read.endsWith(AHOJ));
94         }
95     }
96
97     public EchoClientHandler connectClient(final InetSocketAddress address) {
98         final EchoClientHandler echoClientHandler = new EchoClientHandler();
99         final ChannelInitializer<NioSocketChannel> channelInitializer = new ChannelInitializer<>() {
100             @Override
101             public void initChannel(final NioSocketChannel ch) throws Exception {
102                 ch.pipeline().addFirst(AsyncSshHandler.createForNetconfSubsystem(new LoginPasswordHandler("a", "a")));
103                 ch.pipeline().addLast(echoClientHandler);
104             }
105         };
106         final Bootstrap b = new Bootstrap();
107
108         b.group(nettyGroup)
109                 .channel(NioSocketChannel.class)
110                 .handler(channelInitializer);
111
112         // Start the client.
113         b.connect(address).addListener(echoClientHandler);
114         return echoClientHandler;
115     }
116
117     @Test
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) {
123             Thread.sleep(100);
124         }
125         assertFalse(echoClientHandler.isConnected());
126         assertEquals(State.FAILED_TO_CONNECT, echoClientHandler.getState());
127     }
128
129 }