1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/nss/cmd/libpkix/testutil/testutil.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,586 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 +/* 1.8 + * testutil.c 1.9 + * 1.10 + * Utility error handling functions 1.11 + * 1.12 + */ 1.13 + 1.14 +#include "testutil.h" 1.15 + 1.16 +/* 1.17 + * static global variable to keep track of total number of errors for 1.18 + * a particular test suite (eg. all the OID tests) 1.19 + */ 1.20 +static int errCount = 0; 1.21 + 1.22 +/* 1.23 + * FUNCTION: startTests 1.24 + * DESCRIPTION: 1.25 + * 1.26 + * Prints standard message for starting the test suite with the name pointed 1.27 + * to by "testName". This function should be called in the beginning of every 1.28 + * test suite. 1.29 + * 1.30 + * PARAMETERS: 1.31 + * "testName" 1.32 + * Address of string representing name of test suite. 1.33 + * THREAD SAFETY: 1.34 + * Not Thread Safe - assumes exclusive access to "errCount" 1.35 + * (see Thread Safety Definitions in Programmer's Guide) 1.36 + * RETURNS: 1.37 + * Returns nothing. 1.38 + */ 1.39 +void 1.40 +startTests(char *testName) 1.41 +{ 1.42 + (void) printf("*START OF TESTS FOR %s:\n", testName); 1.43 + errCount = 0; 1.44 +} 1.45 + 1.46 +/* 1.47 + * FUNCTION: endTests 1.48 + * DESCRIPTION: 1.49 + * 1.50 + * Prints standard message for ending the test suite with the name pointed 1.51 + * to by "testName", followed by a success/failure message. This function 1.52 + * should be called at the end of every test suite. 1.53 + * 1.54 + * PARAMETERS: 1.55 + * "testName" 1.56 + * Address of string representing name of test suite. 1.57 + * THREAD SAFETY: 1.58 + * Not Thread Safe - assumes exclusive access to "errCount" 1.59 + * (see Thread Safety Definitions in Programmer's Guide) 1.60 + * RETURNS: 1.61 + * Returns nothing. 1.62 + */ 1.63 +void 1.64 +endTests(char *testName) 1.65 +{ 1.66 + char plural = ' '; 1.67 + 1.68 + (void) printf("*END OF TESTS FOR %s: ", testName); 1.69 + if (errCount > 0) { 1.70 + if (errCount > 1) plural = 's'; 1.71 + (void) printf("%d SUBTEST%c FAILED.\n\n", errCount, plural); 1.72 + } else { 1.73 + (void) printf("ALL TESTS COMPLETED SUCCESSFULLY.\n\n"); 1.74 + } 1.75 +} 1.76 + 1.77 +/* 1.78 + * FUNCTION: subTest 1.79 + * DESCRIPTION: 1.80 + * 1.81 + * Prints standard message for starting the subtest with the name pointed to 1.82 + * by "subTestName". This function should be called at the beginning of each 1.83 + * subtest. 1.84 + * 1.85 + * PARAMETERS: 1.86 + * "subTestName" 1.87 + * Address of string representing name of subTest. 1.88 + * THREAD SAFETY: 1.89 + * Thread Safe (see Thread Safety Definitions in Programmer's Guide) 1.90 + * RETURNS: 1.91 + * Returns nothing. 1.92 + */ 1.93 +void 1.94 +subTest(char *subTestName) 1.95 +{ 1.96 + (void) printf("TESTING: %s ...\n", subTestName); 1.97 +} 1.98 + 1.99 +/* 1.100 + * FUNCTION: testErrorUndo 1.101 + * DESCRIPTION: 1.102 + * 1.103 + * Decrements the global variable "errCount" and prints a test failure 1.104 + * expected message followed by the string pointed to by "msg". This function 1.105 + * should be called when an expected error condition is encountered in the 1.106 + * tests. Calling this function *correct* the previous errCount increment. 1.107 + * It should only be called ONCE per subtest. 1.108 + * 1.109 + * PARAMETERS: 1.110 + * "msg" 1.111 + * Address of text of error message. 1.112 + * THREAD SAFETY: 1.113 + * Not Thread Safe - assumes exclusive access to "errCount" 1.114 + * (see Thread Safety Definitions in Programmer's Guide) 1.115 + * RETURNS: 1.116 + * Returns nothing. 1.117 + */ 1.118 +void 1.119 +testErrorUndo(char *msg) 1.120 +{ 1.121 + --errCount; 1.122 + (void) printf("TEST FAILURE *** EXPECTED *** :%s\n", msg); 1.123 +} 1.124 + 1.125 +/* 1.126 + * FUNCTION: testError 1.127 + * DESCRIPTION: 1.128 + * 1.129 + * Increments the global variable "errCount" and prints a standard test 1.130 + * failure message followed by the string pointed to by "msg". This function 1.131 + * should be called when an unexpected error condition is encountered in the 1.132 + * tests. It should only be called ONCE per subtest. 1.133 + * 1.134 + * PARAMETERS: 1.135 + * "msg" 1.136 + * Address of text of error message. 1.137 + * THREAD SAFETY: 1.138 + * Not Thread Safe - assumes exclusive access to "errCount" 1.139 + * (see Thread Safety Definitions in Programmer's Guide) 1.140 + * RETURNS: 1.141 + * Returns nothing. 1.142 + */ 1.143 +void 1.144 +testError(char *msg) 1.145 +{ 1.146 + ++errCount; 1.147 + (void) printf("TEST FAILURE: %s\n", msg); 1.148 +} 1.149 + 1.150 +/* 1.151 + * FUNCTION: PKIX_String2ASCII 1.152 + * DESCRIPTION: 1.153 + * 1.154 + * Converts String object pointed to by "string" to its ASCII representation 1.155 + * and returns the converted value. Returns NULL upon failure. 1.156 + * 1.157 + * XXX Might want to use ESCASCII_DEBUG to show control characters, etc. 1.158 + * 1.159 + * PARAMETERS: 1.160 + * "string" 1.161 + * Address of String to be converted to ASCII. Must be non-NULL. 1.162 + * "plContext" 1.163 + * Platform-specific context pointer. 1.164 + * THREAD SAFETY: 1.165 + * Thread Safe (see Thread Safety Definitions in Programmer's Guide) 1.166 + * RETURNS: 1.167 + * Returns the ASCII representation of "string" upon success; 1.168 + * NULL upon failure. 1.169 + */ 1.170 +char * 1.171 +PKIX_String2ASCII(PKIX_PL_String *string, void *plContext) 1.172 +{ 1.173 + PKIX_UInt32 length; 1.174 + char *asciiString = NULL; 1.175 + PKIX_Error *errorResult; 1.176 + 1.177 + errorResult = PKIX_PL_String_GetEncoded 1.178 + (string, 1.179 + PKIX_ESCASCII, 1.180 + (void **)&asciiString, 1.181 + &length, 1.182 + plContext); 1.183 + 1.184 + if (errorResult) goto cleanup; 1.185 + 1.186 +cleanup: 1.187 + 1.188 + if (errorResult){ 1.189 + return (NULL); 1.190 + } 1.191 + 1.192 + return (asciiString); 1.193 + 1.194 +} 1.195 + 1.196 +/* 1.197 + * FUNCTION: PKIX_Error2ASCII 1.198 + * DESCRIPTION: 1.199 + * 1.200 + * Converts Error pointed to by "error" to its ASCII representation and 1.201 + * returns the converted value. Returns NULL upon failure. 1.202 + * 1.203 + * PARAMETERS: 1.204 + * "error" 1.205 + * Address of Error to be converted to ASCII. Must be non-NULL. 1.206 + * "plContext" 1.207 + * Platform-specific context pointer. 1.208 + * THREAD SAFETY: 1.209 + * Thread Safe (see Thread Safety Definitions in Programmer's Guide) 1.210 + * RETURNS: 1.211 + * Returns the ASCII representation of "error" upon success; 1.212 + * NULL upon failure. 1.213 + */ 1.214 +char * 1.215 +PKIX_Error2ASCII(PKIX_Error *error, void *plContext) 1.216 +{ 1.217 + PKIX_UInt32 length; 1.218 + char *asciiString = NULL; 1.219 + PKIX_PL_String *pkixString = NULL; 1.220 + PKIX_Error *errorResult = NULL; 1.221 + 1.222 + errorResult = PKIX_PL_Object_ToString 1.223 + ((PKIX_PL_Object*)error, &pkixString, plContext); 1.224 + if (errorResult) goto cleanup; 1.225 + 1.226 + errorResult = PKIX_PL_String_GetEncoded 1.227 + (pkixString, 1.228 + PKIX_ESCASCII, 1.229 + (void **)&asciiString, 1.230 + &length, 1.231 + plContext); 1.232 + 1.233 +cleanup: 1.234 + 1.235 + if (pkixString){ 1.236 + if (PKIX_PL_Object_DecRef 1.237 + ((PKIX_PL_Object*)pkixString, plContext)){ 1.238 + return (NULL); 1.239 + } 1.240 + } 1.241 + 1.242 + if (errorResult){ 1.243 + return (NULL); 1.244 + } 1.245 + 1.246 + return (asciiString); 1.247 +} 1.248 + 1.249 +/* 1.250 + * FUNCTION: PKIX_Object2ASCII 1.251 + * DESCRIPTION: 1.252 + * 1.253 + * Converts Object pointed to by "object" to its ASCII representation and 1.254 + * returns the converted value. Returns NULL upon failure. 1.255 + * 1.256 + * PARAMETERS: 1.257 + * "object" 1.258 + * Address of Object to be converted to ASCII. Must be non-NULL. 1.259 + * THREAD SAFETY: 1.260 + * Thread Safe (see Thread Safety Definitions in Programmer's Guide) 1.261 + * RETURNS: 1.262 + * Returns the ASCII representation of "object" upon success; 1.263 + * NULL upon failure. 1.264 + */ 1.265 +char * 1.266 +PKIX_Object2ASCII(PKIX_PL_Object *object) 1.267 +{ 1.268 + PKIX_UInt32 length; 1.269 + char *asciiString = NULL; 1.270 + PKIX_PL_String *pkixString = NULL; 1.271 + PKIX_Error *errorResult = NULL; 1.272 + 1.273 + errorResult = PKIX_PL_Object_ToString 1.274 + (object, &pkixString, NULL); 1.275 + if (errorResult) goto cleanup; 1.276 + 1.277 + errorResult = PKIX_PL_String_GetEncoded 1.278 + (pkixString, PKIX_ESCASCII, (void **)&asciiString, &length, NULL); 1.279 + 1.280 +cleanup: 1.281 + 1.282 + if (pkixString){ 1.283 + if (PKIX_PL_Object_DecRef((PKIX_PL_Object*)pkixString, NULL)){ 1.284 + return (NULL); 1.285 + } 1.286 + } 1.287 + 1.288 + if (errorResult){ 1.289 + return (NULL); 1.290 + } 1.291 + 1.292 + return (asciiString); 1.293 +} 1.294 + 1.295 +/* 1.296 + * FUNCTION: PKIX_Cert2ASCII 1.297 + * DESCRIPTION: 1.298 + * 1.299 + * Converts Cert pointed to by "cert" to its partial ASCII representation and 1.300 + * returns the converted value. Returns NULL upon failure. 1.301 + * 1.302 + * PARAMETERS: 1.303 + * "cert" 1.304 + * Address of Cert to be converted to ASCII. Must be non-NULL. 1.305 + * THREAD SAFETY: 1.306 + * Thread Safe (see Thread Safety Definitions in Programmer's Guide) 1.307 + * RETURNS: 1.308 + * Returns the partial ASCII representation of "cert" upon success; 1.309 + * NULL upon failure. 1.310 + */ 1.311 +char * 1.312 +PKIX_Cert2ASCII(PKIX_PL_Cert *cert) 1.313 +{ 1.314 + PKIX_PL_X500Name *issuer = NULL; 1.315 + void *issuerAscii = NULL; 1.316 + PKIX_PL_X500Name *subject = NULL; 1.317 + void *subjectAscii = NULL; 1.318 + void *asciiString = NULL; 1.319 + PKIX_Error *errorResult = NULL; 1.320 + PKIX_UInt32 numChars; 1.321 + 1.322 + /* Issuer */ 1.323 + errorResult = PKIX_PL_Cert_GetIssuer(cert, &issuer, NULL); 1.324 + if (errorResult) goto cleanup; 1.325 + 1.326 + issuerAscii = PKIX_Object2ASCII((PKIX_PL_Object*)issuer); 1.327 + 1.328 + /* Subject */ 1.329 + errorResult = PKIX_PL_Cert_GetSubject(cert, &subject, NULL); 1.330 + if (errorResult) goto cleanup; 1.331 + 1.332 + if (subject){ 1.333 + subjectAscii = PKIX_Object2ASCII((PKIX_PL_Object*)subject); 1.334 + } 1.335 + 1.336 + errorResult = PKIX_PL_Malloc(200, &asciiString, NULL); 1.337 + if (errorResult) goto cleanup; 1.338 + 1.339 + numChars = 1.340 + PR_snprintf 1.341 + (asciiString, 1.342 + 200, 1.343 + "Issuer=%s\nSubject=%s\n", 1.344 + issuerAscii, 1.345 + subjectAscii); 1.346 + 1.347 + if (!numChars) goto cleanup; 1.348 + 1.349 +cleanup: 1.350 + 1.351 + if (issuer){ 1.352 + if (PKIX_PL_Object_DecRef((PKIX_PL_Object*)issuer, NULL)){ 1.353 + return (NULL); 1.354 + } 1.355 + } 1.356 + 1.357 + if (subject){ 1.358 + if (PKIX_PL_Object_DecRef((PKIX_PL_Object*)subject, NULL)){ 1.359 + return (NULL); 1.360 + } 1.361 + } 1.362 + 1.363 + if (PKIX_PL_Free((PKIX_PL_Object*)issuerAscii, NULL)){ 1.364 + return (NULL); 1.365 + } 1.366 + 1.367 + if (PKIX_PL_Free((PKIX_PL_Object*)subjectAscii, NULL)){ 1.368 + return (NULL); 1.369 + } 1.370 + 1.371 + if (errorResult){ 1.372 + return (NULL); 1.373 + } 1.374 + 1.375 + return (asciiString); 1.376 +} 1.377 + 1.378 +/* 1.379 + * FUNCTION: testHashcodeHelper 1.380 + * DESCRIPTION: 1.381 + * 1.382 + * Computes the hashcode of the Object pointed to by "goodObject" and the 1.383 + * Object pointed to by "otherObject" and compares them. If the result of the 1.384 + * comparison is not the desired match as specified by "match", an error 1.385 + * message is generated. 1.386 + * 1.387 + * PARAMETERS: 1.388 + * "goodObject" 1.389 + * Address of an object. Must be non-NULL. 1.390 + * "otherObject" 1.391 + * Address of another object. Must be non-NULL. 1.392 + * "match" 1.393 + * Boolean value representing the desired comparison result. 1.394 + * "plContext" 1.395 + * Platform-specific context pointer. 1.396 + * THREAD SAFETY: 1.397 + * Thread Safe (see Thread Safety Definitions in Programmer's Guide) 1.398 + * RETURNS: 1.399 + * Returns nothing. 1.400 + */ 1.401 +void 1.402 +testHashcodeHelper( 1.403 + PKIX_PL_Object *goodObject, 1.404 + PKIX_PL_Object *otherObject, 1.405 + PKIX_Boolean match, 1.406 + void *plContext) 1.407 +{ 1.408 + 1.409 + PKIX_UInt32 goodHash; 1.410 + PKIX_UInt32 otherHash; 1.411 + PKIX_Boolean cmpResult; 1.412 + PKIX_TEST_STD_VARS(); 1.413 + 1.414 + PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Hashcode 1.415 + ((PKIX_PL_Object *)goodObject, &goodHash, plContext)); 1.416 + 1.417 + PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Hashcode 1.418 + ((PKIX_PL_Object *)otherObject, &otherHash, plContext)); 1.419 + 1.420 + cmpResult = (goodHash == otherHash); 1.421 + 1.422 + if ((match && !cmpResult) || (!match && cmpResult)){ 1.423 + testError("unexpected mismatch"); 1.424 + (void) printf("Hash1:\t%d\n", goodHash); 1.425 + (void) printf("Hash2:\t%d\n", otherHash); 1.426 + } 1.427 + 1.428 +cleanup: 1.429 + 1.430 + PKIX_TEST_RETURN(); 1.431 + 1.432 +} 1.433 + 1.434 +/* 1.435 + * FUNCTION: testToStringHelper 1.436 + * DESCRIPTION: 1.437 + * 1.438 + * Calls toString on the Object pointed to by "goodObject" and compares the 1.439 + * result to the string pointed to by "expected". If the results are not 1.440 + * equal, an error message is generated. 1.441 + * 1.442 + * PARAMETERS: 1.443 + * "goodObject" 1.444 + * Address of Object. Must be non-NULL. 1.445 + * "expected" 1.446 + * Address of the desired string. 1.447 + * "plContext" 1.448 + * Platform-specific context pointer. 1.449 + * THREAD SAFETY: 1.450 + * Thread Safe (see Thread Safety Definitions in Programmer's Guide) 1.451 + * RETURNS: 1.452 + * Returns nothing. 1.453 + */ 1.454 +void 1.455 +testToStringHelper( 1.456 + PKIX_PL_Object *goodObject, 1.457 + char *expected, 1.458 + void *plContext) 1.459 +{ 1.460 + PKIX_PL_String *stringRep = NULL; 1.461 + char *actual = NULL; 1.462 + PKIX_TEST_STD_VARS(); 1.463 + 1.464 + PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_ToString 1.465 + (goodObject, &stringRep, plContext)); 1.466 + 1.467 + actual = PKIX_String2ASCII(stringRep, plContext); 1.468 + if (actual == NULL){ 1.469 + pkixTestErrorMsg = "PKIX_String2ASCII Failed"; 1.470 + goto cleanup; 1.471 + } 1.472 + 1.473 + /* 1.474 + * If you are having trouble matching the string, uncomment the 1.475 + * PL_strstr function to figure out what's going on. 1.476 + */ 1.477 + 1.478 + /* 1.479 + if (PL_strstr(actual, expected) == NULL){ 1.480 + testError("PL_strstr failed"); 1.481 + } 1.482 + */ 1.483 + 1.484 + 1.485 + if (PL_strcmp(actual, expected) != 0){ 1.486 + testError("unexpected mismatch"); 1.487 + (void) printf("Actual value:\t%s\n", actual); 1.488 + (void) printf("Expected value:\t%s\n", expected); 1.489 + } 1.490 + 1.491 +cleanup: 1.492 + 1.493 + PKIX_PL_Free(actual, plContext); 1.494 + 1.495 + PKIX_TEST_DECREF_AC(stringRep); 1.496 + 1.497 + PKIX_TEST_RETURN(); 1.498 +} 1.499 + 1.500 +/* 1.501 + * FUNCTION: testEqualsHelper 1.502 + * DESCRIPTION: 1.503 + * 1.504 + * Checks if the Object pointed to by "goodObject" is Equal to the Object 1.505 + * pointed to by "otherObject". If the result of the check is not the desired 1.506 + * match as specified by "match", an error message is generated. 1.507 + * 1.508 + * PARAMETERS: 1.509 + * "goodObject" 1.510 + * Address of an Object. Must be non-NULL. 1.511 + * "otherObject" 1.512 + * Address of another Object. Must be non-NULL. 1.513 + * "match" 1.514 + * Boolean value representing the desired comparison result. 1.515 + * "plContext" 1.516 + * Platform-specific context pointer. 1.517 + * THREAD SAFETY: 1.518 + * Thread Safe (see Thread Safety Definitions in Programmer's Guide) 1.519 + * RETURNS: 1.520 + * Returns nothing. 1.521 + */ 1.522 +void 1.523 +testEqualsHelper( 1.524 + PKIX_PL_Object *goodObject, 1.525 + PKIX_PL_Object *otherObject, 1.526 + PKIX_Boolean match, 1.527 + void *plContext) 1.528 +{ 1.529 + 1.530 + PKIX_Boolean cmpResult; 1.531 + PKIX_TEST_STD_VARS(); 1.532 + 1.533 + PKIX_TEST_EXPECT_NO_ERROR 1.534 + (PKIX_PL_Object_Equals 1.535 + (goodObject, otherObject, &cmpResult, plContext)); 1.536 + 1.537 + if ((match && !cmpResult) || (!match && cmpResult)){ 1.538 + testError("unexpected mismatch"); 1.539 + (void) printf("Actual value:\t%d\n", cmpResult); 1.540 + (void) printf("Expected value:\t%d\n", match); 1.541 + } 1.542 + 1.543 +cleanup: 1.544 + 1.545 + PKIX_TEST_RETURN(); 1.546 +} 1.547 + 1.548 + 1.549 +/* 1.550 + * FUNCTION: testDuplicateHelper 1.551 + * DESCRIPTION: 1.552 + * Checks if the Object pointed to by "object" is equal to its duplicate. 1.553 + * If the result of the check is not equality, an error message is generated. 1.554 + * PARAMETERS: 1.555 + * "object" 1.556 + * Address of Object. Must be non-NULL. 1.557 + * "plContext" 1.558 + * Platform-specific context pointer. 1.559 + * THREAD SAFETY: 1.560 + * Thread Safe (see Thread Safety Definitions in Programmer's Guide) 1.561 + * RETURNS: 1.562 + * Returns nothing. 1.563 + */ 1.564 +void 1.565 +testDuplicateHelper(PKIX_PL_Object *object, void *plContext) 1.566 +{ 1.567 + PKIX_PL_Object *newObject = NULL; 1.568 + PKIX_Boolean cmpResult; 1.569 + 1.570 + PKIX_TEST_STD_VARS(); 1.571 + 1.572 + PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Duplicate 1.573 + (object, &newObject, plContext)); 1.574 + 1.575 + PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Equals 1.576 + (object, newObject, &cmpResult, plContext)); 1.577 + 1.578 + if (!cmpResult){ 1.579 + testError("unexpected mismatch"); 1.580 + (void) printf("Actual value:\t%d\n", cmpResult); 1.581 + (void) printf("Expected value:\t%d\n", PKIX_TRUE); 1.582 + } 1.583 + 1.584 +cleanup: 1.585 + 1.586 + PKIX_TEST_DECREF_AC(newObject); 1.587 + 1.588 + PKIX_TEST_RETURN(); 1.589 +}