Handle unsupported extension field types more gracefully.
[apps/agl-service-can-low-level.git] / tests / test_decode_extensions.c
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
10 #define TEST(x) if (!(x)) { \
11     printf("Test " #x " failed.\n"); \
12     return 2; \
13     }
14
15 int main(int argc, char **argv)
16 {
17     uint8_t buffer[1024];
18     size_t count = fread(buffer, 1, sizeof(buffer), stdin);
19     pb_istream_t stream = pb_istream_from_buffer(buffer, count);
20     
21     AllTypes alltypes = {};
22     
23     int32_t extensionfield1;
24     pb_extension_t ext1 = {&AllTypes_extensionfield1, &extensionfield1, NULL};
25     alltypes.extensions = &ext1;
26         
27     ExtensionMessage extensionfield2 = {};
28     pb_extension_t ext2 = {&ExtensionMessage_AllTypes_extensionfield2, &extensionfield2, NULL};
29     ext1.next = &ext2;
30     
31     if (!pb_decode(&stream, AllTypes_fields, &alltypes))
32     {
33         printf("Parsing failed: %s\n", PB_GET_ERROR(&stream));
34         return 1;
35     }
36
37     TEST(extensionfield1 == 12345)
38     TEST(strcmp(extensionfield2.test1, "test") == 0)
39     TEST(extensionfield2.test2 == 54321)
40     
41     return 0;
42 }
43