summaryrefslogtreecommitdiffstats
path: root/security/nss/cmd/libpkix/testutil/testutil.c
blob: 0f438ba430ab3b25ad3e77d3147d3e335d66897d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
 * testutil.c
 *
 * Utility error handling functions
 *
 */

#include "testutil.h"

/*
 * static global variable to keep track of total number of errors for
 * a particular test suite (eg. all the OID tests)
 */
static int errCount = 0;

/*
 * FUNCTION: startTests
 * DESCRIPTION:
 *
 *  Prints standard message for starting the test suite with the name pointed
 *  to by "testName". This function should be called in the beginning of every
 *  test suite.
 *
 * PARAMETERS:
 *  "testName"
 *      Address of string representing name of test suite.
 * THREAD SAFETY:
 *  Not Thread Safe - assumes exclusive access to "errCount"
 *  (see Thread Safety Definitions in Programmer's Guide)
 * RETURNS:
 *  Returns nothing.
 */
void
startTests(char *testName)
{
    (void)printf("*START OF TESTS FOR %s:\n", testName);
    errCount = 0;
}

/*
 * FUNCTION: endTests
 * DESCRIPTION:
 *
 *  Prints standard message for ending the test suite with the name pointed
 *  to by "testName", followed by a success/failure message. This function
 *  should be called at the end of every test suite.
 *
 * PARAMETERS:
 *  "testName"
 *      Address of string representing name of test suite.
 * THREAD SAFETY:
 *  Not Thread Safe - assumes exclusive access to "errCount"
 *  (see Thread Safety Definitions in Programmer's Guide)
 * RETURNS:
 *  Returns nothing.
 */
void
endTests(char *testName)
{
    char plural = ' ';

    (void)printf("*END OF TESTS FOR %s: ", testName);
    if (errCount > 0) {
        if (errCount > 1)
            plural = 's';
        (void)printf("%d SUBTEST%c FAILED.\n\n", errCount, plural);
    } else {
        (void)printf("ALL TESTS COMPLETED SUCCESSFULLY.\n\n");
    }
}

/*
 * FUNCTION: subTest
 * DESCRIPTION:
 *
 *  Prints standard message for starting the subtest with the name pointed to
 *  by "subTestName". This function should be called at the beginning of each
 *  subtest.
 *
 * PARAMETERS:
 *  "subTestName"
 *      Address of string representing name of subTest.
 * THREAD SAFETY:
 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
 * RETURNS:
 *  Returns nothing.
 */
void
subTest(char *subTestName)
{
    (void)printf("TESTING: %s ...\n", subTestName);
}

/*
 * FUNCTION: testErrorUndo
 * DESCRIPTION:
 *
 *  Decrements the global variable "errCount" and prints a test failure
 *  expected message followed by the string pointed to by "msg". This function
 *  should be called when an expected error condition is encountered in the
 *  tests. Calling this function *correct* the previous errCount increment.
 *  It should only be called ONCE per subtest.
 *
 * PARAMETERS:
 *  "msg"
 *      Address of text of error message.
 * THREAD SAFETY:
 *  Not Thread Safe - assumes exclusive access to "errCount"
 *  (see Thread Safety Definitions in Programmer's Guide)
 * RETURNS:
 *  Returns nothing.
 */
void
testErrorUndo(char *msg)
{
    --errCount;
    (void)printf("TEST FAILURE *** EXPECTED *** :%s\n", msg);
}

/*
 * FUNCTION: testError
 * DESCRIPTION:
 *
 *  Increments the global variable "errCount" and prints a standard test
 *  failure message followed by the string pointed to by "msg". This function
 *  should be called when an unexpected error condition is encountered in the
 *  tests. It should only be called ONCE per subtest.
 *
 * PARAMETERS:
 *  "msg"
 *      Address of text of error message.
 * THREAD SAFETY:
 *  Not Thread Safe - assumes exclusive access to "errCount"
 *  (see Thread Safety Definitions in Programmer's Guide)
 * RETURNS:
 *  Returns nothing.
 */
