MOS Source Code
Loading...
Searching...
No Matches
missing_fields.c
Go to the documentation of this file.
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
8int 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
int main()
#define PB_GET_ERROR(stream)
Definition pb.h:891
bool pb_decode(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct)
Definition pb_decode.c:1182
pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t msglen)
Definition pb_decode.c:143
pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize)
Definition pb_encode.c:63
bool pb_encode(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct)
Definition pb_encode.c:512
unsigned char uint8_t
Definition pb_syshdr.h:20
size_t size
Definition slab.c:30
static char buffer[2048]
Definition test_printf.c:7