aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_missing_fields.c
blob: 277418475a5a407bd89849b92f079dc8f8a40789 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* Checks that missing required fields are detected properly */

#include <stdio.h>
#include <pb_encode.h>
#include <pb_decode.h>
#include "missing_fields.pb.h"

int main()
{
    uint8_t buffer[512] = {};
    
    /* Create a message with one missing field */
    {
        MissingField msg = {};
        pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
        if (!pb_encode(&stream, MissingField_fields, &msg))
        {
            printf("Encode failed.\n");
            return 1;
        }
    }

    /* Test that it decodes properly if we don't require that field */
    {
        MissingField msg = {};
        pb_istream_t stream = pb_istream_from_buffer(buffer, sizeof(buffer));
        
        if (!pb_decode(&stream, MissingField_fields, &msg))
        {
            printf("Decode failed: %s\n", PB_GET_ERROR(&stream));
            return 2;
        }
    }
    
    /* Test that it does *not* decode properly if we require the field */
    {
        AllFields msg = {};
        pb_istream_t stream = pb_istream_from_buffer(buffer, sizeof(buffer));
        
        if (pb_decode(&stream, AllFields_fields, &msg))
        {
            printf("Decode didn't detect missing field.\n");
            return 3;
        }
    }
    
    return 0; /* All ok */
}