]> git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c-examples/test-lib/test-lib.c
e334ffd70867a415c4763699a1a77c8390996354
[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 /*
20 * c_examples/test_lib/test_lib.c
21 *
22 * uses SBufs for buffers
23 *
24 * MS 92
25 *
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
30 *
31 * Revision 1.2 2001/05/05 00:59:20 rmurphy
32 * Adding darwin license headers
33 *
34 * Revision 1.1.1.1 1999/03/16 18:06:09 aram
35 * Originals from SMIME Free Library.
36 *
37 * Revision 1.5 1995/07/24 20:50:34 rj
38 * ``#error "..."'' instead of ``#error ...''.
39 *
40 * changed `_' to `-' in file names.
41 *
42 * Revision 1.4 1995/02/18 16:17:44 rj
43 * utilize either isinf(3) or finite(3), whatever happens to be present.
44 *
45 * Revision 1.3 1994/08/31 23:48:45 rj
46 * more portable .h file inclusion.
47 *
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.
50 *
51 */
52
53 #include <stdio.h>
54
55 #include "asn-incl.h"
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 int bufSize = 256;
69
70 int
71 main()
72 {
73 int isErr = FALSE;
74
75 /* set up the PLUS and MINUS INFINITY globals */
76 InitAsnInfinity();
77
78 /* needed for OCTET STRING, BIT STRING and OBJECT IDENTIFIER decoding */
79 InitNibbleMem (256, 256);
80
81 if (!TestAsnBuffers())
82 {
83 fprintf (stdout, "Failed buffer tests, no point in proceeding ... bye!\n");
84 return 1;
85 }
86
87
88 if (!TestAsnTag())
89 {
90 fprintf (stdout, "Failed Tag test.\n" );
91 isErr = TRUE;
92 }
93
94 if (!TestAsnLen())
95 {
96 fprintf (stdout, "Failed Length test.\n" );
97 isErr = TRUE;
98 }
99
100 if (!TestAsnBool())
101 {
102 fprintf (stdout, "Failed BOOLEAN test.\n" );
103 isErr = TRUE;
104 }
105
106
107 if (!TestAsnInt())
108 {
109 fprintf (stdout, "Failed INTEGER test.\n" );
110 isErr = TRUE;
111 }
112
113 if (!TestAsnOcts())
114 {
115 fprintf (stdout, "Failed OCTET STRING test.\n" );
116 isErr = TRUE;
117 }
118
119
120 if (!TestAsnBits())
121 {
122 fprintf (stdout, "Failed BIT STRING test.\n" );
123 isErr = TRUE;
124 }
125
126
127 if (!TestAsnOid())
128 {
129 fprintf (stdout, "Failed OBJECT IDENTIFIER test.\n" );
130 isErr = TRUE;
131 }
132
133
134 if (!TestAsnReal())
135 {
136 fprintf (stdout, "Failed REAL test.\n" );
137 isErr = TRUE;
138 }
139
140
141
142 if (isErr)
143 {
144 fprintf (stdout, "There are errors in the primitive type encoding/decoding\n" );
145 fprintf (stdout, "library for this architecture. Time for gdb...\n" );
146 }
147 else
148 {
149 fprintf (stdout, "The primitive type encoding/decoding library passed simple tests.\n");
150 fprintf (stdout, "It should be safe to use...\n" );
151 }
152
153 return isErr;
154 }
155
156
157 /*
158 * returns TRUE if passes encode/decode tests
159 */
160 int
161 TestAsnBuffers()
162 {
163 int i,j;
164 int noErr = TRUE;
165 SBuf b;
166 char bufData[256];
167
168 /* initialize buffer */
169 SBufInit (&b, bufData, 256);
170 SBufResetInWriteRvsMode (&b);
171
172 /*
173 * write whole range of byte (0..255)
174 * remember, write works in reverse
175 */
176 for (i = 0; i < 256; i++)
177 BufPutByteRvs (&b,i);
178
179 if (BufWriteError (&b))
180 {
181 fprintf (stdout, "Error writing to buffer.\n" );
182 noErr = FALSE;
183 }
184
185 /* read in values & verify */
186 SBufResetInReadMode (&b);
187 for (i = 255; i >= 0; i--)
188 if (BufGetByte (&b) != i)
189 {
190 fprintf (stdout, "Error verifying data written to buffer.\n" );
191 noErr = FALSE;
192 }
193
194 if (BufReadError (&b))
195 {
196 fprintf (stdout, "Error reading from buffer.\n" );
197 noErr = FALSE;
198 }
199
200
201 /* now make sure errors are detected */
202 SBufResetInWriteRvsMode (&b);
203
204 for (i = 0; i < 257; i++) /* write past end of buffer */
205 BufPutByteRvs (&b,0);
206
207 if (!BufWriteError (&b))
208 {
209 fprintf (stdout, "Buffers failed to report buffer write overflow.\n" );
210 noErr = FALSE;
211 }
212
213
214 SBufResetInReadMode (&b);
215 for (i = 256; i >= 0; i--) /* read past end of buffer */
216 BufGetByte (&b);
217
218 if (!BufReadError (&b))
219 {
220 fprintf (stdout, "Buffers failed to report buffer read overflow.\n" );
221 noErr = FALSE;
222 }
223
224 return noErr;
225 } /* TestAsnBuffers */
226
227
228
229 /*
230 * returns TRUE if passes encode/decode tests
231 */
232 int
233 TestAsnTag()
234 {
235 AsnTag aTag1;
236 AsnTag aTag2;
237 int i, j;
238 AsnLen len1;
239 AsnLen len2;
240 AsnTag tag;
241 int noErr = TRUE;
242 ENV_TYPE env;
243 SBuf b;
244 char bufData[256];
245 long int val;
246 BER_CLASS class;
247 BER_FORM form;
248 BER_UNIV_CODE code;
249
250
251 /* initialize buffer */
252 SBufInit (&b, bufData, 256);
253
254
255 /* encode a true value and verify */
256 class = UNIV;
257 form = PRIM;
258 code = INTEGER_TAG_CODE;
259 aTag1 = MAKE_TAG_ID (class, form, code);
260
261 for (i = 0; i < 2; i++)
262 {
263 SBufResetInWriteRvsMode (&b);
264 len1 = BEncTag1 (&b, class, form, code);
265
266 if (BufWriteError (&b))
267 {
268 noErr = FALSE;
269 fprintf (stdout, "Error encoding a Tag.\n" );
270 }
271
272 SBufResetInReadMode (&b);
273
274 aTag2 = 0;
275
276 /* make sure no decode errors and that it decodes to same tag */
277 len2 = 0;
278 if ((val = setjmp (env)) == 0)
279 {
280 aTag2 = BDecTag (&b, &len2, env);
281 }
282 else
283 {
284 noErr = FALSE;
285 fprintf (stdout, "Error decoding a Tag - error number %d\n", val);
286 }
287 if (noErr && ((aTag2 != aTag1) || (len1 != len2)))
288 {
289 noErr = FALSE;
290 fprintf (stdout, "Error decoded Tag does not match encoded Tag.\n" );
291 }
292 /* set a new test tag value */
293 class = CNTX;
294 form = CONS;
295 code = 29;
296 aTag1 = MAKE_TAG_ID (class, form, code);
297 }
298 return noErr;
299 } /* TestAsnTag */
300
301
302 /*
303 * returns TRUE if passes encode/decode tests
304 */
305 int
306 TestAsnLen()
307 {
308 AsnLen aLen1;
309 AsnLen aLen2;
310 int i,j;
311 AsnLen len1;
312 AsnLen len2;
313 AsnTag tag;
314 int noErr = TRUE;
315 ENV_TYPE env;
316 SBuf b;
317 char bufData[256];
318 long int val;
319
320 /* initialize buffer */
321 SBufInit (&b, bufData, 256);
322
323
324 /* encode a true value and verify */
325 aLen1 = 99999;
326 for (i = 0; i < 2; i++)
327 {
328 SBufResetInWriteRvsMode (&b);
329 len1 = BEncDefLen (&b, aLen1);
330
331 if (BufWriteError (&b))
332 {
333 noErr = FALSE;
334 fprintf (stdout, "Error encoding Length.\n" );
335 }
336
337 SBufResetInReadMode (&b);
338
339 aLen2 = 0;
340
341 /* make sure no decode errors and that it decodes to true */
342 len2 = 0;
343 if ((val = setjmp (env)) == 0)
344 {
345 aLen2 = BDecLen (&b, &len2, env);
346 }
347 else
348 {
349 noErr = FALSE;
350 fprintf (stdout, "Error decoding Length - error number %d\n", val);
351 }
352
353
354 if (noErr && ((aLen2 != aLen1) || (len1 != len2)))
355 {
356 noErr = FALSE;
357 fprintf (stdout, "Error - decoded lenght does not match encoded length\n");
358 }
359 aLen1 = 2;
360 }
361
362
363 /* test indef len */
364 SBufResetInWriteRvsMode (&b);
365 len1 = BEncIndefLen (&b);
366
367 if (BufWriteError (&b))
368 {
369 noErr = FALSE;
370 fprintf (stdout, "Error encoding indefinite Length.\n" );
371 }
372
373 SBufResetInReadMode (&b);
374
375 aLen2 = 0;
376
377 /* make sure no decode errors */
378 len2 = 0;
379 if ((val = setjmp (env)) == 0)
380 {
381 aLen2 = BDecLen (&b, &len2, env);
382 }
383 else
384 {
385 noErr = FALSE;
386 fprintf (stdout, "Error decoding Length - error number %d\n", val);
387 }
388
389
390 if (noErr && ((aLen2 != INDEFINITE_LEN) || (len1 != len2)))
391 {
392 noErr = FALSE;
393 fprintf (stdout, "Error - decoded length does not match encoded length\n");
394 }
395
396 /* test EOC */
397 SBufResetInWriteRvsMode (&b);
398 len1 = BEncEoc (&b);
399
400 if (BufWriteError (&b))
401 {
402 noErr = FALSE;
403 fprintf (stdout, "Error encoding indefinite Length.\n" );
404 }
405
406 SBufResetInReadMode (&b);
407
408 aLen2 = 0;
409
410 /* make sure no decode errors */
411 len2 = 0;
412 if ((val = setjmp (env)) == 0)
413 {
414 BDecEoc (&b, &len2, env);
415 }
416 else
417 {
418 noErr = FALSE;
419 fprintf (stdout, "Error decoding Length - error number %d\n", val);
420 }
421
422
423 if (noErr && (len1 != len2))
424 {
425 noErr = FALSE;
426 fprintf (stdout, "Error - decoded EOC length error.\n");
427 }
428
429 return noErr;
430 } /* TestAsnLen */
431
432
433 /*
434 * returns TRUE if passes encode/decode tests
435 */
436 int
437 TestAsnBool()
438 {
439 AsnBool aBool1;
440 AsnBool aBool2;
441 int j;
442 AsnLen len1;
443 AsnLen len2;
444 AsnTag tag;
445 int noErr = TRUE;
446 ENV_TYPE env;
447 SBuf b;
448 char bufData[256];
449 long int val;
450
451 /* initialize buffer */
452 SBufInit (&b, bufData, 256);
453 SBufResetInWriteRvsMode (&b);
454
455 /* encode a true value and verify */
456 aBool1 = TRUE;
457 len1 = BEncAsnBoolContent (&b, &aBool1);
458
459 if (BufWriteError (&b))
460 {
461 noErr = FALSE;
462 fprintf (stdout, "Error encoding TRUE BOOLEAN value.\n" );
463 }
464
465 SBufResetInReadMode (&b);
466
467 aBool2 = FALSE; /* set to opposite of expected value */
468
469 /* make sure no decode errors and that it decodes to true */
470 len2 = 0;
471 if ((val = setjmp (env)) == 0)
472 {
473 BDecAsnBoolContent (&b, tag, len1, &aBool2, &len2, env);
474 }
475 else
476 {
477 noErr = FALSE;
478 fprintf (stdout, "Error decoding a BOOLEAN - error number %d\n", val);
479 }
480
481
482 if (noErr && ((aBool2 != aBool1) || (len1 != len2)))
483 {
484 noErr = FALSE;
485 fprintf (stdout, "Error decoding TRUE BOOLEAN value.\n" );
486 }
487
488 /* now encode a false value and verify */
489 SBufResetInWriteRvsMode (&b);
490 aBool1 = FALSE;
491
492 len1 = BEncAsnBoolContent (&b, &aBool1);
493 if (BufWriteError (&b))
494 {
495 noErr = FALSE;
496 fprintf (stdout, "Error encoding FALSE BOOLEAN value.\n" );
497 }
498
499 SBufResetInReadMode (&b);
500
501 aBool2 = TRUE; /* set to opposite of expected value */
502
503 /* make sure no decode errors and that it decodes to true */
504 len2 = 0;
505 if ((val = setjmp (env)) == 0)
506 {
507 BDecAsnBoolContent (&b, tag, len1, &aBool2, &len2, env);
508 }
509 else
510 {
511 noErr = FALSE;
512 fprintf (stdout, "Error decoding a BOOLEAN - error number %d\n", val);
513 }
514
515
516 if (noErr && ((aBool2 != aBool1) || (len1 != len2)))
517 {
518 noErr = FALSE;
519 fprintf (stdout, "Error decoding TRUE BOOLEAN value.\n" );
520 }
521
522 /* make sure no decode errors and that it decodes to false */
523
524 return noErr;
525 } /* TestAsnBool */
526
527
528
529 /*
530 * returns TRUE if passes encode/decode tests
531 */
532 int
533 TestAsnInt()
534 {
535 AsnInt a1;
536 AsnInt a2;
537 int i,j;
538 AsnLen len1;
539 AsnLen len2;
540 AsnTag tag;
541 int noErr = TRUE;
542 ENV_TYPE env;
543 SBuf b;
544 char bufData[256];
545 long int val;
546 int sign;
547
548 /* initialize buffer */
549 SBufInit (&b, bufData, 256);
550
551 /*
552 * Encode a range of integers: negative & positive in
553 * the 1 to sizeof (AsnInt) range
554 */
555 sign = 1;
556 for (j = 0; j < 2; j++)
557 {
558 for (i = 0; i < sizeof (AsnInt); i++)
559 {
560 SBufResetInWriteRvsMode (&b);
561
562 a1 = sign * (17 << (i * 8)); /* 17 is a random choice :) */
563 len1 = BEncAsnIntContent (&b, &a1);
564 if (BufWriteError (&b))
565 {
566 noErr = FALSE;
567 fprintf (stdout, "Error encoding INTEGER value %d.\n", a1 );
568 }
569
570 SBufResetInReadMode (&b);
571
572 /* make sure no decode errors and that it decodes to true */
573 len2 = 0;
574 if ((val = setjmp (env)) == 0)
575 {
576 BDecAsnIntContent (&b, tag, len1, &a2, &len2, env);
577 }
578 else
579 {
580 noErr = FALSE;
581 fprintf (stdout, "Error decoding a INTEGER - error number %d\n", val);
582 }
583
584 if (noErr && ((a2 != a1) || (len1 != len2)))
585 {
586 noErr = FALSE;
587 fprintf (stdout, "Error decoding INTEGER value %d.\n", a1 );
588 }
589 }
590 sign = -1;
591 }
592
593 return noErr;
594
595 } /* TestAsnInt */
596
597
598 /*
599 * returns TRUE if passes encode/decode tests
600 */
601 int
602 TestAsnOcts()
603 {
604 AsnOcts a1;
605 AsnOcts a2;
606 int i,j;
607 AsnLen len1;
608 AsnLen len2;
609 AsnTag tag;
610 int noErr = TRUE;
611 ENV_TYPE env;
612 SBuf b;
613 char bufData[256];
614 long int val;
615
616 /* initialize buffer */
617 SBufInit (&b, bufData, 256);
618
619 a1.octs = "Hello Gumby";
620 a1.octetLen = strlen (a1.octs);
621
622 /*
623 * octet string decoder needs to know tag form
624 * (snacc always encodes octet strings as primitives)
625 */
626 tag = MAKE_TAG_ID (UNIV, PRIM, OCTETSTRING_TAG_CODE);
627
628 for (j = 0; j < 2; j++)
629 {
630 SBufResetInWriteRvsMode (&b);
631
632 len1 = BEncAsnOctsContent (&b, &a1);
633 if (BufWriteError (&b))
634 {
635 noErr = FALSE;
636 fprintf (stdout, "Error encoding OCTET STRING value \"%s\".\n", a1.octs );
637 }
638 SBufResetInReadMode (&b);
639
640 /* make sure no decode errors and that it decodes to true */
641 len2 = 0;
642 if ((val = setjmp (env)) == 0)
643 {
644 BDecAsnOctsContent (&b, tag, len1, &a2, &len2, env);
645 }
646 else
647 {
648 noErr = FALSE;
649 fprintf (stdout, "Error decoding an OCTET STRING - error number %d\n", val);
650 }
651
652 if (noErr && (!AsnOctsEquiv (&a2,&a1) || (len1 != len2)))
653 {
654 noErr = FALSE;
655 fprintf (stdout, "Error decoding OCTET STRING value %s.\n", a1.octs );
656 }
657 a1.octs = ""; /* test empty string */
658 a1.octetLen = strlen (a1.octs);
659 }
660
661 ResetNibbleMem();
662 return noErr;
663
664 } /* TestAsnOcts */
665
666
667
668 /*
669 * returns TRUE if passes encode/decode tests
670 */
671 int
672 TestAsnBits()
673 {
674 AsnBits a1;
675 AsnBits a2;
676 int i,j;
677 AsnLen len1;
678 AsnLen len2;
679 AsnTag tag;
680 int noErr = TRUE;
681 ENV_TYPE env;
682 SBuf b;
683 char bufData[256];
684 long int val;
685 short bitsToSet[35];
686
687 /*
688 * init bitsToSet - old compilers don't support automatic init
689 * of aggregate types.
690 */
691 bitsToSet[0] = 0;
692 bitsToSet[1] = 1;
693 bitsToSet[2] = 0;
694 bitsToSet[3] = 0;
695 bitsToSet[4] = 1;
696 bitsToSet[5] = 1;
697 bitsToSet[6] = 0;
698 bitsToSet[7] = 1;
699 bitsToSet[8] = 0;
700 bitsToSet[9] = 1;
701 bitsToSet[10] = 0;
702 bitsToSet[11] = 0;
703 bitsToSet[12] = 1;
704 bitsToSet[13] = 1;
705 bitsToSet[14] = 0;
706 bitsToSet[15] = 1;
707 bitsToSet[16] = 0;
708 bitsToSet[17] = 1;
709 bitsToSet[18] = 0;
710 bitsToSet[19] = 0;
711 bitsToSet[20] = 1;
712 bitsToSet[21] = 1;
713 bitsToSet[22] = 0;
714 bitsToSet[23] = 1;
715 bitsToSet[24] = 0;
716 bitsToSet[25] = 1;
717 bitsToSet[26] = 0;
718 bitsToSet[27] = 1;
719 bitsToSet[28] = 1;
720 bitsToSet[29] = 0;
721 bitsToSet[30] = 1;
722 bitsToSet[31] = 1;
723 bitsToSet[32] = 0;
724 bitsToSet[33] = 1;
725 bitsToSet[34] = 0;
726
727 /* initialize buffer */
728 SBufInit (&b, bufData, 256);
729
730 /* initialize bit string */
731 a1.bits = Asn1Alloc (5);
732 a1.bitLen = 35;
733 for (i = 0; i < 35; i++)
734 {
735 if (bitsToSet[i])
736 SetAsnBit (&a1, i);
737 else
738 ClrAsnBit (&a1, i);
739 }
740
741 /*
742 * bit string decoder needs to know tag form
743 * (snacc always encodes bit strings as primitives)
744 */
745 tag = MAKE_TAG_ID (UNIV, PRIM, BITSTRING_TAG_CODE);
746
747 SBufResetInWriteRvsMode (&b);
748
749 len1 = BEncAsnBitsContent (&b, &a1);
750 if (BufWriteError (&b))
751 {
752 noErr = FALSE;
753 fprintf (stdout, "Error encoding BIT STRING value ");
754 PrintAsnBits (stdout, &a1, 0);
755 fprintf (stdout, "\n");
756 }
757 SBufResetInReadMode (&b);
758
759 /* make sure no decode errors and that it decodes to true */
760 len2 = 0;
761 if ((val = setjmp (env)) == 0)
762 {
763 BDecAsnBitsContent (&b, tag, len1, &a2, &len2, env);
764 }
765 else
766 {
767 noErr = FALSE;
768 fprintf (stdout, "Error decoding an BIT STRING - error number %d\n", val);
769 }
770
771 if (noErr && (!AsnBitsEquiv (&a2,&a1) || (len1 != len2)))
772 {
773 noErr = FALSE;
774 fprintf (stdout, "Error decoding BIT STRING value ");
775 PrintAsnBits (stdout, &a1, 0);
776 fprintf (stdout, "\n");
777 }
778 ResetNibbleMem();
779 return noErr;
780
781 } /* TestAsnBits */
782
783
784 /*
785 * returns TRUE if passes encode/decode tests
786 */
787 int
788 TestAsnOid()
789 {
790 AsnOid a1;
791 AsnOid a2;
792 int i,j;
793 AsnLen len1;
794 AsnLen len2;
795 AsnTag tag;
796 int noErr = TRUE;
797 ENV_TYPE env;
798 SBuf b;
799 char bufData[256];
800 long int val;
801
802 /* initialize buffer */
803 SBufInit (&b, bufData, 256);
804
805 /* mib-2 oid { iso 3 6 1 2 1 }*/
806 a1.octetLen = 5;
807 a1.octs = "\53\6\1\2\1";
808
809
810 for (j = 0; j < 2; j++)
811 {
812 SBufResetInWriteRvsMode (&b);
813
814 len1 = BEncAsnOidContent (&b, &a1);
815 if (BufWriteError (&b))
816 {
817 noErr = FALSE;
818 fprintf (stdout, "Error encoding OCTET STRING value \"%s\".\n", a1.octs );
819 }
820 SBufResetInReadMode (&b);
821
822 /* make sure no decode errors and that it decodes to true */
823 len2 = 0;
824 if ((val = setjmp (env)) == 0)
825 {
826 BDecAsnOidContent (&b, tag, len1, &a2, &len2, env);
827 }
828 else
829 {
830 noErr = FALSE;
831 fprintf (stdout, "Error decoding an OCTET STRING - error number %d\n", val);
832 }
833
834 if (noErr && (!AsnOidsEquiv (&a2,&a1) || (len1 != len2)))
835 {
836 noErr = FALSE;
837 fprintf (stdout, "Error decoding OCTET STRING value %s.\n", a1.octs );
838 }
839 /* system { mib-2 1 }*/
840 a1.octs = "\53\6\1\2\1\1";
841 a1.octetLen = 6;
842 }
843 ResetNibbleMem();
844 return noErr;
845
846 } /* TestAsnOid */
847
848 /*
849 * returns TRUE if passes encode/decode tests
850 */
851 int
852 TestAsnReal()
853 {
854 AsnReal a1[5];
855 AsnReal a2;
856 int i,j;
857 AsnLen len1;
858 AsnLen len2;
859 AsnTag tag;
860 int noErr = TRUE;
861 int elmtErr = FALSE;
862 ENV_TYPE env;
863 SBuf b;
864 char bufData[256];
865 long int val;
866 int sign;
867 AsnReal inf;
868 unsigned char *c;
869
870
871 /*
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)
875 */
876 #if HAVE_ISINF
877 if (!isinf (PLUS_INFINITY) || !isinf (MINUS_INFINITY))
878 #else
879 #if HAVE_FINITE
880 if (finite (PLUS_INFINITY) || finite (MINUS_INFINITY))
881 #else
882 #error "oops: you've got neither isinf(3) nor finite(3)?!"
883 #endif
884 #endif
885 {
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");
888 }
889
890 /*
891 * init test value array.
892 * some old compilers don't support automatic init of aggregate types
893 * like:
894 * AsnReal a1[] = { 0.0, 0.8, -22.484848, PLUS_INFINITY, MINUS_INFINITY};
895 */
896 a1[0] = 0.0;
897 a1[1] = 0.8;
898 a1[2] = -22.484848;
899 a1[3] = PLUS_INFINITY;
900 a1[4] = MINUS_INFINITY;
901
902 /* initialize buffer */
903 SBufInit (&b, bufData, 256);
904
905 /*
906 * Encode a range of integers: negative & positive in
907 * the 1 to sizeof (AsnInt) range
908 */
909 for (i = 0; i < 5; i++)
910 {
911 elmtErr = FALSE;
912 SBufResetInWriteRvsMode (&b);
913
914 len1 = BEncAsnRealContent (&b, &a1[i]);
915 if (BufWriteError (&b))
916 {
917 elmtErr = TRUE;
918 fprintf (stdout, "Error encoding REAL value ");
919 PrintAsnReal (stdout,&a1[i],0);
920 fprintf (stdout, ".\n");
921 }
922
923 SBufResetInReadMode (&b);
924
925 /* make sure no decode errors and that it decodes to true */
926 len2 = 0;
927 if ((val = setjmp (env)) == 0)
928 {
929 BDecAsnRealContent (&b, tag, len1, &a2, &len2, env);
930 }
931 else
932 {
933 elmtErr = TRUE;
934 fprintf (stdout, "Error decoding a REAL - error number %d\n", val);
935 }
936
937 /* testing reals for equality is sketchy */
938 if (!elmtErr && ((a2 != a1[i]) || (len1 != len2)))
939 {
940
941 elmtErr = TRUE;
942 fprintf (stdout, "Error decoding REAL value ");
943 PrintAsnReal (stdout, &a1[i], 0);
944 fprintf (stdout, ".\n");
945
946 if (len1 == len2) /* therefore a2 != a1[i] */
947 {
948 fprintf (stdout, "The value decoded was ");
949 PrintAsnReal (stdout, &a2, 0);
950 fprintf (stdout, ".\n");
951 }
952 else
953 fprintf (stdout, "The encoded and decoded length disagree.\n");
954 }
955 if (elmtErr)
956 noErr = FALSE;
957 }
958
959
960 return noErr;
961
962 } /* TestAsnReal */