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.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 io.netty.channel.EventLoopGroup;
16 import io.netty.channel.nio.NioEventLoopGroup;
18 import java.net.InetSocketAddress;
19 import java.nio.file.Files;
20 import java.util.concurrent.ExecutorService;
21 import java.util.concurrent.Executors;
22 import java.util.concurrent.ScheduledExecutorService;
23 import java.util.concurrent.TimeUnit;
24 import org.apache.sshd.ClientSession;
25 import org.apache.sshd.SshClient;
26 import org.apache.sshd.client.future.AuthFuture;
27 import org.apache.sshd.client.future.ConnectFuture;
28 import org.apache.sshd.server.keyprovider.PEMGeneratorHostKeyProvider;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.opendaylight.netconf.auth.AuthProvider;
34 import org.opendaylight.netconf.ssh.SshProxyServer;
35 import org.opendaylight.netconf.ssh.SshProxyServerConfigurationBuilder;
36 import org.opendaylight.netconf.util.osgi.NetconfConfigUtil;
37 import org.osgi.framework.BundleContext;
38 import org.osgi.framework.ServiceListener;
39 import org.osgi.framework.ServiceReference;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 public class SSHServerTest {
46 private static final String USER = "netconf";
47 private static final String PASSWORD = "netconf";
48 private static final String HOST = "127.0.0.1";
49 private static final int PORT = 1830;
50 private static final Logger LOG = LoggerFactory.getLogger(SSHServerTest.class);
52 private File sshKeyPair;
53 private SshProxyServer server;
56 private BundleContext mockedContext;
57 private final ExecutorService nioExec = Executors.newFixedThreadPool(1);
58 private final EventLoopGroup clientGroup = new NioEventLoopGroup();
59 private final ScheduledExecutorService minaTimerEx = Executors.newScheduledThreadPool(1);
62 public void setUp() throws Exception {
63 sshKeyPair = Files.createTempFile("sshKeyPair", ".pem").toFile();
64 sshKeyPair.deleteOnExit();
66 MockitoAnnotations.initMocks(this);
67 doReturn(null).when(mockedContext).createFilter(anyString());
68 doNothing().when(mockedContext).addServiceListener(any(ServiceListener.class), anyString());
69 doReturn(new ServiceReference[0]).when(mockedContext).getServiceReferences(anyString(), anyString());
71 LOG.info("Creating SSH server");
73 final InetSocketAddress addr = InetSocketAddress.createUnresolved(HOST, PORT);
74 server = new SshProxyServer(minaTimerEx, clientGroup, nioExec);
76 new SshProxyServerConfigurationBuilder().setBindingAddress(addr).setLocalAddress(NetconfConfigUtil.getNetconfLocalAddress()).setAuthenticator(new AuthProvider() {
78 public boolean authenticated(final String username, final String password) {
81 }).setKeyPairProvider(new PEMGeneratorHostKeyProvider(sshKeyPair.toPath().toAbsolutePath().toString())).setIdleTimeout(Integer.MAX_VALUE).createSshProxyServerConfiguration());
82 LOG.info("SSH server started on {}", PORT);
86 public void connect() throws Exception {
87 final SshClient sshClient = SshClient.setUpDefaultClient();
90 final ConnectFuture connect = sshClient.connect(USER, HOST, PORT);
91 connect.await(30, TimeUnit.SECONDS);
92 org.junit.Assert.assertTrue(connect.isConnected());
93 final ClientSession session = connect.getSession();
94 session.addPasswordIdentity(PASSWORD);
95 final AuthFuture auth = session.auth();
96 auth.await(30, TimeUnit.SECONDS);
97 org.junit.Assert.assertTrue(auth.isSuccess());
99 sshClient.close(true);
101 clientGroup.shutdownGracefully().await();
102 minaTimerEx.shutdownNow();
103 nioExec.shutdownNow();