blob: 3f1b1cdf8b7b8c28021aacc60b719ae16aea63c8 [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
Lev Walkin22b81a82004-08-13 12:38:42 +000010
11/*
12 * Test that the optional member (c) is really optional.
13 */
Lev Walkinf15320b2004-06-03 03:38:44 +000014uint8_t buf1[] = {
15 32 | 17, /* [UNIVERSAL 17], constructed */
16 8, /* L */
17
18 /* a INTEGER */
19 64 | 3, /* [APPLICATION 3] */
20 1, /* L */
21 96,
22
23 /* b IA5String */
24 22, /* [UNIVERSAL 22] */
25 3, /* L */
26 'x',
27 'y',
28 'z'
29};
30
31/*
32 * This buffer aims at checking the duplication.
33 */
34uint8_t buf2[] = {
35 32 | 17, /* [UNIVERSAL 17], constructed */
36 8, /* L */
37
38 /* a INTEGER */
39 64 | 3, /* [APPLICATION 3] */
40 1, /* L */
41 96,
42
43 /* a INTEGER _again_ */
44 64 | 3, /* [APPLICATION 3] */
45 1, /* L */
46 97,
47};
48
Lev Walkin22b81a82004-08-13 12:38:42 +000049/*
50 * This buffer checks that an unexpected member may be properly ignored.
51 */
52uint8_t buf3[] = {
53 32 | 17, /* [UNIVERSAL 17], constructed */
54 14, /* L */
55
Lev Walkin2a8672e2004-10-03 09:14:49 +000056 /* INTEGER */
Lev Walkin22b81a82004-08-13 12:38:42 +000057 64 | 3, /* [APPLICATION 3] */
58 1, /* L */
59 96,
60
Lev Walkin2a8672e2004-10-03 09:14:49 +000061 /* IA5String */
Lev Walkin22b81a82004-08-13 12:38:42 +000062 22, /* [UNIVERSAL 22] */
63 3, /* L */
64 'x',
65 'y',
66 'z',
67
68 /* unexpected INTEGER */
69 64 | 4, /* [APPLICATION 4] */
70 1, /* L */
71 96,
72
Lev Walkin2a8672e2004-10-03 09:14:49 +000073 /* [2] BOOLEAN */
Lev Walkin22b81a82004-08-13 12:38:42 +000074 ((2 << 6) + 2), /* [2] */
75 1, /* L */
76 0xff
77};
78
79/*
80 * This buffer checks that an unexpected member may be properly ignored.
81 * This time, with indefinite length encoding.
82 */
83uint8_t buf4[] = {
84 32 | 17, /* [UNIVERSAL 17], constructed */
85 16, /* L */
86
87 /* a INTEGER */
88 64 | 3, /* [APPLICATION 3] */
89 1, /* L */
90 96,
91
92 /* b IA5String */
93 22, /* [UNIVERSAL 22] */
94 3, /* L */
95 'x',
96 'y',
97 'z',
98
99 /* unexpected data structure */
100 32 | 64 | 4, /* [APPLICATION 4] */
101 0x80, /* indefinite L */
102 64 | 1, /* [APPLICATION 1] */
103 2, /* L */
104 'a', 'b',
105
106 0x00,
107 0x00
108};
109
110/*
111 * This buffer checks that an unexpected member may be properly ignored.
112 * This time, with indefinite length encoding at the outer level too.
113 */
114uint8_t buf5[] = {
115 32 | 17, /* [UNIVERSAL 17], constructed */
116 0x80, /* indefinite L */
117
Lev Walkin2a8672e2004-10-03 09:14:49 +0000118 /* INTEGER */
Lev Walkin22b81a82004-08-13 12:38:42 +0000119 64 | 3, /* [APPLICATION 3] */
120 1, /* L */
121 96,
122
Lev Walkin2a8672e2004-10-03 09:14:49 +0000123 /* IA5String */
Lev Walkin22b81a82004-08-13 12:38:42 +0000124 22, /* [UNIVERSAL 22] */
125 3, /* L */
126 'x',
127 'y',
128 'z',
129
130 /* unexpected data structure */
131 32 | 64 | 4, /* [APPLICATION 4] */
132 0x80, /* indefinite L */
133 64 | 1, /* [APPLICATION 1] */
134 2, /* L */
135 'a', 'b',
136
137 0x00,
138 0x00,
139
140 0x00,
141 0x00
142};
143
Lev Walkinf15320b2004-06-03 03:38:44 +0000144static void
Lev Walkin1715b322013-03-28 04:02:13 -0700145check(int is_ok, uint8_t *buf, size_t size, size_t consumed) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000146 T_t t, *tp;
Lev Walkin90bf7ae2004-10-20 15:46:56 +0000147 asn_dec_rval_t rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000148
Lev Walkin22b81a82004-08-13 12:38:42 +0000149 fprintf(stderr, "\nMust %s:\n", is_ok?"suceed":"fail");
150
Lev Walkinf15320b2004-06-03 03:38:44 +0000151 tp = memset(&t, 0, sizeof(t));
152
153 fprintf(stderr, "Buf %p\n", buf);
Lev Walkinc7400c52004-09-29 13:14:36 +0000154 rval = ber_decode(0, &asn_DEF_T, (void **)&tp, buf, size);
Lev Walkin1715b322013-03-28 04:02:13 -0700155 fprintf(stderr, "Returned code %d, consumed %zd (out of %zd)\n",
156 (int)rval.code, rval.consumed, size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000157
158 if(is_ok) {
159 assert(rval.code == RC_OK);
160 assert(rval.consumed == consumed);
161
Lev Walkin2a8672e2004-10-03 09:14:49 +0000162 assert(t.i.size == 1);
163 assert(t.i.buf[0] == 96);
164 assert(t.s.size == 3);
Lev Walkin5b63acf2014-01-14 01:48:37 -0800165 assert(strcmp((char *)t.s.buf, "xyz") == 0);
Lev Walkin22b81a82004-08-13 12:38:42 +0000166 if(buf == buf3) {
Lev Walkin2a8672e2004-10-03 09:14:49 +0000167 assert(t.b);
Lev Walkin22b81a82004-08-13 12:38:42 +0000168 } else {
Lev Walkin2a8672e2004-10-03 09:14:49 +0000169 assert(t.b == 0);
Lev Walkin22b81a82004-08-13 12:38:42 +0000170 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000171 } else {
172 if(rval.code == RC_OK) {
Lev Walkin2a8672e2004-10-03 09:14:49 +0000173 assert(t.i.size != 1
174 || t.s.size != 3
175 || !t.b
Lev Walkinf15320b2004-06-03 03:38:44 +0000176 );
177 }
178 assert(rval.consumed <= consumed);
179 }
Vasil Velichkovcef21e02017-10-09 23:40:17 +0300180 ASN_STRUCT_RESET(asn_DEF_T, tp);
Lev Walkinf15320b2004-06-03 03:38:44 +0000181}
182
Lev Walkin2a8672e2004-10-03 09:14:49 +0000183
184static char xer_buf[128];
185static int xer_off;
186
187static int
188xer_cb(const void *buffer, size_t size, void *key) {
189 (void)key;
190 assert(xer_off + size < sizeof(xer_buf));
191 memcpy(xer_buf + xer_off, buffer, size);
192 xer_off += size;
193 return 0;
194}
195
196static void
197check_xer(uint8_t *buf, uint8_t size, char *xer_sample) {
198 T_t *tp = 0;
Lev Walkin90bf7ae2004-10-20 15:46:56 +0000199 asn_dec_rval_t rval;
Lev Walkin2a8672e2004-10-03 09:14:49 +0000200 asn_enc_rval_t er;
201 int xer_sample_len = strlen(xer_sample);
202
203 rval = ber_decode(0, &asn_DEF_T, (void **)&tp, buf, size);
204 assert(rval.code == RC_OK);
205 assert(rval.consumed == size);
206 assert(tp);
207
208 xer_off = 0;
209 er = xer_encode(&asn_DEF_T, tp, XER_F_CANONICAL, xer_cb, 0);
210 assert(er.encoded == xer_off);
211 assert(xer_off);
212 xer_buf[xer_off] = 0;
213 printf("[%s] vs [%s]\n", xer_buf, xer_sample);
Lev Walkin4a6f3cf2004-10-03 10:54:34 +0000214 assert(xer_off == xer_sample_len);
Lev Walkin2a8672e2004-10-03 09:14:49 +0000215 assert(memcmp(xer_buf, xer_sample, xer_off) == 0);
Vasil Velichkovcef21e02017-10-09 23:40:17 +0300216
217 ASN_STRUCT_FREE(asn_DEF_T, tp);
Lev Walkin2a8672e2004-10-03 09:14:49 +0000218}
219
Lev Walkinf15320b2004-06-03 03:38:44 +0000220static void
Lev Walkin1715b322013-03-28 04:02:13 -0700221try_corrupt(uint8_t *buf, size_t size) {
Lev Walkina4f8e942017-10-08 19:28:20 -0700222 uint8_t tmp[size];
Lev Walkinf15320b2004-06-03 03:38:44 +0000223
224 fprintf(stderr, "\nCorrupting...\n");
225
Lev Walkina4f8e942017-10-08 19:28:20 -0700226 for(int i = 0; i < 1000; i++) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000227 int loc;
228 memcpy(tmp, buf, size);
229
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));
235
236 fprintf(stderr, "\nTry %d: corrupting byte %d (%d->%d)\n",
237 i, loc, buf[loc], tmp[loc]);
238
239 check(0, tmp, size, size);
240 }
241}
242
243int
244main(int ac, char **av) {
245
Lev Walkind9bd7752004-06-05 08:17:50 +0000246 (void)ac; /* Unused argument */
247 (void)av; /* Unused argument */
248
Lev Walkinf15320b2004-06-03 03:38:44 +0000249 check(1, buf1, sizeof(buf1) + 20, sizeof(buf1));
Lev Walkinf15320b2004-06-03 03:38:44 +0000250 check(0, buf2, sizeof(buf2) + 1, 5);
Lev Walkin22b81a82004-08-13 12:38:42 +0000251 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));
Lev Walkinf15320b2004-06-03 03:38:44 +0000256
Lev Walkin2a8672e2004-10-03 09:14:49 +0000257 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>");
259
Lev Walkinf15320b2004-06-03 03:38:44 +0000260 fprintf(stderr, "\nPseudo-random buffer corruptions must fail\n");
261 try_corrupt(buf1, sizeof(buf1));
262
263 return 0;
264}