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