You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by GitBox <gi...@apache.org> on 2020/04/26 16:59:13 UTC

[GitHub] [commons-crypto] aremily opened a new pull request #99: JaCoCo Increase for Streams

aremily opened a new pull request #99:
URL: https://github.com/apache/commons-crypto/pull/99


   Increases the stream package code coverage to 90% and the overall project coverage to 80%.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [commons-crypto] geoffreyblake commented on pull request #99: JaCoCo Increase for Streams

Posted by GitBox <gi...@apache.org>.
geoffreyblake commented on pull request #99:
URL: https://github.com/apache/commons-crypto/pull/99#issuecomment-622393754


   LGTM.  Need @vanzin to merge though.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [commons-crypto] vanzin commented on a change in pull request #99: JaCoCo Increase for Streams

Posted by GitBox <gi...@apache.org>.
vanzin commented on a change in pull request #99:
URL: https://github.com/apache/commons-crypto/pull/99#discussion_r421165813



##########
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:
       Wait, you're actually expecting this exception, at least now, so the comment is wrong. I'll remove the comment by using the suggestion thing and then merge.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [commons-crypto] geoffreyblake commented on a change in pull request #99: JaCoCo Increase for Streams

Posted by GitBox <gi...@apache.org>.
geoffreyblake commented on a change in pull request #99:
URL: https://github.com/apache/commons-crypto/pull/99#discussion_r417320698



