summaryrefslogtreecommitdiffstats
path: root/tests/missing_fields
diff options
context:
space:
mode:
Diffstat (limited to 'tests/missing_fields')
-rw-r--r--tests/missing_fields/missing_fields.c49
-rw-r--r--tests/missing_fields/missing_fields.proto138
2 files changed, 187 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 */
+}
+
diff --git a/tests/missing_fields/missing_fields.proto b/tests/missing_fields/missing_fields.proto
new file mode 100644
index 00000000..cbb23ba1
--- /dev/null
+++ b/tests/missing_fields/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;
+}
+