From 0f6b615ae3395734ee9a1b35185540acad18c452 Mon Sep 17 00:00:00 2001 From: Petteri Aimonen Date: Thu, 12 Jan 2012 19:06:33 +0200 Subject: [PATCH] Added an encode/decode test for 'required' fields of all types. --- tests/Makefile | 11 ++++++--- tests/alltypes.proto | 40 ++++++++++++++++++++++++++++++ tests/test_decode3.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/test_encode3.c | 50 +++++++++++++++++++++++++++++++++++++ 4 files changed, 168 insertions(+), 3 deletions(-) create mode 100644 tests/alltypes.proto create mode 100644 tests/test_decode3.c create mode 100644 tests/test_encode3.c diff --git a/tests/Makefile b/tests/Makefile index 11f061a..8e2df14 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,12 +1,12 @@ 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 +DEPS=../pb_decode.h ../pb_encode.h ../pb.h person.pb.h callbacks.pb.h unittests.h unittestproto.pb.h alltypes.pb.h TESTS=test_decode1 test_encode1 decode_unittests encode_unittests all: breakpoints $(TESTS) run_unittests clean: - rm -f $(TESTS) person.pb* *.o *.gcda *.gcno + rm -f $(TESTS) person.pb* alltypes.pb* *.o *.gcda *.gcno %.o: %.c %.o: %.c $(DEPS) @@ -19,8 +19,10 @@ pb_decode.o: ../pb_decode.c $(DEPS) test_decode1: test_decode1.o pb_decode.o person.pb.o test_decode2: test_decode2.o pb_decode.o person.pb.o +test_decode3: test_decode3.o pb_decode.o alltypes.pb.o test_encode1: test_encode1.o pb_encode.o person.pb.o 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 decode_unittests: decode_unittests.o pb_decode.o unittestproto.pb.o @@ -39,7 +41,7 @@ coverage: run_unittests gcov pb_encode.gcda gcov pb_decode.gcda -run_unittests: decode_unittests encode_unittests test_encode1 test_encode2 test_decode1 test_decode2 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 rm -f *.gcda ./decode_unittests > /dev/null @@ -57,5 +59,8 @@ run_unittests: decode_unittests encode_unittests test_encode1 test_encode2 test_ [ "`./test_encode_callbacks | ./test_decode_callbacks`" = \ "`./test_encode_callbacks | protoc --decode=TestMessage callbacks.proto`" ] + ./test_encode3 | ./test_decode3 + ./test_encode3 | protoc --decode=AllTypes -I. -I../generator -I/usr/include alltypes.proto >/dev/null + 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/alltypes.proto b/tests/alltypes.proto new file mode 100644 index 0000000..744a0fc --- /dev/null +++ b/tests/alltypes.proto @@ -0,0 +1,40 @@ +import "nanopb.proto"; + +message SubMessage { + required string substuff1 = 1 [(nanopb).max_size = 16]; + required int32 substuff2 = 2; +} + +enum MyEnum { + First = 1; + Second = 2; + Truth = 42; +} + +message AllTypes { + required int32 req_int32 = 1; + required int64 req_int64 = 2; + required uint32 req_uint32 = 3; + required uint64 req_uint64 = 4; + required sint32 req_sint32 = 5; + required sint64 req_sint64 = 6; + required bool req_bool = 7; + + required fixed32 req_fixed32 = 8; + required sfixed32 req_sfixed32= 9; + required float req_float = 10; + + required fixed64 req_fixed64 = 11; + required sfixed64 req_sfixed64= 12; + required double req_double = 13; + + required string req_string = 14 [(nanopb).max_size = 16]; + required bytes req_bytes = 15 [(nanopb).max_size = 16]; + required SubMessage req_submsg = 16; + required MyEnum req_enum = 17; + + // Just to make sure that the size of the fields has been calculated + // properly, i.e. otherwise a bug in last field might not be detected. + required int32 end = 99; +} + diff --git a/tests/test_decode3.c b/tests/test_decode3.c new file mode 100644 index 0000000..a7106de --- /dev/null +++ b/tests/test_decode3.c @@ -0,0 +1,70 @@ +/* Tests the decoding of all types. Currently only in the 'required' variety. + * This is the counterpart of test_encode3. + * Run e.g. ./test_encode3 | ./test_decode3 + */ + +#include +#include +#include +#include "alltypes.pb.h" + +#define TEST(x) if (!(x)) { \ + printf("Test " #x " failed.\n"); \ + return false; \ + } + +/* This function is called once from main(), it handles + the decoding and checks the fields. */ +bool check_alltypes(pb_istream_t *stream) +{ + AllTypes alltypes = {}; + + if (!pb_decode(stream, AllTypes_fields, &alltypes)) + return false; + + TEST(alltypes.req_int32 == 1001); + TEST(alltypes.req_int64 == 1002); + TEST(alltypes.req_uint32 == 1003); + TEST(alltypes.req_uint64 == 1004); + TEST(alltypes.req_sint32 == 1005); + TEST(alltypes.req_sint64 == 1006); + TEST(alltypes.req_bool == true); + + TEST(alltypes.req_fixed32 == 1008); + TEST(alltypes.req_sfixed32 == 1009); + TEST(alltypes.req_float == 1010.0f); + + TEST(alltypes.req_fixed64 == 1011); + TEST(alltypes.req_sfixed64 == 1012); + TEST(alltypes.req_double == 1013.0f); + + TEST(strcmp(alltypes.req_string, "1014") == 0); + TEST(alltypes.req_bytes.size == 4); + TEST(memcmp(alltypes.req_bytes.bytes, "1015", 4) == 0); + TEST(strcmp(alltypes.req_submsg.substuff1, "1016") == 0); + TEST(alltypes.req_submsg.substuff2 == 1016); + TEST(alltypes.req_enum == MyEnum_Truth); + + TEST(alltypes.end == 1099); + + return true; +} + +int main() +{ + /* Read the data into buffer */ + uint8_t buffer[512]; + size_t count = fread(buffer, 1, sizeof(buffer), stdin); + + /* Construct a pb_istream_t for reading from the buffer */ + pb_istream_t stream = pb_istream_from_buffer(buffer, count); + + /* Decode and print out the stuff */ + if (!check_alltypes(&stream)) + { + printf("Parsing failed.\n"); + return 1; + } else { + return 0; + } +} diff --git a/tests/test_encode3.c b/tests/test_encode3.c new file mode 100644 index 0000000..5e94be5 --- /dev/null +++ b/tests/test_encode3.c @@ -0,0 +1,50 @@ +/* Attempts to test all the datatypes supported by ProtoBuf. + * Currently only tests the 'required' variety. + */ + +#include +#include +#include "alltypes.pb.h" + +int main() +{ + /* Initialize the structure with constants */ + AllTypes alltypes = { + 1001, + 1002, + 1003, + 1004, + 1005, + 1006, + true, + + 1008, + 1009, + 1010.0f, + + 1011, + 1012, + 1013.0, + + "1014", + {4, "1015"}, + {"1016", 1016}, + MyEnum_Truth, + + 1099 + }; + + uint8_t buffer[512]; + pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); + + /* Now encode it and check if we succeeded. */ + if (pb_encode(&stream, AllTypes_fields, &alltypes)) + { + fwrite(buffer, 1, stream.bytes_written, stdout); + return 0; /* Success */ + } + else + { + return 1; /* Failure */ + } +} -- 2.16.6