You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by gn...@apache.org on 2018/04/16 14:04:25 UTC

[2/2] mina-sshd git commit: [SSHD-640] Add unit test

[SSHD-640] Add unit test


Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/dc5682e6
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/dc5682e6
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/dc5682e6

Branch: refs/heads/master
Commit: dc5682e6e1d55ae8daee547aface6af2c8e72a44
Parents: 0e8fbf9
Author: Guillaume Nodet <gn...@apache.org>
Authored: Mon Apr 16 16:03:52 2018 +0200
Committer: Guillaume Nodet <gn...@apache.org>
Committed: Mon Apr 16 16:03:52 2018 +0200

----------------------------------------------------------------------
 .../sshd/server/channel/ChannelSessionTest.java | 53 ++++++++++++++++++++
 1 file changed, 53 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/dc5682e6/sshd-core/src/test/java/org/apache/sshd/server/channel/ChannelSessionTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/server/channel/ChannelSessionTest.java b/sshd-core/src/test/java/org/apache/sshd/server/channel/ChannelSessionTest.java
index 00e5a1b..8b86012 100644
--- a/sshd-core/src/test/java/org/apache/sshd/server/channel/ChannelSessionTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/server/channel/ChannelSessionTest.java
@@ -19,17 +19,29 @@
 package org.apache.sshd.server.channel;
 
 import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.Collection;
 import java.util.Collections;
+import java.util.EnumSet;
+import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
 
+import org.apache.sshd.client.SshClient;
+import org.apache.sshd.client.channel.ClientChannel;
+import org.apache.sshd.client.channel.ClientChannelEvent;
+import org.apache.sshd.client.session.ClientSession;
 import org.apache.sshd.common.PropertyResolverUtils;
+import org.apache.sshd.common.channel.Channel;
 import org.apache.sshd.common.channel.ChannelAsyncOutputStream;
 import org.apache.sshd.common.channel.Window;
 import org.apache.sshd.common.util.buffer.Buffer;
 import org.apache.sshd.common.util.buffer.ByteArrayBuffer;
+import org.apache.sshd.server.SshServer;
 import org.apache.sshd.util.test.BaseTestSupport;
 import org.apache.sshd.util.test.BogusChannel;
+import org.apache.sshd.util.test.CommandExecutionHelper;
 import org.apache.sshd.util.test.NoIoTestCase;
 import org.junit.FixMethodOrder;
 import org.junit.Test;
@@ -43,6 +55,47 @@ public class ChannelSessionTest extends BaseTestSupport {
         super();
     }
 
+    @Test
+    public void testNoFlush() throws Exception {
+        try (SshServer server = setupTestServer();
+             SshClient client = setupTestClient()) {
+
+            server.setShellFactory(() -> new CommandExecutionHelper(null) {
+                @Override
+                protected boolean handleCommandLine(String command) throws Exception {
+                    OutputStream out = getOutputStream();
+                    out.write((command + "\n").getBytes(StandardCharsets.UTF_8));
+                    return !"exit".equals(command);
+                }
+            });
+            server.start();
+            client.start();
+
+            try (ClientSession session = client.connect(getCurrentTestName(), TEST_LOCALHOST, server.getPort()).verify(7L, TimeUnit.SECONDS).getSession()) {
+                session.addPasswordIdentity(getCurrentTestName());
+                session.auth().verify(5L, TimeUnit.SECONDS);
+
+                try (ClientChannel channel = session.createChannel(Channel.CHANNEL_SHELL)) {
+
+                    channel.open().await();
+
+                    channel.getInvertedIn().write("echo foo\nexit\n".getBytes());
+                    channel.getInvertedIn().flush();
+
+                    Collection<ClientChannelEvent> result =
+                            channel.waitFor(EnumSet.of(ClientChannelEvent.CLOSED), 5000);
+                    assertTrue("Wrong channel state: " + result, result.containsAll(EnumSet.of(ClientChannelEvent.CLOSED)));
+
+                    byte[] b = new byte[1024];
+                    int l = channel.getInvertedOut().read(b);
+                    String s = l > 0 ? new String(b, 0, l) : "";
+
+                    assertEquals("echo foo\nexit\n", s);
+                }
+            }
+        }
+    }
+
     /*
      * Test whether onWindowExpanded is called from server session
      */