1 /* Tests extension fields.
2  */
3 
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <pb_encode.h>
8 #include "alltypes.pb.h"
9 #include "extensions.pb.h"
10 #include "test_helpers.h"
11 
main(int argc,char ** argv)12 int main(int argc, char **argv)
13 {
14     uint8_t buffer[1024];
15     pb_ostream_t stream;
16 
17     AllTypes alltypes = {0};
18     int32_t extensionfield1 = 12345;
19     pb_extension_t ext1 = pb_extension_init_zero;
20     ExtensionMessage extensionfield2 = {"test", 54321};
21     pb_extension_t ext2 = pb_extension_init_zero;
22 
23     /* Set up the extensions */
24     alltypes.extensions = &ext1;
25 
26     ext1.type = &AllTypes_extensionfield1;
27     ext1.dest = &extensionfield1;
28     ext1.next = &ext2;
29 
30     ext2.type = &ExtensionMessage_AllTypes_extensionfield2;
31     ext2.dest = &extensionfield2;
32     ext2.next = NULL;
33 
34     /* Set up the output stream */
35     stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
36 
37     /* Now encode the message and check if we succeeded. */
38     if (pb_encode(&stream, AllTypes_fields, &alltypes))
39     {
40         SET_BINARY_MODE(stdout);
41         fwrite(buffer, 1, stream.bytes_written, stdout);
42         return 0; /* Success */
43     }
44     else
45     {
46         fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream));
47         return 1; /* Failure */
48     }
49 
50     /* Check that the field tags are properly generated */
51     (void)AllTypes_extensionfield1_tag;
52     (void)ExtensionMessage_AllTypes_extensionfield2_tag;
53 }
54 
55