Do not start netconf-impl for css netconf endpoint
[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 static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.ArgumentMatchers.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.io.File;
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.client.SshClient;
25 import org.apache.sshd.client.future.AuthFuture;
26 import org.apache.sshd.client.future.ConnectFuture;
27 import org.apache.sshd.client.session.ClientSession;
28 import org.apache.sshd.common.util.security.SecurityUtils;
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.ssh.SshProxyServer;
34 import org.opendaylight.netconf.ssh.SshProxyServerConfigurationBuilder;
35 import org.opendaylight.netconf.util.NetconfConfiguration;
36 import org.osgi.framework.BundleContext;
37 import org.osgi.framework.ServiceListener;
38 import org.osgi.framework.ServiceReference;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42
43 public class SSHServerTest {
44
45     private static final String USER = "netconf";
46     private static final String PASSWORD = "netconf";
47     private static final String HOST = "127.0.0.1";
48     private static final int PORT = 1830;
49     private static final Logger LOG = LoggerFactory.getLogger(SSHServerTest.class);
50
51     private File sshKeyPair;
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         sshKeyPair = Files.createTempFile("sshKeyPair", ".pem").toFile();
63         sshKeyPair.deleteOnExit();
64
65         MockitoAnnotations.initMocks(this);
66         doReturn(null).when(mockedContext).createFilter(anyString());
67         doNothing().when(mockedContext).addServiceListener(any(ServiceListener.class), anyString());
68         doReturn(new ServiceReference[0]).when(mockedContext).getServiceReferences(anyString(), anyString());
69
70         LOG.info("Creating SSH server");
71
72         final InetSocketAddress addr = InetSocketAddress.createUnresolved(HOST, PORT);
73         server = new SshProxyServer(minaTimerEx, clientGroup, nioExec);
74         server.bind(new SshProxyServerConfigurationBuilder()
75                 .setBindingAddress(addr).setLocalAddress(NetconfConfiguration.NETCONF_LOCAL_ADDRESS)
76                 .setAuthenticator((username, password) -> true)
77                 .setKeyPairProvider(SecurityUtils.createGeneratorHostKeyProvider(sshKeyPair.toPath()))
78                 .setIdleTimeout(Integer.MAX_VALUE).createSshProxyServerConfiguration());
79         LOG.info("SSH server started on {}", PORT);
80     }
81
82     @Test
83     public void connect() throws Exception {
84         final SshClient sshClient = SshClient.setUpDefaultClient();
85         sshClient.start();
86         try {
87             final ConnectFuture connect = sshClient.connect(USER, HOST, PORT);
88             connect.await(30, TimeUnit.SECONDS);
89             org.junit.Assert.assertTrue(connect.isConnected());
90             final ClientSession session = connect.getSession();
91             session.addPasswordIdentity(PASSWORD);
92             final AuthFuture auth = session.auth();
93             auth.await(30, TimeUnit.SECONDS);
94             org.junit.Assert.assertTrue(auth.isSuccess());
95         } finally {
96             sshClient.close(true);
97             server.close();
98             clientGroup.shutdownGracefully().await();
99             minaTimerEx.shutdownNow();
100             nioExec.shutdownNow();
101         }
102     }
103
104 }