tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

testutil.c (14955B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 /*
      5 * testutil.c
      6 *
      7 * Utility error handling functions
      8 *
      9 */
     10 
     11 #include "testutil.h"
     12 
     13 /*
     14 * static global variable to keep track of total number of errors for
     15 * a particular test suite (eg. all the OID tests)
     16 */
     17 static int errCount = 0;
     18 
     19 /*
     20 * FUNCTION: startTests
     21 * DESCRIPTION:
     22 *
     23 *  Prints standard message for starting the test suite with the name pointed
     24 *  to by "testName". This function should be called in the beginning of every
     25 *  test suite.
     26 *
     27 * PARAMETERS:
     28 *  "testName"
     29 *      Address of string representing name of test suite.
     30 * THREAD SAFETY:
     31 *  Not Thread Safe - assumes exclusive access to "errCount"
     32 *  (see Thread Safety Definitions in Programmer's Guide)
     33 * RETURNS:
     34 *  Returns nothing.
     35 */
     36 void
     37 startTests(char *testName)
     38 {
     39    (void)printf("*START OF TESTS FOR %s:\n", testName);
     40    errCount = 0;
     41 }
     42 
     43 /*
     44 * FUNCTION: endTests
     45 * DESCRIPTION:
     46 *
     47 *  Prints standard message for ending the test suite with the name pointed
     48 *  to by "testName", followed by a success/failure message. This function
     49 *  should be called at the end of every test suite.
     50 *
     51 * PARAMETERS:
     52 *  "testName"
     53 *      Address of string representing name of test suite.
     54 * THREAD SAFETY:
     55 *  Not Thread Safe - assumes exclusive access to "errCount"
     56 *  (see Thread Safety Definitions in Programmer's Guide)
     57 * RETURNS:
     58 *  Returns nothing.
     59 */
     60 void
     61 endTests(char *testName)
     62 {
     63    char plural = ' ';
     64 
     65    (void)printf("*END OF TESTS FOR %s: ", testName);
     66    if (errCount > 0) {
     67        if (errCount > 1)
     68            plural = 's';
     69        (void)printf("%d SUBTEST%c FAILED.\n\n", errCount, plural);
     70    } else {
     71        (void)printf("ALL TESTS COMPLETED SUCCESSFULLY.\n\n");
     72    }
     73 }
     74 
     75 /*
     76 * FUNCTION: subTest
     77 * DESCRIPTION:
     78 *
     79 *  Prints standard message for starting the subtest with the name pointed to
     80 *  by "subTestName". This function should be called at the beginning of each
     81 *  subtest.
     82 *
     83 * PARAMETERS:
     84 *  "subTestName"
     85 *      Address of string representing name of subTest.
     86 * THREAD SAFETY:
     87 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
     88 * RETURNS:
     89 *  Returns nothing.
     90 */
     91 void
     92 subTest(char *subTestName)
     93 {
     94    (void)printf("TESTING: %s ...\n", subTestName);
     95 }
     96 
     97 /*
     98 * FUNCTION: testErrorUndo
     99 * DESCRIPTION:
    100 *
    101 *  Decrements the global variable "errCount" and prints a test failure
    102 *  expected message followed by the string pointed to by "msg". This function
    103 *  should be called when an expected error condition is encountered in the
    104 *  tests. Calling this function *correct* the previous errCount increment.
    105 *  It should only be called ONCE per subtest.
    106 *
    107 * PARAMETERS:
    108 *  "msg"
    109 *      Address of text of error message.
    110 * THREAD SAFETY:
    111 *  Not Thread Safe - assumes exclusive access to "errCount"
    112 *  (see Thread Safety Definitions in Programmer's Guide)
    113 * RETURNS:
    114 *  Returns nothing.
    115 */
    116 void
    117 testErrorUndo(char *msg)
    118 {
    119    --errCount;
    120    (void)printf("TEST FAILURE *** EXPECTED *** :%s\n", msg);
    121 }
    122 
    123 /*
    124 * FUNCTION: testError
    125 * DESCRIPTION:
    126 *
    127 *  Increments the global variable "errCount" and prints a standard test
    128 *  failure message followed by the string pointed to by "msg". This function
    129 *  should be called when an unexpected error condition is encountered in the
    130 *  tests. It should only be called ONCE per subtest.
    131 *
    132 * PARAMETERS:
    133 *  "msg"
    134 *      Address of text of error message.
    135 * THREAD SAFETY:
    136 *  Not Thread Safe - assumes exclusive access to "errCount"
    137 *  (see Thread Safety Definitions in Programmer's Guide)
    138 * RETURNS:
    139 *  Returns nothing.
    140 */
    141 void
    142 testError(char *msg)
    143 {
    144    ++errCount;
    145    (void)printf("TEST FAILURE: %s\n", msg);
    146 }
    147 
    148 /*
    149 * FUNCTION: PKIX_String2ASCII
    150 * DESCRIPTION:
    151 *
    152 *  Converts String object pointed to by "string" to its ASCII representation
    153 *  and returns the converted value. Returns NULL upon failure.
    154 *
    155 *  XXX Might want to use ESCASCII_DEBUG to show control characters, etc.
    156 *
    157 * PARAMETERS:
    158 *  "string"
    159 *      Address of String to be converted to ASCII. Must be non-NULL.
    160 *  "plContext"
    161 *      Platform-specific context pointer.
    162 * THREAD SAFETY:
    163 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
    164 * RETURNS:
    165 *  Returns the ASCII representation of "string" upon success;
    166 *  NULL upon failure.
    167 */
    168 char *
    169 PKIX_String2ASCII(PKIX_PL_String *string, void *plContext)
    170 {
    171    PKIX_UInt32 length;
    172    char *asciiString = NULL;
    173    PKIX_Error *errorResult;
    174 
    175    errorResult = PKIX_PL_String_GetEncoded(string,
    176                                            PKIX_ESCASCII,
    177                                            (void **)&asciiString,
    178                                            &length,
    179                                            plContext);
    180 
    181    if (errorResult)
    182        goto cleanup;
    183 
    184 cleanup:
    185 
    186    if (errorResult) {
    187        return (NULL);
    188    }
    189 
    190    return (asciiString);
    191 }
    192 
    193 /*
    194 * FUNCTION: PKIX_Error2ASCII
    195 * DESCRIPTION:
    196 *
    197 *  Converts Error pointed to by "error" to its ASCII representation and
    198 *  returns the converted value. Returns NULL upon failure.
    199 *
    200 * PARAMETERS:
    201 *  "error"
    202 *      Address of Error to be converted to ASCII. Must be non-NULL.
    203 *  "plContext"
    204 *      Platform-specific context pointer.
    205 * THREAD SAFETY:
    206 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
    207 * RETURNS:
    208 *  Returns the ASCII representation of "error" upon success;
    209 *  NULL upon failure.
    210 */
    211 char *
    212 PKIX_Error2ASCII(PKIX_Error *error, void *plContext)
    213 {
    214    PKIX_UInt32 length;
    215    char *asciiString = NULL;
    216    PKIX_PL_String *pkixString = NULL;
    217    PKIX_Error *errorResult = NULL;
    218 
    219    errorResult = PKIX_PL_Object_ToString((PKIX_PL_Object *)error, &pkixString, plContext);
    220    if (errorResult)
    221        goto cleanup;
    222 
    223    errorResult = PKIX_PL_String_GetEncoded(pkixString,
    224                                            PKIX_ESCASCII,
    225                                            (void **)&asciiString,
    226                                            &length,
    227                                            plContext);
    228 
    229 cleanup:
    230 
    231    if (pkixString) {
    232        if (PKIX_PL_Object_DecRef((PKIX_PL_Object *)pkixString, plContext)) {
    233            return (NULL);
    234        }
    235    }
    236 
    237    if (errorResult) {
    238        return (NULL);
    239    }
    240 
    241    return (asciiString);
    242 }
    243 
    244 /*
    245 * FUNCTION: PKIX_Object2ASCII
    246 * DESCRIPTION:
    247 *
    248 *  Converts Object pointed to by "object" to its ASCII representation and
    249 *  returns the converted value. Returns NULL upon failure.
    250 *
    251 * PARAMETERS:
    252 *  "object"
    253 *      Address of Object to be converted to ASCII. Must be non-NULL.
    254 * THREAD SAFETY:
    255 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
    256 * RETURNS:
    257 *  Returns the ASCII representation of "object" upon success;
    258 *  NULL upon failure.
    259 */
    260 char *
    261 PKIX_Object2ASCII(PKIX_PL_Object *object)
    262 {
    263    PKIX_UInt32 length;
    264    char *asciiString = NULL;
    265    PKIX_PL_String *pkixString = NULL;
    266    PKIX_Error *errorResult = NULL;
    267 
    268    errorResult = PKIX_PL_Object_ToString(object, &pkixString, NULL);
    269    if (errorResult)
    270        goto cleanup;
    271 
    272    errorResult = PKIX_PL_String_GetEncoded(pkixString, PKIX_ESCASCII, (void **)&asciiString, &length, NULL);
    273 
    274 cleanup:
    275 
    276    if (pkixString) {
    277        if (PKIX_PL_Object_DecRef((PKIX_PL_Object *)pkixString, NULL)) {
    278            return (NULL);
    279        }
    280    }
    281 
    282    if (errorResult) {
    283        return (NULL);
    284    }
    285 
    286    return (asciiString);
    287 }
    288 
    289 /*
    290 * FUNCTION: PKIX_Cert2ASCII
    291 * DESCRIPTION:
    292 *
    293 *  Converts Cert pointed to by "cert" to its partial ASCII representation and
    294 *  returns the converted value. Returns NULL upon failure.
    295 *
    296 * PARAMETERS:
    297 *  "cert"
    298 *      Address of Cert to be converted to ASCII. Must be non-NULL.
    299 * THREAD SAFETY:
    300 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
    301 * RETURNS:
    302 *  Returns the partial ASCII representation of "cert" upon success;
    303 *  NULL upon failure.
    304 */
    305 char *
    306 PKIX_Cert2ASCII(PKIX_PL_Cert *cert)
    307 {
    308    PKIX_PL_X500Name *issuer = NULL;
    309    void *issuerAscii = NULL;
    310    PKIX_PL_X500Name *subject = NULL;
    311    void *subjectAscii = NULL;
    312    void *asciiString = NULL;
    313    PKIX_Error *errorResult = NULL;
    314    PKIX_UInt32 numChars;
    315 
    316    /* Issuer */
    317    errorResult = PKIX_PL_Cert_GetIssuer(cert, &issuer, NULL);
    318    if (errorResult)
    319        goto cleanup;
    320 
    321    issuerAscii = PKIX_Object2ASCII((PKIX_PL_Object *)issuer);
    322 
    323    /* Subject */
    324    errorResult = PKIX_PL_Cert_GetSubject(cert, &subject, NULL);
    325    if (errorResult)
    326        goto cleanup;
    327 
    328    if (subject) {
    329        subjectAscii = PKIX_Object2ASCII((PKIX_PL_Object *)subject);
    330    }
    331 
    332    errorResult = PKIX_PL_Malloc(200, &asciiString, NULL);
    333    if (errorResult)
    334        goto cleanup;
    335 
    336    numChars =
    337        PR_snprintf(asciiString,
    338                    200,
    339                    "Issuer=%s\nSubject=%s\n",
    340                    issuerAscii,
    341                    subjectAscii);
    342 
    343    if (!numChars)
    344        goto cleanup;
    345 
    346 cleanup:
    347 
    348    if (issuer) {
    349        if (PKIX_PL_Object_DecRef((PKIX_PL_Object *)issuer, NULL)) {
    350            return (NULL);
    351        }
    352    }
    353 
    354    if (subject) {
    355        if (PKIX_PL_Object_DecRef((PKIX_PL_Object *)subject, NULL)) {
    356            return (NULL);
    357        }
    358    }
    359 
    360    if (PKIX_PL_Free((PKIX_PL_Object *)issuerAscii, NULL)) {
    361        return (NULL);
    362    }
    363 
    364    if (PKIX_PL_Free((PKIX_PL_Object *)subjectAscii, NULL)) {
    365        return (NULL);
    366    }
    367 
    368    if (errorResult) {
    369        return (NULL);
    370    }
    371 
    372    return (asciiString);
    373 }
    374 
    375 /*
    376 * FUNCTION: testHashcodeHelper
    377 * DESCRIPTION:
    378 *
    379 *  Computes the hashcode of the Object pointed to by "goodObject" and the
    380 *  Object pointed to by "otherObject" and compares them. If the result of the
    381 *  comparison is not the desired match as specified by "match", an error
    382 *  message is generated.
    383 *
    384 * PARAMETERS:
    385 *  "goodObject"
    386 *      Address of an object. Must be non-NULL.
    387 *  "otherObject"
    388 *      Address of another object. Must be non-NULL.
    389 *  "match"
    390 *      Boolean value representing the desired comparison result.
    391 *  "plContext"
    392 *      Platform-specific context pointer.
    393 * THREAD SAFETY:
    394 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
    395 * RETURNS:
    396 *  Returns nothing.
    397 */
    398 void
    399 testHashcodeHelper(
    400    PKIX_PL_Object *goodObject,
    401    PKIX_PL_Object *otherObject,
    402    PKIX_Boolean match,
    403    void *plContext)
    404 {
    405 
    406    PKIX_UInt32 goodHash;
    407    PKIX_UInt32 otherHash;
    408    PKIX_Boolean cmpResult;
    409    PKIX_TEST_STD_VARS();
    410 
    411    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Hashcode((PKIX_PL_Object *)goodObject, &goodHash, plContext));
    412 
    413    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Hashcode((PKIX_PL_Object *)otherObject, &otherHash, plContext));
    414 
    415    cmpResult = (goodHash == otherHash);
    416 
    417    if ((match && !cmpResult) || (!match && cmpResult)) {
    418        testError("unexpected mismatch");
    419        (void)printf("Hash1:\t%d\n", goodHash);
    420        (void)printf("Hash2:\t%d\n", otherHash);
    421    }
    422 
    423 cleanup:
    424 
    425    PKIX_TEST_RETURN();
    426 }
    427 
    428 /*
    429 * FUNCTION: testToStringHelper
    430 * DESCRIPTION:
    431 *
    432 *  Calls toString on the Object pointed to by "goodObject" and compares the
    433 *  result to the string pointed to by "expected". If the results are not
    434 *  equal, an error message is generated.
    435 *
    436 * PARAMETERS:
    437 *  "goodObject"
    438 *      Address of Object. Must be non-NULL.
    439 *  "expected"
    440 *      Address of the desired string.
    441 *  "plContext"
    442 *      Platform-specific context pointer.
    443 * THREAD SAFETY:
    444 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
    445 * RETURNS:
    446 *  Returns nothing.
    447 */
    448 void
    449 testToStringHelper(
    450    PKIX_PL_Object *goodObject,
    451    char *expected,
    452    void *plContext)
    453 {
    454    PKIX_PL_String *stringRep = NULL;
    455    char *actual = NULL;
    456    PKIX_TEST_STD_VARS();
    457 
    458    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_ToString(goodObject, &stringRep, plContext));
    459 
    460    actual = PKIX_String2ASCII(stringRep, plContext);
    461    if (actual == NULL) {
    462        pkixTestErrorMsg = "PKIX_String2ASCII Failed";
    463        goto cleanup;
    464    }
    465 
    466    /*
    467     * If you are having trouble matching the string, uncomment the
    468     * PL_strstr function to figure out what's going on.
    469     */
    470 
    471    /*
    472            if (PL_strstr(actual, expected) == NULL){
    473                testError("PL_strstr failed");
    474            }
    475        */
    476 
    477    if (PL_strcmp(actual, expected) != 0) {
    478        testError("unexpected mismatch");
    479        (void)printf("Actual value:\t%s\n", actual);
    480        (void)printf("Expected value:\t%s\n", expected);
    481    }
    482 
    483 cleanup:
    484 
    485    PKIX_PL_Free(actual, plContext);
    486 
    487    PKIX_TEST_DECREF_AC(stringRep);
    488 
    489    PKIX_TEST_RETURN();
    490 }
    491 
    492 /*
    493 * FUNCTION: testEqualsHelper
    494 * DESCRIPTION:
    495 *
    496 *  Checks if the Object pointed to by "goodObject" is Equal to the Object
    497 *  pointed to by "otherObject". If the result of the check is not the desired
    498 *  match as specified by "match", an error message is generated.
    499 *
    500 * PARAMETERS:
    501 *  "goodObject"
    502 *      Address of an Object. Must be non-NULL.
    503 *  "otherObject"
    504 *      Address of another Object. Must be non-NULL.
    505 *  "match"
    506 *      Boolean value representing the desired comparison result.
    507 *  "plContext"
    508 *      Platform-specific context pointer.
    509 * THREAD SAFETY:
    510 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
    511 * RETURNS:
    512 *  Returns nothing.
    513 */
    514 void
    515 testEqualsHelper(
    516    PKIX_PL_Object *goodObject,
    517    PKIX_PL_Object *otherObject,
    518    PKIX_Boolean match,
    519    void *plContext)
    520 {
    521 
    522    PKIX_Boolean cmpResult;
    523    PKIX_TEST_STD_VARS();
    524 
    525    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Equals(goodObject, otherObject, &cmpResult, plContext));
    526 
    527    if ((match && !cmpResult) || (!match && cmpResult)) {
    528        testError("unexpected mismatch");
    529        (void)printf("Actual value:\t%d\n", cmpResult);
    530        (void)printf("Expected value:\t%d\n", match);
    531    }
    532 
    533 cleanup:
    534 
    535    PKIX_TEST_RETURN();
    536 }
    537 
    538 /*
    539 * FUNCTION: testDuplicateHelper
    540 * DESCRIPTION:
    541 *  Checks if the Object pointed to by "object" is equal to its duplicate.
    542 *  If the result of the check is not equality, an error message is generated.
    543 * PARAMETERS:
    544 *  "object"
    545 *      Address of Object. Must be non-NULL.
    546 *  "plContext"
    547 *      Platform-specific context pointer.
    548 * THREAD SAFETY:
    549 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
    550 * RETURNS:
    551 *  Returns nothing.
    552 */
    553 void
    554 testDuplicateHelper(PKIX_PL_Object *object, void *plContext)
    555 {
    556    PKIX_PL_Object *newObject = NULL;
    557    PKIX_Boolean cmpResult;
    558 
    559    PKIX_TEST_STD_VARS();
    560 
    561    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Duplicate(object, &newObject, plContext));
    562 
    563    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Equals(object, newObject, &cmpResult, plContext));
    564 
    565    if (!cmpResult) {
    566        testError("unexpected mismatch");
    567        (void)printf("Actual value:\t%d\n", cmpResult);
    568        (void)printf("Expected value:\t%d\n", PKIX_TRUE);
    569    }
    570 
    571 cleanup:
    572 
    573    PKIX_TEST_DECREF_AC(newObject);
    574 
    575    PKIX_TEST_RETURN();
    576 }