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