2
0

Some more error handling.

This commit is contained in:
Maarten Billemont 2017-08-01 17:13:30 -04:00
parent 99e286456e
commit b00ad53e42
4 changed files with 15 additions and 9 deletions

View File

@ -26,13 +26,12 @@
#define MP_N 32768 #define MP_N 32768
#define MP_r 8 #define MP_r 8
#define MP_p 2 #define MP_p 2
#define MP_hash PearlHashSHA256
static const char *mpw_templateForType_v0(MPPasswordType type, uint16_t seedByte) { static const char *mpw_templateForType_v0(MPPasswordType type, uint16_t seedByte) {
size_t count = 0; size_t count = 0;
const char **templates = mpw_templatesForType( type, &count ); const char **templates = mpw_templatesForType( type, &count );
char const *template = count? templates[seedByte % count]: NULL; char const *template = templates && count? templates[seedByte % count]: NULL;
free( templates ); free( templates );
return template; return template;
} }

View File

@ -150,7 +150,7 @@ const char *mpw_templateForType(MPPasswordType type, uint8_t seedByte) {
size_t count = 0; size_t count = 0;
const char **templates = mpw_templatesForType( type, &count ); const char **templates = mpw_templatesForType( type, &count );
char const *template = count? templates[seedByte % count]: NULL; char const *template = templates && count? templates[seedByte % count]: NULL;
free( templates ); free( templates );
return template; return template;
} }

View File

@ -48,7 +48,7 @@ xmlNodePtr mpw_xmlTestCaseNode(xmlNodePtr testCaseNode, const char *nodeName) {
for (xmlNodePtr otherTestCaseNode = testCaseNode->parent->children; otherTestCaseNode; otherTestCaseNode = otherTestCaseNode->next) { for (xmlNodePtr otherTestCaseNode = testCaseNode->parent->children; otherTestCaseNode; otherTestCaseNode = otherTestCaseNode->next) {
xmlChar *id = mpw_xmlTestCaseString( otherTestCaseNode, "id" ); xmlChar *id = mpw_xmlTestCaseString( otherTestCaseNode, "id" );
int foundParent = xmlStrcmp( id, parentId ) == 0; int foundParent = id && xmlStrcmp( id, parentId ) == 0;
xmlFree( id ); xmlFree( id );
if (foundParent) { if (foundParent) {
@ -58,18 +58,19 @@ xmlNodePtr mpw_xmlTestCaseNode(xmlNodePtr testCaseNode, const char *nodeName) {
} }
ftl( "Missing parent: %s, for case: %s\n", parentId, mpw_xmlTestCaseString( testCaseNode, "id" ) ); ftl( "Missing parent: %s, for case: %s\n", parentId, mpw_xmlTestCaseString( testCaseNode, "id" ) );
return NULL;
} }
xmlChar *mpw_xmlTestCaseString(xmlNodePtr context, const char *nodeName) { xmlChar *mpw_xmlTestCaseString(xmlNodePtr context, const char *nodeName) {
xmlNodePtr child = mpw_xmlTestCaseNode( context, nodeName ); xmlNodePtr child = mpw_xmlTestCaseNode( context, nodeName );
return xmlNodeGetContent( child ); return child? xmlNodeGetContent( child ): NULL;
} }
uint32_t mpw_xmlTestCaseInteger(xmlNodePtr context, const char *nodeName) { uint32_t mpw_xmlTestCaseInteger(xmlNodePtr context, const char *nodeName) {
xmlChar *string = mpw_xmlTestCaseString( context, nodeName ); xmlChar *string = mpw_xmlTestCaseString( context, nodeName );
uint32_t integer = (uint32_t)atol( (char *)string ); uint32_t integer = string? (uint32_t)atol( (char *)string ): 0;
xmlFree( string ); xmlFree( string );
return integer; return integer;

View File

@ -13,8 +13,10 @@ int main(int argc, char *const argv[]) {
int failedTests = 0; int failedTests = 0;
xmlNodePtr tests = xmlDocGetRootElement( xmlParseFile( "mpw_tests.xml" ) ); xmlNodePtr tests = xmlDocGetRootElement( xmlParseFile( "mpw_tests.xml" ) );
if (!tests) if (!tests) {
ftl( "Couldn't find test case: mpw_tests.xml\n" ); ftl( "Couldn't find test case: mpw_tests.xml\n" );
abort();
}
for (xmlNodePtr testCase = tests->children; testCase; testCase = testCase->next) { for (xmlNodePtr testCase = tests->children; testCase; testCase = testCase->next) {
if (testCase->type != XML_ELEMENT_NODE || xmlStrcmp( testCase->name, BAD_CAST "case" ) != 0) if (testCase->type != XML_ELEMENT_NODE || xmlStrcmp( testCase->name, BAD_CAST "case" ) != 0)
@ -46,8 +48,10 @@ int main(int argc, char *const argv[]) {
// 1. calculate the master key. // 1. calculate the master key.
MPMasterKey masterKey = mpw_masterKey( MPMasterKey masterKey = mpw_masterKey(
(char *)fullName, (char *)masterPassword, algorithm ); (char *)fullName, (char *)masterPassword, algorithm );
if (!masterKey) if (!masterKey) {
ftl( "Couldn't derive master key." ); ftl( "Couldn't derive master key." );
continue;
}
// 2. calculate the site password. // 2. calculate the site password.
MPSiteKey siteKey = mpw_siteKey( MPSiteKey siteKey = mpw_siteKey(
@ -56,8 +60,10 @@ int main(int argc, char *const argv[]) {
siteKey, siteType, algorithm ); siteKey, siteType, algorithm );
mpw_free( masterKey, MPMasterKeySize ); mpw_free( masterKey, MPMasterKeySize );
mpw_free( siteKey, MPSiteKeySize ); mpw_free( siteKey, MPSiteKeySize );
if (!sitePassword) if (!sitePassword) {
ftl( "Couldn't derive site password." ); ftl( "Couldn't derive site password." );
continue;
}
// Check the result. // Check the result.
if (xmlStrcmp( result, BAD_CAST sitePassword ) == 0) if (xmlStrcmp( result, BAD_CAST sitePassword ) == 0)