summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>2012-06-30 18:10:08 +0300
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>2012-06-30 18:10:08 +0300
commit95eb4a549981dc556dd30c5d76a6b437bb5ed06d (patch)
treec0344bb22676b1167d66c41bcde9bb228c1a16f3 /tests
parent7bcf7ef5796c66125a2a4df1317b55a27adedd89 (diff)
Improve the detection of missing required fields.
Now the limit of tracked fields is configurable at compile-time using PB_MAX_REQUIRED_FIELDS. Added related test and updated documentation. Fixes issue #18.
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile7
-rw-r--r--tests/missing_fields.proto138
-rw-r--r--tests/test_missing_fields.c49
3 files changed, 192 insertions, 2 deletions
diff --git a/tests/Makefile b/tests/Makefile
index 8e2df140..24e9ba62 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -1,6 +1,6 @@
CFLAGS=-ansi -Wall -Werror -I .. -g -O0 --coverage
LDFLAGS=--coverage
-DEPS=../pb_decode.h ../pb_encode.h ../pb.h person.pb.h callbacks.pb.h unittests.h unittestproto.pb.h alltypes.pb.h
+DEPS=../pb_decode.h ../pb_encode.h ../pb.h person.pb.h callbacks.pb.h unittests.h unittestproto.pb.h alltypes.pb.h missing_fields.pb.h
TESTS=test_decode1 test_encode1 decode_unittests encode_unittests
all: breakpoints $(TESTS) run_unittests
@@ -25,6 +25,7 @@ test_encode2: test_encode2.o pb_encode.o person.pb.o
test_encode3: test_encode3.o pb_encode.o alltypes.pb.o
test_decode_callbacks: test_decode_callbacks.o pb_decode.o callbacks.pb.o
test_encode_callbacks: test_encode_callbacks.o pb_encode.o callbacks.pb.o
+test_missing_fields: test_missing_fields.o pb_encode.o pb_decode.o missing_fields.pb.o
decode_unittests: decode_unittests.o pb_decode.o unittestproto.pb.o
encode_unittests: encode_unittests.o pb_encode.o unittestproto.pb.o
@@ -41,7 +42,7 @@ coverage: run_unittests
gcov pb_encode.gcda
gcov pb_decode.gcda
-run_unittests: decode_unittests encode_unittests test_encode1 test_encode2 test_encode3 test_decode1 test_decode2 test_decode3 test_encode_callbacks test_decode_callbacks
+run_unittests: decode_unittests encode_unittests test_encode1 test_encode2 test_encode3 test_decode1 test_decode2 test_decode3 test_encode_callbacks test_decode_callbacks test_missing_fields
rm -f *.gcda
./decode_unittests > /dev/null
@@ -61,6 +62,8 @@ run_unittests: decode_unittests encode_unittests test_encode1 test_encode2 test_
./test_encode3 | ./test_decode3
./test_encode3 | protoc --decode=AllTypes -I. -I../generator -I/usr/include alltypes.proto >/dev/null
+
+ ./test_missing_fields
run_fuzztest: test_decode2
bash -c 'I=1; while true; do cat /dev/urandom | ./test_decode2 > /dev/null; I=$$(($$I+1)); echo -en "\r$$I"; done'
diff --git a/tests/missing_fields.proto b/tests/missing_fields.proto
new file mode 100644
index 00000000..cbb23ba1
--- /dev/null
+++ b/tests/missing_fields.proto
@@ -0,0 +1,138 @@
+/* Test for one missing field among many */
+
+message AllFields
+{
+ required int32 field1 = 1;
+ required int32 field2 = 2;
+ required int32 field3 = 3;
+ required int32 field4 = 4;
+ required int32 field5 = 5;
+ required int32 field6 = 6;
+ required int32 field7 = 7;
+ required int32 field8 = 8;
+ required int32 field9 = 9;
+ required int32 field10 = 10;
+ required int32 field11 = 11;
+ required int32 field12 = 12;
+ required int32 field13 = 13;
+ required int32 field14 = 14;
+ required int32 field15 = 15;
+ required int32 field16 = 16;
+ required int32 field17 = 17;
+ required int32 field18 = 18;
+ required int32 field19 = 19;
+ required int32 field20 = 20;
+ required int32 field21 = 21;
+ required int32 field22 = 22;
+ required int32 field23 = 23;
+ required int32 field24 = 24;
+ required int32 field25 = 25;
+ required int32 field26 = 26;
+ required int32 field27 = 27;
+ required int32 field28 = 28;
+ required int32 field29 = 29;
+ required int32 field30 = 30;
+ required int32 field31 = 31;
+ required int32 field32 = 32;
+ required int32 field33 = 33;
+ required int32 field34 = 34;
+ required int32 field35 = 35;
+ required int32 field36 = 36;
+ required int32 field37 = 37;
+ required int32 field38 = 38;
+ required int32 field39 = 39;
+ required int32 field40 = 40;
+ required int32 field41 = 41;
+ required int32 field42 = 42;
+ required int32 field43 = 43;
+ required int32 field44 = 44;
+ required int32 field45 = 45;
+ required int32 field46 = 46;
+ required int32 field47 = 47;
+ required int32 field48 = 48;
+ required int32 field49 = 49;
+ required int32 field50 = 50;
+ required int32 field51 = 51;
+ required int32 field52 = 52;
+ required int32 field53 = 53;
+ required int32 field54 = 54;
+ required int32 field55 = 55;
+ required int32 field56 = 56;
+ required int32 field57 = 57;
+ required int32 field58 = 58;
+ required int32 field59 = 59;
+ required int32 field60 = 60;
+ required int32 field61 = 61;
+ required int32 field62 = 62;
+ required int32 field63 = 63;
+ required int32 field64 = 64;
+}
+
+message MissingField
+{
+ required int32 field1 = 1;
+ required int32 field2 = 2;
+ required int32 field3 = 3;
+ required int32 field4 = 4;
+ required int32 field5 = 5;
+ required int32 field6 = 6;
+ required int32 field7 = 7;
+ required int32 field8 = 8;
+ required int32 field9 = 9;
+ required int32 field10 = 10;
+ required int32 field11 = 11;
+ required int32 field12 = 12;
+ required int32 field13 = 13;
+ required int32 field14 = 14;
+ required int32 field15 = 15;
+ required int32 field16 = 16;
+ required int32 field17 = 17;
+ required int32 field18 = 18;
+ required int32 field19 = 19;
+ required int32 field20 = 20;
+ required int32 field21 = 21;
+ required int32 field22 = 22;
+ required int32 field23 = 23;
+ required int32 field24 = 24;
+ required int32 field25 = 25;
+ required int32 field26 = 26;
+ required int32 field27 = 27;
+ required int32 field28 = 28;
+ required int32 field29 = 29;
+ required int32 field30 = 30;
+ required int32 field31 = 31;
+ required int32 field32 = 32;
+ required int32 field33 = 33;
+ required int32 field34 = 34;
+ required int32 field35 = 35;
+ required int32 field36 = 36;
+ required int32 field37 = 37;
+ required int32 field38 = 38;
+ required int32 field39 = 39;
+ required int32 field40 = 40;
+ required int32 field41 = 41;
+ required int32 field42 = 42;
+ required int32 field43 = 43;
+ required int32 field44 = 44;
+ required int32 field45 = 45;
+ required int32 field46 = 46;
+ required int32 field47 = 47;
+ required int32 field48 = 48;
+ required int32 field49 = 49;
+ required int32 field50 = 50;
+ required int32 field51 = 51;
+ required int32 field52 = 52;
+ required int32 field53 = 53;
+ required int32 field54 = 54;
+ required int32 field55 = 55;
+ required int32 field56 = 56;
+ required int32 field57 = 57;
+ required int32 field58 = 58;
+ required int32 field59 = 59;
+ required int32 field60 = 60;
+ required int32 field61 = 61;
+ required int32 field62 = 62;
+/* required int32 field63 = 63; */
+ required int32 field64 = 64;
+}
+
diff --git a/tests/test_missing_fields.c b/tests/test_missing_fields.c
new file mode 100644
index 00000000..46cd7d96
--- /dev/null
+++ b/tests/test_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.\n");
+ 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 */
+}
+