From 262c62676cf740ec3ce14a22bde47b7968fec8f0 Mon Sep 17 00:00:00 2001 From: Petteri Aimonen Date: Sun, 8 Sep 2013 17:52:03 +0300 Subject: 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 --- tests/backwards_compatibility/encode_legacy.c | 131 ++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 tests/backwards_compatibility/encode_legacy.c (limited to 'tests/backwards_compatibility/encode_legacy.c') diff --git a/tests/backwards_compatibility/encode_legacy.c b/tests/backwards_compatibility/encode_legacy.c new file mode 100644 index 00000000..e84f0908 --- /dev/null +++ b/tests/backwards_compatibility/encode_legacy.c @@ -0,0 +1,131 @@ +/* Attempts to test all the datatypes supported by ProtoBuf. + * This is a backwards-compatibility test, using bc_alltypes.pb.h. + * It is similar to test_encode3, but duplicated in order to allow + * test_encode3 to test any new features introduced later. + */ + +#include +#include +#include +#include +#include "bc_alltypes.pb.h" + +int main(int argc, char **argv) +{ + int mode = (argc > 1) ? atoi(argv[1]) : 0; + + /* Initialize the structure with constants */ + AllTypes alltypes = {0}; + + alltypes.req_int32 = -1001; + alltypes.req_int64 = -1002; + alltypes.req_uint32 = 1003; + alltypes.req_uint64 = 1004; + alltypes.req_sint32 = -1005; + alltypes.req_sint64 = -1006; + alltypes.req_bool = true; + + alltypes.req_fixed32 = 1008; + alltypes.req_sfixed32 = -1009; + alltypes.req_float = 1010.0f; + + alltypes.req_fixed64 = 1011; + alltypes.req_sfixed64 = -1012; + alltypes.req_double = 1013.0; + + strcpy(alltypes.req_string, "1014"); + alltypes.req_bytes.size = 4; + memcpy(alltypes.req_bytes.bytes, "1015", 4); + strcpy(alltypes.req_submsg.substuff1, "1016"); + alltypes.req_submsg.substuff2 = 1016; + alltypes.req_enum = MyEnum_Truth; + + alltypes.rep_int32_count = 5; alltypes.rep_int32[4] = -2001; + alltypes.rep_int64_count = 5; alltypes.rep_int64[4] = -2002; + alltypes.rep_uint32_count = 5; alltypes.rep_uint32[4] = 2003; + alltypes.rep_uint64_count = 5; alltypes.rep_uint64[4] = 2004; + alltypes.rep_sint32_count = 5; alltypes.rep_sint32[4] = -2005; + alltypes.rep_sint64_count = 5; alltypes.rep_sint64[4] = -2006; + alltypes.rep_bool_count = 5; alltypes.rep_bool[4] = true; + + alltypes.rep_fixed32_count = 5; alltypes.rep_fixed32[4] = 2008; + alltypes.rep_sfixed32_count = 5; alltypes.rep_sfixed32[4] = -2009; + alltypes.rep_float_count = 5; alltypes.rep_float[4] = 2010.0f; + + alltypes.rep_fixed64_count = 5; alltypes.rep_fixed64[4] = 2011; + alltypes.rep_sfixed64_count = 5; alltypes.rep_sfixed64[4] = -2012; + alltypes.rep_double_count = 5; alltypes.rep_double[4] = 2013.0; + + alltypes.rep_string_count = 5; strcpy(alltypes.rep_string[4], "2014"); + alltypes.rep_bytes_count = 5; alltypes.rep_bytes[4].size = 4; + memcpy(alltypes.rep_bytes[4].bytes, "2015", 4); + + alltypes.rep_submsg_count = 5; + strcpy(alltypes.rep_submsg[4].substuff1, "2016"); + alltypes.rep_submsg[4].substuff2 = 2016; + alltypes.rep_submsg[4].has_substuff3 = true; + alltypes.rep_submsg[4].substuff3 = 2016; + + alltypes.rep_enum_count = 5; alltypes.rep_enum[4] = MyEnum_Truth; + + if (mode != 0) + { + /* Fill in values for optional fields */ + alltypes.has_opt_int32 = true; + alltypes.opt_int32 = 3041; + alltypes.has_opt_int64 = true; + alltypes.opt_int64 = 3042; + alltypes.has_opt_uint32 = true; + alltypes.opt_uint32 = 3043; + alltypes.has_opt_uint64 = true; + alltypes.opt_uint64 = 3044; + alltypes.has_opt_sint32 = true; + alltypes.opt_sint32 = 3045; + alltypes.has_opt_sint64 = true; + alltypes.opt_sint64 = 3046; + alltypes.has_opt_bool = true; + alltypes.opt_bool = true; + + alltypes.has_opt_fixed32 = true; + alltypes.opt_fixed32 = 3048; + alltypes.has_opt_sfixed32 = true; + alltypes.opt_sfixed32 = 3049; + alltypes.has_opt_float = true; + alltypes.opt_float = 3050.0f; + + alltypes.has_opt_fixed64 = true; + alltypes.opt_fixed64 = 3051; + alltypes.has_opt_sfixed64 = true; + alltypes.opt_sfixed64 = 3052; + alltypes.has_opt_double = true; + alltypes.opt_double = 3053.0; + + alltypes.has_opt_string = true; + strcpy(alltypes.opt_string, "3054"); + alltypes.has_opt_bytes = true; + alltypes.opt_bytes.size = 4; + memcpy(alltypes.opt_bytes.bytes, "3055", 4); + alltypes.has_opt_submsg = true; + strcpy(alltypes.opt_submsg.substuff1, "3056"); + alltypes.opt_submsg.substuff2 = 3056; + alltypes.has_opt_enum = true; + alltypes.opt_enum = MyEnum_Truth; + } + + alltypes.end = 1099; + + uint8_t buffer[1024]; + 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 + { + fprintf(stderr, "Encoding failed!\n"); + return 1; /* Failure */ + } +} -- cgit 1.2.3-korg From e2e9980627810fe0ee2b8f119bcf651f0f318a8a Mon Sep 17 00:00:00 2001 From: Petteri Aimonen Date: Tue, 10 Sep 2013 22:34:54 +0300 Subject: Move the rest of the tests to scons --- tests/backwards_compatibility/SConscript | 11 +++++++++++ tests/backwards_compatibility/alltypes_legacy.c | 2 +- tests/backwards_compatibility/decode_legacy.c | 10 +++++----- tests/backwards_compatibility/encode_legacy.c | 8 ++++---- tests/basic_stream/SConscript | 12 ++++++++++++ tests/callbacks/SConscript | 14 ++++++++++++++ tests/extensions/SConscript | 16 ++++++++++++++++ tests/extra_fields/SConscript | 10 ++++++++++ tests/extra_fields/person_with_extra_field.expected | 14 ++++++++++++++ tests/missing_fields/SConscript | 8 ++++++++ tests/multiple_files/SConscript | 13 +++++++++++++ tests/multiple_files/test_multiple_files.c | 3 +-- tests/no_messages/SConscript | 7 +++++++ tests/options/SConscript | 9 +++++++++ tests/site_scons/site_init.py | 21 +++++++++++++++++++-- tests/special_characters/SConscript | 7 +++++++ tests/special_characters/funny-proto+name.proto | 0 17 files changed, 151 insertions(+), 14 deletions(-) create mode 100644 tests/backwards_compatibility/SConscript create mode 100644 tests/basic_stream/SConscript create mode 100644 tests/callbacks/SConscript create mode 100644 tests/extensions/SConscript create mode 100644 tests/extra_fields/SConscript create mode 100644 tests/extra_fields/person_with_extra_field.expected create mode 100644 tests/missing_fields/SConscript create mode 100644 tests/multiple_files/SConscript create mode 100644 tests/no_messages/SConscript create mode 100644 tests/options/SConscript create mode 100644 tests/special_characters/SConscript delete mode 100644 tests/special_characters/funny-proto+name.proto (limited to 'tests/backwards_compatibility/encode_legacy.c') diff --git a/tests/backwards_compatibility/SConscript b/tests/backwards_compatibility/SConscript new file mode 100644 index 00000000..5fb978f3 --- /dev/null +++ b/tests/backwards_compatibility/SConscript @@ -0,0 +1,11 @@ +# Check that the old generated .pb.c/.pb.h files are still compatible with the +# current version of nanopb. + +Import("env") + +enc = env.Program(["encode_legacy.c", "alltypes_legacy.c", "#common/pb_encode.o"]) +dec = env.Program(["decode_legacy.c", "alltypes_legacy.c", "#common/pb_decode.o"]) + +env.RunTest(enc) +env.RunTest([dec, "encode_legacy.output"]) + diff --git a/tests/backwards_compatibility/alltypes_legacy.c b/tests/backwards_compatibility/alltypes_legacy.c index b144b1e3..9134d5e6 100644 --- a/tests/backwards_compatibility/alltypes_legacy.c +++ b/tests/backwards_compatibility/alltypes_legacy.c @@ -5,7 +5,7 @@ * incompatible changes made to the generator in future versions. */ -#include "bc_alltypes.pb.h" +#include "alltypes_legacy.h" const char SubMessage_substuff1_default[16] = "1"; const int32_t SubMessage_substuff2_default = 2; diff --git a/tests/backwards_compatibility/decode_legacy.c b/tests/backwards_compatibility/decode_legacy.c index b74172fd..315b16ef 100644 --- a/tests/backwards_compatibility/decode_legacy.c +++ b/tests/backwards_compatibility/decode_legacy.c @@ -1,16 +1,16 @@ /* Tests the decoding of all types. - * This is a backwards-compatibility test, using bc_alltypes.pb.h. - * It is similar to test_decode3, but duplicated in order to allow - * test_decode3 to test any new features introduced later. + * This is a backwards-compatibility test, using alltypes_legacy.h. + * It is similar to decode_alltypes, but duplicated in order to allow + * decode_alltypes to test any new features introduced later. * - * Run e.g. ./bc_encode | ./bc_decode + * Run e.g. ./encode_legacy | ./decode_legacy */ #include #include #include #include -#include "bc_alltypes.pb.h" +#include "alltypes_legacy.h" #define TEST(x) if (!(x)) { \ printf("Test " #x " failed.\n"); \ diff --git a/tests/backwards_compatibility/encode_legacy.c b/tests/backwards_compatibility/encode_legacy.c index e84f0908..0e313093 100644 --- a/tests/backwards_compatibility/encode_legacy.c +++ b/tests/backwards_compatibility/encode_legacy.c @@ -1,14 +1,14 @@ /* Attempts to test all the datatypes supported by ProtoBuf. - * This is a backwards-compatibility test, using bc_alltypes.pb.h. - * It is similar to test_encode3, but duplicated in order to allow - * test_encode3 to test any new features introduced later. + * This is a backwards-compatibility test, using alltypes_legacy.h. + * It is similar to encode_alltypes, but duplicated in order to allow + * encode_alltypes to test any new features introduced later. */ #include #include #include #include -#include "bc_alltypes.pb.h" +#include "alltypes_legacy.h" int main(int argc, char **argv) { diff --git a/tests/basic_stream/SConscript b/tests/basic_stream/SConscript new file mode 100644 index 00000000..17382a98 --- /dev/null +++ b/tests/basic_stream/SConscript @@ -0,0 +1,12 @@ +# Build and run a basic round-trip test using direct stream encoding. + +Import("env") + +enc = env.Program(["encode_stream.c", "#common/person.pb.c", "#common/pb_encode.o"]) +dec = env.Program(["decode_stream.c", "#common/person.pb.c", "#common/pb_decode.o"]) + +env.RunTest(enc) +env.RunTest([dec, "encode_stream.output"]) +env.Decode(["encode_stream.output", "#common/person.proto"], MESSAGE = "Person") +env.Compare(["decode_stream.output", "encode_stream.decoded"]) + diff --git a/tests/callbacks/SConscript b/tests/callbacks/SConscript new file mode 100644 index 00000000..729fd65f --- /dev/null +++ b/tests/callbacks/SConscript @@ -0,0 +1,14 @@ +# Test the functionality of the callback fields. + +Import("env") + +env.NanopbProto("callbacks") +enc = env.Program(["encode_callbacks.c", "callbacks.pb.c", "#common/pb_encode.o"]) +dec = env.Program(["decode_callbacks.c", "callbacks.pb.c", "#common/pb_decode.o"]) + +env.RunTest(enc) +env.RunTest([dec, "encode_callbacks.output"]) + +env.Decode(["encode_callbacks.output", "callbacks.proto"], MESSAGE = "TestMessage") +env.Compare(["decode_callbacks.output", "encode_callbacks.decoded"]) + diff --git a/tests/extensions/SConscript b/tests/extensions/SConscript new file mode 100644 index 00000000..b48d6a66 --- /dev/null +++ b/tests/extensions/SConscript @@ -0,0 +1,16 @@ +# Test the support for extension fields. + +Import("env") + +# We use the files from the alltypes test case +incpath = env.Clone() +incpath.Append(PROTOCPATH = '#alltypes') +incpath.Append(CPPPATH = '#alltypes') + +incpath.NanopbProto("extensions") +enc = incpath.Program(["encode_extensions.c", "extensions.pb.c", "#alltypes/alltypes.pb.o", "#common/pb_encode.o"]) +dec = incpath.Program(["decode_extensions.c", "extensions.pb.c", "#alltypes/alltypes.pb.o", "#common/pb_decode.o"]) + +env.RunTest(enc) +env.RunTest([dec, "encode_extensions.output"]) + diff --git a/tests/extra_fields/SConscript b/tests/extra_fields/SConscript new file mode 100644 index 00000000..a6f0e751 --- /dev/null +++ b/tests/extra_fields/SConscript @@ -0,0 +1,10 @@ +# Test that the decoder properly handles unknown fields in the input. + +Import("env") + +dec = env.GetBuildPath('#basic_buffer/${PROGPREFIX}decode_buffer${PROGSUFFIX}') +env.RunTest('person_with_extra_field.output', [dec, "person_with_extra_field.pb"]) +env.Compare(["person_with_extra_field.output", "person_with_extra_field.expected"]) + +dec2 = env.GetBuildPath('#alltypes/${PROGPREFIX}decode_alltypes${PROGSUFFIX}') +env.RunTest('alltypes_with_extra_fields.output', [dec2, 'alltypes_with_extra_fields.pb']) diff --git a/tests/extra_fields/person_with_extra_field.expected b/tests/extra_fields/person_with_extra_field.expected new file mode 100644 index 00000000..da9c32df --- /dev/null +++ b/tests/extra_fields/person_with_extra_field.expected @@ -0,0 +1,14 @@ +name: "Test Person 99" +id: 99 +email: "test@person.com" +phone { + number: "555-12345678" + type: MOBILE +} +phone { + number: "99-2342" +} +phone { + number: "1234-5678" + type: WORK +} diff --git a/tests/missing_fields/SConscript b/tests/missing_fields/SConscript new file mode 100644 index 00000000..361b5502 --- /dev/null +++ b/tests/missing_fields/SConscript @@ -0,0 +1,8 @@ +# Check that the decoder properly detects when required fields are missing. + +Import("env") + +env.NanopbProto("missing_fields") +test = env.Program(["missing_fields.c", "missing_fields.pb.c", "#common/pb_encode.o", "#common/pb_decode.o"]) +env.RunTest(test) + diff --git a/tests/multiple_files/SConscript b/tests/multiple_files/SConscript new file mode 100644 index 00000000..6b4f6b69 --- /dev/null +++ b/tests/multiple_files/SConscript @@ -0,0 +1,13 @@ +# Test that multiple .proto files don't cause name collisions. + +Import("env") + +incpath = env.Clone() +incpath.Append(PROTOCPATH = '#multiple_files') + +incpath.NanopbProto("callbacks") +incpath.NanopbProto("callbacks2") +test = incpath.Program(["test_multiple_files.c", "callbacks.pb.c", "callbacks2.pb.c"]) + +env.RunTest(test) + diff --git a/tests/multiple_files/test_multiple_files.c b/tests/multiple_files/test_multiple_files.c index cb4e16d3..05722dc5 100644 --- a/tests/multiple_files/test_multiple_files.c +++ b/tests/multiple_files/test_multiple_files.c @@ -1,6 +1,5 @@ /* - * Tests if still compile if typedefs are redfefined in STATIC_ASSERTS when - * proto file includes another poto file + * Tests if this still compiles when multiple .proto files are involved. */ #include diff --git a/tests/no_messages/SConscript b/tests/no_messages/SConscript new file mode 100644 index 00000000..6492e2cf --- /dev/null +++ b/tests/no_messages/SConscript @@ -0,0 +1,7 @@ +# Test that a .proto file without any messages compiles fine. + +Import("env") + +env.NanopbProto("no_messages") +env.Object('no_messages.pb.c') + diff --git a/tests/options/SConscript b/tests/options/SConscript new file mode 100644 index 00000000..89a00fa5 --- /dev/null +++ b/tests/options/SConscript @@ -0,0 +1,9 @@ +# Test that the generator options work as expected. + +Import("env") + +env.NanopbProto("options") +env.Object('options.pb.c') + +env.Match(['options.pb.h', 'options.expected']) + diff --git a/tests/site_scons/site_init.py b/tests/site_scons/site_init.py index b69db646..86e50335 100644 --- a/tests/site_scons/site_init.py +++ b/tests/site_scons/site_init.py @@ -1,5 +1,6 @@ import subprocess import sys +import re try: # Make terminal colors work on windows @@ -13,8 +14,9 @@ def add_nanopb_builders(env): # Build command for building .pb from .proto using protoc def proto_actions(source, target, env, for_signature): - dirs = ' '.join(['-I' + env.GetBuildPath(d) for d in env['PROTOCPATH']]) - return '$PROTOC $PROTOCFLAGS %s -o%s %s' % (dirs, target[0], source[0]) + esc = env['ESCAPE'] + dirs = ' '.join(['-I' + esc(env.GetBuildPath(d)) for d in env['PROTOCPATH']]) + return '$PROTOC $PROTOCFLAGS %s -o%s %s' % (dirs, esc(str(target[0])), esc(str(source[0]))) proto_file_builder = Builder(generator = proto_actions, suffix = '.pb', @@ -91,4 +93,19 @@ def add_nanopb_builders(env): suffix = '.equal') env.Append(BUILDERS = {'Compare': compare_builder}) + # Build command that checks that each pattern in source2 is found in source1. + def match_files(target, source, env): + data = open(str(source[0]), 'rU').read() + patterns = open(str(source[1])) + for pattern in patterns: + if pattern.strip() and not re.search(pattern.strip(), data, re.MULTILINE): + print '\033[31m[FAIL]\033[0m Pattern not found in ' + str(source[0]) + ': ' + pattern + return 1 + else: + print '\033[32m[ OK ]\033[0m All patterns found in ' + str(source[0]) + return 0 + + match_builder = Builder(action = match_files, suffix = '.matched') + env.Append(BUILDERS = {'Match': match_builder}) + diff --git a/tests/special_characters/SConscript b/tests/special_characters/SConscript new file mode 100644 index 00000000..05dccaee --- /dev/null +++ b/tests/special_characters/SConscript @@ -0,0 +1,7 @@ +# Test that special characters in .proto filenames work. + +Import('env') + +env.Proto("funny-proto+name has.characters.proto") +env.Nanopb("funny-proto+name has.characters.pb.c", "funny-proto+name has.characters.pb") +env.Object("funny-proto+name has.characters.pb.c") diff --git a/tests/special_characters/funny-proto+name.proto b/tests/special_characters/funny-proto+name.proto deleted file mode 100644 index e69de29b..00000000 -- cgit 1.2.3-korg From b9f14bddf778a5ed27e3289b90a0657fec3e1a53 Mon Sep 17 00:00:00 2001 From: Petteri Aimonen Date: Wed, 11 Sep 2013 13:16:20 +0300 Subject: Make all the tests ANSI C compatible. --- tests/SConstruct | 10 +++---- tests/backwards_compatibility/encode_legacy.c | 32 ++++++++++++--------- tests/basic_stream/decode_stream.c | 9 +++--- tests/basic_stream/encode_stream.c | 5 +++- tests/callbacks/decode_callbacks.c | 16 +++++++---- tests/callbacks/encode_callbacks.c | 14 ++++++--- tests/extensions/decode_extensions.c | 33 ++++++++++++++++------ tests/extensions/encode_extensions.c | 26 ++++++++++++----- tests/missing_fields/missing_fields.c | 9 +++--- .../funny-proto+name has.characters.proto | 0 10 files changed, 100 insertions(+), 54 deletions(-) create mode 100644 tests/special_characters/funny-proto+name has.characters.proto (limited to 'tests/backwards_compatibility/encode_legacy.c') diff --git a/tests/SConstruct b/tests/SConstruct index 92cb0c62..26a513d5 100644 --- a/tests/SConstruct +++ b/tests/SConstruct @@ -70,15 +70,15 @@ if 'gcc' in env['CC']: # GNU Compiler Collection # Debug info, warnings as errors - env.Append(CFLAGS = '-ansi -g -O0 -Wall -Werror --coverage -fstack-protector-all') + env.Append(CFLAGS = '-ansi -pedantic -g -O0 -Wall -Werror --coverage -fstack-protector-all') env.Append(LINKFLAGS = '--coverage') # More strict checks on the nanopb core - env.Append(CORECFLAGS = '-pedantic -Wextra -Wcast-qual -Wlogical-op -Wconversion') + env.Append(CORECFLAGS = '-Wextra -Wcast-qual -Wlogical-op -Wconversion') elif 'clang' in env['CC']: # CLang - env.Append(CFLAGS = '-ansi -g -O0 -Wall -Werror') - env.Append(CORECFLAGS = '-pedantic -Wextra -Wcast-qual -Wconversion') + env.Append(CFLAGS = '-ansi -pedantic -g -O0 -Wall -Werror') + env.Append(CORECFLAGS = ' -Wextra -Wcast-qual -Wconversion') elif 'cl' in env['CC']: # Microsoft Visual C++ @@ -87,7 +87,7 @@ elif 'cl' in env['CC']: env.Append(LINKFLAGS = '/DEBUG') # More strict checks on the nanopb core - env.Append(CORECFLAGS = '/W4 /Za') + env.Append(CORECFLAGS = '/W4') # PB_RETURN_ERROR triggers C4127 because of while(0) env.Append(CFLAGS = '/wd4127') diff --git a/tests/backwards_compatibility/encode_legacy.c b/tests/backwards_compatibility/encode_legacy.c index 0e313093..5c9d41b3 100644 --- a/tests/backwards_compatibility/encode_legacy.c +++ b/tests/backwards_compatibility/encode_legacy.c @@ -9,6 +9,7 @@ #include #include #include "alltypes_legacy.h" +#include "test_helpers.h" int main(int argc, char **argv) { @@ -113,19 +114,22 @@ int main(int argc, char **argv) } alltypes.end = 1099; - - uint8_t buffer[1024]; - 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 - { - fprintf(stderr, "Encoding failed!\n"); - return 1; /* Failure */ + + { + uint8_t buffer[1024]; + 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)) + { + SET_BINARY_MODE(stdout); + fwrite(buffer, 1, stream.bytes_written, stdout); + return 0; /* Success */ + } + else + { + fprintf(stderr, "Encoding failed!\n"); + return 1; /* Failure */ + } } } diff --git a/tests/basic_stream/decode_stream.c b/tests/basic_stream/decode_stream.c index 2142977e..667bf3c5 100644 --- a/tests/basic_stream/decode_stream.c +++ b/tests/basic_stream/decode_stream.c @@ -4,6 +4,7 @@ #include #include #include "person.pb.h" +#include "test_helpers.h" /* This function is called once from main(), it handles the decoding and printing. @@ -69,10 +70,10 @@ bool callback(pb_istream_t *stream, uint8_t *buf, size_t count) int main() { - /* Maximum size is specified to prevent infinite length messages from - * hanging this in the fuzz test. - */ - pb_istream_t stream = {&callback, stdin, 10000}; + pb_istream_t stream = {&callback, NULL, SIZE_MAX}; + stream.state = stdin; + SET_BINARY_MODE(stdin); + if (!print_person(&stream)) { printf("Parsing failed: %s\n", PB_GET_ERROR(&stream)); diff --git a/tests/basic_stream/encode_stream.c b/tests/basic_stream/encode_stream.c index fd25c6cb..7f571c41 100644 --- a/tests/basic_stream/encode_stream.c +++ b/tests/basic_stream/encode_stream.c @@ -4,6 +4,7 @@ #include #include #include "person.pb.h" +#include "test_helpers.h" /* This binds the pb_ostream_t into the stdout stream */ bool streamcallback(pb_ostream_t *stream, const uint8_t *buf, size_t count) @@ -22,7 +23,9 @@ int main() }}; /* Prepare the stream, output goes directly to stdout */ - pb_ostream_t stream = {&streamcallback, stdout, SIZE_MAX, 0}; + pb_ostream_t stream = {&streamcallback, NULL, SIZE_MAX, 0}; + stream.state = stdout; + SET_BINARY_MODE(stdout); /* Now encode it and check if we succeeded. */ if (pb_encode(&stream, Person_fields, &person)) diff --git a/tests/callbacks/decode_callbacks.c b/tests/callbacks/decode_callbacks.c index b5056923..c8daed29 100644 --- a/tests/callbacks/decode_callbacks.c +++ b/tests/callbacks/decode_callbacks.c @@ -5,6 +5,7 @@ #include #include #include "callbacks.pb.h" +#include "test_helpers.h" bool print_string(pb_istream_t *stream, const pb_field_t *field, void **arg) { @@ -50,21 +51,24 @@ bool print_fixed64(pb_istream_t *stream, const pb_field_t *field, void **arg) if (!pb_decode_fixed64(stream, &value)) return false; - printf((char*)*arg, (long long)value); + printf((char*)*arg, (long)value); return true; } int main() { uint8_t buffer[1024]; - size_t length = fread(buffer, 1, 1024, stdin); - pb_istream_t stream = pb_istream_from_buffer(buffer, length); - + size_t length; + pb_istream_t stream; /* Note: empty initializer list initializes the struct with all-0. * This is recommended so that unused callbacks are set to NULL instead * of crashing at runtime. */ - TestMessage testmessage = {}; + TestMessage testmessage = {{{NULL}}}; + + SET_BINARY_MODE(stdin); + length = fread(buffer, 1, 1024, stdin); + stream = pb_istream_from_buffer(buffer, length); testmessage.submsg.stringvalue.funcs.decode = &print_string; testmessage.submsg.stringvalue.arg = "submsg {\n stringvalue: \"%s\"\n"; @@ -73,7 +77,7 @@ int main() testmessage.submsg.fixed32value.funcs.decode = &print_fixed32; testmessage.submsg.fixed32value.arg = " fixed32value: %ld\n"; testmessage.submsg.fixed64value.funcs.decode = &print_fixed64; - testmessage.submsg.fixed64value.arg = " fixed64value: %lld\n}\n"; + testmessage.submsg.fixed64value.arg = " fixed64value: %ld\n}\n"; testmessage.stringvalue.funcs.decode = &print_string; testmessage.stringvalue.arg = "stringvalue: \"%s\"\n"; diff --git a/tests/callbacks/encode_callbacks.c b/tests/callbacks/encode_callbacks.c index 3bb6a45e..6cb67b1e 100644 --- a/tests/callbacks/encode_callbacks.c +++ b/tests/callbacks/encode_callbacks.c @@ -4,6 +4,7 @@ #include #include #include "callbacks.pb.h" +#include "test_helpers.h" bool encode_string(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) { @@ -25,19 +26,21 @@ bool encode_int32(pb_ostream_t *stream, const pb_field_t *field, void * const *a bool encode_fixed32(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) { + uint32_t value = 42; + if (!pb_encode_tag_for_field(stream, field)) return false; - uint32_t value = 42; return pb_encode_fixed32(stream, &value); } bool encode_fixed64(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) { + uint64_t value = 42; + if (!pb_encode_tag_for_field(stream, field)) return false; - uint64_t value = 42; return pb_encode_fixed64(stream, &value); } @@ -60,8 +63,10 @@ bool encode_repeatedstring(pb_ostream_t *stream, const pb_field_t *field, void * int main() { uint8_t buffer[1024]; - pb_ostream_t stream = pb_ostream_from_buffer(buffer, 1024); - TestMessage testmessage = {}; + pb_ostream_t stream; + TestMessage testmessage = {{{NULL}}}; + + stream = pb_ostream_from_buffer(buffer, 1024); testmessage.stringvalue.funcs.encode = &encode_string; testmessage.int32value.funcs.encode = &encode_int32; @@ -79,6 +84,7 @@ int main() if (!pb_encode(&stream, TestMessage_fields, &testmessage)) return 1; + SET_BINARY_MODE(stdout); if (fwrite(buffer, stream.bytes_written, 1, stdout) != 1) return 2; diff --git a/tests/extensions/decode_extensions.c b/tests/extensions/decode_extensions.c index ef6a0228..f8ebbde0 100644 --- a/tests/extensions/decode_extensions.c +++ b/tests/extensions/decode_extensions.c @@ -6,6 +6,7 @@ #include #include "alltypes.pb.h" #include "extensions.pb.h" +#include "test_helpers.h" #define TEST(x) if (!(x)) { \ printf("Test " #x " failed.\n"); \ @@ -15,25 +16,39 @@ int main(int argc, char **argv) { uint8_t buffer[1024]; - size_t count = fread(buffer, 1, sizeof(buffer), stdin); - pb_istream_t stream = pb_istream_from_buffer(buffer, count); - - AllTypes alltypes = {}; + size_t count; + pb_istream_t stream; + AllTypes alltypes = {0}; int32_t extensionfield1; - pb_extension_t ext1 = {&AllTypes_extensionfield1, &extensionfield1, NULL}; - alltypes.extensions = &ext1; - - ExtensionMessage extensionfield2 = {}; - pb_extension_t ext2 = {&ExtensionMessage_AllTypes_extensionfield2, &extensionfield2, NULL}; + pb_extension_t ext1; + ExtensionMessage extensionfield2; + pb_extension_t ext2; + + /* Read the message data */ + SET_BINARY_MODE(stdin); + count = fread(buffer, 1, sizeof(buffer), stdin); + stream = pb_istream_from_buffer(buffer, count); + + /* Add the extensions */ + alltypes.extensions = &ext1; + + ext1.type = &AllTypes_extensionfield1; + ext1.dest = &extensionfield1; ext1.next = &ext2; + ext2.type = &ExtensionMessage_AllTypes_extensionfield2; + ext2.dest = &extensionfield2; + ext2.next = NULL; + + /* Decode the message */ if (!pb_decode(&stream, AllTypes_fields, &alltypes)) { printf("Parsing failed: %s\n", PB_GET_ERROR(&stream)); return 1; } + /* Check that the extensions decoded properly */ TEST(extensionfield1 == 12345) TEST(strcmp(extensionfield2.test1, "test") == 0) TEST(extensionfield2.test2 == 54321) diff --git a/tests/extensions/encode_extensions.c b/tests/extensions/encode_extensions.c index 8857f148..dee3597d 100644 --- a/tests/extensions/encode_extensions.c +++ b/tests/extensions/encode_extensions.c @@ -7,25 +7,37 @@ #include #include "alltypes.pb.h" #include "extensions.pb.h" +#include "test_helpers.h" int main(int argc, char **argv) { - AllTypes alltypes = {}; + uint8_t buffer[1024]; + pb_ostream_t stream; + AllTypes alltypes = {0}; int32_t extensionfield1 = 12345; - pb_extension_t ext1 = {&AllTypes_extensionfield1, &extensionfield1, NULL}; + pb_extension_t ext1; + ExtensionMessage extensionfield2 = {"test", 54321}; + pb_extension_t ext2; + + /* Set up the extensions */ alltypes.extensions = &ext1; - ExtensionMessage extensionfield2 = {"test", 54321}; - pb_extension_t ext2 = {&ExtensionMessage_AllTypes_extensionfield2, &extensionfield2, NULL}; + ext1.type = &AllTypes_extensionfield1; + ext1.dest = &extensionfield1; ext1.next = &ext2; - uint8_t buffer[1024]; - pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); + ext2.type = &ExtensionMessage_AllTypes_extensionfield2; + ext2.dest = &extensionfield2; + ext2.next = NULL; + + /* Set up the output stream */ + stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); - /* Now encode it and check if we succeeded. */ + /* Now encode the message and check if we succeeded. */ if (pb_encode(&stream, AllTypes_fields, &alltypes)) { + SET_BINARY_MODE(stdout); fwrite(buffer, 1, stream.bytes_written, stdout); return 0; /* Success */ } diff --git a/tests/missing_fields/missing_fields.c b/tests/missing_fields/missing_fields.c index 27741847..b9a273a2 100644 --- a/tests/missing_fields/missing_fields.c +++ b/tests/missing_fields/missing_fields.c @@ -7,12 +7,13 @@ int main() { - uint8_t buffer[512] = {}; + uint8_t buffer[512]; /* Create a message with one missing field */ { - MissingField msg = {}; + MissingField msg = {0}; pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); + if (!pb_encode(&stream, MissingField_fields, &msg)) { printf("Encode failed.\n"); @@ -22,7 +23,7 @@ int main() /* Test that it decodes properly if we don't require that field */ { - MissingField msg = {}; + MissingField msg = {0}; pb_istream_t stream = pb_istream_from_buffer(buffer, sizeof(buffer)); if (!pb_decode(&stream, MissingField_fields, &msg)) @@ -34,7 +35,7 @@ int main() /* Test that it does *not* decode properly if we require the field */ { - AllFields msg = {}; + AllFields msg = {0}; pb_istream_t stream = pb_istream_from_buffer(buffer, sizeof(buffer)); if (pb_decode(&stream, AllFields_fields, &msg)) diff --git a/tests/special_characters/funny-proto+name has.characters.proto b/tests/special_characters/funny-proto+name has.characters.proto new file mode 100644 index 00000000..e69de29b -- cgit 1.2.3-korg