package com.github.jamesnetherton;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.file.Paths;

import org.apache.sshd.ClientChannel;
import org.apache.sshd.ClientSession;
import org.apache.sshd.SshClient;
import org.apache.sshd.client.future.ConnectFuture;
import org.apache.sshd.client.future.OpenFuture;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class SSHTest {

    private EmbeddedSSHServer sshServer;

    @Before
    public void before() throws Exception {
        sshServer = new EmbeddedSSHServer(Paths.get("target/sshd"));
        sshServer.start();
    }

    @After
    public void after() throws Exception {
        if (sshServer != null) {
            sshServer.stop();
        }
    }

    @Test
    public void testEcho() throws Exception {
        SshClient client = SshClient.setUpDefaultClient();
        ClientSession session = null;
        ClientChannel channel = null;

        try {
            client.start();

            ConnectFuture connectFuture = client.connect("admin", "localhost", 1100);
            connectFuture.await(5000);

            if (!connectFuture.isDone() || !connectFuture.isConnected()) {
                throw new IllegalStateException("Connect timeout period expired");
            }

            session = connectFuture.getSession();
            session.addPasswordIdentity("admin");
            session.auth().verify();

            ByteArrayInputStream in = new ByteArrayInputStream(new byte[]{0});
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ByteArrayOutputStream err = new ByteArrayOutputStream();

            channel = session.createExecChannel("echo Hello Kermit");
            channel.setIn(in);
            channel.setOut(out);
            channel.setErr(err);

            OpenFuture openFuture = channel.open();
            openFuture.await(5000);
            if (openFuture.isOpened()) {
                channel.waitFor(ClientChannel.CLOSED, 0);
            }

            Assert.assertEquals("Hello Kermit\n", new String(out.toByteArray()));
        } finally {
            if (channel != null) {
                channel.close(true);
            }
            // need to make sure the session is closed
            if (session != null) {
                session.close(false);
            }

            client.stop();
        }
    }
}

@@ -243,16 +243,65 @@
 Client;%0A
+import org.apache.sshd.client.future.AuthFuture;%0A
 import o
@@ -1430,24 +1430,295 @@
 tSession();%0A
+%0A            AuthFuture authFuture = session.authPassword(%22admin%22, %22admin%22);%0A            authFuture.await(5000);%0A%0A            if (!authFuture.isDone() %7C%7C authFuture.isFailure()) %7B%0A                throw new IllegalStateException(%22Authentication Failed%22);%0A            %7D%0A%0A//
             
@@ -1747,32 +1747,34 @@
 ntity(%22admin%22);%0A
+//
             sess
@@ -2009,24 +2009,120 @@
 tStream();%0A%0A
+            channel = session.createChannel(ClientChannel.CHANNEL_EXEC, %22echo Hello Kermit%22);%0A//
             

Test using deprecated API