Hashes: make tests faster when 'skip long tests' profile is enabled
This commit is contained in:
parent
f59a584622
commit
e224d62b7e
@ -1,10 +1,10 @@
|
|||||||
package fr.xephi.authme.security.crypts;
|
package fr.xephi.authme.security.crypts;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
|
import com.google.common.collect.ImmutableMap;
|
||||||
import fr.xephi.authme.security.crypts.description.AsciiRestricted;
|
import fr.xephi.authme.security.crypts.description.AsciiRestricted;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@ -20,12 +20,12 @@ import static org.junit.Assume.assumeThat;
|
|||||||
public abstract class AbstractEncryptionMethodTest {
|
public abstract class AbstractEncryptionMethodTest {
|
||||||
|
|
||||||
/** The username used to query {@link EncryptionMethod#comparePassword}. */
|
/** The username used to query {@link EncryptionMethod#comparePassword}. */
|
||||||
public static final String USERNAME = "Test_Player00";
|
private static final String USERNAME = "Test_Player00";
|
||||||
/**
|
/**
|
||||||
* List of passwords whose hash is provided to the class to test against; this verifies that previously constructed
|
* List of passwords whose hash is provided to the class to test against; this verifies that previously constructed
|
||||||
* hashes remain valid.
|
* hashes remain valid.
|
||||||
*/
|
*/
|
||||||
public static final String[] GIVEN_PASSWORDS = {"password", "PassWord1", "&^%te$t?Pw@_", "âË_3(íù*"};
|
private static final String[] GIVEN_PASSWORDS = {"password", "PassWord1", "&^%te$t?Pw@_", "âË_3(íù*"};
|
||||||
/**
|
/**
|
||||||
* List of passwords that are hashed at runtime and then tested against; this verifies that newly generated hashes
|
* List of passwords that are hashed at runtime and then tested against; this verifies that newly generated hashes
|
||||||
* are valid.
|
* are valid.
|
||||||
@ -51,26 +51,40 @@ public abstract class AbstractEncryptionMethodTest {
|
|||||||
private Map<String, HashedPassword> hashes;
|
private Map<String, HashedPassword> hashes;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new test for the given encryption method.
|
* Creates a new test for the given encryption method. This is for encryption methods that do not have
|
||||||
|
* a separate salt.
|
||||||
*
|
*
|
||||||
* @param method The encryption method to test
|
* @param method the encryption method to test
|
||||||
* @param computedHashes The pre-generated hashes for the elements in {@link #GIVEN_PASSWORDS}
|
* @param hash0 the pre-generated hash for the first entry in {@link #GIVEN_PASSWORDS}
|
||||||
|
* @param hash1 hash for second given password
|
||||||
|
* @param hash2 hash for third given password
|
||||||
|
* @param hash3 hash for fourth given password
|
||||||
*/
|
*/
|
||||||
public AbstractEncryptionMethodTest(EncryptionMethod method, String... computedHashes) {
|
public AbstractEncryptionMethodTest(EncryptionMethod method, String hash0,
|
||||||
|
String hash1, String hash2, String hash3) {
|
||||||
if (method.hasSeparateSalt()) {
|
if (method.hasSeparateSalt()) {
|
||||||
throw new UnsupportedOperationException("Test must be initialized with HashedPassword objects if "
|
throw new UnsupportedOperationException("Test must be initialized with HashedPassword objects if "
|
||||||
+ "the salt is stored separately. Use the other constructor");
|
+ "the salt is stored separately. Use the other constructor");
|
||||||
} else if (computedHashes.length != GIVEN_PASSWORDS.length) {
|
|
||||||
throw new UnsupportedOperationException("Expected " + GIVEN_PASSWORDS.length + " hashes");
|
|
||||||
}
|
}
|
||||||
this.method = method;
|
this.method = method;
|
||||||
|
|
||||||
hashes = new HashMap<>();
|
hashes = ImmutableMap.of(
|
||||||
for (int i = 0; i < GIVEN_PASSWORDS.length; ++i) {
|
GIVEN_PASSWORDS[0], new HashedPassword(hash0),
|
||||||
hashes.put(GIVEN_PASSWORDS[i], new HashedPassword(computedHashes[i]));
|
GIVEN_PASSWORDS[1], new HashedPassword(hash1),
|
||||||
}
|
GIVEN_PASSWORDS[2], new HashedPassword(hash2),
|
||||||
|
GIVEN_PASSWORDS[3], new HashedPassword(hash3));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new test for the given encryption method. This is for encryption methods that use
|
||||||
|
* a separate salt.
|
||||||
|
*
|
||||||
|
* @param method the encryption method to test
|
||||||
|
* @param result0 the pre-generated hash for the first entry in {@link #GIVEN_PASSWORDS}
|
||||||
|
* @param result1 hash for second given password
|
||||||
|
* @param result2 hash for third given password
|
||||||
|
* @param result3 hash for fourth given password
|
||||||
|
*/
|
||||||
public AbstractEncryptionMethodTest(EncryptionMethod method, HashedPassword result0, HashedPassword result1,
|
public AbstractEncryptionMethodTest(EncryptionMethod method, HashedPassword result0, HashedPassword result1,
|
||||||
HashedPassword result2, HashedPassword result3) {
|
HashedPassword result2, HashedPassword result3) {
|
||||||
if (!method.hasSeparateSalt()) {
|
if (!method.hasSeparateSalt()) {
|
||||||
@ -79,11 +93,11 @@ public abstract class AbstractEncryptionMethodTest {
|
|||||||
}
|
}
|
||||||
this.method = method;
|
this.method = method;
|
||||||
|
|
||||||
hashes = new HashMap<>();
|
hashes = ImmutableMap.of(
|
||||||
hashes.put(GIVEN_PASSWORDS[0], result0);
|
GIVEN_PASSWORDS[0], result0,
|
||||||
hashes.put(GIVEN_PASSWORDS[1], result1);
|
GIVEN_PASSWORDS[1], result1,
|
||||||
hashes.put(GIVEN_PASSWORDS[2], result2);
|
GIVEN_PASSWORDS[2], result2,
|
||||||
hashes.put(GIVEN_PASSWORDS[3], result3);
|
GIVEN_PASSWORDS[3], result3);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -127,6 +141,8 @@ public abstract class AbstractEncryptionMethodTest {
|
|||||||
|
|
||||||
assertTrue("Generated hash for '" + password + "' should match password (hash = '" + hash + "')",
|
assertTrue("Generated hash for '" + password + "' should match password (hash = '" + hash + "')",
|
||||||
method.comparePassword(password, hashedPassword, USERNAME));
|
method.comparePassword(password, hashedPassword, USERNAME));
|
||||||
|
assumeThat(SKIP_LONG_TESTS, equalTo(false));
|
||||||
|
|
||||||
if (!password.equals(password.toLowerCase())) {
|
if (!password.equals(password.toLowerCase())) {
|
||||||
assertFalse("Lower-case of '" + password + "' should not match generated hash '" + hash + "'",
|
assertFalse("Lower-case of '" + password + "' should not match generated hash '" + hash + "'",
|
||||||
method.comparePassword(password.toLowerCase(), hashedPassword, USERNAME));
|
method.comparePassword(password.toLowerCase(), hashedPassword, USERNAME));
|
||||||
@ -135,7 +151,6 @@ public abstract class AbstractEncryptionMethodTest {
|
|||||||
assertFalse("Upper-case of '" + password + "' should not match generated hash '" + hash + "'",
|
assertFalse("Upper-case of '" + password + "' should not match generated hash '" + hash + "'",
|
||||||
method.comparePassword(password.toUpperCase(), hashedPassword, USERNAME));
|
method.comparePassword(password.toUpperCase(), hashedPassword, USERNAME));
|
||||||
}
|
}
|
||||||
assumeThat(SKIP_LONG_TESTS, equalTo(false));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user