NativeEnumerated.c vars NULL init and check
[com/asn1c.git] / tests / tests-c-compiler / check-src / check-31.-fwide-types.c
1 #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
10 uint8_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
42 uint8_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
72 size_t buf_pos;
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));
76
77         (void)key;      /* Unused argument */
78
79         fprintf(stderr,  "  writing %zd (%zd)\n", size, buf_pos + size);
80
81         for(; size; buf_pos++, size--, buffer++) {
82                 if(buf1_reconstr[buf_pos] != *buffer) {
83                         fprintf(stderr,
84                                 "Byte %zd 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
96 static void
97 check(int is_ok, uint8_t *buf, size_t size, size_t consumed) {
98         Forest_t t, *tp;
99         asn_dec_rval_t rval;
100
101         tp = memset(&t, 0, sizeof(t));
102
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);
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                 ASN_STRUCT_RESET(asn_DEF_Forest, &t);
128                 return;
129         }
130
131         /*
132          * Try to re-create the buffer.
133          */
134         buf_pos = 0;
135         der_encode(&asn_DEF_Forest, &t,
136                 bytes_compare, buf1_reconstr);
137         assert(buf_pos == (ssize_t)sizeof(buf1_reconstr));
138
139         asn_fprint(stderr, &asn_DEF_Forest, &t);
140         xer_fprint(stderr, &asn_DEF_Forest, &t);
141
142         ASN_STRUCT_RESET(asn_DEF_Forest, &t);
143 }
144
145 static char xer_buf[512];
146 static int xer_off;
147
148 static int
149 xer_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
157 static void
158 check_xer(uint8_t *buf, uint8_t size, char *xer_sample) {
159         Forest_t *tp = 0;
160         asn_dec_rval_t rval;
161         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);
175         assert(xer_off == xer_sample_len);
176         assert(memcmp(xer_buf, xer_sample, xer_off) == 0);
177
178         ASN_STRUCT_FREE(asn_DEF_Forest, tp);
179 }
180
181
182 static void
183 try_corrupt(uint8_t *buf, size_t size) {
184         uint8_t tmp[size];
185
186         fprintf(stderr, "\nCorrupting...\n");
187
188         for(int 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
205 int
206 main(int ac, char **av) {
207
208         (void)ac;       /* Unused argument */
209         (void)av;       /* Unused argument */
210
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));
215
216         return 0;
217 }