blob: 53ccf5ab0683303d9f33a8c298afcdf8149389e0 [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 <Forest.h>
9
10uint8_t buf1[] = {
11 32 | 17, /* [UNIVERSAL 17], constructed */
12 128, /* L, indefinite */
13
14 32 | 16, /* [UNIVERSAL 16], constructed */
15 6, /* L */
16
17 /* height INTEGER */
18 2, /* [UNIVERSAL 2] */
19 1, /* L */
20 100,
21 /* width INTEGER */
22 2, /* [UNIVERSAL 2] */
23 1, /* L */
24 80,
25
26 32 | 16, /* [UNIVERSAL 16], constructed */
27 6, /* L */
28
29 /* height INTEGER */
30 2, /* [UNIVERSAL 2] */
31 1, /* L */
32 110,
33 /* width INTEGER */
34 2, /* [UNIVERSAL 2] */
35 1, /* L */
36 82,
37
38 0, /* End of forest */
39 0
40};
41
42uint8_t buf1_reconstr[] = {
43 32 | 17, /* [UNIVERSAL 17], constructed */
44 16, /* L */
45
46 32 | 16, /* [UNIVERSAL 16], constructed */
47 6, /* L */
48
49 /* height INTEGER */
50 2, /* [UNIVERSAL 2] */
51 1, /* L */
52 100,
53 /* width INTEGER */
54 2, /* [UNIVERSAL 2] */
55 1, /* L */
56 80,
57
58 32 | 16, /* [UNIVERSAL 16], constructed */
59 6, /* L */
60
61 /* height INTEGER */
62 2, /* [UNIVERSAL 2] */
63 1, /* L */
64 110,
65 /* width INTEGER */
66 2, /* [UNIVERSAL 2] */
67 1, /* L */
68 82
69
70};
71
72int buf_pos;
Lev Walkinb4eb8b52004-08-16 07:02:39 +000073static int bytes_compare(const void *bufferp, size_t size, void *key) {
Lev Walkinf15320b2004-06-03 03:38:44 +000074 const uint8_t *buffer = bufferp;
75 assert(buf_pos + size <= sizeof(buf1_reconstr));
76
Lev Walkin67c2aec2004-06-05 08:47:18 +000077 (void)key; /* Unused argument */
78
Lev Walkinf15320b2004-06-03 03:38:44 +000079 fprintf(stderr, " writing %d (%d)\n", (int)size, buf_pos + (int)size);
80
81 for(; size; buf_pos++, size--, buffer++) {
82 if(buf1_reconstr[buf_pos] != *buffer) {
83 fprintf(stderr,
84 "Byte %d is different: %d != %d (%x != %x)\n",
85 buf_pos,
86 *buffer, buf1_reconstr[buf_pos],
87 *buffer, buf1_reconstr[buf_pos]
88 );
89 assert(buf1_reconstr[buf_pos] == *buffer);
90 }
91 }
92
93 return 0;
94}
95
96static void
Lev Walkind9bd7752004-06-05 08:17:50 +000097check(int is_ok, uint8_t *buf, int size, size_t consumed) {
Lev Walkinf15320b2004-06-03 03:38:44 +000098 Forest_t t, *tp;
Lev Walkin90bf7ae2004-10-20 15:46:56 +000099 asn_dec_rval_t rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000100
101 tp = memset(&t, 0, sizeof(t));
102
103 fprintf(stderr, "Buf %p\n", buf);
Lev Walkinc7400c52004-09-29 13:14:36 +0000104 rval = ber_decode(0, &asn_DEF_Forest, (void **)&tp, buf, size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000105 fprintf(stderr, "Returned code %d, consumed %d\n",
106 (int)rval.code, (int)rval.consumed);
107
108 if(is_ok) {
109 assert(rval.code == RC_OK);
110 assert(rval.consumed == consumed);
111
112 assert(t.list.count == 2);
113 assert(t.list.array[0]->height.size == 1);
114 assert(t.list.array[0]->width.size == 1);
115 assert(t.list.array[1]->height.size == 1);
116 assert(t.list.array[1]->width.size == 1);
117 } else {
118 if(rval.code == RC_OK) {
119 assert(t.list.count != 2
120 || t.list.array[0]->height.size != 1
121 || t.list.array[0]->width.size != 1
122 || t.list.array[1]->height.size != 1
123 || t.list.array[1]->width.size != 1
124 );
125 }
126 assert(rval.consumed <= consumed);
127 return;
128 }
129
130 /*
131 * Try to re-create the buffer.
132 */
133 buf_pos = 0;
Lev Walkinc7400c52004-09-29 13:14:36 +0000134 der_encode(&asn_DEF_Forest, &t,
Lev Walkinf15320b2004-06-03 03:38:44 +0000135 bytes_compare, buf1_reconstr);
136 assert(buf_pos == sizeof(buf1_reconstr));
137
Lev Walkinc7400c52004-09-29 13:14:36 +0000138 asn_fprint(stderr, &asn_DEF_Forest, &t);
139 xer_fprint(stderr, &asn_DEF_Forest, &t);
Lev Walkinf15320b2004-06-03 03:38:44 +0000140
Lev Walkinc7400c52004-09-29 13:14:36 +0000141 asn_DEF_Forest.free_struct(&asn_DEF_Forest, &t, 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000142}
143
Lev Walkin2a8672e2004-10-03 09:14:49 +0000144static char xer_buf[512];
145static int xer_off;
146
147static int
148xer_cb(const void *buffer, size_t size, void *key) {
149 (void)key;
150 assert(xer_off + size < sizeof(xer_buf));
151 memcpy(xer_buf + xer_off, buffer, size);
152 xer_off += size;
153 return 0;
154}
155
156static void
157check_xer(uint8_t *buf, uint8_t size, char *xer_sample) {
158 Forest_t *tp = 0;
Lev Walkin90bf7ae2004-10-20 15:46:56 +0000159 asn_dec_rval_t rval;
Lev Walkin2a8672e2004-10-03 09:14:49 +0000160 asn_enc_rval_t er;
161 int xer_sample_len = strlen(xer_sample);
162
163 rval = ber_decode(0, &asn_DEF_Forest, (void **)&tp, buf, size);
164 assert(rval.code == RC_OK);
165 assert(rval.consumed == size);
166 assert(tp);
167
168 xer_off = 0;
169 er = xer_encode(&asn_DEF_Forest, tp, XER_F_CANONICAL, xer_cb, 0);
170 assert(er.encoded == xer_off);
171 assert(xer_off);
172 xer_buf[xer_off] = 0;
173 printf("[%s] vs [%s]\n", xer_buf, xer_sample);
Lev Walkin4a6f3cf2004-10-03 10:54:34 +0000174 assert(xer_off == xer_sample_len);
Lev Walkin2a8672e2004-10-03 09:14:49 +0000175 assert(memcmp(xer_buf, xer_sample, xer_off) == 0);
176}
177
178
Lev Walkinf15320b2004-06-03 03:38:44 +0000179static void
180try_corrupt(uint8_t *buf, int size) {
181 uint8_t *tmp;
182 int i;
183
184 fprintf(stderr, "\nCorrupting...\n");
185
186 tmp = alloca(size);
187
188 for(i = 0; i < 1000; i++) {
189 int loc;
190 memcpy(tmp, buf, size);
191
192 /* Corrupt random _non-value_ location. */
193 do { loc = random() % size; } while(tmp[loc] >= 70);
194 do { tmp[loc] = buf[loc] ^ random(); } while(
195 (tmp[loc] == buf[loc])
196 || (buf[loc] == 0 && tmp[loc] == 0x80));
197
198 fprintf(stderr, "\nTry %d: corrupting byte %d (%d->%d)\n",
199 i, loc, buf[loc], tmp[loc]);
200
201 check(0, tmp, size, size);
202 }
203}
204
205int
206main(int ac, char **av) {
207
Lev Walkind9bd7752004-06-05 08:17:50 +0000208 (void)ac; /* Unused argument */
209 (void)av; /* Unused argument */
210
Lev Walkinf15320b2004-06-03 03:38:44 +0000211 check(1, buf1, sizeof(buf1), sizeof(buf1));
Lev Walkin2a8672e2004-10-03 09:14:49 +0000212 check_xer(buf1, sizeof(buf1), "<Forest><Tree><height>100</height><width>80</width></Tree><Tree><height>110</height><width>82</width></Tree></Forest>");
Lev Walkinf15320b2004-06-03 03:38:44 +0000213 try_corrupt(buf1, sizeof(buf1));
214 check(1, buf1, sizeof(buf1) + 20, sizeof(buf1));
215
216 return 0;
217}