blob: 399f0dab832517fcaa29e26e15e665a547b418af [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
Lev Walkinb7c40b62013-03-28 05:54:12 -070072size_t 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 Walkin1715b322013-03-28 04:02:13 -070079 fprintf(stderr, " writing %zd (%zd)\n", size, buf_pos + size);
Lev Walkinf15320b2004-06-03 03:38:44 +000080
81 for(; size; buf_pos++, size--, buffer++) {
82 if(buf1_reconstr[buf_pos] != *buffer) {
83 fprintf(stderr,
Lev Walkinb7c40b62013-03-28 05:54:12 -070084 "Byte %zd is different: %d != %d (%x != %x)\n",
Lev Walkinf15320b2004-06-03 03:38:44 +000085 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 Walkin1715b322013-03-28 04:02:13 -070097check(int is_ok, uint8_t *buf, size_t 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);
Vasil Velichkovcef21e02017-10-09 23:40:17 +0300127 ASN_STRUCT_RESET(asn_DEF_Forest, &t);
Lev Walkinf15320b2004-06-03 03:38:44 +0000128 return;
129 }
130
131 /*
132 * Try to re-create the buffer.
133 */
134 buf_pos = 0;
Lev Walkinc7400c52004-09-29 13:14:36 +0000135 der_encode(&asn_DEF_Forest, &t,
Lev Walkinf15320b2004-06-03 03:38:44 +0000136 bytes_compare, buf1_reconstr);
Lev Walkin5b63acf2014-01-14 01:48:37 -0800137 assert(buf_pos == (ssize_t)sizeof(buf1_reconstr));
Lev Walkinf15320b2004-06-03 03:38:44 +0000138
Lev Walkinc7400c52004-09-29 13:14:36 +0000139 asn_fprint(stderr, &asn_DEF_Forest, &t);
140 xer_fprint(stderr, &asn_DEF_Forest, &t);
Lev Walkinf15320b2004-06-03 03:38:44 +0000141
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700142 ASN_STRUCT_RESET(asn_DEF_Forest, &t);
Lev Walkinf15320b2004-06-03 03:38:44 +0000143}
144
Lev Walkin2a8672e2004-10-03 09:14:49 +0000145static char xer_buf[512];
146static int xer_off;
147
148static int
149xer_cb(const void *buffer, size_t size, void *key) {
150 (void)key;
151 assert(xer_off + size < sizeof(xer_buf));
152 memcpy(xer_buf + xer_off, buffer, size);
153 xer_off += size;
154 return 0;
155}
156
157static void
158check_xer(uint8_t *buf, uint8_t size, char *xer_sample) {
159 Forest_t *tp = 0;
Lev Walkin90bf7ae2004-10-20 15:46:56 +0000160 asn_dec_rval_t rval;
Lev Walkin2a8672e2004-10-03 09:14:49 +0000161 asn_enc_rval_t er;
162 int xer_sample_len = strlen(xer_sample);
163
164 rval = ber_decode(0, &asn_DEF_Forest, (void **)&tp, buf, size);
165 assert(rval.code == RC_OK);
166 assert(rval.consumed == size);
167 assert(tp);
168
169 xer_off = 0;
170 er = xer_encode(&asn_DEF_Forest, tp, XER_F_CANONICAL, xer_cb, 0);
171 assert(er.encoded == xer_off);
172 assert(xer_off);
173 xer_buf[xer_off] = 0;
174 printf("[%s] vs [%s]\n", xer_buf, xer_sample);
Lev Walkin4a6f3cf2004-10-03 10:54:34 +0000175 assert(xer_off == xer_sample_len);
Lev Walkin2a8672e2004-10-03 09:14:49 +0000176 assert(memcmp(xer_buf, xer_sample, xer_off) == 0);
Vasil Velichkovcef21e02017-10-09 23:40:17 +0300177
178 ASN_STRUCT_FREE(asn_DEF_Forest, tp);
Lev Walkin2a8672e2004-10-03 09:14:49 +0000179}
180
181
Lev Walkinf15320b2004-06-03 03:38:44 +0000182static void
Lev Walkin1715b322013-03-28 04:02:13 -0700183try_corrupt(uint8_t *buf, size_t size) {
Lev Walkina4f8e942017-10-08 19:28:20 -0700184 uint8_t tmp[size];
Lev Walkinf15320b2004-06-03 03:38:44 +0000185
186 fprintf(stderr, "\nCorrupting...\n");
187
Lev Walkina4f8e942017-10-08 19:28:20 -0700188 for(int i = 0; i < 1000; i++) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000189 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}