You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by lg...@apache.org on 2018/08/13 11:15:53 UTC

[2/3] mina-sshd git commit: Added unit tests for NullInput/OutputStream

Added unit tests for NullInput/OutputStream


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

Branch: refs/heads/master
Commit: 5b2ef0aec8000545e9ae839ed793135393855e58
Parents: b222a67
Author: Goldstein Lyor <ly...@c-b4.com>
Authored: Mon Aug 13 10:35:25 2018 +0300
Committer: Goldstein Lyor <ly...@c-b4.com>
Committed: Mon Aug 13 14:15:46 2018 +0300

----------------------------------------------------------------------
 .../sshd/common/util/io/NullInputStream.java    |  11 +-
 .../sshd/common/util/io/NullOutputStream.java   |   7 +-
 .../common/util/io/NullInputStreamTest.java     | 118 +++++++++++++++++++
 .../common/util/io/NullOutputStreamTest.java    |  80 +++++++++++++
 4 files changed, 208 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/5b2ef0ae/sshd-core/src/main/java/org/apache/sshd/common/util/io/NullInputStream.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/io/NullInputStream.java b/sshd-core/src/main/java/org/apache/sshd/common/util/io/NullInputStream.java
index 5ac7f3e..eb21383 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/util/io/NullInputStream.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/util/io/NullInputStream.java
@@ -19,6 +19,7 @@
 
 package org.apache.sshd.common.util.io;
 
