security/nss/cmd/libpkix/testutil/testutil.c

changeset 2
7e26c7da4463
equal deleted inserted replaced
-1:000000000000 0:17a258118e3c
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) plural = 's';
68 (void) printf("%d SUBTEST%c FAILED.\n\n", errCount, plural);
69 } else {
70 (void) printf("ALL TESTS COMPLETED SUCCESSFULLY.\n\n");
71 }
72 }
73
74 /*
75 * FUNCTION: subTest
76 * DESCRIPTION:
77 *
78 * Prints standard message for starting the subtest with the name pointed to
79 * by "subTestName". This function should be called at the beginning of each
80 * subtest.
81 *
82 * PARAMETERS:
83 * "subTestName"
84 * Address of string representing name of subTest.
85 * THREAD SAFETY:
86 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
87 * RETURNS:
88 * Returns nothing.
89 */
90 void
91 subTest(char *subTestName)
92 {
93 (void) printf("TESTING: %s ...\n", subTestName);
94 }
95
96 /*
97 * FUNCTION: testErrorUndo
98 * DESCRIPTION:
99 *
100 * Decrements the global variable "errCount" and prints a test failure
101 * expected message followed by the string pointed to by "msg". This function
102 * should be called when an expected error condition is encountered in the
103 * tests. Calling this function *correct* the previous errCount increment.
104 * It should only be called ONCE per subtest.
105 *
106 * PARAMETERS:
107 * "msg"
108 * Address of text of error message.
109 * THREAD SAFETY:
110 * Not Thread Safe - assumes exclusive access to "errCount"
111 * (see Thread Safety Definitions in Programmer's Guide)
112 * RETURNS:
113 * Returns nothing.
114 */
115 void
116 testErrorUndo(char *msg)
117 {
118 --errCount;
119 (void) printf("TEST FAILURE *** EXPECTED *** :%s\n", msg);
120 }
121
122 /*
123 * FUNCTION: testError
124 * DESCRIPTION:
125 *
126 * Increments the global variable "errCount" and prints a standard test
127 * failure message followed by the string pointed to by "msg". This function
128 * should be called when an unexpected error condition is encountered in the
129 * tests. It should only be called ONCE per subtest.
130 *
131 * PARAMETERS:
132 * "msg"
133 * Address of text of error message.
134 * THREAD SAFETY:
135 * Not Thread Safe - assumes exclusive access to "errCount"
136 * (see Thread Safety Definitions in Programmer's Guide)
137 * RETURNS:
138 * Returns nothing.
139 */
140 void
141 testError(char *msg)
142 {
143 ++errCount;
144 (void) printf("TEST FAILURE: %s\n", msg);
145 }
146
147 /*
148 * FUNCTION: PKIX_String2ASCII
149 * DESCRIPTION:
150 *
151 * Converts String object pointed to by "string" to its ASCII representation
152 * and returns the converted value. Returns NULL upon failure.
153 *
154 * XXX Might want to use ESCASCII_DEBUG to show control characters, etc.
155 *
156 * PARAMETERS:
157 * "string"
158 * Address of String to be converted to ASCII. Must be non-NULL.
159 * "plContext"
160 * Platform-specific context pointer.
161 * THREAD SAFETY:
162 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
163 * RETURNS:
164 * Returns the ASCII representation of "string" upon success;
165 * NULL upon failure.
166 */
167 char *
168 PKIX_String2ASCII(PKIX_PL_String *string, void *plContext)
169 {
170 PKIX_UInt32 length;
171 char *asciiString = NULL;
172 PKIX_Error *errorResult;
173
174 errorResult = PKIX_PL_String_GetEncoded
175 (string,
176 PKIX_ESCASCII,
177 (void **)&asciiString,
178 &length,
179 plContext);
180
181 if (errorResult) goto cleanup;
182
183 cleanup:
184
185 if (errorResult){
186 return (NULL);
187 }
188
189 return (asciiString);
190
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
220 ((PKIX_PL_Object*)error, &pkixString, plContext);
221 if (errorResult) goto cleanup;
222
223 errorResult = PKIX_PL_String_GetEncoded
224 (pkixString,
225 PKIX_ESCASCII,
226 (void **)&asciiString,
227 &length,
228 plContext);
229
230 cleanup:
231
232 if (pkixString){
233 if (PKIX_PL_Object_DecRef
234 ((PKIX_PL_Object*)pkixString, plContext)){
235 return (NULL);
236 }
237 }
238
239 if (errorResult){
240 return (NULL);
241 }
242
243 return (asciiString);
244 }
245
246 /*
247 * FUNCTION: PKIX_Object2ASCII
248 * DESCRIPTION:
249 *
250 * Converts Object pointed to by "object" to its ASCII representation and
251 * returns the converted value. Returns NULL upon failure.
252 *
253 * PARAMETERS:
254 * "object"
255 * Address of Object to be converted to ASCII. Must be non-NULL.
256 * THREAD SAFETY:
257 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
258 * RETURNS:
259 * Returns the ASCII representation of "object" upon success;
260 * NULL upon failure.
261 */
262 char *
263 PKIX_Object2ASCII(PKIX_PL_Object *object)
264 {
265 PKIX_UInt32 length;
266 char *asciiString = NULL;
267 PKIX_PL_String *pkixString = NULL;
268 PKIX_Error *errorResult = NULL;
269
270 errorResult = PKIX_PL_Object_ToString
271 (object, &pkixString, NULL);
272 if (errorResult) goto cleanup;
273
274 errorResult = PKIX_PL_String_GetEncoded
275 (pkixString, PKIX_ESCASCII, (void **)&asciiString, &length, NULL);
276
277 cleanup:
278
279 if (pkixString){
280 if (PKIX_PL_Object_DecRef((PKIX_PL_Object*)pkixString, NULL)){
281 return (NULL);
282 }
283 }
284
285 if (errorResult){
286 return (NULL);
287 }
288
289 return (asciiString);
290 }
291
292 /*
293 * FUNCTION: PKIX_Cert2ASCII
294 * DESCRIPTION:
295 *
296 * Converts Cert pointed to by "cert" to its partial ASCII representation and
297 * returns the converted value. Returns NULL upon failure.
298 *
299 * PARAMETERS:
300 * "cert"
301 * Address of Cert to be converted to ASCII. Must be non-NULL.
302 * THREAD SAFETY:
303 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
304 * RETURNS:
305 * Returns the partial ASCII representation of "cert" upon success;
306 * NULL upon failure.
307 */
308 char *
309 PKIX_Cert2ASCII(PKIX_PL_Cert *cert)
310 {
311 PKIX_PL_X500Name *issuer = NULL;
312 void *issuerAscii = NULL;
313 PKIX_PL_X500Name *subject = NULL;
314 void *subjectAscii = NULL;
315 void *asciiString = NULL;
316 PKIX_Error *errorResult = NULL;
317 PKIX_UInt32 numChars;
318
319 /* Issuer */
320 errorResult = PKIX_PL_Cert_GetIssuer(cert, &issuer, NULL);
321 if (errorResult) goto cleanup;
322
323 issuerAscii = PKIX_Object2ASCII((PKIX_PL_Object*)issuer);
324
325 /* Subject */
326 errorResult = PKIX_PL_Cert_GetSubject(cert, &subject, NULL);
327 if (errorResult) goto cleanup;
328
329 if (subject){
330 subjectAscii = PKIX_Object2ASCII((PKIX_PL_Object*)subject);
331 }
332
333 errorResult = PKIX_PL_Malloc(200, &asciiString, NULL);
334 if (errorResult) goto cleanup;
335
336 numChars =
337 PR_snprintf
338 (asciiString,
339 200,
340 "Issuer=%s\nSubject=%s\n",
341 issuerAscii,
342 subjectAscii);
343
344 if (!numChars) 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
412 ((PKIX_PL_Object *)goodObject, &goodHash, plContext));
413
414 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Hashcode
415 ((PKIX_PL_Object *)otherObject, &otherHash, plContext));
416
417 cmpResult = (goodHash == otherHash);
418
419 if ((match && !cmpResult) || (!match && cmpResult)){
420 testError("unexpected mismatch");
421 (void) printf("Hash1:\t%d\n", goodHash);
422 (void) printf("Hash2:\t%d\n", otherHash);
423 }
424
425 cleanup:
426
427 PKIX_TEST_RETURN();
428
429 }
430
431 /*
432 * FUNCTION: testToStringHelper
433 * DESCRIPTION:
434 *
435 * Calls toString on the Object pointed to by "goodObject" and compares the
436 * result to the string pointed to by "expected". If the results are not
437 * equal, an error message is generated.
438 *
439 * PARAMETERS:
440 * "goodObject"
441 * Address of Object. Must be non-NULL.
442 * "expected"
443 * Address of the desired string.
444 * "plContext"
445 * Platform-specific context pointer.
446 * THREAD SAFETY:
447 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
448 * RETURNS:
449 * Returns nothing.
450 */
451 void
452 testToStringHelper(
453 PKIX_PL_Object *goodObject,
454 char *expected,
455 void *plContext)
456 {
457 PKIX_PL_String *stringRep = NULL;
458 char *actual = NULL;
459 PKIX_TEST_STD_VARS();
460
461 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_ToString
462 (goodObject, &stringRep, plContext));
463
464 actual = PKIX_String2ASCII(stringRep, plContext);
465 if (actual == NULL){
466 pkixTestErrorMsg = "PKIX_String2ASCII Failed";
467 goto cleanup;
468 }
469
470 /*
471 * If you are having trouble matching the string, uncomment the
472 * PL_strstr function to figure out what's going on.
473 */
474
475 /*
476 if (PL_strstr(actual, expected) == NULL){
477 testError("PL_strstr failed");
478 }
479 */
480
481
482 if (PL_strcmp(actual, expected) != 0){
483 testError("unexpected mismatch");
484 (void) printf("Actual value:\t%s\n", actual);
485 (void) printf("Expected value:\t%s\n", expected);
486 }
487
488 cleanup:
489
490 PKIX_PL_Free(actual, plContext);
491
492 PKIX_TEST_DECREF_AC(stringRep);
493
494 PKIX_TEST_RETURN();
495 }
496
497 /*
498 * FUNCTION: testEqualsHelper
499 * DESCRIPTION:
500 *
501 * Checks if the Object pointed to by "goodObject" is Equal to the Object
502 * pointed to by "otherObject". If the result of the check is not the desired
503 * match as specified by "match", an error message is generated.
504 *
505 * PARAMETERS:
506 * "goodObject"
507 * Address of an Object. Must be non-NULL.
508 * "otherObject"
509 * Address of another Object. Must be non-NULL.
510 * "match"
511 * Boolean value representing the desired comparison result.
512 * "plContext"
513 * Platform-specific context pointer.
514 * THREAD SAFETY:
515 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
516 * RETURNS:
517 * Returns nothing.
518 */
519 void
520 testEqualsHelper(
521 PKIX_PL_Object *goodObject,
522 PKIX_PL_Object *otherObject,
523 PKIX_Boolean match,
524 void *plContext)
525 {
526
527 PKIX_Boolean cmpResult;
528 PKIX_TEST_STD_VARS();
529
530 PKIX_TEST_EXPECT_NO_ERROR
531 (PKIX_PL_Object_Equals
532 (goodObject, otherObject, &cmpResult, plContext));
533
534 if ((match && !cmpResult) || (!match && cmpResult)){
535 testError("unexpected mismatch");
536 (void) printf("Actual value:\t%d\n", cmpResult);
537 (void) printf("Expected value:\t%d\n", match);
538 }
539
540 cleanup:
541
542 PKIX_TEST_RETURN();
543 }
544
545
546 /*
547 * FUNCTION: testDuplicateHelper
548 * DESCRIPTION:
549 * Checks if the Object pointed to by "object" is equal to its duplicate.
550 * If the result of the check is not equality, an error message is generated.
551 * PARAMETERS:
552 * "object"
553 * Address of Object. Must be non-NULL.
554 * "plContext"
555 * Platform-specific context pointer.
556 * THREAD SAFETY:
557 * Thread Safe (see Thread Safety Definitions in Programmer's Guide)
558 * RETURNS:
559 * Returns nothing.
560 */
561 void
562 testDuplicateHelper(PKIX_PL_Object *object, void *plContext)
563 {
564 PKIX_PL_Object *newObject = NULL;
565 PKIX_Boolean cmpResult;
566
567 PKIX_TEST_STD_VARS();
568
569 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Duplicate
570 (object, &newObject, plContext));
571
572 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Equals
573 (object, newObject, &cmpResult, plContext));
574
575 if (!cmpResult){
576 testError("unexpected mismatch");
577 (void) printf("Actual value:\t%d\n", cmpResult);
578 (void) printf("Expected value:\t%d\n", PKIX_TRUE);
579 }
580
581 cleanup:
582
583 PKIX_TEST_DECREF_AC(newObject);
584
585 PKIX_TEST_RETURN();
586 }

mercurial