blob: 79de91156c3409635756da52b5f1b21be9c58350 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001#undef NDEBUG
2#include <stdio.h>
3#include <stdlib.h>
4#include <sys/types.h>
5#include <string.h>
6#include <assert.h>
7
8#include <T.h>
9
10uint8_t buf1[] = {
11 32 | ((2 << 6) + 5), /* [5], constructed */
12 17, /* L */
13 32 | 16, /* [UNIVERSAL 16], constructed */
14 15, /* L */
15 /* INTEGER a */
16 2, /* [UNIVERSAL 2] */
17 2, /* L */
18 150,
19 70,
20 /* INTEGER b */
21 ((2 << 6) + 0), /* [0] */
22 1, /* L */
23 123,
24 /* INTEGER c */
25 ((2 << 6) + 1), /* [1] */
26 1, /* L */
27 123,
28 /* INTEGER d */
29 32 | ((2 << 6) + 5), /* [5], constructed */
30 3,
31 2,
32 1, /* L */
33 123,
34};
35
36static void
Lev Walkind9bd7752004-06-05 08:17:50 +000037check(int is_ok, uint8_t *buf, int size, size_t consumed) {
Lev Walkinf15320b2004-06-03 03:38:44 +000038 T_t t, *tp;
39 ber_dec_rval_t rval;
40
41 tp = memset(&t, 0, sizeof(t));
42
43 fprintf(stderr, "Buf %p\n", buf);
44 rval = ber_decode(&asn1_DEF_T, (void **)&tp, buf, size);
45 fprintf(stderr, "Returned code %d, consumed %d\n",
46 (int)rval.code, (int)rval.consumed);
47
48 if(is_ok) {
49 assert(rval.code == RC_OK);
50 assert(rval.consumed == consumed);
51 } else {
52 if(rval.code == RC_OK) {
53 assert(t.a.size != 2
54 || (!t.b || t.b->size != 1)
55 || (!t.c || t.c->size != 1)
56 || t.d.size != 1
57 );
58 }
59 assert(rval.consumed <= consumed);
60 }
61}
62
63static void
64try_corrupt(uint8_t *buf, int size) {
65 uint8_t *tmp;
66 int i;
67
68 fprintf(stderr, "\nCorrupting...\n");
69
70 tmp = alloca(size);
71
72 for(i = 0; i < 1000; i++) {
73 int loc;
74 memcpy(tmp, buf, size);
75
76 /* Corrupt random _non-value_ location. */
77 do { loc = random() % size; } while(tmp[loc] >= 70);
78 do { tmp[loc] ^= random(); } while(tmp[loc] == buf[loc]);
79
80 fprintf(stderr, "\nTry %d: corrupting byte %d (%d->%d)\n",
81 i, loc, buf[loc], tmp[loc]);
82
83 check(0, tmp, size, size);
84 }
85}
86
87int
88main(int ac, char **av) {
89
Lev Walkind9bd7752004-06-05 08:17:50 +000090 (void)ac; /* Unused argument */
91 (void)av; /* Unused argument */
92
Lev Walkinf15320b2004-06-03 03:38:44 +000093 check(1, buf1, sizeof(buf1), sizeof(buf1));
94 try_corrupt(buf1, sizeof(buf1));
95
96 return 0;
97}