2 * Copyright (c) 2013 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
8 package org.opendaylight.controller.netconf.ssh.authentication;
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;
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.StubUserManager;
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;
36 public class SSHServerTest {
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;
47 private BundleContext mockedContext;
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());
57 logger.info("Creating SSH server");
58 StubUserManager um = new StubUserManager(USER, PASSWORD);
60 try (InputStream is = getClass().getResourceAsStream("/RSA.pk")) {
61 pem = IOUtils.toString(is);
63 AuthProvider ap = new AuthProvider(pem, mockedContext);
64 ap.setNullableUserManager(um);
65 EventLoopGroup bossGroup = new NioEventLoopGroup();
66 NetconfSSHServer server = NetconfSSHServer.start(PORT, NetconfConfigUtil.getNetconfLocalAddress(),
69 sshServerThread = new Thread(server);
70 sshServerThread.setDaemon(true);
71 sshServerThread.start();
72 logger.info("SSH server on " + PORT);
76 public void connect() {
78 Connection conn = new Connection(HOST, PORT);
79 Assert.assertNotNull(conn);
80 logger.info("connecting to SSH server");
82 logger.info("authenticating ...");
83 boolean isAuthenticated = conn.authenticateWithPassword(USER, PASSWORD);
84 Assert.assertTrue(isAuthenticated);
85 } catch (Exception e) {
86 logger.error("Error while starting SSH server.", e);