Separation Generator to a dedicated repo
[apps/low-level-can-service.git] / libs / nanopb / tests / missing_fields / 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     size_t size;
12     
13     /* Create a message with one missing field */
14     {
15         MissingField msg = {0};
16         pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
17         
18         if (!pb_encode(&stream, MissingField_fields, &msg))
19         {
20             printf("Encode failed.\n");
21             return 1;
22         }
23
24         size = stream.bytes_written;
25     }
26
27     /* Test that it decodes properly if we don't require that field */
28     {
29         MissingField msg = {0};
30         pb_istream_t stream = pb_istream_from_buffer(buffer, size);
31         
32         if (!pb_decode(&stream, MissingField_fields, &msg))
33         {
34             printf("Decode failed: %s\n", PB_GET_ERROR(&stream));
35             return 2;
36         }
37     }
38     
39     /* Test that it does *not* decode properly if we require the field */
40     {
41         AllFields msg = {0};
42         pb_istream_t stream = pb_istream_from_buffer(buffer, size);
43         
44         if (pb_decode(&stream, AllFields_fields, &msg))
45         {
46             printf("Decode didn't detect missing field.\n");
47             return 3;
48         }
49     }
50     
51     return 0; /* All ok */
52 }
53