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;
10 import io.netty.channel.EventLoopGroup;
11 import io.netty.channel.nio.NioEventLoopGroup;
13 import java.net.InetSocketAddress;
14 import java.nio.file.Files;
15 import java.util.concurrent.ExecutorService;
16 import java.util.concurrent.Executors;
17 import java.util.concurrent.ScheduledExecutorService;
18 import java.util.concurrent.TimeUnit;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.netconf.shaded.sshd.client.SshClient;
22 import org.opendaylight.netconf.shaded.sshd.client.future.AuthFuture;
23 import org.opendaylight.netconf.shaded.sshd.client.future.ConnectFuture;
24 import org.opendaylight.netconf.shaded.sshd.client.session.ClientSession;
25 import org.opendaylight.netconf.shaded.sshd.common.util.security.SecurityUtils;
26 import org.opendaylight.netconf.util.NetconfConfiguration;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class SSHServerTest {
31 private static final String USER = "netconf";
32 private static final String PASSWORD = "netconf";
33 private static final String HOST = "127.0.0.1";
34 private static final int PORT = 1830;
35 private static final Logger LOG = LoggerFactory.getLogger(SSHServerTest.class);
37 private File sshKeyPair;
38 private SshProxyServer server;
40 private final ExecutorService nioExec = Executors.newFixedThreadPool(1);
41 private final EventLoopGroup clientGroup = new NioEventLoopGroup();
42 private final ScheduledExecutorService minaTimerEx = Executors.newScheduledThreadPool(1);
45 public void setUp() throws Exception {
46 sshKeyPair = Files.createTempFile("sshKeyPair", ".pem").toFile();
47 sshKeyPair.deleteOnExit();
49 LOG.info("Creating SSH server");
51 final InetSocketAddress addr = InetSocketAddress.createUnresolved(HOST, PORT);
52 server = new SshProxyServer(minaTimerEx, clientGroup, nioExec);
53 server.bind(new SshProxyServerConfigurationBuilder()
54 .setBindingAddress(addr).setLocalAddress(NetconfConfiguration.NETCONF_LOCAL_ADDRESS)
55 .setAuthenticator((username, password) -> true)
56 .setKeyPairProvider(SecurityUtils.createGeneratorHostKeyProvider(sshKeyPair.toPath()))
57 .setIdleTimeout(Integer.MAX_VALUE).createSshProxyServerConfiguration());
58 LOG.info("SSH server started on {}", PORT);
62 public void connect() throws Exception {
63 final SshClient sshClient = SshClient.setUpDefaultClient();
66 final ConnectFuture connect = sshClient.connect(USER, HOST, PORT);
67 connect.await(30, TimeUnit.SECONDS);
68 org.junit.Assert.assertTrue(connect.isConnected());
69 final ClientSession session = connect.getSession();
70 session.addPasswordIdentity(PASSWORD);
71 final AuthFuture auth = session.auth();
72 auth.await(30, TimeUnit.SECONDS);
73 org.junit.Assert.assertTrue(auth.isSuccess());
75 sshClient.close(true);
77 clientGroup.shutdownGracefully().await();
78 minaTimerEx.shutdownNow();
79 nioExec.shutdownNow();