void
testError(char *msg)
{
    ++errCount;
    (void)printf("TEST FAILURE: %s\n", msg);
}

/*
 * FUNCTION: PKIX_String2ASCII
 * DESCRIPTION:
 *
 *  Converts String object pointed to by "string" to its ASCII representation
 *  and returns the converted value. Returns NULL upon failure.
 *
 *  XXX Might want to use ESCASCII_DEBUG to show control characters, etc.
 *
 * PARAMETERS:
 *  "string"
 *      Address of String to be converted to ASCII. Must be non-NULL.
 *  "plContext"
 *      Platform-specific context pointer.
 * THREAD SAFETY:
 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
 * RETURNS:
 *  Returns the ASCII representation of "string" upon success;
 *  NULL upon failure.
 */
char *
PKIX_String2ASCII(PKIX_PL_String *string, void *plContext)
{
    PKIX_UInt32 length;
    char *asciiString = NULL;
    PKIX_Error *errorResult;

    errorResult = PKIX_PL_String_GetEncoded(string,
                                            PKIX_ESCASCII,
                                            (void **)&asciiString,
                                            &length,
                                            plContext);

    if (errorResult)
        goto cleanup;

cleanup:

    if (errorResult) {
        return (NULL);
    }

    return (asciiString);
}

/*
 * FUNCTION: PKIX_Error2ASCII
 * DESCRIPTION:
 *
 *  Converts Error pointed to by "error" to its ASCII representation and
 *  returns the converted value. Returns NULL upon failure.
 *
 * PARAMETERS:
 *  "error"
 *      Address of Error to be converted to ASCII. Must be non-NULL.
 *  "plContext"
 *      Platform-specific context pointer.
 * THREAD SAFETY:
 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
 * RETURNS:
 *  Returns the ASCII representation of "error" upon success;
 *  NULL upon failure.
 */
char *
PKIX_Error2ASCII(PKIX_Error *error, void *plContext)
{
    PKIX_UInt32 length;
    char *asciiString = NULL;
    PKIX_PL_String *pkixString = NULL;
    PKIX_Error *errorResult = NULL;

    errorResult = PKIX_PL_Object_ToString((PKIX_PL_Object *)error, &pkixString, plContext);
    if (errorResult)
        goto cleanup;

    errorResult = PKIX_PL_String_GetEncoded(pkixString,
                                            PKIX_ESCASCII,
                                            (void **)&asciiString,
                                            &length,
                                            plContext);

cleanup:

    if (pkixString) {
        if (PKIX_PL_Object_DecRef((PKIX_PL_Object *)pkixString, plContext)) {
            return (NULL);
        }
    }

    if (errorResult) {
        return (NULL);
    }

    return (asciiString);
}

/*
 * FUNCTION: PKIX_Object2ASCII
 * DESCRIPTION:
 *
 *  Converts Object pointed to by "object" to its ASCII representation and
 *  returns the converted value. Returns NULL upon failure.
 *
 * PARAMETERS:
 *  "object"
 *      Address of Object to be converted to ASCII. Must be non-NULL.
 * THREAD SAFETY:
 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
 * RETURNS:
 *  Returns the ASCII representation of "object" upon success;
 *  NULL upon failure.
 */
char *
PKIX_Object2ASCII(PKIX_PL_Object *object)
{
    PKIX_UInt32 length;
    char *asciiString = NULL;
    PKIX_PL_String *pkixString = NULL;
    PKIX_Error *errorResult = NULL;

    errorResult = PKIX_PL_Object_ToString(object, &pkixString, NULL);
    if (errorResult)
        goto cleanup;

    errorResult = PKIX_PL_String_GetEncoded(pkixString, PKIX_ESCASCII, (void **)&asciiString, &length, NULL);

cleanup:

    if (pkixString) {
        if (PKIX_PL_Object_DecRef((PKIX_PL_Object *)pkixString, NULL)) {
            return (NULL);
        }
    }

    if (errorResult) {
        return (NULL);
    }

    return (asciiString);
}

