Test tweaks, expecting the correct error

This commit is contained in:
Dave
2015-06-29 22:27:54 +02:00
parent 7f641d31e3
commit 59b2be78c1
3 changed files with 18 additions and 21 deletions

View File

@@ -77,7 +77,7 @@ public final class NoUtil {
} }
} }
private static byte[] getPbeKeyFromPassword(char[] password) { public static byte[] getPbeKeyFromPassword(char[] password) {
SecretKeyFactory skf; SecretKeyFactory skf;
try { try {
skf = SecretKeyFactory.getInstance(NoUtil.PBE_TYPE); skf = SecretKeyFactory.getInstance(NoUtil.PBE_TYPE);

View File

@@ -100,14 +100,14 @@ public class NoUserTest {
try { try {
NoUser.createUserFromFile(null, "password".toCharArray()); NoUser.createUserFromFile(null, "password".toCharArray());
fail("Should have thrown a NullPointerException."); fail("Should have thrown a IllegalArgumentException.");
} catch (NullPointerException e) { } catch (IllegalArgumentException e) {
// Do nothing, correct // Do nothing, correct
} }
try { try {
NoUser.createUserFromFile(null, null); NoUser.createUserFromFile(null, null);
fail("Should have thrown a NullPointerException."); fail("Should have thrown a IllegalArgumentException.");
} catch (NullPointerException e) { } catch (NullPointerException e) {
// Do nothing, correct // Do nothing, correct
} }

View File

@@ -126,15 +126,15 @@ public class NoUtilTest {
@Test @Test
public void testByteKeyEncryptionDecryptionAes() throws IllegalBlockSizeException, BadPaddingException { public void testByteKeyEncryptionDecryptionAes() throws IllegalBlockSizeException, BadPaddingException {
final byte[] originalBytes = {'s', 'o', 'm', 'e', 'b', 'y', 't', 'e', 's'}; 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[] 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); byte[] encryptedByByteKey = NoUtil.encrypt(bytes, byteKey);
try { 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."); fail("Did not throw BadPaddingException while decrypting with bad key.");
} catch (BadPaddingException e) { } catch (BadPaddingException e) {
// Do nothing, correct // Do nothing, correct
@@ -147,25 +147,21 @@ public class NoUtilTest {
try { try {
NoUtil.decrypt(nullByte, nullByte); NoUtil.decrypt(nullByte, nullByte);
fail("Allowed null parameter without thrown exception."); fail("Allowed null parameter without thrown exception.");
} catch (NullPointerException e) { } catch (IllegalArgumentException e) {
// Do nothing, correct // Do nothing, correct
} catch (IllegalBlockSizeException e) {
fail("Allowed null parameter without thrown exception.");
} catch (BadPaddingException e) {
fail("Allowed null parameter without thrown exception.");
} }
try { try {
NoUtil.decrypt(bytes, nullByte); NoUtil.decrypt(bytes, nullByte);
fail("Allowed null parameter without thrown exception."); fail("Allowed null parameter without thrown exception.");
} catch (NullPointerException e) { } catch (IllegalArgumentException e) {
// Do nothing, correct // Do nothing, correct
} }
try { try {
NoUtil.decrypt(nullByte, bytes); NoUtil.decrypt(nullByte, NoUtil.getPbeKeyFromPassword(password.toCharArray()));
fail("Allowed null parameter without thrown exception."); fail("Allowed null parameter without thrown exception.");
} catch (NullPointerException e) { } catch (IllegalArgumentException e) {
// Do nothing, correct // Do nothing, correct
} }
} }
@@ -181,12 +177,13 @@ public class NoUtilTest {
byte[] encryptedByCharKey = NoUtil.encrypt(bytes, charKey); byte[] encryptedByCharKey = NoUtil.encrypt(bytes, charKey);
try { 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."); fail("Did not throw BadPaddingException while decrypting with bad key.");
} catch (BadPaddingException e) { } catch (BadPaddingException e) {
// Do nothing, correct // Do nothing, correct
} }
charKey = Arrays.copyOf(originalCharKey, originalCharKey.length);
byte[] decryptedByCharKey = NoUtil.decrypt(encryptedByCharKey, charKey); byte[] decryptedByCharKey = NoUtil.decrypt(encryptedByCharKey, charKey);
assertTrue(Arrays.equals(originalBytes, decryptedByCharKey)); assertTrue(Arrays.equals(originalBytes, decryptedByCharKey));
@@ -195,21 +192,21 @@ public class NoUtilTest {
try { try {
NoUtil.decrypt(nullByte, nullChar); NoUtil.decrypt(nullByte, nullChar);
fail("Allowed null parameter without thrown exception."); fail("Allowed null parameter without thrown exception.");
} catch (NullPointerException e) { } catch (IllegalArgumentException e) {
// Do nothing, correct // Do nothing, correct
} }
try { try {
NoUtil.decrypt(bytes, nullChar); NoUtil.decrypt(bytes, nullChar);
fail("Allowed null parameter without thrown exception."); fail("Allowed null parameter without thrown exception.");
} catch (NullPointerException e) { } catch (IllegalArgumentException e) {
// Do nothing, correct // Do nothing, correct
} }
try { try {
NoUtil.decrypt(nullByte, new char[] {'c', 'h', 'a', 'r'}); NoUtil.decrypt(nullByte, new char[] {'c', 'h', 'a', 'r'});
fail("Allowed null parameter without thrown exception."); fail("Allowed null parameter without thrown exception.");
} catch (NullPointerException e) { } catch (IllegalArgumentException e) {
// Do nothing, correct // Do nothing, correct
} }
} }
@@ -226,7 +223,7 @@ public class NoUtilTest {
try { try {
NoUtil.decrypt(null); NoUtil.decrypt(null);
fail("Allowed null parameter without thrown exception."); fail("Allowed null parameter without thrown exception.");
} catch (NullPointerException e) { } catch (IllegalArgumentException e) {
// Do nothing, correct // Do nothing, correct
} }
} }