2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
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
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.
19 // c++_examples/test_lib/test_lib.C
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
26 // Revision 1.3 2001/05/05 00:59:17 rmurphy
27 // Adding darwin license headers
29 // Revision 1.2 2000/06/08 19:59:34 dmitch
32 // Revision 1.1.1.1 1999/03/16 18:05:58 aram
33 // Originals from SMIME Free Library.
35 // Revision 1.5 1997/02/28 13:39:42 wan
36 // Modifications collected for new version 1.3: Bug fixes, tk4.2.
38 // Revision 1.4 1995/07/24 15:44:10 rj
39 // #error "..." instead of #error ...
41 // changed `_' to `-' in file names.
43 // function and file names adjusted.
45 // Revision 1.3 1995/02/18 16:40:08 rj
46 // utilize either isinf(3) or finite(3), whatever happens to be present.
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.
68 const int bufSize = 256;
74 if (!TestAsnBuffers())
76 cout << "Failed buffer tests, no point in proceeding ... bye!" << endl;
82 cout << "Failed Tag test." << endl;
88 cout << "Failed Length test." << endl;
94 cout << "Failed BOOLEAN test." << endl;
101 cout << "Failed INTEGER test." << endl;
107 cout << "Failed OCTET STRING test." << endl;
114 cout << "Failed BIT STRING test." << endl;
121 cout << "Failed OBJECT IDENTIFIER test." << endl;
128 cout << "Failed REAL test." << endl;
136 cout << "There are errors in the primitive type encoding/decoding" << endl;
137 cout << "library for this architecture. Time for gdb..." << endl;
141 cout << "The primitive type encoding/decoding library passed simple tests." << endl;
142 cout << "It should be safe to use..." << endl;
149 * returns true if passes encode/decode tests
160 b.Init (bufData, 256);
161 b.ResetInWriteRvsMode();
163 // write whole range of byte (0..255)
164 // remember, write works in reverse
165 for (i = 0; i < 256; i++)
170 cout << "Error writing to buffer." << endl;
174 // read in values & verify
176 for (i = 255; i >= 0; i--)
177 if (b.GetByte() != i)
179 cout << "Error verifying data written to buffer." << endl;
185 cout << "Error reading from buffer." << endl;
190 /* now make sure errors are detected */
191 b.ResetInWriteRvsMode();
193 for (i = 0; i < 257; i++) // write past end of buffer
198 cout << "Buffers failed to report buffer write overflow." << endl;
204 for (i = 256; i >= 0; i--) // read past end of buffer
209 cout << "Buffers failed to report buffer read overflow." << endl;
214 } /* TestAsnBuffers */
219 * returns true if passes encode/decode tests
240 /* initialize buffer */
241 b.Init (bufData, 256);
243 /* encode a TRUE value and verify */
246 code = INTEGER_TAG_CODE;
247 aTag1 = MAKE_TAG_ID (tagClass, form, code);
249 for (i = 0; i < 2; i++)
251 b.ResetInWriteRvsMode();
252 len1 = BEncTag1 (b, tagClass, form, code);
257 cout << "Error encoding a Tag." << endl;
264 /* make sure no decode errors and that it decodes to same tag */
266 if ((val = setjmp (env)) == 0)
268 aTag2 = BDecTag (b, len2, env);
273 cout << "Error decoding a Tag - error number " << val << endl;
275 if (noErr && ((aTag2 != aTag1) || (len1 != len2)))
278 cout << "Error decoded Tag does not match encoded Tag." << endl;
280 /* set a new test tag value */
283 code = (BER_UNIV_CODE) 29;
284 aTag1 = MAKE_TAG_ID (tagClass, form, code);
291 * returns true if passes encode/decode tests
308 /* initialize buffer */
309 b.Init (bufData, 256);
312 /* encode a TRUE value and verify */
314 for (i = 0; i < 2; i++)
316 b.ResetInWriteRvsMode();
317 len1 = BEncDefLen (b, aLen1);
322 cout << "Error encoding Length." << endl;
329 /* make sure no decode errors and that it decodes to true */
331 if ((val = setjmp (env)) == 0)
333 aLen2 = BDecLen (b, len2, env);
338 cout << "Error decoding Length - error number " << val << endl;
342 if (noErr && ((aLen2 != aLen1) || (len1 != len2)))
345 cout << "Error - decoded length does not match encoded length" << endl;
352 b.ResetInWriteRvsMode();
353 len1 = BEncIndefLen (b);
358 cout << "Error encoding indefinite Length." << endl;
365 /* make sure no decode errors */
367 if ((val = setjmp (env)) == 0)
369 aLen2 = BDecLen (b, len2, env);
374 cout << "Error decoding Length - error number " << val << endl;
378 if (noErr && ((aLen2 != INDEFINITE_LEN) || (len1 != len2)))
381 cout << "Error - decoded length does not match encoded length" << endl;
385 b.ResetInWriteRvsMode();
391 cout << "Error encoding indefinite Length." << endl;
398 /* make sure no decode errors */
400 if ((val = setjmp (env)) == 0)
402 BDecEoc (b, len2, env);
407 cout << "Error decoding Length - error number " << val << endl;
411 if (noErr && (len1 != len2))
414 cout << "Error - decoded EOC length error" << endl;
423 * returns true if passes encode/decode tests
429 char bufData[bufSize];
437 // initialize a small buffer
438 b.Init (bufData, bufSize);
439 b.ResetInWriteRvsMode();
441 // encode a true value and verify
444 if (!aBool1.BEncPdu (b, len1))
447 cout << "Error encoding TRUE BOOLEAN value." << endl;
452 aBool2 = false; // set to opposite of expected value
454 // make sure no decode errors and that it decodes to true
455 if (!aBool2.BDecPdu (b, len2) || !aBool2 || (len1 != len2))
458 cout << "Error decoding TRUE BOOLEAN value." << endl;
461 // now encode a false value and verify
462 b.ResetInWriteRvsMode();
465 if (!aBool1.BEncPdu (b, len1))
468 cout << "Error encoding FALSE BOOLEAN value." << endl;
473 aBool2 = true; // set to opposite of expected value
475 // make sure no decode errors and that it decodes to false
476 if (!aBool2.BDecPdu (b, len2) || aBool2 || (len1 != len2))
479 cout << "Error decoding FALSE BOOLEAN value." << endl;
487 * returns true if passes encode/decode tests
493 char bufData[bufSize];
501 // initialize a small buffer
502 b.Init (bufData, bufSize);
505 // Encode a range of integers: negative & positive in
506 // the 1 to sizeof (long int) range
510 for (j = 0; j < 2; j++)
512 for (i = 0; i < sizeof (long int); i++)
514 b.ResetInWriteRvsMode();
516 a1 = sign * (17 << (i * 8)); // 17 is a random choice
517 if (!a1.BEncPdu (b, len1))
520 cout << "Error encoding INTEGER value " << a1 << "." << endl;
526 // make sure no decode errors and that it decodes to the correc val
527 if (!a2.BDecPdu (b, len2) || (a2 != a1) || (len1 != len2))
530 cout << "Error decoding INTEGER value " << a1 << "." << endl;
542 * returns true if passes encode/decode tests
548 char bufData[bufSize];
556 // initialize a small buffer
557 b.Init (bufData, bufSize);
560 for (j = 0; j < 2; j++)
562 b.ResetInWriteRvsMode();
564 if (!a1.BEncPdu (b, len1))
567 cout << "Error encoding OCTET STRING value " << a1 << "." << endl;
572 // make sure no decode errors and that it decodes to the correc val
573 if (!a2.BDecPdu (b, len2) || (a2 != a1) || (len1 != len2))
576 cout << "Error decoding OCTET STRING value " << a1 << "." << endl;
578 a1 = ""; // try an empty string
588 * returns true if passes encode/decode tests
594 char bufData[bufSize];
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 };
606 // initialize a small buffer
607 b.Init (bufData, bufSize);
611 for (i = 0; i < 32; i++)
620 b.ResetInWriteRvsMode();
621 if (!a1.BEncPdu (b, len1))
624 cout << "Error encoding BIT STRING value " << a1 << "." << endl;
629 // make sure no decode errors and that it decodes to the correc val
630 if (!a2.BDecPdu (b, len2) || (a2 != a1) || (len1 != len2))
633 cout << "Error decoding BIT STRING value " << a1 << "." << endl;
644 * returns true if passes encode/decode tests
650 char bufData[bufSize];
651 AsnOid a1 (0,1,2,3,4,5,6);
653 AsnOid a3 (2,38,29,40,200,10,4000);
659 // initialize a small buffer
660 b.Init (bufData, bufSize);
662 for (i = 0; i < 2; i++)
664 b.ResetInWriteRvsMode();
666 if (!a1.BEncPdu (b, len1))
669 cout << "Error encoding OBJECT IDENTIFIER value " << a1 << "." << endl;
674 // make sure no decode errors and that it decodes to the correc val
675 if (!a2.BDecPdu (b, len2) || (a2 != a1) || (len1 != len2))
678 cout << "Error decoding OBJECT IDENTIFIER value " << a1 << "." << endl;
688 * returns true if passes encode/decode tests
690 * NOT USED - nuked template design.
697 char bufData[bufSize];
698 AsnList<AsnInt> intList1;
699 AsnList<AsnInt> intList2;
700 AsnList<AsnBool> boolList1;
701 AsnList<AsnBool> boolList2;
707 b.Init (bufData, bufSize);
709 b.ResetInWriteRvsMode();
711 if (!intList1.BEncPdu (b, len1))
714 cout << "Error encoding SEQUENCE OF value " << intList1 << "." << endl;
719 if (!intList2.BDecPdu (b, len2) || (len1 != len2))
722 cout << "Error decoding SEQUENCE OF value " << intList1 << "." << endl;
724 cout << "intlist 1 = " << intList1 << endl;
725 cout << "intlist 2 = " << intList1 << endl;
728 if (!boolList1.BEncPdu (b, len1))
731 cout << "Error encoding SEQUENCE OF value " << boolList1 << "." << endl;
736 if (!boolList2.BDecPdu (b, len2) || (len1 != len2))
739 cout << "Error decoding SEQUENCE OF value " << boolList1 << "." << endl;
741 cout << "boolList 1 = " << boolList1 << endl;
742 cout << "boolList 2 = " << boolList1 << endl;
751 * returns true if passes encode/decode tests
757 /* we don't seem to have any of this stuff */
761 char bufData[bufSize];
763 AsnReal a[] = { 0.0, 0.8, -22.484848, PLUS_INFINITY, MINUS_INFINITY};
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)
776 if (!isinf ((double)PLUS_INFINITY)) || !isinf ((double)MINUS_INFINITY))
779 if (finite ((double)PLUS_INFINITY) || finite ((double)MINUS_INFINITY))
781 #error "oops: you've got neither isinf(3) nor finite(3)?!"
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;
790 // initialize a small buffer
791 b.Init (bufData, bufSize);
793 for (i = 0; i < 5; i++)
795 b.ResetInWriteRvsMode();
797 if (!a[i].BEncPdu (b, len1))
800 cout << "Error encoding REAL value " << a[i] << "." << endl;
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))
809 cout << "Error decoding REAL value " << a[i] << "." << endl;