12 * Test that the optional member (c) is really optional.
15 32 | 17, /* [UNIVERSAL 17], constructed */
19 64 | 3, /* [APPLICATION 3] */
24 22, /* [UNIVERSAL 22] */
32 * This buffer aims at checking the duplication.
35 32 | 17, /* [UNIVERSAL 17], constructed */
39 64 | 3, /* [APPLICATION 3] */
43 /* a INTEGER _again_ */
44 64 | 3, /* [APPLICATION 3] */
50 * This buffer checks that an unexpected member may be properly ignored.
53 32 | 17, /* [UNIVERSAL 17], constructed */
57 64 | 3, /* [APPLICATION 3] */
62 22, /* [UNIVERSAL 22] */
68 /* unexpected INTEGER */
69 64 | 4, /* [APPLICATION 4] */
74 ((2 << 6) + 2), /* [2] */
80 * This buffer checks that an unexpected member may be properly ignored.
81 * This time, with indefinite length encoding.
84 32 | 17, /* [UNIVERSAL 17], constructed */
88 64 | 3, /* [APPLICATION 3] */
93 22, /* [UNIVERSAL 22] */
99 /* unexpected data structure */
100 32 | 64 | 4, /* [APPLICATION 4] */
101 0x80, /* indefinite L */
102 64 | 1, /* [APPLICATION 1] */
111 * This buffer checks that an unexpected member may be properly ignored.
112 * This time, with indefinite length encoding at the outer level too.
115 32 | 17, /* [UNIVERSAL 17], constructed */
116 0x80, /* indefinite L */
119 64 | 3, /* [APPLICATION 3] */
124 22, /* [UNIVERSAL 22] */
130 /* unexpected data structure */
131 32 | 64 | 4, /* [APPLICATION 4] */
132 0x80, /* indefinite L */
133 64 | 1, /* [APPLICATION 1] */
145 check(int is_ok, uint8_t *buf, size_t size, size_t consumed) {
149 fprintf(stderr, "\nMust %s:\n", is_ok?"suceed":"fail");
151 tp = memset(&t, 0, sizeof(t));
153 fprintf(stderr, "Buf %p\n", buf);
154 rval = ber_decode(0, &asn_DEF_T, (void **)&tp, buf, size);
155 fprintf(stderr, "Returned code %d, consumed %zd (out of %zd)\n",
156 (int)rval.code, rval.consumed, size);
159 assert(rval.code == RC_OK);
160 assert(rval.consumed == consumed);
162 assert(t.i.size == 1);
163 assert(t.i.buf[0] == 96);
164 assert(t.s.size == 3);
165 assert(strcmp((char *)t.s.buf, "xyz") == 0);
172 if(rval.code == RC_OK) {
178 assert(rval.consumed <= consumed);
180 ASN_STRUCT_RESET(asn_DEF_T, tp);
184 static char xer_buf[128];
188 xer_cb(const void *buffer, size_t size, void *key) {
190 assert(xer_off + size < sizeof(xer_buf));
191 memcpy(xer_buf + xer_off, buffer, size);
197 check_xer(uint8_t *buf, uint8_t size, char *xer_sample) {
201 int xer_sample_len = strlen(xer_sample);
203 rval = ber_decode(0, &asn_DEF_T, (void **)&tp, buf, size);
204 assert(rval.code == RC_OK);
205 assert(rval.consumed == size);
209 er = xer_encode(&asn_DEF_T, tp, XER_F_CANONICAL, xer_cb, 0);
210 assert(er.encoded == xer_off);
212 xer_buf[xer_off] = 0;
213 printf("[%s] vs [%s]\n", xer_buf, xer_sample);
214 assert(xer_off == xer_sample_len);
215 assert(memcmp(xer_buf, xer_sample, xer_off) == 0);
217 ASN_STRUCT_FREE(asn_DEF_T, tp);
221 try_corrupt(uint8_t *buf, size_t size) {
224 fprintf(stderr, "\nCorrupting...\n");
226 for(int i = 0; i < 1000; i++) {
228 memcpy(tmp, buf, size);
230 /* Corrupt random _non-value_ location. */
231 do { loc = random() % size; } while(tmp[loc] >= 70);
232 do { tmp[loc] = buf[loc] ^ random(); } while(
233 (tmp[loc] == buf[loc])
234 || (buf[loc] == 0 && tmp[loc] == 0x80));
236 fprintf(stderr, "\nTry %d: corrupting byte %d (%d->%d)\n",
237 i, loc, buf[loc], tmp[loc]);
239 check(0, tmp, size, size);
244 main(int ac, char **av) {
246 (void)ac; /* Unused argument */
247 (void)av; /* Unused argument */
249 check(1, buf1, sizeof(buf1) + 20, sizeof(buf1));
250 check(0, buf2, sizeof(buf2) + 1, 5);
251 check(1, buf3, sizeof(buf3) + 1, sizeof(buf3));
252 check(1, buf4, sizeof(buf4), sizeof(buf4));
253 check(1, buf5, sizeof(buf5), sizeof(buf5));
254 check(1, buf5, sizeof(buf5) + 1, sizeof(buf5));
255 check(0, buf5, sizeof(buf5) - 1, sizeof(buf5));
257 check_xer(buf1, sizeof(buf1), "<T><s>xyz</s><i>96</i></T>");
258 check_xer(buf3, sizeof(buf3), "<T><s>xyz</s><i>96</i><b><true/></b></T>");
260 fprintf(stderr, "\nPseudo-random buffer corruptions must fail\n");
261 try_corrupt(buf1, sizeof(buf1));