+import java.io.EOFException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.nio.channels.Channel;
@@ -43,7 +44,7 @@ public class NullInputStream extends InputStream implements Channel {
     @Override
     public int read() throws IOException {
         if (!isOpen()) {
-            throw new IOException("Stream is closed for reading one value");
+            throw new EOFException("Stream is closed for reading one value");
         }
         return -1;
     }
@@ -51,7 +52,7 @@ public class NullInputStream extends InputStream implements Channel {
     @Override
     public int read(byte[] b, int off, int len) throws IOException {
         if (!isOpen()) {
-            throw new IOException("Stream is closed for reading " + len + " bytes");
+            throw new EOFException("Stream is closed for reading " + len + " bytes");
         }
         return -1;
     }
@@ -59,7 +60,7 @@ public class NullInputStream extends InputStream implements Channel {
     @Override
     public long skip(long n) throws IOException {
         if (!isOpen()) {
-            throw new IOException("Stream is closed for skipping " + n + " bytes");
+            throw new EOFException("Stream is closed for skipping " + n + " bytes");
         }
         return 0L;
     }
@@ -67,7 +68,7 @@ public class NullInputStream extends InputStream implements Channel {
     @Override
     public int available() throws IOException {
         if (!isOpen()) {
-            throw new IOException("Stream is closed for availability query");
+            throw new EOFException("Stream is closed for availability query");
         }
         return 0;
     }
@@ -75,7 +76,7 @@ public class NullInputStream extends InputStream implements Channel {
     @Override
     public synchronized void reset() throws IOException {
         if (!isOpen()) {
-            throw new IOException("Stream is closed for reset");
+            throw new EOFException("Stream is closed for reset");
         }
     }
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/5b2ef0ae/sshd-core/src/main/java/org/apache/sshd/common/util/io/NullOutputStream.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/io/NullOutputStream.java b/sshd-core/src/main/java/org/apache/sshd/common/util/io/NullOutputStream.java
index 5e3b719..67fa2d0 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/util/io/NullOutputStream.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/util/io/NullOutputStream.java
@@ -19,6 +19,7 @@
 
 package org.apache.sshd.common.util.io;
 
+import java.io.EOFException;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.nio.channels.Channel;
@@ -43,21 +44,21 @@ public class NullOutputStream extends OutputStream implements Channel {
     @Override
     public void write(int b) throws IOException {
         if (!isOpen()) {
-            throw new IOException("Stream is closed for writing one byte");
+            throw new EOFException("Stream is closed for writing one byte");
         }
     }
 
     @Override
     public void write(byte[] b, int off, int len) throws IOException {
         if (!isOpen()) {
-            throw new IOException("Stream is closed for writing " + len + " bytes");
+            throw new EOFException("Stream is closed for writing " + len + " bytes");
         }
     }
 
     @Override
     public void flush() throws IOException {
         if (!isOpen()) {
-            throw new IOException("Stream is closed for flushing");
+            throw new EOFException("Stream is closed for flushing");
         }
     }
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/5b2ef0ae/sshd-core/src/test/java/org/apache/sshd/common/util/io/NullInputStreamTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/util/io/NullInputStreamTest.java b/sshd-core/src/test/java/org/apache/sshd/common/util/io/NullInputStreamTest.java
new file mode 100644
index 0000000..bc97b9c
--- /dev/null
+++ b/sshd-core/src/test/java/org/apache/sshd/common/util/io/NullInputStreamTest.java
@@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.sshd.common.util.io;
+
+import java.io.EOFException;
+import java.io.IOException;
+
+import org.apache.sshd.util.test.BaseTestSupport;
+import org.apache.sshd.util.test.NoIoTestCase;
+import org.junit.FixMethodOrder;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runners.MethodSorters;
+
+/**
+ * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
+ */
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+@Category({ NoIoTestCase.class })
+public class NullInputStreamTest extends BaseTestSupport {
+    private static final NullInputStream INSTANCE = new NullInputStream();
+
+    public NullInputStreamTest() {
+        super();
+    }
+
+    @Test
+    public void testReadOneChar() throws IOException {
+        assertEquals(-1, INSTANCE.read());
+    }
+
+    @Test
+    public void testReadFullBuffer() throws IOException {
+        assertEquals(-1, INSTANCE.read(new byte[Byte.SIZE]));
+    }
+
+    @Test
+    public void testReadPartialBuffer() throws IOException {
+        byte[] buf = new byte[Byte.SIZE];
+        assertEquals(-1, INSTANCE.read(buf, buf.length / 2, (buf.length / 2) - 1));
+    }
+
+    @Test
+    public void testSkip() throws IOException {
+        assertEquals(0L, INSTANCE.skip(Long.SIZE));
+    }
+
+    @Test
+    public void testAvailable() throws IOException {
+        assertEquals(0, INSTANCE.available());
+    }
+
+    @Test
+    public void testNotAllowedToAccessAfterClose() throws IOException {
+        NullInputStream stream = new NullInputStream();
+        stream.close();
+        assertFalse("Stream not marked as closed", stream.isOpen());
+
+        try {
+            int nRead = stream.read();
+            fail("Unexpected single byte read: " + nRead);
+        } catch (EOFException e) {
+            // expected
+        }
+
+        byte[] buf = new byte[Byte.SIZE];
+        try {
+            int nRead = stream.read(buf);
+            fail("Unexpected full buffer read: " + nRead);
+        } catch (EOFException e) {
+            // expected
+        }
+
+        try {
+            int nRead = stream.read(buf, buf.length / 2, (buf.length / 2) - 1);
+            fail("Unexpected partial buffer read: " + nRead);
+        } catch (EOFException e) {
+            // expected
+        }
+
+        try {
+            long skip = stream.skip(Long.SIZE);
+            fail("Unexpected skip result: " + skip);
+        } catch (EOFException e) {
+            // expected
+        }
+
+        try {
+            int nRead = stream.available();
+            fail("Unexpected available count: " + nRead);
+        } catch (IOException e) {
+            // expected
+        }
+        try {
+            stream.reset();
+            fail("Unexpected reset success");
+        } catch (EOFException e) {
+            // expected
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/5b2ef0ae/sshd-core/src/test/java/org/apache/sshd/common/util/io/NullOutputStreamTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/util/io/NullOutputStreamTest.java b/sshd-core/src/test/java/org/apache/sshd/common/util/io/NullOutputStreamTest.java
new file mode 100644
index 0000000..b6a0230
--- /dev/null
+++ b/sshd-core/src/test/java/org/apache/sshd/common/util/io/NullOutputStreamTest.java
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.sshd.common.util.io;
+
+import java.io.EOFException;
+import java.io.IOException;
+import java.util.Arrays;
+
+import org.apache.sshd.util.test.BaseTestSupport;
+import org.apache.sshd.util.test.NoIoTestCase;
+import org.junit.FixMethodOrder;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runners.MethodSorters;
+
+/**
+ * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
+ */
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+@Category({ NoIoTestCase.class })
+public class NullOutputStreamTest extends BaseTestSupport {
+    public NullOutputStreamTest() {
+        super();
+    }
+
+    @Test
+    public void testNoAccessAllowedAfterClose() throws IOException {
+        NullOutputStream stream = new NullOutputStream();
+        stream.close();
+        assertFalse("Stream not marked as closed", stream.isOpen());
+
+        try {
+            stream.write('a');
+            fail("Unexpected single value write success");
+        } catch (EOFException e) {
+            // expected
+        }
+
+        byte[] buf = new byte[Byte.SIZE];
+        try {
+            Arrays.fill(buf, (byte) 0x41);
+            stream.write(buf);
+            fail("Unexpected full buffer write success");
+        } catch (EOFException e) {
+            // expected
+        }
+
+        try {
+            Arrays.fill(buf, (byte) 0x42);
+            stream.write(buf, buf.length / 2, (buf.length / 2) - 1);
+            fail("Unexpected partial buffer write success");
+        } catch (EOFException e) {
+            // expected
+        }
+
+        try {
+            stream.flush();
+            fail("Unexpected flush success");
+        } catch (EOFException e) {
+            // expected
+        }
+    }
+}