Fix bugs in proto3 mode encoding of submessages (#256)
[apps/agl-service-can-low-level.git] / tests / alltypes_callback / decode_alltypes_callback.c
1 /* Attempts to test all the datatypes supported by ProtoBuf when used as callback fields.
2  * Note that normally there would be no reason to use callback fields for this,
3  * because each encoder defined here only gives a single field.
4  */
5
6 #include <stdio.h>
7 #include <string.h>
8 #include <stdlib.h>
9 #include <pb_decode.h>
10 #include "alltypes.pb.h"
11 #include "test_helpers.h"
12
13 #define TEST(x) if (!(x)) { \
14     printf("Test " #x " failed (in field %d).\n", field->tag); \
15     return false; \
16     }
17
18 static bool read_varint(pb_istream_t *stream, const pb_field_t *field, void **arg)
19 {
20     uint64_t value;
21     if (!pb_decode_varint(stream, &value))
22         return false;
23     
24     TEST((int64_t)value == (long)*arg);
25     return true;
26 }
27
28 static bool read_svarint(pb_istream_t *stream, const pb_field_t *field, void **arg)
29 {
30     int64_t value;
31     if (!pb_decode_svarint(stream, &value))
32         return false;
33     
34     TEST(value == (long)*arg);
35     return true;
36 }
37
38 static bool read_fixed32(pb_istream_t *stream, const pb_field_t *field, void **arg)
39 {
40     uint32_t value;
41     if (!pb_decode_fixed32(stream, &value))
42         return false;
43     
44     TEST(value == *(uint32_t*)*arg);
45     return true;
46 }
47
48 static bool read_fixed64(pb_istream_t *stream, const pb_field_t *field, void **arg)
49 {
50     uint64_t value;
51     if (!pb_decode_fixed64(stream, &value))
52         return false;
53     
54     TEST(value == *(uint64_t*)*arg);
55     return true;
56 }
57
58 static bool read_string(pb_istream_t *stream, const pb_field_t *field, void **arg)
59 {
60     uint8_t buf[16] = {0};
61     size_t len = stream->bytes_left;
62     
63     if (len > sizeof(buf) - 1 || !pb_read(stream, buf, len))
64         return false;
65     
66     TEST(strcmp((char*)buf, *arg) == 0);
67     return true;
68 }
69
70 static bool read_submsg(pb_istream_t *stream, const pb_field_t *field, void **arg)
71 {
72     SubMessage submsg = {""};
73     SubMessage *ref = *arg;
74     
75     if (!pb_decode(stream, SubMessage_fields, &submsg))
76         return false;
77     
78     TEST(strcmp(submsg.substuff1, ref->substuff1) == 0);
79     TEST(submsg.substuff2 == ref->substuff2);
80     TEST(submsg.has_substuff3 == ref->has_substuff3);
81     TEST(submsg.substuff3 == ref->substuff3); 
82     return true;
83 }
84
85 static bool read_emptymsg(pb_istream_t *stream, const pb_field_t *field, void **arg)
86 {
87     EmptyMessage emptymsg = {0};
88     return pb_decode(stream, EmptyMessage_fields, &emptymsg);
89 }
90
91 static bool read_repeated_varint(pb_istream_t *stream, const pb_field_t *field, void **arg)
92 {
93     int32_t** expected = (int32_t**)arg;
94     uint64_t value;
95     if (!pb_decode_varint(stream, &value))
96         return false;
97
98     TEST(*(*expected)++ == value);
99     return true;
100 }
101
102 static bool read_repeated_svarint(pb_istream_t *stream, const pb_field_t *field, void **arg)
103 {
104     int32_t** expected = (int32_t**)arg;
105     int64_t value;
106     if (!pb_decode_svarint(stream, &value))
107         return false;
108
109     TEST(*(*expected)++ == value);
110     return true;
111 }
112
113 static bool read_repeated_fixed32(pb_istream_t *stream, const pb_field_t *field, void **arg)
114 {
115     uint32_t** expected = (uint32_t**)arg;
116     uint32_t value;
117     if (!pb_decode_fixed32(stream, &value))
118         return false;
119
120     TEST(*(*expected)++ == value);
121     return true;
122 }
123
124 static bool read_repeated_fixed64(pb_istream_t *stream, const pb_field_t *field, void **arg)
125 {
126     uint64_t** expected = (uint64_t**)arg;
127     uint64_t value;
128     if (!pb_decode_fixed64(stream, &value))
129         return false;
130
131     TEST(*(*expected)++ == value);
132     return true;
133 }
134
135 static bool read_repeated_string(pb_istream_t *stream, const pb_field_t *field, void **arg)
136 {
137     uint8_t*** expected = (uint8_t***)arg;
138     uint8_t buf[16] = {0};
139     size_t len = stream->bytes_left;
140     
141     if (len > sizeof(buf) - 1 || !pb_read(stream, buf, len))
142         return false;
143     
144     TEST(strcmp((char*)*(*expected)++, (char*)buf) == 0);
145     return true;
146 }
147
148 static bool read_repeated_submsg(pb_istream_t *stream, const pb_field_t *field, void **arg)
149 {
150     SubMessage** expected = (SubMessage**)arg;
151     SubMessage submsg = {""};
152     if (!pb_decode(stream, SubMessage_fields, &submsg))
153         return false;
154
155     TEST(strcmp(submsg.substuff1, (*expected)->substuff1) == 0);
156     TEST(submsg.substuff2 == (*expected)->substuff2);
157     TEST(submsg.has_substuff3 == (*expected)->has_substuff3);
158     TEST(submsg.substuff3 == (*expected)->substuff3);
159     (*expected)++;
160
161     return true;
162 }
163
164 static bool read_limits(pb_istream_t *stream, const pb_field_t *field, void **arg)
165 {
166     Limits decoded = {0};
167     if (!pb_decode(stream, Limits_fields, &decoded))
168         return false;
169
170     TEST(decoded.int32_min  == INT32_MIN);
171     TEST(decoded.int32_max  == INT32_MAX);
172     TEST(decoded.uint32_min == 0);
173     TEST(decoded.uint32_max == UINT32_MAX);
174     TEST(decoded.int64_min  == INT64_MIN);
175     TEST(decoded.int64_max  == INT64_MAX);
176     TEST(decoded.uint64_min == 0);
177     TEST(decoded.uint64_max == UINT64_MAX);
178     TEST(decoded.enum_min   == HugeEnum_Negative);
179     TEST(decoded.enum_max   == HugeEnum_Positive);
180     
181     return true;
182 }
183
184 /* This function is called once from main(), it handles
185    the decoding and checks the fields. */
186 bool check_alltypes(pb_istream_t *stream, int mode)
187 {
188     /* Values for use from callbacks through pointers. */
189     bool status;
190     uint32_t    req_fixed32     = 1008;
191     int32_t     req_sfixed32    = -1009;
192     float       req_float       = 1010.0f;
193     uint64_t    req_fixed64     = 1011;
194     int64_t     req_sfixed64    = -1012;
195     double      req_double      = 1013.0;
196     SubMessage  req_submsg      = {"1016", 1016, false, 3};
197     
198     int32_t     rep_int32[5]    = {0, 0, 0, 0, -2001};
199     int32_t     rep_int64[5]    = {0, 0, 0, 0, -2002};
200     int32_t     rep_uint32[5]   = {0, 0, 0, 0,  2003};
201     int32_t     rep_uint64[5]   = {0, 0, 0, 0,  2004};
202     int32_t     rep_sint32[5]   = {0, 0, 0, 0, -2005};
203     int32_t     rep_sint64[5]   = {0, 0, 0, 0, -2006};
204     int32_t     rep_bool[5]     = {false, false, false, false, true};
205     uint32_t    rep_fixed32[5]  = {0, 0, 0, 0,  2008};
206     int32_t     rep_sfixed32[5] = {0, 0, 0, 0, -2009};
207     float       rep_float[5]    = {0, 0, 0, 0,  2010.0f};
208     uint64_t    rep_fixed64[5]  = {0, 0, 0, 0,  2011};
209     int64_t     rep_sfixed64[5] = {0, 0, 0, 0, -2012};
210     double      rep_double[5]   = {0, 0, 0, 0,  2013.0};
211     char*       rep_string[5]   = {"", "", "", "", "2014"};
212     char*       rep_bytes[5]    = {"", "", "", "", "2015"};
213     SubMessage  rep_submsg[5]   = {{"", 0, 0, 3},
214                                    {"", 0, 0, 3},
215                                    {"", 0, 0, 3},
216                                    {"", 0, 0, 3},
217                                    {"2016", 2016, true, 2016}};
218     int32_t     rep_enum[5]     = {0, 0, 0, 0, MyEnum_Truth};
219     
220     uint32_t    opt_fixed32     = 3048;
221     int32_t     opt_sfixed32    = 3049;
222     float       opt_float       = 3050.0f;
223     uint64_t    opt_fixed64     = 3051;
224     int64_t     opt_sfixed64    = 3052;
225     double      opt_double      = 3053.0f;
226     SubMessage  opt_submsg      = {"3056", 3056, false, 3};
227
228     SubMessage  oneof_msg1      = {"4059", 4059, false, 3};
229     
230     /* Bind callbacks for required fields */
231     AllTypes alltypes = AllTypes_init_zero;
232     
233     alltypes.req_int32.funcs.decode = &read_varint;
234     alltypes.req_int32.arg = (void*)-1001;
235     
236     alltypes.req_int64.funcs.decode = &read_varint;
237     alltypes.req_int64.arg = (void*)-1002;
238     
239     alltypes.req_uint32.funcs.decode = &read_varint;
240     alltypes.req_uint32.arg = (void*)1003;
241
242     alltypes.req_uint32.funcs.decode = &read_varint;
243     alltypes.req_uint32.arg = (void*)1003;    
244     
245     alltypes.req_uint64.funcs.decode = &read_varint;
246     alltypes.req_uint64.arg = (void*)1004;
247     
248     alltypes.req_sint32.funcs.decode = &read_svarint;
249     alltypes.req_sint32.arg = (void*)-1005;   
250     
251     alltypes.req_sint64.funcs.decode = &read_svarint;
252     alltypes.req_sint64.arg = (void*)-1006;   
253     
254     alltypes.req_bool.funcs.decode = &read_varint;
255     alltypes.req_bool.arg = (void*)true;   
256     
257     alltypes.req_fixed32.funcs.decode = &read_fixed32;
258     alltypes.req_fixed32.arg = &req_fixed32;
259     
260     alltypes.req_sfixed32.funcs.decode = &read_fixed32;
261     alltypes.req_sfixed32.arg = &req_sfixed32;
262     
263     alltypes.req_float.funcs.decode = &read_fixed32;
264     alltypes.req_float.arg = &req_float;
265     
266     alltypes.req_fixed64.funcs.decode = &read_fixed64;
267     alltypes.req_fixed64.arg = &req_fixed64;
268     
269     alltypes.req_sfixed64.funcs.decode = &read_fixed64;
270     alltypes.req_sfixed64.arg = &req_sfixed64;
271     
272     alltypes.req_double.funcs.decode = &read_fixed64;
273     alltypes.req_double.arg = &req_double;
274     
275     alltypes.req_string.funcs.decode = &read_string;
276     alltypes.req_string.arg = "1014";
277     
278     alltypes.req_bytes.funcs.decode = &read_string;
279     alltypes.req_bytes.arg = "1015";
280     
281     alltypes.req_submsg.funcs.decode = &read_submsg;
282     alltypes.req_submsg.arg = &req_submsg;
283     
284     alltypes.req_enum.funcs.decode = &read_varint;
285     alltypes.req_enum.arg = (void*)MyEnum_Truth;
286     
287     alltypes.req_emptymsg.funcs.decode = &read_emptymsg;
288     
289     /* Bind callbacks for repeated fields */
290     alltypes.rep_int32.funcs.decode = &read_repeated_varint;
291     alltypes.rep_int32.arg = rep_int32;
292     
293     alltypes.rep_int64.funcs.decode = &read_repeated_varint;
294     alltypes.rep_int64.arg = rep_int64;
295     
296     alltypes.rep_uint32.funcs.decode = &read_repeated_varint;
297     alltypes.rep_uint32.arg = rep_uint32;
298     
299     alltypes.rep_uint64.funcs.decode = &read_repeated_varint;
300     alltypes.rep_uint64.arg = rep_uint64;
301     
302     alltypes.rep_sint32.funcs.decode = &read_repeated_svarint;
303     alltypes.rep_sint32.arg = rep_sint32;
304     
305     alltypes.rep_sint64.funcs.decode = &read_repeated_svarint;
306     alltypes.rep_sint64.arg = rep_sint64;
307     
308     alltypes.rep_bool.funcs.decode = &read_repeated_varint;
309     alltypes.rep_bool.arg = rep_bool;
310     
311     alltypes.rep_fixed32.funcs.decode = &read_repeated_fixed32;
312     alltypes.rep_fixed32.arg = rep_fixed32;
313     
314     alltypes.rep_sfixed32.funcs.decode = &read_repeated_fixed32;
315     alltypes.rep_sfixed32.arg = rep_sfixed32;
316     
317     alltypes.rep_float.funcs.decode = &read_repeated_fixed32;
318     alltypes.rep_float.arg = rep_float;
319     
320     alltypes.rep_fixed64.funcs.decode = &read_repeated_fixed64;
321     alltypes.rep_fixed64.arg = rep_fixed64;
322     
323     alltypes.rep_sfixed64.funcs.decode = &read_repeated_fixed64;
324     alltypes.rep_sfixed64.arg = rep_sfixed64;
325     
326     alltypes.rep_double.funcs.decode = &read_repeated_fixed64;
327     alltypes.rep_double.arg = rep_double;
328     
329     alltypes.rep_string.funcs.decode = &read_repeated_string;
330     alltypes.rep_string.arg = rep_string;
331     
332     alltypes.rep_bytes.funcs.decode = &read_repeated_string;
333     alltypes.rep_bytes.arg = rep_bytes;
334     
335     alltypes.rep_submsg.funcs.decode = &read_repeated_submsg;
336     alltypes.rep_submsg.arg = rep_submsg;
337     
338     alltypes.rep_enum.funcs.decode = &read_repeated_varint;
339     alltypes.rep_enum.arg = rep_enum;
340     
341     alltypes.rep_emptymsg.funcs.decode = &read_emptymsg;
342     
343     alltypes.req_limits.funcs.decode = &read_limits;
344     
345     alltypes.end.funcs.decode = &read_varint;
346     alltypes.end.arg = (void*)1099;
347     
348     /* Bind callbacks for optional fields */
349     if (mode == 1)
350     {
351         alltypes.opt_int32.funcs.decode = &read_varint;
352         alltypes.opt_int32.arg = (void*)3041;
353         
354         alltypes.opt_int64.funcs.decode = &read_varint;
355         alltypes.opt_int64.arg = (void*)3042;
356         
357         alltypes.opt_uint32.funcs.decode = &read_varint;
358         alltypes.opt_uint32.arg = (void*)3043;
359         
360         alltypes.opt_uint64.funcs.decode = &read_varint;
361         alltypes.opt_uint64.arg = (void*)3044;
362         
363         alltypes.opt_sint32.funcs.decode = &read_svarint;
364         alltypes.opt_sint32.arg = (void*)3045;
365         
366         alltypes.opt_sint64.funcs.decode = &read_svarint;
367         alltypes.opt_sint64.arg = (void*)3046;
368         
369         alltypes.opt_bool.funcs.decode = &read_varint;
370         alltypes.opt_bool.arg = (void*)true;
371
372         alltypes.opt_fixed32.funcs.decode = &read_fixed32;
373         alltypes.opt_fixed32.arg = &opt_fixed32;
374         
375         alltypes.opt_sfixed32.funcs.decode = &read_fixed32;
376         alltypes.opt_sfixed32.arg = &opt_sfixed32;
377         
378         alltypes.opt_float.funcs.decode = &read_fixed32;
379         alltypes.opt_float.arg = &opt_float;
380         
381         alltypes.opt_fixed64.funcs.decode = &read_fixed64;
382         alltypes.opt_fixed64.arg = &opt_fixed64;
383         
384         alltypes.opt_sfixed64.funcs.decode = &read_fixed64;
385         alltypes.opt_sfixed64.arg = &opt_sfixed64;
386         
387         alltypes.opt_double.funcs.decode = &read_fixed64;
388         alltypes.opt_double.arg = &opt_double;
389         
390         alltypes.opt_string.funcs.decode = &read_string;
391         alltypes.opt_string.arg = "3054";
392         
393         alltypes.opt_bytes.funcs.decode = &read_string;
394         alltypes.opt_bytes.arg = "3055";
395         
396         alltypes.opt_submsg.funcs.decode = &read_submsg;
397         alltypes.opt_submsg.arg = &opt_submsg;
398         
399         alltypes.opt_enum.funcs.decode = &read_varint;
400         alltypes.opt_enum.arg = (void*)MyEnum_Truth;
401         
402         alltypes.opt_emptymsg.funcs.decode = &read_emptymsg;
403
404         alltypes.oneof_msg1.funcs.decode = &read_submsg;
405         alltypes.oneof_msg1.arg = &oneof_msg1;
406     }
407     
408     status = pb_decode(stream, AllTypes_fields, &alltypes);
409     
410 #ifdef PB_ENABLE_MALLOC
411     /* Just to check for any interference between pb_release() and callback fields */
412     pb_release(AllTypes_fields, &alltypes);
413 #endif
414
415     return status;
416 }
417
418 int main(int argc, char **argv)
419 {
420     uint8_t buffer[1024];
421     size_t count;
422     pb_istream_t stream;
423
424     /* Whether to expect the optional values or the default values. */
425     int mode = (argc > 1) ? atoi(argv[1]) : 0;
426     
427     /* Read the data into buffer */
428     SET_BINARY_MODE(stdin);
429     count = fread(buffer, 1, sizeof(buffer), stdin);
430     
431     /* Construct a pb_istream_t for reading from the buffer */
432     stream = pb_istream_from_buffer(buffer, count);
433     
434     /* Decode and print out the stuff */
435     if (!check_alltypes(&stream, mode))
436     {
437         printf("Parsing failed: %s\n", PB_GET_ERROR(&stream));
438         return 1;
439     } else {
440         return 0;
441     }
442 }