#364 Add test for bogus hashes

- Verify that a "hash" in the wrong format doesn't throw exception (this is relevant when the supportOldPasswordHash setting is enabled)
This commit is contained in:
ljacqu
2015-12-30 23:24:36 +01:00
parent c0a393b8b3
commit eea3697fa4
6 changed files with 35 additions and 3 deletions
@@ -32,6 +32,10 @@ public abstract class AbstractEncryptionMethodTest {
private static final List<String> INTERNAL_PASSWORDS =
ImmutableList.of("test1234", "Ab_C73", "(!#&$~`_-Aa0", "Ûïé1&?+A");
private static final String[] BOGUS_HASHES = {"", "test", "$t$test$", "$SHA$Test$$$$", "$$$$$",
"asdfg:hjkl", "::test", "~#$#~~~#$#~", "d41d8cd98f00b204e9800998ecf427e",
"$2y$7a$da641e404b982ed" };
/** The encryption method to test. */
private EncryptionMethod method;
/** Map with the hashes against which the entries in GIVEN_PASSWORDS are tested. */
@@ -123,6 +127,17 @@ public abstract class AbstractEncryptionMethodTest {
}
}
/** Tests various strings to ensure that encryption methods don't rely on the hash's format too much. */
@Test
public void testMalformedHashes() {
String salt = method.hasSeparateSalt() ? "testSalt" : null;
for (String bogusHash : BOGUS_HASHES) {
HashedPassword hashedPwd = new HashedPassword(bogusHash, salt);
assertFalse("Passing bogus hash '" + bogusHash + "' does not result in an error",
method.comparePassword("Password", hashedPwd, "player"));
}
}
private boolean doesGivenHashMatch(String password, EncryptionMethod method) {
return method.comparePassword(password, hashes.get(password), USERNAME);
}