Merge "XSQL Karaf Command"
[controller.git] / opendaylight / netconf / netconf-ssh / src / test / java / org / opendaylight / controller / netconf / ssh / authentication / SSHServerTest.java
1 /*
2  * Copyright (c) 2013 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.controller.netconf.ssh.authentication;
9
10 import static org.mockito.Matchers.any;
11 import static org.mockito.Matchers.anyString;
12 import static org.mockito.Mockito.doNothing;
13 import static org.mockito.Mockito.doReturn;
14
15 import ch.ethz.ssh2.Connection;
16 import io.netty.channel.EventLoopGroup;
17 import io.netty.channel.nio.NioEventLoopGroup;
18 import java.io.InputStream;
19 import java.net.InetSocketAddress;
20 import junit.framework.Assert;
21 import org.apache.commons.io.IOUtils;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.Mock;
25 import org.mockito.MockitoAnnotations;
26 import org.opendaylight.controller.netconf.auth.AuthProvider;
27 import org.opendaylight.controller.netconf.ssh.NetconfSSHServer;
28 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil;
29 import org.osgi.framework.BundleContext;
30 import org.osgi.framework.ServiceListener;
31 import org.osgi.framework.ServiceReference;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35
36 public class SSHServerTest {
37
38     private static final String USER = "netconf";
39     private static final String PASSWORD = "netconf";
40     private static final String HOST = "127.0.0.1";
41     private static final int PORT = 1830;
42     private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 8383);
43     private static final Logger logger = LoggerFactory.getLogger(SSHServerTest.class);
44     private Thread sshServerThread;
45
46     @Mock
47     private BundleContext mockedContext;
48
49
50     @Before
51     public void setUp() throws Exception {
52         MockitoAnnotations.initMocks(this);
53         doReturn(null).when(mockedContext).createFilter(anyString());
54         doNothing().when(mockedContext).addServiceListener(any(ServiceListener.class), anyString());
55         doReturn(new ServiceReference[0]).when(mockedContext).getServiceReferences(anyString(), anyString());
56
57         logger.info("Creating SSH server");
58         String pem;
59         try (InputStream is = getClass().getResourceAsStream("/RSA.pk")) {
60             pem = IOUtils.toString(is);
61         }
62
63
64         EventLoopGroup bossGroup = new NioEventLoopGroup();
65         NetconfSSHServer server = NetconfSSHServer.start(PORT, NetconfConfigUtil.getNetconfLocalAddress(),
66                 bossGroup, pem.toCharArray());
67         server.setAuthProvider(new AuthProvider() {
68             @Override
69             public boolean authenticated(final String username, final String password) {
70                 return true;
71             }
72         });
73
74         sshServerThread = new Thread(server);
75         sshServerThread.setDaemon(true);
76         sshServerThread.start();
77         logger.info("SSH server on " + PORT);
78     }
79
80     @Test
81     public void connect() {
82         try {
83             Connection conn = new Connection(HOST, PORT);
84             Assert.assertNotNull(conn);
85             logger.info("connecting to SSH server");
86             conn.connect();
87             logger.info("authenticating ...");
88             boolean isAuthenticated = conn.authenticateWithPassword(USER, PASSWORD);
89             Assert.assertTrue(isAuthenticated);
90         } catch (Exception e) {
91             logger.error("Error while starting SSH server.", e);
92         }
93
94     }
95
96 }