##########
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());
+    }
+    
+    @Override
+    protected void doFieldGetterTest(final String cipherClass, 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
+            }
+        }
+        
+        StreamInput streamInput = new StreamInput(new ByteArrayInputStream(encData), 0);
+        try {
+            streamInput.seek(0);
+            Assert.fail("Expected UnsupportedOperationException.");
+        } catch (UnsupportedOperationException ex) {
+        	Assert.assertEquals(ex.getMessage(), "Seek is not supported by this implementation");

Review comment:
       Lets keep it as is then.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [commons-crypto] geoffreyblake commented on a change in pull request #99: JaCoCo Increase for Streams

Posted by GitBox <gi...@apache.org>.
geoffreyblake commented on a change in pull request #99:
URL: https://github.com/apache/commons-crypto/pull/99#discussion_r418545093



##########
File path: src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
##########
@@ -197,46 +224,236 @@ 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 closed stream.
+            in = getCryptoInputStream(new ByteArrayInputStream(encData), 
+                    getCipher(cipherClass), defaultBufferSize, iv, withChannel);
+            in.close();
+            in.close(); // Don't throw exception.
+            in.read(); // Throw exception.

Review comment:
       LGTM

##########
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());
+    }
+    
+    @Override
+    protected void doFieldGetterTest(final String cipherClass, 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
+            }
+        }
+        
+        StreamInput streamInput = new StreamInput(new ByteArrayInputStream(encData), 0);
+        try {
+            streamInput.seek(0);
+            Assert.fail("Expected UnsupportedOperationException.");
+        } catch (UnsupportedOperationException ex) {
+        	Assert.assertEquals(ex.getMessage(), "Seek is not supported by this implementation");

Review comment:
       LGTM




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [commons-crypto] aremily commented on a change in pull request #99: JaCoCo Increase for Streams

Posted by GitBox <gi...@apache.org>.
aremily commented on a change in pull request #99:
URL: https://github.com/apache/commons-crypto/pull/99#discussion_r418961005



##########
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:
       Unused constructors were a significant gap in code coverage, so it was an easy win to overload this method and use it to rerun existing tests against them.  Same with the getInputStream, as alluded to in an earlier comment.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [commons-crypto] aremily commented on a change in pull request #99: JaCoCo Increase for Streams

Posted by GitBox <gi...@apache.org>.
aremily commented on a change in pull request #99:
URL: https://github.com/apache/commons-crypto/pull/99#discussion_r416105218



##########
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());
+    }
+    
+    @Override
+    protected void doFieldGetterTest(final String cipherClass, 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
+            }
+        }
+        
+        StreamInput streamInput = new StreamInput(new ByteArrayInputStream(encData), 0);
+        try {
+            streamInput.seek(0);
+            Assert.fail("Expected UnsupportedOperationException.");
+        } catch (UnsupportedOperationException ex) {
+        	Assert.assertEquals(ex.getMessage(), "Seek is not supported by this implementation");

Review comment:
       Actually, now that I think about it I'd prefer it remained in.  I think it helps with readability.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [commons-crypto] aremily commented on a change in pull request #99: JaCoCo Increase for Streams

Posted by GitBox <gi...@apache.org>.
aremily commented on a change in pull request #99:
URL: https://github.com/apache/commons-crypto/pull/99#discussion_r416095434



##########
File path: src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
##########
@@ -197,46 +224,236 @@ 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 closed stream.
+            in = getCryptoInputStream(new ByteArrayInputStream(encData), 
+                    getCipher(cipherClass), defaultBufferSize, iv, withChannel);
+            in.close();
+            in.close(); // Don't throw exception.
+            in.read(); // Throw exception.
+        } catch (IOException ex) {
+            Assert.assertTrue(ex.getMessage().equals("Stream closed"));
+        } 
+
+        try { // Test closed stream.
+            out = getCryptoOutputStream(transformation, props, baos, key, new IvParameterSpec(iv), 
+                    withChannel);
+            out.close();
+            out.close(); // Don't throw exception.

Review comment:
       Same answer.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [commons-crypto] coveralls edited a comment on pull request #99: JaCoCo Increase for Streams

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #99:
URL: https://github.com/apache/commons-crypto/pull/99#issuecomment-619587463


   
   [![Coverage Status](https://coveralls.io/builds/30508890/badge)](https://coveralls.io/builds/30508890)
   
   Coverage increased (+6.2%) to 83.114% when pulling **089ee0427e7c78a55dc24446bc92e669d49e21ba on aremily:master** into **218b3db4149a53397f1e726d37cc3928e2e2135c on apache:master**.
   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [commons-crypto] aremily commented on a change in pull request #99: JaCoCo Increase for Streams

Posted by GitBox <gi...@apache.org>.
aremily commented on a change in pull request #99:
URL: https://github.com/apache/commons-crypto/pull/99#discussion_r418958911



##########
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:
       I'll close the stream.  I snuck the mark method in here only to add to coverage, and it seemed like the most logical place to put it.  I'm really testing that mark and reset are not supported.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [commons-crypto] geoffreyblake commented on a change in pull request #99: JaCoCo Increase for Streams

Posted by GitBox <gi...@apache.org>.
geoffreyblake commented on a change in pull request #99:
URL: https://github.com/apache/commons-crypto/pull/99#discussion_r415881524



##########
File path: src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
##########
@@ -197,46 +224,236 @@ 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 closed stream.
+            in = getCryptoInputStream(new ByteArrayInputStream(encData), 
+                    getCipher(cipherClass), defaultBufferSize, iv, withChannel);
+            in.close();
+            in.close(); // Don't throw exception.
+            in.read(); // Throw exception.

Review comment:
       Why 2 calls to close()?  

##########
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());
+    }
+    
+    @Override
+    protected void doFieldGetterTest(final String cipherClass, 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
+            }
+        }
+        
+        StreamInput streamInput = new StreamInput(new ByteArrayInputStream(encData), 0);
+        try {
+            streamInput.seek(0);
+            Assert.fail("Expected UnsupportedOperationException.");
+        } catch (UnsupportedOperationException ex) {
+        	Assert.assertEquals(ex.getMessage(), "Seek is not supported by this implementation");

Review comment:
       Isn't asserting the exception is generated enough?  The same goes for the below tests as well, why compare the error message?

##########
File path: src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
##########
@@ -197,46 +224,236 @@ 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 closed stream.
+            in = getCryptoInputStream(new ByteArrayInputStream(encData), 
+                    getCipher(cipherClass), defaultBufferSize, iv, withChannel);
+            in.close();
+            in.close(); // Don't throw exception.
+            in.read(); // Throw exception.
+        } catch (IOException ex) {
+            Assert.assertTrue(ex.getMessage().equals("Stream closed"));
+        } 
+
+        try { // Test closed stream.
+            out = getCryptoOutputStream(transformation, props, baos, key, new IvParameterSpec(iv), 
+                    withChannel);
+            out.close();
+            out.close(); // Don't throw exception.

Review comment:
       Same question here.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [commons-crypto] vanzin commented on a change in pull request #99: JaCoCo Increase for Streams

Posted by GitBox <gi...@apache.org>.
vanzin commented on a change in pull request #99:
URL: https://github.com/apache/commons-crypto/pull/99#discussion_r421165944



##########
File path: src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
##########
@@ -197,46 +224,261 @@ protected void doByteBufferRead(final String cipherClass, final boolean withChan
                 getCipher(cipherClass), smallBufferSize, iv, withChannel);
         buf.clear();
         byteBufferReadCheck(in, buf, 11);
+        in.close();      
+        
+        // Direct buffer, small 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);
-
-        Assert.assertEquals(dataLen, n1 + n2 + n3);
+        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());
+    }
 
