decode_oneof.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* Decode a message using oneof fields */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <pb_decode.h>
  6. #include "oneof.pb.h"
  7. #include "test_helpers.h"
  8. #include "unittests.h"
  9. /* Test the 'OneOfMessage' */
  10. int test_oneof_1(pb_istream_t *stream, int option)
  11. {
  12. OneOfMessage msg;
  13. int status = 0;
  14. /* To better catch initialization errors */
  15. memset(&msg, 0xAA, sizeof(msg));
  16. if (!pb_decode(stream, OneOfMessage_fields, &msg))
  17. {
  18. printf("Decoding failed: %s\n", PB_GET_ERROR(stream));
  19. return 1;
  20. }
  21. /* Check that the basic fields work normally */
  22. TEST(msg.prefix == 123);
  23. TEST(msg.suffix == 321);
  24. /* Check that we got the right oneof according to command line */
  25. if (option == 1)
  26. {
  27. TEST(msg.which_values == OneOfMessage_first_tag);
  28. TEST(msg.values.first == 999);
  29. }
  30. else if (option == 2)
  31. {
  32. TEST(msg.which_values == OneOfMessage_second_tag);
  33. TEST(strcmp(msg.values.second, "abcd") == 0);
  34. }
  35. else if (option == 3)
  36. {
  37. TEST(msg.which_values == OneOfMessage_third_tag);
  38. TEST(msg.values.third.array[0] == 1);
  39. TEST(msg.values.third.array[1] == 2);
  40. TEST(msg.values.third.array[2] == 3);
  41. TEST(msg.values.third.array[3] == 4);
  42. TEST(msg.values.third.array[4] == 5);
  43. }
  44. return status;
  45. }
  46. /* Test the 'PlainOneOfMessage' */
  47. int test_oneof_2(pb_istream_t *stream, int option)
  48. {
  49. PlainOneOfMessage msg = PlainOneOfMessage_init_zero;
  50. int status = 0;
  51. if (!pb_decode(stream, PlainOneOfMessage_fields, &msg))
  52. {
  53. printf("Decoding failed: %s\n", PB_GET_ERROR(stream));
  54. return 1;
  55. }
  56. /* Check that we got the right oneof according to command line */
  57. if (option == 1)
  58. {
  59. TEST(msg.which_values == OneOfMessage_first_tag);
  60. TEST(msg.values.first == 999);
  61. }
  62. else if (option == 2)
  63. {
  64. TEST(msg.which_values == OneOfMessage_second_tag);
  65. TEST(strcmp(msg.values.second, "abcd") == 0);
  66. }
  67. else if (option == 3)
  68. {
  69. TEST(msg.which_values == OneOfMessage_third_tag);
  70. TEST(msg.values.third.array[0] == 1);
  71. TEST(msg.values.third.array[1] == 2);
  72. TEST(msg.values.third.array[2] == 3);
  73. TEST(msg.values.third.array[3] == 4);
  74. TEST(msg.values.third.array[4] == 5);
  75. }
  76. return status;
  77. }
  78. int main(int argc, char **argv)
  79. {
  80. uint8_t buffer[OneOfMessage_size];
  81. size_t count;
  82. int option;
  83. if (argc != 2)
  84. {
  85. fprintf(stderr, "Usage: decode_oneof [number]\n");
  86. return 1;
  87. }
  88. option = atoi(argv[1]);
  89. SET_BINARY_MODE(stdin);
  90. count = fread(buffer, 1, sizeof(buffer), stdin);
  91. if (!feof(stdin))
  92. {
  93. printf("Message does not fit in buffer\n");
  94. return 1;
  95. }
  96. {
  97. int status = 0;
  98. pb_istream_t stream;
  99. stream = pb_istream_from_buffer(buffer, count);
  100. status = test_oneof_1(&stream, option);
  101. if (status != 0)
  102. return status;
  103. stream = pb_istream_from_buffer(buffer, count);
  104. status = test_oneof_2(&stream, option);
  105. if (status != 0)
  106. return status;
  107. }
  108. return 0;
  109. }