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/decode_unittests/decode_unittests.c | 306 ++++++++++++++++++++++++++++++ 1 file changed, 306 insertions(+) create mode 100644 tests/decode_unittests/decode_unittests.c (limited to 'tests/decode_unittests/decode_unittests.c') diff --git a/tests/decode_unittests/decode_unittests.c b/tests/decode_unittests/decode_unittests.c new file mode 100644 index 00000000..6ad05f00 --- /dev/null +++ b/tests/decode_unittests/decode_unittests.c @@ -0,0 +1,306 @@ +#define NANOPB_INTERNALS + +#include +#include +#include "pb_decode.h" +#include "unittests.h" +#include "unittestproto.pb.h" + +#define S(x) pb_istream_from_buffer((uint8_t*)x, sizeof(x) - 1) + +bool stream_callback(pb_istream_t *stream, uint8_t *buf, size_t count) +{ + if (stream->state != NULL) + return false; /* Simulate error */ + + if (buf != NULL) + memset(buf, 'x', count); + return true; +} + +/* Verifies that the stream passed to callback matches the byte array pointed to by arg. */ +bool callback_check(pb_istream_t *stream, const pb_field_t *field, void **arg) +{ + int i; + uint8_t byte; + pb_bytes_array_t *ref = (pb_bytes_array_t*) *arg; + + for (i = 0; i < ref->size; i++) + { + if (!pb_read(stream, &byte, 1)) + return false; + + if (byte != ref->bytes[i]) + return false; + } + + return true; +} + +int main() +{ + int status = 0; + + { + uint8_t buffer1[] = "foobartest1234"; + uint8_t buffer2[sizeof(buffer1)]; + pb_istream_t stream = pb_istream_from_buffer(buffer1, sizeof(buffer1)); + + COMMENT("Test pb_read and pb_istream_t"); + TEST(pb_read(&stream, buffer2, 6)) + TEST(memcmp(buffer2, "foobar", 6) == 0) + TEST(stream.bytes_left == sizeof(buffer1) - 6) + TEST(pb_read(&stream, buffer2 + 6, stream.bytes_left)) + TEST(memcmp(buffer1, buffer2, sizeof(buffer1)) == 0) + TEST(stream.bytes_left == 0) + TEST(!pb_read(&stream, buffer2, 1)) + } + + { + uint8_t buffer[20]; + pb_istream_t stream = {&stream_callback, NULL, 20}; + + COMMENT("Test pb_read with custom callback"); + TEST(pb_read(&stream, buffer, 5)) + TEST(memcmp(buffer, "xxxxx", 5) == 0) + TEST(!pb_read(&stream, buffer, 50)) + stream.state = (void*)1; /* Simulated error return from callback */ + TEST(!pb_read(&stream, buffer, 5)) + stream.state = NULL; + TEST(pb_read(&stream, buffer, 15)) + } + + { + pb_istream_t s; + uint64_t u; + int64_t i; + + COMMENT("Test pb_decode_varint"); + TEST((s = S("\x00"), pb_decode_varint(&s, &u) && u == 0)); + TEST((s = S("\x01"), pb_decode_varint(&s, &u) && u == 1)); + TEST((s = S("\xAC\x02"), pb_decode_varint(&s, &u) && u == 300)); + TEST((s = S("\xFF\xFF\xFF\xFF\x0F"), pb_decode_varint(&s, &u) && u == UINT32_MAX)); + TEST((s = S("\xFF\xFF\xFF\xFF\x0F"), pb_decode_varint(&s, (uint64_t*)&i) && i == UINT32_MAX)); + TEST((s = S("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"), + pb_decode_varint(&s, (uint64_t*)&i) && i == -1)); + TEST((s = S("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"), + pb_decode_varint(&s, &u) && u == UINT64_MAX)); + } + + { + pb_istream_t s; + COMMENT("Test pb_skip_varint"); + TEST((s = S("\x00""foobar"), pb_skip_varint(&s) && s.bytes_left == 6)) + TEST((s = S("\xAC\x02""foobar"), pb_skip_varint(&s) && s.bytes_left == 6)) + TEST((s = S("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01""foobar"), + pb_skip_varint(&s) && s.bytes_left == 6)) + TEST((s = S("\xFF"), !pb_skip_varint(&s))) + } + + { + pb_istream_t s; + COMMENT("Test pb_skip_string") + TEST((s = S("\x00""foobar"), pb_skip_string(&s) && s.bytes_left == 6)) + TEST((s = S("\x04""testfoobar"), pb_skip_string(&s) && s.bytes_left == 6)) + TEST((s = S("\x04"), !pb_skip_string(&s))) + TEST((s = S("\xFF"), !pb_skip_string(&s))) + } + + { + pb_istream_t s = S("\x01\xFF\xFF\x03"); + pb_field_t f = {1, PB_LTYPE_VARINT, 0, 0, 4, 0, 0}; + uint32_t d; + COMMENT("Test pb_dec_varint using uint32_t") + TEST(pb_dec_varint(&s, &f, &d) && d == 1) + + /* Verify that no more than data_size is written. */ + d = 0; + f.data_size = 1; + TEST(pb_dec_varint(&s, &f, &d) && (d == 0xFF || d == 0xFF000000)) + } + + { + pb_istream_t s; + pb_field_t f = {1, PB_LTYPE_SVARINT, 0, 0, 4, 0, 0}; + int32_t d; + + COMMENT("Test pb_dec_svarint using int32_t") + TEST((s = S("\x01"), pb_dec_svarint(&s, &f, &d) && d == -1)) + TEST((s = S("\x02"), pb_dec_svarint(&s, &f, &d) && d == 1)) + TEST((s = S("\xfe\xff\xff\xff\x0f"), pb_dec_svarint(&s, &f, &d) && d == INT32_MAX)) + TEST((s = S("\xff\xff\xff\xff\x0f"), pb_dec_svarint(&s, &f, &d) && d == INT32_MIN)) + } + + { + pb_istream_t s; + pb_field_t f = {1, PB_LTYPE_SVARINT, 0, 0, 8, 0, 0}; + uint64_t d; + + COMMENT("Test pb_dec_svarint using uint64_t") + TEST((s = S("\x01"), pb_dec_svarint(&s, &f, &d) && d == -1)) + TEST((s = S("\x02"), pb_dec_svarint(&s, &f, &d) && d == 1)) + TEST((s = S("\xFE\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"), pb_dec_svarint(&s, &f, &d) && d == INT64_MAX)) + TEST((s = S("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"), pb_dec_svarint(&s, &f, &d) && d == INT64_MIN)) + } + + { + pb_istream_t s; + pb_field_t f = {1, PB_LTYPE_FIXED32, 0, 0, 4, 0, 0}; + float d; + + COMMENT("Test pb_dec_fixed32 using float (failures here may be caused by imperfect rounding)") + TEST((s = S("\x00\x00\x00\x00"), pb_dec_fixed32(&s, &f, &d) && d == 0.0f)) + TEST((s = S("\x00\x00\xc6\x42"), pb_dec_fixed32(&s, &f, &d) && d == 99.0f)) + TEST((s = S("\x4e\x61\x3c\xcb"), pb_dec_fixed32(&s, &f, &d) && d == -12345678.0f)) + TEST((s = S("\x00"), !pb_dec_fixed32(&s, &f, &d) && d == -12345678.0f)) + } + + { + pb_istream_t s; + pb_field_t f = {1, PB_LTYPE_FIXED64, 0, 0, 8, 0, 0}; + double d; + + COMMENT("Test pb_dec_fixed64 using double (failures here may be caused by imperfect rounding)") + TEST((s = S("\x00\x00\x00\x00\x00\x00\x00\x00"), pb_dec_fixed64(&s, &f, &d) && d == 0.0)) + TEST((s = S("\x00\x00\x00\x00\x00\xc0\x58\x40"), pb_dec_fixed64(&s, &f, &d) && d == 99.0)) + TEST((s = S("\x00\x00\x00\xc0\x29\x8c\x67\xc1"), pb_dec_fixed64(&s, &f, &d) && d == -12345678.0f)) + } + + { + pb_istream_t s; + struct { size_t size; uint8_t bytes[5]; } d; + pb_field_t f = {1, PB_LTYPE_BYTES, 0, 0, sizeof(d), 0, 0}; + + COMMENT("Test pb_dec_bytes") + TEST((s = S("\x00"), pb_dec_bytes(&s, &f, &d) && d.size == 0)) + TEST((s = S("\x01\xFF"), pb_dec_bytes(&s, &f, &d) && d.size == 1 && d.bytes[0] == 0xFF)) + TEST((s = S("\x05xxxxx"), pb_dec_bytes(&s, &f, &d) && d.size == 5)) + TEST((s = S("\x05xxxx"), !pb_dec_bytes(&s, &f, &d))) + + /* Note: the size limit on bytes-fields is not strictly obeyed, as + * the compiler may add some padding to the struct. Using this padding + * is not a very good thing to do, but it is difficult to avoid when + * we use only a single uint8_t to store the size of the field. + * Therefore this tests against a 10-byte string, while otherwise even + * 6 bytes should error out. + */ + TEST((s = S("\x10xxxxxxxxxx"), !pb_dec_bytes(&s, &f, &d))) + } + + { + pb_istream_t s; + pb_field_t f = {1, PB_LTYPE_STRING, 0, 0, 5, 0, 0}; + char d[5]; + + COMMENT("Test pb_dec_string") + TEST((s = S("\x00"), pb_dec_string(&s, &f, &d) && d[0] == '\0')) + TEST((s = S("\x04xyzz"), pb_dec_string(&s, &f, &d) && strcmp(d, "xyzz") == 0)) + TEST((s = S("\x05xyzzy"), !pb_dec_string(&s, &f, &d))) + } + + { + pb_istream_t s; + IntegerArray dest; + + COMMENT("Testing pb_decode with repeated int32 field") + TEST((s = S(""), pb_decode(&s, IntegerArray_fields, &dest) && dest.data_count == 0)) + TEST((s = S("\x08\x01\x08\x02"), pb_decode(&s, IntegerArray_fields, &dest) + && dest.data_count == 2 && dest.data[0] == 1 && dest.data[1] == 2)) + s = S("\x08\x01\x08\x02\x08\x03\x08\x04\x08\x05\x08\x06\x08\x07\x08\x08\x08\x09\x08\x0A"); + TEST(pb_decode(&s, IntegerArray_fields, &dest) && dest.data_count == 10 && dest.data[9] == 10) + s = S("\x08\x01\x08\x02\x08\x03\x08\x04\x08\x05\x08\x06\x08\x07\x08\x08\x08\x09\x08\x0A\x08\x0B"); + TEST(!pb_decode(&s, IntegerArray_fields, &dest)) + } + + { + pb_istream_t s; + IntegerArray dest; + + COMMENT("Testing pb_decode with packed int32 field") + TEST((s = S("\x0A\x00"), pb_decode(&s, IntegerArray_fields, &dest) + && dest.data_count == 0)) + TEST((s = S("\x0A\x01\x01"), pb_decode(&s, IntegerArray_fields, &dest) + && dest.data_count == 1 && dest.data[0] == 1)) + TEST((s = S("\x0A\x0A\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A"), pb_decode(&s, IntegerArray_fields, &dest) + && dest.data_count == 10 && dest.data[0] == 1 && dest.data[9] == 10)) + TEST((s = S("\x0A\x0B\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B"), !pb_decode(&s, IntegerArray_fields, &dest))) + + /* Test invalid wire data */ + TEST((s = S("\x0A\xFF"), !pb_decode(&s, IntegerArray_fields, &dest))) + TEST((s = S("\x0A\x01"), !pb_decode(&s, IntegerArray_fields, &dest))) + } + + { + pb_istream_t s; + IntegerArray dest; + + COMMENT("Testing pb_decode with unknown fields") + TEST((s = S("\x18\x0F\x08\x01"), pb_decode(&s, IntegerArray_fields, &dest) + && dest.data_count == 1 && dest.data[0] == 1)) + TEST((s = S("\x19\x00\x00\x00\x00\x00\x00\x00\x00\x08\x01"), pb_decode(&s, IntegerArray_fields, &dest) + && dest.data_count == 1 && dest.data[0] == 1)) + TEST((s = S("\x1A\x00\x08\x01"), pb_decode(&s, IntegerArray_fields, &dest) + && dest.data_count == 1 && dest.data[0] == 1)) + TEST((s = S("\x1B\x08\x01"), !pb_decode(&s, IntegerArray_fields, &dest))) + TEST((s = S("\x1D\x00\x00\x00\x00\x08\x01"), pb_decode(&s, IntegerArray_fields, &dest) + && dest.data_count == 1 && dest.data[0] == 1)) + } + + { + pb_istream_t s; + CallbackArray dest; + struct { size_t size; uint8_t bytes[10]; } ref; + dest.data.funcs.decode = &callback_check; + dest.data.arg = &ref; + + COMMENT("Testing pb_decode with callbacks") + /* Single varint */ + ref.size = 1; ref.bytes[0] = 0x55; + TEST((s = S("\x08\x55"), pb_decode(&s, CallbackArray_fields, &dest))) + /* Packed varint */ + ref.size = 3; ref.bytes[0] = ref.bytes[1] = ref.bytes[2] = 0x55; + TEST((s = S("\x0A\x03\x55\x55\x55"), pb_decode(&s, CallbackArray_fields, &dest))) + /* Packed varint with loop */ + ref.size = 1; ref.bytes[0] = 0x55; + TEST((s = S("\x0A\x03\x55\x55\x55"), pb_decode(&s, CallbackArray_fields, &dest))) + /* Single fixed32 */ + ref.size = 4; ref.bytes[0] = ref.bytes[1] = ref.bytes[2] = ref.bytes[3] = 0xAA; + TEST((s = S("\x0D\xAA\xAA\xAA\xAA"), pb_decode(&s, CallbackArray_fields, &dest))) + /* Single fixed64 */ + ref.size = 8; memset(ref.bytes, 0xAA, 8); + TEST((s = S("\x09\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"), pb_decode(&s, CallbackArray_fields, &dest))) + /* Unsupported field type */ + TEST((s = S("\x0B\x00"), !pb_decode(&s, CallbackArray_fields, &dest))) + + /* Just make sure that our test function works */ + ref.size = 1; ref.bytes[0] = 0x56; + TEST((s = S("\x08\x55"), !pb_decode(&s, CallbackArray_fields, &dest))) + } + + { + pb_istream_t s; + IntegerArray dest; + + COMMENT("Testing pb_decode message termination") + TEST((s = S(""), pb_decode(&s, IntegerArray_fields, &dest))) + TEST((s = S("\x00"), pb_decode(&s, IntegerArray_fields, &dest))) + TEST((s = S("\x08\x01"), pb_decode(&s, IntegerArray_fields, &dest))) + TEST((s = S("\x08\x01\x00"), pb_decode(&s, IntegerArray_fields, &dest))) + TEST((s = S("\x08"), !pb_decode(&s, IntegerArray_fields, &dest))) + } + + { + pb_istream_t s; + IntegerContainer dest = {}; + + COMMENT("Testing pb_decode_delimited") + TEST((s = S("\x09\x0A\x07\x0A\x05\x01\x02\x03\x04\x05"), + pb_decode_delimited(&s, IntegerContainer_fields, &dest)) && + dest.submsg.data_count == 5) + } + + if (status != 0) + fprintf(stdout, "\n\nSome tests FAILED!\n"); + + return status; +} -- cgit 1.2.3-korg From f04ab838abd90fb70f7d6ef77fdacdf07f09ba4d Mon Sep 17 00:00:00 2001 From: Petteri Aimonen Date: Tue, 10 Sep 2013 17:44:32 +0300 Subject: Build fixes for Windows/Visual C++ --- compat/pb_syshdr.h | 23 ++++++++++++++ tests/SConstruct | 52 +++++++++++++++++++++++++------ tests/alltypes/SConscript | 8 ++--- tests/alltypes/encode_alltypes.c | 26 +++++++++------- tests/basic_buffer/SConscript | 8 ++--- tests/basic_buffer/decode_buffer.c | 11 +++++-- tests/basic_buffer/encode_buffer.c | 11 +++++-- tests/common/test_helpers.h | 17 ++++++++++ tests/cxx_main_program/SConscript | 8 ++--- tests/decode_unittests/SConscript | 4 +-- tests/decode_unittests/decode_unittests.c | 2 +- tests/encode_unittests/SConscript | 4 +-- tests/site_scons/site_init.py | 16 +++++++--- 13 files changed, 141 insertions(+), 49 deletions(-) create mode 100644 tests/common/test_helpers.h (limited to 'tests/decode_unittests/decode_unittests.c') diff --git a/compat/pb_syshdr.h b/compat/pb_syshdr.h index c170c8de..b69a1671 100644 --- a/compat/pb_syshdr.h +++ b/compat/pb_syshdr.h @@ -12,6 +12,9 @@ #define _PB_SYSHDR_H_ /* stdint.h subset */ +#ifdef HAVE_STDINT_H +#include +#else /* You will need to modify these to match the word size of your platform. */ typedef signed char int8_t; typedef unsigned char uint8_t; @@ -21,8 +24,13 @@ typedef signed int int32_t; typedef unsigned int uint32_t; typedef signed long long int64_t; typedef unsigned long long uint64_t; +#endif /* stddef.h subset */ +#ifdef HAVE_STDDEF_H +#include +#else + typedef uint32_t size_t; #define offsetof(st, m) ((size_t)(&((st *)0)->m)) @@ -30,12 +38,26 @@ typedef uint32_t size_t; #define NULL 0 #endif +#endif + /* stdbool.h subset */ +#ifdef HAVE_STDBOOL_H +#include +#else + +#ifndef __cplusplus typedef int bool; #define false 0 #define true 1 +#endif + +#endif /* string.h subset */ +#ifdef HAVE_STRING_H +#include +#else + /* Implementations are from the Public Domain C Library (PDCLib). */ static size_t strlen( const char * s ) { @@ -67,5 +89,6 @@ static void * memset( void * s, int c, size_t n ) } return s; } +#endif #endif diff --git a/tests/SConstruct b/tests/SConstruct index a690aff0..8bbb31d1 100644 --- a/tests/SConstruct +++ b/tests/SConstruct @@ -1,4 +1,5 @@ -env = DefaultEnvironment() +import os +env = Environment(ENV = {'PATH': os.environ['PATH']}) # Add the builders defined in site_init.py add_nanopb_builders(env) @@ -7,18 +8,49 @@ add_nanopb_builders(env) env.Append(CPPPATH = ["#../", "#common"]) # Path for finding nanopb.proto -env.Append(PROTOCPATH = ['#../generator', '/usr/include', '.']) +env.Append(PROTOCPATH = '#../generator') -# Define the include path to find nanopb.proto -env.Append(PROTOCPATH = ['#../generator', '/usr/include', '.']) +# Check the compilation environment, unless we are just cleaning up. +if not env.GetOption('clean'): + conf = Configure(env) -# If the platform doesn't support C99, use our own header file instead. -conf = Configure(env) -if not conf.CheckCHeader('stdbool.h'): - conf.env.Append(CPPDEFINES = {'PB_SYSTEM_HEADER': '\\"pb_syshdr.h\\"'}) - conf.env.Append(CPPPATH = "#../compat") -env = conf.Finish() + # If the platform doesn't support C99, use our own header file instead. + stdbool = conf.CheckCHeader('stdbool.h') + stdint = conf.CheckCHeader('stdint.h') + stddef = conf.CheckCHeader('stddef.h') + string = conf.CheckCHeader('string.h') + if not stdbool or not stdint or not stddef or not string: + conf.env.Append(CPPDEFINES = {'PB_SYSTEM_HEADER': '\\"pb_syshdr.h\\"'}) + conf.env.Append(CPPPATH = "#../compat") + + if stdbool: conf.env.Append(CPPDEFINES = {'HAVE_STDBOOL_H': 1}) + if stdint: conf.env.Append(CPPDEFINES = {'HAVE_STDINT_H': 1}) + if stddef: conf.env.Append(CPPDEFINES = {'HAVE_STDDEF_H': 1}) + if string: conf.env.Append(CPPDEFINES = {'HAVE_STRING_H': 1}) + + # Check if we can use pkg-config to find protobuf include path + status, output = conf.TryAction('pkg-config protobuf --variable=includedir > $TARGET') + if status: + conf.env.Append(PROTOCPATH = output.strip()) + else: + conf.env.Append(PROTOCPATH = '/usr/include') + + # End the config stuff + env = conf.Finish() +# Initialize the CCFLAGS according to the compiler +if 'cl' in env['CC']: + # Microsoft Visual C++ + + # Debug info on, warning level 2 for tests, warnings as errors + env.Append(CCFLAGS = '/Zi /W2 /WX') + env.Append(LINKFLAGS = '/DEBUG') + + # PB_RETURN_ERROR triggers C4127 because of while(0) + env.Append(CCFLAGS = '/wd4127') + + + # Now include the SConscript files from all subdirectories SConscript(Glob('*/SConscript'), exports = 'env') diff --git a/tests/alltypes/SConscript b/tests/alltypes/SConscript index 3c8adc48..8aa45b6b 100644 --- a/tests/alltypes/SConscript +++ b/tests/alltypes/SConscript @@ -4,9 +4,9 @@ Import("env") env.NanopbProto("alltypes") -env.Program(["encode_alltypes.c", "alltypes.pb.c", "#common/pb_encode.o"]) -env.Program(["decode_alltypes.c", "alltypes.pb.c", "#common/pb_decode.o"]) +enc = env.Program(["encode_alltypes.c", "alltypes.pb.c", "#common/pb_encode.o"]) +dec = env.Program(["decode_alltypes.c", "alltypes.pb.c", "#common/pb_decode.o"]) -env.RunTest("encode_alltypes") -env.RunTest(["decode_alltypes", "encode_alltypes.output"]) +env.RunTest(enc) +env.RunTest([dec, "encode_alltypes.output"]) diff --git a/tests/alltypes/encode_alltypes.c b/tests/alltypes/encode_alltypes.c index 982ad3c8..802e1574 100644 --- a/tests/alltypes/encode_alltypes.c +++ b/tests/alltypes/encode_alltypes.c @@ -113,18 +113,20 @@ 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: %s\n", PB_GET_ERROR(&stream)); - 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)) + { + fwrite(buffer, 1, stream.bytes_written, stdout); + return 0; /* Success */ + } + else + { + fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream)); + return 1; /* Failure */ + } } } diff --git a/tests/basic_buffer/SConscript b/tests/basic_buffer/SConscript index 5b85e132..349fb14f 100644 --- a/tests/basic_buffer/SConscript +++ b/tests/basic_buffer/SConscript @@ -2,11 +2,11 @@ Import("env") -env.Program(["encode_buffer.c", "#common/person.pb.c", "#common/pb_encode.o"]) -env.Program(["decode_buffer.c", "#common/person.pb.c", "#common/pb_decode.o"]) +enc = env.Program(["encode_buffer.c", "#common/person.pb.c", "#common/pb_encode.o"]) +dec = env.Program(["decode_buffer.c", "#common/person.pb.c", "#common/pb_decode.o"]) -env.RunTest("encode_buffer") -env.RunTest(["decode_buffer", "encode_buffer.output"]) +env.RunTest(enc) +env.RunTest([dec, "encode_buffer.output"]) env.Decode(["encode_buffer.output", "#common/person.proto"], MESSAGE = "Person") env.Compare(["decode_buffer.output", "encode_buffer.decoded"]) diff --git a/tests/basic_buffer/decode_buffer.c b/tests/basic_buffer/decode_buffer.c index 56bbd8f8..d231c916 100644 --- a/tests/basic_buffer/decode_buffer.c +++ b/tests/basic_buffer/decode_buffer.c @@ -9,6 +9,7 @@ #include #include #include "person.pb.h" +#include "test_helpers.h" /* This function is called once from main(), it handles the decoding and printing. */ @@ -59,9 +60,13 @@ bool print_person(pb_istream_t *stream) int main() { - /* Read the data into buffer */ uint8_t buffer[512]; - size_t count = fread(buffer, 1, sizeof(buffer), stdin); + pb_istream_t stream; + size_t count; + + /* Read the data into buffer */ + SET_BINARY_MODE(stdin); + count = fread(buffer, 1, sizeof(buffer), stdin); if (!feof(stdin)) { @@ -70,7 +75,7 @@ int main() } /* Construct a pb_istream_t for reading from the buffer */ - pb_istream_t stream = pb_istream_from_buffer(buffer, count); + stream = pb_istream_from_buffer(buffer, count); /* Decode and print out the stuff */ if (!print_person(&stream)) diff --git a/tests/basic_buffer/encode_buffer.c b/tests/basic_buffer/encode_buffer.c index 742c99f4..d3e4f6e6 100644 --- a/tests/basic_buffer/encode_buffer.c +++ b/tests/basic_buffer/encode_buffer.c @@ -6,9 +6,13 @@ #include #include #include "person.pb.h" +#include "test_helpers.h" int main() { + uint8_t buffer[512]; + pb_ostream_t stream; + /* Initialize the structure with constants */ Person person = {"Test Person 99", 99, true, "test@person.com", 3, {{"555-12345678", true, Person_PhoneType_MOBILE}, @@ -16,12 +20,13 @@ int main() {"1234-5678", true, Person_PhoneType_WORK}, }}; - uint8_t buffer[512]; - pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); + stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); /* Now encode it and check if we succeeded. */ if (pb_encode(&stream, Person_fields, &person)) - { + { + /* Write the result data to stdout */ + SET_BINARY_MODE(stdout); fwrite(buffer, 1, stream.bytes_written, stdout); return 0; /* Success */ } diff --git a/tests/common/test_helpers.h b/tests/common/test_helpers.h new file mode 100644 index 00000000..f77760a5 --- /dev/null +++ b/tests/common/test_helpers.h @@ -0,0 +1,17 @@ +/* Compatibility helpers for the test programs. */ + +#ifndef _TEST_HELPERS_H_ +#define _TEST_HELPERS_H_ + +#ifdef _WIN32 +#include +#include +#define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY) + +#else +#define SET_BINARY_MODE(file) + +#endif + + +#endif diff --git a/tests/cxx_main_program/SConscript b/tests/cxx_main_program/SConscript index b5482319..0d0a25ca 100644 --- a/tests/cxx_main_program/SConscript +++ b/tests/cxx_main_program/SConscript @@ -13,8 +13,8 @@ Command("encode_alltypes.cxx", "#alltypes/encode_alltypes.c", c) Command("decode_alltypes.cxx", "#alltypes/decode_alltypes.c", c) # Now build and run the test normally. -env.Program(["encode_alltypes.cxx", "alltypes.pb.cxx", "pb_encode.cxx"]) -env.Program(["decode_alltypes.cxx", "alltypes.pb.cxx", "pb_decode.cxx"]) +enc = env.Program(["encode_alltypes.cxx", "alltypes.pb.cxx", "pb_encode.cxx"]) +dec = env.Program(["decode_alltypes.cxx", "alltypes.pb.cxx", "pb_decode.cxx"]) -env.RunTest("encode_alltypes") -env.RunTest(["decode_alltypes", "encode_alltypes.output"]) +env.RunTest(enc) +env.RunTest([dec, "encode_alltypes.output"]) diff --git a/tests/decode_unittests/SConscript b/tests/decode_unittests/SConscript index 860d7738..5e0f8407 100644 --- a/tests/decode_unittests/SConscript +++ b/tests/decode_unittests/SConscript @@ -1,4 +1,4 @@ Import('env') -env.Program(["decode_unittests.c", "#common/unittestproto.pb.c", "#common/pb_decode.o"]) -env.RunTest('decode_unittests') +p = env.Program(["decode_unittests.c", "#common/unittestproto.pb.c", "#common/pb_decode.o"]) +env.RunTest(p) diff --git a/tests/decode_unittests/decode_unittests.c b/tests/decode_unittests/decode_unittests.c index 6ad05f00..9c447a57 100644 --- a/tests/decode_unittests/decode_unittests.c +++ b/tests/decode_unittests/decode_unittests.c @@ -291,7 +291,7 @@ int main() { pb_istream_t s; - IntegerContainer dest = {}; + IntegerContainer dest = {{0}}; COMMENT("Testing pb_decode_delimited") TEST((s = S("\x09\x0A\x07\x0A\x05\x01\x02\x03\x04\x05"), diff --git a/tests/encode_unittests/SConscript b/tests/encode_unittests/SConscript index 0864a910..6a5ffcff 100644 --- a/tests/encode_unittests/SConscript +++ b/tests/encode_unittests/SConscript @@ -1,5 +1,5 @@ # Build and run the stand-alone unit tests for the nanopb encoder part. Import('env') -env.Program(["encode_unittests.c", "#common/unittestproto.pb.c", "#common/pb_encode.o"]) -env.RunTest('encode_unittests') +p = env.Program(["encode_unittests.c", "#common/unittestproto.pb.c", "#common/pb_encode.o"]) +env.RunTest(p) diff --git a/tests/site_scons/site_init.py b/tests/site_scons/site_init.py index 1383067d..b69db646 100644 --- a/tests/site_scons/site_init.py +++ b/tests/site_scons/site_init.py @@ -1,6 +1,13 @@ import subprocess import sys +try: + # Make terminal colors work on windows + import colorama + colorama.init() +except ImportError: + pass + def add_nanopb_builders(env): '''Add the necessary builder commands for nanopb tests.''' @@ -14,6 +21,7 @@ def add_nanopb_builders(env): src_suffix = '.proto') env.Append(BUILDERS = {'Proto': proto_file_builder}) env.SetDefault(PROTOC = 'protoc') + env.SetDefault(PROTOCPATH = ['.']) # Build command for running nanopb generator import os.path @@ -50,9 +58,9 @@ def add_nanopb_builders(env): stderr = sys.stderr) result = pipe.wait() if result == 0: - print '\033[92m[ OK ]\033[0m Ran ' + str(source[0]) + print '\033[32m[ OK ]\033[0m Ran ' + str(source[0]) else: - print '\033[91m[FAIL]\033[0m Program ' + str(source[0]) + ' returned ' + str(result) + print '\033[31m[FAIL]\033[0m Program ' + str(source[0]) + ' returned ' + str(result) return result run_test_builder = Builder(action = run_test, @@ -73,10 +81,10 @@ def add_nanopb_builders(env): data1 = open(str(source[0]), 'rb').read() data2 = open(str(source[1]), 'rb').read() if data1 == data2: - print '\033[92m[ OK ]\033[0m Files equal: ' + str(source[0]) + ' and ' + str(source[1]) + print '\033[32m[ OK ]\033[0m Files equal: ' + str(source[0]) + ' and ' + str(source[1]) return 0 else: - print '\033[91m[FAIL]\033[0m Files differ: ' + str(source[0]) + ' and ' + str(source[1]) + print '\033[31m[FAIL]\033[0m Files differ: ' + str(source[0]) + ' and ' + str(source[1]) return 1 compare_builder = Builder(action = compare_files, -- cgit 1.2.3-korg