Merge "BUG-1568 Remove ganymed Ssh client adapter"
[controller.git] / opendaylight / netconf / netconf-ssh / src / test / java / org / opendaylight / controller / 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
9 package org.opendaylight.controller.netconf.netty;
10
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;
17
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.AsyncSshHandler;
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.AuthProviderImpl;
36 import org.opendaylight.controller.netconf.ssh.authentication.PEMGenerator;
37 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 public class SSHTest {
42     public static final Logger logger = LoggerFactory.getLogger(SSHTest.class);
43     public static final String AHOJ = "ahoj\n";
44     private EventLoopGroup nettyGroup;
45     HashedWheelTimer hashedWheelTimer;
46
47     @Before
48     public void setUp() throws Exception {
49         hashedWheelTimer = new HashedWheelTimer();
50         nettyGroup = new NioEventLoopGroup();
51     }
52
53     @After
54     public void tearDown() throws Exception {
55         hashedWheelTimer.stop();
56         nettyGroup.shutdownGracefully();
57     }
58
59     @Test
60     public void test() throws Exception {
61         new Thread(new EchoServer(), "EchoServer").start();
62         AuthProvider authProvider = mock(AuthProviderImpl.class);
63         doReturn(PEMGenerator.generate().toCharArray()).when(authProvider).getPEMAsCharArray();
64         doReturn(true).when(authProvider).authenticated(anyString(), anyString());
65         NetconfSSHServer netconfSSHServer = NetconfSSHServer.start(10831, NetconfConfigUtil.getNetconfLocalAddress(),
66                 authProvider, new NioEventLoopGroup());
67
68         InetSocketAddress address = netconfSSHServer.getLocalSocketAddress();
69         final EchoClientHandler echoClientHandler = connectClient(address);
70         Stopwatch stopwatch = new Stopwatch().start();
71         while(echoClientHandler.isConnected() == false && stopwatch.elapsed(TimeUnit.SECONDS) < 5) {
72             Thread.sleep(100);
73         }
74         assertTrue(echoClientHandler.isConnected());
75         logger.info("connected, writing to client");
76         echoClientHandler.write(AHOJ);
77         // check that server sent back the same string
78         stopwatch = stopwatch.reset().start();
79         while (echoClientHandler.read().endsWith(AHOJ) == false && stopwatch.elapsed(TimeUnit.SECONDS) < 5) {
80             Thread.sleep(100);
81         }
82         try {
83             String read = echoClientHandler.read();
84             assertTrue(read + " should end with " + AHOJ, read.endsWith(AHOJ));
85         } finally {
86             logger.info("Closing socket");
87             netconfSSHServer.close();
88             netconfSSHServer.join();
89         }
90     }
91
92     public EchoClientHandler connectClient(InetSocketAddress address) {
93         final EchoClientHandler echoClientHandler = new EchoClientHandler();
94         ChannelInitializer<NioSocketChannel> channelInitializer = new ChannelInitializer<NioSocketChannel>() {
95             @Override
96             public void initChannel(NioSocketChannel ch) throws Exception {
97                 ch.pipeline().addFirst(AsyncSshHandler.createForNetconfSubsystem(new LoginPassword("a", "a")));
98                 ch.pipeline().addLast(echoClientHandler);
99             }
100         };
101         Bootstrap b = new Bootstrap();
102
103         b.group(nettyGroup)
104                 .channel(NioSocketChannel.class)
105                 .handler(channelInitializer);
106
107         // Start the client.
108         b.connect(address).addListener(echoClientHandler);
109         return echoClientHandler;
110     }
111
112     @Test
113     public void testClientWithoutServer() throws Exception {
114         InetSocketAddress address = new InetSocketAddress(12345);
115         final EchoClientHandler echoClientHandler = connectClient(address);
116         Stopwatch stopwatch = new Stopwatch().start();
117         while(echoClientHandler.getState() == State.CONNECTING && stopwatch.elapsed(TimeUnit.SECONDS) < 5) {
118             Thread.sleep(100);
119         }
120         assertFalse(echoClientHandler.isConnected());
121         assertEquals(State.FAILED_TO_CONNECT, echoClientHandler.getState());
122     }
123
124 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.