aboutsummaryrefslogtreecommitdiffstats
path: root/tests/missing_fields/missing_fields.c
diff options
context:
space:
mode:
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>2013-09-08 17:52:03 +0300
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>2013-09-08 17:52:03 +0300
commit262c62676cf740ec3ce14a22bde47b7968fec8f0 (patch)
tree01cb3a8cc56ea58c536c9fe1d9ca3a46e96db46e /tests/missing_fields/missing_fields.c
parentd7f3a74388b4825d2c980c53d0a740ddfd0e4770 (diff)
Start moving the tests into subfolders. Transition to SCons for build system for the tests.
Only a few tests updated so far. Have to include all the rest before merging to mainline. Update issue 63 Status: Started
Diffstat (limited to 'tests/missing_fields/missing_fields.c')
-rw-r--r--tests/missing_fields/missing_fields.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/tests/missing_fields/missing_fields.c b/tests/missing_fields/missing_fields.c
new file mode 100644
index 00000000..27741847
--- /dev/null
+++ b/tests/missing_fields/missing_fields.c
@@ -0,0 +1,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 */
+}
+