KeyCrypterScryptTest: Fix spurious test failure in testKeyCrypterWrongPassword().

This commit is contained in:
Andreas Schildbach
2019-04-09 12:31:57 +02:00
parent b567260189
commit 55612cd1dc

View File

@@ -44,6 +44,7 @@ public class KeyCrypterScryptTest {
private static final CharSequence PASSWORD2 = "0123456789"; private static final CharSequence PASSWORD2 = "0123456789";
private static final CharSequence WRONG_PASSWORD = "thisIsTheWrongPassword"; private static final CharSequence WRONG_PASSWORD = "thisIsTheWrongPassword";
private static final CharSequence WRONG_PASSWORD2 = "anotherWrongPassword";
private ScryptParameters scryptParameters; private ScryptParameters scryptParameters;
@@ -106,15 +107,21 @@ public class KeyCrypterScryptTest {
builder.append(i).append(" The quick brown fox"); builder.append(i).append(" The quick brown fox");
} }
EncryptedData data = keyCrypter.encrypt(builder.toString().getBytes(), keyCrypter.deriveKey(PASSWORD2)); byte[] plainText = builder.toString().getBytes();
EncryptedData data = keyCrypter.encrypt(plainText, keyCrypter.deriveKey(PASSWORD2));
assertNotNull(data); assertNotNull(data);
try { try {
keyCrypter.decrypt(data, keyCrypter.deriveKey(WRONG_PASSWORD)); // This sometimes doesn't throw due to relying on padding...
// TODO: This test sometimes fails due to relying on padding. byte[] cipherText = keyCrypter.decrypt(data, keyCrypter.deriveKey(WRONG_PASSWORD));
// ...so we also check for length, because that's the 2nd level test we're doing e.g. in ECKey/DeterministicKey...
assertNotEquals(plainText.length, cipherText.length);
// ...and then try with another wrong password again.
keyCrypter.decrypt(data, keyCrypter.deriveKey(WRONG_PASSWORD2));
// Note: it can still fail, but it should be extremely rare.
fail("Decrypt with wrong password did not throw exception"); fail("Decrypt with wrong password did not throw exception");
} catch (KeyCrypterException ede) { } catch (KeyCrypterException.InvalidCipherText x) {
assertTrue(ede.getMessage().contains("Could not decrypt")); // expected
} }
} }