Import from 1.9a8 tarball
[mozilla-nss.git] / security / nss / cmd / libpkix / testutil / testutil.c
blob49af03747cbdc1825662584863881ea3a71c5140
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
12 * License.
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.
21 * Contributor(s):
22 * Sun Microsystems
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 ***** */
38 * testutil.c
40 * Utility error handling functions
44 #include "testutil.h"
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
54 * DESCRIPTION:
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
58 * test suite.
60 * PARAMETERS:
61 * "testName"
62 * Address of string representing name of test suite.
63 * THREAD SAFETY:
64 * Not Thread Safe - assumes exclusive access to "errCount"
65 * (see Thread Safety Definitions in Programmer's Guide)
66 * RETURNS:
67 * Returns nothing.
69 void
70 startTests(char *testName)
72 (void) printf("*START OF TESTS FOR %s:\n", testName);
73 errCount = 0;
77 * FUNCTION: endTests
78 * DESCRIPTION:
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.
84 * PARAMETERS:
85 * "testName"
86 * Address of string representing name of test suite.
87 * THREAD SAFETY:
88 * Not Thread Safe - assumes exclusive access to "errCount"
89 * (see Thread Safety Definitions in Programmer's Guide)
90 * RETURNS:
91 * Returns nothing.
93 void
94 endTests(char *testName)
96 char plural = ' ';
98 (void) printf("*END OF TESTS FOR %s: ", testName);
99 if (errCount > 0) {
100 if (errCount > 1) plural = 's';
101 (void) printf("%d SUBTEST%c FAILED.\n\n", errCount, plural);
102 } else {
103 (void) printf("ALL TESTS COMPLETED SUCCESSFULLY.\n\n");
108 * FUNCTION: subTest
109 * DESCRIPTION:
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
113 * subtest.
115 * PARAMETERS:
116 * "subTestName"
117 * Address of string representing name of subTest.
118 * THREAD SAFETY:
119 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
120 * RETURNS:
121 * Returns nothing.
123 void
124 subTest(char *subTestName)
126 (void) printf("TESTING: %s ...\n", subTestName);
130 * FUNCTION: testErrorUndo
131 * DESCRIPTION:
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.
139 * PARAMETERS:
140 * "msg"
141 * Address of text of error message.
142 * THREAD SAFETY:
143 * Not Thread Safe - assumes exclusive access to "errCount"
144 * (see Thread Safety Definitions in Programmer's Guide)
145 * RETURNS:
146 * Returns nothing.
148 void
149 testErrorUndo(char *msg)
151 --errCount;
152 (void) printf("TEST FAILURE *** EXPECTED *** :%s\n", msg);
156 * FUNCTION: testError
157 * DESCRIPTION:
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.
164 * PARAMETERS:
165 * "msg"
166 * Address of text of error message.
167 * THREAD SAFETY:
168 * Not Thread Safe - assumes exclusive access to "errCount"
169 * (see Thread Safety Definitions in Programmer's Guide)
170 * RETURNS:
171 * Returns nothing.
173 void
174 testError(char *msg)
176 ++errCount;
177 (void) printf("TEST FAILURE: %s\n", msg);
181 * FUNCTION: PKIX_String2ASCII
182 * DESCRIPTION:
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.
189 * PARAMETERS:
190 * "string"
191 * Address of String to be converted to ASCII. Must be non-NULL.
192 * "plContext"
193 * Platform-specific context pointer.
194 * THREAD SAFETY:
195 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
196 * RETURNS:
197 * Returns the ASCII representation of "string" upon success;
198 * NULL upon failure.
200 char *
201 PKIX_String2ASCII(PKIX_PL_String *string, void *plContext)
203 PKIX_UInt32 length;
204 char *asciiString = NULL;
205 PKIX_Error *errorResult;
207 errorResult = PKIX_PL_String_GetEncoded
208 (string,
209 PKIX_ESCASCII,
210 (void **)&asciiString,
211 &length,
212 plContext);
214 if (errorResult) goto cleanup;
216 cleanup:
218 if (errorResult){
219 return (NULL);
222 return (asciiString);
227 * FUNCTION: PKIX_Error2ASCII
228 * DESCRIPTION:
230 * Converts Error pointed to by "error" to its ASCII representation and
231 * returns the converted value. Returns NULL upon failure.
233 * PARAMETERS:
234 * "error"
235 * Address of Error to be converted to ASCII. Must be non-NULL.
236 * "plContext"
237 * Platform-specific context pointer.
238 * THREAD SAFETY:
239 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
240 * RETURNS:
241 * Returns the ASCII representation of "error" upon success;
242 * NULL upon failure.
244 char *
245 PKIX_Error2ASCII(PKIX_Error *error, void *plContext)
247 PKIX_UInt32 length;
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
257 (pkixString,
258 PKIX_ESCASCII,
259 (void **)&asciiString,
260 &length,
261 plContext);
263 cleanup:
265 if (pkixString){
266 if (PKIX_PL_Object_DecRef
267 ((PKIX_PL_Object*)pkixString, plContext)){
268 return (NULL);
272 if (errorResult){
273 return (NULL);
276 return (asciiString);
280 * FUNCTION: PKIX_Object2ASCII
281 * DESCRIPTION:
283 * Converts Object pointed to by "object" to its ASCII representation and
284 * returns the converted value. Returns NULL upon failure.
286 * PARAMETERS:
287 * "object"
288 * Address of Object to be converted to ASCII. Must be non-NULL.
289 * THREAD SAFETY:
290 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
291 * RETURNS:
292 * Returns the ASCII representation of "object" upon success;
293 * NULL upon failure.
295 char *
296 PKIX_Object2ASCII(PKIX_PL_Object *object)
298 PKIX_UInt32 length;
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);
310 cleanup:
312 if (pkixString){
313 if (PKIX_PL_Object_DecRef((PKIX_PL_Object*)pkixString, NULL)){
314 return (NULL);
318 if (errorResult){
319 return (NULL);
322 return (asciiString);
326 * FUNCTION: PKIX_Cert2ASCII
327 * DESCRIPTION:
329 * Converts Cert pointed to by "cert" to its partial ASCII representation and
330 * returns the converted value. Returns NULL upon failure.
332 * PARAMETERS:
333 * "cert"
334 * Address of Cert to be converted to ASCII. Must be non-NULL.
335 * THREAD SAFETY:
336 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
337 * RETURNS:
338 * Returns the partial ASCII representation of "cert" upon success;
339 * NULL upon failure.
341 char *
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;
352 /* Issuer */
353 errorResult = PKIX_PL_Cert_GetIssuer(cert, &issuer, NULL);
354 if (errorResult) goto cleanup;
356 issuerAscii = PKIX_Object2ASCII((PKIX_PL_Object*)issuer);
358 /* Subject */
359 errorResult = PKIX_PL_Cert_GetSubject(cert, &subject, NULL);
360 if (errorResult) goto cleanup;
362 if (subject){
363 subjectAscii = PKIX_Object2ASCII((PKIX_PL_Object*)subject);
366 errorResult = PKIX_PL_Malloc(200, &asciiString, NULL);
367 if (errorResult) goto cleanup;
369 numChars =
370 PR_snprintf
371 (asciiString,
372 200,
373 "Issuer=%s\nSubject=%s\n",
374 issuerAscii,
375 subjectAscii);
377 if (!numChars) goto cleanup;
379 cleanup:
381 if (issuer){
382 if (PKIX_PL_Object_DecRef((PKIX_PL_Object*)issuer, NULL)){
383 return (NULL);
387 if (subject){
388 if (PKIX_PL_Object_DecRef((PKIX_PL_Object*)subject, NULL)){
389 return (NULL);
393 if (PKIX_PL_Free((PKIX_PL_Object*)issuerAscii, NULL)){
394 return (NULL);
397 if (PKIX_PL_Free((PKIX_PL_Object*)subjectAscii, NULL)){
398 return (NULL);
401 if (errorResult){
402 return (NULL);
405 return (asciiString);
409 * FUNCTION: testHashcodeHelper
410 * DESCRIPTION:
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.
417 * PARAMETERS:
418 * "goodObject"
419 * Address of an object. Must be non-NULL.
420 * "otherObject"
421 * Address of another object. Must be non-NULL.
422 * "match"
423 * Boolean value representing the desired comparison result.
424 * "plContext"
425 * Platform-specific context pointer.
426 * THREAD SAFETY:
427 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
428 * RETURNS:
429 * Returns nothing.
431 void
432 testHashcodeHelper(
433 PKIX_PL_Object *goodObject,
434 PKIX_PL_Object *otherObject,
435 PKIX_Boolean match,
436 void *plContext)
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);
458 cleanup:
460 PKIX_TEST_RETURN();
465 * FUNCTION: testToStringHelper
466 * DESCRIPTION:
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.
472 * PARAMETERS:
473 * "goodObject"
474 * Address of Object. Must be non-NULL.
475 * "expected"
476 * Address of the desired string.
477 * "plContext"
478 * Platform-specific context pointer.
479 * THREAD SAFETY:
480 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
481 * RETURNS:
482 * Returns nothing.
484 void
485 testToStringHelper(
486 PKIX_PL_Object *goodObject,
487 char *expected,
488 void *plContext)
490 PKIX_PL_String *stringRep = NULL;
491 char *actual = 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);
498 if (actual == NULL){
499 pkixTestErrorMsg = "PKIX_String2ASCII Failed";
500 goto cleanup;
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);
521 cleanup:
523 PKIX_PL_Free(actual, plContext);
525 PKIX_TEST_DECREF_AC(stringRep);
527 PKIX_TEST_RETURN();
531 * FUNCTION: testEqualsHelper
532 * DESCRIPTION:
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.
538 * PARAMETERS:
539 * "goodObject"
540 * Address of an Object. Must be non-NULL.
541 * "otherObject"
542 * Address of another Object. Must be non-NULL.
543 * "match"
544 * Boolean value representing the desired comparison result.
545 * "plContext"
546 * Platform-specific context pointer.
547 * THREAD SAFETY:
548 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
549 * RETURNS:
550 * Returns nothing.
552 void
553 testEqualsHelper(
554 PKIX_PL_Object *goodObject,
555 PKIX_PL_Object *otherObject,
556 PKIX_Boolean match,
557 void *plContext)
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);
573 cleanup:
575 PKIX_TEST_RETURN();
580 * FUNCTION: testDuplicateHelper
581 * DESCRIPTION:
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.
584 * PARAMETERS:
585 * "object"
586 * Address of Object. Must be non-NULL.
587 * "plContext"
588 * Platform-specific context pointer.
589 * THREAD SAFETY:
590 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
591 * RETURNS:
592 * Returns nothing.
594 void
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));
608 if (!cmpResult){
609 testError("unexpected mismatch");
610 (void) printf("Actual value:\t%d\n", cmpResult);
611 (void) printf("Expected value:\t%d\n", PKIX_TRUE);
614 cleanup:
616 PKIX_TEST_DECREF_AC(newObject);
618 PKIX_TEST_RETURN();