From 59b2be78c140913e541b9229f5c3d95c3956b43e Mon Sep 17 00:00:00 2001 From: Dave Date: Mon, 29 Jun 2015 22:27:54 +0200 Subject: [PATCH] Test tweaks, expecting the correct error --- src/nodash/core/NoUtil.java | 2 +- src/nodash/test/NoUserTest.java | 6 +++--- src/nodash/test/NoUtilTest.java | 31 ++++++++++++++----------------- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/nodash/core/NoUtil.java b/src/nodash/core/NoUtil.java index d33e79c..b2ec433 100644 --- a/src/nodash/core/NoUtil.java +++ b/src/nodash/core/NoUtil.java @@ -77,7 +77,7 @@ public final class NoUtil { } } - private static byte[] getPbeKeyFromPassword(char[] password) { + public static byte[] getPbeKeyFromPassword(char[] password) { SecretKeyFactory skf; try { skf = SecretKeyFactory.getInstance(NoUtil.PBE_TYPE); diff --git a/src/nodash/test/NoUserTest.java b/src/nodash/test/NoUserTest.java index 63b64e4..371a084 100644 --- a/src/nodash/test/NoUserTest.java +++ b/src/nodash/test/NoUserTest.java @@ -100,14 +100,14 @@ public class NoUserTest { try { NoUser.createUserFromFile(null, "password".toCharArray()); - fail("Should have thrown a NullPointerException."); - } catch (NullPointerException e) { + fail("Should have thrown a IllegalArgumentException."); + } catch (IllegalArgumentException e) { // Do nothing, correct } try { NoUser.createUserFromFile(null, null); - fail("Should have thrown a NullPointerException."); + fail("Should have thrown a IllegalArgumentException."); } catch (NullPointerException e) { // Do nothing, correct } diff --git a/src/nodash/test/NoUtilTest.java b/src/nodash/test/NoUtilTest.java index 063e406..c6118cd 100644 --- a/src/nodash/test/NoUtilTest.java +++ b/src/nodash/test/NoUtilTest.java @@ -126,15 +126,15 @@ public class NoUtilTest { @Test public void testByteKeyEncryptionDecryptionAes() throws IllegalBlockSizeException, BadPaddingException { final byte[] originalBytes = {'s', 'o', 'm', 'e', 'b', 'y', 't', 'e', 's'}; - final byte[] originalByteKey = {'p', 'a', 's', 's', 'w', 'o', 'r', 'd'}; + final String password = "password"; byte[] bytes = Arrays.copyOf(originalBytes, originalBytes.length); - byte[] byteKey = Arrays.copyOf(originalByteKey, originalByteKey.length); + byte[] byteKey = NoUtil.getPbeKeyFromPassword(password.toCharArray()); byte[] encryptedByByteKey = NoUtil.encrypt(bytes, byteKey); try { - NoUtil.decrypt(encryptedByByteKey, new byte[] {'b', 'a', 'd', 'k', 'e', 'y'}); + NoUtil.decrypt(encryptedByByteKey, NoUtil.getPbeKeyFromPassword("badpass".toCharArray())); fail("Did not throw BadPaddingException while decrypting with bad key."); } catch (BadPaddingException e) { // Do nothing, correct @@ -147,25 +147,21 @@ public class NoUtilTest { try { NoUtil.decrypt(nullByte, nullByte); fail("Allowed null parameter without thrown exception."); - } catch (NullPointerException e) { + } catch (IllegalArgumentException e) { // Do nothing, correct - } catch (IllegalBlockSizeException e) { - fail("Allowed null parameter without thrown exception."); - } catch (BadPaddingException e) { - fail("Allowed null parameter without thrown exception."); } try { NoUtil.decrypt(bytes, nullByte); fail("Allowed null parameter without thrown exception."); - } catch (NullPointerException e) { + } catch (IllegalArgumentException e) { // Do nothing, correct } try { - NoUtil.decrypt(nullByte, bytes); + NoUtil.decrypt(nullByte, NoUtil.getPbeKeyFromPassword(password.toCharArray())); fail("Allowed null parameter without thrown exception."); - } catch (NullPointerException e) { + } catch (IllegalArgumentException e) { // Do nothing, correct } } @@ -181,12 +177,13 @@ public class NoUtilTest { byte[] encryptedByCharKey = NoUtil.encrypt(bytes, charKey); try { - NoUtil.decrypt(encryptedByCharKey, new byte[] {'b', 'a', 'd', 'k', 'e', 'y'}); + NoUtil.decrypt(encryptedByCharKey, new char[] {'b', 'a', 'd', 'k', 'e', 'y'}); fail("Did not throw BadPaddingException while decrypting with bad key."); } catch (BadPaddingException e) { // Do nothing, correct } - + + charKey = Arrays.copyOf(originalCharKey, originalCharKey.length); byte[] decryptedByCharKey = NoUtil.decrypt(encryptedByCharKey, charKey); assertTrue(Arrays.equals(originalBytes, decryptedByCharKey)); @@ -195,21 +192,21 @@ public class NoUtilTest { try { NoUtil.decrypt(nullByte, nullChar); fail("Allowed null parameter without thrown exception."); - } catch (NullPointerException e) { + } catch (IllegalArgumentException e) { // Do nothing, correct } try { NoUtil.decrypt(bytes, nullChar); fail("Allowed null parameter without thrown exception."); - } catch (NullPointerException e) { + } catch (IllegalArgumentException e) { // Do nothing, correct } try { NoUtil.decrypt(nullByte, new char[] {'c', 'h', 'a', 'r'}); fail("Allowed null parameter without thrown exception."); - } catch (NullPointerException e) { + } catch (IllegalArgumentException e) { // Do nothing, correct } } @@ -226,7 +223,7 @@ public class NoUtilTest { try { NoUtil.decrypt(null); fail("Allowed null parameter without thrown exception."); - } catch (NullPointerException e) { + } catch (IllegalArgumentException e) { // Do nothing, correct } }