Bug 1636: Config Netconf Connector did not serialize service type
[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 ch.ethz.ssh2.Connection;
16 import io.netty.channel.EventLoopGroup;
17 import io.netty.channel.nio.NioEventLoopGroup;
18 import java.io.InputStream;
19 import java.net.InetSocketAddress;
20 import junit.framework.Assert;
21 import org.apache.commons.io.IOUtils;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.Mock;
25 import org.mockito.MockitoAnnotations;
26 import org.opendaylight.controller.netconf.StubUserManager;
27 import org.opendaylight.controller.netconf.ssh.NetconfSSHServer;
28 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil;
29 import org.osgi.framework.BundleContext;
30 import org.osgi.framework.ServiceListener;
31 import org.osgi.framework.ServiceReference;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35
36 public class SSHServerTest {
37
38     private static final String USER = "netconf";
39     private static final String PASSWORD = "netconf";
40     private static final String HOST = "127.0.0.1";
41     private static final int PORT = 1830;
42     private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 8383);
43     private static final Logger logger = LoggerFactory.getLogger(SSHServerTest.class);
44     private Thread sshServerThread;
45
46     @Mock
47     private BundleContext mockedContext;
48
49
50     @Before
51     public void setUp() throws Exception {
52         MockitoAnnotations.initMocks(this);
53         doReturn(null).when(mockedContext).createFilter(anyString());
54         doNothing().when(mockedContext).addServiceListener(any(ServiceListener.class), anyString());
55         doReturn(new ServiceReference[0]).when(mockedContext).getServiceReferences(anyString(), anyString());
56
57         logger.info("Creating SSH server");
58         StubUserManager um = new StubUserManager(USER, PASSWORD);
59         String pem;
60         try (InputStream is = getClass().getResourceAsStream("/RSA.pk")) {
61             pem = IOUtils.toString(is);
62         }
63         AuthProviderImpl ap = new AuthProviderImpl(pem, mockedContext);
64         ap.setNullableUserManager(um);
65         EventLoopGroup bossGroup = new NioEventLoopGroup();
66         NetconfSSHServer server = NetconfSSHServer.start(PORT, NetconfConfigUtil.getNetconfLocalAddress(),
67                 ap, bossGroup);
68
69         sshServerThread = new Thread(server);
70         sshServerThread.setDaemon(true);
71         sshServerThread.start();
72         logger.info("SSH server on " + PORT);
73     }
74
75     @Test
76     public void connect() {
77         try {
78             Connection conn = new Connection(HOST, PORT);
79             Assert.assertNotNull(conn);
80             logger.info("connecting to SSH server");
81             conn.connect();
82             logger.info("authenticating ...");
83             boolean isAuthenticated = conn.authenticateWithPassword(USER, PASSWORD);
84             Assert.assertTrue(isAuthenticated);
85         } catch (Exception e) {
86             logger.error("Error while starting SSH server.", e);
87         }
88
89     }
90
91 }