vanzin commented on a change in pull request #99:
URL: https://github.com/apache/commons-crypto/pull/99#discussion_r418815800



##########
File path: 
src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
##########
@@ -197,46 +224,246 @@ protected void doByteBufferRead(final String 
cipherClass, final boolean withChan
                 getCipher(cipherClass), smallBufferSize, iv, withChannel);
         buf.clear();
         byteBufferReadCheck(in, buf, 11);
+        in.close();      
+        
+        // Direct buffer, default buffer size, initial buffer position is 0, 
final read
+        in = getCryptoInputStream(new ByteArrayInputStream(encData),
+                getCipher(cipherClass), smallBufferSize, iv, withChannel);
+        buf.clear();
+        byteBufferFinalReadCheck(in, buf, 0);
+        in.close();
+        
+        // Default buffer size, initial buffer position is 0, insufficient 
dest buffer length
+        in = getCryptoInputStream(new ByteArrayInputStream(encData),
+                getCipher(cipherClass), defaultBufferSize, iv, withChannel);
+        buf = ByteBuffer.allocate(100);
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+        
+        // Default buffer size, initial buffer position is 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf = ByteBuffer.allocate(dataLen + 100);
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+        
+        // Default buffer size, initial buffer position is not 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 11);
+        in.close();
+
+        // Small buffer size, initial buffer position is 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+
+        // Small buffer size, initial buffer position is not 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 11);
+        in.close();
+
+        // Direct buffer, default buffer size, initial buffer position is 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf = ByteBuffer.allocateDirect(dataLen + 100);
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+
+        // Direct buffer, default buffer size, initial buffer position is not 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 11);
+        in.close();
+
+        // Direct buffer, small buffer size, initial buffer position is 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+
+        // Direct buffer, small buffer size, initial buffer position is not 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 11);
+        in.close();
+        
+        // Direct buffer, default buffer size, initial buffer position is 0, 
final read
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferFinalReadCheck(in, buf, 0);
+        in.close();
+        
+        // Default buffer size, initial buffer position is 0, insufficient 
dest buffer length
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf = ByteBuffer.allocate(100);
+        byteBufferReadCheck(in, buf, 0);
         in.close();
     }
 
     protected void doByteBufferWrite(final String cipherClass,
-            final ByteArrayOutputStream baos, final boolean withChannel) 
throws Exception {
+            final ByteArrayOutputStream baos, final boolean withChannel) 
+                throws Exception {
         if (AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME.equals(cipherClass)) {
             if (!Crypto.isNativeCodeLoaded()) {
                 return; // Skip this test if no JNI
             }
         }
         baos.reset();
-        final CryptoOutputStream out = getCryptoOutputStream(baos,
+        CryptoOutputStream out = getCryptoOutputStream(baos,
                 getCipher(cipherClass), defaultBufferSize, iv, withChannel);
-        ByteBuffer buf = ByteBuffer.allocateDirect(dataLen / 2);
-        buf.put(data, 0, dataLen / 2);
-        buf.flip();
-        final int n1 = out.write(buf);
-
-        buf.clear();
-        buf.put(data, n1, dataLen / 3);
-        buf.flip();
-        final int n2 = out.write(buf);
-
-        buf.clear();
-        buf.put(data, n1 + n2, dataLen - n1 - n2);
-        buf.flip();
-        final int n3 = out.write(buf);
+        doByteBufferWrite(out, withChannel);
+        
+        baos.reset();
+        CryptoCipher cipher = getCipher(cipherClass);
+        String transformation = cipher.getAlgorithm();
+        out = getCryptoOutputStream(transformation, props, baos, key, 
+                new IvParameterSpec(iv), withChannel);
+        doByteBufferWrite(out, withChannel);
+        out.write(1);
+        Assert.assertTrue(out.isOpen()); 
+        
+        out = getCryptoOutputStream(transformation, props, baos, key, 
+                new IvParameterSpec(iv), withChannel);
+        out.close();
+        Assert.assertTrue(!out.isOpen());
+    }
 
-        Assert.assertEquals(dataLen, n1 + n2 + n3);
+    protected void doExceptionTest(final String cipherClass, 
ByteArrayOutputStream baos,
+            final boolean withChannel) throws IOException {
+        if (AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME.equals(cipherClass)) {
+            if (!Crypto.isNativeCodeLoaded()) {
+                return; // Skip this test if no JNI
+            }
+        }
+        
+        InputStream in = null;
+        OutputStream out = null;
+        try { // Test InvalidAlgorithmParameters
+               in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), 
+                    new SecretKeySpec(key, "AES"), new GCMParameterSpec(0, new 
byte[0]), 
+                    withChannel);
+            Assert.fail("Expected IOException.");
+        } catch (IOException ex) {
+            Assert.assertEquals(ex.getMessage(),"Illegal parameters");
+        } 
+        
+        try { // Test InvalidAlgorithmParameters

Review comment:
       ```suggestion
           // Test InvalidAlgorithmParameters
           try {
   ```

##########
File path: 
src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
##########
@@ -197,46 +224,246 @@ protected void doByteBufferRead(final String 
cipherClass, final boolean withChan
                 getCipher(cipherClass), smallBufferSize, iv, withChannel);
         buf.clear();
         byteBufferReadCheck(in, buf, 11);
+        in.close();      
+        
+        // Direct buffer, default buffer size, initial buffer position is 0, 
final read

Review comment:
       This says default but code says `smallBufferSize`. Not sure if that 
matters... also looking at the code you're adding, it seems a bit redundant 
with existing code, but didn't really double-check things.

##########
File path: 
src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
##########
@@ -197,46 +224,246 @@ protected void doByteBufferRead(final String 
cipherClass, final boolean withChan
                 getCipher(cipherClass), smallBufferSize, iv, withChannel);
         buf.clear();
         byteBufferReadCheck(in, buf, 11);
+        in.close();      
+        
+        // Direct buffer, default buffer size, initial buffer position is 0, 
final read
+        in = getCryptoInputStream(new ByteArrayInputStream(encData),
+                getCipher(cipherClass), smallBufferSize, iv, withChannel);
+        buf.clear();
+        byteBufferFinalReadCheck(in, buf, 0);
+        in.close();
+        
+        // Default buffer size, initial buffer position is 0, insufficient 
dest buffer length
+        in = getCryptoInputStream(new ByteArrayInputStream(encData),
+                getCipher(cipherClass), defaultBufferSize, iv, withChannel);
+        buf = ByteBuffer.allocate(100);
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+        
+        // Default buffer size, initial buffer position is 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf = ByteBuffer.allocate(dataLen + 100);
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+        
+        // Default buffer size, initial buffer position is not 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 11);
+        in.close();
+
+        // Small buffer size, initial buffer position is 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+
+        // Small buffer size, initial buffer position is not 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 11);
+        in.close();
+
+        // Direct buffer, default buffer size, initial buffer position is 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf = ByteBuffer.allocateDirect(dataLen + 100);
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+
+        // Direct buffer, default buffer size, initial buffer position is not 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 11);
+        in.close();
+
+        // Direct buffer, small buffer size, initial buffer position is 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+
+        // Direct buffer, small buffer size, initial buffer position is not 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 11);
+        in.close();
+        
+        // Direct buffer, default buffer size, initial buffer position is 0, 
final read
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferFinalReadCheck(in, buf, 0);
+        in.close();
+        
+        // Default buffer size, initial buffer position is 0, insufficient 
dest buffer length
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf = ByteBuffer.allocate(100);
+        byteBufferReadCheck(in, buf, 0);
         in.close();
     }
 
     protected void doByteBufferWrite(final String cipherClass,
-            final ByteArrayOutputStream baos, final boolean withChannel) 
throws Exception {
+            final ByteArrayOutputStream baos, final boolean withChannel) 
+                throws Exception {
         if (AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME.equals(cipherClass)) {
             if (!Crypto.isNativeCodeLoaded()) {
                 return; // Skip this test if no JNI
             }
         }
         baos.reset();
-        final CryptoOutputStream out = getCryptoOutputStream(baos,
+        CryptoOutputStream out = getCryptoOutputStream(baos,
                 getCipher(cipherClass), defaultBufferSize, iv, withChannel);
-        ByteBuffer buf = ByteBuffer.allocateDirect(dataLen / 2);
-        buf.put(data, 0, dataLen / 2);
-        buf.flip();
-        final int n1 = out.write(buf);
-
-        buf.clear();
-        buf.put(data, n1, dataLen / 3);
-        buf.flip();
-        final int n2 = out.write(buf);
-
-        buf.clear();
-        buf.put(data, n1 + n2, dataLen - n1 - n2);
-        buf.flip();
-        final int n3 = out.write(buf);
+        doByteBufferWrite(out, withChannel);
+        
+        baos.reset();
+        CryptoCipher cipher = getCipher(cipherClass);
+        String transformation = cipher.getAlgorithm();
+        out = getCryptoOutputStream(transformation, props, baos, key, 
+                new IvParameterSpec(iv), withChannel);
+        doByteBufferWrite(out, withChannel);
+        out.write(1);
+        Assert.assertTrue(out.isOpen()); 
+        
+        out = getCryptoOutputStream(transformation, props, baos, key, 
+                new IvParameterSpec(iv), withChannel);
+        out.close();
+        Assert.assertTrue(!out.isOpen());
+    }
 
-        Assert.assertEquals(dataLen, n1 + n2 + n3);
+    protected void doExceptionTest(final String cipherClass, 
ByteArrayOutputStream baos,
+            final boolean withChannel) throws IOException {
+        if (AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME.equals(cipherClass)) {
+            if (!Crypto.isNativeCodeLoaded()) {
+                return; // Skip this test if no JNI
+            }
+        }
+        
+        InputStream in = null;
+        OutputStream out = null;
+        try { // Test InvalidAlgorithmParameters

Review comment:
       ```suggestion
           // Test InvalidAlgorithmParameters
           try {
   ```

##########
File path: 
src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
##########
@@ -197,46 +224,246 @@ protected void doByteBufferRead(final String 
cipherClass, final boolean withChan
                 getCipher(cipherClass), smallBufferSize, iv, withChannel);
         buf.clear();
         byteBufferReadCheck(in, buf, 11);
+        in.close();      
+        
+        // Direct buffer, default buffer size, initial buffer position is 0, 
final read
+        in = getCryptoInputStream(new ByteArrayInputStream(encData),
+                getCipher(cipherClass), smallBufferSize, iv, withChannel);
+        buf.clear();
+        byteBufferFinalReadCheck(in, buf, 0);
+        in.close();
+        
+        // Default buffer size, initial buffer position is 0, insufficient 
dest buffer length
+        in = getCryptoInputStream(new ByteArrayInputStream(encData),
+                getCipher(cipherClass), defaultBufferSize, iv, withChannel);
+        buf = ByteBuffer.allocate(100);
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+        
+        // Default buffer size, initial buffer position is 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf = ByteBuffer.allocate(dataLen + 100);
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+        
+        // Default buffer size, initial buffer position is not 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 11);
+        in.close();
+
+        // Small buffer size, initial buffer position is 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+
+        // Small buffer size, initial buffer position is not 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 11);
+        in.close();
+
+        // Direct buffer, default buffer size, initial buffer position is 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf = ByteBuffer.allocateDirect(dataLen + 100);
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+
+        // Direct buffer, default buffer size, initial buffer position is not 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 11);
+        in.close();
+
+        // Direct buffer, small buffer size, initial buffer position is 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+
+        // Direct buffer, small buffer size, initial buffer position is not 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 11);
+        in.close();
+        
+        // Direct buffer, default buffer size, initial buffer position is 0, 
final read
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferFinalReadCheck(in, buf, 0);
+        in.close();
+        
+        // Default buffer size, initial buffer position is 0, insufficient 
dest buffer length
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf = ByteBuffer.allocate(100);
+        byteBufferReadCheck(in, buf, 0);
         in.close();
     }
 
     protected void doByteBufferWrite(final String cipherClass,
-            final ByteArrayOutputStream baos, final boolean withChannel) 
throws Exception {
+            final ByteArrayOutputStream baos, final boolean withChannel) 
+                throws Exception {
         if (AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME.equals(cipherClass)) {
             if (!Crypto.isNativeCodeLoaded()) {
                 return; // Skip this test if no JNI
             }
         }
         baos.reset();
-        final CryptoOutputStream out = getCryptoOutputStream(baos,
+        CryptoOutputStream out = getCryptoOutputStream(baos,
                 getCipher(cipherClass), defaultBufferSize, iv, withChannel);
-        ByteBuffer buf = ByteBuffer.allocateDirect(dataLen / 2);
-        buf.put(data, 0, dataLen / 2);
-        buf.flip();
-        final int n1 = out.write(buf);
-
-        buf.clear();
-        buf.put(data, n1, dataLen / 3);
-        buf.flip();
-        final int n2 = out.write(buf);
-
-        buf.clear();
-        buf.put(data, n1 + n2, dataLen - n1 - n2);
-        buf.flip();
-        final int n3 = out.write(buf);
+        doByteBufferWrite(out, withChannel);
+        
+        baos.reset();
+        CryptoCipher cipher = getCipher(cipherClass);
+        String transformation = cipher.getAlgorithm();
+        out = getCryptoOutputStream(transformation, props, baos, key, 
+                new IvParameterSpec(iv), withChannel);
+        doByteBufferWrite(out, withChannel);
+        out.write(1);
+        Assert.assertTrue(out.isOpen()); 
+        
+        out = getCryptoOutputStream(transformation, props, baos, key, 
+                new IvParameterSpec(iv), withChannel);
+        out.close();
+        Assert.assertTrue(!out.isOpen());
+    }
 
-        Assert.assertEquals(dataLen, n1 + n2 + n3);
+    protected void doExceptionTest(final String cipherClass, 
ByteArrayOutputStream baos,
+            final boolean withChannel) throws IOException {
+        if (AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME.equals(cipherClass)) {
+            if (!Crypto.isNativeCodeLoaded()) {
+                return; // Skip this test if no JNI
+            }
+        }
+        
+        InputStream in = null;
+        OutputStream out = null;
+        try { // Test InvalidAlgorithmParameters
+               in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), 
+                    new SecretKeySpec(key, "AES"), new GCMParameterSpec(0, new 
byte[0]), 
+                    withChannel);
+            Assert.fail("Expected IOException.");
+        } catch (IOException ex) {
+            Assert.assertEquals(ex.getMessage(),"Illegal parameters");
+        } 
+        
+        try { // Test InvalidAlgorithmParameters
+            out = getCryptoOutputStream(transformation, props, baos, 
+                    new SecretKeySpec(key, "AES"), new GCMParameterSpec(0, 
+                    new byte[0]), withChannel);
+            Assert.fail("Expected IOException.");
+        } catch (IOException ex) {
+               Assert.assertEquals(ex.getMessage(),"Illegal parameters");
+        } 
+        
+        try { // Test Invalid Key

Review comment:
       ```suggestion
           // Test Invalid Key
           try {
   ```

##########
File path: 
src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
##########
@@ -197,46 +224,246 @@ protected void doByteBufferRead(final String 
cipherClass, final boolean withChan
                 getCipher(cipherClass), smallBufferSize, iv, withChannel);
         buf.clear();
         byteBufferReadCheck(in, buf, 11);
+        in.close();      
+        
+        // Direct buffer, default buffer size, initial buffer position is 0, 
final read
+        in = getCryptoInputStream(new ByteArrayInputStream(encData),
+                getCipher(cipherClass), smallBufferSize, iv, withChannel);
+        buf.clear();
+        byteBufferFinalReadCheck(in, buf, 0);
+        in.close();
+        
+        // Default buffer size, initial buffer position is 0, insufficient 
dest buffer length
+        in = getCryptoInputStream(new ByteArrayInputStream(encData),
+                getCipher(cipherClass), defaultBufferSize, iv, withChannel);
+        buf = ByteBuffer.allocate(100);
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+        
+        // Default buffer size, initial buffer position is 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf = ByteBuffer.allocate(dataLen + 100);
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+        
+        // Default buffer size, initial buffer position is not 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 11);
+        in.close();
+
+        // Small buffer size, initial buffer position is 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+
+        // Small buffer size, initial buffer position is not 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 11);
+        in.close();
+
+        // Direct buffer, default buffer size, initial buffer position is 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf = ByteBuffer.allocateDirect(dataLen + 100);
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+
+        // Direct buffer, default buffer size, initial buffer position is not 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 11);
+        in.close();
+
+        // Direct buffer, small buffer size, initial buffer position is 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+
+        // Direct buffer, small buffer size, initial buffer position is not 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 11);
+        in.close();
+        
+        // Direct buffer, default buffer size, initial buffer position is 0, 
final read
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferFinalReadCheck(in, buf, 0);
+        in.close();
+        
+        // Default buffer size, initial buffer position is 0, insufficient 
dest buffer length
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf = ByteBuffer.allocate(100);
+        byteBufferReadCheck(in, buf, 0);
         in.close();
     }
 
     protected void doByteBufferWrite(final String cipherClass,
-            final ByteArrayOutputStream baos, final boolean withChannel) 
throws Exception {
+            final ByteArrayOutputStream baos, final boolean withChannel) 
+                throws Exception {
         if (AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME.equals(cipherClass)) {
             if (!Crypto.isNativeCodeLoaded()) {
                 return; // Skip this test if no JNI
             }
         }
         baos.reset();
-        final CryptoOutputStream out = getCryptoOutputStream(baos,
+        CryptoOutputStream out = getCryptoOutputStream(baos,
                 getCipher(cipherClass), defaultBufferSize, iv, withChannel);
-        ByteBuffer buf = ByteBuffer.allocateDirect(dataLen / 2);
-        buf.put(data, 0, dataLen / 2);
-        buf.flip();
-        final int n1 = out.write(buf);
-
-        buf.clear();
-        buf.put(data, n1, dataLen / 3);
-        buf.flip();
-        final int n2 = out.write(buf);
-
-        buf.clear();
-        buf.put(data, n1 + n2, dataLen - n1 - n2);
-        buf.flip();
-        final int n3 = out.write(buf);
+        doByteBufferWrite(out, withChannel);
+        
+        baos.reset();
+        CryptoCipher cipher = getCipher(cipherClass);
+        String transformation = cipher.getAlgorithm();
+        out = getCryptoOutputStream(transformation, props, baos, key, 
+                new IvParameterSpec(iv), withChannel);
+        doByteBufferWrite(out, withChannel);
+        out.write(1);
+        Assert.assertTrue(out.isOpen()); 
+        
+        out = getCryptoOutputStream(transformation, props, baos, key, 
+                new IvParameterSpec(iv), withChannel);
+        out.close();
+        Assert.assertTrue(!out.isOpen());
+    }
 
-        Assert.assertEquals(dataLen, n1 + n2 + n3);
+    protected void doExceptionTest(final String cipherClass, 
ByteArrayOutputStream baos,
+            final boolean withChannel) throws IOException {
+        if (AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME.equals(cipherClass)) {
+            if (!Crypto.isNativeCodeLoaded()) {
+                return; // Skip this test if no JNI
+            }
+        }
+        
+        InputStream in = null;
+        OutputStream out = null;
+        try { // Test InvalidAlgorithmParameters
+               in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), 
+                    new SecretKeySpec(key, "AES"), new GCMParameterSpec(0, new 
byte[0]), 
+                    withChannel);
+            Assert.fail("Expected IOException.");
+        } catch (IOException ex) {
+            Assert.assertEquals(ex.getMessage(),"Illegal parameters");
+        } 
+        
+        try { // Test InvalidAlgorithmParameters
+            out = getCryptoOutputStream(transformation, props, baos, 
+                    new SecretKeySpec(key, "AES"), new GCMParameterSpec(0, 
+                    new byte[0]), withChannel);
+            Assert.fail("Expected IOException.");
+        } catch (IOException ex) {
+               Assert.assertEquals(ex.getMessage(),"Illegal parameters");
+        } 
+        
+        try { // Test Invalid Key
+            in = getCryptoInputStream(transformation,props, new 
ByteArrayInputStream(encData), 
+                    new SecretKeySpec(new byte[10], "AES"), new 
IvParameterSpec(iv), withChannel);
+            Assert.fail("Expected IOException for Invalid Key");
+        } catch (IOException ex) {
+            Assert.assertNotNull(ex);
+        }
+        
+        try { // Test Invalid Key

Review comment:
       ```suggestion
           // Test Invalid Key
           try {
   ```

##########
File path: 
src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
##########
@@ -197,46 +224,246 @@ protected void doByteBufferRead(final String 
cipherClass, final boolean withChan
                 getCipher(cipherClass), smallBufferSize, iv, withChannel);
         buf.clear();
         byteBufferReadCheck(in, buf, 11);
+        in.close();      
+        
+        // Direct buffer, default buffer size, initial buffer position is 0, 
final read
+        in = getCryptoInputStream(new ByteArrayInputStream(encData),
+                getCipher(cipherClass), smallBufferSize, iv, withChannel);
+        buf.clear();
+        byteBufferFinalReadCheck(in, buf, 0);
+        in.close();
+        
+        // Default buffer size, initial buffer position is 0, insufficient 
dest buffer length
+        in = getCryptoInputStream(new ByteArrayInputStream(encData),
+                getCipher(cipherClass), defaultBufferSize, iv, withChannel);
+        buf = ByteBuffer.allocate(100);
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+        
+        // Default buffer size, initial buffer position is 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf = ByteBuffer.allocate(dataLen + 100);
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+        
+        // Default buffer size, initial buffer position is not 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 11);
+        in.close();
+
+        // Small buffer size, initial buffer position is 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+
+        // Small buffer size, initial buffer position is not 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 11);
+        in.close();
+
+        // Direct buffer, default buffer size, initial buffer position is 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf = ByteBuffer.allocateDirect(dataLen + 100);
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+
+        // Direct buffer, default buffer size, initial buffer position is not 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 11);
+        in.close();
+
+        // Direct buffer, small buffer size, initial buffer position is 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+
+        // Direct buffer, small buffer size, initial buffer position is not 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 11);
+        in.close();
+        
+        // Direct buffer, default buffer size, initial buffer position is 0, 
final read
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferFinalReadCheck(in, buf, 0);
+        in.close();
+        
+        // Default buffer size, initial buffer position is 0, insufficient 
dest buffer length
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf = ByteBuffer.allocate(100);
+        byteBufferReadCheck(in, buf, 0);
         in.close();
     }
 
     protected void doByteBufferWrite(final String cipherClass,
-            final ByteArrayOutputStream baos, final boolean withChannel) 
throws Exception {
+            final ByteArrayOutputStream baos, final boolean withChannel) 
+                throws Exception {
         if (AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME.equals(cipherClass)) {
             if (!Crypto.isNativeCodeLoaded()) {
                 return; // Skip this test if no JNI
             }
         }
         baos.reset();
-        final CryptoOutputStream out = getCryptoOutputStream(baos,
+        CryptoOutputStream out = getCryptoOutputStream(baos,
                 getCipher(cipherClass), defaultBufferSize, iv, withChannel);
-        ByteBuffer buf = ByteBuffer.allocateDirect(dataLen / 2);
-        buf.put(data, 0, dataLen / 2);
-        buf.flip();
-        final int n1 = out.write(buf);
-
-        buf.clear();
-        buf.put(data, n1, dataLen / 3);
-        buf.flip();
-        final int n2 = out.write(buf);
-
-        buf.clear();
-        buf.put(data, n1 + n2, dataLen - n1 - n2);
-        buf.flip();
-        final int n3 = out.write(buf);
+        doByteBufferWrite(out, withChannel);
+        
+        baos.reset();
+        CryptoCipher cipher = getCipher(cipherClass);
+        String transformation = cipher.getAlgorithm();
+        out = getCryptoOutputStream(transformation, props, baos, key, 
+                new IvParameterSpec(iv), withChannel);
+        doByteBufferWrite(out, withChannel);
+        out.write(1);
+        Assert.assertTrue(out.isOpen()); 
+        
+        out = getCryptoOutputStream(transformation, props, baos, key, 
+                new IvParameterSpec(iv), withChannel);
+        out.close();
+        Assert.assertTrue(!out.isOpen());
+    }
 
-        Assert.assertEquals(dataLen, n1 + n2 + n3);
+    protected void doExceptionTest(final String cipherClass, 
ByteArrayOutputStream baos,
+            final boolean withChannel) throws IOException {
+        if (AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME.equals(cipherClass)) {
+            if (!Crypto.isNativeCodeLoaded()) {
+                return; // Skip this test if no JNI
+            }
+        }
+        
+        InputStream in = null;
+        OutputStream out = null;
+        try { // Test InvalidAlgorithmParameters
+               in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), 
+                    new SecretKeySpec(key, "AES"), new GCMParameterSpec(0, new 
byte[0]), 
+                    withChannel);
+            Assert.fail("Expected IOException.");
+        } catch (IOException ex) {
+            Assert.assertEquals(ex.getMessage(),"Illegal parameters");
+        } 
+        
+        try { // Test InvalidAlgorithmParameters
+            out = getCryptoOutputStream(transformation, props, baos, 
+                    new SecretKeySpec(key, "AES"), new GCMParameterSpec(0, 
+                    new byte[0]), withChannel);
+            Assert.fail("Expected IOException.");
+        } catch (IOException ex) {
+               Assert.assertEquals(ex.getMessage(),"Illegal parameters");
+        } 
+        
+        try { // Test Invalid Key
+            in = getCryptoInputStream(transformation,props, new 
ByteArrayInputStream(encData), 
+                    new SecretKeySpec(new byte[10], "AES"), new 
IvParameterSpec(iv), withChannel);
+            Assert.fail("Expected IOException for Invalid Key");
+        } catch (IOException ex) {
+            Assert.assertNotNull(ex);
+        }
+        
+        try { // Test Invalid Key
+            out = getCryptoOutputStream(transformation, props, baos, new 
byte[10], 
+                    new IvParameterSpec(iv), withChannel);
+            Assert.fail("Expected IOException for Invalid Key");
+        } catch (IOException ex) {
+            Assert.assertNotNull(ex);
+        }
+        
+        try { // Test reading a closed stream.

Review comment:
       I think you get the idea... (the suggestion editor is not very good so 
adding those is kinda painful.)

##########
File path: 
src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
##########
@@ -282,6 +570,24 @@ protected CryptoInputStream getCryptoInputStream(final 
ByteArrayInputStream bais
         return new CryptoInputStream(bais, cipher, bufferSize,
                 new SecretKeySpec(key, "AES"), new IvParameterSpec(iv));
     }
+    
+    protected CryptoInputStream getCryptoInputStream(final String 
transformation, final Properties props, 
+           final ByteArrayInputStream bais, final byte[] key, final 
AlgorithmParameterSpec params,
+           boolean withChannel) throws IOException {
+        if(withChannel) {

Review comment:
       ```suggestion
           if (withChannel) {
   ```
   
   But can't you just call the other `getCryptoInputStream`?

##########
File path: 
src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
##########
@@ -197,46 +224,246 @@ protected void doByteBufferRead(final String 
cipherClass, final boolean withChan
                 getCipher(cipherClass), smallBufferSize, iv, withChannel);
         buf.clear();
         byteBufferReadCheck(in, buf, 11);
+        in.close();      
+        
+        // Direct buffer, default buffer size, initial buffer position is 0, 
final read
+        in = getCryptoInputStream(new ByteArrayInputStream(encData),
+                getCipher(cipherClass), smallBufferSize, iv, withChannel);
+        buf.clear();
+        byteBufferFinalReadCheck(in, buf, 0);
+        in.close();
+        
+        // Default buffer size, initial buffer position is 0, insufficient 
dest buffer length
+        in = getCryptoInputStream(new ByteArrayInputStream(encData),
+                getCipher(cipherClass), defaultBufferSize, iv, withChannel);
+        buf = ByteBuffer.allocate(100);
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+        
+        // Default buffer size, initial buffer position is 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf = ByteBuffer.allocate(dataLen + 100);
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+        
+        // Default buffer size, initial buffer position is not 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 11);
+        in.close();
+
+        // Small buffer size, initial buffer position is 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+
+        // Small buffer size, initial buffer position is not 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 11);
+        in.close();
+
+        // Direct buffer, default buffer size, initial buffer position is 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf = ByteBuffer.allocateDirect(dataLen + 100);
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+
+        // Direct buffer, default buffer size, initial buffer position is not 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 11);
+        in.close();
+
+        // Direct buffer, small buffer size, initial buffer position is 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+
+        // Direct buffer, small buffer size, initial buffer position is not 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 11);
+        in.close();
+        
+        // Direct buffer, default buffer size, initial buffer position is 0, 
final read
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferFinalReadCheck(in, buf, 0);
+        in.close();
+        
+        // Default buffer size, initial buffer position is 0, insufficient 
dest buffer length
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf = ByteBuffer.allocate(100);
+        byteBufferReadCheck(in, buf, 0);
         in.close();
     }
 
     protected void doByteBufferWrite(final String cipherClass,
-            final ByteArrayOutputStream baos, final boolean withChannel) 
throws Exception {
+            final ByteArrayOutputStream baos, final boolean withChannel) 
+                throws Exception {
         if (AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME.equals(cipherClass)) {
             if (!Crypto.isNativeCodeLoaded()) {
                 return; // Skip this test if no JNI
             }
         }
         baos.reset();
-        final CryptoOutputStream out = getCryptoOutputStream(baos,
+        CryptoOutputStream out = getCryptoOutputStream(baos,
                 getCipher(cipherClass), defaultBufferSize, iv, withChannel);
-        ByteBuffer buf = ByteBuffer.allocateDirect(dataLen / 2);
-        buf.put(data, 0, dataLen / 2);
-        buf.flip();
-        final int n1 = out.write(buf);
-
-        buf.clear();
-        buf.put(data, n1, dataLen / 3);
-        buf.flip();
-        final int n2 = out.write(buf);
-
-        buf.clear();
-        buf.put(data, n1 + n2, dataLen - n1 - n2);
-        buf.flip();
-        final int n3 = out.write(buf);
+        doByteBufferWrite(out, withChannel);
+        
+        baos.reset();
+        CryptoCipher cipher = getCipher(cipherClass);
+        String transformation = cipher.getAlgorithm();
+        out = getCryptoOutputStream(transformation, props, baos, key, 
+                new IvParameterSpec(iv), withChannel);
+        doByteBufferWrite(out, withChannel);
+        out.write(1);
+        Assert.assertTrue(out.isOpen()); 
+        
+        out = getCryptoOutputStream(transformation, props, baos, key, 
+                new IvParameterSpec(iv), withChannel);
+        out.close();
+        Assert.assertTrue(!out.isOpen());
+    }
 
-        Assert.assertEquals(dataLen, n1 + n2 + n3);
+    protected void doExceptionTest(final String cipherClass, 
ByteArrayOutputStream baos,
+            final boolean withChannel) throws IOException {
+        if (AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME.equals(cipherClass)) {
+            if (!Crypto.isNativeCodeLoaded()) {
+                return; // Skip this test if no JNI
+            }
+        }
+        
+        InputStream in = null;
+        OutputStream out = null;
+        try { // Test InvalidAlgorithmParameters
+               in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), 
+                    new SecretKeySpec(key, "AES"), new GCMParameterSpec(0, new 
byte[0]), 
+                    withChannel);
+            Assert.fail("Expected IOException.");
+        } catch (IOException ex) {
+            Assert.assertEquals(ex.getMessage(),"Illegal parameters");
+        } 
+        
+        try { // Test InvalidAlgorithmParameters
+            out = getCryptoOutputStream(transformation, props, baos, 
+                    new SecretKeySpec(key, "AES"), new GCMParameterSpec(0, 
+                    new byte[0]), withChannel);
+            Assert.fail("Expected IOException.");
+        } catch (IOException ex) {
+               Assert.assertEquals(ex.getMessage(),"Illegal parameters");
+        } 
+        
+        try { // Test Invalid Key
+            in = getCryptoInputStream(transformation,props, new 
ByteArrayInputStream(encData), 
+                    new SecretKeySpec(new byte[10], "AES"), new 
IvParameterSpec(iv), withChannel);
+            Assert.fail("Expected IOException for Invalid Key");
+        } catch (IOException ex) {
+            Assert.assertNotNull(ex);
+        }
+        
+        try { // Test Invalid Key
+            out = getCryptoOutputStream(transformation, props, baos, new 
byte[10], 
+                    new IvParameterSpec(iv), withChannel);
+            Assert.fail("Expected IOException for Invalid Key");
+        } catch (IOException ex) {
+            Assert.assertNotNull(ex);
+        }
+        
+        try { // Test reading a closed stream.
+            in = getCryptoInputStream(new ByteArrayInputStream(encData), 
+                    getCipher(cipherClass), defaultBufferSize, iv, 
withChannel);
+            in.close();
+            in.read(); // Throw exception.
+        } catch (IOException ex) {
+            Assert.assertTrue(ex.getMessage().equals("Stream closed"));
+        } 
+        
+        try { // Test closing a closed stream.
+            in.close(); // Don't throw exception on double-close.
+        } catch (IOException ex) {
+            Assert.fail("Should not throw exception closing a closed stream.");
+        } 
+
+        try { // Test checking a closed stream.
+            out = getCryptoOutputStream(transformation, props, baos, key, new 
IvParameterSpec(iv), 
+                    withChannel);
+            out.close();
+            ((CryptoOutputStream)out).checkStream(); // Throw exception.
+        } catch (IOException ex) {
+            Assert.assertTrue(ex.getMessage().equals("Stream closed"));
+        } 
+
+        try { // Test closing a closed stream.
+            out.close(); // Don't throw exception.
+        } catch (IOException ex) {
+            Assert.fail("Should not throw exception closing a closed stream.");
+        } 
+        
+        try { // Test checkStreamCipher
+            CryptoInputStream.checkStreamCipher(getCipher(cipherClass));
+        } catch (IOException ex) {
+            Assert.assertTrue(ex.getMessage().equals("AES/CTR/NoPadding is 
required"));
+        } finally {
+            in.close();
+        }
 
-        out.flush();
+        try { // Test unsupported operation handling.
+            in = getCryptoInputStream(new ByteArrayInputStream(encData), 
+                    getCipher(cipherClass), defaultBufferSize, iv, false);
+            in.mark(0); // Should not throw an exception.

Review comment:
       Normally things that should not throw an exception should be outside the 
`try`. (You're also leaving the stream open but that's minor in a test.)

##########
File path: 
src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
##########
@@ -294,6 +600,26 @@ protected CryptoOutputStream getCryptoOutputStream(
         return new CryptoOutputStream(baos, cipher, bufferSize,
                 new SecretKeySpec(key, "AES"), new IvParameterSpec(iv));
     }
+    
+    protected CryptoOutputStream getCryptoOutputStream(final String 
transformation,

Review comment:
       can just call the other `getCryptoOutputStream`?

##########
File path: 
src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
##########
@@ -197,46 +224,246 @@ protected void doByteBufferRead(final String 
cipherClass, final boolean withChan
                 getCipher(cipherClass), smallBufferSize, iv, withChannel);
         buf.clear();
         byteBufferReadCheck(in, buf, 11);
+        in.close();      
+        
+        // Direct buffer, default buffer size, initial buffer position is 0, 
final read
+        in = getCryptoInputStream(new ByteArrayInputStream(encData),
+                getCipher(cipherClass), smallBufferSize, iv, withChannel);
+        buf.clear();
+        byteBufferFinalReadCheck(in, buf, 0);
+        in.close();
+        
+        // Default buffer size, initial buffer position is 0, insufficient 
dest buffer length
+        in = getCryptoInputStream(new ByteArrayInputStream(encData),
+                getCipher(cipherClass), defaultBufferSize, iv, withChannel);
+        buf = ByteBuffer.allocate(100);
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+        
+        // Default buffer size, initial buffer position is 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf = ByteBuffer.allocate(dataLen + 100);
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+        
+        // Default buffer size, initial buffer position is not 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 11);
+        in.close();
+
+        // Small buffer size, initial buffer position is 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+
+        // Small buffer size, initial buffer position is not 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 11);
+        in.close();
+
+        // Direct buffer, default buffer size, initial buffer position is 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf = ByteBuffer.allocateDirect(dataLen + 100);
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+
+        // Direct buffer, default buffer size, initial buffer position is not 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 11);
+        in.close();
+
+        // Direct buffer, small buffer size, initial buffer position is 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 0);
+        in.close();
+
+        // Direct buffer, small buffer size, initial buffer position is not 0
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferReadCheck(in, buf, 11);
+        in.close();
+        
+        // Direct buffer, default buffer size, initial buffer position is 0, 
final read
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf.clear();
+        byteBufferFinalReadCheck(in, buf, 0);
+        in.close();
+        
+        // Default buffer size, initial buffer position is 0, insufficient 
dest buffer length
+        in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), key, 
+                new IvParameterSpec(iv), withChannel);
+        buf = ByteBuffer.allocate(100);
+        byteBufferReadCheck(in, buf, 0);
         in.close();
     }
 
     protected void doByteBufferWrite(final String cipherClass,
-            final ByteArrayOutputStream baos, final boolean withChannel) 
throws Exception {
+            final ByteArrayOutputStream baos, final boolean withChannel) 
+                throws Exception {
         if (AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME.equals(cipherClass)) {
             if (!Crypto.isNativeCodeLoaded()) {
                 return; // Skip this test if no JNI
             }
         }
         baos.reset();
-        final CryptoOutputStream out = getCryptoOutputStream(baos,
+        CryptoOutputStream out = getCryptoOutputStream(baos,
                 getCipher(cipherClass), defaultBufferSize, iv, withChannel);
-        ByteBuffer buf = ByteBuffer.allocateDirect(dataLen / 2);
-        buf.put(data, 0, dataLen / 2);
-        buf.flip();
-        final int n1 = out.write(buf);
-
-        buf.clear();
-        buf.put(data, n1, dataLen / 3);
-        buf.flip();
-        final int n2 = out.write(buf);
-
-        buf.clear();
-        buf.put(data, n1 + n2, dataLen - n1 - n2);
-        buf.flip();
-        final int n3 = out.write(buf);
+        doByteBufferWrite(out, withChannel);
+        
+        baos.reset();
+        CryptoCipher cipher = getCipher(cipherClass);
+        String transformation = cipher.getAlgorithm();
+        out = getCryptoOutputStream(transformation, props, baos, key, 
+                new IvParameterSpec(iv), withChannel);
+        doByteBufferWrite(out, withChannel);
+        out.write(1);
+        Assert.assertTrue(out.isOpen()); 
+        
+        out = getCryptoOutputStream(transformation, props, baos, key, 
+                new IvParameterSpec(iv), withChannel);
+        out.close();
+        Assert.assertTrue(!out.isOpen());
+    }
 
-        Assert.assertEquals(dataLen, n1 + n2 + n3);
+    protected void doExceptionTest(final String cipherClass, 
ByteArrayOutputStream baos,
+            final boolean withChannel) throws IOException {
+        if (AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME.equals(cipherClass)) {
+            if (!Crypto.isNativeCodeLoaded()) {
+                return; // Skip this test if no JNI
+            }
+        }
+        
+        InputStream in = null;
+        OutputStream out = null;
+        try { // Test InvalidAlgorithmParameters
+               in = getCryptoInputStream(transformation, props, new 
ByteArrayInputStream(encData), 
+                    new SecretKeySpec(key, "AES"), new GCMParameterSpec(0, new 
byte[0]), 
+                    withChannel);
+            Assert.fail("Expected IOException.");
+        } catch (IOException ex) {
+            Assert.assertEquals(ex.getMessage(),"Illegal parameters");
+        } 
+        
+        try { // Test InvalidAlgorithmParameters
+            out = getCryptoOutputStream(transformation, props, baos, 
+                    new SecretKeySpec(key, "AES"), new GCMParameterSpec(0, 
+                    new byte[0]), withChannel);
+            Assert.fail("Expected IOException.");
+        } catch (IOException ex) {
+               Assert.assertEquals(ex.getMessage(),"Illegal parameters");
+        } 
+        
+        try { // Test Invalid Key
+            in = getCryptoInputStream(transformation,props, new 
ByteArrayInputStream(encData), 
+                    new SecretKeySpec(new byte[10], "AES"), new 
IvParameterSpec(iv), withChannel);
+            Assert.fail("Expected IOException for Invalid Key");
+        } catch (IOException ex) {
+            Assert.assertNotNull(ex);
+        }
+        
+        try { // Test Invalid Key
+            out = getCryptoOutputStream(transformation, props, baos, new 
byte[10], 
+                    new IvParameterSpec(iv), withChannel);
+            Assert.fail("Expected IOException for Invalid Key");
+        } catch (IOException ex) {
+            Assert.assertNotNull(ex);
+        }
+        
+        try { // Test reading a closed stream.
+            in = getCryptoInputStream(new ByteArrayInputStream(encData), 
+                    getCipher(cipherClass), defaultBufferSize, iv, 
withChannel);
+            in.close();
+            in.read(); // Throw exception.
+        } catch (IOException ex) {
+            Assert.assertTrue(ex.getMessage().equals("Stream closed"));
+        } 
+        
+        try { // Test closing a closed stream.
+            in.close(); // Don't throw exception on double-close.
+        } catch (IOException ex) {
+            Assert.fail("Should not throw exception closing a closed stream.");
+        } 
+
+        try { // Test checking a closed stream.
+            out = getCryptoOutputStream(transformation, props, baos, key, new 
IvParameterSpec(iv), 
+                    withChannel);
+            out.close();
+            ((CryptoOutputStream)out).checkStream(); // Throw exception.
+        } catch (IOException ex) {
+            Assert.assertTrue(ex.getMessage().equals("Stream closed"));
+        } 
+
+        try { // Test closing a closed stream.
+            out.close(); // Don't throw exception.
+        } catch (IOException ex) {
+            Assert.fail("Should not throw exception closing a closed stream.");
+        } 
+        
+        try { // Test checkStreamCipher
+            CryptoInputStream.checkStreamCipher(getCipher(cipherClass));
+        } catch (IOException ex) {
+            Assert.assertTrue(ex.getMessage().equals("AES/CTR/NoPadding is 
required"));
+        } finally {
+            in.close();
+        }
 
-        out.flush();
+        try { // Test unsupported operation handling.
+            in = getCryptoInputStream(new ByteArrayInputStream(encData), 
+                    getCipher(cipherClass), defaultBufferSize, iv, false);
+            in.mark(0); // Should not throw an exception.
+            assertEquals(false, in.markSupported());
+            in.reset();
+            Assert.fail("Expected IOException.");
+        } catch (IOException ex) {
+            Assert.assertTrue(ex.getMessage().equals("Mark/reset not 
supported"));
+        }  
+    }
 
-        try (InputStream in = getCryptoInputStream(
-                new ByteArrayInputStream(encData), getCipher(cipherClass),
-                defaultBufferSize, iv, withChannel)) {
-            buf = ByteBuffer.allocate(dataLen + 100);
-            byteBufferReadCheck(in, buf, 0);
+    protected void doFieldGetterTest(final String cipherClass, 
ByteArrayOutputStream baos,
+            final boolean withChannel) throws Exception {
+        if (AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME.equals(cipherClass)) {
+            if (!Crypto.isNativeCodeLoaded()) {
+                return; // Skip this test if no JNI
+            }
         }
-    }
+        
+        CryptoInputStream in = getCryptoInputStream(
+                new ByteArrayInputStream(encData), getCipher(cipherClass), 
defaultBufferSize, 
+                iv, withChannel); 
+
+        Properties props = new Properties();
+        String bufferSize = Integer.toString(defaultBufferSize / 2);
+        props.put(CryptoInputStream.STREAM_BUFFER_SIZE_KEY, bufferSize);
+
+        Assert.assertEquals(CryptoInputStream.getBufferSize(props), 
Integer.parseInt(bufferSize));
+        Assert.assertEquals(in.getBufferSize(), defaultBufferSize);
+        Assert.assertEquals(in.getCipher().getClass(), 
Class.forName(cipherClass));
+        Assert.assertEquals(in.getKey().getAlgorithm(), "AES");
+        Assert.assertEquals(in.getParams().getClass(), IvParameterSpec.class);
+        Assert.assertNotNull(in.getInput());
+
+        CryptoOutputStream out = getCryptoOutputStream(baos, 
getCipher(cipherClass), 
+                defaultBufferSize, iv, withChannel);
 
+        Assert.assertEquals(out.getOutBuffer().capacity(), defaultBufferSize + 
16);

Review comment:
       Is there a constant for the `16` anywhere?

##########
File path: 
src/test/java/org/apache/commons/crypto/stream/CtrCryptoStreamTest.java
##########
@@ -52,4 +75,115 @@ protected CtrCryptoOutputStream getCryptoOutputStream(
         }
         return new CtrCryptoOutputStream(baos, cipher, bufferSize, key, iv);
     }
+    
+    @Override
+    protected CtrCryptoOutputStream getCryptoOutputStream(final String 
transformation,
+            final Properties props, final ByteArrayOutputStream baos, final 
byte[] key, 
+            final AlgorithmParameterSpec params, final boolean withChannel) 
throws IOException {
+        if (withChannel) {
+            return new CtrCryptoOutputStream(props, Channels.newChannel(baos), 
key, 
+                    ((IvParameterSpec)params).getIV());
+        }
+        return new CtrCryptoOutputStream(props, baos, 
key,((IvParameterSpec)params).getIV());

Review comment:
       ```suggestion
           return new CtrCryptoOutputStream(props, baos, key, 
((IvParameterSpec)params).getIV());
   ```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to