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.controller.netconf.netty;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.Matchers.anyString;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
18 import com.google.common.base.Stopwatch;
19 import io.netty.bootstrap.Bootstrap;
20 import io.netty.channel.ChannelInitializer;
21 import io.netty.channel.EventLoopGroup;
22 import io.netty.channel.nio.NioEventLoopGroup;
23 import io.netty.channel.socket.nio.NioSocketChannel;
24 import io.netty.util.HashedWheelTimer;
25 import java.net.InetSocketAddress;
26 import java.util.concurrent.TimeUnit;
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.opendaylight.controller.netconf.netty.EchoClientHandler.State;
31 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.LoginPassword;
32 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.client.SshHandler;
33 import org.opendaylight.controller.netconf.ssh.NetconfSSHServer;
34 import org.opendaylight.controller.netconf.ssh.authentication.AuthProvider;
35 import org.opendaylight.controller.netconf.ssh.authentication.PEMGenerator;
36 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class SSHTest {
41 public static final Logger logger = LoggerFactory.getLogger(SSHTest.class);
42 public static final String AHOJ = "ahoj\n";
43 private EventLoopGroup nettyGroup;
44 HashedWheelTimer hashedWheelTimer;
47 public void setUp() throws Exception {
48 hashedWheelTimer = new HashedWheelTimer();
49 nettyGroup = new NioEventLoopGroup();
53 public void tearDown() throws Exception {
54 hashedWheelTimer.stop();
55 nettyGroup.shutdownGracefully();
59 public void test() throws Exception {
60 new Thread(new EchoServer(), "EchoServer").start();
61 AuthProvider authProvider = mock(AuthProvider.class);
62 doReturn(PEMGenerator.generate().toCharArray()).when(authProvider).getPEMAsCharArray();
63 doReturn(true).when(authProvider).authenticated(anyString(), anyString());
64 NetconfSSHServer netconfSSHServer = NetconfSSHServer.start(10831, NetconfConfigUtil.getNetconfLocalAddress(),
65 authProvider, new NioEventLoopGroup());
67 InetSocketAddress address = netconfSSHServer.getLocalSocketAddress();
68 final EchoClientHandler echoClientHandler = connectClient(address);
69 Stopwatch stopwatch = new Stopwatch().start();
70 while(echoClientHandler.isConnected() == false && stopwatch.elapsed(TimeUnit.SECONDS) < 5) {
73 assertTrue(echoClientHandler.isConnected());
74 logger.info("connected, writing to client");
75 echoClientHandler.write(AHOJ);
76 // check that server sent back the same string
77 stopwatch = stopwatch.reset().start();
78 while (echoClientHandler.read().endsWith(AHOJ) == false && stopwatch.elapsed(TimeUnit.SECONDS) < 5) {
82 String read = echoClientHandler.read();
83 assertTrue(read + " should end with " + AHOJ, read.endsWith(AHOJ));
85 logger.info("Closing socket");
86 netconfSSHServer.close();
87 netconfSSHServer.join();
91 public EchoClientHandler connectClient(InetSocketAddress address) {
92 final EchoClientHandler echoClientHandler = new EchoClientHandler();
93 ChannelInitializer<NioSocketChannel> channelInitializer = new ChannelInitializer<NioSocketChannel>() {
95 public void initChannel(NioSocketChannel ch) throws Exception {
96 ch.pipeline().addFirst(SshHandler.createForNetconfSubsystem(new LoginPassword("a", "a")));
97 ch.pipeline().addLast(echoClientHandler);
100 Bootstrap b = new Bootstrap();
103 .channel(NioSocketChannel.class)
104 .handler(channelInitializer);
107 b.connect(address).addListener(echoClientHandler);
108 return echoClientHandler;
112 public void testClientWithoutServer() throws Exception {
113 InetSocketAddress address = new InetSocketAddress(12345);
114 final EchoClientHandler echoClientHandler = connectClient(address);
115 Stopwatch stopwatch = new Stopwatch().start();
116 while(echoClientHandler.getState() == State.CONNECTING && stopwatch.elapsed(TimeUnit.SECONDS) < 5) {
119 assertFalse(echoClientHandler.isConnected());
120 assertEquals(State.FAILED_TO_CONNECT, echoClientHandler.getState());