]> git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c++-examples/test-lib/test-lib.C
Security-28.tar.gz
[apple/security.git] / SecuritySNACCRuntime / c++-examples / test-lib / test-lib.C
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 // c++_examples/test_lib/test_lib.C
20 //
21 // $Header: /cvs/Darwin/Security/SecuritySNACCRuntime/c++-examples/test-lib/test-lib.C,v 1.1.1.1 2001/05/18 23:14:05 mb Exp $
22 // $Log: test-lib.C,v $
23 // Revision 1.1.1.1 2001/05/18 23:14:05 mb
24 // Move from private repository to open source repository
25 //
26 // Revision 1.3 2001/05/05 00:59:17 rmurphy
27 // Adding darwin license headers
28 //
29 // Revision 1.2 2000/06/08 19:59:34 dmitch
30 // Mods for X port.
31 //
32 // Revision 1.1.1.1 1999/03/16 18:05:58 aram
33 // Originals from SMIME Free Library.
34 //
35 // Revision 1.5 1997/02/28 13:39:42 wan
36 // Modifications collected for new version 1.3: Bug fixes, tk4.2.
37 //
38 // Revision 1.4 1995/07/24 15:44:10 rj
39 // #error "..." instead of #error ...
40 //
41 // changed `_' to `-' in file names.
42 //
43 // function and file names adjusted.
44 //
45 // Revision 1.3 1995/02/18 16:40:08 rj
46 // utilize either isinf(3) or finite(3), whatever happens to be present.
47 //
48 // Revision 1.2 1994/08/31 08:56:35 rj
49 // first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
50 //
51
52 #include <stdio.h>
53 #include <iostream.h>
54 #include "asn-incl.h"
55
56
57 int TestAsnBuffers();
58 int TestAsnTag();
59 int TestAsnLen();
60 int TestAsnBool();
61 int TestAsnInt();
62 int TestAsnReal();
63 int TestAsnOcts();
64 int TestAsnBits();
65 int TestAsnOid();
66 int TestAsnList();
67
68 const int bufSize = 256;
69
70 int main()
71 {
72 int isErr = false;
73
74 if (!TestAsnBuffers())
75 {
76 cout << "Failed buffer tests, no point in proceeding ... bye!" << endl;
77 return 1;
78 }
79
80 if (!TestAsnTag())
81 {
82 cout << "Failed Tag test." << endl;
83 isErr = true;
84 }
85
86 if (!TestAsnLen())
87 {
88 cout << "Failed Length test." << endl;
89 isErr = true;
90 }
91
92 if (!TestAsnBool())
93 {
94 cout << "Failed BOOLEAN test." << endl;
95 isErr = true;
96 }
97
98
99 if (!TestAsnInt())
100 {
101 cout << "Failed INTEGER test." << endl;
102 isErr = true;
103 }
104
105 if (!TestAsnOcts())
106 {
107 cout << "Failed OCTET STRING test." << endl;
108 isErr = true;
109 }
110
111
112 if (!TestAsnBits())
113 {
114 cout << "Failed BIT STRING test." << endl;
115 isErr = true;
116 }
117
118
119 if (!TestAsnOid())
120 {
121 cout << "Failed OBJECT IDENTIFIER test." << endl;
122 isErr = true;
123 }
124
125
126 if (!TestAsnReal())
127 {
128 cout << "Failed REAL test." << endl;
129 isErr = true;
130 }
131
132
133
134 if (isErr)
135 {
136 cout << "There are errors in the primitive type encoding/decoding" << endl;
137 cout << "library for this architecture. Time for gdb..." << endl;
138 }
139 else
140 {
141 cout << "The primitive type encoding/decoding library passed simple tests." << endl;
142 cout << "It should be safe to use..." << endl;
143 }
144 return isErr;
145 }
146
147
148 /*
149 * returns true if passes encode/decode tests
150 */
151 int
152 TestAsnBuffers()
153 {
154 AsnBuf b;
155 char bufData[256];
156 int i,j;
157 int noErr = true;
158
159 // initialize buffer
160 b.Init (bufData, 256);
161 b.ResetInWriteRvsMode();
162
163 // write whole range of byte (0..255)
164 // remember, write works in reverse
165 for (i = 0; i < 256; i++)
166 b.PutByteRvs (i);
167
168 if (b.WriteError())
169 {
170 cout << "Error writing to buffer." << endl;
171 noErr = false;
172 }
173
174 // read in values & verify
175 b.ResetInReadMode();
176 for (i = 255; i >= 0; i--)
177 if (b.GetByte() != i)
178 {
179 cout << "Error verifying data written to buffer." << endl;
180 noErr = false;
181 }
182
183 if (b.ReadError())
184 {
185 cout << "Error reading from buffer." << endl;
186 noErr = false;
187 }
188
189
190 /* now make sure errors are detected */
191 b.ResetInWriteRvsMode();
192
193 for (i = 0; i < 257; i++) // write past end of buffer
194 b.PutByteRvs (0);
195
196 if (!b.WriteError())
197 {
198 cout << "Buffers failed to report buffer write overflow." << endl;
199 noErr = false;
200 }
201
202
203 b.ResetInReadMode();
204 for (i = 256; i >= 0; i--) // read past end of buffer
205 b.GetByte();
206
207 if (!b.ReadError())
208 {
209 cout << "Buffers failed to report buffer read overflow." << endl;
210 noErr = false;
211 }
212
213 return noErr;
214 } /* TestAsnBuffers */
215
216
217
218 /*
219 * returns true if passes encode/decode tests
220 */
221 int
222 TestAsnTag()
223 {
224 AsnTag aTag1;
225 AsnTag aTag2;
226 int i, j;
227 AsnLen len1;
228 AsnLen len2;
229 AsnTag tag;
230 int noErr = true;
231 ENV_TYPE env;
232 AsnBuf b;
233 char bufData[256];
234 long int val;
235 BER_CLASS tagClass;
236 BER_FORM form;
237 BER_UNIV_CODE code;
238
239
240 /* initialize buffer */
241 b.Init (bufData, 256);
242
243 /* encode a TRUE value and verify */
244 tagClass = UNIV;
245 form = PRIM;
246 code = INTEGER_TAG_CODE;
247 aTag1 = MAKE_TAG_ID (tagClass, form, code);
248
249 for (i = 0; i < 2; i++)
250 {
251 b.ResetInWriteRvsMode();
252 len1 = BEncTag1 (b, tagClass, form, code);
253
254 if (b.WriteError())
255 {
256 noErr = false;
257 cout << "Error encoding a Tag." << endl;
258 }
259
260 b.ResetInReadMode();
261
262 aTag2 = 0;
263
264 /* make sure no decode errors and that it decodes to same tag */
265 len2 = 0;
266 if ((val = setjmp (env)) == 0)
267 {
268 aTag2 = BDecTag (b, len2, env);
269 }
270 else
271 {
272 noErr = false;
273 cout << "Error decoding a Tag - error number " << val << endl;
274 }
275 if (noErr && ((aTag2 != aTag1) || (len1 != len2)))
276 {
277 noErr = false;
278 cout << "Error decoded Tag does not match encoded Tag." << endl;
279 }
280 /* set a new test tag value */
281 tagClass = CNTX;
282 form = CONS;
283 code = (BER_UNIV_CODE) 29;
284 aTag1 = MAKE_TAG_ID (tagClass, form, code);
285 }
286 return noErr;
287 } /* TestAsnTag */
288
289
290 /*
291 * returns true if passes encode/decode tests
292 */
293 int
294 TestAsnLen()
295 {
296 AsnLen aLen1;
297 AsnLen aLen2;
298 int i,j;
299 AsnLen len1;
300 AsnLen len2;
301 AsnTag tag;
302 int noErr = true;
303 ENV_TYPE env;
304 AsnBuf b;
305 char bufData[256];
306 long int val;
307
308 /* initialize buffer */
309 b.Init (bufData, 256);
310
311
312 /* encode a TRUE value and verify */
313 aLen1 = 99999;
314 for (i = 0; i < 2; i++)
315 {
316 b.ResetInWriteRvsMode();
317 len1 = BEncDefLen (b, aLen1);
318
319 if (b.WriteError())
320 {
321 noErr = false;
322 cout << "Error encoding Length." << endl;
323 }
324
325 b.ResetInReadMode();
326
327 aLen2 = 0;
328
329 /* make sure no decode errors and that it decodes to true */
330 len2 = 0;
331 if ((val = setjmp (env)) == 0)
332 {
333 aLen2 = BDecLen (b, len2, env);
334 }
335 else
336 {
337 noErr = false;
338 cout << "Error decoding Length - error number " << val << endl;
339 }
340
341
342 if (noErr && ((aLen2 != aLen1) || (len1 != len2)))
343 {
344 noErr = false;
345 cout << "Error - decoded length does not match encoded length" << endl;
346 }
347 aLen1 = 2;
348 }
349
350
351 /* test indef len */
352 b.ResetInWriteRvsMode();
353 len1 = BEncIndefLen (b);
354
355 if (b.WriteError())
356 {
357 noErr = false;
358 cout << "Error encoding indefinite Length." << endl;
359 }
360
361 b.ResetInReadMode();
362
363 aLen2 = 0;
364
365 /* make sure no decode errors */
366 len2 = 0;
367 if ((val = setjmp (env)) == 0)
368 {
369 aLen2 = BDecLen (b, len2, env);
370 }
371 else
372 {
373 noErr = false;
374 cout << "Error decoding Length - error number " << val << endl;
375 }
376
377
378 if (noErr && ((aLen2 != INDEFINITE_LEN) || (len1 != len2)))
379 {
380 noErr = false;
381 cout << "Error - decoded length does not match encoded length" << endl;
382 }
383
384 /* test EOC */
385 b.ResetInWriteRvsMode();
386 len1 = BEncEoc (b);
387
388 if (b.WriteError())
389 {
390 noErr = false;
391 cout << "Error encoding indefinite Length." << endl;
392 }
393
394 b.ResetInReadMode();
395
396 aLen2 = 0;
397
398 /* make sure no decode errors */
399 len2 = 0;
400 if ((val = setjmp (env)) == 0)
401 {
402 BDecEoc (b, len2, env);
403 }
404 else
405 {
406 noErr = false;
407 cout << "Error decoding Length - error number " << val << endl;
408 }
409
410
411 if (noErr && (len1 != len2))
412 {
413 noErr = false;
414 cout << "Error - decoded EOC length error" << endl;
415 }
416
417 return noErr;
418 } /* TestAsnLen */
419
420
421
422 /*
423 * returns true if passes encode/decode tests
424 */
425 int
426 TestAsnBool()
427 {
428 AsnBuf b;
429 char bufData[bufSize];
430 AsnBool aBool1;
431 AsnBool aBool2;
432 int j;
433 AsnLen len1;
434 AsnLen len2;
435 int noErr = true;
436
437 // initialize a small buffer
438 b.Init (bufData, bufSize);
439 b.ResetInWriteRvsMode();
440
441 // encode a true value and verify
442 aBool1 = true;
443
444 if (!aBool1.BEncPdu (b, len1))
445 {
446 noErr = false;
447 cout << "Error encoding TRUE BOOLEAN value." << endl;
448 }
449
450 b.ResetInReadMode();
451
452 aBool2 = false; // set to opposite of expected value
453
454 // make sure no decode errors and that it decodes to true
455 if (!aBool2.BDecPdu (b, len2) || !aBool2 || (len1 != len2))
456 {
457 noErr = false;
458 cout << "Error decoding TRUE BOOLEAN value." << endl;
459 }
460
461 // now encode a false value and verify
462 b.ResetInWriteRvsMode();
463 aBool1 = false;
464
465 if (!aBool1.BEncPdu (b, len1))
466 {
467 noErr = false;
468 cout << "Error encoding FALSE BOOLEAN value." << endl;
469 }
470
471 b.ResetInReadMode();
472
473 aBool2 = true; // set to opposite of expected value
474
475 // make sure no decode errors and that it decodes to false
476 if (!aBool2.BDecPdu (b, len2) || aBool2 || (len1 != len2))
477 {
478 noErr = false;
479 cout << "Error decoding FALSE BOOLEAN value." << endl;
480 }
481
482 return noErr;
483 } /* TestAsnBool */
484
485
486 /*
487 * returns true if passes encode/decode tests
488 */
489 int
490 TestAsnInt()
491 {
492 AsnBuf b;
493 char bufData[bufSize];
494 AsnInt a1;
495 AsnInt a2;
496 int i,j, sign;
497 AsnLen len1;
498 AsnLen len2;
499 int noErr = true;
500
501 // initialize a small buffer
502 b.Init (bufData, bufSize);
503
504 //
505 // Encode a range of integers: negative & positive in
506 // the 1 to sizeof (long int) range
507 //
508
509 sign = 1;
510 for (j = 0; j < 2; j++)
511 {
512 for (i = 0; i < sizeof (long int); i++)
513 {
514 b.ResetInWriteRvsMode();
515
516 a1 = sign * (17 << (i * 8)); // 17 is a random choice
517 if (!a1.BEncPdu (b, len1))
518 {
519 noErr = false;
520 cout << "Error encoding INTEGER value " << a1 << "." << endl;
521 }
522
523 b.ResetInReadMode();
524 a2 = 0;
525
526 // make sure no decode errors and that it decodes to the correc val
527 if (!a2.BDecPdu (b, len2) || (a2 != a1) || (len1 != len2))
528 {
529 noErr = false;
530 cout << "Error decoding INTEGER value " << a1 << "." << endl;
531 }
532 }
533 sign = -1;
534 }
535
536 return noErr;
537
538 } /* TestAsnInt */
539
540
541 /*
542 * returns true if passes encode/decode tests
543 */
544 int
545 TestAsnOcts()
546 {
547 AsnBuf b;
548 char bufData[bufSize];
549 AsnOcts a1;
550 AsnOcts a2;
551 int i,j;
552 AsnLen len1;
553 AsnLen len2;
554 int noErr = true;
555
556 // initialize a small buffer
557 b.Init (bufData, bufSize);
558
559 a1 = "Hello Gumby?";
560 for (j = 0; j < 2; j++)
561 {
562 b.ResetInWriteRvsMode();
563
564 if (!a1.BEncPdu (b, len1))
565 {
566 noErr = false;
567 cout << "Error encoding OCTET STRING value " << a1 << "." << endl;
568 }
569
570 b.ResetInReadMode();
571
572 // make sure no decode errors and that it decodes to the correc val
573 if (!a2.BDecPdu (b, len2) || (a2 != a1) || (len1 != len2))
574 {
575 noErr = false;
576 cout << "Error decoding OCTET STRING value " << a1 << "." << endl;
577 }
578 a1 = ""; // try an empty string
579 }
580
581 return noErr;
582
583 } /* TestAsnOcts */
584
585
586
587 /*
588 * returns true if passes encode/decode tests
589 */
590 int
591 TestAsnBits()
592 {
593 AsnBuf b;
594 char bufData[bufSize];
595 AsnBits a1 (32);
596 AsnBits a2 (32);
597 short bitsToSet[32] = { 0, 1, 0, 0, 1, 1, 0, 1,
598 0, 1, 0, 0, 1, 1, 0, 1,
599 0, 1, 0, 0, 1, 1, 0, 1,
600 0, 1, 0, 0, 1, 1, 0, 1 };
601 int i,j;
602 AsnLen len1;
603 AsnLen len2;
604 int noErr = true;
605
606 // initialize a small buffer
607 b.Init (bufData, bufSize);
608
609
610 // set some bits
611 for (i = 0; i < 32; i++)
612 {
613 if (bitsToSet[i])
614 a1.SetBit (i);
615 else
616 a1.ClrBit (i);
617
618 }
619
620 b.ResetInWriteRvsMode();
621 if (!a1.BEncPdu (b, len1))
622 {
623 noErr = false;
624 cout << "Error encoding BIT STRING value " << a1 << "." << endl;
625 }
626
627 b.ResetInReadMode();
628
629 // make sure no decode errors and that it decodes to the correc val
630 if (!a2.BDecPdu (b, len2) || (a2 != a1) || (len1 != len2))
631 {
632 noErr = false;
633 cout << "Error decoding BIT STRING value " << a1 << "." << endl;
634 }
635
636
637 return noErr;
638
639 } /* TestAsnBits */
640
641
642
643 /*
644 * returns true if passes encode/decode tests
645 */
646 int
647 TestAsnOid()
648 {
649 AsnBuf b;
650 char bufData[bufSize];
651 AsnOid a1 (0,1,2,3,4,5,6);
652 AsnOid a2;
653 AsnOid a3 (2,38,29,40,200,10,4000);
654 int i,j;
655 AsnLen len1;
656 AsnLen len2;
657 int noErr = true;
658
659 // initialize a small buffer
660 b.Init (bufData, bufSize);
661
662 for (i = 0; i < 2; i++)
663 {
664 b.ResetInWriteRvsMode();
665
666 if (!a1.BEncPdu (b, len1))
667 {
668 noErr = false;
669 cout << "Error encoding OBJECT IDENTIFIER value " << a1 << "." << endl;
670 }
671
672 b.ResetInReadMode();
673
674 // make sure no decode errors and that it decodes to the correc val
675 if (!a2.BDecPdu (b, len2) || (a2 != a1) || (len1 != len2))
676 {
677 noErr = false;
678 cout << "Error decoding OBJECT IDENTIFIER value " << a1 << "." << endl;
679 }
680
681 a1 = a3;
682 }
683 return noErr;
684
685 } /* TestAsnOid */
686
687 /*
688 * returns true if passes encode/decode tests
689 *
690 * NOT USED - nuked template design.
691 */
692 /*
693 int
694 TestAsnList()
695 {
696 AsnBuf b;
697 char bufData[bufSize];
698 AsnList<AsnInt> intList1;
699 AsnList<AsnInt> intList2;
700 AsnList<AsnBool> boolList1;
701 AsnList<AsnBool> boolList2;
702 int i,j;
703 AsnLen len1;
704 AsnLen len2;
705 int noErr = true;
706
707 b.Init (bufData, bufSize);
708
709 b.ResetInWriteRvsMode();
710
711 if (!intList1.BEncPdu (b, len1))
712 {
713 noErr = false;
714 cout << "Error encoding SEQUENCE OF value " << intList1 << "." << endl;
715 }
716
717 b.ResetInReadMode();
718
719 if (!intList2.BDecPdu (b, len2) || (len1 != len2))
720 {
721 noErr = false;
722 cout << "Error decoding SEQUENCE OF value " << intList1 << "." << endl;
723 }
724 cout << "intlist 1 = " << intList1 << endl;
725 cout << "intlist 2 = " << intList1 << endl;
726
727
728 if (!boolList1.BEncPdu (b, len1))
729 {
730 noErr = false;
731 cout << "Error encoding SEQUENCE OF value " << boolList1 << "." << endl;
732 }
733
734 b.ResetInReadMode();
735
736 if (!boolList2.BDecPdu (b, len2) || (len1 != len2))
737 {
738 noErr = false;
739 cout << "Error decoding SEQUENCE OF value " << boolList1 << "." << endl;
740 }
741 cout << "boolList 1 = " << boolList1 << endl;
742 cout << "boolList 2 = " << boolList1 << endl;
743
744 return noErr;
745
746 } TestAsnList */
747
748
749
750 /*
751 * returns true if passes encode/decode tests
752 */
753 int
754 TestAsnReal()
755 {
756 #ifdef __APPLE__
757 /* we don't seem to have any of this stuff */
758 return true;
759 #else
760 AsnBuf b;
761 char bufData[bufSize];
762 AsnReal a2;
763 AsnReal a[] = { 0.0, 0.8, -22.484848, PLUS_INFINITY, MINUS_INFINITY};
764 int i,j;
765 AsnLen len1;
766 AsnLen len2;
767 int noErr = true;
768
769
770 /*
771 * if you do not have the ieee_functions in your math lib,
772 * this will not link. Comment it out and cross you fingers.
773 * (or check/set the +/-infinity values for you architecture)
774 */
775 #if HAVE_ISINF
776 if (!isinf ((double)PLUS_INFINITY)) || !isinf ((double)MINUS_INFINITY))
777 #else
778 #if HAVE_FINITE
779 if (finite ((double)PLUS_INFINITY) || finite ((double)MINUS_INFINITY))
780 #else
781 #error "oops: you've got neither isinf(3) nor finite(3)?!"
782 #endif
783 #endif
784 {
785 cout << "WARNING: PLUS_INFINITY and MINUS_INFINITY in .../c++-lib/src/asn-real.C are" << endl;
786 cout << "not correct for this architecture. Modify the AsnPlusInfinity() routine." << endl;
787 }
788
789
790 // initialize a small buffer
791 b.Init (bufData, bufSize);
792
793 for (i = 0; i < 5; i++)
794 {
795 b.ResetInWriteRvsMode();
796
797 if (!a[i].BEncPdu (b, len1))
798 {
799 noErr = false;
800 cout << "Error encoding REAL value " << a[i] << "." << endl;
801 }
802
803 b.ResetInReadMode();
804
805 // make sure no decode errors and that it decodes to the correc val
806 if (!a2.BDecPdu (b, len2) || (a2 != a[i]) || (len1 != len2))
807 {
808 noErr = false;
809 cout << "Error decoding REAL value " << a[i] << "." << endl;
810 }
811 }
812
813 return noErr;
814 #endif
815 } /* TestAsnReal */