/*
 * FUNCTION: PKIX_Cert2ASCII
 * DESCRIPTION:
 *
 *  Converts Cert pointed to by "cert" to its partial ASCII representation and
 *  returns the converted value. Returns NULL upon failure.
 *
 * PARAMETERS:
 *  "cert"
 *      Address of Cert to be converted to ASCII. Must be non-NULL.
 * THREAD SAFETY:
 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
 * RETURNS:
 *  Returns the partial ASCII representation of "cert" upon success;
 *  NULL upon failure.
 */
char *
PKIX_Cert2ASCII(PKIX_PL_Cert *cert)
{
    PKIX_PL_X500Name *issuer = NULL;
    void *issuerAscii = NULL;
    PKIX_PL_X500Name *subject = NULL;
    void *subjectAscii = NULL;
    void *asciiString = NULL;
    PKIX_Error *errorResult = NULL;
    PKIX_UInt32 numChars;

    /* Issuer */
    errorResult = PKIX_PL_Cert_GetIssuer(cert, &issuer, NULL);
    if (errorResult)
        goto cleanup;

    issuerAscii = PKIX_Object2ASCII((PKIX_PL_Object *)issuer);

    /* Subject */
    errorResult = PKIX_PL_Cert_GetSubject(cert, &subject, NULL);
    if (errorResult)
        goto cleanup;

    if (subject) {
        subjectAscii = PKIX_Object2ASCII((PKIX_PL_Object *)subject);
    }

    errorResult = PKIX_PL_Malloc(200, &asciiString, NULL);
    if (errorResult)
        goto cleanup;

    numChars =
        PR_snprintf(asciiString,
                    200,
                    "Issuer=%s\nSubject=%s\n",
                    issuerAscii,
                    subjectAscii);

    if (!numChars)
        goto cleanup;

cleanup:

    if (issuer) {
        if (PKIX_PL_Object_DecRef((PKIX_PL_Object *)issuer, NULL)) {
            return (NULL);
        }
    }

    if (subject) {
        if (PKIX_PL_Object_DecRef((PKIX_PL_Object *)subject, NULL)) {
            return (NULL);
        }
    }

    if (PKIX_PL_Free((PKIX_PL_Object *)issuerAscii, NULL)) {
        return (NULL);
    }

    if (PKIX_PL_Free((PKIX_PL_Object *)subjectAscii, NULL)) {
        return (NULL);
    }

    if (errorResult) {
        return (NULL);
    }

    return (asciiString);
}

/*
 * FUNCTION: testHashcodeHelper
 * DESCRIPTION:
 *
 *  Computes the hashcode of the Object pointed to by "goodObject" and the
 *  Object pointed to by "otherObject" and compares them. If the result of the
 *  comparison is not the desired match as specified by "match", an error
 *  message is generated.
 *
 * PARAMETERS:
 *  "goodObject"
 *      Address of an object. Must be non-NULL.
 *  "otherObject"
 *      Address of another object. Must be non-NULL.
 *  "match"
 *      Boolean value representing the desired comparison result.
 *  "plContext"
 *      Platform-specific context pointer.
 * THREAD SAFETY:
 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
 * RETURNS:
 *  Returns nothing.
 */
void
testHashcodeHelper(
    PKIX_PL_Object *goodObject,
    PKIX_PL_Object *otherObject,
    PKIX_Boolean match,
    void *plContext)
{

    PKIX_UInt32 goodHash;
    PKIX_UInt32 otherHash;
    PKIX_Boolean cmpResult;
    PKIX_TEST_STD_VARS();

    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Hashcode((PKIX_PL_Object *)goodObject, &goodHash, plContext));

    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Hashcode((PKIX_PL_Object *)otherObject, &otherHash, plContext));

    cmpResult = (goodHash == otherHash);

    if ((match && !cmpResult) || (!match && cmpResult)) {
        testError("unexpected mismatch");
        (void)printf("Hash1:\t%d\n", goodHash);
        (void)printf("Hash2:\t%d\n", otherHash);
    }

cleanup:

    PKIX_TEST_RETURN();
}

