47 uint8_t buffer1[] =
"foobartest1234";
48 uint8_t buffer2[
sizeof(buffer1)];
51 COMMENT(
"Test pb_read and pb_istream_t");
54 TEST(stream.bytes_left ==
sizeof(buffer1) - 6)
55 TEST(
pb_read(&stream, buffer2 + 6, stream.bytes_left))
56 TEST(
memcmp(buffer1, buffer2,
sizeof(buffer1)) == 0)
57 TEST(stream.bytes_left == 0)
65 COMMENT(
"Test pb_read with custom callback");
69 stream.state = (
void*)1;
80 COMMENT(
"Test pb_decode_varint");
86 TEST((s =
S(
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"),
88 TEST((s =
S(
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"),
90 TEST((s =
S(
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"),
98 COMMENT(
"Test pb_decode_varint32");
112 COMMENT(
"Test pb_skip_varint");
115 TEST((s =
S(
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01""foobar"),
130 pb_istream_t s =
S(
"\x01\x00");
135 f.data_size =
sizeof(d);
138 COMMENT(
"Test pb_dec_varint using uint32_t")
153 f.data_size =
sizeof(d);
156 COMMENT(
"Test pb_dec_varint using sint32_t")
169 f.data_size =
sizeof(d);
172 COMMENT(
"Test pb_dec_varint using sint64_t")
175 TEST((s =
S(
"\xFE\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"),
pb_dec_varint(&s, &f) && d == INT64_MAX))
176 TEST((s =
S(
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"),
pb_dec_varint(&s, &f) && d == INT64_MIN))
185 f.data_size =
sizeof(d);
188 COMMENT(
"Test pb_dec_varint overflow detection using sint32_t");
201 f.data_size =
sizeof(d);
204 COMMENT(
"Test pb_dec_varint using uint32_t")
216 f.data_size =
sizeof(d);
219 COMMENT(
"Test pb_dec_varint using uint64_t")
222 TEST((s =
S(
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"),
pb_dec_varint(&s, &f) && d == UINT64_MAX))
231 f.data_size =
sizeof(d);
234 COMMENT(
"Test pb_dec_varint overflow detection using uint32_t");
243 COMMENT(
"Test pb_dec_fixed using float (failures here may be caused by imperfect rounding)")
251 if (
sizeof(
double) == 8)
256 COMMENT(
"Test pb_dec_fixed64 using double (failures here may be caused by imperfect rounding)")
268 f.data_size =
sizeof(d);
273 TEST((s =
S(
"\x01\xFF"),
pb_dec_bytes(&s, &f) && d.size == 1 && d.bytes[0] == 0xFF))
293 f.data_size =
sizeof(d);
306 COMMENT(
"Testing pb_decode with repeated int32 field")
307 TEST((s =
S(
""),
pb_decode(&s, IntegerArray_fields, &dest) && dest.data_count == 0))
308 TEST((s =
S(
"\x08\x01\x08\x02"),
pb_decode(&s, IntegerArray_fields, &dest)
309 && dest.data_count == 2 && dest.data[0] == 1 && dest.data[1] == 2))
310 s =
S(
"\x08\x01\x08\x02\x08\x03\x08\x04\x08\x05\x08\x06\x08\x07\x08\x08\x08\x09\x08\x0A");
311 TEST(
pb_decode(&s, IntegerArray_fields, &dest) && dest.data_count == 10 && dest.data[9] == 10)
312 s =
S(
"\x08\x01\x08\x02\x08\x03\x08\x04\x08\x05\x08\x06\x08\x07\x08\x08\x08\x09\x08\x0A\x08\x0B");
320 COMMENT(
"Testing pb_decode with packed int32 field")
321 TEST((s =
S(
"\x0A\x00"),
pb_decode(&s, IntegerArray_fields, &dest)
322 && dest.data_count == 0))
323 TEST((s =
S(
"\x0A\x01\x01"),
pb_decode(&s, IntegerArray_fields, &dest)
324 && dest.data_count == 1 && dest.data[0] == 1))
325 TEST((s =
S(
"\x0A\x0A\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A"),
pb_decode(&s, IntegerArray_fields, &dest)
326 && dest.data_count == 10 && dest.data[0] == 1 && dest.data[9] == 10))
327 TEST((s =
S(
"\x0A\x0B\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B"), !
pb_decode(&s, IntegerArray_fields, &dest)))
330 TEST((s =
S(
"\x0A\xFF"), !
pb_decode(&s, IntegerArray_fields, &dest)))
331 TEST((s =
S(
"\x0A\x01"), !
pb_decode(&s, IntegerArray_fields, &dest)))
338 COMMENT(
"Testing pb_decode with unknown fields")
339 TEST((s =
S(
"\x18\x0F\x08\x01"),
pb_decode(&s, IntegerArray_fields, &dest)
340 && dest.data_count == 1 && dest.data[0] == 1))
341 TEST((s =
S(
"\x19\x00\x00\x00\x00\x00\x00\x00\x00\x08\x01"),
pb_decode(&s, IntegerArray_fields, &dest)
342 && dest.data_count == 1 && dest.data[0] == 1))
343 TEST((s =
S(
"\x1A\x00\x08\x01"),
pb_decode(&s, IntegerArray_fields, &dest)
344 && dest.data_count == 1 && dest.data[0] == 1))
345 TEST((s =
S(
"\x1B\x08\x01"), !
pb_decode(&s, IntegerArray_fields, &dest)))
346 TEST((s =
S(
"\x1D\x00\x00\x00\x00\x08\x01"),
pb_decode(&s, IntegerArray_fields, &dest)
347 && dest.data_count == 1 && dest.data[0] == 1))
355 dest.data.arg = &ref;
357 COMMENT(
"Testing pb_decode with callbacks")
359 ref.size = 1; ref.bytes[0] = 0x55;
360 TEST((s =
S(
"\x08\x55"),
pb_decode(&s, CallbackArray_fields, &dest)))
362 ref.size = 3; ref.bytes[0] = ref.bytes[1] = ref.bytes[2] = 0x55;
363 TEST((s =
S(
"\x0A\x03\x55\x55\x55"),
pb_decode(&s, CallbackArray_fields, &dest)))
365 ref.size = 1; ref.bytes[0] = 0x55;
366 TEST((s =
S(
"\x0A\x03\x55\x55\x55"),
pb_decode(&s, CallbackArray_fields, &dest)))
368 ref.size = 4; ref.bytes[0] = ref.bytes[1] = ref.bytes[2] = ref.bytes[3] = 0xAA;
369 TEST((s =
S(
"\x0D\xAA\xAA\xAA\xAA"),
pb_decode(&s, CallbackArray_fields, &dest)))
371 ref.size = 8;
memset(ref.bytes, 0xAA, 8);
372 TEST((s =
S(
"\x09\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"),
pb_decode(&s, CallbackArray_fields, &dest)))
374 TEST((s =
S(
"\x0B\x00"), !
pb_decode(&s, CallbackArray_fields, &dest)))
377 ref.size = 1; ref.bytes[0] = 0x56;
378 TEST((s =
S(
"\x08\x55"), !
pb_decode(&s, CallbackArray_fields, &dest)))
385 COMMENT(
"Testing pb_decode message termination")
387 TEST((s =
S(
"\x08\x01"),
pb_decode(&s, IntegerArray_fields, &dest)))
395 COMMENT(
"Testing pb_decode_ex null termination")
405 COMMENT(
"Testing pb_decode with invalid tag numbers")
406 TEST((s =
S(
"\x9f\xea"), !
pb_decode(&s, IntegerArray_fields, &dest)));
407 TEST((s =
S(
"\x00"), !
pb_decode(&s, IntegerArray_fields, &dest)));
412 IntegerContainer dest = {{0}};
414 COMMENT(
"Testing pb_decode_delimited")
415 TEST((s =
S(
"\x09\x0A\x07\x0A\x05\x01\x02\x03\x04\x05"),
417 dest.submsg.data_count == 5)
421 pb_istream_t s = {0};
424 COMMENT(
"Testing allocate_field")
425 TEST(allocate_field(&s, &data, 10, 10) && data !=
NULL);
426 TEST(allocate_field(&s, &data, 10, 20) && data !=
NULL);
429 void *oldvalue = data;
430 size_t very_big = (
size_t)-1;
431 size_t somewhat_big = very_big / 2 + 1;
432 size_t not_so_big = (
size_t)1 << (4 *
sizeof(
size_t));
434 TEST(!allocate_field(&s, &data, very_big, 2) && data == oldvalue);
435 TEST(!allocate_field(&s, &data, somewhat_big, 2) && data == oldvalue);
436 TEST(!allocate_field(&s, &data, not_so_big, not_so_big) && data == oldvalue);
443 fprintf(
stdout,
"\n\nSome tests FAILED!\n");