-        out.flush();
+    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;
+        
+        // Test InvalidAlgorithmParameters
+        try {
+        	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");
+        } 
+        
+        // Test InvalidAlgorithmParameters
+        try {
+            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");
+        } 
+        
+        // Test Invalid Key
+        try {
+            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);
+        }
+        
+        // Test Invalid Key
+        try {
+            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);
+        }
+        
+        // Test reading a closed stream.
+        try { 
+            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"));
+        } 
+        
+        // Test closing a closed stream.
+        try { 
+            in.close(); // Don't throw exception on double-close.
+        } catch (IOException ex) {
+            Assert.fail("Should not throw exception closing a closed stream.");
+        } 
+
+        // Test checking a closed stream.
+        try { 
+            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"));
+        } 
+
+        // Test closing a closed stream.
+        try { 
+            out.close(); // Don't throw exception.
+        } catch (IOException ex) {
+            Assert.fail("Should not throw exception closing a closed stream.");
+        } 
+        
+        // Test checkStreamCipher
+        try { 
+            CryptoInputStream.checkStreamCipher(getCipher(cipherClass));
+        } catch (IOException ex) {
+            Assert.assertTrue(ex.getMessage().equals("AES/CTR/NoPadding is required"));
+        } finally {
+            in.close();
+        }
 
