From ed564186e14c79c767096f4b306dc3c6c5bd2e7d Mon Sep 17 00:00:00 2001 From: Petteri Aimonen Date: Thu, 24 Oct 2013 21:45:39 +0300 Subject: Detect invalid sizes when encoding bytes fields. --- tests/encode_unittests/encode_unittests.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'tests/encode_unittests') diff --git a/tests/encode_unittests/encode_unittests.c b/tests/encode_unittests/encode_unittests.c index 14bc62ee..fd9a730c 100644 --- a/tests/encode_unittests/encode_unittests.c +++ b/tests/encode_unittests/encode_unittests.c @@ -172,9 +172,9 @@ int main() struct { size_t size; uint8_t bytes[5]; } value = {5, {'x', 'y', 'z', 'z', 'y'}}; COMMENT("Test pb_enc_bytes") - TEST(WRITES(pb_enc_bytes(&s, NULL, &value), "\x05xyzzy")) + TEST(WRITES(pb_enc_bytes(&s, &BytesMessage_fields[0], &value), "\x05xyzzy")) value.size = 0; - TEST(WRITES(pb_enc_bytes(&s, NULL, &value), "\x00")) + TEST(WRITES(pb_enc_bytes(&s, &BytesMessage_fields[0], &value), "\x00")) } { @@ -258,6 +258,20 @@ int main() "\x0A\x07\x0A\x05\x01\x02\x03\x04\x05")) } + { + uint8_t buffer[32]; + pb_ostream_t s; + BytesMessage msg = {{3, "xyz"}}; + + COMMENT("Test pb_encode with bytes message.") + TEST(WRITES(pb_encode(&s, BytesMessage_fields, &msg), + "\x0A\x03xyz")) + + msg.data.size = 17; /* More than maximum */ + TEST(!pb_encode(&s, BytesMessage_fields, &msg)) + } + + { uint8_t buffer[20]; pb_ostream_t s; -- cgit 1.2.3-korg