2
0

Refactoring masterpassword-algorithm.

This commit is contained in:
Maarten Billemont
2018-05-16 00:12:31 -04:00
parent 8bdf1755b7
commit 0aa7baf59e
24 changed files with 468 additions and 479 deletions

View File

@@ -168,17 +168,13 @@ public class MPTestSuite implements Callable<Boolean> {
@Override
public Boolean call()
throws Exception {
return forEach( "mpw", new TestCase() {
@Override
public boolean run(final MPTests.Case testCase)
throws Exception {
MPMasterKey masterKey = new MPMasterKey( testCase.getFullName(), testCase.getMasterPassword().toCharArray() );
String sitePassword = masterKey.siteResult( testCase.getSiteName(), testCase.getSiteCounter(), testCase.getKeyPurpose(),
testCase.getKeyContext(), testCase.getResultType(),
null, testCase.getAlgorithm() );
return forEach( "mpw", testCase -> {
MPMasterKey masterKey = new MPMasterKey( testCase.getFullName(), testCase.getMasterPassword().toCharArray() );
String sitePassword = masterKey.siteResult( testCase.getSiteName(), testCase.getAlgorithm(), testCase.getSiteCounter(),
testCase.getKeyPurpose(), testCase.getKeyContext(),
testCase.getResultType(), null );
return testCase.getResult().equals( sitePassword );
}
return testCase.getResult().equals( sitePassword );
} );
}
@@ -192,12 +188,14 @@ public class MPTestSuite implements Callable<Boolean> {
}
@FunctionalInterface
public interface Listener {
void progress(int current, int max, String messageFormat, Object... args);
}
@FunctionalInterface
public interface TestCase {
boolean run(MPTests.Case testCase)

View File

@@ -176,7 +176,7 @@ public class MPTests {
@Nonnull
public MPAlgorithm getAlgorithm() {
return MPMasterKey.Version.fromInt( checkNotNull( algorithm ) ).getAlgorithm();
return MPAlgorithm.Version.fromInt( checkNotNull( algorithm ) ).getAlgorithm();
}
@Nonnull

View File

@@ -45,34 +45,30 @@ public class MPMasterKeyTest {
public void testMasterKey()
throws Exception {
testSuite.forEach( "testMasterKey", new MPTestSuite.TestCase() {
@Override
public boolean run(final MPTests.Case testCase)
throws Exception {
char[] masterPassword = testCase.getMasterPassword().toCharArray();
MPMasterKey masterKey = new MPMasterKey( testCase.getFullName(), masterPassword );
testSuite.forEach( "testMasterKey", testCase -> {
char[] masterPassword = testCase.getMasterPassword().toCharArray();
MPMasterKey masterKey = new MPMasterKey( testCase.getFullName(), masterPassword );
// Test key
assertEquals(
CodeUtils.encodeHex( masterKey.getKeyID( testCase.getAlgorithm() ) ),
testCase.getKeyID(),
"[testMasterKey] keyID mismatch: " + testCase );
// Test key
assertEquals(
CodeUtils.encodeHex( masterKey.getKeyID( testCase.getAlgorithm() ) ),
testCase.getKeyID(),
"[testMasterKey] keyID mismatch: " + testCase );
// Test invalidation
masterKey.invalidate();
try {
masterKey.getKeyID( testCase.getAlgorithm() );
fail( "[testMasterKey] invalidate ineffective: " + testCase );
}
catch (final MPKeyUnavailableException ignored) {
}
assertNotEquals(
masterPassword,
testCase.getMasterPassword().toCharArray(),
"[testMasterKey] masterPassword not wiped: " + testCase );
return true;
// Test invalidation
masterKey.invalidate();
try {
masterKey.getKeyID( testCase.getAlgorithm() );
fail( "[testMasterKey] invalidate ineffective: " + testCase );
}
catch (final MPKeyUnavailableException ignored) {
}
assertNotEquals(
masterPassword,
testCase.getMasterPassword().toCharArray(),
"[testMasterKey] masterPassword not wiped: " + testCase );
return true;
} );
}
@@ -80,23 +76,20 @@ public class MPMasterKeyTest {
public void testSiteResult()
throws Exception {
testSuite.forEach( "testSiteResult", new MPTestSuite.TestCase() {
@Override
public boolean run(final MPTests.Case testCase)
throws Exception {
char[] masterPassword = testCase.getMasterPassword().toCharArray();
MPMasterKey masterKey = new MPMasterKey( testCase.getFullName(), masterPassword );
testSuite.forEach( "testSiteResult", testCase -> {
char[] masterPassword = testCase.getMasterPassword().toCharArray();
MPMasterKey masterKey = new MPMasterKey( testCase.getFullName(), masterPassword );
// Test site result
assertEquals(
masterKey.siteResult( testCase.getSiteName(), testCase.getSiteCounter(), testCase.getKeyPurpose(),
testCase.getKeyContext(), testCase.getResultType(),
null, testCase.getAlgorithm() ),
testCase.getResult(),
"[testSiteResult] result mismatch: " + testCase );
// Test site result
assertEquals(
masterKey.siteResult( testCase.getSiteName(), testCase.getAlgorithm(), testCase.getSiteCounter(),
testCase.getKeyPurpose(),
testCase.getKeyContext(), testCase.getResultType(),
null ),
testCase.getResult(),
"[testSiteResult] result mismatch: " + testCase );
return true;
}
return true;
} );
}
@@ -110,14 +103,14 @@ public class MPMasterKeyTest {
String password = randomString( 8 );
MPResultType resultType = MPResultType.StoredPersonal;
for (final MPMasterKey.Version version : MPMasterKey.Version.values()) {
for (final MPAlgorithm.Version version : MPAlgorithm.Version.values()) {
MPAlgorithm algorithm = version.getAlgorithm();
// Test site state
String state = masterKey.siteState( testCase.getSiteName(), testCase.getSiteCounter(), testCase.getKeyPurpose(),
testCase.getKeyContext(), resultType, password, algorithm );
String result = masterKey.siteResult( testCase.getSiteName(), testCase.getSiteCounter(), testCase.getKeyPurpose(),
testCase.getKeyContext(), resultType, state, algorithm );
String state = masterKey.siteState( testCase.getSiteName(), algorithm, testCase.getSiteCounter(), testCase.getKeyPurpose(),
testCase.getKeyContext(), resultType, password );
String result = masterKey.siteResult( testCase.getSiteName(), algorithm, testCase.getSiteCounter(), testCase.getKeyPurpose(),
testCase.getKeyContext(), resultType, state );
assertEquals(
result,