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