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.
10 #include "alltypes.pb.h"
11 #include "test_helpers.h"
13 #define TEST(x) if (!(x)) { \
14 printf("Test " #x " failed (in field %d).\n", field->tag); \
18 static bool read_varint(pb_istream_t *stream, const pb_field_t *field, void **arg)
21 if (!pb_decode_varint(stream, &value))
24 TEST((int64_t)value == (long)*arg);
28 static bool read_svarint(pb_istream_t *stream, const pb_field_t *field, void **arg)
31 if (!pb_decode_svarint(stream, &value))
34 TEST(value == (long)*arg);
38 static bool read_fixed32(pb_istream_t *stream, const pb_field_t *field, void **arg)
41 if (!pb_decode_fixed32(stream, &value))
44 TEST(value == *(uint32_t*)*arg);
48 static bool read_fixed64(pb_istream_t *stream, const pb_field_t *field, void **arg)
51 if (!pb_decode_fixed64(stream, &value))
54 TEST(value == *(uint64_t*)*arg);
58 static bool read_string(pb_istream_t *stream, const pb_field_t *field, void **arg)
60 uint8_t buf[16] = {0};
61 size_t len = stream->bytes_left;
63 if (len > sizeof(buf) - 1 || !pb_read(stream, buf, len))
66 TEST(strcmp((char*)buf, *arg) == 0);
70 static bool read_submsg(pb_istream_t *stream, const pb_field_t *field, void **arg)
72 SubMessage submsg = {""};
73 SubMessage *ref = *arg;
75 if (!pb_decode(stream, SubMessage_fields, &submsg))
78 TEST(strcmp(submsg.substuff1, ref->substuff1) == 0);
79 TEST(submsg.substuff2 == ref->substuff2);
80 TEST(submsg.substuff3 == ref->substuff3);
84 static bool read_emptymsg(pb_istream_t *stream, const pb_field_t *field, void **arg)
86 EmptyMessage emptymsg = {0};
87 return pb_decode(stream, EmptyMessage_fields, &emptymsg);
90 static bool read_repeated_varint(pb_istream_t *stream, const pb_field_t *field, void **arg)
92 int32_t** expected = (int32_t**)arg;
94 if (!pb_decode_varint(stream, &value))
97 TEST(*(*expected)++ == value);
101 static bool read_repeated_svarint(pb_istream_t *stream, const pb_field_t *field, void **arg)
103 int32_t** expected = (int32_t**)arg;
105 if (!pb_decode_svarint(stream, &value))
108 TEST(*(*expected)++ == value);
112 static bool read_repeated_fixed32(pb_istream_t *stream, const pb_field_t *field, void **arg)
114 uint32_t** expected = (uint32_t**)arg;
116 if (!pb_decode_fixed32(stream, &value))
119 TEST(*(*expected)++ == value);
123 static bool read_repeated_fixed64(pb_istream_t *stream, const pb_field_t *field, void **arg)
125 uint64_t** expected = (uint64_t**)arg;
127 if (!pb_decode_fixed64(stream, &value))
130 TEST(*(*expected)++ == value);
134 static bool read_repeated_string(pb_istream_t *stream, const pb_field_t *field, void **arg)
136 uint8_t*** expected = (uint8_t***)arg;
137 uint8_t buf[16] = {0};
138 size_t len = stream->bytes_left;
140 if (len > sizeof(buf) - 1 || !pb_read(stream, buf, len))
143 TEST(strcmp((char*)*(*expected)++, (char*)buf) == 0);
147 static bool read_repeated_submsg(pb_istream_t *stream, const pb_field_t *field, void **arg)
149 SubMessage** expected = (SubMessage**)arg;
150 SubMessage submsg = {""};
151 if (!pb_decode(stream, SubMessage_fields, &submsg))
154 TEST(strcmp(submsg.substuff1, (*expected)->substuff1) == 0);
155 TEST(submsg.substuff2 == (*expected)->substuff2);
156 TEST(submsg.substuff3 == (*expected)->substuff3);
162 static bool read_limits(pb_istream_t *stream, const pb_field_t *field, void **arg)
164 Limits decoded = {0};
165 if (!pb_decode(stream, Limits_fields, &decoded))
168 TEST(decoded.int32_min == INT32_MIN);
169 TEST(decoded.int32_max == INT32_MAX);
170 TEST(decoded.uint32_min == 0);
171 TEST(decoded.uint32_max == UINT32_MAX);
172 TEST(decoded.int64_min == INT64_MIN);
173 TEST(decoded.int64_max == INT64_MAX);
174 TEST(decoded.uint64_min == 0);
175 TEST(decoded.uint64_max == UINT64_MAX);
176 TEST(decoded.enum_min == HugeEnum_Negative);
177 TEST(decoded.enum_max == HugeEnum_Positive);
182 /* This function is called once from main(), it handles
183 the decoding and checks the fields. */
184 bool check_alltypes(pb_istream_t *stream, int mode)
186 /* Values for use from callbacks through pointers. */
189 int32_t rep_int32[5] = {0, 0, 0, 0, -2001};
190 int32_t rep_int64[5] = {0, 0, 0, 0, -2002};
191 int32_t rep_uint32[5] = {0, 0, 0, 0, 2003};
192 int32_t rep_uint64[5] = {0, 0, 0, 0, 2004};
193 int32_t rep_sint32[5] = {0, 0, 0, 0, -2005};
194 int32_t rep_sint64[5] = {0, 0, 0, 0, -2006};
195 int32_t rep_bool[5] = {false, false, false, false, true};
196 uint32_t rep_fixed32[5] = {0, 0, 0, 0, 2008};
197 int32_t rep_sfixed32[5] = {0, 0, 0, 0, -2009};
198 float rep_float[5] = {0, 0, 0, 0, 2010.0f};
199 uint64_t rep_fixed64[5] = {0, 0, 0, 0, 2011};
200 int64_t rep_sfixed64[5] = {0, 0, 0, 0, -2012};
201 double rep_double[5] = {0, 0, 0, 0, 2013.0};
202 char* rep_string[5] = {"", "", "", "", "2014"};
203 char* rep_bytes[5] = {"", "", "", "", "2015"};
204 SubMessage rep_submsg[5] = {{"", 0, 0},
208 {"2016", 2016, 2016}};
209 int32_t rep_enum[5] = {0, 0, 0, 0, MyEnum_Truth};
211 uint32_t sng_fixed32 = 3048;
212 int32_t sng_sfixed32 = 3049;
213 float sng_float = 3050.0f;
214 uint64_t sng_fixed64 = 3051;
215 int64_t sng_sfixed64 = 3052;
216 double sng_double = 3053.0f;
217 SubMessage sng_submsg = {"3056", 3056};
219 SubMessage oneof_msg1 = {"4059", 4059};
221 AllTypes alltypes = AllTypes_init_zero;
223 /* Bind callbacks for repeated fields */
224 alltypes.rep_int32.funcs.decode = &read_repeated_varint;
225 alltypes.rep_int32.arg = rep_int32;
227 alltypes.rep_int64.funcs.decode = &read_repeated_varint;
228 alltypes.rep_int64.arg = rep_int64;
230 alltypes.rep_uint32.funcs.decode = &read_repeated_varint;
231 alltypes.rep_uint32.arg = rep_uint32;
233 alltypes.rep_uint64.funcs.decode = &read_repeated_varint;
234 alltypes.rep_uint64.arg = rep_uint64;
236 alltypes.rep_sint32.funcs.decode = &read_repeated_svarint;
237 alltypes.rep_sint32.arg = rep_sint32;
239 alltypes.rep_sint64.funcs.decode = &read_repeated_svarint;
240 alltypes.rep_sint64.arg = rep_sint64;
242 alltypes.rep_bool.funcs.decode = &read_repeated_varint;
243 alltypes.rep_bool.arg = rep_bool;
245 alltypes.rep_fixed32.funcs.decode = &read_repeated_fixed32;
246 alltypes.rep_fixed32.arg = rep_fixed32;
248 alltypes.rep_sfixed32.funcs.decode = &read_repeated_fixed32;
249 alltypes.rep_sfixed32.arg = rep_sfixed32;
251 alltypes.rep_float.funcs.decode = &read_repeated_fixed32;
252 alltypes.rep_float.arg = rep_float;
254 alltypes.rep_fixed64.funcs.decode = &read_repeated_fixed64;
255 alltypes.rep_fixed64.arg = rep_fixed64;
257 alltypes.rep_sfixed64.funcs.decode = &read_repeated_fixed64;
258 alltypes.rep_sfixed64.arg = rep_sfixed64;
260 alltypes.rep_double.funcs.decode = &read_repeated_fixed64;
261 alltypes.rep_double.arg = rep_double;
263 alltypes.rep_string.funcs.decode = &read_repeated_string;
264 alltypes.rep_string.arg = rep_string;
266 alltypes.rep_bytes.funcs.decode = &read_repeated_string;
267 alltypes.rep_bytes.arg = rep_bytes;
269 alltypes.rep_submsg.funcs.decode = &read_repeated_submsg;
270 alltypes.rep_submsg.arg = rep_submsg;
272 alltypes.rep_enum.funcs.decode = &read_repeated_varint;
273 alltypes.rep_enum.arg = rep_enum;
275 alltypes.rep_emptymsg.funcs.decode = &read_emptymsg;
277 alltypes.req_limits.funcs.decode = &read_limits;
279 alltypes.end.funcs.decode = &read_varint;
280 alltypes.end.arg = (void*)1099;
282 /* Bind callbacks for optional fields */
285 alltypes.sng_int32.funcs.decode = &read_varint;
286 alltypes.sng_int32.arg = (void*)3041;
288 alltypes.sng_int64.funcs.decode = &read_varint;
289 alltypes.sng_int64.arg = (void*)3042;
291 alltypes.sng_uint32.funcs.decode = &read_varint;
292 alltypes.sng_uint32.arg = (void*)3043;
294 alltypes.sng_uint64.funcs.decode = &read_varint;
295 alltypes.sng_uint64.arg = (void*)3044;
297 alltypes.sng_sint32.funcs.decode = &read_svarint;
298 alltypes.sng_sint32.arg = (void*)3045;
300 alltypes.sng_sint64.funcs.decode = &read_svarint;
301 alltypes.sng_sint64.arg = (void*)3046;
303 alltypes.sng_bool.funcs.decode = &read_varint;
304 alltypes.sng_bool.arg = (void*)true;
306 alltypes.sng_fixed32.funcs.decode = &read_fixed32;
307 alltypes.sng_fixed32.arg = &sng_fixed32;
309 alltypes.sng_sfixed32.funcs.decode = &read_fixed32;
310 alltypes.sng_sfixed32.arg = &sng_sfixed32;
312 alltypes.sng_float.funcs.decode = &read_fixed32;
313 alltypes.sng_float.arg = &sng_float;
315 alltypes.sng_fixed64.funcs.decode = &read_fixed64;
316 alltypes.sng_fixed64.arg = &sng_fixed64;
318 alltypes.sng_sfixed64.funcs.decode = &read_fixed64;
319 alltypes.sng_sfixed64.arg = &sng_sfixed64;
321 alltypes.sng_double.funcs.decode = &read_fixed64;
322 alltypes.sng_double.arg = &sng_double;
324 alltypes.sng_string.funcs.decode = &read_string;
325 alltypes.sng_string.arg = "3054";
327 alltypes.sng_bytes.funcs.decode = &read_string;
328 alltypes.sng_bytes.arg = "3055";
330 alltypes.sng_submsg.funcs.decode = &read_submsg;
331 alltypes.sng_submsg.arg = &sng_submsg;
333 alltypes.sng_enum.funcs.decode = &read_varint;
334 alltypes.sng_enum.arg = (void*)MyEnum_Truth;
336 alltypes.sng_emptymsg.funcs.decode = &read_emptymsg;
338 alltypes.oneof_msg1.funcs.decode = &read_submsg;
339 alltypes.oneof_msg1.arg = &oneof_msg1;
342 status = pb_decode(stream, AllTypes_fields, &alltypes);
344 #ifdef PB_ENABLE_MALLOC
345 /* Just to check for any interference between pb_release() and callback fields */
346 pb_release(AllTypes_fields, &alltypes);
352 int main(int argc, char **argv)
354 uint8_t buffer[1024];
358 /* Whether to expect the optional values or the default values. */
359 int mode = (argc > 1) ? atoi(argv[1]) : 0;
361 /* Read the data into buffer */
362 SET_BINARY_MODE(stdin);
363 count = fread(buffer, 1, sizeof(buffer), stdin);
365 /* Construct a pb_istream_t for reading from the buffer */
366 stream = pb_istream_from_buffer(buffer, count);
368 /* Decode and print out the stuff */
369 if (!check_alltypes(&stream, mode))
371 printf("Parsing failed: %s\n", PB_GET_ERROR(&stream));