Cleanup temporary test files
[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.io.File;
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.PasswordAuthenticator;
29 import org.apache.sshd.server.keyprovider.PEMGeneratorHostKeyProvider;
30 import org.apache.sshd.server.session.ServerSession;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.opendaylight.controller.netconf.ssh.SshProxyServer;
36 import org.opendaylight.controller.netconf.ssh.SshProxyServerConfigurationBuilder;
37 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil;
38 import org.osgi.framework.BundleContext;
39 import org.osgi.framework.ServiceListener;
40 import org.osgi.framework.ServiceReference;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44
45 public class SSHServerTest {
46
47     private static final String USER = "netconf";
48     private static final String PASSWORD = "netconf";
49     private static final String HOST = "127.0.0.1";
50     private static final int PORT = 1830;
51     private static final Logger logger = LoggerFactory.getLogger(SSHServerTest.class);
52
53     private File sshKeyPair;
54     private SshProxyServer server;
55
56     @Mock
57     private BundleContext mockedContext;
58     private final ExecutorService nioExec = Executors.newFixedThreadPool(1);
59     private final EventLoopGroup clientGroup = new NioEventLoopGroup();
60     private final ScheduledExecutorService minaTimerEx = Executors.newScheduledThreadPool(1);
61
62     @Before
63     public void setUp() throws Exception {
64         sshKeyPair = Files.createTempFile("sshKeyPair", ".pem").toFile();
65         sshKeyPair.deleteOnExit();
66
67         MockitoAnnotations.initMocks(this);
68         doReturn(null).when(mockedContext).createFilter(anyString());
69         doNothing().when(mockedContext).addServiceListener(any(ServiceListener.class), anyString());
70         doReturn(new ServiceReference[0]).when(mockedContext).getServiceReferences(anyString(), anyString());
71
72         logger.info("Creating SSH server");
73
74         final InetSocketAddress addr = InetSocketAddress.createUnresolved(HOST, PORT);
75         server = new SshProxyServer(minaTimerEx, clientGroup, nioExec);
76         server.bind(
77                 new SshProxyServerConfigurationBuilder().setBindingAddress(addr).setLocalAddress(NetconfConfigUtil.getNetconfLocalAddress()).setAuthenticator(new PasswordAuthenticator() {
78                     @Override
79                     public boolean authenticate(final String username, final String password, final ServerSession session) {
80                         return true;
81                     }
82                 }).setKeyPairProvider(new PEMGeneratorHostKeyProvider(sshKeyPair.toPath().toAbsolutePath().toString())).setIdleTimeout(Integer.MAX_VALUE).createSshProxyServerConfiguration());
83         logger.info("SSH server started on " + PORT);
84     }
85
86     @Test
87     public void connect() throws Exception {
88         final SshClient sshClient = SshClient.setUpDefaultClient();
89         sshClient.start();
90         try {
91             final ConnectFuture connect = sshClient.connect(USER, HOST, PORT);
92             connect.await(30, TimeUnit.SECONDS);
93             org.junit.Assert.assertTrue(connect.isConnected());
94             final ClientSession session = connect.getSession();
95             session.addPasswordIdentity(PASSWORD);
96             final AuthFuture auth = session.auth();
97             auth.await(30, TimeUnit.SECONDS);
98             org.junit.Assert.assertTrue(auth.isSuccess());
99         } finally {
100             sshClient.close(true);
101             server.close();
102             clientGroup.shutdownGracefully().await();
103             minaTimerEx.shutdownNow();
104             nioExec.shutdownNow();
105         }
106     }
107
108 }