From 32e25cbca210a359b09768537b6f443fe90a3070 Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Tue, 20 Jun 2017 10:24:05 +0000 Subject: Separation Generator to a dedicated repo Change-Id: Id94831651c3266861435272a6e36c7884bef2c45 Signed-off-by: Romain Forlot --- libs/nanopb/tests/missing_fields/missing_fields.c | 53 +++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 libs/nanopb/tests/missing_fields/missing_fields.c (limited to 'libs/nanopb/tests/missing_fields/missing_fields.c') diff --git a/libs/nanopb/tests/missing_fields/missing_fields.c b/libs/nanopb/tests/missing_fields/missing_fields.c new file mode 100644 index 00000000..8aded827 --- /dev/null +++ b/libs/nanopb/tests/missing_fields/missing_fields.c @@ -0,0 +1,53 @@ +/* Checks that missing required fields are detected properly */ + +#include +#include +#include +#include "missing_fields.pb.h" + +int main() +{ + uint8_t buffer[512]; + size_t size; + + /* Create a message with one missing field */ + { + MissingField msg = {0}; + pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); + + if (!pb_encode(&stream, MissingField_fields, &msg)) + { + printf("Encode failed.\n"); + return 1; + } + + size = stream.bytes_written; + } + + /* Test that it decodes properly if we don't require that field */ + { + MissingField msg = {0}; + pb_istream_t stream = pb_istream_from_buffer(buffer, size); + + 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 = {0}; + pb_istream_t stream = pb_istream_from_buffer(buffer, size); + + if (pb_decode(&stream, AllFields_fields, &msg)) + { + printf("Decode didn't detect missing field.\n"); + return 3; + } + } + + return 0; /* All ok */ +} + -- cgit 1.2.3-korg