/*
 * FUNCTION: testToStringHelper
 * DESCRIPTION:
 *
 *  Calls toString on the Object pointed to by "goodObject" and compares the
 *  result to the string pointed to by "expected". If the results are not
 *  equal, an error message is generated.
 *
 * PARAMETERS:
 *  "goodObject"
 *      Address of Object. Must be non-NULL.
 *  "expected"
 *      Address of the desired string.
 *  "plContext"
 *      Platform-specific context pointer.
 * THREAD SAFETY:
 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
 * RETURNS:
 *  Returns nothing.
 */
void
testToStringHelper(
    PKIX_PL_Object *goodObject,
    char *expected,
    void *plContext)
{
    PKIX_PL_String *stringRep = NULL;
    char *actual = NULL;
    PKIX_TEST_STD_VARS();

    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_ToString(goodObject, &stringRep, plContext));

    actual = PKIX_String2ASCII(stringRep, plContext);
    if (actual == NULL) {
        pkixTestErrorMsg = "PKIX_String2ASCII Failed";
        goto cleanup;
    }

    /*
         * If you are having trouble matching the string, uncomment the
         * PL_strstr function to figure out what's going on.
         */

    /*
            if (PL_strstr(actual, expected) == NULL){
                testError("PL_strstr failed");
            }
        */

    if (PL_strcmp(actual, expected) != 0) {
        testError("unexpected mismatch");
        (void)printf("Actual value:\t%s\n", actual);
        (void)printf("Expected value:\t%s\n", expected);
    }

cleanup:

    PKIX_PL_Free(actual, plContext);

    PKIX_TEST_DECREF_AC(stringRep);

    PKIX_TEST_RETURN();
}

/*
 * FUNCTION: testEqualsHelper
 * DESCRIPTION:
 *
 *  Checks if the Object pointed to by "goodObject" is Equal to the Object
 *  pointed to by "otherObject". If the result of the check is not the desired
 *  match as specified by "match", an error message is generated.
 *
 * PARAMETERS:
 *  "goodObject"
 *      Address of an Object. Must be non-NULL.
 *  "otherObject"
 *      Address of another Object. Must be non-NULL.
 *  "match"
 *      Boolean value representing the desired comparison result.
 *  "plContext"
 *      Platform-specific context pointer.
 * THREAD SAFETY:
 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
 * RETURNS:
 *  Returns nothing.
 */
void
testEqualsHelper(
    PKIX_PL_Object *goodObject,
    PKIX_PL_Object *otherObject,
    PKIX_Boolean match,
    void *plContext)
{

    PKIX_Boolean cmpResult;
    PKIX_TEST_STD_VARS();

    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Equals(goodObject, otherObject, &cmpResult, plContext));

    if ((match && !cmpResult) || (!match && cmpResult)) {
        testError("unexpected mismatch");
        (void)printf("Actual value:\t%d\n", cmpResult);
        (void)printf("Expected value:\t%d\n", match);
    }

cleanup:

    PKIX_TEST_RETURN();
}

/*
 * FUNCTION: testDuplicateHelper
 * DESCRIPTION:
 *  Checks if the Object pointed to by "object" is equal to its duplicate.
 *  If the result of the check is not equality, an error message is generated.
 * PARAMETERS:
 *  "object"
 *      Address of Object. Must be non-NULL.
 *  "plContext"
 *      Platform-specific context pointer.
 * THREAD SAFETY:
 *  Thread Safe (see Thread Safety Definitions in Programmer's Guide)
 * RETURNS:
 *  Returns nothing.
 */
void
testDuplicateHelper(PKIX_PL_Object *object, void *plContext)
{
    PKIX_PL_Object *newObject = NULL;
    PKIX_Boolean cmpResult;

    PKIX_TEST_STD_VARS();

    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Duplicate(object, &newObject, plContext));

    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Equals(object, newObject, &cmpResult, plContext));

    if (!cmpResult) {
        testError("unexpected mismatch");
        (void)printf("Actual value:\t%d\n", cmpResult);
        (void)printf("Expected value:\t%d\n", PKIX_TRUE);
    }

cleanup:

    PKIX_TEST_DECREF_AC(newObject);

    PKIX_TEST_RETURN();
}