decode_extensions.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* Test decoding of extension fields. */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <pb_decode.h>
  6. #include "alltypes.pb.h"
  7. #include "extensions.pb.h"
  8. #include "test_helpers.h"
  9. #include "unittests.h"
  10. int main(int argc, char **argv)
  11. {
  12. uint8_t buffer[1024];
  13. size_t count;
  14. pb_istream_t stream;
  15. int status = 0;
  16. AllTypes alltypes = AllTypes_init_zero;
  17. int32_t extensionfield1;
  18. pb_extension_t ext1 = pb_extension_init_zero;
  19. ExtensionMessage extensionfield2 = ExtensionMessage_init_zero;
  20. pb_extension_t ext2 = pb_extension_init_zero;
  21. /* Read the message data */
  22. SET_BINARY_MODE(stdin);
  23. count = fread(buffer, 1, sizeof(buffer), stdin);
  24. stream = pb_istream_from_buffer(buffer, count);
  25. /* Add the extensions */
  26. alltypes.extensions = &ext1;
  27. ext1.type = &AllTypes_extensionfield1;
  28. ext1.dest = &extensionfield1;
  29. ext1.next = &ext2;
  30. ext2.type = &ExtensionMessage_AllTypes_extensionfield2;
  31. ext2.dest = &extensionfield2;
  32. ext2.next = NULL;
  33. /* Decode the message */
  34. if (!pb_decode(&stream, AllTypes_fields, &alltypes))
  35. {
  36. printf("Parsing failed: %s\n", PB_GET_ERROR(&stream));
  37. return 1;
  38. }
  39. /* Check that the extensions decoded properly */
  40. TEST(ext1.found)
  41. TEST(extensionfield1 == 12345)
  42. TEST(ext2.found)
  43. TEST(strcmp(extensionfield2.test1, "test") == 0)
  44. TEST(extensionfield2.test2 == 54321)
  45. return status;
  46. }