277418475a5a407bd89849b92f079dc8f8a40789
[apps/agl-service-can-low-level.git] / tests / test_missing_fields.c
1 /* Checks that missing required fields are detected properly */
2
3 #include <stdio.h>
4 #include <pb_encode.h>
5 #include <pb_decode.h>
6 #include "missing_fields.pb.h"
7
8 int main()
9 {
10     uint8_t buffer[512] = {};
11     
12     /* Create a message with one missing field */
13     {
14         MissingField msg = {};
15         pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
16         if (!pb_encode(&stream, MissingField_fields, &msg))
17         {
18             printf("Encode failed.\n");
19             return 1;
20         }
21     }
22
23     /* Test that it decodes properly if we don't require that field */
24     {
25         MissingField msg = {};
26         pb_istream_t stream = pb_istream_from_buffer(buffer, sizeof(buffer));
27         
28         if (!pb_decode(&stream, MissingField_fields, &msg))
29         {
30             printf("Decode failed: %s\n", PB_GET_ERROR(&stream));
31             return 2;
32         }
33     }
34     
35     /* Test that it does *not* decode properly if we require the field */
36     {
37         AllFields msg = {};
38         pb_istream_t stream = pb_istream_from_buffer(buffer, sizeof(buffer));
39         
40         if (pb_decode(&stream, AllFields_fields, &msg))
41         {
42             printf("Decode didn't detect missing field.\n");
43             return 3;
44         }
45     }
46     
47     return 0; /* All ok */
48 }
49