http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/test/java/org/apache/sshd/WindowTest.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/test/java/org/apache/sshd/WindowTest.java b/sshd-core/src/test/java/org/apache/sshd/WindowTest.java index 045fad3..d56c2fb 100644 --- a/sshd-core/src/test/java/org/apache/sshd/WindowTest.java +++ b/sshd-core/src/test/java/org/apache/sshd/WindowTest.java @@ -101,20 +101,20 @@ public class WindowTest extends BaseTestSupport { sshd.setPublickeyAuthenticator(AcceptAllPublickeyAuthenticator.INSTANCE); sshd.setServiceFactories(Arrays.asList( new ServerUserAuthServiceFactory() { - @Override - public Service create(Session session) throws IOException { - return new ServerUserAuthService(session) { - @SuppressWarnings("synthetic-access") - @Override - public void process(int cmd, Buffer buffer) throws Exception { - authLatch.await(); - super.process(cmd, buffer); - } - }; - } - }, + @Override + public Service create(Session session) throws IOException { + return new ServerUserAuthService(session) { + @SuppressWarnings("synthetic-access") + @Override + public void process(int cmd, Buffer buffer) throws Exception { + authLatch.await(); + super.process(cmd, buffer); + } + }; + } + }, ServerConnectionServiceFactory.INSTANCE - )); + )); sshd.setChannelFactories(Arrays.<NamedFactory<Channel>>asList( new ChannelSessionFactory() { @Override @@ -161,36 +161,36 @@ public class WindowTest extends BaseTestSupport { FactoryManagerUtils.updateProperty(sshd, FactoryManager.WINDOW_SIZE, 1024); FactoryManagerUtils.updateProperty(client, FactoryManager.WINDOW_SIZE, 1024); client.start(); - - try(ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { + + try (ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPasswordIdentity(getCurrentTestName()); session.auth().verify(5L, TimeUnit.SECONDS); - try(ChannelShell channel = session.createShellChannel()) { + try (ChannelShell channel = session.createShellChannel()) { channel.open().verify(5L, TimeUnit.SECONDS); - - try(Channel serverChannel = sshd.getActiveSessions().iterator().next().getService(ServerConnectionService.class).getChannels().iterator().next()) { + + try (Channel serverChannel = sshd.getActiveSessions().iterator().next().getService(ServerConnectionService.class).getChannels().iterator().next()) { Window clientLocal = channel.getLocalWindow(); Window clientRemote = channel.getRemoteWindow(); Window serverLocal = serverChannel.getLocalWindow(); Window serverRemote = serverChannel.getRemoteWindow(); - + final String message = "0123456789"; final int nbMessages = 500; - - try(BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(channel.getInvertedIn())); - BufferedReader reader = new BufferedReader(new InputStreamReader(channel.getInvertedOut()))) { + + try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(channel.getInvertedIn())); + BufferedReader reader = new BufferedReader(new InputStreamReader(channel.getInvertedOut()))) { for (int i = 0; i < nbMessages; i++) { writer.write(message); writer.write("\n"); writer.flush(); - + waitForWindowNotEquals(clientLocal, serverRemote, "client local", "server remote"); - + String line = reader.readLine(); assertEquals("Mismatched message at line #" + i, message, line); - + waitForWindowEquals(clientLocal, serverRemote, "client local", "server remote"); waitForWindowEquals(clientRemote, serverLocal, "client remote", "server local"); } @@ -209,42 +209,42 @@ public class WindowTest extends BaseTestSupport { FactoryManagerUtils.updateProperty(client, FactoryManager.WINDOW_SIZE, 1024); client.start(); - - try(ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { + + try (ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPasswordIdentity(getCurrentTestName()); session.auth().verify(5L, TimeUnit.SECONDS); - - try(ChannelShell channel = session.createShellChannel(); - PipedInputStream inPis = new PipedInputStream(); - PipedOutputStream inPos = new PipedOutputStream(inPis); - PipedInputStream outPis = new PipedInputStream(); - PipedOutputStream outPos = new PipedOutputStream(outPis)) { + + try (ChannelShell channel = session.createShellChannel(); + PipedInputStream inPis = new PipedInputStream(); + PipedOutputStream inPos = new PipedOutputStream(inPis); + PipedInputStream outPis = new PipedInputStream(); + PipedOutputStream outPos = new PipedOutputStream(outPis)) { channel.setIn(inPis); channel.setOut(outPos); channel.open().verify(); - - try(Channel serverChannel = sshd.getActiveSessions().iterator().next().getService(ServerConnectionService.class).getChannels().iterator().next()) { + + try (Channel serverChannel = sshd.getActiveSessions().iterator().next().getService(ServerConnectionService.class).getChannels().iterator().next()) { Window clientLocal = channel.getLocalWindow(); Window clientRemote = channel.getRemoteWindow(); Window serverLocal = serverChannel.getLocalWindow(); Window serverRemote = serverChannel.getRemoteWindow(); - + final String message = "0123456789"; final int nbMessages = 500; - - try(BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(inPos)); - BufferedReader reader = new BufferedReader(new InputStreamReader(outPis))) { + + try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(inPos)); + BufferedReader reader = new BufferedReader(new InputStreamReader(outPis))) { for (int i = 0; i < nbMessages; i++) { writer.write(message); writer.write("\n"); writer.flush(); - + waitForWindowEquals(clientLocal, serverRemote, "client local", "server remote"); - + String line = reader.readLine(); assertEquals("Mismatched message at line #" + i, message, line); - + waitForWindowEquals(clientLocal, serverRemote, "client local", "server remote"); waitForWindowEquals(clientRemote, serverLocal, "client remote", "server local"); } @@ -263,36 +263,36 @@ public class WindowTest extends BaseTestSupport { FactoryManagerUtils.updateProperty(client, FactoryManager.WINDOW_SIZE, 1024); client.start(); - - try(ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { + + try (ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPasswordIdentity(getCurrentTestName()); session.auth().verify(5L, TimeUnit.SECONDS); - try(ChannelShell channel = session.createShellChannel()) { + try (ChannelShell channel = session.createShellChannel()) { channel.setStreaming(ClientChannel.Streaming.Async); channel.open().verify(5L, TimeUnit.SECONDS); - - try(Channel serverChannel = sshd.getActiveSessions().iterator().next().getService(ServerConnectionService.class).getChannels().iterator().next()) { + + try (Channel serverChannel = sshd.getActiveSessions().iterator().next().getService(ServerConnectionService.class).getChannels().iterator().next()) { Window clientLocal = channel.getLocalWindow(); Window clientRemote = channel.getRemoteWindow(); Window serverLocal = serverChannel.getLocalWindow(); Window serverRemote = serverChannel.getRemoteWindow(); - + final String message = "0123456789\n"; - final byte[] bytes=message.getBytes(StandardCharsets.UTF_8); + final byte[] bytes = message.getBytes(StandardCharsets.UTF_8); final int nbMessages = 500; for (int i = 0; i < nbMessages; i++) { Buffer buffer = new ByteArrayBuffer(bytes); channel.getAsyncIn().write(buffer).verify(); - + waitForWindowNotEquals(clientLocal, serverRemote, "client local", "server remote"); - + Buffer buf = new ByteArrayBuffer(16); IoReadFuture future = channel.getAsyncOut().read(buf); future.verify(); assertEquals("Mismatched available data at line #" + i, message.length(), buf.available()); assertEquals("Mismatched data at line #" + i, message, new String(buf.array(), buf.rpos(), buf.available())); - + waitForWindowEquals(clientLocal, serverRemote, "client local", "server remote"); waitForWindowEquals(clientRemote, serverLocal, "client remote", "server local"); } @@ -328,6 +328,7 @@ public class WindowTest extends BaseTestSupport { public Command create() { return new TestEchoShell(); } + public static class TestEchoShell extends EchoShell { public static CountDownLatch latch = new CountDownLatch(1);
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/test/java/org/apache/sshd/agent/AgentTest.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/test/java/org/apache/sshd/agent/AgentTest.java b/sshd-core/src/test/java/org/apache/sshd/agent/AgentTest.java index 6aaeb22..24cd4dc 100644 --- a/sshd-core/src/test/java/org/apache/sshd/agent/AgentTest.java +++ b/sshd-core/src/test/java/org/apache/sshd/agent/AgentTest.java @@ -18,8 +18,6 @@ */ package org.apache.sshd.agent; -import static org.apache.sshd.util.Utils.createTestKeyPairProvider; - import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; @@ -53,6 +51,8 @@ import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runners.MethodSorters; +import static org.apache.sshd.util.Utils.createTestKeyPairProvider; + @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class AgentTest extends BaseTestSupport { public AgentTest() { @@ -64,7 +64,7 @@ public class AgentTest extends BaseTestSupport { // TODO: revisit this test to work without BC Assume.assumeTrue("BouncyCastle not registered", SecurityUtils.isBouncyCastleRegistered()); - try(AgentServer agent = new AgentServer()) { + try (AgentServer agent = new AgentServer()) { String authSocket; try { authSocket = agent.start(); @@ -73,25 +73,25 @@ public class AgentTest extends BaseTestSupport { authSocket = null; } Assume.assumeTrue("Native library N/A", authSocket != null); - - try(SshAgent client = new AgentClient(authSocket)) { + + try (SshAgent client = new AgentClient(authSocket)) { List<Pair<PublicKey, String>> keys = client.getIdentities(); assertNotNull("No initial identities", keys); assertEquals("Unexpected initial identities size", 0, keys.size()); - + KeyPair k = Utils.createTestHostKeyProvider().loadKey(KeyPairProvider.SSH_RSA); client.addIdentity(k, ""); keys = client.getIdentities(); assertNotNull("No registered identities after add", keys); assertEquals("Mismatched registered keys size", 1, keys.size()); - + client.removeIdentity(k.getPublic()); keys = client.getIdentities(); assertNotNull("No registered identities after remove", keys); assertEquals("Registered keys size not empty", 0, keys.size()); - + client.removeAllIdentities(); - } + } } } @@ -107,7 +107,7 @@ public class AgentTest extends BaseTestSupport { KeyPair pair = createTestKeyPairProvider("dsaprivkey.pem").loadKey(KeyPairProvider.SSH_DSS); localAgentFactory.getAgent().addIdentity(pair, username); - try(SshServer sshd1 = SshServer.setUpDefaultServer()) { + try (SshServer sshd1 = SshServer.setUpDefaultServer()) { sshd1.setKeyPairProvider(Utils.createTestHostKeyProvider()); sshd1.setShellFactory(shellFactory); sshd1.setPasswordAuthenticator(BogusPasswordAuthenticator.INSTANCE); @@ -115,9 +115,9 @@ public class AgentTest extends BaseTestSupport { sshd1.setAgentFactory(agentFactory); sshd1.setTcpipForwardingFilter(AcceptAllForwardingFilter.INSTANCE); sshd1.start(); - + final int port1 = sshd1.getPort(); - try(SshServer sshd2 = SshServer.setUpDefaultServer()) { + try (SshServer sshd2 = SshServer.setUpDefaultServer()) { sshd2.setKeyPairProvider(Utils.createTestHostKeyProvider()); sshd2.setShellFactory(new TestEchoShellFactory()); sshd2.setPasswordAuthenticator(BogusPasswordAuthenticator.INSTANCE); @@ -125,56 +125,56 @@ public class AgentTest extends BaseTestSupport { sshd1.setTcpipForwardingFilter(AcceptAllForwardingFilter.INSTANCE); sshd2.setAgentFactory(new ProxyAgentFactory()); sshd2.start(); - + final int port2 = sshd2.getPort(); - try(SshClient client1 = SshClient.setUpDefaultClient()) { + try (SshClient client1 = SshClient.setUpDefaultClient()) { client1.setAgentFactory(localAgentFactory); client1.start(); - - try(ClientSession session1 = client1.connect(username, "localhost", port1).verify(7L, TimeUnit.SECONDS).getSession()) { + + try (ClientSession session1 = client1.connect(username, "localhost", port1).verify(7L, TimeUnit.SECONDS).getSession()) { session1.auth().verify(15L, TimeUnit.SECONDS); - try(ChannelShell channel1 = session1.createShellChannel(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - ByteArrayOutputStream err = new ByteArrayOutputStream()) { + try (ChannelShell channel1 = session1.createShellChannel(); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ByteArrayOutputStream err = new ByteArrayOutputStream()) { channel1.setOut(out); channel1.setErr(err); channel1.setAgentForwarding(true); channel1.open().verify(9L, TimeUnit.SECONDS); - - try(OutputStream pipedIn = channel1.getInvertedIn()) { + + try (OutputStream pipedIn = channel1.getInvertedIn()) { synchronized (shellFactory.shell) { System.out.println("Possibly waiting for remote shell to start"); if (!shellFactory.shell.started) { shellFactory.shell.wait(); } } - - try(SshClient client2 = SshClient.setUpDefaultClient()) { + + try (SshClient client2 = SshClient.setUpDefaultClient()) { client2.setAgentFactory(agentFactory); client2.getProperties().putAll(shellFactory.shell.getEnvironment().getEnv()); client2.start(); - - try(ClientSession session2 = client2.connect(username, "localhost", port2).verify(7L, TimeUnit.SECONDS).getSession()) { + + try (ClientSession session2 = client2.connect(username, "localhost", port2).verify(7L, TimeUnit.SECONDS).getSession()) { session2.auth().verify(15L, TimeUnit.SECONDS); - try(ChannelShell channel2 = session2.createShellChannel()) { + try (ChannelShell channel2 = session2.createShellChannel()) { channel2.setIn(shellFactory.shell.getIn()); channel2.setOut(shellFactory.shell.getOut()); channel2.setErr(shellFactory.shell.getErr()); channel2.setAgentForwarding(true); channel2.open().verify(9L, TimeUnit.SECONDS); - + pipedIn.write("foo\n".getBytes(StandardCharsets.UTF_8)); pipedIn.flush(); } - + Thread.sleep(1000); - + System.out.println(out.toString()); System.err.println(err.toString()); - + sshd1.stop(true); sshd2.stop(true); client1.stop(); http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/test/java/org/apache/sshd/client/ClientTest.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/test/java/org/apache/sshd/client/ClientTest.java b/sshd-core/src/test/java/org/apache/sshd/client/ClientTest.java index 7937c51..b3b694d 100644 --- a/sshd-core/src/test/java/org/apache/sshd/client/ClientTest.java +++ b/sshd-core/src/test/java/org/apache/sshd/client/ClientTest.java @@ -156,7 +156,7 @@ public class ClientTest extends BaseTestSupport { } }, ServerConnectionServiceFactory.INSTANCE - )); + )); sshd.setChannelFactories(Arrays.<NamedFactory<Channel>>asList( new ChannelSessionFactory() { @Override @@ -205,21 +205,21 @@ public class ClientTest extends BaseTestSupport { FactoryManagerUtils.updateProperty(client, FactoryManager.WINDOW_SIZE, 1024); client.start(); - try(ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { + try (ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPasswordIdentity(getCurrentTestName()); session.auth().verify(5L, TimeUnit.SECONDS); - try(final ChannelShell channel = session.createShellChannel()) { + try (final ChannelShell channel = session.createShellChannel()) { channel.setStreaming(ClientChannel.Streaming.Async); channel.open().verify(5L, TimeUnit.SECONDS); - + final byte[] message = "0123456789\n".getBytes(StandardCharsets.UTF_8); final int nbMessages = 1000; - - try(final ByteArrayOutputStream baosOut = new ByteArrayOutputStream(); - final ByteArrayOutputStream baosErr = new ByteArrayOutputStream()) { + + try (final ByteArrayOutputStream baosOut = new ByteArrayOutputStream(); + final ByteArrayOutputStream baosErr = new ByteArrayOutputStream()) { final AtomicInteger writes = new AtomicInteger(nbMessages); - + channel.getAsyncIn().write(new ByteArrayBuffer(message)) .addListener(new SshFutureListener<IoWriteFuture>() { @Override @@ -280,12 +280,12 @@ public class ClientTest extends BaseTestSupport { } } }); - + channel.waitFor(ClientChannel.CLOSED, 0); - + assertEquals(nbMessages * message.length, baosOut.size()); } - } + } client.close(true); } finally { @@ -297,13 +297,13 @@ public class ClientTest extends BaseTestSupport { public void testCommandDeadlock() throws Exception { client.start(); - try(ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { + try (ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPasswordIdentity(getCurrentTestName()); session.auth().verify(5L, TimeUnit.SECONDS); - - try(ChannelExec channel = session.createExecChannel(getCurrentTestName()); - OutputStream stdout = new NoCloseOutputStream(System.out); - OutputStream stderr = new NoCloseOutputStream(System.err)) { + + try (ChannelExec channel = session.createExecChannel(getCurrentTestName()); + OutputStream stdout = new NoCloseOutputStream(System.out); + OutputStream stderr = new NoCloseOutputStream(System.err)) { channel.setOut(stdout); channel.setErr(stderr); @@ -330,43 +330,43 @@ public class ClientTest extends BaseTestSupport { public void testClient() throws Exception { client.start(); - try(ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { + try (ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPasswordIdentity(getCurrentTestName()); session.auth().verify(5L, TimeUnit.SECONDS); - - try(ClientChannel channel = session.createShellChannel(); - ByteArrayOutputStream sent = new ByteArrayOutputStream(); - PipedOutputStream pipedIn = new PipedOutputStream(); - PipedInputStream pipedOut = new PipedInputStream(pipedIn)) { + + try (ClientChannel channel = session.createShellChannel(); + ByteArrayOutputStream sent = new ByteArrayOutputStream(); + PipedOutputStream pipedIn = new PipedOutputStream(); + PipedInputStream pipedOut = new PipedInputStream(pipedIn)) { channel.setIn(pipedOut); - try(OutputStream teeOut = new TeeOutputStream(sent, pipedIn); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - ByteArrayOutputStream err = new ByteArrayOutputStream()) { + try (OutputStream teeOut = new TeeOutputStream(sent, pipedIn); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ByteArrayOutputStream err = new ByteArrayOutputStream()) { channel.setOut(out); channel.setErr(err); channel.open(); - + teeOut.write("this is my command\n".getBytes(StandardCharsets.UTF_8)); teeOut.flush(); - + StringBuilder sb = new StringBuilder(); for (int i = 0; i < 1000; i++) { sb.append("0123456789"); } sb.append("\n"); teeOut.write(sb.toString().getBytes(StandardCharsets.UTF_8)); - + teeOut.write("exit\n".getBytes(StandardCharsets.UTF_8)); teeOut.flush(); - + channel.waitFor(ClientChannel.CLOSED, 0); - + channel.close(false); client.stop(); - + assertArrayEquals(sent.toByteArray(), out.toByteArray()); } } @@ -378,40 +378,40 @@ public class ClientTest extends BaseTestSupport { @Test public void testClientInverted() throws Exception { client.start(); - - try(ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { + + try (ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPasswordIdentity(getCurrentTestName()); session.auth().verify(5L, TimeUnit.SECONDS); - - try(ClientChannel channel = session.createShellChannel(); - ByteArrayOutputStream sent = new ByteArrayOutputStream(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - ByteArrayOutputStream err = new ByteArrayOutputStream()) { + + try (ClientChannel channel = session.createShellChannel(); + ByteArrayOutputStream sent = new ByteArrayOutputStream(); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ByteArrayOutputStream err = new ByteArrayOutputStream()) { channel.setOut(out); channel.setErr(err); channel.open().verify(9L, TimeUnit.SECONDS); - - try(OutputStream pipedIn = new TeeOutputStream(sent, channel.getInvertedIn())) { + + try (OutputStream pipedIn = new TeeOutputStream(sent, channel.getInvertedIn())) { pipedIn.write("this is my command\n".getBytes(StandardCharsets.UTF_8)); pipedIn.flush(); - + StringBuilder sb = new StringBuilder(); for (int i = 0; i < 1000; i++) { sb.append("0123456789"); } sb.append("\n"); pipedIn.write(sb.toString().getBytes(StandardCharsets.UTF_8)); - + pipedIn.write("exit\n".getBytes(StandardCharsets.UTF_8)); pipedIn.flush(); } - + channel.waitFor(ClientChannel.CLOSED, 0); - + channel.close(false); client.stop(); - + assertArrayEquals(sent.toByteArray(), out.toByteArray()); } } finally { @@ -422,15 +422,15 @@ public class ClientTest extends BaseTestSupport { @Test public void testClientWithCustomChannel() throws Exception { client.start(); - - try(ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { + + try (ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPasswordIdentity(getCurrentTestName()); session.auth().verify(5L, TimeUnit.SECONDS); - - try(ChannelShell channel = new ChannelShell(); - ByteArrayOutputStream sent = new ByteArrayOutputStream(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - ByteArrayOutputStream err = new ByteArrayOutputStream()) { + + try (ChannelShell channel = new ChannelShell(); + ByteArrayOutputStream sent = new ByteArrayOutputStream(); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ByteArrayOutputStream err = new ByteArrayOutputStream()) { session.getService(ConnectionService.class).registerChannel(channel); channel.setOut(out); @@ -446,40 +446,40 @@ public class ClientTest extends BaseTestSupport { @Test public void testClientClosingStream() throws Exception { client.start(); - - try(ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { + + try (ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPasswordIdentity(getCurrentTestName()); session.auth().verify(5L, TimeUnit.SECONDS); - - try(ClientChannel channel = session.createShellChannel(); - ByteArrayOutputStream sent = new ByteArrayOutputStream(); - PipedOutputStream pipedIn = new PipedOutputStream(); - InputStream inPipe = new PipedInputStream(pipedIn); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - ByteArrayOutputStream err = new ByteArrayOutputStream()) { + + try (ClientChannel channel = session.createShellChannel(); + ByteArrayOutputStream sent = new ByteArrayOutputStream(); + PipedOutputStream pipedIn = new PipedOutputStream(); + InputStream inPipe = new PipedInputStream(pipedIn); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ByteArrayOutputStream err = new ByteArrayOutputStream()) { channel.setIn(inPipe); channel.setOut(out); channel.setErr(err); channel.open(); - try(OutputStream teeOut = new TeeOutputStream(sent, pipedIn)) { + try (OutputStream teeOut = new TeeOutputStream(sent, pipedIn)) { teeOut.write("this is my command\n".getBytes(StandardCharsets.UTF_8)); teeOut.flush(); - + StringBuilder sb = new StringBuilder(); for (int i = 0; i < 1000; i++) { sb.append("0123456789"); } sb.append("\n"); teeOut.write(sb.toString().getBytes(StandardCharsets.UTF_8)); - } - + } + channel.waitFor(ClientChannel.CLOSED, 0); - + channel.close(false); client.stop(); - + assertArrayEquals(sent.toByteArray(), out.toByteArray()); } } finally { @@ -495,28 +495,28 @@ public class ClientTest extends BaseTestSupport { // FactoryManagerUtils.updateProperty(sshd, SshServer.WINDOW_SIZE, 0x20000); // FactoryManagerUtils.updateProperty(sshd, SshServer.MAX_PACKET_SIZE, 0x1000); client.start(); - - try(ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { + + try (ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPasswordIdentity(getCurrentTestName()); session.auth().verify(5L, TimeUnit.SECONDS); - try(ClientChannel channel = session.createShellChannel(); - ByteArrayOutputStream sent = new ByteArrayOutputStream(); - PipedOutputStream pipedIn = new PipedOutputStream(); - InputStream inPipe = new PipedInputStream(pipedIn); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - ByteArrayOutputStream err = new ByteArrayOutputStream()) { + try (ClientChannel channel = session.createShellChannel(); + ByteArrayOutputStream sent = new ByteArrayOutputStream(); + PipedOutputStream pipedIn = new PipedOutputStream(); + InputStream inPipe = new PipedInputStream(pipedIn); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ByteArrayOutputStream err = new ByteArrayOutputStream()) { channel.setIn(inPipe); channel.setOut(out); channel.setErr(err); channel.open().verify(9L, TimeUnit.SECONDS); - - + + int bytes = 0; byte[] data = "01234567890123456789012345678901234567890123456789\n".getBytes(StandardCharsets.UTF_8); long t0 = System.currentTimeMillis(); - try(OutputStream teeOut = new TeeOutputStream(sent, pipedIn)) { + try (OutputStream teeOut = new TeeOutputStream(sent, pipedIn)) { for (int i = 0; i < 10000; i++) { teeOut.write(data); teeOut.flush(); @@ -527,18 +527,18 @@ public class ClientTest extends BaseTestSupport { } teeOut.write("exit\n".getBytes(StandardCharsets.UTF_8)); teeOut.flush(); - } + } long t1 = System.currentTimeMillis(); - + System.out.println("Sent " + (bytes / 1024) + " Kb in " + (t1 - t0) + " ms"); - + System.out.println("Waiting for channel to be closed"); - + channel.waitFor(ClientChannel.CLOSED, 0); - + channel.close(false); client.stop(); - + assertArrayEquals(sent.toByteArray(), out.toByteArray()); //assertArrayEquals(sent.toByteArray(), out.toByteArray()); } @@ -550,18 +550,18 @@ public class ClientTest extends BaseTestSupport { @Test(expected = SshException.class) public void testOpenChannelOnClosedSession() throws Exception { client.start(); - - try(ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { + + try (ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPasswordIdentity(getCurrentTestName()); session.auth().verify(5L, TimeUnit.SECONDS); - - try(ClientChannel channel = session.createShellChannel()) { + + try (ClientChannel channel = session.createShellChannel()) { session.close(false); - - try(PipedOutputStream pipedIn = new PipedOutputStream(); - InputStream inPipe = new PipedInputStream(pipedIn); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - ByteArrayOutputStream err = new ByteArrayOutputStream()) { + + try (PipedOutputStream pipedIn = new PipedOutputStream(); + InputStream inPipe = new PipedInputStream(pipedIn); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ByteArrayOutputStream err = new ByteArrayOutputStream()) { channel.setIn(inPipe); channel.setOut(out); @@ -576,8 +576,8 @@ public class ClientTest extends BaseTestSupport { public void testCloseBeforeAuthSucceed() throws Exception { authLatch = new CountDownLatch(1); client.start(); - - try(ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { + + try (ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPasswordIdentity(getCurrentTestName()); AuthFuture authFuture = session.auth(); @@ -595,15 +595,15 @@ public class ClientTest extends BaseTestSupport { @Test public void testCloseCleanBeforeChannelOpened() throws Exception { client.start(); - - try(ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { + + try (ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPasswordIdentity(getCurrentTestName()); session.auth().verify(5L, TimeUnit.SECONDS); - try(ClientChannel channel = session.createShellChannel(); - InputStream inp = new ByteArrayInputStream(GenericUtils.EMPTY_BYTE_ARRAY); - OutputStream out = new ByteArrayOutputStream(); - OutputStream err = new ByteArrayOutputStream()) { + try (ClientChannel channel = session.createShellChannel(); + InputStream inp = new ByteArrayInputStream(GenericUtils.EMPTY_BYTE_ARRAY); + OutputStream out = new ByteArrayOutputStream(); + OutputStream err = new ByteArrayOutputStream()) { channel.setIn(inp); channel.setOut(out); @@ -626,14 +626,14 @@ public class ClientTest extends BaseTestSupport { channelLatch = new CountDownLatch(1); client.start(); - try(ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { + try (ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPasswordIdentity(getCurrentTestName()); session.auth().verify(5L, TimeUnit.SECONDS); - try(ClientChannel channel = session.createShellChannel(); - InputStream inp = new ByteArrayInputStream(GenericUtils.EMPTY_BYTE_ARRAY); - OutputStream out = new ByteArrayOutputStream(); - OutputStream err = new ByteArrayOutputStream()) { + try (ClientChannel channel = session.createShellChannel(); + InputStream inp = new ByteArrayInputStream(GenericUtils.EMPTY_BYTE_ARRAY); + OutputStream out = new ByteArrayOutputStream(); + OutputStream err = new ByteArrayOutputStream()) { channel.setIn(inp); channel.setOut(out); @@ -655,8 +655,8 @@ public class ClientTest extends BaseTestSupport { @Test public void testPublicKeyAuth() throws Exception { client.start(); - - try(ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { + + try (ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { KeyPair pair = Utils.createTestHostKeyProvider().loadKey(KeyPairProvider.SSH_RSA); session.addPublicKeyIdentity(pair); session.auth().verify(5L, TimeUnit.SECONDS); @@ -669,8 +669,8 @@ public class ClientTest extends BaseTestSupport { public void testPublicKeyAuthNew() throws Exception { client.setUserAuthFactories(Arrays.<NamedFactory<UserAuth>>asList(UserAuthPublicKeyFactory.INSTANCE)); client.start(); - - try(ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { + + try (ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPublicKeyIdentity(Utils.createTestHostKeyProvider().loadKey(KeyPairProvider.SSH_RSA)); session.auth().verify(5L, TimeUnit.SECONDS); } finally { @@ -693,7 +693,7 @@ public class ClientTest extends BaseTestSupport { SimpleGeneratorHostKeyProvider provider = new SimpleGeneratorHostKeyProvider(); provider.setAlgorithm("RSA"); - try(ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { + try (ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPublicKeyIdentity(provider.loadKey(KeyPairProvider.SSH_RSA)); session.addPublicKeyIdentity(pair); session.auth().verify(5L, TimeUnit.SECONDS); @@ -706,8 +706,8 @@ public class ClientTest extends BaseTestSupport { public void testPasswordAuthNew() throws Exception { client.setUserAuthFactories(Arrays.<NamedFactory<UserAuth>>asList(new UserAuthPasswordFactory())); client.start(); - - try(ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { + + try (ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPasswordIdentity(getCurrentTestName()); session.auth().verify(5L, TimeUnit.SECONDS); } finally { @@ -719,8 +719,8 @@ public class ClientTest extends BaseTestSupport { public void testPasswordAuthNewWithFailureOnFirstIdentity() throws Exception { client.setUserAuthFactories(Arrays.<NamedFactory<UserAuth>>asList(new UserAuthPasswordFactory())); client.start(); - - try(ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { + + try (ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPasswordIdentity(getClass().getSimpleName()); session.addPasswordIdentity(getCurrentTestName()); session.auth().verify(5L, TimeUnit.SECONDS); @@ -733,8 +733,8 @@ public class ClientTest extends BaseTestSupport { public void testKeyboardInteractiveAuthNew() throws Exception { client.setUserAuthFactories(Arrays.<NamedFactory<UserAuth>>asList(UserAuthKeyboardInteractiveFactory.INSTANCE)); client.start(); - - try(ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { + + try (ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPasswordIdentity(getCurrentTestName()); session.auth().verify(5L, TimeUnit.SECONDS); } finally { @@ -746,8 +746,8 @@ public class ClientTest extends BaseTestSupport { public void testKeyboardInteractiveAuthNewWithFailureOnFirstIdentity() throws Exception { client.setUserAuthFactories(Arrays.<NamedFactory<UserAuth>>asList(UserAuthKeyboardInteractiveFactory.INSTANCE)); client.start(); - - try(ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { + + try (ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPasswordIdentity(getClass().getSimpleName()); session.addPasswordIdentity(getCurrentTestName()); session.auth().verify(5L, TimeUnit.SECONDS); @@ -760,56 +760,56 @@ public class ClientTest extends BaseTestSupport { public void testKeyboardInteractivePasswordPromptLocationIndependence() throws Exception { final Collection<String> mismatchedPrompts = new LinkedList<String>(); client.setUserAuthFactories(Arrays.<NamedFactory<UserAuth>>asList(new UserAuthKeyboardInteractiveFactory() { - @Override - public UserAuth create() { - return new UserAuthKeyboardInteractive() { - @Override - protected boolean useCurrentPassword(String password, String name, String instruction, String lang, String[] prompt, boolean[] echo) { - boolean expected = GenericUtils.length(password) > 0; - boolean actual = super.useCurrentPassword(password, name, instruction, lang, prompt, echo); - if (expected != actual) { - System.err.println("Mismatched usage result for prompt=" + prompt[0] + ": expected=" + expected + ", actual=actual"); - mismatchedPrompts.add(prompt[0]); - } - return actual; + @Override + public UserAuth create() { + return new UserAuthKeyboardInteractive() { + @Override + protected boolean useCurrentPassword(String password, String name, String instruction, String lang, String[] prompt, boolean[] echo) { + boolean expected = GenericUtils.length(password) > 0; + boolean actual = super.useCurrentPassword(password, name, instruction, lang, prompt, echo); + if (expected != actual) { + System.err.println("Mismatched usage result for prompt=" + prompt[0] + ": expected=" + expected + ", actual=actual"); + mismatchedPrompts.add(prompt[0]); } - }; - } - })); + return actual; + } + }; + } + })); client.start(); - final Transformer<String,String> stripper = new Transformer<String,String>() { - @Override - public String transform(String input) { - int pos = GenericUtils.isEmpty(input) ? (-1) : input.lastIndexOf(':'); - if (pos < 0) { - return input; - } else { - return input.substring(0, pos); - } + final Transformer<String, String> stripper = new Transformer<String, String>() { + @Override + public String transform(String input) { + int pos = GenericUtils.isEmpty(input) ? (-1) : input.lastIndexOf(':'); + if (pos < 0) { + return input; + } else { + return input.substring(0, pos); } - }; - final List<Transformer<String,String>> xformers = - Collections.unmodifiableList(Arrays.<Transformer<String,String>>asList( - new Transformer<String,String>() { // prefixed - @Override - public String transform(String input) { - return getCurrentTestName() + " " + input; - } - }, - new Transformer<String,String>() { // suffixed - @Override - public String transform(String input) { - return stripper.transform(input) + " " + getCurrentTestName() + ":"; - } - }, - new Transformer<String,String>() { // infix - @Override - public String transform(String input) { - return getCurrentTestName() + " " + stripper.transform(input) + " " + getCurrentTestName() + ":"; - } - } - )); + } + }; + final List<Transformer<String, String>> xformers = + Collections.unmodifiableList(Arrays.<Transformer<String, String>>asList( + new Transformer<String, String>() { // prefixed + @Override + public String transform(String input) { + return getCurrentTestName() + " " + input; + } + }, + new Transformer<String, String>() { // suffixed + @Override + public String transform(String input) { + return stripper.transform(input) + " " + getCurrentTestName() + ":"; + } + }, + new Transformer<String, String>() { // infix + @Override + public String transform(String input) { + return getCurrentTestName() + " " + stripper.transform(input) + " " + getCurrentTestName() + ":"; + } + } + )); sshd.setUserAuthFactories(Arrays.<NamedFactory<org.apache.sshd.server.auth.UserAuth>>asList( new org.apache.sshd.server.auth.UserAuthKeyboardInteractiveFactory() { private int xformerIndex; @@ -817,13 +817,13 @@ public class ClientTest extends BaseTestSupport { @Override public org.apache.sshd.server.auth.UserAuth create() { return new org.apache.sshd.server.auth.UserAuthKeyboardInteractive() { - + @SuppressWarnings("synthetic-access") @Override protected String getInteractionPrompt() { String original = super.getInteractionPrompt(); if (xformerIndex < xformers.size()) { - Transformer<String,String> x = xformers.get(xformerIndex); + Transformer<String, String> x = xformers.get(xformerIndex); xformerIndex++; return x.transform(original); } else { @@ -832,14 +832,14 @@ public class ClientTest extends BaseTestSupport { } }; } - })); + })); try { for (int index = 0; index < xformers.size(); index++) { - try(ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7, TimeUnit.SECONDS).getSession()) { + try (ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7, TimeUnit.SECONDS).getSession()) { String password = "bad-" + getCurrentTestName() + "-" + index; session.addPasswordIdentity(password); - + AuthFuture future = session.auth(); assertTrue("Failed to verify password=" + password + " in time", future.await(5L, TimeUnit.SECONDS)); assertFalse("Unexpected success for password=" + password, future.isSuccess()); @@ -847,7 +847,7 @@ public class ClientTest extends BaseTestSupport { } } - try(ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { + try (ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPasswordIdentity(getCurrentTestName()); session.auth().verify(5L, TimeUnit.SECONDS); assertTrue("Mismatched prompts evaluation results", mismatchedPrompts.isEmpty()); @@ -869,15 +869,16 @@ public class ClientTest extends BaseTestSupport { public void welcome(String banner) { // ignored } + @Override public String[] interactive(String destination, String name, String instruction, String lang, String[] prompt, boolean[] echo) { count.incrementAndGet(); - return new String[] { "bad" }; + return new String[]{"bad"}; } }); client.start(); - - try(ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { + + try (ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { AuthFuture future = session.auth(); future.await(); assertTrue("Unexpected authentication success", future.isFailure()); @@ -894,23 +895,23 @@ public class ClientTest extends BaseTestSupport { FactoryManagerUtils.updateProperty(client, ClientFactoryManager.PASSWORD_PROMPTS, MAX_PROMPTS); client.setUserAuthFactories(Arrays - .<NamedFactory<UserAuth>> asList(UserAuthKeyboardInteractiveFactory.INSTANCE)); + .<NamedFactory<UserAuth>>asList(UserAuthKeyboardInteractiveFactory.INSTANCE)); client.start(); - try(ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { + try (ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { session.setUserInteraction(new UserInteraction() { - @Override - public void welcome(String banner) { - // ignored - } - - @Override - public String[] interactive(String destination, String name, String instruction, String lang, - String[] prompt, boolean[] echo) { - count.incrementAndGet(); - return new String[] { getCurrentTestName() }; - } - }); + @Override + public void welcome(String banner) { + // ignored + } + + @Override + public String[] interactive(String destination, String name, String instruction, String lang, + String[] prompt, boolean[] echo) { + count.incrementAndGet(); + return new String[]{getCurrentTestName()}; + } + }); AuthFuture future = session.auth(); future.await(); assertTrue("Authentication not marked as success", future.isSuccess()); @@ -927,21 +928,21 @@ public class ClientTest extends BaseTestSupport { final int MAX_PROMPTS = 3; FactoryManagerUtils.updateProperty(client, ClientFactoryManager.PASSWORD_PROMPTS, MAX_PROMPTS); client.setUserAuthFactories(Arrays - .<NamedFactory<UserAuth>> asList(new UserAuthKeyboardInteractiveFactory())); + .<NamedFactory<UserAuth>>asList(new UserAuthKeyboardInteractiveFactory())); client.start(); - - try(ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { + + try (ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { session.setUserInteraction(new UserInteraction() { @Override public void welcome(String banner) { // ignored } - + @Override public String[] interactive(String destination, String name, String instruction, String lang, String[] prompt, boolean[] echo) { int attemptId = count.incrementAndGet(); - return new String[] { "bad#" + attemptId }; + return new String[]{"bad#" + attemptId}; } }); AuthFuture future = session.auth(); @@ -958,23 +959,23 @@ public class ClientTest extends BaseTestSupport { TestEchoShellFactory.TestEchoShell.latch = new CountDownLatch(1); try { client.start(); - - try(ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { + + try (ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPasswordIdentity(getCurrentTestName()); session.auth().verify(5L, TimeUnit.SECONDS); - - try(ClientChannel channel = session.createShellChannel(); - PipedOutputStream pipedIn = new PipedOutputStream(); - InputStream inPipe = new PipedInputStream(pipedIn); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - ByteArrayOutputStream err = new ByteArrayOutputStream()) { + + try (ClientChannel channel = session.createShellChannel(); + PipedOutputStream pipedIn = new PipedOutputStream(); + InputStream inPipe = new PipedInputStream(pipedIn); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ByteArrayOutputStream err = new ByteArrayOutputStream()) { channel.setIn(inPipe); channel.setOut(out); channel.setErr(err); channel.open().verify(9L, TimeUnit.SECONDS); - - // ((AbstractSession) session).disconnect(SshConstants.SSH2_DISCONNECT_BY_APPLICATION, "Cancel"); + + // ((AbstractSession) session).disconnect(SshConstants.SSH2_DISCONNECT_BY_APPLICATION, "Cancel"); AbstractSession cs = (AbstractSession) session; Buffer buffer = cs.createBuffer(SshConstants.SSH_MSG_DISCONNECT); buffer.putInt(SshConstants.SSH2_DISCONNECT_BY_APPLICATION); @@ -983,7 +984,7 @@ public class ClientTest extends BaseTestSupport { IoWriteFuture f = cs.writePacket(buffer); assertTrue("Packet writing not completed in time", f.await(11L, TimeUnit.SECONDS)); suspend(cs.getIoSession()); - + TestEchoShellFactory.TestEchoShell.latch.await(); } } finally { @@ -1012,8 +1013,8 @@ public class ClientTest extends BaseTestSupport { } ); client.start(); - - try(ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { + + try (ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { session.waitFor(ClientSession.WAIT_AUTH, TimeUnit.SECONDS.toMillis(10L)); assertTrue(ok.get()); } finally { @@ -1026,13 +1027,13 @@ public class ClientTest extends BaseTestSupport { sshd.getCipherFactories().add(BuiltinCiphers.none); client.getCipherFactories().add(BuiltinCiphers.none); client.start(); - - try(ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { + + try (ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPasswordIdentity(getCurrentTestName()); session.auth().verify(5L, TimeUnit.SECONDS); assertTrue("Failed to switch to NONE cipher on time", session.switchToNoneCipher().await(5L, TimeUnit.SECONDS)); - - try(ClientChannel channel = session.createSubsystemChannel(SftpConstants.SFTP_SUBSYSTEM_NAME)) { + + try (ClientChannel channel = session.createSubsystemChannel(SftpConstants.SFTP_SUBSYSTEM_NAME)) { channel.open().verify(5L, TimeUnit.SECONDS); } } finally { @@ -1045,14 +1046,14 @@ public class ClientTest extends BaseTestSupport { client.start(); Collection<ClientChannel> channels = new LinkedList<>(); - try(ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { + try (ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPasswordIdentity(getCurrentTestName()); session.auth().verify(5L, TimeUnit.SECONDS); - + channels.add(session.createChannel(ClientChannel.CHANNEL_SUBSYSTEM, SftpConstants.SFTP_SUBSYSTEM_NAME)); channels.add(session.createChannel(ClientChannel.CHANNEL_EXEC, getCurrentTestName())); channels.add(session.createChannel(ClientChannel.CHANNEL_SHELL, getClass().getSimpleName())); - + Set<Integer> ids = new HashSet<Integer>(channels.size()); for (ClientChannel c : channels) { int id = ((AbstractChannel) c).getId(); @@ -1062,7 +1063,7 @@ public class ClientTest extends BaseTestSupport { for (Closeable c : channels) { try { c.close(); - } catch(IOException e) { + } catch (IOException e) { // ignored } } @@ -1083,6 +1084,7 @@ public class ClientTest extends BaseTestSupport { public Command create() { return new TestEchoShell(); } + public static class TestEchoShell extends EchoShell { public static CountDownLatch latch = new CountDownLatch(1); http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/test/java/org/apache/sshd/client/SshClientMain.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/test/java/org/apache/sshd/client/SshClientMain.java b/sshd-core/src/test/java/org/apache/sshd/client/SshClientMain.java index 390960e..22fcc95 100644 --- a/sshd-core/src/test/java/org/apache/sshd/client/SshClientMain.java +++ b/sshd-core/src/test/java/org/apache/sshd/client/SshClientMain.java @@ -22,6 +22,7 @@ package org.apache.sshd.client; /** * Just a test class used to invoke {@link SshClient#main(String[])} in * order to have logging - which is in {@code test} scope + * * @author <a href="mailto:[email protected]">Apache MINA SSHD Project</a> */ public class SshClientMain { http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/test/java/org/apache/sshd/client/config/keys/ClientIdentityTest.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/test/java/org/apache/sshd/client/config/keys/ClientIdentityTest.java b/sshd-core/src/test/java/org/apache/sshd/client/config/keys/ClientIdentityTest.java index aa8d957..08c29a4 100644 --- a/sshd-core/src/test/java/org/apache/sshd/client/config/keys/ClientIdentityTest.java +++ b/sshd-core/src/test/java/org/apache/sshd/client/config/keys/ClientIdentityTest.java @@ -63,7 +63,7 @@ public class ClientIdentityTest extends BaseTestSupport { System.out.println("Skip non-existing identity file " + file); continue; } - + if (!type.isSupported()) { System.out.println("Skip unsupported identity file " + file); continue; @@ -72,13 +72,13 @@ public class ClientIdentityTest extends BaseTestSupport { expected.add(type); } - Map<String,KeyPair> ids = ClientIdentity.loadDefaultIdentities( + Map<String, KeyPair> ids = ClientIdentity.loadDefaultIdentities( resFolder, false, // don't be strict null, // none of the files is password protected options); assertEquals("Mismatched loaded ids count", GenericUtils.size(expected), GenericUtils.size(ids)); - + Collection<KeyPair> pairs = new ArrayList<KeyPair>(ids.size()); for (BuiltinIdentities type : BuiltinIdentities.VALUES) { if (expected.contains(type)) { @@ -87,7 +87,7 @@ public class ClientIdentityTest extends BaseTestSupport { pairs.add(kp); } } - + KeyPairProvider provider = IdentityUtils.createKeyPairProvider(ids, true /* supported only */); assertNotNull("No provider generated", provider); @@ -95,7 +95,7 @@ public class ClientIdentityTest extends BaseTestSupport { for (KeyPair kp : keys) { assertTrue("Unexpected loaded key: " + kp, pairs.remove(kp)); } - + assertEquals("Not all pairs listed", 0, pairs.size()); } } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/test/java/org/apache/sshd/client/kex/KexTest.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/test/java/org/apache/sshd/client/kex/KexTest.java b/sshd-core/src/test/java/org/apache/sshd/client/kex/KexTest.java index d5733eb..de624de 100644 --- a/sshd-core/src/test/java/org/apache/sshd/client/kex/KexTest.java +++ b/sshd-core/src/test/java/org/apache/sshd/client/kex/KexTest.java @@ -49,6 +49,7 @@ import org.junit.runners.MethodSorters; /** * Test client key exchange algorithms. + * * @author <a href="mailto:[email protected]">Apache MINA SSHD Project</a> */ @FixMethodOrder(MethodSorters.NAME_ASCENDING) @@ -68,7 +69,7 @@ public class KexTest extends BaseTestSupport { sshd.setShellFactory(new EchoShellFactory()); sshd.setPasswordAuthenticator(BogusPasswordAuthenticator.INSTANCE); sshd.start(); - port = sshd.getPort(); + port = sshd.getPort(); } @After @@ -80,22 +81,22 @@ public class KexTest extends BaseTestSupport { @Test public void testClientKeyExchanges() throws Exception { - Exception err=null; + Exception err = null; for (BuiltinDHFactories f : BuiltinDHFactories.VALUES) { if (!f.isSupported()) { System.out.println("Skip KEX=" + f.getName() + " - unsupported"); continue; } - + try { testClient(f); - } catch(Exception e) { + } catch (Exception e) { System.err.println(e.getClass().getSimpleName() + " while test KEX=" + f.getName() + ": " + e.getMessage()); err = e; } } - + if (err != null) { throw err; } @@ -108,48 +109,48 @@ public class KexTest extends BaseTestSupport { private void testClient(NamedFactory<KeyExchange> kex) throws Exception { System.out.println("testClient - KEX=" + kex.getName()); - try(ByteArrayOutputStream sent = new ByteArrayOutputStream(); - ByteArrayOutputStream out = new ByteArrayOutputStream()) { + try (ByteArrayOutputStream sent = new ByteArrayOutputStream(); + ByteArrayOutputStream out = new ByteArrayOutputStream()) { - try(SshClient client = SshClient.setUpDefaultClient()) { + try (SshClient client = SshClient.setUpDefaultClient()) { client.setKeyExchangeFactories(Collections.singletonList(kex)); client.start(); - - try(ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { + + try (ClientSession session = client.connect(getCurrentTestName(), "localhost", port).verify(7L, TimeUnit.SECONDS).getSession()) { session.addPasswordIdentity(getCurrentTestName()); session.auth().verify(5L, TimeUnit.SECONDS); - - try(ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_SHELL); - PipedOutputStream pipedIn = new PipedOutputStream(); - InputStream inPipe = new PipedInputStream(pipedIn); - ByteArrayOutputStream err = new ByteArrayOutputStream(); - OutputStream teeOut = new TeeOutputStream(sent, pipedIn)) { - + + try (ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_SHELL); + PipedOutputStream pipedIn = new PipedOutputStream(); + InputStream inPipe = new PipedInputStream(pipedIn); + ByteArrayOutputStream err = new ByteArrayOutputStream(); + OutputStream teeOut = new TeeOutputStream(sent, pipedIn)) { + channel.setIn(inPipe); channel.setOut(out); channel.setErr(err); channel.open().verify(9L, TimeUnit.SECONDS); - + teeOut.write("this is my command\n".getBytes(StandardCharsets.UTF_8)); teeOut.flush(); - + StringBuilder sb = new StringBuilder(); for (int i = 0; i < 10; i++) { sb.append("0123456789"); } sb.append("\n"); teeOut.write(sb.toString().getBytes(StandardCharsets.UTF_8)); - + teeOut.write("exit\n".getBytes(StandardCharsets.UTF_8)); teeOut.flush(); - + channel.waitFor(ClientChannel.CLOSED, 0); } } finally { client.stop(); } } - + assertArrayEquals(kex.getName(), sent.toByteArray(), out.toByteArray()); } } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17f2d627/sshd-core/src/test/java/org/apache/sshd/client/keyverifier/StaticServerKeyVerifierTest.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/test/java/org/apache/sshd/client/keyverifier/StaticServerKeyVerifierTest.java b/sshd-core/src/test/java/org/apache/sshd/client/keyverifier/StaticServerKeyVerifierTest.java index 2b7d0ec..05a79cb 100644 --- a/sshd-core/src/test/java/org/apache/sshd/client/keyverifier/StaticServerKeyVerifierTest.java +++ b/sshd-core/src/test/java/org/apache/sshd/client/keyverifier/StaticServerKeyVerifierTest.java @@ -55,26 +55,26 @@ public class StaticServerKeyVerifierTest extends BaseTestSupport { } private void testStaticServerKeyVerifier(StaticServerKeyVerifier authenticator) throws Exception { - Method method = ServerKeyVerifier.class.getMethod("verifyServerKey", ClientSession.class, SocketAddress.class, PublicKey.class); - PublicKey key = Mockito.mock(PublicKey.class); + Method method = ServerKeyVerifier.class.getMethod("verifyServerKey", ClientSession.class, SocketAddress.class, PublicKey.class); + PublicKey key = Mockito.mock(PublicKey.class); Mockito.when(key.getAlgorithm()).thenReturn(getCurrentTestName()); Mockito.when(key.getEncoded()).thenReturn(GenericUtils.EMPTY_BYTE_ARRAY); Mockito.when(key.getFormat()).thenReturn(getCurrentTestName()); - Object[] args = { Mockito.mock(ClientSession.class), new InetSocketAddress("localhost", 7365), key }; - Object[] invArgs = new Object[args.length]; - Random rnd = new Random(System.nanoTime()); - boolean expected = authenticator.isAccepted(); - for (int index=0; index < Long.SIZE; index++) { - for (int j=0; j < args.length; j++) { + Object[] args = {Mockito.mock(ClientSession.class), new InetSocketAddress("localhost", 7365), key}; + Object[] invArgs = new Object[args.length]; + Random rnd = new Random(System.nanoTime()); + boolean expected = authenticator.isAccepted(); + for (int index = 0; index < Long.SIZE; index++) { + for (int j = 0; j < args.length; j++) { if (rnd.nextBoolean()) { invArgs[j] = args[j]; } else { invArgs[j] = null; } } - - Object result = method.invoke(authenticator, invArgs); + + Object result = method.invoke(authenticator, invArgs); assertTrue("No boolean result", result instanceof Boolean); assertEquals("Mismatched result for " + Arrays.toString(invArgs), expected, ((Boolean) result).booleanValue()); }
