Handle unsupported extension field types more gracefully.
[apps/agl-service-can-low-level.git] / tests / test_encode_extensions.c
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
11 int main(int argc, char **argv)
12 {
13     AllTypes alltypes = {};
14
15     int32_t extensionfield1 = 12345;
16     pb_extension_t ext1 = {&AllTypes_extensionfield1, &extensionfield1, NULL};
17     alltypes.extensions = &ext1;
18
19     ExtensionMessage extensionfield2 = {"test", 54321};
20     pb_extension_t ext2 = {&ExtensionMessage_AllTypes_extensionfield2, &extensionfield2, NULL};
21     ext1.next = &ext2;
22     
23     uint8_t buffer[1024];
24     pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
25     
26     /* Now encode it and check if we succeeded. */
27     if (pb_encode(&stream, AllTypes_fields, &alltypes))
28     {
29         fwrite(buffer, 1, stream.bytes_written, stdout);
30         return 0; /* Success */
31     }
32     else
33     {
34         fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream));
35         return 1; /* Failure */
36     }
37 }
38