]>
git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c-examples/test-lib/test-lib.c
e334ffd70867a415c4763699a1a77c8390996354
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.
20 * c_examples/test_lib/test_lib.c
22 * uses SBufs for buffers
26 * $Header: /cvs/root/Security/SecuritySNACCRuntime/c-examples/test-lib/Attic/test-lib.c,v 1.1.1.1 2001/05/18 23:14:07 mb Exp $
27 * $Log: test-lib.c,v $
28 * Revision 1.1.1.1 2001/05/18 23:14:07 mb
29 * Move from private repository to open source repository
31 * Revision 1.2 2001/05/05 00:59:20 rmurphy
32 * Adding darwin license headers
34 * Revision 1.1.1.1 1999/03/16 18:06:09 aram
35 * Originals from SMIME Free Library.
37 * Revision 1.5 1995/07/24 20:50:34 rj
38 * ``#error "..."'' instead of ``#error ...''.
40 * changed `_' to `-' in file names.
42 * Revision 1.4 1995/02/18 16:17:44 rj
43 * utilize either isinf(3) or finite(3), whatever happens to be present.
45 * Revision 1.3 1994/08/31 23:48:45 rj
46 * more portable .h file inclusion.
48 * Revision 1.2 1994/08/31 08:59:39 rj
49 * first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
75 /* set up the PLUS and MINUS INFINITY globals */
78 /* needed for OCTET STRING, BIT STRING and OBJECT IDENTIFIER decoding */
79 InitNibbleMem (256, 256);
81 if (!TestAsnBuffers())
83 fprintf (stdout
, "Failed buffer tests, no point in proceeding ... bye!\n");
90 fprintf (stdout
, "Failed Tag test.\n" );
96 fprintf (stdout
, "Failed Length test.\n" );
102 fprintf (stdout
, "Failed BOOLEAN test.\n" );
109 fprintf (stdout
, "Failed INTEGER test.\n" );
115 fprintf (stdout
, "Failed OCTET STRING test.\n" );
122 fprintf (stdout
, "Failed BIT STRING test.\n" );
129 fprintf (stdout
, "Failed OBJECT IDENTIFIER test.\n" );
136 fprintf (stdout
, "Failed REAL test.\n" );
144 fprintf (stdout
, "There are errors in the primitive type encoding/decoding\n" );
145 fprintf (stdout
, "library for this architecture. Time for gdb...\n" );
149 fprintf (stdout
, "The primitive type encoding/decoding library passed simple tests.\n");
150 fprintf (stdout
, "It should be safe to use...\n" );
158 * returns TRUE if passes encode/decode tests
168 /* initialize buffer */
169 SBufInit (&b
, bufData
, 256);
170 SBufResetInWriteRvsMode (&b
);
173 * write whole range of byte (0..255)
174 * remember, write works in reverse
176 for (i
= 0; i
< 256; i
++)
177 BufPutByteRvs (&b
,i
);
179 if (BufWriteError (&b
))
181 fprintf (stdout
, "Error writing to buffer.\n" );
185 /* read in values & verify */
186 SBufResetInReadMode (&b
);
187 for (i
= 255; i
>= 0; i
--)
188 if (BufGetByte (&b
) != i
)
190 fprintf (stdout
, "Error verifying data written to buffer.\n" );
194 if (BufReadError (&b
))
196 fprintf (stdout
, "Error reading from buffer.\n" );
201 /* now make sure errors are detected */
202 SBufResetInWriteRvsMode (&b
);
204 for (i
= 0; i
< 257; i
++) /* write past end of buffer */
205 BufPutByteRvs (&b
,0);
207 if (!BufWriteError (&b
))
209 fprintf (stdout
, "Buffers failed to report buffer write overflow.\n" );
214 SBufResetInReadMode (&b
);
215 for (i
= 256; i
>= 0; i
--) /* read past end of buffer */
218 if (!BufReadError (&b
))
220 fprintf (stdout
, "Buffers failed to report buffer read overflow.\n" );
225 } /* TestAsnBuffers */
230 * returns TRUE if passes encode/decode tests
251 /* initialize buffer */
252 SBufInit (&b
, bufData
, 256);
255 /* encode a true value and verify */
258 code
= INTEGER_TAG_CODE
;
259 aTag1
= MAKE_TAG_ID (class, form
, code
);
261 for (i
= 0; i
< 2; i
++)
263 SBufResetInWriteRvsMode (&b
);
264 len1
= BEncTag1 (&b
, class, form
, code
);
266 if (BufWriteError (&b
))
269 fprintf (stdout
, "Error encoding a Tag.\n" );
272 SBufResetInReadMode (&b
);
276 /* make sure no decode errors and that it decodes to same tag */
278 if ((val
= setjmp (env
)) == 0)
280 aTag2
= BDecTag (&b
, &len2
, env
);
285 fprintf (stdout
, "Error decoding a Tag - error number %d\n", val
);
287 if (noErr
&& ((aTag2
!= aTag1
) || (len1
!= len2
)))
290 fprintf (stdout
, "Error decoded Tag does not match encoded Tag.\n" );
292 /* set a new test tag value */
296 aTag1
= MAKE_TAG_ID (class, form
, code
);
303 * returns TRUE if passes encode/decode tests
320 /* initialize buffer */
321 SBufInit (&b
, bufData
, 256);
324 /* encode a true value and verify */
326 for (i
= 0; i
< 2; i
++)
328 SBufResetInWriteRvsMode (&b
);
329 len1
= BEncDefLen (&b
, aLen1
);
331 if (BufWriteError (&b
))
334 fprintf (stdout
, "Error encoding Length.\n" );
337 SBufResetInReadMode (&b
);
341 /* make sure no decode errors and that it decodes to true */
343 if ((val
= setjmp (env
)) == 0)
345 aLen2
= BDecLen (&b
, &len2
, env
);
350 fprintf (stdout
, "Error decoding Length - error number %d\n", val
);
354 if (noErr
&& ((aLen2
!= aLen1
) || (len1
!= len2
)))
357 fprintf (stdout
, "Error - decoded lenght does not match encoded length\n");
364 SBufResetInWriteRvsMode (&b
);
365 len1
= BEncIndefLen (&b
);
367 if (BufWriteError (&b
))
370 fprintf (stdout
, "Error encoding indefinite Length.\n" );
373 SBufResetInReadMode (&b
);
377 /* make sure no decode errors */
379 if ((val
= setjmp (env
)) == 0)
381 aLen2
= BDecLen (&b
, &len2
, env
);
386 fprintf (stdout
, "Error decoding Length - error number %d\n", val
);
390 if (noErr
&& ((aLen2
!= INDEFINITE_LEN
) || (len1
!= len2
)))
393 fprintf (stdout
, "Error - decoded length does not match encoded length\n");
397 SBufResetInWriteRvsMode (&b
);
400 if (BufWriteError (&b
))
403 fprintf (stdout
, "Error encoding indefinite Length.\n" );
406 SBufResetInReadMode (&b
);
410 /* make sure no decode errors */
412 if ((val
= setjmp (env
)) == 0)
414 BDecEoc (&b
, &len2
, env
);
419 fprintf (stdout
, "Error decoding Length - error number %d\n", val
);
423 if (noErr
&& (len1
!= len2
))
426 fprintf (stdout
, "Error - decoded EOC length error.\n");
434 * returns TRUE if passes encode/decode tests
451 /* initialize buffer */
452 SBufInit (&b
, bufData
, 256);
453 SBufResetInWriteRvsMode (&b
);
455 /* encode a true value and verify */
457 len1
= BEncAsnBoolContent (&b
, &aBool1
);
459 if (BufWriteError (&b
))
462 fprintf (stdout
, "Error encoding TRUE BOOLEAN value.\n" );
465 SBufResetInReadMode (&b
);
467 aBool2
= FALSE
; /* set to opposite of expected value */
469 /* make sure no decode errors and that it decodes to true */
471 if ((val
= setjmp (env
)) == 0)
473 BDecAsnBoolContent (&b
, tag
, len1
, &aBool2
, &len2
, env
);
478 fprintf (stdout
, "Error decoding a BOOLEAN - error number %d\n", val
);
482 if (noErr
&& ((aBool2
!= aBool1
) || (len1
!= len2
)))
485 fprintf (stdout
, "Error decoding TRUE BOOLEAN value.\n" );
488 /* now encode a false value and verify */
489 SBufResetInWriteRvsMode (&b
);
492 len1
= BEncAsnBoolContent (&b
, &aBool1
);
493 if (BufWriteError (&b
))
496 fprintf (stdout
, "Error encoding FALSE BOOLEAN value.\n" );
499 SBufResetInReadMode (&b
);
501 aBool2
= TRUE
; /* set to opposite of expected value */
503 /* make sure no decode errors and that it decodes to true */
505 if ((val
= setjmp (env
)) == 0)
507 BDecAsnBoolContent (&b
, tag
, len1
, &aBool2
, &len2
, env
);
512 fprintf (stdout
, "Error decoding a BOOLEAN - error number %d\n", val
);
516 if (noErr
&& ((aBool2
!= aBool1
) || (len1
!= len2
)))
519 fprintf (stdout
, "Error decoding TRUE BOOLEAN value.\n" );
522 /* make sure no decode errors and that it decodes to false */
530 * returns TRUE if passes encode/decode tests
548 /* initialize buffer */
549 SBufInit (&b
, bufData
, 256);
552 * Encode a range of integers: negative & positive in
553 * the 1 to sizeof (AsnInt) range
556 for (j
= 0; j
< 2; j
++)
558 for (i
= 0; i
< sizeof (AsnInt
); i
++)
560 SBufResetInWriteRvsMode (&b
);
562 a1
= sign
* (17 << (i
* 8)); /* 17 is a random choice :) */
563 len1
= BEncAsnIntContent (&b
, &a1
);
564 if (BufWriteError (&b
))
567 fprintf (stdout
, "Error encoding INTEGER value %d.\n", a1
);
570 SBufResetInReadMode (&b
);
572 /* make sure no decode errors and that it decodes to true */
574 if ((val
= setjmp (env
)) == 0)
576 BDecAsnIntContent (&b
, tag
, len1
, &a2
, &len2
, env
);
581 fprintf (stdout
, "Error decoding a INTEGER - error number %d\n", val
);
584 if (noErr
&& ((a2
!= a1
) || (len1
!= len2
)))
587 fprintf (stdout
, "Error decoding INTEGER value %d.\n", a1
);
599 * returns TRUE if passes encode/decode tests
616 /* initialize buffer */
617 SBufInit (&b
, bufData
, 256);
619 a1
.octs
= "Hello Gumby";
620 a1
.octetLen
= strlen (a1
.octs
);
623 * octet string decoder needs to know tag form
624 * (snacc always encodes octet strings as primitives)
626 tag
= MAKE_TAG_ID (UNIV
, PRIM
, OCTETSTRING_TAG_CODE
);
628 for (j
= 0; j
< 2; j
++)
630 SBufResetInWriteRvsMode (&b
);
632 len1
= BEncAsnOctsContent (&b
, &a1
);
633 if (BufWriteError (&b
))
636 fprintf (stdout
, "Error encoding OCTET STRING value \"%s\".\n", a1
.octs
);
638 SBufResetInReadMode (&b
);
640 /* make sure no decode errors and that it decodes to true */
642 if ((val
= setjmp (env
)) == 0)
644 BDecAsnOctsContent (&b
, tag
, len1
, &a2
, &len2
, env
);
649 fprintf (stdout
, "Error decoding an OCTET STRING - error number %d\n", val
);
652 if (noErr
&& (!AsnOctsEquiv (&a2
,&a1
) || (len1
!= len2
)))
655 fprintf (stdout
, "Error decoding OCTET STRING value %s.\n", a1
.octs
);
657 a1
.octs
= ""; /* test empty string */
658 a1
.octetLen
= strlen (a1
.octs
);
669 * returns TRUE if passes encode/decode tests
688 * init bitsToSet - old compilers don't support automatic init
689 * of aggregate types.
727 /* initialize buffer */
728 SBufInit (&b
, bufData
, 256);
730 /* initialize bit string */
731 a1
.bits
= Asn1Alloc (5);
733 for (i
= 0; i
< 35; i
++)
742 * bit string decoder needs to know tag form
743 * (snacc always encodes bit strings as primitives)
745 tag
= MAKE_TAG_ID (UNIV
, PRIM
, BITSTRING_TAG_CODE
);
747 SBufResetInWriteRvsMode (&b
);
749 len1
= BEncAsnBitsContent (&b
, &a1
);
750 if (BufWriteError (&b
))
753 fprintf (stdout
, "Error encoding BIT STRING value ");
754 PrintAsnBits (stdout
, &a1
, 0);
755 fprintf (stdout
, "\n");
757 SBufResetInReadMode (&b
);
759 /* make sure no decode errors and that it decodes to true */
761 if ((val
= setjmp (env
)) == 0)
763 BDecAsnBitsContent (&b
, tag
, len1
, &a2
, &len2
, env
);
768 fprintf (stdout
, "Error decoding an BIT STRING - error number %d\n", val
);
771 if (noErr
&& (!AsnBitsEquiv (&a2
,&a1
) || (len1
!= len2
)))
774 fprintf (stdout
, "Error decoding BIT STRING value ");
775 PrintAsnBits (stdout
, &a1
, 0);
776 fprintf (stdout
, "\n");
785 * returns TRUE if passes encode/decode tests
802 /* initialize buffer */
803 SBufInit (&b
, bufData
, 256);
805 /* mib-2 oid { iso 3 6 1 2 1 }*/
807 a1
.octs
= "\53\6\1\2\1";
810 for (j
= 0; j
< 2; j
++)
812 SBufResetInWriteRvsMode (&b
);
814 len1
= BEncAsnOidContent (&b
, &a1
);
815 if (BufWriteError (&b
))
818 fprintf (stdout
, "Error encoding OCTET STRING value \"%s\".\n", a1
.octs
);
820 SBufResetInReadMode (&b
);
822 /* make sure no decode errors and that it decodes to true */
824 if ((val
= setjmp (env
)) == 0)
826 BDecAsnOidContent (&b
, tag
, len1
, &a2
, &len2
, env
);
831 fprintf (stdout
, "Error decoding an OCTET STRING - error number %d\n", val
);
834 if (noErr
&& (!AsnOidsEquiv (&a2
,&a1
) || (len1
!= len2
)))
837 fprintf (stdout
, "Error decoding OCTET STRING value %s.\n", a1
.octs
);
839 /* system { mib-2 1 }*/
840 a1
.octs
= "\53\6\1\2\1\1";
849 * returns TRUE if passes encode/decode tests
872 * if you do not have the ieee_functions in your math lib,
873 * this will not link. Comment it out and cross you fingers.
874 * (or check/set the +/-infinity values for you architecture)
877 if (!isinf (PLUS_INFINITY
) || !isinf (MINUS_INFINITY
))
880 if (finite (PLUS_INFINITY
) || finite (MINUS_INFINITY
))
882 #error "oops: you've got neither isinf(3) nor finite(3)?!"
886 fprintf (stdout
, "WARNING: PLUS_INFINITY and MINUS_INFINITY in asn_real.c are not\n");
887 fprintf (stdout
, "correct for this architecture. Modify the InitAsnInfinity() Routine.\n");
891 * init test value array.
892 * some old compilers don't support automatic init of aggregate types
894 * AsnReal a1[] = { 0.0, 0.8, -22.484848, PLUS_INFINITY, MINUS_INFINITY};
899 a1
[3] = PLUS_INFINITY
;
900 a1
[4] = MINUS_INFINITY
;
902 /* initialize buffer */
903 SBufInit (&b
, bufData
, 256);
906 * Encode a range of integers: negative & positive in
907 * the 1 to sizeof (AsnInt) range
909 for (i
= 0; i
< 5; i
++)
912 SBufResetInWriteRvsMode (&b
);
914 len1
= BEncAsnRealContent (&b
, &a1
[i
]);
915 if (BufWriteError (&b
))
918 fprintf (stdout
, "Error encoding REAL value ");
919 PrintAsnReal (stdout
,&a1
[i
],0);
920 fprintf (stdout
, ".\n");
923 SBufResetInReadMode (&b
);
925 /* make sure no decode errors and that it decodes to true */
927 if ((val
= setjmp (env
)) == 0)
929 BDecAsnRealContent (&b
, tag
, len1
, &a2
, &len2
, env
);
934 fprintf (stdout
, "Error decoding a REAL - error number %d\n", val
);
937 /* testing reals for equality is sketchy */
938 if (!elmtErr
&& ((a2
!= a1
[i
]) || (len1
!= len2
)))
942 fprintf (stdout
, "Error decoding REAL value ");
943 PrintAsnReal (stdout
, &a1
[i
], 0);
944 fprintf (stdout
, ".\n");
946 if (len1
== len2
) /* therefore a2 != a1[i] */
948 fprintf (stdout
, "The value decoded was ");
949 PrintAsnReal (stdout
, &a2
, 0);
950 fprintf (stdout
, ".\n");
953 fprintf (stdout
, "The encoded and decoded length disagree.\n");