Add fixed length bytes to alltypes test case (issue #244)
[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     
74     if (!pb_decode(stream, SubMessage_fields, &submsg))
75         return false;
76     
77     TEST(memcmp(&submsg, *arg, sizeof(submsg)));
78     return true;
79 }
80
81 static bool read_emptymsg(pb_istream_t *stream, const pb_field_t *field, void **arg)
82 {
83     EmptyMessage emptymsg = {0};
84     return pb_decode(stream, EmptyMessage_fields, &emptymsg);
85 }
86
87 static bool read_repeated_varint(pb_istream_t *stream, const pb_field_t *field, void **arg)
88 {
89     int32_t** expected = (int32_t**)arg;
90     uint64_t value;
91     if (!pb_decode_varint(stream, &value))
92         return false;
93
94     TEST(*(*expected)++ == value);
95     return true;
96 }
97
98 static bool read_repeated_svarint(pb_istream_t *stream, const pb_field_t *field, void **arg)
99 {
100     int32_t** expected = (int32_t**)arg;
101     int64_t value;
102     if (!pb_decode_svarint(stream, &value))
103         return false;
104
105     TEST(*(*expected)++ == value);
106     return true;
107 }
108
109 static bool read_repeated_fixed32(pb_istream_t *stream, const pb_field_t *field, void **arg)
110 {
111     uint32_t** expected = (uint32_t**)arg;
112     uint32_t value;
113     if (!pb_decode_fixed32(stream, &value))
114         return false;
115
116     TEST(*(*expected)++ == value);
117     return true;
118 }
119
120 static bool read_repeated_fixed64(pb_istream_t *stream, const pb_field_t *field, void **arg)
121 {
122     uint64_t** expected = (uint64_t**)arg;
123     uint64_t value;
124     if (!pb_decode_fixed64(stream, &value))
125         return false;
126
127     TEST(*(*expected)++ == value);
128     return true;
129 }
130
131 static bool read_repeated_string(pb_istream_t *stream, const pb_field_t *field, void **arg)
132 {
133     uint8_t*** expected = (uint8_t***)arg;
134     uint8_t buf[16] = {0};
135     size_t len = stream->bytes_left;
136     
137     if (len > sizeof(buf) - 1 || !pb_read(stream, buf, len))
138         return false;
139     
140     TEST(strcmp((char*)*(*expected)++, (char*)buf) == 0);
141     return true;
142 }
143
144 static bool read_repeated_submsg(pb_istream_t *stream, const pb_field_t *field, void **arg)
145 {
146     SubMessage** expected = (SubMessage**)arg;
147     SubMessage decoded = {""};
148     if (!pb_decode(stream, SubMessage_fields, &decoded))
149         return false;
150
151     TEST(memcmp((*expected)++, &decoded, sizeof(decoded)) == 0);
152     return true;
153 }
154
155 static bool read_limits(pb_istream_t *stream, const pb_field_t *field, void **arg)
156 {
157     Limits decoded = {0};
158     if (!pb_decode(stream, Limits_fields, &decoded))
159         return false;
160
161     TEST(decoded.int32_min  == INT32_MIN);
162     TEST(decoded.int32_max  == INT32_MAX);
163     TEST(decoded.uint32_min == 0);
164     TEST(decoded.uint32_max == UINT32_MAX);
165     TEST(decoded.int64_min  == INT64_MIN);
166     TEST(decoded.int64_max  == INT64_MAX);
167     TEST(decoded.uint64_min == 0);
168     TEST(decoded.uint64_max == UINT64_MAX);
169     TEST(decoded.enum_min   == HugeEnum_Negative);
170     TEST(decoded.enum_max   == HugeEnum_Positive);
171     
172     return true;
173 }
174
175 /* This function is called once from main(), it handles
176    the decoding and checks the fields. */
177 bool check_alltypes(pb_istream_t *stream, int mode)
178 {
179     /* Values for use from callbacks through pointers. */
180     uint32_t    req_fixed32     = 1008;
181     int32_t     req_sfixed32    = -1009;
182     float       req_float       = 1010.0f;
183     uint64_t    req_fixed64     = 1011;
184     int64_t     req_sfixed64    = -1012;
185     double      req_double      = 1013.0;
186     SubMessage  req_submsg      = {"1016", 1016};
187     
188     int32_t     rep_int32[5]    = {0, 0, 0, 0, -2001};
189     int32_t     rep_int64[5]    = {0, 0, 0, 0, -2002};
190     int32_t     rep_uint32[5]   = {0, 0, 0, 0,  2003};
191     int32_t     rep_uint64[5]   = {0, 0, 0, 0,  2004};
192     int32_t     rep_sint32[5]   = {0, 0, 0, 0, -2005};
193     int32_t     rep_sint64[5]   = {0, 0, 0, 0, -2006};
194     int32_t     rep_bool[5]     = {false, false, false, false, true};
195     uint32_t    rep_fixed32[5]  = {0, 0, 0, 0,  2008};
196     int32_t     rep_sfixed32[5] = {0, 0, 0, 0, -2009};
197     float       rep_float[5]    = {0, 0, 0, 0,  2010.0f};
198     uint64_t    rep_fixed64[5]  = {0, 0, 0, 0,  2011};
199     int64_t     rep_sfixed64[5] = {0, 0, 0, 0, -2012};
200     double      rep_double[5]   = {0, 0, 0, 0,  2013.0};
201     char*       rep_string[5]   = {"", "", "", "", "2014"};
202     char*       rep_bytes[5]    = {"", "", "", "", "2015"};
203     SubMessage  rep_submsg[5]   = {{"", 0, 0, 3},
204                                    {"", 0, 0, 3},
205                                    {"", 0, 0, 3},
206                                    {"", 0, 0, 3},
207                                    {"2016", 2016, true, 2016}};
208     int32_t     rep_enum[5]     = {0, 0, 0, 0, MyEnum_Truth};
209     
210     uint32_t    opt_fixed32     = 3048;
211     int32_t     opt_sfixed32    = 3049;
212     float       opt_float       = 3050.0f;
213     uint64_t    opt_fixed64     = 3051;
214     int64_t     opt_sfixed64    = 3052;
215     double      opt_double      = 3053.0f;
216     SubMessage  opt_submsg      = {"3056", 3056};
217
218     SubMessage  oneof_msg1      = {"4059", 4059};
219     
220     /* Bind callbacks for required fields */
221     AllTypes alltypes = AllTypes_init_zero;
222     
223     alltypes.req_int32.funcs.decode = &read_varint;
224     alltypes.req_int32.arg = (void*)-1001;
225     
226     alltypes.req_int64.funcs.decode = &read_varint;
227     alltypes.req_int64.arg = (void*)-1002;
228     
229     alltypes.req_uint32.funcs.decode = &read_varint;
230     alltypes.req_uint32.arg = (void*)1003;
231
232     alltypes.req_uint32.funcs.decode = &read_varint;
233     alltypes.req_uint32.arg = (void*)1003;    
234     
235     alltypes.req_uint64.funcs.decode = &read_varint;
236     alltypes.req_uint64.arg = (void*)1004;
237     
238     alltypes.req_sint32.funcs.decode = &read_svarint;
239     alltypes.req_sint32.arg = (void*)-1005;   
240     
241     alltypes.req_sint64.funcs.decode = &read_svarint;
242     alltypes.req_sint64.arg = (void*)-1006;   
243     
244     alltypes.req_bool.funcs.decode = &read_varint;
245     alltypes.req_bool.arg = (void*)true;   
246     
247     alltypes.req_fixed32.funcs.decode = &read_fixed32;
248     alltypes.req_fixed32.arg = &req_fixed32;
249     
250     alltypes.req_sfixed32.funcs.decode = &read_fixed32;
251     alltypes.req_sfixed32.arg = &req_sfixed32;
252     
253     alltypes.req_float.funcs.decode = &read_fixed32;
254     alltypes.req_float.arg = &req_float;
255     
256     alltypes.req_fixed64.funcs.decode = &read_fixed64;
257     alltypes.req_fixed64.arg = &req_fixed64;
258     
259     alltypes.req_sfixed64.funcs.decode = &read_fixed64;
260     alltypes.req_sfixed64.arg = &req_sfixed64;
261     
262     alltypes.req_double.funcs.decode = &read_fixed64;
263     alltypes.req_double.arg = &req_double;
264     
265     alltypes.req_string.funcs.decode = &read_string;
266     alltypes.req_string.arg = "1014";
267     
268     alltypes.req_bytes.funcs.decode = &read_string;
269     alltypes.req_bytes.arg = "1015";
270     
271     alltypes.req_submsg.funcs.decode = &read_submsg;
272     alltypes.req_submsg.arg = &req_submsg;
273     
274     alltypes.req_enum.funcs.decode = &read_varint;
275     alltypes.req_enum.arg = (void*)MyEnum_Truth;
276     
277     alltypes.req_emptymsg.funcs.decode = &read_emptymsg;
278     
279     /* Bind callbacks for repeated fields */
280     alltypes.rep_int32.funcs.decode = &read_repeated_varint;
281     alltypes.rep_int32.arg = rep_int32;
282     
283     alltypes.rep_int64.funcs.decode = &read_repeated_varint;
284     alltypes.rep_int64.arg = rep_int64;
285     
286     alltypes.rep_uint32.funcs.decode = &read_repeated_varint;
287     alltypes.rep_uint32.arg = rep_uint32;
288     
289     alltypes.rep_uint64.funcs.decode = &read_repeated_varint;
290     alltypes.rep_uint64.arg = rep_uint64;
291     
292     alltypes.rep_sint32.funcs.decode = &read_repeated_svarint;
293     alltypes.rep_sint32.arg = rep_sint32;
294     
295     alltypes.rep_sint64.funcs.decode = &read_repeated_svarint;
296     alltypes.rep_sint64.arg = rep_sint64;
297     
298     alltypes.rep_bool.funcs.decode = &read_repeated_varint;
299     alltypes.rep_bool.arg = rep_bool;
300     
301     alltypes.rep_fixed32.funcs.decode = &read_repeated_fixed32;
302     alltypes.rep_fixed32.arg = rep_fixed32;
303     
304     alltypes.rep_sfixed32.funcs.decode = &read_repeated_fixed32;
305     alltypes.rep_sfixed32.arg = rep_sfixed32;
306     
307     alltypes.rep_float.funcs.decode = &read_repeated_fixed32;
308     alltypes.rep_float.arg = rep_float;
309     
310     alltypes.rep_fixed64.funcs.decode = &read_repeated_fixed64;
311     alltypes.rep_fixed64.arg = rep_fixed64;
312     
313     alltypes.rep_sfixed64.funcs.decode = &read_repeated_fixed64;
314     alltypes.rep_sfixed64.arg = rep_sfixed64;
315     
316     alltypes.rep_double.funcs.decode = &read_repeated_fixed64;
317     alltypes.rep_double.arg = rep_double;
318     
319     alltypes.rep_string.funcs.decode = &read_repeated_string;
320     alltypes.rep_string.arg = rep_string;
321     
322     alltypes.rep_bytes.funcs.decode = &read_repeated_string;
323     alltypes.rep_bytes.arg = rep_bytes;
324     
325     alltypes.rep_submsg.funcs.decode = &read_repeated_submsg;
326     alltypes.rep_submsg.arg = rep_submsg;
327     
328     alltypes.rep_enum.funcs.decode = &read_repeated_varint;
329     alltypes.rep_enum.arg = rep_enum;
330     
331     alltypes.rep_emptymsg.funcs.decode = &read_emptymsg;
332     
333     alltypes.req_limits.funcs.decode = &read_limits;
334     
335     alltypes.end.funcs.decode = &read_varint;
336     alltypes.end.arg = (void*)1099;
337     
338     /* Bind callbacks for optional fields */
339     if (mode == 1)
340     {
341         alltypes.opt_int32.funcs.decode = &read_varint;
342         alltypes.opt_int32.arg = (void*)3041;
343         
344         alltypes.opt_int64.funcs.decode = &read_varint;
345         alltypes.opt_int64.arg = (void*)3042;
346         
347         alltypes.opt_uint32.funcs.decode = &read_varint;
348         alltypes.opt_uint32.arg = (void*)3043;
349         
350         alltypes.opt_uint64.funcs.decode = &read_varint;
351         alltypes.opt_uint64.arg = (void*)3044;
352         
353         alltypes.opt_sint32.funcs.decode = &read_svarint;
354         alltypes.opt_sint32.arg = (void*)3045;
355         
356         alltypes.opt_sint64.funcs.decode = &read_svarint;
357         alltypes.opt_sint64.arg = (void*)3046;
358         
359         alltypes.opt_bool.funcs.decode = &read_varint;
360         alltypes.opt_bool.arg = (void*)true;
361
362         alltypes.opt_fixed32.funcs.decode = &read_fixed32;
363         alltypes.opt_fixed32.arg = &opt_fixed32;
364         
365         alltypes.opt_sfixed32.funcs.decode = &read_fixed32;
366         alltypes.opt_sfixed32.arg = &opt_sfixed32;
367         
368         alltypes.opt_float.funcs.decode = &read_fixed32;
369         alltypes.opt_float.arg = &opt_float;
370         
371         alltypes.opt_fixed64.funcs.decode = &read_fixed64;
372         alltypes.opt_fixed64.arg = &opt_fixed64;
373         
374         alltypes.opt_sfixed64.funcs.decode = &read_fixed64;
375         alltypes.opt_sfixed64.arg = &opt_sfixed64;
376         
377         alltypes.opt_double.funcs.decode = &read_fixed64;
378         alltypes.opt_double.arg = &opt_double;
379         
380         alltypes.opt_string.funcs.decode = &read_string;
381         alltypes.opt_string.arg = "3054";
382         
383         alltypes.opt_bytes.funcs.decode = &read_string;
384         alltypes.opt_bytes.arg = "3055";
385         
386         alltypes.opt_submsg.funcs.decode = &read_submsg;
387         alltypes.opt_submsg.arg = &opt_submsg;
388         
389         alltypes.opt_enum.funcs.decode = &read_varint;
390         alltypes.opt_enum.arg = (void*)MyEnum_Truth;
391         
392         alltypes.opt_emptymsg.funcs.decode = &read_emptymsg;
393
394         alltypes.oneof_msg1.funcs.decode = &read_submsg;
395         alltypes.oneof_msg1.arg = &oneof_msg1;
396     }
397     
398     return pb_decode(stream, AllTypes_fields, &alltypes);
399 }
400
401 int main(int argc, char **argv)
402 {
403     uint8_t buffer[1024];
404     size_t count;
405     pb_istream_t stream;
406
407     /* Whether to expect the optional values or the default values. */
408     int mode = (argc > 1) ? atoi(argv[1]) : 0;
409     
410     /* Read the data into buffer */
411     SET_BINARY_MODE(stdin);
412     count = fread(buffer, 1, sizeof(buffer), stdin);
413     
414     /* Construct a pb_istream_t for reading from the buffer */
415     stream = pb_istream_from_buffer(buffer, count);
416     
417     /* Decode and print out the stuff */
418     if (!check_alltypes(&stream, mode))
419     {
420         printf("Parsing failed: %s\n", PB_GET_ERROR(&stream));
421         return 1;
422     } else {
423         return 0;
424     }
425 }