-        try (InputStream in = getCryptoInputStream(
-                new ByteArrayInputStream(encData), getCipher(cipherClass),
-                defaultBufferSize, iv, withChannel)) {
-            buf = ByteBuffer.allocate(dataLen + 100);
-            byteBufferReadCheck(in, buf, 0);
+        // Test unsupported operation handling.
+        try { 
+            in = getCryptoInputStream(new ByteArrayInputStream(encData), 
+                    getCipher(cipherClass), defaultBufferSize, iv, false);
+            in.mark(0); // Should not throw an exception.

Review comment:
       ```suggestion
               in.mark(0);
   ```




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [commons-crypto] coveralls commented on pull request #99: JaCoCo Increase for Streams

Posted by GitBox <gi...@apache.org>.
coveralls commented on pull request #99:
URL: https://github.com/apache/commons-crypto/pull/99#issuecomment-619587463


   
   [![Coverage Status](https://coveralls.io/builds/30350867/badge)](https://coveralls.io/builds/30350867)
   
   Coverage increased (+6.2%) to 83.114% when pulling **b1e5c6be805cf48fad7802f258b369911006bf3d on aremily:master** into **218b3db4149a53397f1e726d37cc3928e2e2135c on apache:master**.
   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [commons-crypto] coveralls edited a comment on pull request #99: JaCoCo Increase for Streams

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #99:
URL: https://github.com/apache/commons-crypto/pull/99#issuecomment-619587463


   
   [![Coverage Status](https://coveralls.io/builds/30478767/badge)](https://coveralls.io/builds/30478767)
   
   Coverage increased (+6.2%) to 83.114% when pulling **8e830c9d485b515d997fd779fc7f07c861b62944 on aremily:master** into **218b3db4149a53397f1e726d37cc3928e2e2135c on apache:master**.
   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [commons-crypto] aremily commented on pull request #99: JaCoCo Increase for Streams

Posted by GitBox <gi...@apache.org>.
aremily commented on pull request #99:
URL: https://github.com/apache/commons-crypto/pull/99#issuecomment-623202555


   The test timeouts were in the original codebase, so I just added them to the new tests to be consistent.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [commons-crypto] aremily commented on a change in pull request #99: JaCoCo Increase for Streams

Posted by GitBox <gi...@apache.org>.
aremily commented on a change in pull request #99:
URL: https://github.com/apache/commons-crypto/pull/99#discussion_r418960039



##########
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:
       I'll replace it with getBlockSize.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [commons-crypto] vanzin commented on pull request #99: JaCoCo Increase for Streams

Posted by GitBox <gi...@apache.org>.
vanzin commented on pull request #99:
URL: https://github.com/apache/commons-crypto/pull/99#issuecomment-624962946


   Test failure looks like the GCM test that I've seen mentioned as being flaky. Previous run had passed in any case. Merging.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [commons-crypto] aremily commented on a change in pull request #99: JaCoCo Increase for Streams

Posted by GitBox <gi...@apache.org>.
aremily commented on a change in pull request #99:
URL: https://github.com/apache/commons-crypto/pull/99#discussion_r417018129



##########
File path: src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
##########
@@ -197,46 +224,236 @@ 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 closed stream.
+            in = getCryptoInputStream(new ByteArrayInputStream(encData), 
+                    getCipher(cipherClass), defaultBufferSize, iv, withChannel);
+            in.close();
+            in.close(); // Don't throw exception.
+            in.read(); // Throw exception.

Review comment:
       Interesting point.  I did some digging.  In that scenario, if an IOException were thrown at the second call to close, the test would fail at line 393 unless the exception thrown happened to have the same message.  
   
   Nevertheless, I ran the tests with the branch in question removed and the test passes because the second close doesn't throw an exception at all (I stepped through it).  The method contract forces us to deal with an IOException, but it turns out that one isn't thrown in the existing implementation.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [commons-crypto] geoffreyblake commented on a change in pull request #99: JaCoCo Increase for Streams

Posted by GitBox <gi...@apache.org>.
geoffreyblake commented on a change in pull request #99:
URL: https://github.com/apache/commons-crypto/pull/99#discussion_r416643423



##########
File path: src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
##########
@@ -197,46 +224,236 @@ 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 closed stream.
+            in = getCryptoInputStream(new ByteArrayInputStream(encData), 
+                    getCipher(cipherClass), defaultBufferSize, iv, withChannel);
+            in.close();
+            in.close(); // Don't throw exception.
+            in.read(); // Throw exception.

Review comment:
       I see. Then should we split this test into 2 try blocks?  If someone removes that path you're testing with the 2nd close, the test will still pass because you'd catch an IOException from the 2nd close failing instead of from the read() call.
   
   To be honest, I'd expect a double close() to throw and exception, but obviously not in this case.

##########
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());
+    }
+    
+    @Override
+    protected void doFieldGetterTest(final String cipherClass, 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
+            }
+        }
+        
+        StreamInput streamInput = new StreamInput(new ByteArrayInputStream(encData), 0);
+        try {
+            streamInput.seek(0);
+            Assert.fail("Expected UnsupportedOperationException.");
+        } catch (UnsupportedOperationException ex) {
+        	Assert.assertEquals(ex.getMessage(), "Seek is not supported by this implementation");

Review comment:
       I agree it helps with readability.  My reservation is this is a brittle test if the generation method changes in the future, all these tests have to be changed.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [commons-crypto] aremily commented on a change in pull request #99: JaCoCo Increase for Streams

Posted by GitBox <gi...@apache.org>.
aremily commented on a change in pull request #99:
URL: https://github.com/apache/commons-crypto/pull/99#discussion_r418956700



##########
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:
       Nice catch.  The comment is a copy-paste oversight and I'll fix it.  I agree that these tests are largely redundant.  I needed to run different constructors and branch into some final logic in the read method to increase the code coverage.  I added a test that forced the read method to branch.  Then I just ran the tests again using a getCryptoInputSteam method that called different constructors.  It is a bit overkill, but I don't think it does any harm to run each test again against a separate constructor, as opposed to simply picking one test at random to cover the additional constructors.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [commons-crypto] aremily commented on a change in pull request #99: JaCoCo Increase for Streams

Posted by GitBox <gi...@apache.org>.
aremily commented on a change in pull request #99:
URL: https://github.com/apache/commons-crypto/pull/99#discussion_r417019278



##########
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());
+    }
+    
+    @Override
+    protected void doFieldGetterTest(final String cipherClass, 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
+            }
+        }
+        
+        StreamInput streamInput = new StreamInput(new ByteArrayInputStream(encData), 0);
+        try {
+            streamInput.seek(0);
+            Assert.fail("Expected UnsupportedOperationException.");
+        } catch (UnsupportedOperationException ex) {
+        	Assert.assertEquals(ex.getMessage(), "Seek is not supported by this implementation");

Review comment:
       I agree, but I think that's the point of the tests.  I would want a future developer to be conscious of the impact that changes had on the test and make necessary modifications, but I don't want to get into a philosophical discussion about software testing...




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [commons-crypto] aremily commented on a change in pull request #99: JaCoCo Increase for Streams

Posted by GitBox <gi...@apache.org>.
aremily commented on a change in pull request #99:
URL: https://github.com/apache/commons-crypto/pull/99#discussion_r417450230



##########
File path: src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
##########
@@ -197,46 +224,236 @@ 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 closed stream.
+            in = getCryptoInputStream(new ByteArrayInputStream(encData), 
+                    getCipher(cipherClass), defaultBufferSize, iv, withChannel);
+            in.close();
+            in.close(); // Don't throw exception.
+            in.read(); // Throw exception.

Review comment:
       Sure.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [commons-crypto] aremily commented on a change in pull request #99: JaCoCo Increase for Streams

Posted by GitBox <gi...@apache.org>.
aremily commented on a change in pull request #99:
URL: https://github.com/apache/commons-crypto/pull/99#discussion_r416095262



##########
File path: src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
##########
@@ -197,46 +224,236 @@ 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 closed stream.
+            in = getCryptoInputStream(new ByteArrayInputStream(encData), 
+                    getCipher(cipherClass), defaultBufferSize, iv, withChannel);
+            in.close();
+            in.close(); // Don't throw exception.
+            in.read(); // Throw exception.

Review comment:
       I needed to close a closed stream to hit the return branch and cover the line.  
     public void close() throws IOException {
           if (closed) {
               return;
           }




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [commons-crypto] vanzin commented on a change in pull request #99: JaCoCo Increase for Streams

Posted by GitBox <gi...@apache.org>.
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:
users@infra.apache.org



[GitHub] [commons-crypto] aremily commented on a change in pull request #99: JaCoCo Increase for Streams

Posted by GitBox <gi...@apache.org>.
aremily commented on a change in pull request #99:
URL: https://github.com/apache/commons-crypto/pull/99#discussion_r416101439



##########
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());
+    }
+    
+    @Override
+    protected void doFieldGetterTest(final String cipherClass, 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
+            }
+        }
+        
+        StreamInput streamInput = new StreamInput(new ByteArrayInputStream(encData), 0);
+        try {
+            streamInput.seek(0);
+            Assert.fail("Expected UnsupportedOperationException.");
+        } catch (UnsupportedOperationException ex) {
+        	Assert.assertEquals(ex.getMessage(), "Seek is not supported by this implementation");

Review comment:
       In this case I believe it would be enough.  IIRC, I was following an existing pattern for other tests which covered cases where different exceptions were caught, wrapped into a common exception and thrown as that more general exception, which was then only differentiated by the message.  I don't think it's hurting anything but I have no issue with you removing it.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [commons-crypto] coveralls edited a comment on pull request #99: JaCoCo Increase for Streams

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #99:
URL: https://github.com/apache/commons-crypto/pull/99#issuecomment-619587463


   
   [![Coverage Status](https://coveralls.io/builds/30613430/badge)](https://coveralls.io/builds/30613430)
   
   Coverage increased (+5.6%) to 82.566% when pulling **0b6515681a960279ac14a52371c86fbc5de7e3c8 on aremily:master** into **218b3db4149a53397f1e726d37cc3928e2e2135c on apache:master**.
   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [commons-crypto] aremily commented on pull request #99: JaCoCo Increase for Streams

Posted by GitBox <gi...@apache.org>.
aremily commented on pull request #99:
URL: https://github.com/apache/commons-crypto/pull/99#issuecomment-624355145


   @ Vanzin--I think I hit all your points.  Can you have another look at this
   PR?
   
   Alex
   
   On Fri, May 1, 2020 at 9:42 PM Marcelo Vanzin <no...@github.com>
   wrote:
   
   > *@vanzin* commented on this pull request.
   >
   > Just minor things. It's a bit weird to see timeouts in these tests. Do
   > they tend to hang? They shouldn't given what the code is doing.
   > ------------------------------
   >
   > In
   > src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
   > <https://github.com/apache/commons-crypto/pull/99#discussion_r418815328>:
   >
   > > @@ -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
   >
   >
   > 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.
   > ------------------------------
   >
   > In
   > src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
   > <https://github.com/apache/commons-crypto/pull/99#discussion_r418815800>:
   >
   > > +                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
   >
   >
   > ⬇️ Suggested change
   >
   > -        try { // Test InvalidAlgorithmParameters
   >
   > +        // Test InvalidAlgorithmParameters
   >
   > +        try {
   >
   >
   > ------------------------------
   >
   > In
   > src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
   > <https://github.com/apache/commons-crypto/pull/99#discussion_r418816064>:
   >
   > >
   >
   > -        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
   >
   >
   > ⬇️ Suggested change
   >
   > -        try { // Test InvalidAlgorithmParameters
   >
   > +        // Test InvalidAlgorithmParameters
   >
   > +        try {
   >
   >
   > ------------------------------
   >
   > In
   > src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
   > <https://github.com/apache/commons-crypto/pull/99#discussion_r418816208>:
   >
   > > +                    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
   >
   >
   > ⬇️ Suggested change
   >
   > -        try { // Test Invalid Key
   >
   > +        // Test Invalid Key
   >
   > +        try {
   >
   >
   > ------------------------------
   >
   > In
   > src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
   > <https://github.com/apache/commons-crypto/pull/99#discussion_r418816452>:
   >
   > > +                    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
   >
   >
   > ⬇️ Suggested change
   >
   > -        try { // Test Invalid Key
   >
   > +        // Test Invalid Key
   >
   > +        try {
   >
   >
   > ------------------------------
   >
   > In
   > src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
   > <https://github.com/apache/commons-crypto/pull/99#discussion_r418816797>:
   >
   > > +            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.
   >
   >
   > I think you get the idea... (the suggestion editor is not very good so
   > adding those is kinda painful.)
   > ------------------------------
   >
   > In
   > src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
   > <https://github.com/apache/commons-crypto/pull/99#discussion_r418817414>:
   >
   > >
   >
   > -        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.
   >
   >
   > 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.)
   > ------------------------------
   >
   > In
   > src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
   > <https://github.com/apache/commons-crypto/pull/99#discussion_r418818110>:
   >
   > >
   >
   > +        Assert.assertEquals(out.getOutBuffer().capacity(), defaultBufferSize + 16);
   >
   >
   > Is there a constant for the 16 anywhere?
   > ------------------------------
   >
   > In
   > src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
   > <https://github.com/apache/commons-crypto/pull/99#discussion_r418818937>:
   >
   > > @@ -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) {
   >
   >
   > ⬇️ Suggested change
   >
   > -        if(withChannel) {
   >
   > +        if (withChannel) {
   >
   >
   > But can't you just call the other getCryptoInputStream?
   > ------------------------------
   >
   > In
   > src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
   > <https://github.com/apache/commons-crypto/pull/99#discussion_r418819285>:
   >
   > > @@ -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,
   >
   >
   > can just call the other getCryptoOutputStream?
   > ------------------------------
   >
   > In src/test/java/org/apache/commons/crypto/stream/CtrCryptoStreamTest.java
   > <https://github.com/apache/commons-crypto/pull/99#discussion_r418819992>:
   >
   > > @@ -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());
   >
   >
   > ⬇️ Suggested change
   >
   > -        return new CtrCryptoOutputStream(props, baos, key,((IvParameterSpec)params).getIV());
   >
   > +        return new CtrCryptoOutputStream(props, baos, key, ((IvParameterSpec)params).getIV());
   >
   >
   > —
   > You are receiving this because you authored the thread.
   > Reply to this email directly, view it on GitHub
   > <https://github.com/apache/commons-crypto/pull/99#pullrequestreview-404488215>,
   > or unsubscribe
   > <https://github.com/notifications/unsubscribe-auth/ABUDY7Q52PCRNQYWKCKNVG3RPN3CBANCNFSM4MRJ5JTQ>
   > .
   >
   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [commons-crypto] coveralls edited a comment on pull request #99: JaCoCo Increase for Streams

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #99:
URL: https://github.com/apache/commons-crypto/pull/99#issuecomment-619587463


   
   [![Coverage Status](https://coveralls.io/builds/30510199/badge)](https://coveralls.io/builds/30510199)
   
   Coverage increased (+6.2%) to 83.114% when pulling **5af3837e8a0d81e2c6b853bbee5d44f6137a60f3 on aremily:master** into **218b3db4149a53397f1e726d37cc3928e2e2135c on apache:master**.
   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [commons-crypto] geoffreyblake commented on a change in pull request #99: JaCoCo Increase for Streams

Posted by GitBox <gi...@apache.org>.
geoffreyblake commented on a change in pull request #99:
URL: https://github.com/apache/commons-crypto/pull/99#discussion_r417316329



##########
File path: src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
##########
@@ -197,46 +224,236 @@ 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 closed stream.
+            in = getCryptoInputStream(new ByteArrayInputStream(encData), 
+                    getCipher(cipherClass), defaultBufferSize, iv, withChannel);
+            in.close();
+            in.close(); // Don't throw exception.
+            in.read(); // Throw exception.

Review comment:
       Can you still split the test into 2?  One to test double close(), and one to test operating on a closed stream?  That way it is explicit there are two cases being tested here.




----------------------------------------------------------------
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:
users@infra.apache.org