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 11 #define TEST(x) if (!(x)) { \ 12 printf("Test " #x " failed.\n"); \ 13 return 2; \ 14 } 15 16 int main(int argc, char **argv) 17 { 18 uint8_t buffer[1024]; 19 size_t count; 20 pb_istream_t stream; 21 22 AllTypes alltypes = {0}; 23 int32_t extensionfield1; 24 pb_extension_t ext1; 25 ExtensionMessage extensionfield2; 26 pb_extension_t ext2; 27 28 /* Read the message data */ 29 SET_BINARY_MODE(stdin); 30 count = fread(buffer, 1, sizeof(buffer), stdin); 31 stream = pb_istream_from_buffer(buffer, count); 32 33 /* Add the extensions */ 34 alltypes.extensions = &ext1; 35 36 ext1.type = &AllTypes_extensionfield1; 37 ext1.dest = &extensionfield1; 38 ext1.next = &ext2; 39 40 ext2.type = &ExtensionMessage_AllTypes_extensionfield2; 41 ext2.dest = &extensionfield2; 42 ext2.next = NULL; 43 44 /* Decode the message */ 45 if (!pb_decode(&stream, AllTypes_fields, &alltypes)) 46 { 47 printf("Parsing failed: %s\n", PB_GET_ERROR(&stream)); 48 return 1; 49 } 50 51 /* Check that the extensions decoded properly */ 52 TEST(ext1.found) 53 TEST(extensionfield1 == 12345) 54 TEST(ext2.found) 55 TEST(strcmp(extensionfield2.test1, "test") == 0) 56 TEST(extensionfield2.test2 == 54321) 57 58 return 0; 59 } 60 61