Author: gnodet
Date: Sat May 19 12:35:02 2012
New Revision: 1340434
URL: http://svn.apache.org/viewvc?rev=1340434&view=rev
Log:
[SSHD-146] SSH Server : EOF from command not propagating to Process
(InvertedShell)
Added:
mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/common/channel/
mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/common/channel/ChannelPipedInputStreamTest.java
mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/util/BogusChannel.java
Modified:
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/channel/ChannelPipedInputStream.java
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/shell/InvertedShellWrapper.java
Modified:
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/channel/ChannelPipedInputStream.java
URL:
http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/channel/ChannelPipedInputStream.java?rev=1340434&r1=1340433&r2=1340434&view=diff
==============================================================================
---
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/channel/ChannelPipedInputStream.java
(original)
+++
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/common/channel/ChannelPipedInputStream.java
Sat May 19 12:35:02 2012
@@ -49,7 +49,11 @@ public class ChannelPipedInputStream ext
@Override
public int available() throws IOException {
synchronized (buffer) {
- return buffer.available();
+ int avail = buffer.available();
+ if (avail == 0 && writerClosed) {
+ return -1;
+ }
+ return avail;
}
}
Modified:
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/shell/InvertedShellWrapper.java
URL:
http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/shell/InvertedShellWrapper.java?rev=1340434&r1=1340433&r2=1340434&view=diff
==============================================================================
---
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/shell/InvertedShellWrapper.java
(original)
+++
mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/server/shell/InvertedShellWrapper.java
Sat May 19 12:35:02 2012
@@ -145,13 +145,16 @@ public class InvertedShellWrapper implem
}
private boolean pumpStream(InputStream in, OutputStream out, byte[]
buffer) throws IOException {
- if (in.available() > 0) {
+ int available = in.available();
+ if (available > 0) {
int len = in.read(buffer);
if (len > 0) {
out.write(buffer, 0, len);
out.flush();
return true;
}
+ } else if (available == -1) {
+ out.close();
}
return false;
}
Added:
mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/common/channel/ChannelPipedInputStreamTest.java
URL:
http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/common/channel/ChannelPipedInputStreamTest.java?rev=1340434&view=auto
==============================================================================
---
mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/common/channel/ChannelPipedInputStreamTest.java
(added)
+++
mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/common/channel/ChannelPipedInputStreamTest.java
Sat May 19 12:35:02 2012
@@ -0,0 +1,41 @@
+package org.apache.sshd.common.channel;
+
+import org.apache.sshd.util.BogusChannel;
+import org.junit.Test;
+
+import java.util.Arrays;
+
+import static org.junit.Assert.*;
+
+public class ChannelPipedInputStreamTest {
+
+ @Test
+ public void testAvailable() throws Exception {
+ Window window = new Window(new BogusChannel(), null, true, true);
+ ChannelPipedInputStream stream = new ChannelPipedInputStream(window);
+
+ byte[] b = "test".getBytes();
+ stream.receive(b, 0, b.length);
+ assertEquals(b.length, stream.available());
+
+ stream.eof();
+ assertEquals(b.length, stream.available());
+
+ final byte[] readBytes = new byte[50];
+ assertEquals(b.length, stream.read(readBytes));
+ assertStreamEquals(b, readBytes);
+ assertEquals(-1, stream.available());
+ }
+
+ private void assertStreamEquals(byte[] expected, byte[] read) {
+ if (expected.length > read.length) {
+ fail("Less bytes than expected: " + Arrays.toString(expected) + "
but got: " + Arrays.toString(read));
+ } else {
+ assertArrayEquals(expected, Arrays.copyOf(read, expected.length));
+ for (int i = expected.length; i < read.length; i++) {
+ assertEquals('\0', read[i]);
+ }
+ }
+ }
+
+}
Added:
mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/util/BogusChannel.java
URL:
http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/util/BogusChannel.java?rev=1340434&view=auto
==============================================================================
---
mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/util/BogusChannel.java
(added)
+++
mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/util/BogusChannel.java
Sat May 19 12:35:02 2012
@@ -0,0 +1,34 @@
+package org.apache.sshd.util;
+
+import org.apache.sshd.client.future.DefaultOpenFuture;
+import org.apache.sshd.client.future.OpenFuture;
+import org.apache.sshd.common.channel.AbstractChannel;
+import org.apache.sshd.common.util.Buffer;
+
+import java.io.IOException;
+
+public class BogusChannel extends AbstractChannel {
+
+ @Override
+ protected void doWriteData(byte[] data, int off, int len) throws
IOException {
+ }
+
+ @Override
+ protected void doWriteExtendedData(byte[] data, int off, int len) throws
IOException {
+ }
+
+ @Override
+ protected void sendWindowAdjust(int len) throws IOException {
+ }
+
+ public OpenFuture open(int recipient, int rwsize, int rmpsize, Buffer
buffer) {
+ return new DefaultOpenFuture(this.lock);
+ }
+
+ public void handleOpenSuccess(int recipient, int rwsize, int rmpsize,
Buffer buffer) throws IOException {
+ }
+
+ public void handleOpenFailure(Buffer buffer) throws IOException {
+ }
+
+}