11 32 | 17, /* [UNIVERSAL 17], constructed */
12 128, /* L, indefinite */
14 32 | 16, /* [UNIVERSAL 16], constructed */
18 2, /* [UNIVERSAL 2] */
22 2, /* [UNIVERSAL 2] */
26 32 | 16, /* [UNIVERSAL 16], constructed */
30 2, /* [UNIVERSAL 2] */
34 2, /* [UNIVERSAL 2] */
38 0, /* End of forest */
42 uint8_t buf1_reconstr[] = {
43 32 | 17, /* [UNIVERSAL 17], constructed */
46 32 | 16, /* [UNIVERSAL 16], constructed */
50 2, /* [UNIVERSAL 2] */
54 2, /* [UNIVERSAL 2] */
58 32 | 16, /* [UNIVERSAL 16], constructed */
62 2, /* [UNIVERSAL 2] */
66 2, /* [UNIVERSAL 2] */
73 static int bytes_compare(const void *bufferp, size_t size, void *key) {
74 const uint8_t *buffer = bufferp;
75 assert(buf_pos + size <= sizeof(buf1_reconstr));
77 (void)key; /* Unused argument */
79 fprintf(stderr, " writing %zd (%zd)\n", size, buf_pos + size);
81 for(; size; buf_pos++, size--, buffer++) {
82 if(buf1_reconstr[buf_pos] != *buffer) {
84 "Byte %zd is different: %d != %d (%x != %x)\n",
86 *buffer, buf1_reconstr[buf_pos],
87 *buffer, buf1_reconstr[buf_pos]
89 assert(buf1_reconstr[buf_pos] == *buffer);
97 check(int is_ok, uint8_t *buf, size_t size, size_t consumed) {
101 tp = memset(&t, 0, sizeof(t));
103 fprintf(stderr, "Buf %p\n", buf);
104 rval = ber_decode(0, &asn_DEF_Forest, (void **)&tp, buf, size);
105 fprintf(stderr, "Returned code %d, consumed %d\n",
106 (int)rval.code, (int)rval.consumed);
109 assert(rval.code == RC_OK);
110 assert(rval.consumed == consumed);
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);
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
126 assert(rval.consumed <= consumed);
127 ASN_STRUCT_RESET(asn_DEF_Forest, &t);
132 * Try to re-create the buffer.
135 der_encode(&asn_DEF_Forest, &t,
136 bytes_compare, buf1_reconstr);
137 assert(buf_pos == (ssize_t)sizeof(buf1_reconstr));
139 asn_fprint(stderr, &asn_DEF_Forest, &t);
140 xer_fprint(stderr, &asn_DEF_Forest, &t);
142 ASN_STRUCT_RESET(asn_DEF_Forest, &t);
145 static char xer_buf[512];
149 xer_cb(const void *buffer, size_t size, void *key) {
151 assert(xer_off + size < sizeof(xer_buf));
152 memcpy(xer_buf + xer_off, buffer, size);
158 check_xer(uint8_t *buf, uint8_t size, char *xer_sample) {
162 int xer_sample_len = strlen(xer_sample);
164 rval = ber_decode(0, &asn_DEF_Forest, (void **)&tp, buf, size);
165 assert(rval.code == RC_OK);
166 assert(rval.consumed == size);
170 er = xer_encode(&asn_DEF_Forest, tp, XER_F_CANONICAL, xer_cb, 0);
171 assert(er.encoded == xer_off);
173 xer_buf[xer_off] = 0;
174 printf("[%s] vs [%s]\n", xer_buf, xer_sample);
175 assert(xer_off == xer_sample_len);
176 assert(memcmp(xer_buf, xer_sample, xer_off) == 0);
178 ASN_STRUCT_FREE(asn_DEF_Forest, tp);
183 try_corrupt(uint8_t *buf, size_t size) {
186 fprintf(stderr, "\nCorrupting...\n");
188 for(int i = 0; i < 1000; i++) {
190 memcpy(tmp, buf, size);
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));
198 fprintf(stderr, "\nTry %d: corrupting byte %d (%d->%d)\n",
199 i, loc, buf[loc], tmp[loc]);
201 check(0, tmp, size, size);
206 main(int ac, char **av) {
208 (void)ac; /* Unused argument */
209 (void)av; /* Unused argument */
211 check(1, buf1, sizeof(buf1), sizeof(buf1));
212 check_xer(buf1, sizeof(buf1), "<Forest><Tree><height>100</height><width>80</width></Tree><Tree><height>110</height><width>82</width></Tree></Forest>");
213 try_corrupt(buf1, sizeof(buf1));
214 check(1, buf1, sizeof(buf1) + 20, sizeof(buf1));