Convert mdsal-netconf-ssh to OSGi DS
[netconf.git] / netconf / mdsal-netconf-ssh / src / test / java / org / opendaylight / 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.netconf.ssh.authentication;
9
10 import io.netty.channel.EventLoopGroup;
11 import io.netty.channel.nio.NioEventLoopGroup;
12 import java.io.File;
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.ssh.SshProxyServer;
27 import org.opendaylight.netconf.ssh.SshProxyServerConfigurationBuilder;
28 import org.opendaylight.netconf.util.NetconfConfiguration;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class SSHServerTest {
33     private static final String USER = "netconf";
34     private static final String PASSWORD = "netconf";
35     private static final String HOST = "127.0.0.1";
36     private static final int PORT = 1830;
37     private static final Logger LOG = LoggerFactory.getLogger(SSHServerTest.class);
38
39     private File sshKeyPair;
40     private SshProxyServer server;
41
42     private final ExecutorService nioExec = Executors.newFixedThreadPool(1);
43     private final EventLoopGroup clientGroup = new NioEventLoopGroup();
44     private final ScheduledExecutorService minaTimerEx = Executors.newScheduledThreadPool(1);
45
46     @Before
47     public void setUp() throws Exception {
48         sshKeyPair = Files.createTempFile("sshKeyPair", ".pem").toFile();
49         sshKeyPair.deleteOnExit();
50
51         LOG.info("Creating SSH server");
52
53         final InetSocketAddress addr = InetSocketAddress.createUnresolved(HOST, PORT);
54         server = new SshProxyServer(minaTimerEx, clientGroup, nioExec);
55         server.bind(new SshProxyServerConfigurationBuilder()
56                 .setBindingAddress(addr).setLocalAddress(NetconfConfiguration.NETCONF_LOCAL_ADDRESS)
57                 .setAuthenticator((username, password) -> true)
58                 .setKeyPairProvider(SecurityUtils.createGeneratorHostKeyProvider(sshKeyPair.toPath()))
59                 .setIdleTimeout(Integer.MAX_VALUE).createSshProxyServerConfiguration());
60         LOG.info("SSH server started on {}", PORT);
61     }
62
63     @Test
64     public void connect() throws Exception {
65         final SshClient sshClient = SshClient.setUpDefaultClient();
66         sshClient.start();
67         try {
68             final ConnectFuture connect = sshClient.connect(USER, HOST, PORT);
69             connect.await(30, TimeUnit.SECONDS);
70             org.junit.Assert.assertTrue(connect.isConnected());
71             final ClientSession session = connect.getSession();
72             session.addPasswordIdentity(PASSWORD);
73             final AuthFuture auth = session.auth();
74             auth.await(30, TimeUnit.SECONDS);
75             org.junit.Assert.assertTrue(auth.isSuccess());
76         } finally {
77             sshClient.close(true);
78             server.close();
79             clientGroup.shutdownGracefully().await();
80             minaTimerEx.shutdownNow();
81             nioExec.shutdownNow();
82         }
83     }
84 }