Merge "Bug#1854 - Exit command in console causing OOM."
[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.auth.AuthProvider;
31 import org.opendaylight.controller.netconf.netty.EchoClientHandler.State;
32 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.LoginPassword;
33 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.client.AsyncSshHandler;
34 import org.opendaylight.controller.netconf.ssh.NetconfSSHServer;
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;
39
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;
45
46     @Before
47     public void setUp() throws Exception {
48         hashedWheelTimer = new HashedWheelTimer();
49         nettyGroup = new NioEventLoopGroup();
50     }
51
52     @After
53     public void tearDown() throws Exception {
54         hashedWheelTimer.stop();
55         nettyGroup.shutdownGracefully();
56     }
57
58     @Test
59     public void test() throws Exception {
60         new Thread(new EchoServer(), "EchoServer").start();
61         AuthProvider authProvider = mock(AuthProvider.class);
62         doReturn(true).when(authProvider).authenticated(anyString(), anyString());
63         doReturn("auth").when(authProvider).toString();
64
65         NetconfSSHServer netconfSSHServer = NetconfSSHServer.start(10831, NetconfConfigUtil.getNetconfLocalAddress(),
66                 new NioEventLoopGroup(), PEMGenerator.generate().toCharArray());
67         netconfSSHServer.setAuthProvider(authProvider);
68
69         InetSocketAddress address = netconfSSHServer.getLocalSocketAddress();
70
71         final EchoClientHandler echoClientHandler = connectClient(new InetSocketAddress("localhost", address.getPort()));
72
73         Stopwatch stopwatch = new Stopwatch().start();
74         while(echoClientHandler.isConnected() == false && stopwatch.elapsed(TimeUnit.SECONDS) < 5) {
75             Thread.sleep(100);
76         }
77         assertTrue(echoClientHandler.isConnected());
78         logger.info("connected, writing to client");
79         echoClientHandler.write(AHOJ);
80         // check that server sent back the same string
81         stopwatch = stopwatch.reset().start();
82         while (echoClientHandler.read().endsWith(AHOJ) == false && stopwatch.elapsed(TimeUnit.SECONDS) < 5) {
83             Thread.sleep(100);
84         }
85         try {
86             String read = echoClientHandler.read();
87             assertTrue(read + " should end with " + AHOJ, read.endsWith(AHOJ));
88         } finally {
89             logger.info("Closing socket");
90             netconfSSHServer.close();
91             netconfSSHServer.join();
92         }
93     }
94
95     public EchoClientHandler connectClient(InetSocketAddress address) {
96         final EchoClientHandler echoClientHandler = new EchoClientHandler();
97         ChannelInitializer<NioSocketChannel> channelInitializer = new ChannelInitializer<NioSocketChannel>() {
98             @Override
99             public void initChannel(NioSocketChannel ch) throws Exception {
100                 ch.pipeline().addFirst(AsyncSshHandler.createForNetconfSubsystem(new LoginPassword("a", "a")));
101                 ch.pipeline().addLast(echoClientHandler);
102             }
103         };
104         Bootstrap b = new Bootstrap();
105
106         b.group(nettyGroup)
107                 .channel(NioSocketChannel.class)
108                 .handler(channelInitializer);
109
110         // Start the client.
111         b.connect(address).addListener(echoClientHandler);
112         return echoClientHandler;
113     }
114
115     @Test
116     public void testClientWithoutServer() throws Exception {
117         InetSocketAddress address = new InetSocketAddress(12345);
118         final EchoClientHandler echoClientHandler = connectClient(address);
119         Stopwatch stopwatch = new Stopwatch().start();
120         while(echoClientHandler.getState() == State.CONNECTING && stopwatch.elapsed(TimeUnit.SECONDS) < 5) {
121             Thread.sleep(100);
122         }
123         assertFalse(echoClientHandler.isConnected());
124         assertEquals(State.CONNECTION_CLOSED, echoClientHandler.getState());
125     }
126
127 }