X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-ssh%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fnetty%2FSSHTest.java;h=488c3701457039a022b1b0caed1a0d14e899641e;hp=2bda51b495c3505741ff9c697712cb3f98a2b1ee;hb=c0664e68c1408f269a5782f2dba4b1e9044164f6;hpb=c2fd5f62f3b80a7e7b4b7e167349ede433e785d6 diff --git a/opendaylight/netconf/netconf-ssh/src/test/java/org/opendaylight/controller/netconf/netty/SSHTest.java b/opendaylight/netconf/netconf-ssh/src/test/java/org/opendaylight/controller/netconf/netty/SSHTest.java index 2bda51b495..488c370145 100644 --- a/opendaylight/netconf/netconf-ssh/src/test/java/org/opendaylight/controller/netconf/netty/SSHTest.java +++ b/opendaylight/netconf/netconf-ssh/src/test/java/org/opendaylight/controller/netconf/netty/SSHTest.java @@ -8,12 +8,28 @@ package org.opendaylight.controller.netconf.netty; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; +import com.google.common.base.Stopwatch; +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioSocketChannel; +import io.netty.util.HashedWheelTimer; +import java.net.InetSocketAddress; +import java.util.concurrent.TimeUnit; +import org.junit.After; +import org.junit.Before; import org.junit.Test; +import org.opendaylight.controller.netconf.netty.EchoClientHandler.State; +import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.LoginPassword; +import org.opendaylight.controller.netconf.nettyutil.handler.ssh.client.SshHandler; import org.opendaylight.controller.netconf.ssh.NetconfSSHServer; import org.opendaylight.controller.netconf.ssh.authentication.AuthProvider; import org.opendaylight.controller.netconf.ssh.authentication.PEMGenerator; @@ -23,6 +39,21 @@ import org.slf4j.LoggerFactory; public class SSHTest { public static final Logger logger = LoggerFactory.getLogger(SSHTest.class); + public static final String AHOJ = "ahoj\n"; + private EventLoopGroup nettyGroup; + HashedWheelTimer hashedWheelTimer; + + @Before + public void setUp() throws Exception { + hashedWheelTimer = new HashedWheelTimer(); + nettyGroup = new NioEventLoopGroup(); + } + + @After + public void tearDown() throws Exception { + hashedWheelTimer.stop(); + nettyGroup.shutdownGracefully(); + } @Test public void test() throws Exception { @@ -30,10 +61,63 @@ public class SSHTest { AuthProvider authProvider = mock(AuthProvider.class); doReturn(PEMGenerator.generate().toCharArray()).when(authProvider).getPEMAsCharArray(); doReturn(true).when(authProvider).authenticated(anyString(), anyString()); - NetconfSSHServer thread = NetconfSSHServer.start(10831, NetconfConfigUtil.getNetconfLocalAddress(), authProvider, new NioEventLoopGroup()); - Thread.sleep(2000); - logger.info("Closing socket"); - thread.close(); - thread.join(); + NetconfSSHServer netconfSSHServer = NetconfSSHServer.start(10831, NetconfConfigUtil.getNetconfLocalAddress(), + authProvider, new NioEventLoopGroup()); + + InetSocketAddress address = netconfSSHServer.getLocalSocketAddress(); + final EchoClientHandler echoClientHandler = connectClient(address); + Stopwatch stopwatch = new Stopwatch().start(); + while(echoClientHandler.isConnected() == false && stopwatch.elapsed(TimeUnit.SECONDS) < 5) { + Thread.sleep(100); + } + assertTrue(echoClientHandler.isConnected()); + logger.info("connected, writing to client"); + echoClientHandler.write(AHOJ); + // check that server sent back the same string + stopwatch = stopwatch.reset().start(); + while (echoClientHandler.read().endsWith(AHOJ) == false && stopwatch.elapsed(TimeUnit.SECONDS) < 5) { + Thread.sleep(100); + } + try { + String read = echoClientHandler.read(); + assertTrue(read + " should end with " + AHOJ, read.endsWith(AHOJ)); + } finally { + logger.info("Closing socket"); + netconfSSHServer.close(); + netconfSSHServer.join(); + } } + + public EchoClientHandler connectClient(InetSocketAddress address) { + final EchoClientHandler echoClientHandler = new EchoClientHandler(); + ChannelInitializer channelInitializer = new ChannelInitializer() { + @Override + public void initChannel(NioSocketChannel ch) throws Exception { + ch.pipeline().addFirst(SshHandler.createForNetconfSubsystem(new LoginPassword("a", "a"))); + ch.pipeline().addLast(echoClientHandler); + } + }; + Bootstrap b = new Bootstrap(); + + b.group(nettyGroup) + .channel(NioSocketChannel.class) + .handler(channelInitializer); + + // Start the client. + b.connect(address).addListener(echoClientHandler); + return echoClientHandler; + } + + @Test + public void testClientWithoutServer() throws Exception { + InetSocketAddress address = new InetSocketAddress(12345); + final EchoClientHandler echoClientHandler = connectClient(address); + Stopwatch stopwatch = new Stopwatch().start(); + while(echoClientHandler.getState() == State.CONNECTING && stopwatch.elapsed(TimeUnit.SECONDS) < 5) { + Thread.sleep(100); + } + assertFalse(echoClientHandler.isConnected()); + assertEquals(State.FAILED_TO_CONNECT, echoClientHandler.getState()); + } + }