2017-09-04 23:37:36 +00:00
|
|
|
//==============================================================================
|
|
|
|
// This file is part of Master Password.
|
|
|
|
// Copyright (c) 2011-2017, Maarten Billemont.
|
|
|
|
//
|
|
|
|
// Master Password is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Master Password is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You can find a copy of the GNU General Public License in the
|
|
|
|
// LICENSE file. Alternatively, see <http://www.gnu.org/licenses/>.
|
|
|
|
//==============================================================================
|
|
|
|
|
2018-06-03 20:19:14 +00:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
|
2014-12-22 04:45:19 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2018-06-03 20:19:14 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sysexits.h>
|
2014-12-22 04:45:19 +00:00
|
|
|
|
2018-03-24 19:22:37 +00:00
|
|
|
#ifndef mpw_log_do
|
|
|
|
#define mpw_log_do(level, format, ...) ({ \
|
|
|
|
fprintf( stderr, format "\n", ##__VA_ARGS__ ); \
|
|
|
|
if (level == ftl_level) \
|
|
|
|
abort(); \
|
|
|
|
})
|
|
|
|
#endif
|
2014-12-22 04:45:19 +00:00
|
|
|
|
|
|
|
#include "mpw-algorithm.h"
|
|
|
|
#include "mpw-util.h"
|
|
|
|
|
|
|
|
#include "mpw-tests-util.h"
|
|
|
|
|
2018-06-03 20:19:14 +00:00
|
|
|
/** Output the program's usage documentation. */
|
|
|
|
static void usage() {
|
|
|
|
|
|
|
|
inf( ""
|
|
|
|
" Master Password v%s - Tests\n"
|
|
|
|
"--------------------------------------------------------------------------------\n"
|
2018-06-03 20:26:08 +00:00
|
|
|
" https://masterpassword.app\n", stringify_def( MP_VERSION ) );
|
2018-06-03 20:19:14 +00:00
|
|
|
inf( ""
|
|
|
|
"\nUSAGE\n\n"
|
|
|
|
" mpw-tests [-v|-q]* [-h] [test-name ...]\n" );
|
|
|
|
inf( ""
|
|
|
|
" -v Increase output verbosity (can be repeated).\n"
|
|
|
|
" -q Decrease output verbosity (can be repeated).\n" );
|
|
|
|
inf( ""
|
|
|
|
" -h Show this help output instead of performing any operation.\n" );
|
|
|
|
inf( ""
|
|
|
|
" test-name Only run tests whose identifier starts with one of the these.\n" );
|
|
|
|
exit( EX_OK );
|
|
|
|
}
|
|
|
|
|
2014-12-22 04:45:19 +00:00
|
|
|
int main(int argc, char *const argv[]) {
|
|
|
|
|
2018-06-03 20:19:14 +00:00
|
|
|
for (int opt; (opt = getopt( argc, argv, "vqh" )) != EOF;
|
|
|
|
optarg? mpw_zero( optarg, strlen( optarg ) ): (void)0)
|
|
|
|
switch (opt) {
|
|
|
|
case 'v':
|
|
|
|
++mpw_verbosity;
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
--mpw_verbosity;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
usage();
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
ftl( "Unknown option: -%c", optopt );
|
|
|
|
exit( EX_USAGE );
|
|
|
|
default:
|
|
|
|
ftl( "Unexpected option: %c", opt );
|
|
|
|
exit( EX_USAGE );
|
|
|
|
}
|
|
|
|
|
2014-12-22 04:45:19 +00:00
|
|
|
int failedTests = 0;
|
|
|
|
|
|
|
|
xmlNodePtr tests = xmlDocGetRootElement( xmlParseFile( "mpw_tests.xml" ) );
|
2017-08-01 21:13:30 +00:00
|
|
|
if (!tests) {
|
2018-03-24 19:14:41 +00:00
|
|
|
ftl( "Couldn't find test case: mpw_tests.xml" );
|
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" );
|
2017-08-08 00:27:08 +00:00
|
|
|
MPCounterValue siteCounter = (MPCounterValue)mpw_xmlTestCaseInteger( testCase, "siteCounter" );
|
2017-08-10 16:30:42 +00:00
|
|
|
xmlChar *resultTypeString = mpw_xmlTestCaseString( testCase, "resultType" );
|
2017-08-01 17:45:54 +00:00
|
|
|
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" );
|
|
|
|
|
2020-01-23 20:53:55 +00:00
|
|
|
MPResultType resultType = mpw_type_named( (char *)resultTypeString );
|
|
|
|
MPKeyPurpose keyPurpose = mpw_purpose_named( (char *)keyPurposeString );
|
2014-12-22 04:45:19 +00:00
|
|
|
|
|
|
|
// Run the test case.
|
2017-09-22 22:23:08 +00:00
|
|
|
do {
|
2018-06-03 20:19:14 +00:00
|
|
|
if (optind < argc) {
|
|
|
|
bool selected = false;
|
|
|
|
for (int a = optind; !selected && a <= argc; ++a)
|
|
|
|
if (strstr((char *)id, argv[optind]) == (char *)id)
|
|
|
|
selected = true;
|
|
|
|
if (!selected)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-09-22 22:23:08 +00:00
|
|
|
fprintf( stdout, "test case %s... ", id );
|
|
|
|
if (!xmlStrlen( result )) {
|
2018-03-24 19:14:41 +00:00
|
|
|
fprintf( stdout, "abstract." );
|
2017-09-22 22:23:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1. calculate the master key.
|
2020-01-23 20:53:55 +00:00
|
|
|
MPMasterKey masterKey = mpw_master_key(
|
2017-09-22 22:23:08 +00:00
|
|
|
(char *)fullName, (char *)masterPassword, algorithm );
|
|
|
|
if (!masterKey) {
|
2018-03-24 19:14:41 +00:00
|
|
|
ftl( "Couldn't derive master key." );
|
2017-09-22 22:23:08 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the master key.
|
|
|
|
MPKeyID testKeyID = mpw_id_buf( masterKey, MPMasterKeySize );
|
|
|
|
if (xmlStrcmp( keyID, BAD_CAST testKeyID ) != 0) {
|
|
|
|
++failedTests;
|
|
|
|
fprintf( stdout, "FAILED! (keyID: got %s != expected %s)\n", testKeyID, keyID );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. calculate the site password.
|
2020-01-23 20:53:55 +00:00
|
|
|
const char *testResult = mpw_site_result(
|
2017-09-22 22:23:08 +00:00
|
|
|
masterKey, (char *)siteName, siteCounter, keyPurpose, (char *)keyContext, resultType, NULL, algorithm );
|
|
|
|
mpw_free( &masterKey, MPMasterKeySize );
|
|
|
|
if (!testResult) {
|
2018-03-24 19:14:41 +00:00
|
|
|
ftl( "Couldn't derive site password." );
|
2017-09-22 22:23:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the site result.
|
|
|
|
if (xmlStrcmp( result, BAD_CAST testResult ) != 0) {
|
|
|
|
++failedTests;
|
|
|
|
fprintf( stdout, "FAILED! (result: got %s != expected %s)\n", testResult, result );
|
|
|
|
mpw_free_string( &testResult );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
mpw_free_string( &testResult );
|
2014-12-22 04:45:19 +00:00
|
|
|
|
|
|
|
fprintf( stdout, "pass.\n" );
|
2017-09-22 22:23:08 +00:00
|
|
|
} while(false);
|
2014-12-22 04:45:19 +00:00
|
|
|
|
|
|
|
// Free test case.
|
|
|
|
xmlFree( id );
|
|
|
|
xmlFree( fullName );
|
|
|
|
xmlFree( masterPassword );
|
|
|
|
xmlFree( keyID );
|
|
|
|
xmlFree( siteName );
|
2017-08-10 16:30:42 +00:00
|
|
|
xmlFree( resultTypeString );
|
2017-08-01 17:45:54 +00:00
|
|
|
xmlFree( keyPurposeString );
|
|
|
|
xmlFree( keyContext );
|
2014-12-22 04:45:19 +00:00
|
|
|
xmlFree( result );
|
|
|
|
}
|
|
|
|
|
|
|
|
return failedTests;
|
|
|
|
}
|