38aa2e71ace1850cdb42a02e7b018e09ab387170
[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 io.netty.channel.EventLoopGroup;
16 import io.netty.channel.nio.NioEventLoopGroup;
17 import java.net.InetSocketAddress;
18 import java.nio.file.Files;
19 import java.util.concurrent.ExecutorService;
20 import java.util.concurrent.Executors;
21 import java.util.concurrent.ScheduledExecutorService;
22 import java.util.concurrent.TimeUnit;
23 import org.apache.sshd.ClientSession;
24 import org.apache.sshd.SshClient;
25 import org.apache.sshd.client.future.AuthFuture;
26 import org.apache.sshd.client.future.ConnectFuture;
27 import org.apache.sshd.server.PasswordAuthenticator;
28 import org.apache.sshd.server.keyprovider.PEMGeneratorHostKeyProvider;
29 import org.apache.sshd.server.session.ServerSession;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.opendaylight.controller.netconf.ssh.SshProxyServer;
35 import org.opendaylight.controller.netconf.ssh.SshProxyServerConfigurationBuilder;
36 import org.opendaylight.controller.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;
42
43
44 public class SSHServerTest {
45
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 logger = LoggerFactory.getLogger(SSHServerTest.class);
51
52     private SshProxyServer server;
53
54     @Mock
55     private BundleContext mockedContext;
56     private final ExecutorService nioExec = Executors.newFixedThreadPool(1);
57     private final EventLoopGroup clientGroup = new NioEventLoopGroup();
58     private final ScheduledExecutorService minaTimerEx = Executors.newScheduledThreadPool(1);
59
60     @Before
61     public void setUp() throws Exception {
62         MockitoAnnotations.initMocks(this);
63         doReturn(null).when(mockedContext).createFilter(anyString());
64         doNothing().when(mockedContext).addServiceListener(any(ServiceListener.class), anyString());
65         doReturn(new ServiceReference[0]).when(mockedContext).getServiceReferences(anyString(), anyString());
66
67         logger.info("Creating SSH server");
68
69         final InetSocketAddress addr = InetSocketAddress.createUnresolved(HOST, PORT);
70         server = new SshProxyServer(minaTimerEx, clientGroup, nioExec);
71         server.bind(
72                 new SshProxyServerConfigurationBuilder().setBindingAddress(addr).setLocalAddress(NetconfConfigUtil.getNetconfLocalAddress()).setAuthenticator(new PasswordAuthenticator() {
73                     @Override
74                     public boolean authenticate(final String username, final String password, final ServerSession session) {
75                         return true;
76                     }
77                 }).setKeyPairProvider(new PEMGeneratorHostKeyProvider(Files.createTempFile("prefix", "suffix").toAbsolutePath().toString())).setIdleTimeout(Integer.MAX_VALUE).createSshProxyServerConfiguration());
78         logger.info("SSH server started on " + PORT);
79     }
80
81     @Test
82     public void connect() throws Exception {
83         final SshClient sshClient = SshClient.setUpDefaultClient();
84         sshClient.start();
85         try {
86             final ConnectFuture connect = sshClient.connect(USER, HOST, PORT);
87             connect.await(30, TimeUnit.SECONDS);
88             org.junit.Assert.assertTrue(connect.isConnected());
89             final ClientSession session = connect.getSession();
90             session.addPasswordIdentity(PASSWORD);
91             final AuthFuture auth = session.auth();
92             auth.await(30, TimeUnit.SECONDS);
93             org.junit.Assert.assertTrue(auth.isSuccess());
94         } finally {
95             sshClient.close(true);
96             server.close();
97             clientGroup.shutdownGracefully().await();
98             minaTimerEx.shutdownNow();
99             nioExec.shutdownNow();
100         }
101     }
102
103 }