diff options
Diffstat (limited to 'libs/nanopb/tests/common')
-rw-r--r-- | libs/nanopb/tests/common/SConscript | 48 | ||||
-rw-r--r-- | libs/nanopb/tests/common/malloc_wrappers.c | 54 | ||||
-rw-r--r-- | libs/nanopb/tests/common/malloc_wrappers.h | 7 | ||||
-rw-r--r-- | libs/nanopb/tests/common/malloc_wrappers_syshdr.h | 15 | ||||
-rw-r--r-- | libs/nanopb/tests/common/person.proto | 22 | ||||
-rw-r--r-- | libs/nanopb/tests/common/test_helpers.h | 17 | ||||
-rw-r--r-- | libs/nanopb/tests/common/unittestproto.proto | 43 | ||||
-rw-r--r-- | libs/nanopb/tests/common/unittests.h | 14 |
8 files changed, 220 insertions, 0 deletions
diff --git a/libs/nanopb/tests/common/SConscript b/libs/nanopb/tests/common/SConscript new file mode 100644 index 0000000..05e2f85 --- /dev/null +++ b/libs/nanopb/tests/common/SConscript @@ -0,0 +1,48 @@ +# Build the common files needed by multiple test cases + +Import('env') + +# Protocol definitions for the encode/decode_unittests +env.NanopbProto("unittestproto") + +# Protocol definitions for basic_buffer/stream tests +env.NanopbProto("person") + +#-------------------------------------------- +# Binaries of the pb_decode.c and pb_encode.c +# These are built using more strict warning flags. +strict = env.Clone() +strict.Append(CFLAGS = strict['CORECFLAGS']) +strict.Object("pb_decode.o", "$NANOPB/pb_decode.c") +strict.Object("pb_encode.o", "$NANOPB/pb_encode.c") +strict.Object("pb_common.o", "$NANOPB/pb_common.c") + +#----------------------------------------------- +# Binaries of pb_decode etc. with malloc support +# Uses malloc_wrappers.c to count allocations. +malloc_env = env.Clone() +malloc_env.Append(CPPDEFINES = {'PB_ENABLE_MALLOC': 1, + 'PB_SYSTEM_HEADER': '\\"malloc_wrappers_syshdr.h\\"'}) +malloc_env.Append(CPPPATH = ["$COMMON"]) + +if 'SYSHDR' in malloc_env: + malloc_env.Append(CPPDEFINES = {'PB_OLD_SYSHDR': malloc_env['SYSHDR']}) + +# Disable libmudflap, because it will confuse valgrind +# and other memory leak detection tools. +if '-fmudflap' in env["CCFLAGS"]: + malloc_env["CCFLAGS"].remove("-fmudflap") + malloc_env["LINKFLAGS"].remove("-fmudflap") + malloc_env["LIBS"].remove("mudflap") + +malloc_strict = malloc_env.Clone() +malloc_strict.Append(CFLAGS = malloc_strict['CORECFLAGS']) +malloc_strict.Object("pb_decode_with_malloc.o", "$NANOPB/pb_decode.c") +malloc_strict.Object("pb_encode_with_malloc.o", "$NANOPB/pb_encode.c") +malloc_strict.Object("pb_common_with_malloc.o", "$NANOPB/pb_common.c") + +malloc_env.Object("malloc_wrappers.o", "malloc_wrappers.c") +malloc_env.Depends("$NANOPB/pb.h", ["malloc_wrappers_syshdr.h", "malloc_wrappers.h"]) + +Export("malloc_env") + diff --git a/libs/nanopb/tests/common/malloc_wrappers.c b/libs/nanopb/tests/common/malloc_wrappers.c new file mode 100644 index 0000000..ad69f1c --- /dev/null +++ b/libs/nanopb/tests/common/malloc_wrappers.c @@ -0,0 +1,54 @@ +#include "malloc_wrappers.h" +#include <stdint.h> +#include <assert.h> +#include <string.h> + +static size_t alloc_count = 0; + +/* Allocate memory and place check values before and after. */ +void* malloc_with_check(size_t size) +{ + size_t size32 = (size + 3) / 4 + 3; + uint32_t *buf = malloc(size32 * sizeof(uint32_t)); + buf[0] = size32; + buf[1] = 0xDEADBEEF; + buf[size32 - 1] = 0xBADBAD; + return buf + 2; +} + +/* Free memory allocated with malloc_with_check() and do the checks. */ +void free_with_check(void *mem) +{ + uint32_t *buf = (uint32_t*)mem - 2; + assert(buf[1] == 0xDEADBEEF); + assert(buf[buf[0] - 1] == 0xBADBAD); + free(buf); +} + +/* Track memory usage */ +void* counting_realloc(void *ptr, size_t size) +{ + /* Don't allocate crazy amounts of RAM when fuzzing */ + if (size > 1000000) + return NULL; + + if (!ptr && size) + alloc_count++; + + return realloc(ptr, size); +} + +void counting_free(void *ptr) +{ + if (ptr) + { + assert(alloc_count > 0); + alloc_count--; + free(ptr); + } +} + +size_t get_alloc_count() +{ + return alloc_count; +} diff --git a/libs/nanopb/tests/common/malloc_wrappers.h b/libs/nanopb/tests/common/malloc_wrappers.h new file mode 100644 index 0000000..7eec795 --- /dev/null +++ b/libs/nanopb/tests/common/malloc_wrappers.h @@ -0,0 +1,7 @@ +#include <stdlib.h> + +void* malloc_with_check(size_t size); +void free_with_check(void *mem); +void* counting_realloc(void *ptr, size_t size); +void counting_free(void *ptr); +size_t get_alloc_count(); diff --git a/libs/nanopb/tests/common/malloc_wrappers_syshdr.h b/libs/nanopb/tests/common/malloc_wrappers_syshdr.h new file mode 100644 index 0000000..d295d9e --- /dev/null +++ b/libs/nanopb/tests/common/malloc_wrappers_syshdr.h @@ -0,0 +1,15 @@ +/* This is just a wrapper in order to get our own malloc wrappers into nanopb core. */ + +#define pb_realloc(ptr,size) counting_realloc(ptr,size) +#define pb_free(ptr) counting_free(ptr) + +#ifdef PB_OLD_SYSHDR +#include PB_OLD_SYSHDR +#else +#include <stdint.h> +#include <stddef.h> +#include <stdbool.h> +#include <string.h> +#endif + +#include <malloc_wrappers.h> diff --git a/libs/nanopb/tests/common/person.proto b/libs/nanopb/tests/common/person.proto new file mode 100644 index 0000000..becefdf --- /dev/null +++ b/libs/nanopb/tests/common/person.proto @@ -0,0 +1,22 @@ +syntax = "proto2"; + +import "nanopb.proto"; + +message Person { + required string name = 1 [(nanopb).max_size = 40]; + required int32 id = 2; + optional string email = 3 [(nanopb).max_size = 40]; + + enum PhoneType { + MOBILE = 0; + HOME = 1; + WORK = 2; + } + + message PhoneNumber { + required string number = 1 [(nanopb).max_size = 40]; + optional PhoneType type = 2 [default = HOME]; + } + + repeated PhoneNumber phone = 4 [(nanopb).max_count = 5]; +} diff --git a/libs/nanopb/tests/common/test_helpers.h b/libs/nanopb/tests/common/test_helpers.h new file mode 100644 index 0000000..f77760a --- /dev/null +++ b/libs/nanopb/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 <io.h> +#include <fcntl.h> +#define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY) + +#else +#define SET_BINARY_MODE(file) + +#endif + + +#endif diff --git a/libs/nanopb/tests/common/unittestproto.proto b/libs/nanopb/tests/common/unittestproto.proto new file mode 100644 index 0000000..23b5b97 --- /dev/null +++ b/libs/nanopb/tests/common/unittestproto.proto @@ -0,0 +1,43 @@ +syntax = "proto2"; + +import 'nanopb.proto'; + +message IntegerArray { + repeated int32 data = 1 [(nanopb).max_count = 10]; +} + +message FloatArray { + repeated float data = 1 [(nanopb).max_count = 10]; +} + +message StringMessage { + required string data = 1 [(nanopb).max_size = 10]; +} + +message BytesMessage { + required bytes data = 1 [(nanopb).max_size = 16]; +} + +message CallbackArray { + // We cheat a bit and use this message for testing other types, too. + // Nanopb does not care about the actual defined data type for callback + // fields. + repeated int32 data = 1; +} + +message IntegerContainer { + required IntegerArray submsg = 1; +} + +message CallbackContainer { + required CallbackArray submsg = 1; +} + +message CallbackContainerContainer { + required CallbackContainer submsg = 1; +} + +message StringPointerContainer { + repeated string rep_str = 1 [(nanopb).type = FT_POINTER]; +} + diff --git a/libs/nanopb/tests/common/unittests.h b/libs/nanopb/tests/common/unittests.h new file mode 100644 index 0000000..c2b470a --- /dev/null +++ b/libs/nanopb/tests/common/unittests.h @@ -0,0 +1,14 @@ +#include <stdio.h> + +#define COMMENT(x) printf("\n----" x "----\n"); +#define STR(x) #x +#define STR2(x) STR(x) +#define TEST(x) \ + if (!(x)) { \ + fprintf(stderr, "\033[31;1mFAILED:\033[22;39m " __FILE__ ":" STR2(__LINE__) " " #x "\n"); \ + status = 1; \ + } else { \ + printf("\033[32;1mOK:\033[22;39m " #x "\n"); \ + } + + |