2014-12-22 04:45:19 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#define ftl(...) do { fprintf( stderr, __VA_ARGS__ ); exit(2); } while (0)
|
|
|
|
|
|
|
|
#include "mpw-algorithm.h"
|
|
|
|
#include "mpw-util.h"
|
|
|
|
|
|
|
|
#include "mpw-tests-util.h"
|
|
|
|
|
|
|
|
int main(int argc, char *const argv[]) {
|
|
|
|
|
|
|
|
int failedTests = 0;
|
|
|
|
|
|
|
|
xmlNodePtr tests = xmlDocGetRootElement( xmlParseFile( "mpw_tests.xml" ) );
|
2017-08-01 21:13:30 +00:00
|
|
|
if (!tests) {
|
2017-04-08 18:25:54 +00:00
|
|
|
ftl( "Couldn't find test case: mpw_tests.xml\n" );
|
2017-08-01 21:13:30 +00:00
|
|
|
abort();
|
|
|
|
}
|
2017-04-08 18:25:54 +00:00
|
|
|
|
2014-12-22 04:45:19 +00:00
|
|
|
for (xmlNodePtr testCase = tests->children; testCase; testCase = testCase->next) {
|
|
|
|
if (testCase->type != XML_ELEMENT_NODE || xmlStrcmp( testCase->name, BAD_CAST "case" ) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Read in the test case.
|
|
|
|
xmlChar *id = mpw_xmlTestCaseString( testCase, "id" );
|
2017-08-01 12:31:39 +00:00
|
|
|
MPAlgorithmVersion algorithm = (MPAlgorithmVersion)mpw_xmlTestCaseInteger( testCase, "algorithm" );
|
2014-12-22 04:45:19 +00:00
|
|
|
xmlChar *fullName = mpw_xmlTestCaseString( testCase, "fullName" );
|
|
|
|
xmlChar *masterPassword = mpw_xmlTestCaseString( testCase, "masterPassword" );
|
|
|
|
xmlChar *keyID = mpw_xmlTestCaseString( testCase, "keyID" );
|
|
|
|
xmlChar *siteName = mpw_xmlTestCaseString( testCase, "siteName" );
|
|
|
|
uint32_t siteCounter = mpw_xmlTestCaseInteger( testCase, "siteCounter" );
|
2017-08-01 17:45:54 +00:00
|
|
|
xmlChar *passwordTypeString = mpw_xmlTestCaseString( testCase, "passwordType" );
|
|
|
|
xmlChar *keyPurposeString = mpw_xmlTestCaseString( testCase, "keyPurpose" );
|
|
|
|
xmlChar *keyContext = mpw_xmlTestCaseString( testCase, "keyContext" );
|
2014-12-22 04:45:19 +00:00
|
|
|
xmlChar *result = mpw_xmlTestCaseString( testCase, "result" );
|
|
|
|
|
2017-08-01 17:45:54 +00:00
|
|
|
MPPasswordType siteType = mpw_typeWithName( (char *)passwordTypeString );
|
|
|
|
MPKeyPurpose siteVariant = mpw_purposeWithName( (char *)keyPurposeString );
|
2014-12-22 04:45:19 +00:00
|
|
|
|
|
|
|
// Run the test case.
|
|
|
|
fprintf( stdout, "test case %s... ", id );
|
2015-02-18 22:32:33 +00:00
|
|
|
if (!xmlStrlen( result )) {
|
|
|
|
fprintf( stdout, "abstract.\n" );
|
|
|
|
continue;
|
|
|
|
}
|
2014-12-22 04:45:19 +00:00
|
|
|
|
|
|
|
// 1. calculate the master key.
|
2017-08-01 12:31:39 +00:00
|
|
|
MPMasterKey masterKey = mpw_masterKey(
|
2015-02-18 22:32:33 +00:00
|
|
|
(char *)fullName, (char *)masterPassword, algorithm );
|
2017-08-01 21:13:30 +00:00
|
|
|
if (!masterKey) {
|
2017-08-04 13:36:03 +00:00
|
|
|
ftl( "Couldn't derive master key.\n" );
|
2017-08-01 21:13:30 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-12-22 04:45:19 +00:00
|
|
|
|
|
|
|
// 2. calculate the site password.
|
2017-08-01 12:31:39 +00:00
|
|
|
MPSiteKey siteKey = mpw_siteKey(
|
2017-08-01 17:45:54 +00:00
|
|
|
masterKey, (char *)siteName, siteCounter, siteVariant, (char *)keyContext, algorithm );
|
2017-08-01 12:31:39 +00:00
|
|
|
const char *sitePassword = mpw_sitePassword(
|
|
|
|
siteKey, siteType, algorithm );
|
2017-07-16 01:13:49 +00:00
|
|
|
mpw_free( masterKey, MPMasterKeySize );
|
2017-08-01 12:31:39 +00:00
|
|
|
mpw_free( siteKey, MPSiteKeySize );
|
2017-08-01 21:13:30 +00:00
|
|
|
if (!sitePassword) {
|
2017-08-04 13:36:03 +00:00
|
|
|
ftl( "Couldn't derive site password.\n" );
|
2017-08-01 21:13:30 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-12-22 04:45:19 +00:00
|
|
|
|
|
|
|
// Check the result.
|
|
|
|
if (xmlStrcmp( result, BAD_CAST sitePassword ) == 0)
|
|
|
|
fprintf( stdout, "pass.\n" );
|
|
|
|
|
|
|
|
else {
|
|
|
|
++failedTests;
|
2015-02-18 22:32:33 +00:00
|
|
|
fprintf( stdout, "FAILED! (got %s != expected %s)\n", sitePassword, result );
|
2014-12-22 04:45:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Free test case.
|
2016-11-03 14:04:18 +00:00
|
|
|
mpw_free_string( sitePassword );
|
2014-12-22 04:45:19 +00:00
|
|
|
xmlFree( id );
|
|
|
|
xmlFree( fullName );
|
|
|
|
xmlFree( masterPassword );
|
|
|
|
xmlFree( keyID );
|
|
|
|
xmlFree( siteName );
|
2017-08-01 17:45:54 +00:00
|
|
|
xmlFree( passwordTypeString );
|
|
|
|
xmlFree( keyPurposeString );
|
|
|
|
xmlFree( keyContext );
|
2014-12-22 04:45:19 +00:00
|
|
|
xmlFree( result );
|
|
|
|
}
|
|
|
|
|
|
|
|
return failedTests;
|
|
|
|
}
|