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/21 23:45:00 UTC
[GitHub] [commons-crypto] vanzin commented on a change in pull request #97: Additional unit tests for JNA, Cipher, Random, Utils testing error inputs
vanzin commented on a change in pull request #97:
URL: https://github.com/apache/commons-crypto/pull/97#discussion_r412565939
##########
File path: src/test/java/org/apache/commons/crypto/cipher/AbstractCipherTest.java
##########
@@ -147,6 +153,75 @@ public void cryptoTest() throws Exception {
}
}
+ @Test
+ public void testNullTransform() throws Exception {
+ for (String transform : transformations) {
Review comment:
`transform` is not used in the body, is the loop really needed?
(I guess not. Then you can also use `@Test(expected=RuntimeException.class)`.)
##########
File path: src/test/java/org/apache/commons/crypto/cipher/AbstractCipherTest.java
##########
@@ -147,6 +153,75 @@ public void cryptoTest() throws Exception {
}
}
+ @Test
+ public void testNullTransform() throws Exception {
+ for (String transform : transformations) {
+ try {
+ final CryptoCipher cipher = getCipher(null);
+ Assert.fail("Expected RuntimeException via NullPointerException");
+ } catch (final RuntimeException e) {
+ }
+ }
+ }
+
+ @Test
+ public void testInvalidTransform() throws Exception {
+ for (String transform : transformations) {
+ try {
+ final CryptoCipher cipher = getCipher("AES/CBR/NoPadding/garbage/garbage");
+ Assert.fail("Expected RuntimeException via a InvocationTargetException");
+ } catch (final RuntimeException e) {
+ }
+ }
+ }
+
+ @Test
+ public void testInvalidKey() throws Exception {
+ for (String transform : transformations) {
+ try {
+ final CryptoCipher cipher = getCipher(transform);
+ Assert.assertNotNull(cipher);
+
+ final byte[] invalidKey = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
+ 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x11 };
+ cipher.init(OpenSsl.ENCRYPT_MODE, new SecretKeySpec(invalidKey, "AES"), new IvParameterSpec(IV));
+ Assert.fail("Expected InvalidKeyException");
+ } catch (final InvalidKeyException ike) {
+ }
+ }
+ }
+
+ //@Test(expected = InvalidAlgorithmParameterException.class, timeout = 120000)
Review comment:
delete
##########
File path: src/test/java/org/apache/commons/crypto/cipher/AbstractCipherTest.java
##########
@@ -147,6 +153,75 @@ public void cryptoTest() throws Exception {
}
}
+ @Test
+ public void testNullTransform() throws Exception {
+ for (String transform : transformations) {
+ try {
+ final CryptoCipher cipher = getCipher(null);
+ Assert.fail("Expected RuntimeException via NullPointerException");
+ } catch (final RuntimeException e) {
+ }
+ }
+ }
+
+ @Test
+ public void testInvalidTransform() throws Exception {
+ for (String transform : transformations) {
Review comment:
Same question.
##########
File path: src/test/java/org/apache/commons/crypto/utils/UtilsTest.java
##########
@@ -41,4 +42,13 @@ public void testSplitOmitEmptyLine() {
public void testSplitNull() {
Assert.assertEquals(Collections.<String> emptyList(), Utils.splitClassNames(null, ","));
}
+ @Test
Review comment:
nit: add blank line between methods
##########
File path: src/test/java/org/apache/commons/crypto/cipher/AbstractCipherTest.java
##########
@@ -147,6 +153,75 @@ public void cryptoTest() throws Exception {
}
}
+ @Test
+ public void testNullTransform() throws Exception {
+ for (String transform : transformations) {
+ try {
+ final CryptoCipher cipher = getCipher(null);
+ Assert.fail("Expected RuntimeException via NullPointerException");
+ } catch (final RuntimeException e) {
+ }
+ }
+ }
+
+ @Test
+ public void testInvalidTransform() throws Exception {
+ for (String transform : transformations) {
+ try {
+ final CryptoCipher cipher = getCipher("AES/CBR/NoPadding/garbage/garbage");
+ Assert.fail("Expected RuntimeException via a InvocationTargetException");
+ } catch (final RuntimeException e) {
+ }
+ }
+ }
+
+ @Test
+ public void testInvalidKey() throws Exception {
+ for (String transform : transformations) {
+ try {
+ final CryptoCipher cipher = getCipher(transform);
+ Assert.assertNotNull(cipher);
+
+ final byte[] invalidKey = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
+ 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x11 };
+ cipher.init(OpenSsl.ENCRYPT_MODE, new SecretKeySpec(invalidKey, "AES"), new IvParameterSpec(IV));
+ Assert.fail("Expected InvalidKeyException");
+ } catch (final InvalidKeyException ike) {
+ }
+ }
+ }
+
+ //@Test(expected = InvalidAlgorithmParameterException.class, timeout = 120000)
+ @Test
+ public void testInvalidIV() throws Exception {
+ for (String transform : transformations) {
+ try {
+ final CryptoCipher cipher = getCipher(transform);
+ Assert.assertNotNull(cipher);
+
+ final byte[] invalidIV = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
+ 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x11 };
+ cipher.init(OpenSsl.ENCRYPT_MODE, new SecretKeySpec(KEY, "AES"), new IvParameterSpec(invalidIV));
+ Assert.fail("Expected InvalidAlgorithmParameterException");
+ } catch (final InvalidAlgorithmParameterException iape) {
+ }
+ }
+ }
+
+ @Test
+ public void testInvalidIVClass() throws Exception {
+ for (String transform : transformations) {
+ try {
+ final CryptoCipher cipher = getCipher(transform);
Review comment:
Funky indentation.
##########
File path: src/test/java/org/apache/commons/crypto/cipher/OpenSslCipherTest.java
##########
@@ -136,6 +137,15 @@ public void testInvalidIV() throws Exception {
cipher.init(OpenSsl.ENCRYPT_MODE, KEY, new IvParameterSpec(invalidIV));
}
+ @Test(expected = InvalidAlgorithmParameterException.class, timeout = 120000)
+ public void testInvalidIVClass() throws Exception {
+ final OpenSsl cipher = OpenSsl.getInstance("AES/CTR/NoPadding");
+ Assert.assertNotNull(cipher);
+
+ cipher.init(OpenSsl.ENCRYPT_MODE, KEY, new GCMParameterSpec(IV.length, IV));
+ Assert.fail("Should have caught an InvalidAlgorithmParameterException");
Review comment:
Unnecessary (`@Test` tag covers 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