1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is the Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
40 * Utility error handling functions
47 * static global variable to keep track of total number of errors for
48 * a particular test suite (eg. all the OID tests)
50 static int errCount
= 0;
53 * FUNCTION: startTests
56 * Prints standard message for starting the test suite with the name pointed
57 * to by "testName". This function should be called in the beginning of every
62 * Address of string representing name of test suite.
64 * Not Thread Safe - assumes exclusive access to "errCount"
65 * (see Thread Safety Definitions in Programmer's Guide)
70 startTests(char *testName
)
72 (void) printf("*START OF TESTS FOR %s:\n", testName
);
80 * Prints standard message for ending the test suite with the name pointed
81 * to by "testName", followed by a success/failure message. This function
82 * should be called at the end of every test suite.
86 * Address of string representing name of test suite.
88 * Not Thread Safe - assumes exclusive access to "errCount"
89 * (see Thread Safety Definitions in Programmer's Guide)
94 endTests(char *testName
)
98 (void) printf("*END OF TESTS FOR %s: ", testName
);
100 if (errCount
> 1) plural
= 's';
101 (void) printf("%d SUBTEST%c FAILED.\n\n", errCount
, plural
);
103 (void) printf("ALL TESTS COMPLETED SUCCESSFULLY.\n\n");
111 * Prints standard message for starting the subtest with the name pointed to
112 * by "subTestName". This function should be called at the beginning of each
117 * Address of string representing name of subTest.
119 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
124 subTest(char *subTestName
)
126 (void) printf("TESTING: %s ...\n", subTestName
);
130 * FUNCTION: testErrorUndo
133 * Decrements the global variable "errCount" and prints a test failure
134 * expected message followed by the string pointed to by "msg". This function
135 * should be called when an expected error condition is encountered in the
136 * tests. Calling this function *correct* the previous errCount increment.
137 * It should only be called ONCE per subtest.
141 * Address of text of error message.
143 * Not Thread Safe - assumes exclusive access to "errCount"
144 * (see Thread Safety Definitions in Programmer's Guide)
149 testErrorUndo(char *msg
)
152 (void) printf("TEST FAILURE *** EXPECTED *** :%s\n", msg
);
156 * FUNCTION: testError
159 * Increments the global variable "errCount" and prints a standard test
160 * failure message followed by the string pointed to by "msg". This function
161 * should be called when an unexpected error condition is encountered in the
162 * tests. It should only be called ONCE per subtest.
166 * Address of text of error message.
168 * Not Thread Safe - assumes exclusive access to "errCount"
169 * (see Thread Safety Definitions in Programmer's Guide)
177 (void) printf("TEST FAILURE: %s\n", msg
);
181 * FUNCTION: PKIX_String2ASCII
184 * Converts String object pointed to by "string" to its ASCII representation
185 * and returns the converted value. Returns NULL upon failure.
187 * XXX Might want to use ESCASCII_DEBUG to show control characters, etc.
191 * Address of String to be converted to ASCII. Must be non-NULL.
193 * Platform-specific context pointer.
195 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
197 * Returns the ASCII representation of "string" upon success;
201 PKIX_String2ASCII(PKIX_PL_String
*string
, void *plContext
)
204 char *asciiString
= NULL
;
205 PKIX_Error
*errorResult
;
207 errorResult
= PKIX_PL_String_GetEncoded
210 (void **)&asciiString
,
214 if (errorResult
) goto cleanup
;
222 return (asciiString
);
227 * FUNCTION: PKIX_Error2ASCII
230 * Converts Error pointed to by "error" to its ASCII representation and
231 * returns the converted value. Returns NULL upon failure.
235 * Address of Error to be converted to ASCII. Must be non-NULL.
237 * Platform-specific context pointer.
239 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
241 * Returns the ASCII representation of "error" upon success;
245 PKIX_Error2ASCII(PKIX_Error
*error
, void *plContext
)
248 char *asciiString
= NULL
;
249 PKIX_PL_String
*pkixString
= NULL
;
250 PKIX_Error
*errorResult
= NULL
;
252 errorResult
= PKIX_PL_Object_ToString
253 ((PKIX_PL_Object
*)error
, &pkixString
, plContext
);
254 if (errorResult
) goto cleanup
;
256 errorResult
= PKIX_PL_String_GetEncoded
259 (void **)&asciiString
,
266 if (PKIX_PL_Object_DecRef
267 ((PKIX_PL_Object
*)pkixString
, plContext
)){
276 return (asciiString
);
280 * FUNCTION: PKIX_Object2ASCII
283 * Converts Object pointed to by "object" to its ASCII representation and
284 * returns the converted value. Returns NULL upon failure.
288 * Address of Object to be converted to ASCII. Must be non-NULL.
290 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
292 * Returns the ASCII representation of "object" upon success;
296 PKIX_Object2ASCII(PKIX_PL_Object
*object
)
299 char *asciiString
= NULL
;
300 PKIX_PL_String
*pkixString
= NULL
;
301 PKIX_Error
*errorResult
= NULL
;
303 errorResult
= PKIX_PL_Object_ToString
304 (object
, &pkixString
, NULL
);
305 if (errorResult
) goto cleanup
;
307 errorResult
= PKIX_PL_String_GetEncoded
308 (pkixString
, PKIX_ESCASCII
, (void **)&asciiString
, &length
, NULL
);
313 if (PKIX_PL_Object_DecRef((PKIX_PL_Object
*)pkixString
, NULL
)){
322 return (asciiString
);
326 * FUNCTION: PKIX_Cert2ASCII
329 * Converts Cert pointed to by "cert" to its partial ASCII representation and
330 * returns the converted value. Returns NULL upon failure.
334 * Address of Cert to be converted to ASCII. Must be non-NULL.
336 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
338 * Returns the partial ASCII representation of "cert" upon success;
342 PKIX_Cert2ASCII(PKIX_PL_Cert
*cert
)
344 PKIX_PL_X500Name
*issuer
= NULL
;
345 void *issuerAscii
= NULL
;
346 PKIX_PL_X500Name
*subject
= NULL
;
347 void *subjectAscii
= NULL
;
348 void *asciiString
= NULL
;
349 PKIX_Error
*errorResult
= NULL
;
350 PKIX_UInt32 numChars
;
353 errorResult
= PKIX_PL_Cert_GetIssuer(cert
, &issuer
, NULL
);
354 if (errorResult
) goto cleanup
;
356 issuerAscii
= PKIX_Object2ASCII((PKIX_PL_Object
*)issuer
);
359 errorResult
= PKIX_PL_Cert_GetSubject(cert
, &subject
, NULL
);
360 if (errorResult
) goto cleanup
;
363 subjectAscii
= PKIX_Object2ASCII((PKIX_PL_Object
*)subject
);
366 errorResult
= PKIX_PL_Malloc(200, &asciiString
, NULL
);
367 if (errorResult
) goto cleanup
;
373 "Issuer=%s\nSubject=%s\n",
377 if (!numChars
) goto cleanup
;
382 if (PKIX_PL_Object_DecRef((PKIX_PL_Object
*)issuer
, NULL
)){
388 if (PKIX_PL_Object_DecRef((PKIX_PL_Object
*)subject
, NULL
)){
393 if (PKIX_PL_Free((PKIX_PL_Object
*)issuerAscii
, NULL
)){
397 if (PKIX_PL_Free((PKIX_PL_Object
*)subjectAscii
, NULL
)){
405 return (asciiString
);
409 * FUNCTION: testHashcodeHelper
412 * Computes the hashcode of the Object pointed to by "goodObject" and the
413 * Object pointed to by "otherObject" and compares them. If the result of the
414 * comparison is not the desired match as specified by "match", an error
415 * message is generated.
419 * Address of an object. Must be non-NULL.
421 * Address of another object. Must be non-NULL.
423 * Boolean value representing the desired comparison result.
425 * Platform-specific context pointer.
427 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
433 PKIX_PL_Object
*goodObject
,
434 PKIX_PL_Object
*otherObject
,
439 PKIX_UInt32 goodHash
;
440 PKIX_UInt32 otherHash
;
441 PKIX_Boolean cmpResult
;
442 PKIX_TEST_STD_VARS();
444 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Hashcode
445 ((PKIX_PL_Object
*)goodObject
, &goodHash
, plContext
));
447 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Hashcode
448 ((PKIX_PL_Object
*)otherObject
, &otherHash
, plContext
));
450 cmpResult
= (goodHash
== otherHash
);
452 if ((match
&& !cmpResult
) || (!match
&& cmpResult
)){
453 testError("unexpected mismatch");
454 (void) printf("Hash1:\t%d\n", goodHash
);
455 (void) printf("Hash2:\t%d\n", otherHash
);
465 * FUNCTION: testToStringHelper
468 * Calls toString on the Object pointed to by "goodObject" and compares the
469 * result to the string pointed to by "expected". If the results are not
470 * equal, an error message is generated.
474 * Address of Object. Must be non-NULL.
476 * Address of the desired string.
478 * Platform-specific context pointer.
480 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
486 PKIX_PL_Object
*goodObject
,
490 PKIX_PL_String
*stringRep
= NULL
;
492 PKIX_TEST_STD_VARS();
494 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_ToString
495 (goodObject
, &stringRep
, plContext
));
497 actual
= PKIX_String2ASCII(stringRep
, plContext
);
499 pkixTestErrorMsg
= "PKIX_String2ASCII Failed";
504 * If you are having trouble matching the string, uncomment the
505 * PL_strstr function to figure out what's going on.
509 if (PL_strstr(actual, expected) == NULL){
510 testError("PL_strstr failed");
515 if (PL_strcmp(actual
, expected
) != 0){
516 testError("unexpected mismatch");
517 (void) printf("Actual value:\t%s\n", actual
);
518 (void) printf("Expected value:\t%s\n", expected
);
523 PKIX_PL_Free(actual
, plContext
);
525 PKIX_TEST_DECREF_AC(stringRep
);
531 * FUNCTION: testEqualsHelper
534 * Checks if the Object pointed to by "goodObject" is Equal to the Object
535 * pointed to by "otherObject". If the result of the check is not the desired
536 * match as specified by "match", an error message is generated.
540 * Address of an Object. Must be non-NULL.
542 * Address of another Object. Must be non-NULL.
544 * Boolean value representing the desired comparison result.
546 * Platform-specific context pointer.
548 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
554 PKIX_PL_Object
*goodObject
,
555 PKIX_PL_Object
*otherObject
,
560 PKIX_Boolean cmpResult
;
561 PKIX_TEST_STD_VARS();
563 PKIX_TEST_EXPECT_NO_ERROR
564 (PKIX_PL_Object_Equals
565 (goodObject
, otherObject
, &cmpResult
, plContext
));
567 if ((match
&& !cmpResult
) || (!match
&& cmpResult
)){
568 testError("unexpected mismatch");
569 (void) printf("Actual value:\t%d\n", cmpResult
);
570 (void) printf("Expected value:\t%d\n", match
);
580 * FUNCTION: testDuplicateHelper
582 * Checks if the Object pointed to by "object" is equal to its duplicate.
583 * If the result of the check is not equality, an error message is generated.
586 * Address of Object. Must be non-NULL.
588 * Platform-specific context pointer.
590 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
595 testDuplicateHelper(PKIX_PL_Object
*object
, void *plContext
)
597 PKIX_PL_Object
*newObject
= NULL
;
598 PKIX_Boolean cmpResult
;
600 PKIX_TEST_STD_VARS();
602 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Duplicate
603 (object
, &newObject
, plContext
));
605 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Equals
606 (object
, newObject
, &cmpResult
, plContext
));
609 testError("unexpected mismatch");
610 (void) printf("Actual value:\t%d\n", cmpResult
);
611 (void) printf("Expected value:\t%d\n", PKIX_TRUE
);
616 PKIX_TEST_DECREF_AC(newObject
);