663a0b4a82c6ac3d480b7d1d498aa0339344d220
[controller.git] / opendaylight / netconf / netconf-ssh / src / test / java / org / opendaylight / controller / netconf / 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;
9
10 import ch.ethz.ssh2.Connection;
11 import junit.framework.Assert;
12 import org.apache.commons.io.IOUtils;
13 import org.junit.Test;
14 import org.opendaylight.controller.netconf.ssh.NetconfSSHServer;
15 import org.opendaylight.controller.netconf.ssh.authentication.AuthProvider;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 import java.io.InputStream;
20 import java.net.InetSocketAddress;
21
22
23 public class SSHServerTest {
24
25     private static final String USER = "netconf";
26     private static final String PASSWORD  = "netconf";
27     private static final String HOST = "127.0.0.1";
28     private static final int PORT = 1830;
29     private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 8383);
30     private static final Logger logger =  LoggerFactory.getLogger(SSHServerTest.class);
31     private Thread sshServerThread;
32
33
34
35
36     public void startSSHServer() throws Exception{
37         logger.info("Creating SSH server");
38         StubUserManager um = new StubUserManager(USER,PASSWORD);
39         String pem;
40         try(InputStream is = getClass().getResourceAsStream("/RSA.pk")) {
41             pem = IOUtils.toString(is);
42         }
43         AuthProvider ap = new AuthProvider(um, pem);
44         NetconfSSHServer server = NetconfSSHServer.start(PORT,tcpAddress,ap);
45         sshServerThread = new Thread(server);
46         sshServerThread.setDaemon(true);
47         sshServerThread.start();
48         logger.info("SSH server on");
49     }
50
51     @Test
52     public void connect(){
53         try {
54             this.startSSHServer();
55             Connection conn = new Connection(HOST,PORT);
56             Assert.assertNotNull(conn);
57             logger.info("connecting to SSH server");
58             conn.connect();
59             logger.info("authenticating ...");
60             boolean isAuthenticated = conn.authenticateWithPassword(USER,PASSWORD);
61             Assert.assertTrue(isAuthenticated);
62         } catch (Exception e) {
63             logger.error("Error while starting SSH server.", e);
64         }
65
66     }
67
68 }