aboutsummaryrefslogtreecommitdiffstats
path: root/libs/nanopb/tests
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2017-06-20 10:24:05 +0000
committerRomain Forlot <romain.forlot@iot.bzh>2017-06-20 10:24:05 +0000
commit32e25cbca210a359b09768537b6f443fe90a3070 (patch)
tree3309794c15d8a8f8e9c1c08cad072ee1378813ba /libs/nanopb/tests
parent76c43dec62b2e21cd6446360c00d4fe6b437533f (diff)
Separation Generator to a dedicated repo
Change-Id: Id94831651c3266861435272a6e36c7884bef2c45 Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'libs/nanopb/tests')
-rw-r--r--libs/nanopb/tests/Makefile21
-rw-r--r--libs/nanopb/tests/SConstruct157
-rw-r--r--libs/nanopb/tests/alltypes/SConscript35
-rw-r--r--libs/nanopb/tests/alltypes/alltypes.options3
-rw-r--r--libs/nanopb/tests/alltypes/alltypes.proto125
-rw-r--r--libs/nanopb/tests/alltypes/decode_alltypes.c229
-rw-r--r--libs/nanopb/tests/alltypes/encode_alltypes.c155
-rw-r--r--libs/nanopb/tests/alltypes_callback/SConscript28
-rw-r--r--libs/nanopb/tests/alltypes_callback/alltypes.options8
-rw-r--r--libs/nanopb/tests/alltypes_callback/decode_alltypes_callback.c442
-rw-r--r--libs/nanopb/tests/alltypes_callback/encode_alltypes_callback.c411
-rw-r--r--libs/nanopb/tests/alltypes_pointer/SConscript40
-rw-r--r--libs/nanopb/tests/alltypes_pointer/alltypes.options4
-rw-r--r--libs/nanopb/tests/alltypes_pointer/decode_alltypes_pointer.c186
-rw-r--r--libs/nanopb/tests/alltypes_pointer/encode_alltypes_pointer.c200
-rw-r--r--libs/nanopb/tests/alltypes_proto3/SConscript45
-rw-r--r--libs/nanopb/tests/alltypes_proto3/alltypes.options4
-rw-r--r--libs/nanopb/tests/alltypes_proto3/alltypes.proto100
-rw-r--r--libs/nanopb/tests/alltypes_proto3/decode_alltypes.c167
-rw-r--r--libs/nanopb/tests/alltypes_proto3/encode_alltypes.c111
-rw-r--r--libs/nanopb/tests/alltypes_proto3_callback/SConscript23
-rw-r--r--libs/nanopb/tests/alltypes_proto3_callback/alltypes.options8
-rw-r--r--libs/nanopb/tests/alltypes_proto3_callback/decode_alltypes_callback.c376
-rw-r--r--libs/nanopb/tests/alltypes_proto3_callback/encode_alltypes_callback.c343
-rw-r--r--libs/nanopb/tests/anonymous_oneof/SConscript30
-rw-r--r--libs/nanopb/tests/anonymous_oneof/decode_oneof.c88
-rw-r--r--libs/nanopb/tests/anonymous_oneof/oneof.proto23
-rw-r--r--libs/nanopb/tests/backwards_compatibility/SConscript11
-rw-r--r--libs/nanopb/tests/backwards_compatibility/alltypes_legacy.c153
-rw-r--r--libs/nanopb/tests/backwards_compatibility/alltypes_legacy.h274
-rw-r--r--libs/nanopb/tests/backwards_compatibility/alltypes_legacy.options3
-rw-r--r--libs/nanopb/tests/backwards_compatibility/alltypes_legacy.proto110
-rw-r--r--libs/nanopb/tests/backwards_compatibility/decode_legacy.c199
-rw-r--r--libs/nanopb/tests/backwards_compatibility/encode_legacy.c135
-rw-r--r--libs/nanopb/tests/basic_buffer/SConscript12
-rw-r--r--libs/nanopb/tests/basic_buffer/decode_buffer.c88
-rw-r--r--libs/nanopb/tests/basic_buffer/encode_buffer.c38
-rw-r--r--libs/nanopb/tests/basic_stream/SConscript12
-rw-r--r--libs/nanopb/tests/basic_stream/decode_stream.c84
-rw-r--r--libs/nanopb/tests/basic_stream/encode_stream.c40
-rw-r--r--libs/nanopb/tests/buffer_only/SConscript28
-rw-r--r--libs/nanopb/tests/callbacks/SConscript14
-rw-r--r--libs/nanopb/tests/callbacks/callbacks.proto18
-rw-r--r--libs/nanopb/tests/callbacks/decode_callbacks.c97
-rw-r--r--libs/nanopb/tests/callbacks/encode_callbacks.c92
-rw-r--r--libs/nanopb/tests/common/SConscript48
-rw-r--r--libs/nanopb/tests/common/malloc_wrappers.c54
-rw-r--r--libs/nanopb/tests/common/malloc_wrappers.h7
-rw-r--r--libs/nanopb/tests/common/malloc_wrappers_syshdr.h15
-rw-r--r--libs/nanopb/tests/common/person.proto22
-rw-r--r--libs/nanopb/tests/common/test_helpers.h17
-rw-r--r--libs/nanopb/tests/common/unittestproto.proto43
-rw-r--r--libs/nanopb/tests/common/unittests.h14
-rw-r--r--libs/nanopb/tests/cxx_main_program/SConscript25
-rw-r--r--libs/nanopb/tests/cyclic_messages/SConscript11
-rw-r--r--libs/nanopb/tests/cyclic_messages/cyclic.proto27
-rw-r--r--libs/nanopb/tests/cyclic_messages/cyclic_callback.options7
-rw-r--r--libs/nanopb/tests/cyclic_messages/encode_cyclic_callback.c148
-rw-r--r--libs/nanopb/tests/decode_unittests/SConscript4
-rw-r--r--libs/nanopb/tests/decode_unittests/decode_unittests.c388
-rw-r--r--libs/nanopb/tests/encode_unittests/SConscript5
-rw-r--r--libs/nanopb/tests/encode_unittests/encode_unittests.c355
-rw-r--r--libs/nanopb/tests/enum_sizes/SConscript12
-rw-r--r--libs/nanopb/tests/enum_sizes/enumsizes.proto86
-rw-r--r--libs/nanopb/tests/enum_sizes/enumsizes_unittests.c72
-rw-r--r--libs/nanopb/tests/enum_to_string/SConscript7
-rw-r--r--libs/nanopb/tests/enum_to_string/enum.proto19
-rw-r--r--libs/nanopb/tests/enum_to_string/enum_to_string.c19
-rw-r--r--libs/nanopb/tests/extensions/SConscript16
-rw-r--r--libs/nanopb/tests/extensions/decode_extensions.c60
-rw-r--r--libs/nanopb/tests/extensions/encode_extensions.c54
-rw-r--r--libs/nanopb/tests/extensions/extensions.options1
-rw-r--r--libs/nanopb/tests/extensions/extensions.proto19
-rw-r--r--libs/nanopb/tests/extra_fields/SConscript16
-rw-r--r--libs/nanopb/tests/extra_fields/alltypes_with_extra_fields.pbbin0 -> 523 bytes
-rw-r--r--libs/nanopb/tests/extra_fields/person_with_extra_field.expected14
-rw-r--r--libs/nanopb/tests/extra_fields/person_with_extra_field.pbbin0 -> 124 bytes
-rw-r--r--libs/nanopb/tests/field_size_16/SConscript29
-rw-r--r--libs/nanopb/tests/field_size_16/alltypes.options4
-rw-r--r--libs/nanopb/tests/field_size_16/alltypes.proto123
-rw-r--r--libs/nanopb/tests/field_size_16_proto3/SConscript34
-rw-r--r--libs/nanopb/tests/field_size_16_proto3/alltypes.options4
-rw-r--r--libs/nanopb/tests/field_size_16_proto3/alltypes.proto100
-rw-r--r--libs/nanopb/tests/field_size_16_proto3/decode_alltypes.c167
-rw-r--r--libs/nanopb/tests/field_size_16_proto3/encode_alltypes.c111
-rw-r--r--libs/nanopb/tests/field_size_32/SConscript29
-rw-r--r--libs/nanopb/tests/field_size_32/alltypes.options3
-rw-r--r--libs/nanopb/tests/field_size_32/alltypes.proto123
-rw-r--r--libs/nanopb/tests/fuzztest/SConscript43
-rw-r--r--libs/nanopb/tests/fuzztest/alltypes_pointer.options3
-rw-r--r--libs/nanopb/tests/fuzztest/alltypes_static.options4
-rw-r--r--libs/nanopb/tests/fuzztest/fuzzstub.c189
-rw-r--r--libs/nanopb/tests/fuzztest/fuzztest.c432
-rw-r--r--libs/nanopb/tests/fuzztest/generate_message.c101
-rwxr-xr-xlibs/nanopb/tests/fuzztest/run_radamsa.sh12
-rw-r--r--libs/nanopb/tests/fuzztest/sample_data/sample1.pbbin0 -> 573 bytes
-rw-r--r--libs/nanopb/tests/fuzztest/sample_data/sample2.pbbin0 -> 466 bytes
-rw-r--r--libs/nanopb/tests/inline/SConscript16
-rw-r--r--libs/nanopb/tests/inline/inline.expected3
-rw-r--r--libs/nanopb/tests/inline/inline.proto17
-rw-r--r--libs/nanopb/tests/inline/inline_unittests.c73
-rw-r--r--libs/nanopb/tests/intsizes/SConscript12
-rw-r--r--libs/nanopb/tests/intsizes/intsizes.proto41
-rw-r--r--libs/nanopb/tests/intsizes/intsizes_unittests.c122
-rw-r--r--libs/nanopb/tests/io_errors/SConscript15
-rw-r--r--libs/nanopb/tests/io_errors/alltypes.options3
-rw-r--r--libs/nanopb/tests/io_errors/io_errors.c140
-rw-r--r--libs/nanopb/tests/io_errors_pointers/SConscript26
-rw-r--r--libs/nanopb/tests/io_errors_pointers/alltypes.options3
-rw-r--r--libs/nanopb/tests/mem_release/SConscript13
-rw-r--r--libs/nanopb/tests/mem_release/mem_release.c187
-rw-r--r--libs/nanopb/tests/mem_release/mem_release.proto35
-rw-r--r--libs/nanopb/tests/message_sizes/SConscript11
-rw-r--r--libs/nanopb/tests/message_sizes/dummy.c9
-rw-r--r--libs/nanopb/tests/message_sizes/messages1.proto29
-rw-r--r--libs/nanopb/tests/message_sizes/messages2.proto10
-rw-r--r--libs/nanopb/tests/missing_fields/SConscript8
-rw-r--r--libs/nanopb/tests/missing_fields/missing_fields.c53
-rw-r--r--libs/nanopb/tests/missing_fields/missing_fields.proto140
-rw-r--r--libs/nanopb/tests/multiple_files/SConscript16
-rw-r--r--libs/nanopb/tests/multiple_files/multifile1.options1
-rw-r--r--libs/nanopb/tests/multiple_files/multifile1.proto34
-rw-r--r--libs/nanopb/tests/multiple_files/multifile2.proto22
-rw-r--r--libs/nanopb/tests/multiple_files/subdir/multifile2.proto25
-rw-r--r--libs/nanopb/tests/multiple_files/test_multiple_files.c30
-rw-r--r--libs/nanopb/tests/no_errmsg/SConscript28
-rw-r--r--libs/nanopb/tests/no_messages/SConscript7
-rw-r--r--libs/nanopb/tests/no_messages/no_messages.proto9
-rw-r--r--libs/nanopb/tests/oneof/SConscript33
-rw-r--r--libs/nanopb/tests/oneof/decode_oneof.c131
-rw-r--r--libs/nanopb/tests/oneof/encode_oneof.c64
-rw-r--r--libs/nanopb/tests/oneof/oneof.proto32
-rw-r--r--libs/nanopb/tests/options/SConscript12
-rw-r--r--libs/nanopb/tests/options/options.expected20
-rw-r--r--libs/nanopb/tests/options/options.proto98
-rw-r--r--libs/nanopb/tests/options/proto3_options.expected4
-rw-r--r--libs/nanopb/tests/options/proto3_options.proto11
-rw-r--r--libs/nanopb/tests/package_name/SConscript38
-rw-r--r--libs/nanopb/tests/regression/issue_118/SConscript12
-rw-r--r--libs/nanopb/tests/regression/issue_118/enumdef.proto8
-rw-r--r--libs/nanopb/tests/regression/issue_118/enumuse.proto7
-rw-r--r--libs/nanopb/tests/regression/issue_125/SConscript9
-rw-r--r--libs/nanopb/tests/regression/issue_125/extensionbug.expected3
-rw-r--r--libs/nanopb/tests/regression/issue_125/extensionbug.options4
-rw-r--r--libs/nanopb/tests/regression/issue_125/extensionbug.proto18
-rw-r--r--libs/nanopb/tests/regression/issue_141/SConscript8
-rw-r--r--libs/nanopb/tests/regression/issue_141/testproto.expected7
-rw-r--r--libs/nanopb/tests/regression/issue_141/testproto.proto52
-rw-r--r--libs/nanopb/tests/regression/issue_145/SConscript9
-rw-r--r--libs/nanopb/tests/regression/issue_145/comments.expected3
-rw-r--r--libs/nanopb/tests/regression/issue_145/comments.options6
-rw-r--r--libs/nanopb/tests/regression/issue_145/comments.proto7
-rw-r--r--libs/nanopb/tests/regression/issue_166/SConscript13
-rw-r--r--libs/nanopb/tests/regression/issue_166/enum_encoded_size.c43
-rw-r--r--libs/nanopb/tests/regression/issue_166/enums.proto18
-rw-r--r--libs/nanopb/tests/regression/issue_172/SConscript16
-rw-r--r--libs/nanopb/tests/regression/issue_172/msg_size.c9
-rw-r--r--libs/nanopb/tests/regression/issue_172/submessage/submessage.options1
-rw-r--r--libs/nanopb/tests/regression/issue_172/submessage/submessage.proto4
-rw-r--r--libs/nanopb/tests/regression/issue_172/test.proto6
-rw-r--r--libs/nanopb/tests/regression/issue_188/SConscript6
-rw-r--r--libs/nanopb/tests/regression/issue_188/oneof.proto29
-rw-r--r--libs/nanopb/tests/regression/issue_195/SConscript10
-rw-r--r--libs/nanopb/tests/regression/issue_195/test.expected1
-rw-r--r--libs/nanopb/tests/regression/issue_195/test.proto8
-rw-r--r--libs/nanopb/tests/regression/issue_203/SConscript9
-rw-r--r--libs/nanopb/tests/regression/issue_203/file1.proto10
-rw-r--r--libs/nanopb/tests/regression/issue_203/file2.proto10
-rw-r--r--libs/nanopb/tests/regression/issue_205/SConscript14
-rw-r--r--libs/nanopb/tests/regression/issue_205/size_corruption.c12
-rw-r--r--libs/nanopb/tests/regression/issue_205/size_corruption.proto11
-rw-r--r--libs/nanopb/tests/regression/issue_227/SConscript14
-rw-r--r--libs/nanopb/tests/regression/issue_227/unaligned_uint64.c14
-rw-r--r--libs/nanopb/tests/regression/issue_227/unaligned_uint64.proto8
-rw-r--r--libs/nanopb/tests/regression/issue_229/SConscript13
-rw-r--r--libs/nanopb/tests/regression/issue_229/multiple_oneof.c35
-rw-r--r--libs/nanopb/tests/regression/issue_229/multiple_oneof.proto11
-rw-r--r--libs/nanopb/tests/regression/issue_242/SConscript13
-rw-r--r--libs/nanopb/tests/regression/issue_242/zero_value.c51
-rw-r--r--libs/nanopb/tests/regression/issue_242/zero_value.proto15
-rw-r--r--libs/nanopb/tests/regression/issue_247/SConscript14
-rw-r--r--libs/nanopb/tests/regression/issue_247/padding.c32
-rw-r--r--libs/nanopb/tests/regression/issue_247/padding.proto12
-rw-r--r--libs/nanopb/tests/regression/issue_249/SConscript12
-rw-r--r--libs/nanopb/tests/regression/issue_249/test.c59
-rw-r--r--libs/nanopb/tests/regression/issue_249/test.proto10
-rw-r--r--libs/nanopb/tests/regression/issue_253/SConscript15
-rw-r--r--libs/nanopb/tests/regression/issue_253/short_array.c24
-rw-r--r--libs/nanopb/tests/regression/issue_253/short_array.proto7
-rw-r--r--libs/nanopb/tests/regression/issue_256/SConscript16
-rw-r--r--libs/nanopb/tests/regression/issue_256/submsg_array.c38
-rw-r--r--libs/nanopb/tests/regression/issue_256/submsg_array.proto11
-rw-r--r--libs/nanopb/tests/site_scons/site_init.py109
-rw-r--r--libs/nanopb/tests/site_scons/site_tools/nanopb.py126
-rw-r--r--libs/nanopb/tests/special_characters/SConscript6
-rw-r--r--libs/nanopb/tests/special_characters/funny-proto+name has.characters.proto1
-rw-r--r--libs/nanopb/tests/splint/SConscript16
-rw-r--r--libs/nanopb/tests/splint/splint.rc37
198 files changed, 10549 insertions, 0 deletions
diff --git a/libs/nanopb/tests/Makefile b/libs/nanopb/tests/Makefile
new file mode 100644
index 00000000..cee6bf67
--- /dev/null
+++ b/libs/nanopb/tests/Makefile
@@ -0,0 +1,21 @@
+all:
+ scons
+
+clean:
+ scons -c
+
+coverage:
+ rm -rf build coverage
+
+ # LCOV does not like the newer gcov format
+ scons CC=gcc-4.6 CXX=gcc-4.6
+
+ # Collect the data
+ mkdir build/coverage
+ lcov --base-directory . --directory build/ --gcov-tool gcov-4.6 -c -o build/coverage/nanopb.info
+
+ # Remove the test code from results
+ lcov -r build/coverage/nanopb.info '*tests*' -o build/coverage/nanopb.info
+
+ # Generate HTML
+ genhtml -o build/coverage build/coverage/nanopb.info
diff --git a/libs/nanopb/tests/SConstruct b/libs/nanopb/tests/SConstruct
new file mode 100644
index 00000000..ae79f710
--- /dev/null
+++ b/libs/nanopb/tests/SConstruct
@@ -0,0 +1,157 @@
+Help('''
+Type 'scons' to build and run all the available test cases.
+It will automatically detect your platform and C compiler and
+build appropriately.
+
+You can modify the behavious using following options:
+CC Name of C compiler
+CXX Name of C++ compiler
+CCFLAGS Flags to pass to the C compiler
+CXXFLAGS Flags to pass to the C++ compiler
+
+For example, for a clang build, use:
+scons CC=clang CXX=clang++
+''')
+
+import os
+env = Environment(ENV = os.environ, tools = ['default', 'nanopb'])
+
+# Allow overriding the compiler with scons CC=???
+if 'CC' in ARGUMENTS: env.Replace(CC = ARGUMENTS['CC'])
+if 'CXX' in ARGUMENTS: env.Replace(CXX = ARGUMENTS['CXX'])
+if 'CCFLAGS' in ARGUMENTS: env.Append(CCFLAGS = ARGUMENTS['CCFLAGS'])
+if 'CXXFLAGS' in ARGUMENTS: env.Append(CXXFLAGS = ARGUMENTS['CXXFLAGS'])
+
+# Add the builders defined in site_init.py
+add_nanopb_builders(env)
+
+# Path to the files shared by tests, and to the nanopb core.
+env.Append(CPPPATH = ["#../", "$COMMON"])
+
+# Path for finding nanopb.proto
+env.Append(PROTOCPATH = '#../generator')
+
+# Check the compilation environment, unless we are just cleaning up.
+if not env.GetOption('clean'):
+ def check_ccflags(context, flags, linkflags = ''):
+ '''Check if given CCFLAGS are supported'''
+ context.Message('Checking support for CCFLAGS="%s"... ' % flags)
+ oldflags = context.env['CCFLAGS']
+ oldlinkflags = context.env['CCFLAGS']
+ context.env.Append(CCFLAGS = flags)
+ context.env.Append(LINKFLAGS = linkflags)
+ result = context.TryCompile("int main() {return 0;}", '.c')
+ context.env.Replace(CCFLAGS = oldflags)
+ context.env.Replace(LINKFLAGS = oldlinkflags)
+ context.Result(result)
+ return result
+
+ conf = Configure(env, custom_tests = {'CheckCCFLAGS': check_ccflags})
+
+ # 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')
+ stdlib = conf.CheckCHeader('stdlib.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 = "#../extra")
+ conf.env.Append(SYSHDR = '\\"pb_syshdr.h\\"')
+
+ 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})
+ if stdlib: conf.env.Append(CPPDEFINES = {'HAVE_STDLIB_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')
+
+ # Check protoc version
+ status, output = conf.TryAction('$PROTOC --version > $TARGET')
+ if status:
+ conf.env['PROTOC_VERSION'] = output
+
+ # Check if libmudflap is available (only with GCC)
+ if 'gcc' in env['CC']:
+ if conf.CheckLib('mudflap'):
+ conf.env.Append(CCFLAGS = '-fmudflap')
+ conf.env.Append(LINKFLAGS = '-fmudflap')
+
+ # Check if we can use extra strict warning flags (only with GCC)
+ extra = '-Wcast-qual -Wlogical-op -Wconversion'
+ extra += ' -fstrict-aliasing -Wstrict-aliasing=1'
+ extra += ' -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls'
+ extra += ' -Wstack-protector '
+ if 'gcc' in env['CC']:
+ if conf.CheckCCFLAGS(extra):
+ conf.env.Append(CORECFLAGS = extra)
+
+ # Check if we can use undefined behaviour sanitizer (only with clang)
+ # TODO: Fuzz test triggers the bool sanitizer, figure out whether to
+ # modify the fuzz test or to keep ignoring the check.
+ extra = '-fsanitize=undefined,integer -fno-sanitize-recover=undefined,integer -fsanitize-recover=bool '
+ if 'clang' in env['CC']:
+ if conf.CheckCCFLAGS(extra, linkflags = extra):
+ conf.env.Append(CORECFLAGS = extra)
+ conf.env.Append(LINKFLAGS = extra)
+
+ # End the config stuff
+ env = conf.Finish()
+
+# Initialize the CCFLAGS according to the compiler
+if 'gcc' in env['CC']:
+ # GNU Compiler Collection
+
+ # Debug info, warnings as errors
+ env.Append(CFLAGS = '-ansi -pedantic -g -Wall -Werror -fprofile-arcs -ftest-coverage ')
+ env.Append(CORECFLAGS = '-Wextra')
+ env.Append(LINKFLAGS = '-g --coverage')
+
+ # We currently need uint64_t anyway, even though ANSI C90 otherwise..
+ env.Append(CFLAGS = '-Wno-long-long')
+elif 'clang' in env['CC']:
+ # CLang
+ env.Append(CFLAGS = '-ansi -g -Wall -Werror')
+ env.Append(CORECFLAGS = ' -Wextra -Wcast-qual -Wconversion')
+elif 'cl' in env['CC']:
+ # Microsoft Visual C++
+
+ # Debug info on, warning level 2 for tests, warnings as errors
+ env.Append(CFLAGS = '/Zi /W2 /WX')
+ env.Append(LINKFLAGS = '/DEBUG')
+
+ # More strict checks on the nanopb core
+ env.Append(CORECFLAGS = '/W4')
+elif 'tcc' in env['CC']:
+ # Tiny C Compiler
+ env.Append(CFLAGS = '-Wall -Werror -g')
+
+env.SetDefault(CORECFLAGS = '')
+
+if 'clang' in env['CXX']:
+ env.Append(CXXFLAGS = '-g -Wall -Werror -Wextra -Wno-missing-field-initializers')
+elif 'g++' in env['CXX'] or 'gcc' in env['CXX']:
+ env.Append(CXXFLAGS = '-g -Wall -Werror -Wextra -Wno-missing-field-initializers')
+elif 'cl' in env['CXX']:
+ env.Append(CXXFLAGS = '/Zi /W2 /WX')
+
+# Now include the SConscript files from all subdirectories
+import os.path
+env['VARIANT_DIR'] = 'build'
+env['BUILD'] = '#' + env['VARIANT_DIR']
+env['COMMON'] = '#' + env['VARIANT_DIR'] + '/common'
+
+# Include common/SConscript first to make sure its exports are available
+# to other SConscripts.
+SConscript("common/SConscript", exports = 'env', variant_dir = env['VARIANT_DIR'] + '/common')
+
+for subdir in Glob('*/SConscript') + Glob('regression/*/SConscript'):
+ if str(subdir).startswith("common"): continue
+ SConscript(subdir, exports = 'env', variant_dir = env['VARIANT_DIR'] + '/' + os.path.dirname(str(subdir)))
+
diff --git a/libs/nanopb/tests/alltypes/SConscript b/libs/nanopb/tests/alltypes/SConscript
new file mode 100644
index 00000000..6c6238c6
--- /dev/null
+++ b/libs/nanopb/tests/alltypes/SConscript
@@ -0,0 +1,35 @@
+# Build and run a test that encodes and decodes a message that contains
+# all of the Protocol Buffers data types.
+
+Import("env")
+
+env.NanopbProto(["alltypes", "alltypes.options"])
+enc = env.Program(["encode_alltypes.c", "alltypes.pb.c", "$COMMON/pb_encode.o", "$COMMON/pb_common.o"])
+dec = env.Program(["decode_alltypes.c", "alltypes.pb.c", "$COMMON/pb_decode.o", "$COMMON/pb_common.o"])
+
+# Test the round-trip from nanopb encoder to nanopb decoder
+env.RunTest(enc)
+env.RunTest([dec, "encode_alltypes.output"])
+
+# Re-encode the data using protoc, and check that the results from nanopb
+# match byte-per-byte to the protoc output.
+env.Decode("encode_alltypes.output.decoded",
+ ["encode_alltypes.output", "alltypes.proto"],
+ MESSAGE='AllTypes')
+env.Encode("encode_alltypes.output.recoded",
+ ["encode_alltypes.output.decoded", "alltypes.proto"],
+ MESSAGE='AllTypes')
+env.Compare(["encode_alltypes.output", "encode_alltypes.output.recoded"])
+
+# Do the same checks with the optional fields present.
+env.RunTest("optionals.output", enc, ARGS = ['1'])
+env.RunTest("optionals.decout", [dec, "optionals.output"], ARGS = ['1'])
+env.Decode("optionals.output.decoded",
+ ["optionals.output", "alltypes.proto"],
+ MESSAGE='AllTypes')
+env.Encode("optionals.output.recoded",
+ ["optionals.output.decoded", "alltypes.proto"],
+ MESSAGE='AllTypes')
+env.Compare(["optionals.output", "optionals.output.recoded"])
+
+
diff --git a/libs/nanopb/tests/alltypes/alltypes.options b/libs/nanopb/tests/alltypes/alltypes.options
new file mode 100644
index 00000000..0d5ab12b
--- /dev/null
+++ b/libs/nanopb/tests/alltypes/alltypes.options
@@ -0,0 +1,3 @@
+* max_size:16
+* max_count:5
+*.*fbytes fixed_length:true max_size:4
diff --git a/libs/nanopb/tests/alltypes/alltypes.proto b/libs/nanopb/tests/alltypes/alltypes.proto
new file mode 100644
index 00000000..b2250c00
--- /dev/null
+++ b/libs/nanopb/tests/alltypes/alltypes.proto
@@ -0,0 +1,125 @@
+syntax = "proto2";
+// package name placeholder
+
+message SubMessage {
+ required string substuff1 = 1 [default = "1"];
+ required int32 substuff2 = 2 [default = 2];
+ optional fixed32 substuff3 = 3 [default = 3];
+}
+
+message EmptyMessage {
+
+}
+
+enum HugeEnum {
+ Negative = -2147483647; /* protoc doesn't accept -2147483648 here */
+ Positive = 2147483647;
+}
+
+message Limits {
+ required int32 int32_min = 1 [default = 2147483647];
+ required int32 int32_max = 2 [default = -2147483647];
+ required uint32 uint32_min = 3 [default = 4294967295];
+ required uint32 uint32_max = 4 [default = 0];
+ required int64 int64_min = 5 [default = 9223372036854775807];
+ required int64 int64_max = 6 [default = -9223372036854775807];
+ required uint64 uint64_min = 7 [default = 18446744073709551615];
+ required uint64 uint64_max = 8 [default = 0];
+ required HugeEnum enum_min = 9 [default = Positive];
+ required HugeEnum enum_max = 10 [default = Negative];
+}
+
+enum MyEnum {
+ Zero = 0;
+ 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;
+ required bytes req_bytes = 15;
+ required SubMessage req_submsg = 16;
+ required MyEnum req_enum = 17;
+ required EmptyMessage req_emptymsg = 18;
+ required bytes req_fbytes = 19;
+
+ repeated int32 rep_int32 = 21 [packed = true];
+ repeated int64 rep_int64 = 22 [packed = true];
+ repeated uint32 rep_uint32 = 23 [packed = true];
+ repeated uint64 rep_uint64 = 24 [packed = true];
+ repeated sint32 rep_sint32 = 25 [packed = true];
+ repeated sint64 rep_sint64 = 26 [packed = true];
+ repeated bool rep_bool = 27 [packed = true];
+
+ repeated fixed32 rep_fixed32 = 28 [packed = true];
+ repeated sfixed32 rep_sfixed32= 29 [packed = true];
+ repeated float rep_float = 30 [packed = true];
+
+ repeated fixed64 rep_fixed64 = 31 [packed = true];
+ repeated sfixed64 rep_sfixed64= 32 [packed = true];
+ repeated double rep_double = 33 [packed = true];
+
+ repeated string rep_string = 34;
+ repeated bytes rep_bytes = 35;
+ repeated SubMessage rep_submsg = 36;
+ repeated MyEnum rep_enum = 37 [packed = true];
+ repeated EmptyMessage rep_emptymsg = 38;
+ repeated bytes rep_fbytes = 39;
+
+ optional int32 opt_int32 = 41 [default = 4041];
+ optional int64 opt_int64 = 42 [default = 4042];
+ optional uint32 opt_uint32 = 43 [default = 4043];
+ optional uint64 opt_uint64 = 44 [default = 4044];
+ optional sint32 opt_sint32 = 45 [default = 4045];
+ optional sint64 opt_sint64 = 46 [default = 4046];
+ optional bool opt_bool = 47 [default = false];
+
+ optional fixed32 opt_fixed32 = 48 [default = 4048];
+ optional sfixed32 opt_sfixed32= 49 [default = 4049];
+ optional float opt_float = 50 [default = 4050];
+
+ optional fixed64 opt_fixed64 = 51 [default = 4051];
+ optional sfixed64 opt_sfixed64= 52 [default = 4052];
+ optional double opt_double = 53 [default = 4053];
+
+ optional string opt_string = 54 [default = "4054"];
+ optional bytes opt_bytes = 55 [default = "4055"];
+ optional SubMessage opt_submsg = 56;
+ optional MyEnum opt_enum = 57 [default = Second];
+ optional EmptyMessage opt_emptymsg = 58;
+ optional bytes opt_fbytes = 59 [default = "4059"];
+
+ oneof oneof
+ {
+ SubMessage oneof_msg1 = 60;
+ EmptyMessage oneof_msg2 = 61;
+ }
+
+ // Check that extreme integer values are handled correctly
+ required Limits req_limits = 98;
+
+ // 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;
+
+
+ extensions 200 to 255;
+}
+
diff --git a/libs/nanopb/tests/alltypes/decode_alltypes.c b/libs/nanopb/tests/alltypes/decode_alltypes.c
new file mode 100644
index 00000000..2e609e56
--- /dev/null
+++ b/libs/nanopb/tests/alltypes/decode_alltypes.c
@@ -0,0 +1,229 @@
+/* Tests the decoding of all types.
+ * This is the counterpart of test_encode3.
+ * Run e.g. ./test_encode3 | ./test_decode3
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <pb_decode.h>
+#include "alltypes.pb.h"
+#include "test_helpers.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, int mode)
+{
+ /* Uses _init_default to just make sure that it works. */
+ AllTypes alltypes = AllTypes_init_default;
+
+ /* Fill with garbage to better detect initialization errors */
+ memset(&alltypes, 0xAA, sizeof(alltypes));
+ alltypes.extensions = 0;
+
+ 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_submsg.substuff3 == 3);
+ TEST(alltypes.req_enum == MyEnum_Truth);
+ TEST(memcmp(alltypes.req_fbytes, "1019", 4) == 0);
+
+ TEST(alltypes.rep_int32_count == 5 && alltypes.rep_int32[4] == -2001 && alltypes.rep_int32[0] == 0);
+ TEST(alltypes.rep_int64_count == 5 && alltypes.rep_int64[4] == -2002 && alltypes.rep_int64[0] == 0);
+ TEST(alltypes.rep_uint32_count == 5 && alltypes.rep_uint32[4] == 2003 && alltypes.rep_uint32[0] == 0);
+ TEST(alltypes.rep_uint64_count == 5 && alltypes.rep_uint64[4] == 2004 && alltypes.rep_uint64[0] == 0);
+ TEST(alltypes.rep_sint32_count == 5 && alltypes.rep_sint32[4] == -2005 && alltypes.rep_sint32[0] == 0);
+ TEST(alltypes.rep_sint64_count == 5 && alltypes.rep_sint64[4] == -2006 && alltypes.rep_sint64[0] == 0);
+ TEST(alltypes.rep_bool_count == 5 && alltypes.rep_bool[4] == true && alltypes.rep_bool[0] == false);
+
+ TEST(alltypes.rep_fixed32_count == 5 && alltypes.rep_fixed32[4] == 2008 && alltypes.rep_fixed32[0] == 0);
+ TEST(alltypes.rep_sfixed32_count == 5 && alltypes.rep_sfixed32[4] == -2009 && alltypes.rep_sfixed32[0] == 0);
+ TEST(alltypes.rep_float_count == 5 && alltypes.rep_float[4] == 2010.0f && alltypes.rep_float[0] == 0.0f);
+
+ TEST(alltypes.rep_fixed64_count == 5 && alltypes.rep_fixed64[4] == 2011 && alltypes.rep_fixed64[0] == 0);
+ TEST(alltypes.rep_sfixed64_count == 5 && alltypes.rep_sfixed64[4] == -2012 && alltypes.rep_sfixed64[0] == 0);
+ TEST(alltypes.rep_double_count == 5 && alltypes.rep_double[4] == 2013.0 && alltypes.rep_double[0] == 0.0);
+
+ TEST(alltypes.rep_string_count == 5 && strcmp(alltypes.rep_string[4], "2014") == 0 && alltypes.rep_string[0][0] == '\0');
+ TEST(alltypes.rep_bytes_count == 5 && alltypes.rep_bytes[4].size == 4 && alltypes.rep_bytes[0].size == 0);
+ TEST(memcmp(alltypes.rep_bytes[4].bytes, "2015", 4) == 0);
+
+ TEST(alltypes.rep_submsg_count == 5);
+ TEST(strcmp(alltypes.rep_submsg[4].substuff1, "2016") == 0 && alltypes.rep_submsg[0].substuff1[0] == '\0');
+ TEST(alltypes.rep_submsg[4].substuff2 == 2016 && alltypes.rep_submsg[0].substuff2 == 0);
+ TEST(alltypes.rep_submsg[4].substuff3 == 2016 && alltypes.rep_submsg[0].substuff3 == 3);
+
+ TEST(alltypes.rep_enum_count == 5 && alltypes.rep_enum[4] == MyEnum_Truth && alltypes.rep_enum[0] == MyEnum_Zero);
+ TEST(alltypes.rep_emptymsg_count == 5);
+ TEST(alltypes.rep_fbytes_count == 5);
+ TEST(alltypes.rep_fbytes[0][0] == 0 && alltypes.rep_fbytes[0][3] == 0);
+ TEST(memcmp(alltypes.rep_fbytes[4], "2019", 4) == 0);
+
+ if (mode == 0)
+ {
+ /* Expect default values */
+ TEST(alltypes.has_opt_int32 == false);
+ TEST(alltypes.opt_int32 == 4041);
+ TEST(alltypes.has_opt_int64 == false);
+ TEST(alltypes.opt_int64 == 4042);
+ TEST(alltypes.has_opt_uint32 == false);
+ TEST(alltypes.opt_uint32 == 4043);
+ TEST(alltypes.has_opt_uint64 == false);
+ TEST(alltypes.opt_uint64 == 4044);
+ TEST(alltypes.has_opt_sint32 == false);
+ TEST(alltypes.opt_sint32 == 4045);
+ TEST(alltypes.has_opt_sint64 == false);
+ TEST(alltypes.opt_sint64 == 4046);
+ TEST(alltypes.has_opt_bool == false);
+ TEST(alltypes.opt_bool == false);
+
+ TEST(alltypes.has_opt_fixed32 == false);
+ TEST(alltypes.opt_fixed32 == 4048);
+ TEST(alltypes.has_opt_sfixed32 == false);
+ TEST(alltypes.opt_sfixed32 == 4049);
+ TEST(alltypes.has_opt_float == false);
+ TEST(alltypes.opt_float == 4050.0f);
+
+ TEST(alltypes.has_opt_fixed64 == false);
+ TEST(alltypes.opt_fixed64 == 4051);
+ TEST(alltypes.has_opt_sfixed64 == false);
+ TEST(alltypes.opt_sfixed64 == 4052);
+ TEST(alltypes.has_opt_double == false);
+ TEST(alltypes.opt_double == 4053.0);
+
+ TEST(alltypes.has_opt_string == false);
+ TEST(strcmp(alltypes.opt_string, "4054") == 0);
+ TEST(alltypes.has_opt_bytes == false);
+ TEST(alltypes.opt_bytes.size == 4);
+ TEST(memcmp(alltypes.opt_bytes.bytes, "4055", 4) == 0);
+ TEST(alltypes.has_opt_submsg == false);
+ TEST(strcmp(alltypes.opt_submsg.substuff1, "1") == 0);
+ TEST(alltypes.opt_submsg.substuff2 == 2);
+ TEST(alltypes.opt_submsg.substuff3 == 3);
+ TEST(alltypes.has_opt_enum == false);
+ TEST(alltypes.opt_enum == MyEnum_Second);
+ TEST(alltypes.has_opt_emptymsg == false);
+ TEST(alltypes.has_opt_fbytes == false);
+ TEST(memcmp(alltypes.opt_fbytes, "4059", 4) == 0);
+
+ TEST(alltypes.which_oneof == 0);
+ }
+ else
+ {
+ /* Expect filled-in values */
+ TEST(alltypes.has_opt_int32 == true);
+ TEST(alltypes.opt_int32 == 3041);
+ TEST(alltypes.has_opt_int64 == true);
+ TEST(alltypes.opt_int64 == 3042);
+ TEST(alltypes.has_opt_uint32 == true);
+ TEST(alltypes.opt_uint32 == 3043);
+ TEST(alltypes.has_opt_uint64 == true);
+ TEST(alltypes.opt_uint64 == 3044);
+ TEST(alltypes.has_opt_sint32 == true);
+ TEST(alltypes.opt_sint32 == 3045);
+ TEST(alltypes.has_opt_sint64 == true);
+ TEST(alltypes.opt_sint64 == 3046);
+ TEST(alltypes.has_opt_bool == true);
+ TEST(alltypes.opt_bool == true);
+
+ TEST(alltypes.has_opt_fixed32 == true);
+ TEST(alltypes.opt_fixed32 == 3048);
+ TEST(alltypes.has_opt_sfixed32 == true);
+ TEST(alltypes.opt_sfixed32 == 3049);
+ TEST(alltypes.has_opt_float == true);
+ TEST(alltypes.opt_float == 3050.0f);
+
+ TEST(alltypes.has_opt_fixed64 == true);
+ TEST(alltypes.opt_fixed64 == 3051);
+ TEST(alltypes.has_opt_sfixed64 == true);
+ TEST(alltypes.opt_sfixed64 == 3052);
+ TEST(alltypes.has_opt_double == true);
+ TEST(alltypes.opt_double == 3053.0);
+
+ TEST(alltypes.has_opt_string == true);
+ TEST(strcmp(alltypes.opt_string, "3054") == 0);
+ TEST(alltypes.has_opt_bytes == true);
+ TEST(alltypes.opt_bytes.size == 4);
+ TEST(memcmp(alltypes.opt_bytes.bytes, "3055", 4) == 0);
+ TEST(alltypes.has_opt_submsg == true);
+ TEST(strcmp(alltypes.opt_submsg.substuff1, "3056") == 0);
+ TEST(alltypes.opt_submsg.substuff2 == 3056);
+ TEST(alltypes.opt_submsg.substuff3 == 3);
+ TEST(alltypes.has_opt_enum == true);
+ TEST(alltypes.opt_enum == MyEnum_Truth);
+ TEST(alltypes.has_opt_emptymsg == true);
+ TEST(alltypes.has_opt_fbytes == true);
+ TEST(memcmp(alltypes.opt_fbytes, "3059", 4) == 0);
+
+ TEST(alltypes.which_oneof == AllTypes_oneof_msg1_tag);
+ TEST(strcmp(alltypes.oneof.oneof_msg1.substuff1, "4059") == 0);
+ TEST(alltypes.oneof.oneof_msg1.substuff2 == 4059);
+ }
+
+ TEST(alltypes.req_limits.int32_min == INT32_MIN);
+ TEST(alltypes.req_limits.int32_max == INT32_MAX);
+ TEST(alltypes.req_limits.uint32_min == 0);
+ TEST(alltypes.req_limits.uint32_max == UINT32_MAX);
+ TEST(alltypes.req_limits.int64_min == INT64_MIN);
+ TEST(alltypes.req_limits.int64_max == INT64_MAX);
+ TEST(alltypes.req_limits.uint64_min == 0);
+ TEST(alltypes.req_limits.uint64_max == UINT64_MAX);
+ TEST(alltypes.req_limits.enum_min == HugeEnum_Negative);
+ TEST(alltypes.req_limits.enum_max == HugeEnum_Positive);
+
+ TEST(alltypes.end == 1099);
+
+ return true;
+}
+
+int main(int argc, char **argv)
+{
+ uint8_t buffer[1024];
+ size_t count;
+ pb_istream_t stream;
+
+ /* Whether to expect the optional values or the default values. */
+ int mode = (argc > 1) ? atoi(argv[1]) : 0;
+
+ /* Read the data into buffer */
+ SET_BINARY_MODE(stdin);
+ count = fread(buffer, 1, sizeof(buffer), stdin);
+
+ /* Construct a pb_istream_t for reading from the buffer */
+ stream = pb_istream_from_buffer(buffer, count);
+
+ /* Decode and print out the stuff */
+ if (!check_alltypes(&stream, mode))
+ {
+ printf("Parsing failed: %s\n", PB_GET_ERROR(&stream));
+ return 1;
+ } else {
+ return 0;
+ }
+}
diff --git a/libs/nanopb/tests/alltypes/encode_alltypes.c b/libs/nanopb/tests/alltypes/encode_alltypes.c
new file mode 100644
index 00000000..1b863555
--- /dev/null
+++ b/libs/nanopb/tests/alltypes/encode_alltypes.c
@@ -0,0 +1,155 @@
+/* Attempts to test all the datatypes supported by ProtoBuf.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pb_encode.h>
+#include "alltypes.pb.h"
+#include "test_helpers.h"
+
+int main(int argc, char **argv)
+{
+ int mode = (argc > 1) ? atoi(argv[1]) : 0;
+
+ /* Initialize the structure with constants */
+ AllTypes alltypes = AllTypes_init_zero;
+
+ 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;
+ memcpy(alltypes.req_fbytes, "1019", 4);
+
+ 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;
+ alltypes.rep_emptymsg_count = 5;
+
+ alltypes.rep_fbytes_count = 5;
+ memcpy(alltypes.rep_fbytes[4], "2019", 4);
+
+ alltypes.req_limits.int32_min = INT32_MIN;
+ alltypes.req_limits.int32_max = INT32_MAX;
+ alltypes.req_limits.uint32_min = 0;
+ alltypes.req_limits.uint32_max = UINT32_MAX;
+ alltypes.req_limits.int64_min = INT64_MIN;
+ alltypes.req_limits.int64_max = INT64_MAX;
+ alltypes.req_limits.uint64_min = 0;
+ alltypes.req_limits.uint64_max = UINT64_MAX;
+ alltypes.req_limits.enum_min = HugeEnum_Negative;
+ alltypes.req_limits.enum_max = HugeEnum_Positive;
+
+ 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.has_opt_emptymsg = true;
+ alltypes.has_opt_fbytes = true;
+ memcpy(alltypes.opt_fbytes, "3059", 4);
+
+ alltypes.which_oneof = AllTypes_oneof_msg1_tag;
+ strcpy(alltypes.oneof.oneof_msg1.substuff1, "4059");
+ alltypes.oneof.oneof_msg1.substuff2 = 4059;
+ }
+
+ alltypes.end = 1099;
+
+ {
+ uint8_t buffer[AllTypes_size];
+ 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: %s\n", PB_GET_ERROR(&stream));
+ return 1; /* Failure */
+ }
+ }
+}
diff --git a/libs/nanopb/tests/alltypes_callback/SConscript b/libs/nanopb/tests/alltypes_callback/SConscript
new file mode 100644
index 00000000..8be53908
--- /dev/null
+++ b/libs/nanopb/tests/alltypes_callback/SConscript
@@ -0,0 +1,28 @@
+# Test the AllTypes encoding & decoding using callbacks for all fields.
+
+Import("env", "malloc_env")
+
+c = Copy("$TARGET", "$SOURCE")
+env.Command("alltypes.proto", "#alltypes/alltypes.proto", c)
+
+env.NanopbProto(["alltypes", "alltypes.options"])
+enc = env.Program(["encode_alltypes_callback.c", "alltypes.pb.c", "$COMMON/pb_encode.o", "$COMMON/pb_common.o"])
+dec = env.Program(["decode_alltypes_callback.c", "alltypes.pb.c", "$COMMON/pb_decode.o", "$COMMON/pb_common.o"])
+
+refdec = "$BUILD/alltypes/decode_alltypes$PROGSUFFIX"
+
+# Encode and compare results
+env.RunTest(enc)
+env.RunTest("decode_alltypes.output", [refdec, "encode_alltypes_callback.output"])
+env.RunTest("decode_alltypes_callback.output", [dec, "encode_alltypes_callback.output"])
+
+# Do the same thing with the optional fields present
+env.RunTest("optionals.output", enc, ARGS = ['1'])
+env.RunTest("optionals.refdecout", [refdec, "optionals.output"], ARGS = ['1'])
+env.RunTest("optionals.decout", [dec, "optionals.output"], ARGS = ['1'])
+
+# Try with malloc support also
+mallocbin1 = malloc_env.Object("decode_with_malloc.o", "decode_alltypes_callback.c")
+mallocbin2 = malloc_env.Object("alltypes_malloc.pb.o", "alltypes.pb.c")
+mallocdec = malloc_env.Program("decode_with_malloc", [mallocbin1, mallocbin2, "$COMMON/pb_decode_with_malloc.o", "$COMMON/pb_common_with_malloc.o", "$COMMON/malloc_wrappers.o"])
+env.RunTest("decode_with_malloc.output", [mallocdec, "encode_alltypes_callback.output"])
diff --git a/libs/nanopb/tests/alltypes_callback/alltypes.options b/libs/nanopb/tests/alltypes_callback/alltypes.options
new file mode 100644
index 00000000..74d7a9c0
--- /dev/null
+++ b/libs/nanopb/tests/alltypes_callback/alltypes.options
@@ -0,0 +1,8 @@
+# Generate all fields as callbacks.
+AllTypes.* type:FT_CALLBACK
+SubMessage.substuff1 max_size:16
+AllTypes.oneof no_unions:true
+
+# With FT_CALLBACK, these options should get ignored
+*.*fbytes fixed_length:true max_size:4
+
diff --git a/libs/nanopb/tests/alltypes_callback/decode_alltypes_callback.c b/libs/nanopb/tests/alltypes_callback/decode_alltypes_callback.c
new file mode 100644
index 00000000..576ce307
--- /dev/null
+++ b/libs/nanopb/tests/alltypes_callback/decode_alltypes_callback.c
@@ -0,0 +1,442 @@
+/* Attempts to test all the datatypes supported by ProtoBuf when used as callback fields.
+ * Note that normally there would be no reason to use callback fields for this,
+ * because each encoder defined here only gives a single field.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <pb_decode.h>
+#include "alltypes.pb.h"
+#include "test_helpers.h"
+
+#define TEST(x) if (!(x)) { \
+ printf("Test " #x " failed (in field %d).\n", field->tag); \
+ return false; \
+ }
+
+static bool read_varint(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ uint64_t value;
+ if (!pb_decode_varint(stream, &value))
+ return false;
+
+ TEST((int64_t)value == (long)*arg);
+ return true;
+}
+
+static bool read_svarint(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ int64_t value;
+ if (!pb_decode_svarint(stream, &value))
+ return false;
+
+ TEST(value == (long)*arg);
+ return true;
+}
+
+static bool read_fixed32(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ uint32_t value;
+ if (!pb_decode_fixed32(stream, &value))
+ return false;
+
+ TEST(value == *(uint32_t*)*arg);
+ return true;
+}
+
+static bool read_fixed64(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ uint64_t value;
+ if (!pb_decode_fixed64(stream, &value))
+ return false;
+
+ TEST(value == *(uint64_t*)*arg);
+ return true;
+}
+
+static bool read_string(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ uint8_t buf[16] = {0};
+ size_t len = stream->bytes_left;
+
+ if (len > sizeof(buf) - 1 || !pb_read(stream, buf, len))
+ return false;
+
+ TEST(strcmp((char*)buf, *arg) == 0);
+ return true;
+}
+
+static bool read_submsg(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ SubMessage submsg = {""};
+ SubMessage *ref = *arg;
+
+ if (!pb_decode(stream, SubMessage_fields, &submsg))
+ return false;
+
+ TEST(strcmp(submsg.substuff1, ref->substuff1) == 0);
+ TEST(submsg.substuff2 == ref->substuff2);
+ TEST(submsg.has_substuff3 == ref->has_substuff3);
+ TEST(submsg.substuff3 == ref->substuff3);
+ return true;
+}
+
+static bool read_emptymsg(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ EmptyMessage emptymsg = {0};
+ return pb_decode(stream, EmptyMessage_fields, &emptymsg);
+}
+
+static bool read_repeated_varint(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ int32_t** expected = (int32_t**)arg;
+ uint64_t value;
+ if (!pb_decode_varint(stream, &value))
+ return false;
+
+ TEST(*(*expected)++ == value);
+ return true;
+}
+
+static bool read_repeated_svarint(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ int32_t** expected = (int32_t**)arg;
+ int64_t value;
+ if (!pb_decode_svarint(stream, &value))
+ return false;
+
+ TEST(*(*expected)++ == value);
+ return true;
+}
+
+static bool read_repeated_fixed32(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ uint32_t** expected = (uint32_t**)arg;
+ uint32_t value;
+ if (!pb_decode_fixed32(stream, &value))
+ return false;
+
+ TEST(*(*expected)++ == value);
+ return true;
+}
+
+static bool read_repeated_fixed64(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ uint64_t** expected = (uint64_t**)arg;
+ uint64_t value;
+ if (!pb_decode_fixed64(stream, &value))
+ return false;
+
+ TEST(*(*expected)++ == value);
+ return true;
+}
+
+static bool read_repeated_string(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ uint8_t*** expected = (uint8_t***)arg;
+ uint8_t buf[16] = {0};
+ size_t len = stream->bytes_left;
+
+ if (len > sizeof(buf) - 1 || !pb_read(stream, buf, len))
+ return false;
+
+ TEST(strcmp((char*)*(*expected)++, (char*)buf) == 0);
+ return true;
+}
+
+static bool read_repeated_submsg(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ SubMessage** expected = (SubMessage**)arg;
+ SubMessage submsg = {""};
+ if (!pb_decode(stream, SubMessage_fields, &submsg))
+ return false;
+
+ TEST(strcmp(submsg.substuff1, (*expected)->substuff1) == 0);
+ TEST(submsg.substuff2 == (*expected)->substuff2);
+ TEST(submsg.has_substuff3 == (*expected)->has_substuff3);
+ TEST(submsg.substuff3 == (*expected)->substuff3);
+ (*expected)++;
+
+ return true;
+}
+
+static bool read_limits(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ Limits decoded = {0};
+ if (!pb_decode(stream, Limits_fields, &decoded))
+ return false;
+
+ TEST(decoded.int32_min == INT32_MIN);
+ TEST(decoded.int32_max == INT32_MAX);
+ TEST(decoded.uint32_min == 0);
+ TEST(decoded.uint32_max == UINT32_MAX);
+ TEST(decoded.int64_min == INT64_MIN);
+ TEST(decoded.int64_max == INT64_MAX);
+ TEST(decoded.uint64_min == 0);
+ TEST(decoded.uint64_max == UINT64_MAX);
+ TEST(decoded.enum_min == HugeEnum_Negative);
+ TEST(decoded.enum_max == HugeEnum_Positive);
+
+ return true;
+}
+
+/* This function is called once from main(), it handles
+ the decoding and checks the fields. */
+bool check_alltypes(pb_istream_t *stream, int mode)
+{
+ /* Values for use from callbacks through pointers. */
+ bool status;
+ uint32_t req_fixed32 = 1008;
+ int32_t req_sfixed32 = -1009;
+ float req_float = 1010.0f;
+ uint64_t req_fixed64 = 1011;
+ int64_t req_sfixed64 = -1012;
+ double req_double = 1013.0;
+ SubMessage req_submsg = {"1016", 1016, false, 3};
+
+ int32_t rep_int32[5] = {0, 0, 0, 0, -2001};
+ int32_t rep_int64[5] = {0, 0, 0, 0, -2002};
+ int32_t rep_uint32[5] = {0, 0, 0, 0, 2003};
+ int32_t rep_uint64[5] = {0, 0, 0, 0, 2004};
+ int32_t rep_sint32[5] = {0, 0, 0, 0, -2005};
+ int32_t rep_sint64[5] = {0, 0, 0, 0, -2006};
+ int32_t rep_bool[5] = {false, false, false, false, true};
+ uint32_t rep_fixed32[5] = {0, 0, 0, 0, 2008};
+ int32_t rep_sfixed32[5] = {0, 0, 0, 0, -2009};
+ float rep_float[5] = {0, 0, 0, 0, 2010.0f};
+ uint64_t rep_fixed64[5] = {0, 0, 0, 0, 2011};
+ int64_t rep_sfixed64[5] = {0, 0, 0, 0, -2012};
+ double rep_double[5] = {0, 0, 0, 0, 2013.0};
+ char* rep_string[5] = {"", "", "", "", "2014"};
+ char* rep_bytes[5] = {"", "", "", "", "2015"};
+ SubMessage rep_submsg[5] = {{"", 0, 0, 3},
+ {"", 0, 0, 3},
+ {"", 0, 0, 3},
+ {"", 0, 0, 3},
+ {"2016", 2016, true, 2016}};
+ int32_t rep_enum[5] = {0, 0, 0, 0, MyEnum_Truth};
+
+ uint32_t opt_fixed32 = 3048;
+ int32_t opt_sfixed32 = 3049;
+ float opt_float = 3050.0f;
+ uint64_t opt_fixed64 = 3051;
+ int64_t opt_sfixed64 = 3052;
+ double opt_double = 3053.0f;
+ SubMessage opt_submsg = {"3056", 3056, false, 3};
+
+ SubMessage oneof_msg1 = {"4059", 4059, false, 3};
+
+ /* Bind callbacks for required fields */
+ AllTypes alltypes = AllTypes_init_zero;
+
+ alltypes.req_int32.funcs.decode = &read_varint;
+ alltypes.req_int32.arg = (void*)-1001;
+
+ alltypes.req_int64.funcs.decode = &read_varint;
+ alltypes.req_int64.arg = (void*)-1002;
+
+ alltypes.req_uint32.funcs.decode = &read_varint;
+ alltypes.req_uint32.arg = (void*)1003;
+
+ alltypes.req_uint32.funcs.decode = &read_varint;
+ alltypes.req_uint32.arg = (void*)1003;
+
+ alltypes.req_uint64.funcs.decode = &read_varint;
+ alltypes.req_uint64.arg = (void*)1004;
+
+ alltypes.req_sint32.funcs.decode = &read_svarint;
+ alltypes.req_sint32.arg = (void*)-1005;
+
+ alltypes.req_sint64.funcs.decode = &read_svarint;
+ alltypes.req_sint64.arg = (void*)-1006;
+
+ alltypes.req_bool.funcs.decode = &read_varint;
+ alltypes.req_bool.arg = (void*)true;
+
+ alltypes.req_fixed32.funcs.decode = &read_fixed32;
+ alltypes.req_fixed32.arg = &req_fixed32;
+
+ alltypes.req_sfixed32.funcs.decode = &read_fixed32;
+ alltypes.req_sfixed32.arg = &req_sfixed32;
+
+ alltypes.req_float.funcs.decode = &read_fixed32;
+ alltypes.req_float.arg = &req_float;
+
+ alltypes.req_fixed64.funcs.decode = &read_fixed64;
+ alltypes.req_fixed64.arg = &req_fixed64;
+
+ alltypes.req_sfixed64.funcs.decode = &read_fixed64;
+ alltypes.req_sfixed64.arg = &req_sfixed64;
+
+ alltypes.req_double.funcs.decode = &read_fixed64;
+ alltypes.req_double.arg = &req_double;
+
+ alltypes.req_string.funcs.decode = &read_string;
+ alltypes.req_string.arg = "1014";
+
+ alltypes.req_bytes.funcs.decode = &read_string;
+ alltypes.req_bytes.arg = "1015";
+
+ alltypes.req_submsg.funcs.decode = &read_submsg;
+ alltypes.req_submsg.arg = &req_submsg;
+
+ alltypes.req_enum.funcs.decode = &read_varint;
+ alltypes.req_enum.arg = (void*)MyEnum_Truth;
+
+ alltypes.req_emptymsg.funcs.decode = &read_emptymsg;
+
+ /* Bind callbacks for repeated fields */
+ alltypes.rep_int32.funcs.decode = &read_repeated_varint;
+ alltypes.rep_int32.arg = rep_int32;
+
+ alltypes.rep_int64.funcs.decode = &read_repeated_varint;
+ alltypes.rep_int64.arg = rep_int64;
+
+ alltypes.rep_uint32.funcs.decode = &read_repeated_varint;
+ alltypes.rep_uint32.arg = rep_uint32;
+
+ alltypes.rep_uint64.funcs.decode = &read_repeated_varint;
+ alltypes.rep_uint64.arg = rep_uint64;
+
+ alltypes.rep_sint32.funcs.decode = &read_repeated_svarint;
+ alltypes.rep_sint32.arg = rep_sint32;
+
+ alltypes.rep_sint64.funcs.decode = &read_repeated_svarint;
+ alltypes.rep_sint64.arg = rep_sint64;
+
+ alltypes.rep_bool.funcs.decode = &read_repeated_varint;
+ alltypes.rep_bool.arg = rep_bool;
+
+ alltypes.rep_fixed32.funcs.decode = &read_repeated_fixed32;
+ alltypes.rep_fixed32.arg = rep_fixed32;
+
+ alltypes.rep_sfixed32.funcs.decode = &read_repeated_fixed32;
+ alltypes.rep_sfixed32.arg = rep_sfixed32;
+
+ alltypes.rep_float.funcs.decode = &read_repeated_fixed32;
+ alltypes.rep_float.arg = rep_float;
+
+ alltypes.rep_fixed64.funcs.decode = &read_repeated_fixed64;
+ alltypes.rep_fixed64.arg = rep_fixed64;
+
+ alltypes.rep_sfixed64.funcs.decode = &read_repeated_fixed64;
+ alltypes.rep_sfixed64.arg = rep_sfixed64;
+
+ alltypes.rep_double.funcs.decode = &read_repeated_fixed64;
+ alltypes.rep_double.arg = rep_double;
+
+ alltypes.rep_string.funcs.decode = &read_repeated_string;
+ alltypes.rep_string.arg = rep_string;
+
+ alltypes.rep_bytes.funcs.decode = &read_repeated_string;
+ alltypes.rep_bytes.arg = rep_bytes;
+
+ alltypes.rep_submsg.funcs.decode = &read_repeated_submsg;
+ alltypes.rep_submsg.arg = rep_submsg;
+
+ alltypes.rep_enum.funcs.decode = &read_repeated_varint;
+ alltypes.rep_enum.arg = rep_enum;
+
+ alltypes.rep_emptymsg.funcs.decode = &read_emptymsg;
+
+ alltypes.req_limits.funcs.decode = &read_limits;
+
+ alltypes.end.funcs.decode = &read_varint;
+ alltypes.end.arg = (void*)1099;
+
+ /* Bind callbacks for optional fields */
+ if (mode == 1)
+ {
+ alltypes.opt_int32.funcs.decode = &read_varint;
+ alltypes.opt_int32.arg = (void*)3041;
+
+ alltypes.opt_int64.funcs.decode = &read_varint;
+ alltypes.opt_int64.arg = (void*)3042;
+
+ alltypes.opt_uint32.funcs.decode = &read_varint;
+ alltypes.opt_uint32.arg = (void*)3043;
+
+ alltypes.opt_uint64.funcs.decode = &read_varint;
+ alltypes.opt_uint64.arg = (void*)3044;
+
+ alltypes.opt_sint32.funcs.decode = &read_svarint;
+ alltypes.opt_sint32.arg = (void*)3045;
+
+ alltypes.opt_sint64.funcs.decode = &read_svarint;
+ alltypes.opt_sint64.arg = (void*)3046;
+
+ alltypes.opt_bool.funcs.decode = &read_varint;
+ alltypes.opt_bool.arg = (void*)true;
+
+ alltypes.opt_fixed32.funcs.decode = &read_fixed32;
+ alltypes.opt_fixed32.arg = &opt_fixed32;
+
+ alltypes.opt_sfixed32.funcs.decode = &read_fixed32;
+ alltypes.opt_sfixed32.arg = &opt_sfixed32;
+
+ alltypes.opt_float.funcs.decode = &read_fixed32;
+ alltypes.opt_float.arg = &opt_float;
+
+ alltypes.opt_fixed64.funcs.decode = &read_fixed64;
+ alltypes.opt_fixed64.arg = &opt_fixed64;
+
+ alltypes.opt_sfixed64.funcs.decode = &read_fixed64;
+ alltypes.opt_sfixed64.arg = &opt_sfixed64;
+
+ alltypes.opt_double.funcs.decode = &read_fixed64;
+ alltypes.opt_double.arg = &opt_double;
+
+ alltypes.opt_string.funcs.decode = &read_string;
+ alltypes.opt_string.arg = "3054";
+
+ alltypes.opt_bytes.funcs.decode = &read_string;
+ alltypes.opt_bytes.arg = "3055";
+
+ alltypes.opt_submsg.funcs.decode = &read_submsg;
+ alltypes.opt_submsg.arg = &opt_submsg;
+
+ alltypes.opt_enum.funcs.decode = &read_varint;
+ alltypes.opt_enum.arg = (void*)MyEnum_Truth;
+
+ alltypes.opt_emptymsg.funcs.decode = &read_emptymsg;
+
+ alltypes.oneof_msg1.funcs.decode = &read_submsg;
+ alltypes.oneof_msg1.arg = &oneof_msg1;
+ }
+
+ status = pb_decode(stream, AllTypes_fields, &alltypes);
+
+#ifdef PB_ENABLE_MALLOC
+ /* Just to check for any interference between pb_release() and callback fields */
+ pb_release(AllTypes_fields, &alltypes);
+#endif
+
+ return status;
+}
+
+int main(int argc, char **argv)
+{
+ uint8_t buffer[1024];
+ size_t count;
+ pb_istream_t stream;
+
+ /* Whether to expect the optional values or the default values. */
+ int mode = (argc > 1) ? atoi(argv[1]) : 0;
+
+ /* Read the data into buffer */
+ SET_BINARY_MODE(stdin);
+ count = fread(buffer, 1, sizeof(buffer), stdin);
+
+ /* Construct a pb_istream_t for reading from the buffer */
+ stream = pb_istream_from_buffer(buffer, count);
+
+ /* Decode and print out the stuff */
+ if (!check_alltypes(&stream, mode))
+ {
+ printf("Parsing failed: %s\n", PB_GET_ERROR(&stream));
+ return 1;
+ } else {
+ return 0;
+ }
+}
diff --git a/libs/nanopb/tests/alltypes_callback/encode_alltypes_callback.c b/libs/nanopb/tests/alltypes_callback/encode_alltypes_callback.c
new file mode 100644
index 00000000..b206783b
--- /dev/null
+++ b/libs/nanopb/tests/alltypes_callback/encode_alltypes_callback.c
@@ -0,0 +1,411 @@
+/* Attempts to test all the datatypes supported by ProtoBuf when used as callback fields.
+ * Note that normally there would be no reason to use callback fields for this,
+ * because each encoder defined here only gives a single field.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pb_encode.h>
+#include "alltypes.pb.h"
+#include "test_helpers.h"
+
+static bool write_varint(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ return pb_encode_tag_for_field(stream, field) &&
+ pb_encode_varint(stream, (long)*arg);
+}
+
+static bool write_svarint(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ return pb_encode_tag_for_field(stream, field) &&
+ pb_encode_svarint(stream, (long)*arg);
+}
+
+static bool write_fixed32(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ return pb_encode_tag_for_field(stream, field) &&
+ pb_encode_fixed32(stream, *arg);
+}
+
+static bool write_fixed64(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ return pb_encode_tag_for_field(stream, field) &&
+ pb_encode_fixed64(stream, *arg);
+}
+
+static bool write_string(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ return pb_encode_tag_for_field(stream, field) &&
+ pb_encode_string(stream, *arg, strlen(*arg));
+}
+
+static bool write_submsg(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+
+ return pb_encode_tag_for_field(stream, field) &&
+ pb_encode_submessage(stream, SubMessage_fields, *arg);
+}
+
+static bool write_emptymsg(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ EmptyMessage emptymsg = {0};
+ return pb_encode_tag_for_field(stream, field) &&
+ pb_encode_submessage(stream, EmptyMessage_fields, &emptymsg);
+}
+
+static bool write_repeated_varint(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ return pb_encode_tag_for_field(stream, field) &&
+ pb_encode_varint(stream, 0) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_varint(stream, 0) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_varint(stream, 0) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_varint(stream, 0) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_varint(stream, (long)*arg);
+}
+
+static bool write_repeated_svarint(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ return pb_encode_tag_for_field(stream, field) &&
+ pb_encode_svarint(stream, 0) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_svarint(stream, 0) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_svarint(stream, 0) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_svarint(stream, 0) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_svarint(stream, (long)*arg);
+}
+
+static bool write_repeated_fixed32(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ uint32_t dummy = 0;
+
+ /* Make it a packed field */
+ return pb_encode_tag(stream, PB_WT_STRING, field->tag) &&
+ pb_encode_varint(stream, 5 * 4) && /* Number of bytes */
+ pb_encode_fixed32(stream, &dummy) &&
+ pb_encode_fixed32(stream, &dummy) &&
+ pb_encode_fixed32(stream, &dummy) &&
+ pb_encode_fixed32(stream, &dummy) &&
+ pb_encode_fixed32(stream, *arg);
+}
+
+static bool write_repeated_fixed64(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ uint64_t dummy = 0;
+
+ /* Make it a packed field */
+ return pb_encode_tag(stream, PB_WT_STRING, field->tag) &&
+ pb_encode_varint(stream, 5 * 8) && /* Number of bytes */
+ pb_encode_fixed64(stream, &dummy) &&
+ pb_encode_fixed64(stream, &dummy) &&
+ pb_encode_fixed64(stream, &dummy) &&
+ pb_encode_fixed64(stream, &dummy) &&
+ pb_encode_fixed64(stream, *arg);
+}
+
+static bool write_repeated_string(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ return pb_encode_tag_for_field(stream, field) &&
+ pb_encode_string(stream, 0, 0) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_string(stream, 0, 0) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_string(stream, 0, 0) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_string(stream, 0, 0) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_string(stream, *arg, strlen(*arg));
+}
+
+static bool write_repeated_submsg(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ SubMessage dummy = {""};
+
+ return pb_encode_tag_for_field(stream, field) &&
+ pb_encode_submessage(stream, SubMessage_fields, &dummy) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_submessage(stream, SubMessage_fields, &dummy) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_submessage(stream, SubMessage_fields, &dummy) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_submessage(stream, SubMessage_fields, &dummy) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_submessage(stream, SubMessage_fields, *arg);
+}
+
+static bool write_limits(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ Limits limits = {0};
+ limits.int32_min = INT32_MIN;
+ limits.int32_max = INT32_MAX;
+ limits.uint32_min = 0;
+ limits.uint32_max = UINT32_MAX;
+ limits.int64_min = INT64_MIN;
+ limits.int64_max = INT64_MAX;
+ limits.uint64_min = 0;
+ limits.uint64_max = UINT64_MAX;
+ limits.enum_min = HugeEnum_Negative;
+ limits.enum_max = HugeEnum_Positive;
+
+ return pb_encode_tag_for_field(stream, field) &&
+ pb_encode_submessage(stream, Limits_fields, &limits);
+}
+
+static bool write_repeated_emptymsg(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ EmptyMessage emptymsg = {0};
+ return pb_encode_tag_for_field(stream, field) &&
+ pb_encode_submessage(stream, EmptyMessage_fields, &emptymsg) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_submessage(stream, EmptyMessage_fields, &emptymsg) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_submessage(stream, EmptyMessage_fields, &emptymsg) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_submessage(stream, EmptyMessage_fields, &emptymsg) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_submessage(stream, EmptyMessage_fields, &emptymsg);
+}
+
+int main(int argc, char **argv)
+{
+ int mode = (argc > 1) ? atoi(argv[1]) : 0;
+
+ /* Values for use from callbacks through pointers. */
+ uint32_t req_fixed32 = 1008;
+ int32_t req_sfixed32 = -1009;
+ float req_float = 1010.0f;
+ uint64_t req_fixed64 = 1011;
+ int64_t req_sfixed64 = -1012;
+ double req_double = 1013.0;
+ SubMessage req_submsg = {"1016", 1016};
+
+ uint32_t rep_fixed32 = 2008;
+ int32_t rep_sfixed32 = -2009;
+ float rep_float = 2010.0f;
+ uint64_t rep_fixed64 = 2011;
+ int64_t rep_sfixed64 = -2012;
+ double rep_double = 2013.0;
+ SubMessage rep_submsg = {"2016", 2016, true, 2016};
+
+ uint32_t opt_fixed32 = 3048;
+ int32_t opt_sfixed32 = 3049;
+ float opt_float = 3050.0f;
+ uint64_t opt_fixed64 = 3051;
+ int64_t opt_sfixed64 = 3052;
+ double opt_double = 3053.0f;
+ SubMessage opt_submsg = {"3056", 3056};
+
+ SubMessage oneof_msg1 = {"4059", 4059};
+
+ /* Bind callbacks for required fields */
+ AllTypes alltypes = {{{0}}};
+
+ alltypes.req_int32.funcs.encode = &write_varint;
+ alltypes.req_int32.arg = (void*)-1001;
+
+ alltypes.req_int64.funcs.encode = &write_varint;
+ alltypes.req_int64.arg = (void*)-1002;
+
+ alltypes.req_uint32.funcs.encode = &write_varint;
+ alltypes.req_uint32.arg = (void*)1003;
+
+ alltypes.req_uint32.funcs.encode = &write_varint;
+ alltypes.req_uint32.arg = (void*)1003;
+
+ alltypes.req_uint64.funcs.encode = &write_varint;
+ alltypes.req_uint64.arg = (void*)1004;
+
+ alltypes.req_sint32.funcs.encode = &write_svarint;
+ alltypes.req_sint32.arg = (void*)-1005;
+
+ alltypes.req_sint64.funcs.encode = &write_svarint;
+ alltypes.req_sint64.arg = (void*)-1006;
+
+ alltypes.req_bool.funcs.encode = &write_varint;
+ alltypes.req_bool.arg = (void*)true;
+
+ alltypes.req_fixed32.funcs.encode = &write_fixed32;
+ alltypes.req_fixed32.arg = &req_fixed32;
+
+ alltypes.req_sfixed32.funcs.encode = &write_fixed32;
+ alltypes.req_sfixed32.arg = &req_sfixed32;
+
+ alltypes.req_float.funcs.encode = &write_fixed32;
+ alltypes.req_float.arg = &req_float;
+
+ alltypes.req_fixed64.funcs.encode = &write_fixed64;
+ alltypes.req_fixed64.arg = &req_fixed64;
+
+ alltypes.req_sfixed64.funcs.encode = &write_fixed64;
+ alltypes.req_sfixed64.arg = &req_sfixed64;
+
+ alltypes.req_double.funcs.encode = &write_fixed64;
+ alltypes.req_double.arg = &req_double;
+
+ alltypes.req_string.funcs.encode = &write_string;
+ alltypes.req_string.arg = "1014";
+
+ alltypes.req_bytes.funcs.encode = &write_string;
+ alltypes.req_bytes.arg = "1015";
+
+ alltypes.req_submsg.funcs.encode = &write_submsg;
+ alltypes.req_submsg.arg = &req_submsg;
+
+ alltypes.req_enum.funcs.encode = &write_varint;
+ alltypes.req_enum.arg = (void*)MyEnum_Truth;
+
+ alltypes.req_emptymsg.funcs.encode = &write_emptymsg;
+
+ alltypes.req_fbytes.funcs.encode = &write_string;
+ alltypes.req_fbytes.arg = "1019";
+
+ /* Bind callbacks for repeated fields */
+ alltypes.rep_int32.funcs.encode = &write_repeated_varint;
+ alltypes.rep_int32.arg = (void*)-2001;
+
+ alltypes.rep_int64.funcs.encode = &write_repeated_varint;
+ alltypes.rep_int64.arg = (void*)-2002;
+
+ alltypes.rep_uint32.funcs.encode = &write_repeated_varint;
+ alltypes.rep_uint32.arg = (void*)2003;
+
+ alltypes.rep_uint64.funcs.encode = &write_repeated_varint;
+ alltypes.rep_uint64.arg = (void*)2004;
+
+ alltypes.rep_sint32.funcs.encode = &write_repeated_svarint;
+ alltypes.rep_sint32.arg = (void*)-2005;
+
+ alltypes.rep_sint64.funcs.encode = &write_repeated_svarint;
+ alltypes.rep_sint64.arg = (void*)-2006;
+
+ alltypes.rep_bool.funcs.encode = &write_repeated_varint;
+ alltypes.rep_bool.arg = (void*)true;
+
+ alltypes.rep_fixed32.funcs.encode = &write_repeated_fixed32;
+ alltypes.rep_fixed32.arg = &rep_fixed32;
+
+ alltypes.rep_sfixed32.funcs.encode = &write_repeated_fixed32;
+ alltypes.rep_sfixed32.arg = &rep_sfixed32;
+
+ alltypes.rep_float.funcs.encode = &write_repeated_fixed32;
+ alltypes.rep_float.arg = &rep_float;
+
+ alltypes.rep_fixed64.funcs.encode = &write_repeated_fixed64;
+ alltypes.rep_fixed64.arg = &rep_fixed64;
+
+ alltypes.rep_sfixed64.funcs.encode = &write_repeated_fixed64;
+ alltypes.rep_sfixed64.arg = &rep_sfixed64;
+
+ alltypes.rep_double.funcs.encode = &write_repeated_fixed64;
+ alltypes.rep_double.arg = &rep_double;
+
+ alltypes.rep_string.funcs.encode = &write_repeated_string;
+ alltypes.rep_string.arg = "2014";
+
+ alltypes.rep_bytes.funcs.encode = &write_repeated_string;
+ alltypes.rep_bytes.arg = "2015";
+
+ alltypes.rep_submsg.funcs.encode = &write_repeated_submsg;
+ alltypes.rep_submsg.arg = &rep_submsg;
+
+ alltypes.rep_enum.funcs.encode = &write_repeated_varint;
+ alltypes.rep_enum.arg = (void*)MyEnum_Truth;
+
+ alltypes.rep_emptymsg.funcs.encode = &write_repeated_emptymsg;
+
+ alltypes.rep_fbytes.funcs.encode = &write_repeated_string;
+ alltypes.rep_fbytes.arg = "2019";
+
+ alltypes.req_limits.funcs.encode = &write_limits;
+
+ /* Bind callbacks for optional fields */
+ if (mode != 0)
+ {
+ alltypes.opt_int32.funcs.encode = &write_varint;
+ alltypes.opt_int32.arg = (void*)3041;
+
+ alltypes.opt_int64.funcs.encode = &write_varint;
+ alltypes.opt_int64.arg = (void*)3042;
+
+ alltypes.opt_uint32.funcs.encode = &write_varint;
+ alltypes.opt_uint32.arg = (void*)3043;
+
+ alltypes.opt_uint64.funcs.encode = &write_varint;
+ alltypes.opt_uint64.arg = (void*)3044;
+
+ alltypes.opt_sint32.funcs.encode = &write_svarint;
+ alltypes.opt_sint32.arg = (void*)3045;
+
+ alltypes.opt_sint64.funcs.encode = &write_svarint;
+ alltypes.opt_sint64.arg = (void*)3046;
+
+ alltypes.opt_bool.funcs.encode = &write_varint;
+ alltypes.opt_bool.arg = (void*)true;
+
+ alltypes.opt_fixed32.funcs.encode = &write_fixed32;
+ alltypes.opt_fixed32.arg = &opt_fixed32;
+
+ alltypes.opt_sfixed32.funcs.encode = &write_fixed32;
+ alltypes.opt_sfixed32.arg = &opt_sfixed32;
+
+ alltypes.opt_float.funcs.encode = &write_fixed32;
+ alltypes.opt_float.arg = &opt_float;
+
+ alltypes.opt_fixed64.funcs.encode = &write_fixed64;
+ alltypes.opt_fixed64.arg = &opt_fixed64;
+
+ alltypes.opt_sfixed64.funcs.encode = &write_fixed64;
+ alltypes.opt_sfixed64.arg = &opt_sfixed64;
+
+ alltypes.opt_double.funcs.encode = &write_fixed64;
+ alltypes.opt_double.arg = &opt_double;
+
+ alltypes.opt_string.funcs.encode = &write_string;
+ alltypes.opt_string.arg = "3054";
+
+ alltypes.opt_bytes.funcs.encode = &write_string;
+ alltypes.opt_bytes.arg = "3055";
+
+ alltypes.opt_submsg.funcs.encode = &write_submsg;
+ alltypes.opt_submsg.arg = &opt_submsg;
+
+ alltypes.opt_enum.funcs.encode = &write_varint;
+ alltypes.opt_enum.arg = (void*)MyEnum_Truth;
+
+ alltypes.opt_emptymsg.funcs.encode = &write_emptymsg;
+
+ alltypes.opt_fbytes.funcs.encode = &write_string;
+ alltypes.opt_fbytes.arg = "3059";
+
+ alltypes.oneof_msg1.funcs.encode = &write_submsg;
+ alltypes.oneof_msg1.arg = &oneof_msg1;
+ }
+
+ alltypes.end.funcs.encode = &write_varint;
+ alltypes.end.arg = (void*)1099;
+
+ {
+ uint8_t buffer[2048];
+ 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: %s\n", PB_GET_ERROR(&stream));
+ return 1; /* Failure */
+ }
+ }
+}
diff --git a/libs/nanopb/tests/alltypes_pointer/SConscript b/libs/nanopb/tests/alltypes_pointer/SConscript
new file mode 100644
index 00000000..b095ae03
--- /dev/null
+++ b/libs/nanopb/tests/alltypes_pointer/SConscript
@@ -0,0 +1,40 @@
+# Encode the AllTypes message using pointers for all fields, and verify the
+# output against the normal AllTypes test case.
+
+Import("env", "malloc_env")
+
+c = Copy("$TARGET", "$SOURCE")
+env.Command("alltypes.proto", "#alltypes/alltypes.proto", c)
+
+env.NanopbProto(["alltypes", "alltypes.options"])
+enc = malloc_env.Program(["encode_alltypes_pointer.c",
+ "alltypes.pb.c",
+ "$COMMON/pb_encode_with_malloc.o",
+ "$COMMON/pb_common_with_malloc.o",
+ "$COMMON/malloc_wrappers.o"])
+dec = malloc_env.Program(["decode_alltypes_pointer.c",
+ "alltypes.pb.c",
+ "$COMMON/pb_decode_with_malloc.o",
+ "$COMMON/pb_common_with_malloc.o",
+ "$COMMON/malloc_wrappers.o"])
+
+# Encode and compare results to non-pointer alltypes test case
+env.RunTest(enc)
+env.Compare(["encode_alltypes_pointer.output", "$BUILD/alltypes/encode_alltypes.output"])
+
+# Decode (under valgrind if available)
+valgrind = env.WhereIs('valgrind')
+kwargs = {}
+if valgrind:
+ kwargs['COMMAND'] = valgrind
+ kwargs['ARGS'] = ["-q", "--error-exitcode=99", dec[0].abspath]
+
+env.RunTest("decode_alltypes.output", [dec, "encode_alltypes_pointer.output"], **kwargs)
+
+# Do the same thing with the optional fields present
+env.RunTest("optionals.output", enc, ARGS = ['1'])
+env.Compare(["optionals.output", "$BUILD/alltypes/optionals.output"])
+
+kwargs['ARGS'] = kwargs.get('ARGS', []) + ['1']
+env.RunTest("optionals.decout", [dec, "optionals.output"], **kwargs)
+
diff --git a/libs/nanopb/tests/alltypes_pointer/alltypes.options b/libs/nanopb/tests/alltypes_pointer/alltypes.options
new file mode 100644
index 00000000..8699fe27
--- /dev/null
+++ b/libs/nanopb/tests/alltypes_pointer/alltypes.options
@@ -0,0 +1,4 @@
+# Generate all fields as pointers.
+* type:FT_POINTER
+*.*fbytes fixed_length:true max_size:4
+
diff --git a/libs/nanopb/tests/alltypes_pointer/decode_alltypes_pointer.c b/libs/nanopb/tests/alltypes_pointer/decode_alltypes_pointer.c
new file mode 100644
index 00000000..4ee6f8bf
--- /dev/null
+++ b/libs/nanopb/tests/alltypes_pointer/decode_alltypes_pointer.c
@@ -0,0 +1,186 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <pb_decode.h>
+#include "alltypes.pb.h"
+#include "test_helpers.h"
+
+#define TEST(x) if (!(x)) { \
+ fprintf(stderr, "Test " #x " failed.\n"); \
+ status = false; \
+ }
+
+/* This function is called once from main(), it handles
+ the decoding and checks the fields. */
+bool check_alltypes(pb_istream_t *stream, int mode)
+{
+ bool status = true;
+ AllTypes alltypes;
+
+ /* Fill with garbage to better detect initialization errors */
+ memset(&alltypes, 0xAA, sizeof(alltypes));
+ alltypes.extensions = 0;
+
+ if (!pb_decode(stream, AllTypes_fields, &alltypes))
+ return false;
+
+ TEST(alltypes.req_int32 && *alltypes.req_int32 == -1001);
+ TEST(alltypes.req_int64 && *alltypes.req_int64 == -1002);
+ TEST(alltypes.req_uint32 && *alltypes.req_uint32 == 1003);
+ TEST(alltypes.req_uint64 && *alltypes.req_uint64 == 1004);
+ TEST(alltypes.req_sint32 && *alltypes.req_sint32 == -1005);
+ TEST(alltypes.req_sint64 && *alltypes.req_sint64 == -1006);
+ TEST(alltypes.req_bool && *alltypes.req_bool == true);
+
+ TEST(alltypes.req_fixed32 && *alltypes.req_fixed32 == 1008);
+ TEST(alltypes.req_sfixed32 && *alltypes.req_sfixed32 == -1009);
+ TEST(alltypes.req_float && *alltypes.req_float == 1010.0f);
+
+ TEST(alltypes.req_fixed64 && *alltypes.req_fixed64 == 1011);
+ TEST(alltypes.req_sfixed64 && *alltypes.req_sfixed64 == -1012);
+ TEST(alltypes.req_double && *alltypes.req_double == 1013.0f);
+
+ TEST(alltypes.req_string && strcmp(alltypes.req_string, "1014") == 0);
+ TEST(alltypes.req_bytes && alltypes.req_bytes->size == 4);
+ TEST(alltypes.req_bytes && memcmp(&alltypes.req_bytes->bytes, "1015", 4) == 0);
+ TEST(alltypes.req_submsg && alltypes.req_submsg->substuff1
+ && strcmp(alltypes.req_submsg->substuff1, "1016") == 0);
+ TEST(alltypes.req_submsg && alltypes.req_submsg->substuff2
+ && *alltypes.req_submsg->substuff2 == 1016);
+ TEST(alltypes.req_enum && *alltypes.req_enum == MyEnum_Truth);
+ TEST(alltypes.req_fbytes && memcmp(alltypes.req_fbytes, "1019", 4) == 0);
+
+ TEST(alltypes.rep_int32_count == 5 && alltypes.rep_int32[4] == -2001 && alltypes.rep_int32[0] == 0);
+ TEST(alltypes.rep_int64_count == 5 && alltypes.rep_int64[4] == -2002 && alltypes.rep_int64[0] == 0);
+ TEST(alltypes.rep_uint32_count == 5 && alltypes.rep_uint32[4] == 2003 && alltypes.rep_uint32[0] == 0);
+ TEST(alltypes.rep_uint64_count == 5 && alltypes.rep_uint64[4] == 2004 && alltypes.rep_uint64[0] == 0);
+ TEST(alltypes.rep_sint32_count == 5 && alltypes.rep_sint32[4] == -2005 && alltypes.rep_sint32[0] == 0);
+ TEST(alltypes.rep_sint64_count == 5 && alltypes.rep_sint64[4] == -2006 && alltypes.rep_sint64[0] == 0);
+ TEST(alltypes.rep_bool_count == 5 && alltypes.rep_bool[4] == true && alltypes.rep_bool[0] == false);
+
+ TEST(alltypes.rep_fixed32_count == 5 && alltypes.rep_fixed32[4] == 2008 && alltypes.rep_fixed32[0] == 0);
+ TEST(alltypes.rep_sfixed32_count == 5 && alltypes.rep_sfixed32[4] == -2009 && alltypes.rep_sfixed32[0] == 0);
+ TEST(alltypes.rep_float_count == 5 && alltypes.rep_float[4] == 2010.0f && alltypes.rep_float[0] == 0.0f);
+
+ TEST(alltypes.rep_fixed64_count == 5 && alltypes.rep_fixed64[4] == 2011 && alltypes.rep_fixed64[0] == 0);
+ TEST(alltypes.rep_sfixed64_count == 5 && alltypes.rep_sfixed64[4] == -2012 && alltypes.rep_sfixed64[0] == 0);
+ TEST(alltypes.rep_double_count == 5 && alltypes.rep_double[4] == 2013.0 && alltypes.rep_double[0] == 0.0);
+
+ TEST(alltypes.rep_string_count == 5 && strcmp(alltypes.rep_string[4], "2014") == 0 && alltypes.rep_string[0][0] == '\0');
+ TEST(alltypes.rep_bytes_count == 5 && alltypes.rep_bytes[4]->size == 4 && alltypes.rep_bytes[0]->size == 0);
+ TEST(memcmp(&alltypes.rep_bytes[4]->bytes, "2015", 4) == 0);
+
+ TEST(alltypes.rep_submsg_count == 5);
+ TEST(strcmp(alltypes.rep_submsg[4].substuff1, "2016") == 0 && alltypes.rep_submsg[0].substuff1[0] == '\0');
+ TEST(*alltypes.rep_submsg[4].substuff2 == 2016 && *alltypes.rep_submsg[0].substuff2 == 0);
+ TEST(*alltypes.rep_submsg[4].substuff3 == 2016 && alltypes.rep_submsg[0].substuff3 == NULL);
+
+ TEST(alltypes.rep_enum_count == 5 && alltypes.rep_enum[4] == MyEnum_Truth && alltypes.rep_enum[0] == MyEnum_Zero);
+ TEST(alltypes.rep_emptymsg_count == 5);
+ TEST(alltypes.rep_fbytes_count == 5);
+ TEST(alltypes.rep_fbytes[0][0] == 0 && alltypes.rep_fbytes[0][3] == 0);
+ TEST(memcmp(alltypes.rep_fbytes[4], "2019", 4) == 0);
+
+ if (mode == 0)
+ {
+ /* Expect that optional values are not present */
+ TEST(alltypes.opt_int32 == NULL);
+ TEST(alltypes.opt_int64 == NULL);
+ TEST(alltypes.opt_uint32 == NULL);
+ TEST(alltypes.opt_uint64 == NULL);
+ TEST(alltypes.opt_sint32 == NULL);
+ TEST(alltypes.opt_sint64 == NULL);
+ TEST(alltypes.opt_bool == NULL);
+
+ TEST(alltypes.opt_fixed32 == NULL);
+ TEST(alltypes.opt_sfixed32 == NULL);
+ TEST(alltypes.opt_float == NULL);
+ TEST(alltypes.opt_fixed64 == NULL);
+ TEST(alltypes.opt_sfixed64 == NULL);
+ TEST(alltypes.opt_double == NULL);
+
+ TEST(alltypes.opt_string == NULL);
+ TEST(alltypes.opt_bytes == NULL);
+ TEST(alltypes.opt_submsg == NULL);
+ TEST(alltypes.opt_enum == NULL);
+ TEST(alltypes.opt_fbytes == NULL);
+
+ TEST(alltypes.which_oneof == 0);
+ }
+ else
+ {
+ /* Expect filled-in values */
+ TEST(alltypes.opt_int32 && *alltypes.opt_int32 == 3041);
+ TEST(alltypes.opt_int64 && *alltypes.opt_int64 == 3042);
+ TEST(alltypes.opt_uint32 && *alltypes.opt_uint32 == 3043);
+ TEST(alltypes.opt_uint64 && *alltypes.opt_uint64 == 3044);
+ TEST(alltypes.opt_sint32 && *alltypes.opt_sint32 == 3045);
+ TEST(alltypes.opt_sint64 && *alltypes.opt_sint64 == 3046);
+ TEST(alltypes.opt_bool && *alltypes.opt_bool == true);
+
+ TEST(alltypes.opt_fixed32 && *alltypes.opt_fixed32 == 3048);
+ TEST(alltypes.opt_sfixed32 && *alltypes.opt_sfixed32== 3049);
+ TEST(alltypes.opt_float && *alltypes.opt_float == 3050.0f);
+ TEST(alltypes.opt_fixed64 && *alltypes.opt_fixed64 == 3051);
+ TEST(alltypes.opt_sfixed64 && *alltypes.opt_sfixed64== 3052);
+ TEST(alltypes.opt_double && *alltypes.opt_double == 3053.0);
+
+ TEST(alltypes.opt_string && strcmp(alltypes.opt_string, "3054") == 0);
+ TEST(alltypes.opt_bytes && alltypes.opt_bytes->size == 4);
+ TEST(alltypes.opt_bytes && memcmp(&alltypes.opt_bytes->bytes, "3055", 4) == 0);
+ TEST(alltypes.opt_submsg && strcmp(alltypes.opt_submsg->substuff1, "3056") == 0);
+ TEST(alltypes.opt_submsg && *alltypes.opt_submsg->substuff2 == 3056);
+ TEST(alltypes.opt_enum && *alltypes.opt_enum == MyEnum_Truth);
+ TEST(alltypes.opt_emptymsg);
+ TEST(alltypes.opt_fbytes && memcmp(alltypes.opt_fbytes, "3059", 4) == 0);
+
+ TEST(alltypes.which_oneof == AllTypes_oneof_msg1_tag);
+ TEST(alltypes.oneof.oneof_msg1 && strcmp(alltypes.oneof.oneof_msg1->substuff1, "4059") == 0);
+ TEST(alltypes.oneof.oneof_msg1->substuff2 && *alltypes.oneof.oneof_msg1->substuff2 == 4059);
+ }
+
+ TEST(alltypes.req_limits->int32_min && *alltypes.req_limits->int32_min == INT32_MIN);
+ TEST(alltypes.req_limits->int32_max && *alltypes.req_limits->int32_max == INT32_MAX);
+ TEST(alltypes.req_limits->uint32_min && *alltypes.req_limits->uint32_min == 0);
+ TEST(alltypes.req_limits->uint32_max && *alltypes.req_limits->uint32_max == UINT32_MAX);
+ TEST(alltypes.req_limits->int64_min && *alltypes.req_limits->int64_min == INT64_MIN);
+ TEST(alltypes.req_limits->int64_max && *alltypes.req_limits->int64_max == INT64_MAX);
+ TEST(alltypes.req_limits->uint64_min && *alltypes.req_limits->uint64_min == 0);
+ TEST(alltypes.req_limits->uint64_max && *alltypes.req_limits->uint64_max == UINT64_MAX);
+ TEST(alltypes.req_limits->enum_min && *alltypes.req_limits->enum_min == HugeEnum_Negative);
+ TEST(alltypes.req_limits->enum_max && *alltypes.req_limits->enum_max == HugeEnum_Positive);
+
+ TEST(alltypes.end && *alltypes.end == 1099);
+
+ pb_release(AllTypes_fields, &alltypes);
+
+ return status;
+}
+
+int main(int argc, char **argv)
+{
+ uint8_t buffer[1024];
+ size_t count;
+ pb_istream_t stream;
+
+ /* Whether to expect the optional values or the default values. */
+ int mode = (argc > 1) ? atoi(argv[1]) : 0;
+
+ /* Read the data into buffer */
+ SET_BINARY_MODE(stdin);
+ count = fread(buffer, 1, sizeof(buffer), stdin);
+
+ /* Construct a pb_istream_t for reading from the buffer */
+ stream = pb_istream_from_buffer(buffer, count);
+
+ /* Decode and verify the message */
+ if (!check_alltypes(&stream, mode))
+ {
+ fprintf(stderr, "Test failed: %s\n", PB_GET_ERROR(&stream));
+ return 1;
+ }
+ else
+ {
+ return 0;
+ }
+}
diff --git a/libs/nanopb/tests/alltypes_pointer/encode_alltypes_pointer.c b/libs/nanopb/tests/alltypes_pointer/encode_alltypes_pointer.c
new file mode 100644
index 00000000..a39af6fa
--- /dev/null
+++ b/libs/nanopb/tests/alltypes_pointer/encode_alltypes_pointer.c
@@ -0,0 +1,200 @@
+/* Attempts to test all the datatypes supported by ProtoBuf.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pb_encode.h>
+#include "alltypes.pb.h"
+#include "test_helpers.h"
+
+int main(int argc, char **argv)
+{
+ int mode = (argc > 1) ? atoi(argv[1]) : 0;
+
+ /* Values for required fields */
+ int32_t req_int32 = -1001;
+ int64_t req_int64 = -1002;
+ uint32_t req_uint32 = 1003;
+ uint64_t req_uint64 = 1004;
+ int32_t req_sint32 = -1005;
+ int64_t req_sint64 = -1006;
+ bool req_bool = true;
+ uint32_t req_fixed32 = 1008;
+ int32_t req_sfixed32 = -1009;
+ float req_float = 1010.0f;
+ uint64_t req_fixed64 = 1011;
+ int64_t req_sfixed64 = -1012;
+ double req_double = 1013.0;
+ char* req_string = "1014";
+ PB_BYTES_ARRAY_T(4) req_bytes = {4, {'1', '0', '1', '5'}};
+ static int32_t req_substuff = 1016;
+ SubMessage req_submsg = {"1016", &req_substuff};
+ MyEnum req_enum = MyEnum_Truth;
+ EmptyMessage req_emptymsg = {0};
+ pb_byte_t req_fbytes[4] = {'1', '0', '1', '9'};
+
+ int32_t end = 1099;
+
+ /* Values for repeated fields */
+ int32_t rep_int32[5] = {0, 0, 0, 0, -2001};
+ int64_t rep_int64[5] = {0, 0, 0, 0, -2002};
+ uint32_t rep_uint32[5] = {0, 0, 0, 0, 2003};
+ uint64_t rep_uint64[5] = {0, 0, 0, 0, 2004};
+ int32_t rep_sint32[5] = {0, 0, 0, 0, -2005};
+ int64_t rep_sint64[5] = {0, 0, 0, 0, -2006};
+ bool rep_bool[5] = {false, false, false, false, true};
+ uint32_t rep_fixed32[5] = {0, 0, 0, 0, 2008};
+ int32_t rep_sfixed32[5] = {0, 0, 0, 0, -2009};
+ float rep_float[5] = {0, 0, 0, 0, 2010.0f};
+ uint64_t rep_fixed64[5] = {0, 0, 0, 0, 2011};
+ int64_t rep_sfixed64[5] = {0, 0, 0, 0, -2012};
+ double rep_double[5] = {0, 0, 0, 0, 2013.0f};
+ char* rep_string[5] = {"", "", "", "", "2014"};
+ static PB_BYTES_ARRAY_T(4) rep_bytes_4 = {4, {'2', '0', '1', '5'}};
+ pb_bytes_array_t *rep_bytes[5]= {NULL, NULL, NULL, NULL, (pb_bytes_array_t*)&rep_bytes_4};
+ static int32_t rep_sub2zero = 0;
+ static int32_t rep_substuff2 = 2016;
+ static uint32_t rep_substuff3 = 2016;
+ SubMessage rep_submsg[5] = {{"", &rep_sub2zero},
+ {"", &rep_sub2zero},
+ {"", &rep_sub2zero},
+ {"", &rep_sub2zero},
+ {"2016", &rep_substuff2, &rep_substuff3}};
+ MyEnum rep_enum[5] = {0, 0, 0, 0, MyEnum_Truth};
+ EmptyMessage rep_emptymsg[5] = {{0}, {0}, {0}, {0}, {0}};
+ pb_byte_t rep_fbytes[5][4] = {{0}, {0}, {0}, {0}, {'2', '0', '1', '9'}};
+
+ /* Values for optional fields */
+ int32_t opt_int32 = 3041;
+ int64_t opt_int64 = 3042;
+ uint32_t opt_uint32 = 3043;
+ uint64_t opt_uint64 = 3044;
+ int32_t opt_sint32 = 3045;
+ int64_t opt_sint64 = 3046;
+ bool opt_bool = true;
+ uint32_t opt_fixed32 = 3048;
+ int32_t opt_sfixed32 = 3049;
+ float opt_float = 3050.0f;
+ uint64_t opt_fixed64 = 3051;
+ int64_t opt_sfixed64 = 3052;
+ double opt_double = 3053.0;
+ char* opt_string = "3054";
+ PB_BYTES_ARRAY_T(4) opt_bytes = {4, {'3', '0', '5', '5'}};
+ static int32_t opt_substuff = 3056;
+ SubMessage opt_submsg = {"3056", &opt_substuff};
+ MyEnum opt_enum = MyEnum_Truth;
+ EmptyMessage opt_emptymsg = {0};
+ pb_byte_t opt_fbytes[4] = {'3', '0', '5', '9'};
+
+ static int32_t oneof_substuff = 4059;
+ SubMessage oneof_msg1 = {"4059", &oneof_substuff};
+
+ /* Values for the Limits message. */
+ static int32_t int32_min = INT32_MIN;
+ static int32_t int32_max = INT32_MAX;
+ static uint32_t uint32_min = 0;
+ static uint32_t uint32_max = UINT32_MAX;
+ static int64_t int64_min = INT64_MIN;
+ static int64_t int64_max = INT64_MAX;
+ static uint64_t uint64_min = 0;
+ static uint64_t uint64_max = UINT64_MAX;
+ static HugeEnum enum_min = HugeEnum_Negative;
+ static HugeEnum enum_max = HugeEnum_Positive;
+ Limits req_limits = {&int32_min, &int32_max,
+ &uint32_min, &uint32_max,
+ &int64_min, &int64_max,
+ &uint64_min, &uint64_max,
+ &enum_min, &enum_max};
+
+ /* Initialize the message struct with pointers to the fields. */
+ AllTypes alltypes = {0};
+
+ alltypes.req_int32 = &req_int32;
+ alltypes.req_int64 = &req_int64;
+ alltypes.req_uint32 = &req_uint32;
+ alltypes.req_uint64 = &req_uint64;
+ alltypes.req_sint32 = &req_sint32;
+ alltypes.req_sint64 = &req_sint64;
+ alltypes.req_bool = &req_bool;
+ alltypes.req_fixed32 = &req_fixed32;
+ alltypes.req_sfixed32 = &req_sfixed32;
+ alltypes.req_float = &req_float;
+ alltypes.req_fixed64 = &req_fixed64;
+ alltypes.req_sfixed64 = &req_sfixed64;
+ alltypes.req_double = &req_double;
+ alltypes.req_string = req_string;
+ alltypes.req_bytes = (pb_bytes_array_t*)&req_bytes;
+ alltypes.req_submsg = &req_submsg;
+ alltypes.req_enum = &req_enum;
+ alltypes.req_emptymsg = &req_emptymsg;
+ alltypes.req_fbytes = &req_fbytes;
+ alltypes.req_limits = &req_limits;
+
+ alltypes.rep_int32_count = 5; alltypes.rep_int32 = rep_int32;
+ alltypes.rep_int64_count = 5; alltypes.rep_int64 = rep_int64;
+ alltypes.rep_uint32_count = 5; alltypes.rep_uint32 = rep_uint32;
+ alltypes.rep_uint64_count = 5; alltypes.rep_uint64 = rep_uint64;
+ alltypes.rep_sint32_count = 5; alltypes.rep_sint32 = rep_sint32;
+ alltypes.rep_sint64_count = 5; alltypes.rep_sint64 = rep_sint64;
+ alltypes.rep_bool_count = 5; alltypes.rep_bool = rep_bool;
+ alltypes.rep_fixed32_count = 5; alltypes.rep_fixed32 = rep_fixed32;
+ alltypes.rep_sfixed32_count = 5; alltypes.rep_sfixed32 = rep_sfixed32;
+ alltypes.rep_float_count = 5; alltypes.rep_float = rep_float;
+ alltypes.rep_fixed64_count = 5; alltypes.rep_fixed64 = rep_fixed64;
+ alltypes.rep_sfixed64_count = 5; alltypes.rep_sfixed64 = rep_sfixed64;
+ alltypes.rep_double_count = 5; alltypes.rep_double = rep_double;
+ alltypes.rep_string_count = 5; alltypes.rep_string = rep_string;
+ alltypes.rep_bytes_count = 5; alltypes.rep_bytes = rep_bytes;
+ alltypes.rep_submsg_count = 5; alltypes.rep_submsg = rep_submsg;
+ alltypes.rep_enum_count = 5; alltypes.rep_enum = rep_enum;
+ alltypes.rep_emptymsg_count = 5; alltypes.rep_emptymsg = rep_emptymsg;
+ alltypes.rep_fbytes_count = 5; alltypes.rep_fbytes = rep_fbytes;
+
+ if (mode != 0)
+ {
+ /* Fill in values for optional fields */
+ alltypes.opt_int32 = &opt_int32;
+ alltypes.opt_int64 = &opt_int64;
+ alltypes.opt_uint32 = &opt_uint32;
+ alltypes.opt_uint64 = &opt_uint64;
+ alltypes.opt_sint32 = &opt_sint32;
+ alltypes.opt_sint64 = &opt_sint64;
+ alltypes.opt_bool = &opt_bool;
+ alltypes.opt_fixed32 = &opt_fixed32;
+ alltypes.opt_sfixed32 = &opt_sfixed32;
+ alltypes.opt_float = &opt_float;
+ alltypes.opt_fixed64 = &opt_fixed64;
+ alltypes.opt_sfixed64 = &opt_sfixed64;
+ alltypes.opt_double = &opt_double;
+ alltypes.opt_string = opt_string;
+ alltypes.opt_bytes = (pb_bytes_array_t*)&opt_bytes;
+ alltypes.opt_submsg = &opt_submsg;
+ alltypes.opt_enum = &opt_enum;
+ alltypes.opt_emptymsg = &opt_emptymsg;
+ alltypes.opt_fbytes = &opt_fbytes;
+
+ alltypes.which_oneof = AllTypes_oneof_msg1_tag;
+ alltypes.oneof.oneof_msg1 = &oneof_msg1;
+ }
+
+ alltypes.end = &end;
+
+ {
+ uint8_t buffer[4096];
+ 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: %s\n", PB_GET_ERROR(&stream));
+ return 1; /* Failure */
+ }
+ }
+}
diff --git a/libs/nanopb/tests/alltypes_proto3/SConscript b/libs/nanopb/tests/alltypes_proto3/SConscript
new file mode 100644
index 00000000..c0b2fc1b
--- /dev/null
+++ b/libs/nanopb/tests/alltypes_proto3/SConscript
@@ -0,0 +1,45 @@
+# Version of AllTypes test case for protobuf 3 file format.
+
+Import("env")
+
+import re
+match = None
+if 'PROTOC_VERSION' in env:
+ match = re.search('([0-9]+).([0-9]+).([0-9]+)', env['PROTOC_VERSION'])
+
+if match:
+ version = map(int, match.groups())
+
+# proto3 syntax is supported by protoc >= 3.0.0
+if env.GetOption('clean') or (match and version[0] >= 3):
+
+ env.NanopbProto(["alltypes", "alltypes.options"])
+ enc = env.Program(["encode_alltypes.c", "alltypes.pb.c", "$COMMON/pb_encode.o", "$COMMON/pb_common.o"])
+ dec = env.Program(["decode_alltypes.c", "alltypes.pb.c", "$COMMON/pb_decode.o", "$COMMON/pb_common.o"])
+
+ # Test the round-trip from nanopb encoder to nanopb decoder
+ env.RunTest(enc)
+ env.RunTest([dec, "encode_alltypes.output"])
+
+ # Re-encode the data using protoc, and check that the results from nanopb
+ # match byte-per-byte to the protoc output.
+ env.Decode("encode_alltypes.output.decoded",
+ ["encode_alltypes.output", "alltypes.proto"],
+ MESSAGE='AllTypes')
+ env.Encode("encode_alltypes.output.recoded",
+ ["encode_alltypes.output.decoded", "alltypes.proto"],
+ MESSAGE='AllTypes')
+ env.Compare(["encode_alltypes.output", "encode_alltypes.output.recoded"])
+
+ # Do the same checks with the optional fields present.
+ env.RunTest("optionals.output", enc, ARGS = ['1'])
+ env.RunTest("optionals.decout", [dec, "optionals.output"], ARGS = ['1'])
+ env.Decode("optionals.output.decoded",
+ ["optionals.output", "alltypes.proto"],
+ MESSAGE='AllTypes')
+ env.Encode("optionals.output.recoded",
+ ["optionals.output.decoded", "alltypes.proto"],
+ MESSAGE='AllTypes')
+ env.Compare(["optionals.output", "optionals.output.recoded"])
+
+
diff --git a/libs/nanopb/tests/alltypes_proto3/alltypes.options b/libs/nanopb/tests/alltypes_proto3/alltypes.options
new file mode 100644
index 00000000..78dd08d1
--- /dev/null
+++ b/libs/nanopb/tests/alltypes_proto3/alltypes.options
@@ -0,0 +1,4 @@
+* max_size:16
+* max_count:5
+*.*fbytes fixed_length:true max_size:4
+
diff --git a/libs/nanopb/tests/alltypes_proto3/alltypes.proto b/libs/nanopb/tests/alltypes_proto3/alltypes.proto
new file mode 100644
index 00000000..f66109ec
--- /dev/null
+++ b/libs/nanopb/tests/alltypes_proto3/alltypes.proto
@@ -0,0 +1,100 @@
+syntax = "proto3";
+// package name placeholder
+
+message SubMessage {
+ string substuff1 = 1;
+ int32 substuff2 = 2;
+ fixed32 substuff3 = 3;
+}
+
+message EmptyMessage {
+
+}
+
+enum HugeEnum {
+ HE_Zero = 0;
+ Negative = -2147483647; /* protoc doesn't accept -2147483648 here */
+ Positive = 2147483647;
+}
+
+message Limits {
+ int32 int32_min = 1;
+ int32 int32_max = 2;
+ uint32 uint32_min = 3;
+ uint32 uint32_max = 4;
+ int64 int64_min = 5;
+ int64 int64_max = 6;
+ uint64 uint64_min = 7;
+ uint64 uint64_max = 8;
+ HugeEnum enum_min = 9;
+ HugeEnum enum_max = 10;
+}
+
+enum MyEnum {
+ Zero = 0;
+ First = 1;
+ Second = 2;
+ Truth = 42;
+}
+
+message AllTypes {
+ int32 sng_int32 = 1;
+ int64 sng_int64 = 2;
+ uint32 sng_uint32 = 3;
+ uint64 sng_uint64 = 4;
+ sint32 sng_sint32 = 5;
+ sint64 sng_sint64 = 6;
+ bool sng_bool = 7;
+
+ fixed32 sng_fixed32 = 8;
+ sfixed32 sng_sfixed32= 9;
+ float sng_float = 10;
+
+ fixed64 sng_fixed64 = 11;
+ sfixed64 sng_sfixed64= 12;
+ double sng_double = 13;
+
+ string sng_string = 14;
+ bytes sng_bytes = 15;
+ SubMessage sng_submsg = 16;
+ MyEnum sng_enum = 17;
+ EmptyMessage sng_emptymsg = 18;
+ bytes sng_fbytes = 19;
+
+ repeated int32 rep_int32 = 21 [packed = true];
+ repeated int64 rep_int64 = 22 [packed = true];
+ repeated uint32 rep_uint32 = 23 [packed = true];
+ repeated uint64 rep_uint64 = 24 [packed = true];
+ repeated sint32 rep_sint32 = 25 [packed = true];
+ repeated sint64 rep_sint64 = 26 [packed = true];
+ repeated bool rep_bool = 27 [packed = true];
+
+ repeated fixed32 rep_fixed32 = 28 [packed = true];
+ repeated sfixed32 rep_sfixed32= 29 [packed = true];
+ repeated float rep_float = 30 [packed = true];
+
+ repeated fixed64 rep_fixed64 = 31 [packed = true];
+ repeated sfixed64 rep_sfixed64= 32 [packed = true];
+ repeated double rep_double = 33 [packed = true];
+
+ repeated string rep_string = 34;
+ repeated bytes rep_bytes = 35;
+ repeated SubMessage rep_submsg = 36;
+ repeated MyEnum rep_enum = 37 [packed = true];
+ repeated EmptyMessage rep_emptymsg = 38;
+ repeated bytes rep_fbytes = 39;
+
+ oneof oneof
+ {
+ SubMessage oneof_msg1 = 59;
+ EmptyMessage oneof_msg2 = 60;
+ }
+
+ // Check that extreme integer values are handled correctly
+ Limits req_limits = 98;
+
+ // 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.
+ int32 end = 99;
+}
+
diff --git a/libs/nanopb/tests/alltypes_proto3/decode_alltypes.c b/libs/nanopb/tests/alltypes_proto3/decode_alltypes.c
new file mode 100644
index 00000000..51c1c41e
--- /dev/null
+++ b/libs/nanopb/tests/alltypes_proto3/decode_alltypes.c
@@ -0,0 +1,167 @@
+/* Tests the decoding of all types.
+ * This is the counterpart of test_encode3.
+ * Run e.g. ./test_encode3 | ./test_decode3
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <pb_decode.h>
+#include "alltypes.pb.h"
+#include "test_helpers.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, int mode)
+{
+ AllTypes alltypes = AllTypes_init_zero;
+
+ /* Fill with garbage to better detect initialization errors */
+ memset(&alltypes, 0xAA, sizeof(alltypes));
+
+ if (!pb_decode(stream, AllTypes_fields, &alltypes))
+ return false;
+
+ TEST(alltypes.rep_int32_count == 5 && alltypes.rep_int32[4] == -2001 && alltypes.rep_int32[0] == 0);
+ TEST(alltypes.rep_int64_count == 5 && alltypes.rep_int64[4] == -2002 && alltypes.rep_int64[0] == 0);
+ TEST(alltypes.rep_uint32_count == 5 && alltypes.rep_uint32[4] == 2003 && alltypes.rep_uint32[0] == 0);
+ TEST(alltypes.rep_uint64_count == 5 && alltypes.rep_uint64[4] == 2004 && alltypes.rep_uint64[0] == 0);
+ TEST(alltypes.rep_sint32_count == 5 && alltypes.rep_sint32[4] == -2005 && alltypes.rep_sint32[0] == 0);
+ TEST(alltypes.rep_sint64_count == 5 && alltypes.rep_sint64[4] == -2006 && alltypes.rep_sint64[0] == 0);
+ TEST(alltypes.rep_bool_count == 5 && alltypes.rep_bool[4] == true && alltypes.rep_bool[0] == false);
+
+ TEST(alltypes.rep_fixed32_count == 5 && alltypes.rep_fixed32[4] == 2008 && alltypes.rep_fixed32[0] == 0);
+ TEST(alltypes.rep_sfixed32_count == 5 && alltypes.rep_sfixed32[4] == -2009 && alltypes.rep_sfixed32[0] == 0);
+ TEST(alltypes.rep_float_count == 5 && alltypes.rep_float[4] == 2010.0f && alltypes.rep_float[0] == 0.0f);
+
+ TEST(alltypes.rep_fixed64_count == 5 && alltypes.rep_fixed64[4] == 2011 && alltypes.rep_fixed64[0] == 0);
+ TEST(alltypes.rep_sfixed64_count == 5 && alltypes.rep_sfixed64[4] == -2012 && alltypes.rep_sfixed64[0] == 0);
+ TEST(alltypes.rep_double_count == 5 && alltypes.rep_double[4] == 2013.0 && alltypes.rep_double[0] == 0.0);
+
+ TEST(alltypes.rep_string_count == 5 && strcmp(alltypes.rep_string[4], "2014") == 0 && alltypes.rep_string[0][0] == '\0');
+ TEST(alltypes.rep_bytes_count == 5 && alltypes.rep_bytes[4].size == 4 && alltypes.rep_bytes[0].size == 0);
+ TEST(memcmp(alltypes.rep_bytes[4].bytes, "2015", 4) == 0);
+
+ TEST(alltypes.rep_submsg_count == 5);
+ TEST(strcmp(alltypes.rep_submsg[4].substuff1, "2016") == 0 && alltypes.rep_submsg[0].substuff1[0] == '\0');
+ TEST(alltypes.rep_submsg[4].substuff2 == 2016 && alltypes.rep_submsg[0].substuff2 == 0);
+ TEST(alltypes.rep_submsg[4].substuff3 == 2016 && alltypes.rep_submsg[0].substuff3 == 0);
+
+ TEST(alltypes.rep_enum_count == 5 && alltypes.rep_enum[4] == MyEnum_Truth && alltypes.rep_enum[0] == MyEnum_Zero);
+ TEST(alltypes.rep_emptymsg_count == 5);
+
+ TEST(alltypes.rep_fbytes_count == 5);
+ TEST(alltypes.rep_fbytes[0][0] == 0 && alltypes.rep_fbytes[0][3] == 0);
+ TEST(memcmp(alltypes.rep_fbytes[4], "2019", 4) == 0);
+
+ if (mode == 0)
+ {
+ /* Expect default values */
+ TEST(alltypes.sng_int32 == 0);
+ TEST(alltypes.sng_int64 == 0);
+ TEST(alltypes.sng_uint32 == 0);
+ TEST(alltypes.sng_uint64 == 0);
+ TEST(alltypes.sng_sint32 == 0);
+ TEST(alltypes.sng_sint64 == 0);
+ TEST(alltypes.sng_bool == false);
+
+ TEST(alltypes.sng_fixed32 == 0);
+ TEST(alltypes.sng_sfixed32 == 0);
+ TEST(alltypes.sng_float == 0.0f);
+
+ TEST(alltypes.sng_fixed64 == 0);
+ TEST(alltypes.sng_sfixed64 == 0);
+ TEST(alltypes.sng_double == 0.0);
+
+ TEST(strcmp(alltypes.sng_string, "") == 0);
+ TEST(alltypes.sng_bytes.size == 0);
+ TEST(strcmp(alltypes.sng_submsg.substuff1, "") == 0);
+ TEST(alltypes.sng_submsg.substuff2 == 0);
+ TEST(alltypes.sng_submsg.substuff3 == 0);
+ TEST(alltypes.sng_enum == MyEnum_Zero);
+ TEST(alltypes.sng_fbytes[0] == 0 &&
+ alltypes.sng_fbytes[1] == 0 &&
+ alltypes.sng_fbytes[2] == 0 &&
+ alltypes.sng_fbytes[3] == 0);
+
+ TEST(alltypes.which_oneof == 0);
+ }
+ else
+ {
+ /* Expect filled-in values */
+ TEST(alltypes.sng_int32 == 3041);
+ TEST(alltypes.sng_int64 == 3042);
+ TEST(alltypes.sng_uint32 == 3043);
+ TEST(alltypes.sng_uint64 == 3044);
+ TEST(alltypes.sng_sint32 == 3045);
+ TEST(alltypes.sng_sint64 == 3046);
+ TEST(alltypes.sng_bool == true);
+
+ TEST(alltypes.sng_fixed32 == 3048);
+ TEST(alltypes.sng_sfixed32 == 3049);
+ TEST(alltypes.sng_float == 3050.0f);
+
+ TEST(alltypes.sng_fixed64 == 3051);
+ TEST(alltypes.sng_sfixed64 == 3052);
+ TEST(alltypes.sng_double == 3053.0);
+
+ TEST(strcmp(alltypes.sng_string, "3054") == 0);
+ TEST(alltypes.sng_bytes.size == 4);
+ TEST(memcmp(alltypes.sng_bytes.bytes, "3055", 4) == 0);
+ TEST(strcmp(alltypes.sng_submsg.substuff1, "3056") == 0);
+ TEST(alltypes.sng_submsg.substuff2 == 3056);
+ TEST(alltypes.sng_submsg.substuff3 == 0);
+ TEST(alltypes.sng_enum == MyEnum_Truth);
+ TEST(memcmp(alltypes.sng_fbytes, "3059", 4) == 0);
+
+ TEST(alltypes.which_oneof == AllTypes_oneof_msg1_tag);
+ TEST(strcmp(alltypes.oneof.oneof_msg1.substuff1, "4059") == 0);
+ TEST(alltypes.oneof.oneof_msg1.substuff2 == 4059);
+ }
+
+ TEST(alltypes.req_limits.int32_min == INT32_MIN);
+ TEST(alltypes.req_limits.int32_max == INT32_MAX);
+ TEST(alltypes.req_limits.uint32_min == 0);
+ TEST(alltypes.req_limits.uint32_max == UINT32_MAX);
+ TEST(alltypes.req_limits.int64_min == INT64_MIN);
+ TEST(alltypes.req_limits.int64_max == INT64_MAX);
+ TEST(alltypes.req_limits.uint64_min == 0);
+ TEST(alltypes.req_limits.uint64_max == UINT64_MAX);
+ TEST(alltypes.req_limits.enum_min == HugeEnum_Negative);
+ TEST(alltypes.req_limits.enum_max == HugeEnum_Positive);
+
+ TEST(alltypes.end == 1099);
+
+ return true;
+}
+
+int main(int argc, char **argv)
+{
+ uint8_t buffer[1024];
+ size_t count;
+ pb_istream_t stream;
+
+ /* Whether to expect the optional values or the default values. */
+ int mode = (argc > 1) ? atoi(argv[1]) : 0;
+
+ /* Read the data into buffer */
+ SET_BINARY_MODE(stdin);
+ count = fread(buffer, 1, sizeof(buffer), stdin);
+
+ /* Construct a pb_istream_t for reading from the buffer */
+ stream = pb_istream_from_buffer(buffer, count);
+
+ /* Decode and print out the stuff */
+ if (!check_alltypes(&stream, mode))
+ {
+ printf("Parsing failed: %s\n", PB_GET_ERROR(&stream));
+ return 1;
+ } else {
+ return 0;
+ }
+}
diff --git a/libs/nanopb/tests/alltypes_proto3/encode_alltypes.c b/libs/nanopb/tests/alltypes_proto3/encode_alltypes.c
new file mode 100644
index 00000000..1da06688
--- /dev/null
+++ b/libs/nanopb/tests/alltypes_proto3/encode_alltypes.c
@@ -0,0 +1,111 @@
+/* Attempts to test all the datatypes supported by ProtoBuf3.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pb_encode.h>
+#include "alltypes.pb.h"
+#include "test_helpers.h"
+
+int main(int argc, char **argv)
+{
+ int mode = (argc > 1) ? atoi(argv[1]) : 0;
+
+ /* Initialize the structure with constants */
+ AllTypes alltypes = AllTypes_init_zero;
+
+ 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].substuff3 = 2016;
+
+ alltypes.rep_enum_count = 5; alltypes.rep_enum[4] = MyEnum_Truth;
+ alltypes.rep_emptymsg_count = 5;
+
+ alltypes.rep_fbytes_count = 5;
+ memcpy(alltypes.rep_fbytes[4], "2019", 4);
+
+ alltypes.req_limits.int32_min = INT32_MIN;
+ alltypes.req_limits.int32_max = INT32_MAX;
+ alltypes.req_limits.uint32_min = 0;
+ alltypes.req_limits.uint32_max = UINT32_MAX;
+ alltypes.req_limits.int64_min = INT64_MIN;
+ alltypes.req_limits.int64_max = INT64_MAX;
+ alltypes.req_limits.uint64_min = 0;
+ alltypes.req_limits.uint64_max = UINT64_MAX;
+ alltypes.req_limits.enum_min = HugeEnum_Negative;
+ alltypes.req_limits.enum_max = HugeEnum_Positive;
+
+ if (mode != 0)
+ {
+ /* Fill in values for singular fields */
+ alltypes.sng_int32 = 3041;
+ alltypes.sng_int64 = 3042;
+ alltypes.sng_uint32 = 3043;
+ alltypes.sng_uint64 = 3044;
+ alltypes.sng_sint32 = 3045;
+ alltypes.sng_sint64 = 3046;
+ alltypes.sng_bool = true;
+
+ alltypes.sng_fixed32 = 3048;
+ alltypes.sng_sfixed32 = 3049;
+ alltypes.sng_float = 3050.0f;
+
+ alltypes.sng_fixed64 = 3051;
+ alltypes.sng_sfixed64 = 3052;
+ alltypes.sng_double = 3053.0;
+
+ strcpy(alltypes.sng_string, "3054");
+ alltypes.sng_bytes.size = 4;
+ memcpy(alltypes.sng_bytes.bytes, "3055", 4);
+ strcpy(alltypes.sng_submsg.substuff1, "3056");
+ alltypes.sng_submsg.substuff2 = 3056;
+ alltypes.sng_enum = MyEnum_Truth;
+ memcpy(alltypes.sng_fbytes, "3059", 4);
+
+ alltypes.which_oneof = AllTypes_oneof_msg1_tag;
+ strcpy(alltypes.oneof.oneof_msg1.substuff1, "4059");
+ alltypes.oneof.oneof_msg1.substuff2 = 4059;
+ }
+
+ alltypes.end = 1099;
+
+ {
+ uint8_t buffer[AllTypes_size];
+ 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: %s\n", PB_GET_ERROR(&stream));
+ return 1; /* Failure */
+ }
+ }
+}
diff --git a/libs/nanopb/tests/alltypes_proto3_callback/SConscript b/libs/nanopb/tests/alltypes_proto3_callback/SConscript
new file mode 100644
index 00000000..183a138a
--- /dev/null
+++ b/libs/nanopb/tests/alltypes_proto3_callback/SConscript
@@ -0,0 +1,23 @@
+# Test the AllTypes encoding & decoding using callbacks for all fields.
+
+Import("env", "malloc_env")
+
+c = Copy("$TARGET", "$SOURCE")
+env.Command("alltypes.proto", "#alltypes_proto3/alltypes.proto", c)
+
+env.NanopbProto(["alltypes", "alltypes.options"])
+enc = env.Program(["encode_alltypes_callback.c", "alltypes.pb.c", "$COMMON/pb_encode.o", "$COMMON/pb_common.o"])
+dec = env.Program(["decode_alltypes_callback.c", "alltypes.pb.c", "$COMMON/pb_decode.o", "$COMMON/pb_common.o"])
+
+refdec = "$BUILD/alltypes_proto3/decode_alltypes$PROGSUFFIX"
+
+# Encode and compare results
+env.RunTest(enc)
+env.RunTest("decode_alltypes.output", [refdec, "encode_alltypes_callback.output"])
+env.RunTest("decode_alltypes_callback.output", [dec, "encode_alltypes_callback.output"])
+
+# Do the same thing with the optional fields present
+env.RunTest("optionals.output", enc, ARGS = ['1'])
+env.RunTest("optionals.refdecout", [refdec, "optionals.output"], ARGS = ['1'])
+env.RunTest("optionals.decout", [dec, "optionals.output"], ARGS = ['1'])
+
diff --git a/libs/nanopb/tests/alltypes_proto3_callback/alltypes.options b/libs/nanopb/tests/alltypes_proto3_callback/alltypes.options
new file mode 100644
index 00000000..74d7a9c0
--- /dev/null
+++ b/libs/nanopb/tests/alltypes_proto3_callback/alltypes.options
@@ -0,0 +1,8 @@
+# Generate all fields as callbacks.
+AllTypes.* type:FT_CALLBACK
+SubMessage.substuff1 max_size:16
+AllTypes.oneof no_unions:true
+
+# With FT_CALLBACK, these options should get ignored
+*.*fbytes fixed_length:true max_size:4
+
diff --git a/libs/nanopb/tests/alltypes_proto3_callback/decode_alltypes_callback.c b/libs/nanopb/tests/alltypes_proto3_callback/decode_alltypes_callback.c
new file mode 100644
index 00000000..2b3c2f32
--- /dev/null
+++ b/libs/nanopb/tests/alltypes_proto3_callback/decode_alltypes_callback.c
@@ -0,0 +1,376 @@
+/* Attempts to test all the datatypes supported by ProtoBuf when used as callback fields.
+ * Note that normally there would be no reason to use callback fields for this,
+ * because each encoder defined here only gives a single field.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <pb_decode.h>
+#include "alltypes.pb.h"
+#include "test_helpers.h"
+
+#define TEST(x) if (!(x)) { \
+ printf("Test " #x " failed (in field %d).\n", field->tag); \
+ return false; \
+ }
+
+static bool read_varint(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ uint64_t value;
+ if (!pb_decode_varint(stream, &value))
+ return false;
+
+ TEST((int64_t)value == (long)*arg);
+ return true;
+}
+
+static bool read_svarint(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ int64_t value;
+ if (!pb_decode_svarint(stream, &value))
+ return false;
+
+ TEST(value == (long)*arg);
+ return true;
+}
+
+static bool read_fixed32(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ uint32_t value;
+ if (!pb_decode_fixed32(stream, &value))
+ return false;
+
+ TEST(value == *(uint32_t*)*arg);
+ return true;
+}
+
+static bool read_fixed64(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ uint64_t value;
+ if (!pb_decode_fixed64(stream, &value))
+ return false;
+
+ TEST(value == *(uint64_t*)*arg);
+ return true;
+}
+
+static bool read_string(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ uint8_t buf[16] = {0};
+ size_t len = stream->bytes_left;
+
+ if (len > sizeof(buf) - 1 || !pb_read(stream, buf, len))
+ return false;
+
+ TEST(strcmp((char*)buf, *arg) == 0);
+ return true;
+}
+
+static bool read_submsg(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ SubMessage submsg = {""};
+ SubMessage *ref = *arg;
+
+ if (!pb_decode(stream, SubMessage_fields, &submsg))
+ return false;
+
+ TEST(strcmp(submsg.substuff1, ref->substuff1) == 0);
+ TEST(submsg.substuff2 == ref->substuff2);
+ TEST(submsg.substuff3 == ref->substuff3);
+ return true;
+}
+
+static bool read_emptymsg(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ EmptyMessage emptymsg = {0};
+ return pb_decode(stream, EmptyMessage_fields, &emptymsg);
+}
+
+static bool read_repeated_varint(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ int32_t** expected = (int32_t**)arg;
+ uint64_t value;
+ if (!pb_decode_varint(stream, &value))
+ return false;
+
+ TEST(*(*expected)++ == value);
+ return true;
+}
+
+static bool read_repeated_svarint(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ int32_t** expected = (int32_t**)arg;
+ int64_t value;
+ if (!pb_decode_svarint(stream, &value))
+ return false;
+
+ TEST(*(*expected)++ == value);
+ return true;
+}
+
+static bool read_repeated_fixed32(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ uint32_t** expected = (uint32_t**)arg;
+ uint32_t value;
+ if (!pb_decode_fixed32(stream, &value))
+ return false;
+
+ TEST(*(*expected)++ == value);
+ return true;
+}
+
+static bool read_repeated_fixed64(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ uint64_t** expected = (uint64_t**)arg;
+ uint64_t value;
+ if (!pb_decode_fixed64(stream, &value))
+ return false;
+
+ TEST(*(*expected)++ == value);
+ return true;
+}
+
+static bool read_repeated_string(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ uint8_t*** expected = (uint8_t***)arg;
+ uint8_t buf[16] = {0};
+ size_t len = stream->bytes_left;
+
+ if (len > sizeof(buf) - 1 || !pb_read(stream, buf, len))
+ return false;
+
+ TEST(strcmp((char*)*(*expected)++, (char*)buf) == 0);
+ return true;
+}
+
+static bool read_repeated_submsg(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ SubMessage** expected = (SubMessage**)arg;
+ SubMessage submsg = {""};
+ if (!pb_decode(stream, SubMessage_fields, &submsg))
+ return false;
+
+ TEST(strcmp(submsg.substuff1, (*expected)->substuff1) == 0);
+ TEST(submsg.substuff2 == (*expected)->substuff2);
+ TEST(submsg.substuff3 == (*expected)->substuff3);
+ (*expected)++;
+
+ return true;
+}
+
+static bool read_limits(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ Limits decoded = {0};
+ if (!pb_decode(stream, Limits_fields, &decoded))
+ return false;
+
+ TEST(decoded.int32_min == INT32_MIN);
+ TEST(decoded.int32_max == INT32_MAX);
+ TEST(decoded.uint32_min == 0);
+ TEST(decoded.uint32_max == UINT32_MAX);
+ TEST(decoded.int64_min == INT64_MIN);
+ TEST(decoded.int64_max == INT64_MAX);
+ TEST(decoded.uint64_min == 0);
+ TEST(decoded.uint64_max == UINT64_MAX);
+ TEST(decoded.enum_min == HugeEnum_Negative);
+ TEST(decoded.enum_max == HugeEnum_Positive);
+
+ return true;
+}
+
+/* This function is called once from main(), it handles
+ the decoding and checks the fields. */
+bool check_alltypes(pb_istream_t *stream, int mode)
+{
+ /* Values for use from callbacks through pointers. */
+ bool status;
+
+ int32_t rep_int32[5] = {0, 0, 0, 0, -2001};
+ int32_t rep_int64[5] = {0, 0, 0, 0, -2002};
+ int32_t rep_uint32[5] = {0, 0, 0, 0, 2003};
+ int32_t rep_uint64[5] = {0, 0, 0, 0, 2004};
+ int32_t rep_sint32[5] = {0, 0, 0, 0, -2005};
+ int32_t rep_sint64[5] = {0, 0, 0, 0, -2006};
+ int32_t rep_bool[5] = {false, false, false, false, true};
+ uint32_t rep_fixed32[5] = {0, 0, 0, 0, 2008};
+ int32_t rep_sfixed32[5] = {0, 0, 0, 0, -2009};
+ float rep_float[5] = {0, 0, 0, 0, 2010.0f};
+ uint64_t rep_fixed64[5] = {0, 0, 0, 0, 2011};
+ int64_t rep_sfixed64[5] = {0, 0, 0, 0, -2012};
+ double rep_double[5] = {0, 0, 0, 0, 2013.0};
+ char* rep_string[5] = {"", "", "", "", "2014"};
+ char* rep_bytes[5] = {"", "", "", "", "2015"};
+ SubMessage rep_submsg[5] = {{"", 0, 0},
+ {"", 0, 0},
+ {"", 0, 0},
+ {"", 0, 0},
+ {"2016", 2016, 2016}};
+ int32_t rep_enum[5] = {0, 0, 0, 0, MyEnum_Truth};
+
+ uint32_t sng_fixed32 = 3048;
+ int32_t sng_sfixed32 = 3049;
+ float sng_float = 3050.0f;
+ uint64_t sng_fixed64 = 3051;
+ int64_t sng_sfixed64 = 3052;
+ double sng_double = 3053.0f;
+ SubMessage sng_submsg = {"3056", 3056};
+
+ SubMessage oneof_msg1 = {"4059", 4059};
+
+ AllTypes alltypes = AllTypes_init_zero;
+
+ /* Bind callbacks for repeated fields */
+ alltypes.rep_int32.funcs.decode = &read_repeated_varint;
+ alltypes.rep_int32.arg = rep_int32;
+
+ alltypes.rep_int64.funcs.decode = &read_repeated_varint;
+ alltypes.rep_int64.arg = rep_int64;
+
+ alltypes.rep_uint32.funcs.decode = &read_repeated_varint;
+ alltypes.rep_uint32.arg = rep_uint32;
+
+ alltypes.rep_uint64.funcs.decode = &read_repeated_varint;
+ alltypes.rep_uint64.arg = rep_uint64;
+
+ alltypes.rep_sint32.funcs.decode = &read_repeated_svarint;
+ alltypes.rep_sint32.arg = rep_sint32;
+
+ alltypes.rep_sint64.funcs.decode = &read_repeated_svarint;
+ alltypes.rep_sint64.arg = rep_sint64;
+
+ alltypes.rep_bool.funcs.decode = &read_repeated_varint;
+ alltypes.rep_bool.arg = rep_bool;
+
+ alltypes.rep_fixed32.funcs.decode = &read_repeated_fixed32;
+ alltypes.rep_fixed32.arg = rep_fixed32;
+
+ alltypes.rep_sfixed32.funcs.decode = &read_repeated_fixed32;
+ alltypes.rep_sfixed32.arg = rep_sfixed32;
+
+ alltypes.rep_float.funcs.decode = &read_repeated_fixed32;
+ alltypes.rep_float.arg = rep_float;
+
+ alltypes.rep_fixed64.funcs.decode = &read_repeated_fixed64;
+ alltypes.rep_fixed64.arg = rep_fixed64;
+
+ alltypes.rep_sfixed64.funcs.decode = &read_repeated_fixed64;
+ alltypes.rep_sfixed64.arg = rep_sfixed64;
+
+ alltypes.rep_double.funcs.decode = &read_repeated_fixed64;
+ alltypes.rep_double.arg = rep_double;
+
+ alltypes.rep_string.funcs.decode = &read_repeated_string;
+ alltypes.rep_string.arg = rep_string;
+
+ alltypes.rep_bytes.funcs.decode = &read_repeated_string;
+ alltypes.rep_bytes.arg = rep_bytes;
+
+ alltypes.rep_submsg.funcs.decode = &read_repeated_submsg;
+ alltypes.rep_submsg.arg = rep_submsg;
+
+ alltypes.rep_enum.funcs.decode = &read_repeated_varint;
+ alltypes.rep_enum.arg = rep_enum;
+
+ alltypes.rep_emptymsg.funcs.decode = &read_emptymsg;
+
+ alltypes.req_limits.funcs.decode = &read_limits;
+
+ alltypes.end.funcs.decode = &read_varint;
+ alltypes.end.arg = (void*)1099;
+
+ /* Bind callbacks for optional fields */
+ if (mode == 1)
+ {
+ alltypes.sng_int32.funcs.decode = &read_varint;
+ alltypes.sng_int32.arg = (void*)3041;
+
+ alltypes.sng_int64.funcs.decode = &read_varint;
+ alltypes.sng_int64.arg = (void*)3042;
+
+ alltypes.sng_uint32.funcs.decode = &read_varint;
+ alltypes.sng_uint32.arg = (void*)3043;
+
+ alltypes.sng_uint64.funcs.decode = &read_varint;
+ alltypes.sng_uint64.arg = (void*)3044;
+
+ alltypes.sng_sint32.funcs.decode = &read_svarint;
+ alltypes.sng_sint32.arg = (void*)3045;
+
+ alltypes.sng_sint64.funcs.decode = &read_svarint;
+ alltypes.sng_sint64.arg = (void*)3046;
+
+ alltypes.sng_bool.funcs.decode = &read_varint;
+ alltypes.sng_bool.arg = (void*)true;
+
+ alltypes.sng_fixed32.funcs.decode = &read_fixed32;
+ alltypes.sng_fixed32.arg = &sng_fixed32;
+
+ alltypes.sng_sfixed32.funcs.decode = &read_fixed32;
+ alltypes.sng_sfixed32.arg = &sng_sfixed32;
+
+ alltypes.sng_float.funcs.decode = &read_fixed32;
+ alltypes.sng_float.arg = &sng_float;
+
+ alltypes.sng_fixed64.funcs.decode = &read_fixed64;
+ alltypes.sng_fixed64.arg = &sng_fixed64;
+
+ alltypes.sng_sfixed64.funcs.decode = &read_fixed64;
+ alltypes.sng_sfixed64.arg = &sng_sfixed64;
+
+ alltypes.sng_double.funcs.decode = &read_fixed64;
+ alltypes.sng_double.arg = &sng_double;
+
+ alltypes.sng_string.funcs.decode = &read_string;
+ alltypes.sng_string.arg = "3054";
+
+ alltypes.sng_bytes.funcs.decode = &read_string;
+ alltypes.sng_bytes.arg = "3055";
+
+ alltypes.sng_submsg.funcs.decode = &read_submsg;
+ alltypes.sng_submsg.arg = &sng_submsg;
+
+ alltypes.sng_enum.funcs.decode = &read_varint;
+ alltypes.sng_enum.arg = (void*)MyEnum_Truth;
+
+ alltypes.sng_emptymsg.funcs.decode = &read_emptymsg;
+
+ alltypes.oneof_msg1.funcs.decode = &read_submsg;
+ alltypes.oneof_msg1.arg = &oneof_msg1;
+ }
+
+ status = pb_decode(stream, AllTypes_fields, &alltypes);
+
+#ifdef PB_ENABLE_MALLOC
+ /* Just to check for any interference between pb_release() and callback fields */
+ pb_release(AllTypes_fields, &alltypes);
+#endif
+
+ return status;
+}
+
+int main(int argc, char **argv)
+{
+ uint8_t buffer[1024];
+ size_t count;
+ pb_istream_t stream;
+
+ /* Whether to expect the optional values or the default values. */
+ int mode = (argc > 1) ? atoi(argv[1]) : 0;
+
+ /* Read the data into buffer */
+ SET_BINARY_MODE(stdin);
+ count = fread(buffer, 1, sizeof(buffer), stdin);
+
+ /* Construct a pb_istream_t for reading from the buffer */
+ stream = pb_istream_from_buffer(buffer, count);
+
+ /* Decode and print out the stuff */
+ if (!check_alltypes(&stream, mode))
+ {
+ printf("Parsing failed: %s\n", PB_GET_ERROR(&stream));
+ return 1;
+ } else {
+ return 0;
+ }
+}
diff --git a/libs/nanopb/tests/alltypes_proto3_callback/encode_alltypes_callback.c b/libs/nanopb/tests/alltypes_proto3_callback/encode_alltypes_callback.c
new file mode 100644
index 00000000..8c7bdd66
--- /dev/null
+++ b/libs/nanopb/tests/alltypes_proto3_callback/encode_alltypes_callback.c
@@ -0,0 +1,343 @@
+/* Attempts to test all the datatypes supported by ProtoBuf when used as callback fields.
+ * Note that normally there would be no reason to use callback fields for this,
+ * because each encoder defined here only gives a single field.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pb_encode.h>
+#include "alltypes.pb.h"
+#include "test_helpers.h"
+
+static bool write_varint(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ return pb_encode_tag_for_field(stream, field) &&
+ pb_encode_varint(stream, (long)*arg);
+}
+
+static bool write_svarint(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ return pb_encode_tag_for_field(stream, field) &&
+ pb_encode_svarint(stream, (long)*arg);
+}
+
+static bool write_fixed32(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ return pb_encode_tag_for_field(stream, field) &&
+ pb_encode_fixed32(stream, *arg);
+}
+
+static bool write_fixed64(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ return pb_encode_tag_for_field(stream, field) &&
+ pb_encode_fixed64(stream, *arg);
+}
+
+static bool write_string(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ return pb_encode_tag_for_field(stream, field) &&
+ pb_encode_string(stream, *arg, strlen(*arg));
+}
+
+static bool write_submsg(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+
+ return pb_encode_tag_for_field(stream, field) &&
+ pb_encode_submessage(stream, SubMessage_fields, *arg);
+}
+
+static bool write_emptymsg(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ EmptyMessage emptymsg = {0};
+ return pb_encode_tag_for_field(stream, field) &&
+ pb_encode_submessage(stream, EmptyMessage_fields, &emptymsg);
+}
+
+static bool write_repeated_varint(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ return pb_encode_tag_for_field(stream, field) &&
+ pb_encode_varint(stream, 0) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_varint(stream, 0) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_varint(stream, 0) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_varint(stream, 0) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_varint(stream, (long)*arg);
+}
+
+static bool write_repeated_svarint(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ return pb_encode_tag_for_field(stream, field) &&
+ pb_encode_svarint(stream, 0) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_svarint(stream, 0) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_svarint(stream, 0) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_svarint(stream, 0) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_svarint(stream, (long)*arg);
+}
+
+static bool write_repeated_fixed32(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ uint32_t dummy = 0;
+
+ /* Make it a packed field */
+ return pb_encode_tag(stream, PB_WT_STRING, field->tag) &&
+ pb_encode_varint(stream, 5 * 4) && /* Number of bytes */
+ pb_encode_fixed32(stream, &dummy) &&
+ pb_encode_fixed32(stream, &dummy) &&
+ pb_encode_fixed32(stream, &dummy) &&
+ pb_encode_fixed32(stream, &dummy) &&
+ pb_encode_fixed32(stream, *arg);
+}
+
+static bool write_repeated_fixed64(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ uint64_t dummy = 0;
+
+ /* Make it a packed field */
+ return pb_encode_tag(stream, PB_WT_STRING, field->tag) &&
+ pb_encode_varint(stream, 5 * 8) && /* Number of bytes */
+ pb_encode_fixed64(stream, &dummy) &&
+ pb_encode_fixed64(stream, &dummy) &&
+ pb_encode_fixed64(stream, &dummy) &&
+ pb_encode_fixed64(stream, &dummy) &&
+ pb_encode_fixed64(stream, *arg);
+}
+
+static bool write_repeated_string(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ return pb_encode_tag_for_field(stream, field) &&
+ pb_encode_string(stream, 0, 0) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_string(stream, 0, 0) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_string(stream, 0, 0) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_string(stream, 0, 0) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_string(stream, *arg, strlen(*arg));
+}
+
+static bool write_repeated_submsg(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ SubMessage dummy = {""};
+
+ return pb_encode_tag_for_field(stream, field) &&
+ pb_encode_submessage(stream, SubMessage_fields, &dummy) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_submessage(stream, SubMessage_fields, &dummy) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_submessage(stream, SubMessage_fields, &dummy) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_submessage(stream, SubMessage_fields, &dummy) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_submessage(stream, SubMessage_fields, *arg);
+}
+
+static bool write_limits(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ Limits limits = {0};
+ limits.int32_min = INT32_MIN;
+ limits.int32_max = INT32_MAX;
+ limits.uint32_min = 0;
+ limits.uint32_max = UINT32_MAX;
+ limits.int64_min = INT64_MIN;
+ limits.int64_max = INT64_MAX;
+ limits.uint64_min = 0;
+ limits.uint64_max = UINT64_MAX;
+ limits.enum_min = HugeEnum_Negative;
+ limits.enum_max = HugeEnum_Positive;
+
+ return pb_encode_tag_for_field(stream, field) &&
+ pb_encode_submessage(stream, Limits_fields, &limits);
+}
+
+static bool write_repeated_emptymsg(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ EmptyMessage emptymsg = {0};
+ return pb_encode_tag_for_field(stream, field) &&
+ pb_encode_submessage(stream, EmptyMessage_fields, &emptymsg) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_submessage(stream, EmptyMessage_fields, &emptymsg) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_submessage(stream, EmptyMessage_fields, &emptymsg) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_submessage(stream, EmptyMessage_fields, &emptymsg) &&
+ pb_encode_tag_for_field(stream, field) &&
+ pb_encode_submessage(stream, EmptyMessage_fields, &emptymsg);
+}
+
+int main(int argc, char **argv)
+{
+ int mode = (argc > 1) ? atoi(argv[1]) : 0;
+
+ /* Values for use from callbacks through pointers. */
+ uint32_t rep_fixed32 = 2008;
+ int32_t rep_sfixed32 = -2009;
+ float rep_float = 2010.0f;
+ uint64_t rep_fixed64 = 2011;
+ int64_t rep_sfixed64 = -2012;
+ double rep_double = 2013.0;
+ SubMessage rep_submsg = {"2016", 2016, 2016};
+
+ uint32_t sng_fixed32 = 3048;
+ int32_t sng_sfixed32 = 3049;
+ float sng_float = 3050.0f;
+ uint64_t sng_fixed64 = 3051;
+ int64_t sng_sfixed64 = 3052;
+ double sng_double = 3053.0f;
+ SubMessage sng_submsg = {"3056", 3056};
+
+ SubMessage oneof_msg1 = {"4059", 4059};
+
+ AllTypes alltypes = AllTypes_init_zero;
+
+ /* Bind callbacks for repeated fields */
+ alltypes.rep_int32.funcs.encode = &write_repeated_varint;
+ alltypes.rep_int32.arg = (void*)-2001;
+
+ alltypes.rep_int64.funcs.encode = &write_repeated_varint;
+ alltypes.rep_int64.arg = (void*)-2002;
+
+ alltypes.rep_uint32.funcs.encode = &write_repeated_varint;
+ alltypes.rep_uint32.arg = (void*)2003;
+
+ alltypes.rep_uint64.funcs.encode = &write_repeated_varint;
+ alltypes.rep_uint64.arg = (void*)2004;
+
+ alltypes.rep_sint32.funcs.encode = &write_repeated_svarint;
+ alltypes.rep_sint32.arg = (void*)-2005;
+
+ alltypes.rep_sint64.funcs.encode = &write_repeated_svarint;
+ alltypes.rep_sint64.arg = (void*)-2006;
+
+ alltypes.rep_bool.funcs.encode = &write_repeated_varint;
+ alltypes.rep_bool.arg = (void*)true;
+
+ alltypes.rep_fixed32.funcs.encode = &write_repeated_fixed32;
+ alltypes.rep_fixed32.arg = &rep_fixed32;
+
+ alltypes.rep_sfixed32.funcs.encode = &write_repeated_fixed32;
+ alltypes.rep_sfixed32.arg = &rep_sfixed32;
+
+ alltypes.rep_float.funcs.encode = &write_repeated_fixed32;
+ alltypes.rep_float.arg = &rep_float;
+
+ alltypes.rep_fixed64.funcs.encode = &write_repeated_fixed64;
+ alltypes.rep_fixed64.arg = &rep_fixed64;
+
+ alltypes.rep_sfixed64.funcs.encode = &write_repeated_fixed64;
+ alltypes.rep_sfixed64.arg = &rep_sfixed64;
+
+ alltypes.rep_double.funcs.encode = &write_repeated_fixed64;
+ alltypes.rep_double.arg = &rep_double;
+
+ alltypes.rep_string.funcs.encode = &write_repeated_string;
+ alltypes.rep_string.arg = "2014";
+
+ alltypes.rep_bytes.funcs.encode = &write_repeated_string;
+ alltypes.rep_bytes.arg = "2015";
+
+ alltypes.rep_submsg.funcs.encode = &write_repeated_submsg;
+ alltypes.rep_submsg.arg = &rep_submsg;
+
+ alltypes.rep_enum.funcs.encode = &write_repeated_varint;
+ alltypes.rep_enum.arg = (void*)MyEnum_Truth;
+
+ alltypes.rep_emptymsg.funcs.encode = &write_repeated_emptymsg;
+
+ alltypes.rep_fbytes.funcs.encode = &write_repeated_string;
+ alltypes.rep_fbytes.arg = "2019";
+
+ alltypes.req_limits.funcs.encode = &write_limits;
+
+ /* Bind callbacks for singular fields */
+ if (mode != 0)
+ {
+ alltypes.sng_int32.funcs.encode = &write_varint;
+ alltypes.sng_int32.arg = (void*)3041;
+
+ alltypes.sng_int64.funcs.encode = &write_varint;
+ alltypes.sng_int64.arg = (void*)3042;
+
+ alltypes.sng_uint32.funcs.encode = &write_varint;
+ alltypes.sng_uint32.arg = (void*)3043;
+
+ alltypes.sng_uint64.funcs.encode = &write_varint;
+ alltypes.sng_uint64.arg = (void*)3044;
+
+ alltypes.sng_sint32.funcs.encode = &write_svarint;
+ alltypes.sng_sint32.arg = (void*)3045;
+
+ alltypes.sng_sint64.funcs.encode = &write_svarint;
+ alltypes.sng_sint64.arg = (void*)3046;
+
+ alltypes.sng_bool.funcs.encode = &write_varint;
+ alltypes.sng_bool.arg = (void*)true;
+
+ alltypes.sng_fixed32.funcs.encode = &write_fixed32;
+ alltypes.sng_fixed32.arg = &sng_fixed32;
+
+ alltypes.sng_sfixed32.funcs.encode = &write_fixed32;
+ alltypes.sng_sfixed32.arg = &sng_sfixed32;
+
+ alltypes.sng_float.funcs.encode = &write_fixed32;
+ alltypes.sng_float.arg = &sng_float;
+
+ alltypes.sng_fixed64.funcs.encode = &write_fixed64;
+ alltypes.sng_fixed64.arg = &sng_fixed64;
+
+ alltypes.sng_sfixed64.funcs.encode = &write_fixed64;
+ alltypes.sng_sfixed64.arg = &sng_sfixed64;
+
+ alltypes.sng_double.funcs.encode = &write_fixed64;
+ alltypes.sng_double.arg = &sng_double;
+
+ alltypes.sng_string.funcs.encode = &write_string;
+ alltypes.sng_string.arg = "3054";
+
+ alltypes.sng_bytes.funcs.encode = &write_string;
+ alltypes.sng_bytes.arg = "3055";
+
+ alltypes.sng_submsg.funcs.encode = &write_submsg;
+ alltypes.sng_submsg.arg = &sng_submsg;
+
+ alltypes.sng_enum.funcs.encode = &write_varint;
+ alltypes.sng_enum.arg = (void*)MyEnum_Truth;
+
+ alltypes.sng_emptymsg.funcs.encode = &write_emptymsg;
+
+ alltypes.sng_fbytes.funcs.encode = &write_string;
+ alltypes.sng_fbytes.arg = "3059";
+
+ alltypes.oneof_msg1.funcs.encode = &write_submsg;
+ alltypes.oneof_msg1.arg = &oneof_msg1;
+ }
+
+ alltypes.end.funcs.encode = &write_varint;
+ alltypes.end.arg = (void*)1099;
+
+ {
+ uint8_t buffer[2048];
+ 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: %s\n", PB_GET_ERROR(&stream));
+ return 1; /* Failure */
+ }
+ }
+}
diff --git a/libs/nanopb/tests/anonymous_oneof/SConscript b/libs/nanopb/tests/anonymous_oneof/SConscript
new file mode 100644
index 00000000..10672287
--- /dev/null
+++ b/libs/nanopb/tests/anonymous_oneof/SConscript
@@ -0,0 +1,30 @@
+# Test anonymous_oneof generator option
+
+Import('env')
+
+import re
+
+match = None
+if 'PROTOC_VERSION' in env:
+ match = re.search('([0-9]+).([0-9]+).([0-9]+)', env['PROTOC_VERSION'])
+
+if match:
+ version = map(int, match.groups())
+
+# Oneof is supported by protoc >= 2.6.0
+if env.GetOption('clean') or (match and (version[0] > 2 or (version[0] == 2 and version[1] >= 6))):
+ # Anonymous oneofs are supported by clang and gcc
+ if 'clang' in env['CC'] or 'gcc' in env['CC']:
+ env2 = env.Clone()
+ if '-pedantic' in env2['CFLAGS']:
+ env2['CFLAGS'].remove('-pedantic')
+ env2.NanopbProto('oneof')
+
+ dec = env2.Program(['decode_oneof.c',
+ 'oneof.pb.c',
+ '$COMMON/pb_decode.o',
+ '$COMMON/pb_common.o'])
+
+ env2.RunTest("message1.txt", [dec, '$BUILD/oneof/message1.pb'], ARGS = ['1'])
+ env2.RunTest("message2.txt", [dec, '$BUILD/oneof/message2.pb'], ARGS = ['2'])
+ env2.RunTest("message3.txt", [dec, '$BUILD/oneof/message3.pb'], ARGS = ['3'])
diff --git a/libs/nanopb/tests/anonymous_oneof/decode_oneof.c b/libs/nanopb/tests/anonymous_oneof/decode_oneof.c
new file mode 100644
index 00000000..0f774dbc
--- /dev/null
+++ b/libs/nanopb/tests/anonymous_oneof/decode_oneof.c
@@ -0,0 +1,88 @@
+/* Decode a message using oneof fields */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pb_decode.h>
+#include "oneof.pb.h"
+#include "test_helpers.h"
+#include "unittests.h"
+
+/* Test the 'AnonymousOneOfMessage' */
+int test_oneof_1(pb_istream_t *stream, int option)
+{
+ AnonymousOneOfMessage msg;
+ int status = 0;
+
+ /* To better catch initialization errors */
+ memset(&msg, 0xAA, sizeof(msg));
+
+ if (!pb_decode(stream, AnonymousOneOfMessage_fields, &msg))
+ {
+ printf("Decoding failed: %s\n", PB_GET_ERROR(stream));
+ return 1;
+ }
+
+ /* Check that the basic fields work normally */
+ TEST(msg.prefix == 123);
+ TEST(msg.suffix == 321);
+
+ /* Check that we got the right oneof according to command line */
+ if (option == 1)
+ {
+ TEST(msg.which_values == AnonymousOneOfMessage_first_tag);
+ TEST(msg.first == 999);
+ }
+ else if (option == 2)
+ {
+ TEST(msg.which_values == AnonymousOneOfMessage_second_tag);
+ TEST(strcmp(msg.second, "abcd") == 0);
+ }
+ else if (option == 3)
+ {
+ TEST(msg.which_values == AnonymousOneOfMessage_third_tag);
+ TEST(msg.third.array[0] == 1);
+ TEST(msg.third.array[1] == 2);
+ TEST(msg.third.array[2] == 3);
+ TEST(msg.third.array[3] == 4);
+ TEST(msg.third.array[4] == 5);
+ }
+
+ return status;
+}
+
+int main(int argc, char **argv)
+{
+ uint8_t buffer[AnonymousOneOfMessage_size];
+ size_t count;
+ int option;
+
+ if (argc != 2)
+ {
+ fprintf(stderr, "Usage: decode_oneof [number]\n");
+ return 1;
+ }
+ option = atoi(argv[1]);
+
+ SET_BINARY_MODE(stdin);
+ count = fread(buffer, 1, sizeof(buffer), stdin);
+
+ if (!feof(stdin))
+ {
+ printf("Message does not fit in buffer\n");
+ return 1;
+ }
+
+ {
+ int status = 0;
+ pb_istream_t stream;
+
+ stream = pb_istream_from_buffer(buffer, count);
+ status = test_oneof_1(&stream, option);
+
+ if (status != 0)
+ return status;
+ }
+
+ return 0;
+}
diff --git a/libs/nanopb/tests/anonymous_oneof/oneof.proto b/libs/nanopb/tests/anonymous_oneof/oneof.proto
new file mode 100644
index 00000000..d56285c0
--- /dev/null
+++ b/libs/nanopb/tests/anonymous_oneof/oneof.proto
@@ -0,0 +1,23 @@
+syntax = "proto2";
+
+import 'nanopb.proto';
+
+message SubMessage
+{
+ repeated int32 array = 1 [(nanopb).max_count = 8];
+}
+
+/* Oneof in a message with other fields */
+message AnonymousOneOfMessage
+{
+ option (nanopb_msgopt).anonymous_oneof = true;
+ required int32 prefix = 1;
+ oneof values
+ {
+ int32 first = 5;
+ string second = 6 [(nanopb).max_size = 8];
+ SubMessage third = 7;
+ }
+ required int32 suffix = 99;
+}
+
diff --git a/libs/nanopb/tests/backwards_compatibility/SConscript b/libs/nanopb/tests/backwards_compatibility/SConscript
new file mode 100644
index 00000000..81b03182
--- /dev/null
+++ b/libs/nanopb/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", "$COMMON/pb_common.o"])
+dec = env.Program(["decode_legacy.c", "alltypes_legacy.c", "$COMMON/pb_decode.o", "$COMMON/pb_common.o"])
+
+env.RunTest(enc)
+env.RunTest([dec, "encode_legacy.output"])
+
diff --git a/libs/nanopb/tests/backwards_compatibility/alltypes_legacy.c b/libs/nanopb/tests/backwards_compatibility/alltypes_legacy.c
new file mode 100644
index 00000000..7311fd45
--- /dev/null
+++ b/libs/nanopb/tests/backwards_compatibility/alltypes_legacy.c
@@ -0,0 +1,153 @@
+/* Automatically generated nanopb constant definitions */
+/* Generated by nanopb-0.3.0-dev at Tue Aug 19 17:53:24 2014. */
+
+#include "alltypes_legacy.h"
+
+#if PB_PROTO_HEADER_VERSION != 30
+#error Regenerate this file with the current version of nanopb generator.
+#endif
+
+const char SubMessage_substuff1_default[16] = "1";
+const int32_t SubMessage_substuff2_default = 2;
+const uint32_t SubMessage_substuff3_default = 3u;
+const int32_t Limits_int32_min_default = 2147483647;
+const int32_t Limits_int32_max_default = -2147483647;
+const uint32_t Limits_uint32_min_default = 4294967295u;
+const uint32_t Limits_uint32_max_default = 0u;
+const int64_t Limits_int64_min_default = 9223372036854775807ll;
+const int64_t Limits_int64_max_default = -9223372036854775807ll;
+const uint64_t Limits_uint64_min_default = 18446744073709551615ull;
+const uint64_t Limits_uint64_max_default = 0ull;
+const HugeEnum Limits_enum_min_default = HugeEnum_Positive;
+const HugeEnum Limits_enum_max_default = HugeEnum_Negative;
+const int32_t AllTypes_opt_int32_default = 4041;
+const int64_t AllTypes_opt_int64_default = 4042ll;
+const uint32_t AllTypes_opt_uint32_default = 4043u;
+const uint64_t AllTypes_opt_uint64_default = 4044ull;
+const int32_t AllTypes_opt_sint32_default = 4045;
+const int64_t AllTypes_opt_sint64_default = 4046;
+const bool AllTypes_opt_bool_default = false;
+const uint32_t AllTypes_opt_fixed32_default = 4048u;
+const int32_t AllTypes_opt_sfixed32_default = 4049;
+const float AllTypes_opt_float_default = 4050;
+const uint64_t AllTypes_opt_fixed64_default = 4051ull;
+const int64_t AllTypes_opt_sfixed64_default = 4052ll;
+const double AllTypes_opt_double_default = 4053;
+const char AllTypes_opt_string_default[16] = "4054";
+const AllTypes_opt_bytes_t AllTypes_opt_bytes_default = {4, {0x34,0x30,0x35,0x35}};
+const MyEnum AllTypes_opt_enum_default = MyEnum_Second;
+
+
+const pb_field_t SubMessage_fields[4] = {
+ PB_FIELD( 1, STRING , REQUIRED, STATIC , FIRST, SubMessage, substuff1, substuff1, &SubMessage_substuff1_default),
+ PB_FIELD( 2, INT32 , REQUIRED, STATIC , OTHER, SubMessage, substuff2, substuff1, &SubMessage_substuff2_default),
+ PB_FIELD( 3, FIXED32 , OPTIONAL, STATIC , OTHER, SubMessage, substuff3, substuff2, &SubMessage_substuff3_default),
+ PB_LAST_FIELD
+};
+
+const pb_field_t EmptyMessage_fields[1] = {
+ PB_LAST_FIELD
+};
+
+const pb_field_t Limits_fields[11] = {
+ PB_FIELD( 1, INT32 , REQUIRED, STATIC , FIRST, Limits, int32_min, int32_min, &Limits_int32_min_default),
+ PB_FIELD( 2, INT32 , REQUIRED, STATIC , OTHER, Limits, int32_max, int32_min, &Limits_int32_max_default),
+ PB_FIELD( 3, UINT32 , REQUIRED, STATIC , OTHER, Limits, uint32_min, int32_max, &Limits_uint32_min_default),
+ PB_FIELD( 4, UINT32 , REQUIRED, STATIC , OTHER, Limits, uint32_max, uint32_min, &Limits_uint32_max_default),
+ PB_FIELD( 5, INT64 , REQUIRED, STATIC , OTHER, Limits, int64_min, uint32_max, &Limits_int64_min_default),
+ PB_FIELD( 6, INT64 , REQUIRED, STATIC , OTHER, Limits, int64_max, int64_min, &Limits_int64_max_default),
+ PB_FIELD( 7, UINT64 , REQUIRED, STATIC , OTHER, Limits, uint64_min, int64_max, &Limits_uint64_min_default),
+ PB_FIELD( 8, UINT64 , REQUIRED, STATIC , OTHER, Limits, uint64_max, uint64_min, &Limits_uint64_max_default),
+ PB_FIELD( 9, ENUM , REQUIRED, STATIC , OTHER, Limits, enum_min, uint64_max, &Limits_enum_min_default),
+ PB_FIELD( 10, ENUM , REQUIRED, STATIC , OTHER, Limits, enum_max, enum_min, &Limits_enum_max_default),
+ PB_LAST_FIELD
+};
+
+const pb_field_t AllTypes_fields[54] = {
+ PB_FIELD( 1, INT32 , REQUIRED, STATIC , FIRST, AllTypes, req_int32, req_int32, 0),
+ PB_FIELD( 2, INT64 , REQUIRED, STATIC , OTHER, AllTypes, req_int64, req_int32, 0),
+ PB_FIELD( 3, UINT32 , REQUIRED, STATIC , OTHER, AllTypes, req_uint32, req_int64, 0),
+ PB_FIELD( 4, UINT64 , REQUIRED, STATIC , OTHER, AllTypes, req_uint64, req_uint32, 0),
+ PB_FIELD( 5, SINT32 , REQUIRED, STATIC , OTHER, AllTypes, req_sint32, req_uint64, 0),
+ PB_FIELD( 6, SINT64 , REQUIRED, STATIC , OTHER, AllTypes, req_sint64, req_sint32, 0),
+ PB_FIELD( 7, BOOL , REQUIRED, STATIC , OTHER, AllTypes, req_bool, req_sint64, 0),
+ PB_FIELD( 8, FIXED32 , REQUIRED, STATIC , OTHER, AllTypes, req_fixed32, req_bool, 0),
+ PB_FIELD( 9, SFIXED32, REQUIRED, STATIC , OTHER, AllTypes, req_sfixed32, req_fixed32, 0),
+ PB_FIELD( 10, FLOAT , REQUIRED, STATIC , OTHER, AllTypes, req_float, req_sfixed32, 0),
+ PB_FIELD( 11, FIXED64 , REQUIRED, STATIC , OTHER, AllTypes, req_fixed64, req_float, 0),
+ PB_FIELD( 12, SFIXED64, REQUIRED, STATIC , OTHER, AllTypes, req_sfixed64, req_fixed64, 0),
+ PB_FIELD( 13, DOUBLE , REQUIRED, STATIC , OTHER, AllTypes, req_double, req_sfixed64, 0),
+ PB_FIELD( 14, STRING , REQUIRED, STATIC , OTHER, AllTypes, req_string, req_double, 0),
+ PB_FIELD( 15, BYTES , REQUIRED, STATIC , OTHER, AllTypes, req_bytes, req_string, 0),
+ PB_FIELD( 16, MESSAGE , REQUIRED, STATIC , OTHER, AllTypes, req_submsg, req_bytes, &SubMessage_fields),
+ PB_FIELD( 17, ENUM , REQUIRED, STATIC , OTHER, AllTypes, req_enum, req_submsg, 0),
+ PB_FIELD( 21, INT32 , REPEATED, STATIC , OTHER, AllTypes, rep_int32, req_enum, 0),
+ PB_FIELD( 22, INT64 , REPEATED, STATIC , OTHER, AllTypes, rep_int64, rep_int32, 0),
+ PB_FIELD( 23, UINT32 , REPEATED, STATIC , OTHER, AllTypes, rep_uint32, rep_int64, 0),
+ PB_FIELD( 24, UINT64 , REPEATED, STATIC , OTHER, AllTypes, rep_uint64, rep_uint32, 0),
+ PB_FIELD( 25, SINT32 , REPEATED, STATIC , OTHER, AllTypes, rep_sint32, rep_uint64, 0),
+ PB_FIELD( 26, SINT64 , REPEATED, STATIC , OTHER, AllTypes, rep_sint64, rep_sint32, 0),
+ PB_FIELD( 27, BOOL , REPEATED, STATIC , OTHER, AllTypes, rep_bool, rep_sint64, 0),
+ PB_FIELD( 28, FIXED32 , REPEATED, STATIC , OTHER, AllTypes, rep_fixed32, rep_bool, 0),
+ PB_FIELD( 29, SFIXED32, REPEATED, STATIC , OTHER, AllTypes, rep_sfixed32, rep_fixed32, 0),
+ PB_FIELD( 30, FLOAT , REPEATED, STATIC , OTHER, AllTypes, rep_float, rep_sfixed32, 0),
+ PB_FIELD( 31, FIXED64 , REPEATED, STATIC , OTHER, AllTypes, rep_fixed64, rep_float, 0),
+ PB_FIELD( 32, SFIXED64, REPEATED, STATIC , OTHER, AllTypes, rep_sfixed64, rep_fixed64, 0),
+ PB_FIELD( 33, DOUBLE , REPEATED, STATIC , OTHER, AllTypes, rep_double, rep_sfixed64, 0),
+ PB_FIELD( 34, STRING , REPEATED, STATIC , OTHER, AllTypes, rep_string, rep_double, 0),
+ PB_FIELD( 35, BYTES , REPEATED, STATIC , OTHER, AllTypes, rep_bytes, rep_string, 0),
+ PB_FIELD( 36, MESSAGE , REPEATED, STATIC , OTHER, AllTypes, rep_submsg, rep_bytes, &SubMessage_fields),
+ PB_FIELD( 37, ENUM , REPEATED, STATIC , OTHER, AllTypes, rep_enum, rep_submsg, 0),
+ PB_FIELD( 41, INT32 , OPTIONAL, STATIC , OTHER, AllTypes, opt_int32, rep_enum, &AllTypes_opt_int32_default),
+ PB_FIELD( 42, INT64 , OPTIONAL, STATIC , OTHER, AllTypes, opt_int64, opt_int32, &AllTypes_opt_int64_default),
+ PB_FIELD( 43, UINT32 , OPTIONAL, STATIC , OTHER, AllTypes, opt_uint32, opt_int64, &AllTypes_opt_uint32_default),
+ PB_FIELD( 44, UINT64 , OPTIONAL, STATIC , OTHER, AllTypes, opt_uint64, opt_uint32, &AllTypes_opt_uint64_default),
+ PB_FIELD( 45, SINT32 , OPTIONAL, STATIC , OTHER, AllTypes, opt_sint32, opt_uint64, &AllTypes_opt_sint32_default),
+ PB_FIELD( 46, SINT64 , OPTIONAL, STATIC , OTHER, AllTypes, opt_sint64, opt_sint32, &AllTypes_opt_sint64_default),
+ PB_FIELD( 47, BOOL , OPTIONAL, STATIC , OTHER, AllTypes, opt_bool, opt_sint64, &AllTypes_opt_bool_default),
+ PB_FIELD( 48, FIXED32 , OPTIONAL, STATIC , OTHER, AllTypes, opt_fixed32, opt_bool, &AllTypes_opt_fixed32_default),
+ PB_FIELD( 49, SFIXED32, OPTIONAL, STATIC , OTHER, AllTypes, opt_sfixed32, opt_fixed32, &AllTypes_opt_sfixed32_default),
+ PB_FIELD( 50, FLOAT , OPTIONAL, STATIC , OTHER, AllTypes, opt_float, opt_sfixed32, &AllTypes_opt_float_default),
+ PB_FIELD( 51, FIXED64 , OPTIONAL, STATIC , OTHER, AllTypes, opt_fixed64, opt_float, &AllTypes_opt_fixed64_default),
+ PB_FIELD( 52, SFIXED64, OPTIONAL, STATIC , OTHER, AllTypes, opt_sfixed64, opt_fixed64, &AllTypes_opt_sfixed64_default),
+ PB_FIELD( 53, DOUBLE , OPTIONAL, STATIC , OTHER, AllTypes, opt_double, opt_sfixed64, &AllTypes_opt_double_default),
+ PB_FIELD( 54, STRING , OPTIONAL, STATIC , OTHER, AllTypes, opt_string, opt_double, &AllTypes_opt_string_default),
+ PB_FIELD( 55, BYTES , OPTIONAL, STATIC , OTHER, AllTypes, opt_bytes, opt_string, &AllTypes_opt_bytes_default),
+ PB_FIELD( 56, MESSAGE , OPTIONAL, STATIC , OTHER, AllTypes, opt_submsg, opt_bytes, &SubMessage_fields),
+ PB_FIELD( 57, ENUM , OPTIONAL, STATIC , OTHER, AllTypes, opt_enum, opt_submsg, &AllTypes_opt_enum_default),
+ PB_FIELD( 99, INT32 , REQUIRED, STATIC , OTHER, AllTypes, end, opt_enum, 0),
+ PB_FIELD(200, EXTENSION, OPTIONAL, CALLBACK, OTHER, AllTypes, extensions, end, 0),
+ PB_LAST_FIELD
+};
+
+
+/* Check that field information fits in pb_field_t */
+#if !defined(PB_FIELD_32BIT)
+/* If you get an error here, it means that you need to define PB_FIELD_32BIT
+ * compile-time option. You can do that in pb.h or on compiler command line.
+ *
+ * The reason you need to do this is that some of your messages contain tag
+ * numbers or field sizes that are larger than what can fit in 8 or 16 bit
+ * field descriptors.
+ */
+PB_STATIC_ASSERT((pb_membersize(AllTypes, req_submsg) < 65536 && pb_membersize(AllTypes, rep_submsg[0]) < 65536 && pb_membersize(AllTypes, opt_submsg) < 65536), YOU_MUST_DEFINE_PB_FIELD_32BIT_FOR_MESSAGES_SubMessage_EmptyMessage_Limits_AllTypes)
+#endif
+
+#if !defined(PB_FIELD_16BIT) && !defined(PB_FIELD_32BIT)
+/* If you get an error here, it means that you need to define PB_FIELD_16BIT
+ * compile-time option. You can do that in pb.h or on compiler command line.
+ *
+ * The reason you need to do this is that some of your messages contain tag
+ * numbers or field sizes that are larger than what can fit in the default
+ * 8 bit descriptors.
+ */
+PB_STATIC_ASSERT((pb_membersize(AllTypes, req_submsg) < 256 && pb_membersize(AllTypes, rep_submsg[0]) < 256 && pb_membersize(AllTypes, opt_submsg) < 256), YOU_MUST_DEFINE_PB_FIELD_16BIT_FOR_MESSAGES_SubMessage_EmptyMessage_Limits_AllTypes)
+#endif
+
+
+/* On some platforms (such as AVR), double is really float.
+ * These are not directly supported by nanopb, but see example_avr_double.
+ * To get rid of this error, remove any double fields from your .proto.
+ */
+PB_STATIC_ASSERT(sizeof(double) == 8, DOUBLE_MUST_BE_8_BYTES)
+
diff --git a/libs/nanopb/tests/backwards_compatibility/alltypes_legacy.h b/libs/nanopb/tests/backwards_compatibility/alltypes_legacy.h
new file mode 100644
index 00000000..4e0a63be
--- /dev/null
+++ b/libs/nanopb/tests/backwards_compatibility/alltypes_legacy.h
@@ -0,0 +1,274 @@
+/* Automatically generated nanopb header */
+/* Generated by nanopb-0.3.0-dev at Tue Aug 19 17:53:24 2014. */
+
+#ifndef PB_ALLTYPES_LEGACY_H_INCLUDED
+#define PB_ALLTYPES_LEGACY_H_INCLUDED
+#include <pb.h>
+
+#if PB_PROTO_HEADER_VERSION != 30
+#error Regenerate this file with the current version of nanopb generator.
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Enum definitions */
+typedef enum _HugeEnum {
+ HugeEnum_Negative = -2147483647,
+ HugeEnum_Positive = 2147483647
+} HugeEnum;
+
+typedef enum _MyEnum {
+ MyEnum_Zero = 0,
+ MyEnum_First = 1,
+ MyEnum_Second = 2,
+ MyEnum_Truth = 42
+} MyEnum;
+
+/* Struct definitions */
+typedef struct _EmptyMessage {
+ uint8_t dummy_field;
+} EmptyMessage;
+
+typedef struct _Limits {
+ int32_t int32_min;
+ int32_t int32_max;
+ uint32_t uint32_min;
+ uint32_t uint32_max;
+ int64_t int64_min;
+ int64_t int64_max;
+ uint64_t uint64_min;
+ uint64_t uint64_max;
+ HugeEnum enum_min;
+ HugeEnum enum_max;
+} Limits;
+
+typedef struct _SubMessage {
+ char substuff1[16];
+ int32_t substuff2;
+ bool has_substuff3;
+ uint32_t substuff3;
+} SubMessage;
+
+typedef PB_BYTES_ARRAY_T(16) AllTypes_req_bytes_t;
+
+typedef PB_BYTES_ARRAY_T(16) AllTypes_rep_bytes_t;
+
+typedef PB_BYTES_ARRAY_T(16) AllTypes_opt_bytes_t;
+
+typedef struct _AllTypes {
+ int32_t req_int32;
+ int64_t req_int64;
+ uint32_t req_uint32;
+ uint64_t req_uint64;
+ int32_t req_sint32;
+ int64_t req_sint64;
+ bool req_bool;
+ uint32_t req_fixed32;
+ int32_t req_sfixed32;
+ float req_float;
+ uint64_t req_fixed64;
+ int64_t req_sfixed64;
+ double req_double;
+ char req_string[16];
+ AllTypes_req_bytes_t req_bytes;
+ SubMessage req_submsg;
+ MyEnum req_enum;
+ pb_size_t rep_int32_count;
+ int32_t rep_int32[5];
+ pb_size_t rep_int64_count;
+ int64_t rep_int64[5];
+ pb_size_t rep_uint32_count;
+ uint32_t rep_uint32[5];
+ pb_size_t rep_uint64_count;
+ uint64_t rep_uint64[5];
+ pb_size_t rep_sint32_count;
+ int32_t rep_sint32[5];
+ pb_size_t rep_sint64_count;
+ int64_t rep_sint64[5];
+ pb_size_t rep_bool_count;
+ bool rep_bool[5];
+ pb_size_t rep_fixed32_count;
+ uint32_t rep_fixed32[5];
+ pb_size_t rep_sfixed32_count;
+ int32_t rep_sfixed32[5];
+ pb_size_t rep_float_count;
+ float rep_float[5];
+ pb_size_t rep_fixed64_count;
+ uint64_t rep_fixed64[5];
+ pb_size_t rep_sfixed64_count;
+ int64_t rep_sfixed64[5];
+ pb_size_t rep_double_count;
+ double rep_double[5];
+ pb_size_t rep_string_count;
+ char rep_string[5][16];
+ pb_size_t rep_bytes_count;
+ AllTypes_rep_bytes_t rep_bytes[5];
+ pb_size_t rep_submsg_count;
+ SubMessage rep_submsg[5];
+ pb_size_t rep_enum_count;
+ MyEnum rep_enum[5];
+ bool has_opt_int32;
+ int32_t opt_int32;
+ bool has_opt_int64;
+ int64_t opt_int64;
+ bool has_opt_uint32;
+ uint32_t opt_uint32;
+ bool has_opt_uint64;
+ uint64_t opt_uint64;
+ bool has_opt_sint32;
+ int32_t opt_sint32;
+ bool has_opt_sint64;
+ int64_t opt_sint64;
+ bool has_opt_bool;
+ bool opt_bool;
+ bool has_opt_fixed32;
+ uint32_t opt_fixed32;
+ bool has_opt_sfixed32;
+ int32_t opt_sfixed32;
+ bool has_opt_float;
+ float opt_float;
+ bool has_opt_fixed64;
+ uint64_t opt_fixed64;
+ bool has_opt_sfixed64;
+ int64_t opt_sfixed64;
+ bool has_opt_double;
+ double opt_double;
+ bool has_opt_string;
+ char opt_string[16];
+ bool has_opt_bytes;
+ AllTypes_opt_bytes_t opt_bytes;
+ bool has_opt_submsg;
+ SubMessage opt_submsg;
+ bool has_opt_enum;
+ MyEnum opt_enum;
+ int32_t end;
+ pb_extension_t *extensions;
+} AllTypes;
+
+/* Default values for struct fields */
+extern const char SubMessage_substuff1_default[16];
+extern const int32_t SubMessage_substuff2_default;
+extern const uint32_t SubMessage_substuff3_default;
+extern const int32_t Limits_int32_min_default;
+extern const int32_t Limits_int32_max_default;
+extern const uint32_t Limits_uint32_min_default;
+extern const uint32_t Limits_uint32_max_default;
+extern const int64_t Limits_int64_min_default;
+extern const int64_t Limits_int64_max_default;
+extern const uint64_t Limits_uint64_min_default;
+extern const uint64_t Limits_uint64_max_default;
+extern const HugeEnum Limits_enum_min_default;
+extern const HugeEnum Limits_enum_max_default;
+extern const int32_t AllTypes_opt_int32_default;
+extern const int64_t AllTypes_opt_int64_default;
+extern const uint32_t AllTypes_opt_uint32_default;
+extern const uint64_t AllTypes_opt_uint64_default;
+extern const int32_t AllTypes_opt_sint32_default;
+extern const int64_t AllTypes_opt_sint64_default;
+extern const bool AllTypes_opt_bool_default;
+extern const uint32_t AllTypes_opt_fixed32_default;
+extern const int32_t AllTypes_opt_sfixed32_default;
+extern const float AllTypes_opt_float_default;
+extern const uint64_t AllTypes_opt_fixed64_default;
+extern const int64_t AllTypes_opt_sfixed64_default;
+extern const double AllTypes_opt_double_default;
+extern const char AllTypes_opt_string_default[16];
+extern const AllTypes_opt_bytes_t AllTypes_opt_bytes_default;
+extern const MyEnum AllTypes_opt_enum_default;
+
+/* Initializer values for message structs */
+#define SubMessage_init_default {"1", 2, false, 3u}
+#define EmptyMessage_init_default {0}
+#define Limits_init_default {2147483647, -2147483647, 4294967295u, 0u, 9223372036854775807ll, -9223372036854775807ll, 18446744073709551615ull, 0ull, HugeEnum_Positive, HugeEnum_Negative}
+#define AllTypes_init_default {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", {0, {0}}, SubMessage_init_default, (MyEnum)0, 0, {0, 0, 0, 0, 0}, 0, {0, 0, 0, 0, 0}, 0, {0, 0, 0, 0, 0}, 0, {0, 0, 0, 0, 0}, 0, {0, 0, 0, 0, 0}, 0, {0, 0, 0, 0, 0}, 0, {0, 0, 0, 0, 0}, 0, {0, 0, 0, 0, 0}, 0, {0, 0, 0, 0, 0}, 0, {0, 0, 0, 0, 0}, 0, {0, 0, 0, 0, 0}, 0, {0, 0, 0, 0, 0}, 0, {0, 0, 0, 0, 0}, 0, {"", "", "", "", ""}, 0, {{0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}}, 0, {SubMessage_init_default, SubMessage_init_default, SubMessage_init_default, SubMessage_init_default, SubMessage_init_default}, 0, {(MyEnum)0, (MyEnum)0, (MyEnum)0, (MyEnum)0, (MyEnum)0}, false, 4041, false, 4042ll, false, 4043u, false, 4044ull, false, 4045, false, 4046, false, false, false, 4048u, false, 4049, false, 4050, false, 4051ull, false, 4052ll, false, 4053, false, "4054", false, {4, {0x34,0x30,0x35,0x35}}, false, SubMessage_init_default, false, MyEnum_Second, 0, NULL}
+#define SubMessage_init_zero {"", 0, false, 0}
+#define EmptyMessage_init_zero {0}
+#define Limits_init_zero {0, 0, 0, 0, 0, 0, 0, 0, (HugeEnum)0, (HugeEnum)0}
+#define AllTypes_init_zero {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", {0, {0}}, SubMessage_init_zero, (MyEnum)0, 0, {0, 0, 0, 0, 0}, 0, {0, 0, 0, 0, 0}, 0, {0, 0, 0, 0, 0}, 0, {0, 0, 0, 0, 0}, 0, {0, 0, 0, 0, 0}, 0, {0, 0, 0, 0, 0}, 0, {0, 0, 0, 0, 0}, 0, {0, 0, 0, 0, 0}, 0, {0, 0, 0, 0, 0}, 0, {0, 0, 0, 0, 0}, 0, {0, 0, 0, 0, 0}, 0, {0, 0, 0, 0, 0}, 0, {0, 0, 0, 0, 0}, 0, {"", "", "", "", ""}, 0, {{0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}, {0, {0}}}, 0, {SubMessage_init_zero, SubMessage_init_zero, SubMessage_init_zero, SubMessage_init_zero, SubMessage_init_zero}, 0, {(MyEnum)0, (MyEnum)0, (MyEnum)0, (MyEnum)0, (MyEnum)0}, false, 0, false, 0, false, 0, false, 0, false, 0, false, 0, false, 0, false, 0, false, 0, false, 0, false, 0, false, 0, false, 0, false, "", false, {0, {0}}, false, SubMessage_init_zero, false, (MyEnum)0, 0, NULL}
+
+/* Field tags (for use in manual encoding/decoding) */
+#define Limits_int32_min_tag 1
+#define Limits_int32_max_tag 2
+#define Limits_uint32_min_tag 3
+#define Limits_uint32_max_tag 4
+#define Limits_int64_min_tag 5
+#define Limits_int64_max_tag 6
+#define Limits_uint64_min_tag 7
+#define Limits_uint64_max_tag 8
+#define Limits_enum_min_tag 9
+#define Limits_enum_max_tag 10
+#define SubMessage_substuff1_tag 1
+#define SubMessage_substuff2_tag 2
+#define SubMessage_substuff3_tag 3
+#define AllTypes_req_int32_tag 1
+#define AllTypes_req_int64_tag 2
+#define AllTypes_req_uint32_tag 3
+#define AllTypes_req_uint64_tag 4
+#define AllTypes_req_sint32_tag 5
+#define AllTypes_req_sint64_tag 6
+#define AllTypes_req_bool_tag 7
+#define AllTypes_req_fixed32_tag 8
+#define AllTypes_req_sfixed32_tag 9
+#define AllTypes_req_float_tag 10
+#define AllTypes_req_fixed64_tag 11
+#define AllTypes_req_sfixed64_tag 12
+#define AllTypes_req_double_tag 13
+#define AllTypes_req_string_tag 14
+#define AllTypes_req_bytes_tag 15
+#define AllTypes_req_submsg_tag 16
+#define AllTypes_req_enum_tag 17
+#define AllTypes_rep_int32_tag 21
+#define AllTypes_rep_int64_tag 22
+#define AllTypes_rep_uint32_tag 23
+#define AllTypes_rep_uint64_tag 24
+#define AllTypes_rep_sint32_tag 25
+#define AllTypes_rep_sint64_tag 26
+#define AllTypes_rep_bool_tag 27
+#define AllTypes_rep_fixed32_tag 28
+#define AllTypes_rep_sfixed32_tag 29
+#define AllTypes_rep_float_tag 30
+#define AllTypes_rep_fixed64_tag 31
+#define AllTypes_rep_sfixed64_tag 32
+#define AllTypes_rep_double_tag 33
+#define AllTypes_rep_string_tag 34
+#define AllTypes_rep_bytes_tag 35
+#define AllTypes_rep_submsg_tag 36
+#define AllTypes_rep_enum_tag 37
+#define AllTypes_opt_int32_tag 41
+#define AllTypes_opt_int64_tag 42
+#define AllTypes_opt_uint32_tag 43
+#define AllTypes_opt_uint64_tag 44
+#define AllTypes_opt_sint32_tag 45
+#define AllTypes_opt_sint64_tag 46
+#define AllTypes_opt_bool_tag 47
+#define AllTypes_opt_fixed32_tag 48
+#define AllTypes_opt_sfixed32_tag 49
+#define AllTypes_opt_float_tag 50
+#define AllTypes_opt_fixed64_tag 51
+#define AllTypes_opt_sfixed64_tag 52
+#define AllTypes_opt_double_tag 53
+#define AllTypes_opt_string_tag 54
+#define AllTypes_opt_bytes_tag 55
+#define AllTypes_opt_submsg_tag 56
+#define AllTypes_opt_enum_tag 57
+#define AllTypes_end_tag 99
+
+/* Struct field encoding specification for nanopb */
+extern const pb_field_t SubMessage_fields[4];
+extern const pb_field_t EmptyMessage_fields[1];
+extern const pb_field_t Limits_fields[11];
+extern const pb_field_t AllTypes_fields[54];
+
+/* Maximum encoded size of messages (where known) */
+#define SubMessage_size 34
+#define EmptyMessage_size 0
+#define Limits_size 90
+#define AllTypes_size 1362
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif
diff --git a/libs/nanopb/tests/backwards_compatibility/alltypes_legacy.options b/libs/nanopb/tests/backwards_compatibility/alltypes_legacy.options
new file mode 100644
index 00000000..b31e3cf0
--- /dev/null
+++ b/libs/nanopb/tests/backwards_compatibility/alltypes_legacy.options
@@ -0,0 +1,3 @@
+* max_size:16
+* max_count:5
+
diff --git a/libs/nanopb/tests/backwards_compatibility/alltypes_legacy.proto b/libs/nanopb/tests/backwards_compatibility/alltypes_legacy.proto
new file mode 100644
index 00000000..f5bc35ce
--- /dev/null
+++ b/libs/nanopb/tests/backwards_compatibility/alltypes_legacy.proto
@@ -0,0 +1,110 @@
+syntax = "proto2";
+
+message SubMessage {
+ required string substuff1 = 1 [default = "1"];
+ required int32 substuff2 = 2 [default = 2];
+ optional fixed32 substuff3 = 3 [default = 3];
+}
+
+message EmptyMessage {
+
+}
+
+enum HugeEnum {
+ Negative = -2147483647; /* protoc doesn't accept -2147483648 here */
+ Positive = 2147483647;
+}
+
+message Limits {
+ required int32 int32_min = 1 [default = 2147483647];
+ required int32 int32_max = 2 [default = -2147483647];
+ required uint32 uint32_min = 3 [default = 4294967295];
+ required uint32 uint32_max = 4 [default = 0];
+ required int64 int64_min = 5 [default = 9223372036854775807];
+ required int64 int64_max = 6 [default = -9223372036854775807];
+ required uint64 uint64_min = 7 [default = 18446744073709551615];
+ required uint64 uint64_max = 8 [default = 0];
+ required HugeEnum enum_min = 9 [default = Positive];
+ required HugeEnum enum_max = 10 [default = Negative];
+}
+
+enum MyEnum {
+ Zero = 0;
+ 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;
+ required bytes req_bytes = 15;
+ required SubMessage req_submsg = 16;
+ required MyEnum req_enum = 17;
+
+
+ repeated int32 rep_int32 = 21 [packed = true];
+ repeated int64 rep_int64 = 22 [packed = true];
+ repeated uint32 rep_uint32 = 23 [packed = true];
+ repeated uint64 rep_uint64 = 24 [packed = true];
+ repeated sint32 rep_sint32 = 25 [packed = true];
+ repeated sint64 rep_sint64 = 26 [packed = true];
+ repeated bool rep_bool = 27 [packed = true];
+
+ repeated fixed32 rep_fixed32 = 28 [packed = true];
+ repeated sfixed32 rep_sfixed32= 29 [packed = true];
+ repeated float rep_float = 30 [packed = true];
+
+ repeated fixed64 rep_fixed64 = 31 [packed = true];
+ repeated sfixed64 rep_sfixed64= 32 [packed = true];
+ repeated double rep_double = 33 [packed = true];
+
+ repeated string rep_string = 34;
+ repeated bytes rep_bytes = 35;
+ repeated SubMessage rep_submsg = 36;
+ repeated MyEnum rep_enum = 37 [packed = true];
+
+ optional int32 opt_int32 = 41 [default = 4041];
+ optional int64 opt_int64 = 42 [default = 4042];
+ optional uint32 opt_uint32 = 43 [default = 4043];
+ optional uint64 opt_uint64 = 44 [default = 4044];
+ optional sint32 opt_sint32 = 45 [default = 4045];
+ optional sint64 opt_sint64 = 46 [default = 4046];
+ optional bool opt_bool = 47 [default = false];
+
+ optional fixed32 opt_fixed32 = 48 [default = 4048];
+ optional sfixed32 opt_sfixed32= 49 [default = 4049];
+ optional float opt_float = 50 [default = 4050];
+
+ optional fixed64 opt_fixed64 = 51 [default = 4051];
+ optional sfixed64 opt_sfixed64= 52 [default = 4052];
+ optional double opt_double = 53 [default = 4053];
+
+ optional string opt_string = 54 [default = "4054"];
+ optional bytes opt_bytes = 55 [default = "4055"];
+ optional SubMessage opt_submsg = 56;
+ optional MyEnum opt_enum = 57 [default = Second];
+
+ // 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;
+
+
+ extensions 200 to 255;
+}
+
diff --git a/libs/nanopb/tests/backwards_compatibility/decode_legacy.c b/libs/nanopb/tests/backwards_compatibility/decode_legacy.c
new file mode 100644
index 00000000..5f5b6bbe
--- /dev/null
+++ b/libs/nanopb/tests/backwards_compatibility/decode_legacy.c
@@ -0,0 +1,199 @@
+/* Tests the decoding of all types.
+ * 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. ./encode_legacy | ./decode_legacy
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <pb_decode.h>
+#include "alltypes_legacy.h"
+#include "test_helpers.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, int mode)
+{
+ AllTypes alltypes = {0};
+
+ 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_submsg.substuff3 == 3);
+ TEST(alltypes.req_enum == MyEnum_Truth);
+
+ TEST(alltypes.rep_int32_count == 5 && alltypes.rep_int32[4] == -2001 && alltypes.rep_int32[0] == 0);
+ TEST(alltypes.rep_int64_count == 5 && alltypes.rep_int64[4] == -2002 && alltypes.rep_int64[0] == 0);
+ TEST(alltypes.rep_uint32_count == 5 && alltypes.rep_uint32[4] == 2003 && alltypes.rep_uint32[0] == 0);
+ TEST(alltypes.rep_uint64_count == 5 && alltypes.rep_uint64[4] == 2004 && alltypes.rep_uint64[0] == 0);
+ TEST(alltypes.rep_sint32_count == 5 && alltypes.rep_sint32[4] == -2005 && alltypes.rep_sint32[0] == 0);
+ TEST(alltypes.rep_sint64_count == 5 && alltypes.rep_sint64[4] == -2006 && alltypes.rep_sint64[0] == 0);
+ TEST(alltypes.rep_bool_count == 5 && alltypes.rep_bool[4] == true && alltypes.rep_bool[0] == false);
+
+ TEST(alltypes.rep_fixed32_count == 5 && alltypes.rep_fixed32[4] == 2008 && alltypes.rep_fixed32[0] == 0);
+ TEST(alltypes.rep_sfixed32_count == 5 && alltypes.rep_sfixed32[4] == -2009 && alltypes.rep_sfixed32[0] == 0);
+ TEST(alltypes.rep_float_count == 5 && alltypes.rep_float[4] == 2010.0f && alltypes.rep_float[0] == 0.0f);
+
+ TEST(alltypes.rep_fixed64_count == 5 && alltypes.rep_fixed64[4] == 2011 && alltypes.rep_fixed64[0] == 0);
+ TEST(alltypes.rep_sfixed64_count == 5 && alltypes.rep_sfixed64[4] == -2012 && alltypes.rep_sfixed64[0] == 0);
+ TEST(alltypes.rep_double_count == 5 && alltypes.rep_double[4] == 2013.0 && alltypes.rep_double[0] == 0.0);
+
+ TEST(alltypes.rep_string_count == 5 && strcmp(alltypes.rep_string[4], "2014") == 0 && alltypes.rep_string[0][0] == '\0');
+ TEST(alltypes.rep_bytes_count == 5 && alltypes.rep_bytes[4].size == 4 && alltypes.rep_bytes[0].size == 0);
+ TEST(memcmp(alltypes.rep_bytes[4].bytes, "2015", 4) == 0);
+
+ TEST(alltypes.rep_submsg_count == 5);
+ TEST(strcmp(alltypes.rep_submsg[4].substuff1, "2016") == 0 && alltypes.rep_submsg[0].substuff1[0] == '\0');
+ TEST(alltypes.rep_submsg[4].substuff2 == 2016 && alltypes.rep_submsg[0].substuff2 == 0);
+ TEST(alltypes.rep_submsg[4].substuff3 == 2016 && alltypes.rep_submsg[0].substuff3 == 3);
+
+ TEST(alltypes.rep_enum_count == 5 && alltypes.rep_enum[4] == MyEnum_Truth && alltypes.rep_enum[0] == MyEnum_Zero);
+
+ if (mode == 0)
+ {
+ /* Expect default values */
+ TEST(alltypes.has_opt_int32 == false);
+ TEST(alltypes.opt_int32 == 4041);
+ TEST(alltypes.has_opt_int64 == false);
+ TEST(alltypes.opt_int64 == 4042);
+ TEST(alltypes.has_opt_uint32 == false);
+ TEST(alltypes.opt_uint32 == 4043);
+ TEST(alltypes.has_opt_uint64 == false);
+ TEST(alltypes.opt_uint64 == 4044);
+ TEST(alltypes.has_opt_sint32 == false);
+ TEST(alltypes.opt_sint32 == 4045);
+ TEST(alltypes.has_opt_sint64 == false);
+ TEST(alltypes.opt_sint64 == 4046);
+ TEST(alltypes.has_opt_bool == false);
+ TEST(alltypes.opt_bool == false);
+
+ TEST(alltypes.has_opt_fixed32 == false);
+ TEST(alltypes.opt_fixed32 == 4048);
+ TEST(alltypes.has_opt_sfixed32 == false);
+ TEST(alltypes.opt_sfixed32 == 4049);
+ TEST(alltypes.has_opt_float == false);
+ TEST(alltypes.opt_float == 4050.0f);
+
+ TEST(alltypes.has_opt_fixed64 == false);
+ TEST(alltypes.opt_fixed64 == 4051);
+ TEST(alltypes.has_opt_sfixed64 == false);
+ TEST(alltypes.opt_sfixed64 == 4052);
+ TEST(alltypes.has_opt_double == false);
+ TEST(alltypes.opt_double == 4053.0);
+
+ TEST(alltypes.has_opt_string == false);
+ TEST(strcmp(alltypes.opt_string, "4054") == 0);
+ TEST(alltypes.has_opt_bytes == false);
+ TEST(alltypes.opt_bytes.size == 4);
+ TEST(memcmp(alltypes.opt_bytes.bytes, "4055", 4) == 0);
+ TEST(alltypes.has_opt_submsg == false);
+ TEST(strcmp(alltypes.opt_submsg.substuff1, "1") == 0);
+ TEST(alltypes.opt_submsg.substuff2 == 2);
+ TEST(alltypes.opt_submsg.substuff3 == 3);
+ TEST(alltypes.has_opt_enum == false);
+ TEST(alltypes.opt_enum == MyEnum_Second);
+ }
+ else
+ {
+ /* Expect filled-in values */
+ TEST(alltypes.has_opt_int32 == true);
+ TEST(alltypes.opt_int32 == 3041);
+ TEST(alltypes.has_opt_int64 == true);
+ TEST(alltypes.opt_int64 == 3042);
+ TEST(alltypes.has_opt_uint32 == true);
+ TEST(alltypes.opt_uint32 == 3043);
+ TEST(alltypes.has_opt_uint64 == true);
+ TEST(alltypes.opt_uint64 == 3044);
+ TEST(alltypes.has_opt_sint32 == true);
+ TEST(alltypes.opt_sint32 == 3045);
+ TEST(alltypes.has_opt_sint64 == true);
+ TEST(alltypes.opt_sint64 == 3046);
+ TEST(alltypes.has_opt_bool == true);
+ TEST(alltypes.opt_bool == true);
+
+ TEST(alltypes.has_opt_fixed32 == true);
+ TEST(alltypes.opt_fixed32 == 3048);
+ TEST(alltypes.has_opt_sfixed32 == true);
+ TEST(alltypes.opt_sfixed32 == 3049);
+ TEST(alltypes.has_opt_float == true);
+ TEST(alltypes.opt_float == 3050.0f);
+
+ TEST(alltypes.has_opt_fixed64 == true);
+ TEST(alltypes.opt_fixed64 == 3051);
+ TEST(alltypes.has_opt_sfixed64 == true);
+ TEST(alltypes.opt_sfixed64 == 3052);
+ TEST(alltypes.has_opt_double == true);
+ TEST(alltypes.opt_double == 3053.0);
+
+ TEST(alltypes.has_opt_string == true);
+ TEST(strcmp(alltypes.opt_string, "3054") == 0);
+ TEST(alltypes.has_opt_bytes == true);
+ TEST(alltypes.opt_bytes.size == 4);
+ TEST(memcmp(alltypes.opt_bytes.bytes, "3055", 4) == 0);
+ TEST(alltypes.has_opt_submsg == true);
+ TEST(strcmp(alltypes.opt_submsg.substuff1, "3056") == 0);
+ TEST(alltypes.opt_submsg.substuff2 == 3056);
+ TEST(alltypes.opt_submsg.substuff3 == 3);
+ TEST(alltypes.has_opt_enum == true);
+ TEST(alltypes.opt_enum == MyEnum_Truth);
+ }
+
+ TEST(alltypes.end == 1099);
+
+ return true;
+}
+
+int main(int argc, char **argv)
+{
+ uint8_t buffer[1024];
+ size_t count;
+ pb_istream_t stream;
+
+ /* Whether to expect the optional values or the default values. */
+ int mode = (argc > 1) ? atoi(argv[1]) : 0;
+
+ /* Read the data into buffer */
+ SET_BINARY_MODE(stdin);
+ count = fread(buffer, 1, sizeof(buffer), stdin);
+
+ /* Construct a pb_istream_t for reading from the buffer */
+ stream = pb_istream_from_buffer(buffer, count);
+
+ /* Decode and print out the stuff */
+ if (!check_alltypes(&stream, mode))
+ {
+ printf("Parsing failed: %s\n", PB_GET_ERROR(&stream));
+ return 1;
+ } else {
+ return 0;
+ }
+}
diff --git a/libs/nanopb/tests/backwards_compatibility/encode_legacy.c b/libs/nanopb/tests/backwards_compatibility/encode_legacy.c
new file mode 100644
index 00000000..5c9d41b3
--- /dev/null
+++ b/libs/nanopb/tests/backwards_compatibility/encode_legacy.c
@@ -0,0 +1,135 @@
+/* Attempts to test all the datatypes supported by ProtoBuf.
+ * 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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pb_encode.h>
+#include "alltypes_legacy.h"
+#include "test_helpers.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))
+ {
+ 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/libs/nanopb/tests/basic_buffer/SConscript b/libs/nanopb/tests/basic_buffer/SConscript
new file mode 100644
index 00000000..acaf5ffa
--- /dev/null
+++ b/libs/nanopb/tests/basic_buffer/SConscript
@@ -0,0 +1,12 @@
+# Build and run a basic round-trip test using memory buffer encoding.
+
+Import("env")
+
+enc = env.Program(["encode_buffer.c", "$COMMON/person.pb.c", "$COMMON/pb_encode.o", "$COMMON/pb_common.o"])
+dec = env.Program(["decode_buffer.c", "$COMMON/person.pb.c", "$COMMON/pb_decode.o", "$COMMON/pb_common.o"])
+
+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/libs/nanopb/tests/basic_buffer/decode_buffer.c b/libs/nanopb/tests/basic_buffer/decode_buffer.c
new file mode 100644
index 00000000..291d164c
--- /dev/null
+++ b/libs/nanopb/tests/basic_buffer/decode_buffer.c
@@ -0,0 +1,88 @@
+/* A very simple decoding test case, using person.proto.
+ * Produces output compatible with protoc --decode.
+ * Reads the encoded data from stdin and prints the values
+ * to stdout as text.
+ *
+ * Run e.g. ./test_encode1 | ./test_decode1
+ */
+
+#include <stdio.h>
+#include <pb_decode.h>
+#include "person.pb.h"
+#include "test_helpers.h"
+
+/* This function is called once from main(), it handles
+ the decoding and printing. */
+bool print_person(pb_istream_t *stream)
+{
+ int i;
+ Person person = Person_init_zero;
+
+ if (!pb_decode(stream, Person_fields, &person))
+ return false;
+
+ /* Now the decoding is done, rest is just to print stuff out. */
+
+ printf("name: \"%s\"\n", person.name);
+ printf("id: %ld\n", (long)person.id);
+
+ if (person.has_email)
+ printf("email: \"%s\"\n", person.email);
+
+ for (i = 0; i < person.phone_count; i++)
+ {
+ Person_PhoneNumber *phone = &person.phone[i];
+ printf("phone {\n");
+ printf(" number: \"%s\"\n", phone->number);
+
+ if (phone->has_type)
+ {
+ switch (phone->type)
+ {
+ case Person_PhoneType_WORK:
+ printf(" type: WORK\n");
+ break;
+
+ case Person_PhoneType_HOME:
+ printf(" type: HOME\n");
+ break;
+
+ case Person_PhoneType_MOBILE:
+ printf(" type: MOBILE\n");
+ break;
+ }
+ }
+ printf("}\n");
+ }
+
+ return true;
+}
+
+int main()
+{
+ uint8_t buffer[Person_size];
+ 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))
+ {
+ printf("Message does not fit in buffer\n");
+ return 1;
+ }
+
+ /* Construct a pb_istream_t for reading from the buffer */
+ stream = pb_istream_from_buffer(buffer, count);
+
+ /* Decode and print out the stuff */
+ if (!print_person(&stream))
+ {
+ printf("Parsing failed: %s\n", PB_GET_ERROR(&stream));
+ return 1;
+ } else {
+ return 0;
+ }
+}
diff --git a/libs/nanopb/tests/basic_buffer/encode_buffer.c b/libs/nanopb/tests/basic_buffer/encode_buffer.c
new file mode 100644
index 00000000..c412c14e
--- /dev/null
+++ b/libs/nanopb/tests/basic_buffer/encode_buffer.c
@@ -0,0 +1,38 @@
+/* A very simple encoding test case using person.proto.
+ * Just puts constant data in the fields and encodes into
+ * buffer, which is then written to stdout.
+ */
+
+#include <stdio.h>
+#include <pb_encode.h>
+#include "person.pb.h"
+#include "test_helpers.h"
+
+int main()
+{
+ uint8_t buffer[Person_size];
+ 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},
+ {"99-2342", false, 0},
+ {"1234-5678", true, Person_PhoneType_WORK},
+ }};
+
+ 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 */
+ }
+ else
+ {
+ fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream));
+ return 1; /* Failure */
+ }
+}
diff --git a/libs/nanopb/tests/basic_stream/SConscript b/libs/nanopb/tests/basic_stream/SConscript
new file mode 100644
index 00000000..7d668562
--- /dev/null
+++ b/libs/nanopb/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", "$COMMON/pb_common.o"])
+dec = env.Program(["decode_stream.c", "$COMMON/person.pb.c", "$COMMON/pb_decode.o", "$COMMON/pb_common.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/libs/nanopb/tests/basic_stream/decode_stream.c b/libs/nanopb/tests/basic_stream/decode_stream.c
new file mode 100644
index 00000000..798dcc50
--- /dev/null
+++ b/libs/nanopb/tests/basic_stream/decode_stream.c
@@ -0,0 +1,84 @@
+/* Same as test_decode1 but reads from stdin directly.
+ */
+
+#include <stdio.h>
+#include <pb_decode.h>
+#include "person.pb.h"
+#include "test_helpers.h"
+
+/* This function is called once from main(), it handles
+ the decoding and printing.
+ Ugly copy-paste from test_decode1.c. */
+bool print_person(pb_istream_t *stream)
+{
+ int i;
+ Person person = Person_init_zero;
+
+ if (!pb_decode(stream, Person_fields, &person))
+ return false;
+
+ /* Now the decoding is done, rest is just to print stuff out. */
+
+ printf("name: \"%s\"\n", person.name);
+ printf("id: %ld\n", (long)person.id);
+
+ if (person.has_email)
+ printf("email: \"%s\"\n", person.email);
+
+ for (i = 0; i < person.phone_count; i++)
+ {
+ Person_PhoneNumber *phone = &person.phone[i];
+ printf("phone {\n");
+ printf(" number: \"%s\"\n", phone->number);
+
+ if (phone->has_type)
+ {
+ switch (phone->type)
+ {
+ case Person_PhoneType_WORK:
+ printf(" type: WORK\n");
+ break;
+
+ case Person_PhoneType_HOME:
+ printf(" type: HOME\n");
+ break;
+
+ case Person_PhoneType_MOBILE:
+ printf(" type: MOBILE\n");
+ break;
+ }
+ }
+ printf("}\n");
+ }
+
+ return true;
+}
+
+/* This binds the pb_istream_t to stdin */
+bool callback(pb_istream_t *stream, uint8_t *buf, size_t count)
+{
+ FILE *file = (FILE*)stream->state;
+ bool status;
+
+ status = (fread(buf, 1, count, file) == count);
+
+ if (feof(file))
+ stream->bytes_left = 0;
+
+ return status;
+}
+
+int main()
+{
+ 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));
+ return 1;
+ } else {
+ return 0;
+ }
+}
diff --git a/libs/nanopb/tests/basic_stream/encode_stream.c b/libs/nanopb/tests/basic_stream/encode_stream.c
new file mode 100644
index 00000000..7f571c41
--- /dev/null
+++ b/libs/nanopb/tests/basic_stream/encode_stream.c
@@ -0,0 +1,40 @@
+/* Same as test_encode1.c, except writes directly to stdout.
+ */
+
+#include <stdio.h>
+#include <pb_encode.h>
+#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)
+{
+ FILE *file = (FILE*) stream->state;
+ return fwrite(buf, 1, count, file) == count;
+}
+
+int main()
+{
+ /* Initialize the structure with constants */
+ Person person = {"Test Person 99", 99, true, "test@person.com",
+ 3, {{"555-12345678", true, Person_PhoneType_MOBILE},
+ {"99-2342", false, 0},
+ {"1234-5678", true, Person_PhoneType_WORK},
+ }};
+
+ /* Prepare the stream, output goes directly to stdout */
+ 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))
+ {
+ return 0; /* Success */
+ }
+ else
+ {
+ fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream));
+ return 1; /* Failure */
+ }
+}
diff --git a/libs/nanopb/tests/buffer_only/SConscript b/libs/nanopb/tests/buffer_only/SConscript
new file mode 100644
index 00000000..55b747b0
--- /dev/null
+++ b/libs/nanopb/tests/buffer_only/SConscript
@@ -0,0 +1,28 @@
+# Run the alltypes test case, but compile with PB_BUFFER_ONLY=1
+
+Import("env")
+
+# Take copy of the files for custom build.
+c = Copy("$TARGET", "$SOURCE")
+env.Command("alltypes.pb.h", "$BUILD/alltypes/alltypes.pb.h", c)
+env.Command("alltypes.pb.c", "$BUILD/alltypes/alltypes.pb.c", c)
+env.Command("encode_alltypes.c", "$BUILD/alltypes/encode_alltypes.c", c)
+env.Command("decode_alltypes.c", "$BUILD/alltypes/decode_alltypes.c", c)
+
+# Define the compilation options
+opts = env.Clone()
+opts.Append(CPPDEFINES = {'PB_BUFFER_ONLY': 1})
+
+# Build new version of core
+strict = opts.Clone()
+strict.Append(CFLAGS = strict['CORECFLAGS'])
+strict.Object("pb_decode_bufonly.o", "$NANOPB/pb_decode.c")
+strict.Object("pb_encode_bufonly.o", "$NANOPB/pb_encode.c")
+strict.Object("pb_common_bufonly.o", "$NANOPB/pb_common.c")
+
+# Now build and run the test normally.
+enc = opts.Program(["encode_alltypes.c", "alltypes.pb.c", "pb_encode_bufonly.o", "pb_common_bufonly.o"])
+dec = opts.Program(["decode_alltypes.c", "alltypes.pb.c", "pb_decode_bufonly.o", "pb_common_bufonly.o"])
+
+env.RunTest(enc)
+env.RunTest([dec, "encode_alltypes.output"])
diff --git a/libs/nanopb/tests/callbacks/SConscript b/libs/nanopb/tests/callbacks/SConscript
new file mode 100644
index 00000000..44521439
--- /dev/null
+++ b/libs/nanopb/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", "$COMMON/pb_common.o"])
+dec = env.Program(["decode_callbacks.c", "callbacks.pb.c", "$COMMON/pb_decode.o", "$COMMON/pb_common.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/libs/nanopb/tests/callbacks/callbacks.proto b/libs/nanopb/tests/callbacks/callbacks.proto
new file mode 100644
index 00000000..96ac744d
--- /dev/null
+++ b/libs/nanopb/tests/callbacks/callbacks.proto
@@ -0,0 +1,18 @@
+syntax = "proto2";
+
+message SubMessage {
+ optional string stringvalue = 1;
+ repeated int32 int32value = 2;
+ repeated fixed32 fixed32value = 3;
+ repeated fixed64 fixed64value = 4;
+}
+
+message TestMessage {
+ optional string stringvalue = 1;
+ repeated int32 int32value = 2;
+ repeated fixed32 fixed32value = 3;
+ repeated fixed64 fixed64value = 4;
+ optional SubMessage submsg = 5;
+ repeated string repeatedstring = 6;
+}
+
diff --git a/libs/nanopb/tests/callbacks/decode_callbacks.c b/libs/nanopb/tests/callbacks/decode_callbacks.c
new file mode 100644
index 00000000..45724d0b
--- /dev/null
+++ b/libs/nanopb/tests/callbacks/decode_callbacks.c
@@ -0,0 +1,97 @@
+/* Decoding testcase for callback fields.
+ * Run e.g. ./test_encode_callbacks | ./test_decode_callbacks
+ */
+
+#include <stdio.h>
+#include <pb_decode.h>
+#include "callbacks.pb.h"
+#include "test_helpers.h"
+
+bool print_string(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ uint8_t buffer[1024] = {0};
+
+ /* We could read block-by-block to avoid the large buffer... */
+ if (stream->bytes_left > sizeof(buffer) - 1)
+ return false;
+
+ if (!pb_read(stream, buffer, stream->bytes_left))
+ return false;
+
+ /* Print the string, in format comparable with protoc --decode.
+ * Format comes from the arg defined in main().
+ */
+ printf((char*)*arg, buffer);
+ return true;
+}
+
+bool print_int32(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ uint64_t value;
+ if (!pb_decode_varint(stream, &value))
+ return false;
+
+ printf((char*)*arg, (long)value);
+ return true;
+}
+
+bool print_fixed32(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ uint32_t value;
+ if (!pb_decode_fixed32(stream, &value))
+ return false;
+
+ printf((char*)*arg, (long)value);
+ return true;
+}
+
+bool print_fixed64(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ uint64_t value;
+ if (!pb_decode_fixed64(stream, &value))
+ return false;
+
+ printf((char*)*arg, (long)value);
+ return true;
+}
+
+int main()
+{
+ uint8_t buffer[1024];
+ 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 = {{{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";
+ testmessage.submsg.int32value.funcs.decode = &print_int32;
+ testmessage.submsg.int32value.arg = " int32value: %ld\n";
+ 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: %ld\n}\n";
+
+ testmessage.stringvalue.funcs.decode = &print_string;
+ testmessage.stringvalue.arg = "stringvalue: \"%s\"\n";
+ testmessage.int32value.funcs.decode = &print_int32;
+ testmessage.int32value.arg = "int32value: %ld\n";
+ testmessage.fixed32value.funcs.decode = &print_fixed32;
+ testmessage.fixed32value.arg = "fixed32value: %ld\n";
+ testmessage.fixed64value.funcs.decode = &print_fixed64;
+ testmessage.fixed64value.arg = "fixed64value: %ld\n";
+ testmessage.repeatedstring.funcs.decode = &print_string;
+ testmessage.repeatedstring.arg = "repeatedstring: \"%s\"\n";
+
+ if (!pb_decode(&stream, TestMessage_fields, &testmessage))
+ return 1;
+
+ return 0;
+}
diff --git a/libs/nanopb/tests/callbacks/encode_callbacks.c b/libs/nanopb/tests/callbacks/encode_callbacks.c
new file mode 100644
index 00000000..6cb67b1e
--- /dev/null
+++ b/libs/nanopb/tests/callbacks/encode_callbacks.c
@@ -0,0 +1,92 @@
+/* Encoding testcase for callback fields */
+
+#include <stdio.h>
+#include <string.h>
+#include <pb_encode.h>
+#include "callbacks.pb.h"
+#include "test_helpers.h"
+
+bool encode_string(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ char *str = "Hello world!";
+
+ if (!pb_encode_tag_for_field(stream, field))
+ return false;
+
+ return pb_encode_string(stream, (uint8_t*)str, strlen(str));
+}
+
+bool encode_int32(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ if (!pb_encode_tag_for_field(stream, field))
+ return false;
+
+ return pb_encode_varint(stream, 42);
+}
+
+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;
+
+ 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;
+
+ return pb_encode_fixed64(stream, &value);
+}
+
+bool encode_repeatedstring(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ char *str[4] = {"Hello world!", "", "Test", "Test2"};
+ int i;
+
+ for (i = 0; i < 4; i++)
+ {
+ if (!pb_encode_tag_for_field(stream, field))
+ return false;
+
+ if (!pb_encode_string(stream, (uint8_t*)str[i], strlen(str[i])))
+ return false;
+ }
+ return true;
+}
+
+int main()
+{
+ uint8_t buffer[1024];
+ 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;
+ testmessage.fixed32value.funcs.encode = &encode_fixed32;
+ testmessage.fixed64value.funcs.encode = &encode_fixed64;
+
+ testmessage.has_submsg = true;
+ testmessage.submsg.stringvalue.funcs.encode = &encode_string;
+ testmessage.submsg.int32value.funcs.encode = &encode_int32;
+ testmessage.submsg.fixed32value.funcs.encode = &encode_fixed32;
+ testmessage.submsg.fixed64value.funcs.encode = &encode_fixed64;
+
+ testmessage.repeatedstring.funcs.encode = &encode_repeatedstring;
+
+ if (!pb_encode(&stream, TestMessage_fields, &testmessage))
+ return 1;
+
+ SET_BINARY_MODE(stdout);
+ if (fwrite(buffer, stream.bytes_written, 1, stdout) != 1)
+ return 2;
+
+ return 0;
+}
diff --git a/libs/nanopb/tests/common/SConscript b/libs/nanopb/tests/common/SConscript
new file mode 100644
index 00000000..05e2f852
--- /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 00000000..ad69f1ce
--- /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 00000000..7eec7952
--- /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 00000000..d295d9ed
--- /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 00000000..becefdf3
--- /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 00000000..f77760a5
--- /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 00000000..23b5b97f
--- /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 00000000..c2b470ad
--- /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"); \
+ }
+
+
diff --git a/libs/nanopb/tests/cxx_main_program/SConscript b/libs/nanopb/tests/cxx_main_program/SConscript
new file mode 100644
index 00000000..edb88127
--- /dev/null
+++ b/libs/nanopb/tests/cxx_main_program/SConscript
@@ -0,0 +1,25 @@
+# Run the alltypes test case, but compile it as C++ instead.
+# In fact, compile the entire nanopb using C++ compiler.
+
+Import("env")
+
+# This is needed to get INT32_MIN etc. macros defined
+env = env.Clone()
+env.Append(CPPDEFINES = ['__STDC_LIMIT_MACROS'])
+
+# Copy the files to .cxx extension in order to force C++ build.
+c = Copy("$TARGET", "$SOURCE")
+env.Command("pb_encode.cxx", "#../pb_encode.c", c)
+env.Command("pb_decode.cxx", "#../pb_decode.c", c)
+env.Command("pb_common.cxx", "#../pb_common.c", c)
+env.Command("alltypes.pb.h", "$BUILD/alltypes/alltypes.pb.h", c)
+env.Command("alltypes.pb.cxx", "$BUILD/alltypes/alltypes.pb.c", c)
+env.Command("encode_alltypes.cxx", "$BUILD/alltypes/encode_alltypes.c", c)
+env.Command("decode_alltypes.cxx", "$BUILD/alltypes/decode_alltypes.c", c)
+
+# Now build and run the test normally.
+enc = env.Program(["encode_alltypes.cxx", "alltypes.pb.cxx", "pb_encode.cxx", "pb_common.cxx"])
+dec = env.Program(["decode_alltypes.cxx", "alltypes.pb.cxx", "pb_decode.cxx", "pb_common.cxx"])
+
+env.RunTest(enc)
+env.RunTest([dec, "encode_alltypes.output"])
diff --git a/libs/nanopb/tests/cyclic_messages/SConscript b/libs/nanopb/tests/cyclic_messages/SConscript
new file mode 100644
index 00000000..c782001c
--- /dev/null
+++ b/libs/nanopb/tests/cyclic_messages/SConscript
@@ -0,0 +1,11 @@
+Import("env")
+
+# Encode cyclic messages with callback fields
+
+c = Copy("$TARGET", "$SOURCE")
+env.Command("cyclic_callback.proto", "cyclic.proto", c)
+env.NanopbProto(["cyclic_callback", "cyclic_callback.options"])
+
+enc_callback = env.Program(["encode_cyclic_callback.c", "cyclic_callback.pb.c", "$COMMON/pb_encode.o", "$COMMON/pb_common.o"])
+
+
diff --git a/libs/nanopb/tests/cyclic_messages/cyclic.proto b/libs/nanopb/tests/cyclic_messages/cyclic.proto
new file mode 100644
index 00000000..8cab0b14
--- /dev/null
+++ b/libs/nanopb/tests/cyclic_messages/cyclic.proto
@@ -0,0 +1,27 @@
+// Test structures with cyclic references.
+// These can only be handled in pointer/callback mode,
+// see associated .options files.
+
+syntax = "proto2";
+
+message TreeNode
+{
+ optional int32 leaf = 1;
+ optional TreeNode left = 2;
+ optional TreeNode right = 3;
+}
+
+message Dictionary
+{
+ repeated KeyValuePair dictItem = 1;
+}
+
+message KeyValuePair
+{
+ required string key = 1;
+ optional string stringValue = 2;
+ optional int32 intValue = 3;
+ optional Dictionary dictValue = 4;
+ optional TreeNode treeValue = 5;
+}
+
diff --git a/libs/nanopb/tests/cyclic_messages/cyclic_callback.options b/libs/nanopb/tests/cyclic_messages/cyclic_callback.options
new file mode 100644
index 00000000..fd4e1e1c
--- /dev/null
+++ b/libs/nanopb/tests/cyclic_messages/cyclic_callback.options
@@ -0,0 +1,7 @@
+TreeNode.left type:FT_CALLBACK
+TreeNode.right type:FT_CALLBACK
+
+Dictionary.data type:FT_CALLBACK
+KeyValuePair.key max_size:8
+KeyValuePair.stringValue max_size:8
+KeyValuePair.treeValue type:FT_CALLBACK
diff --git a/libs/nanopb/tests/cyclic_messages/encode_cyclic_callback.c b/libs/nanopb/tests/cyclic_messages/encode_cyclic_callback.c
new file mode 100644
index 00000000..7f67e70c
--- /dev/null
+++ b/libs/nanopb/tests/cyclic_messages/encode_cyclic_callback.c
@@ -0,0 +1,148 @@
+/* This program parses an input string in a format a bit like JSON:
+ * {'foobar': 1234, 'xyz': 'abc', 'tree': [[[1, 2], 3], [4, 5]]}
+ * and encodes it as protobuf
+ *
+ * Note: The string parsing here is not in any way intended to be robust
+ * nor safe against buffer overflows. It is just for this test.
+ */
+
+#include <pb_encode.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include "cyclic_callback.pb.h"
+
+static char *find_end_of_item(char *p)
+{
+ int depth = 0;
+ do {
+ if (*p == '[' || *p == '{') depth++;
+ if (*p == ']' || *p == '}') depth--;
+ p++;
+ } while (depth > 0 || (*p != ',' && *p != '}'));
+
+ if (*p == '}')
+ return p; /* End of parent dict */
+
+ p++;
+ while (*p == ' ') p++;
+ return p;
+}
+
+/* Parse a tree in format [[1 2] 3] and encode it directly to protobuf */
+static bool encode_tree(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ TreeNode tree = TreeNode_init_zero;
+ char *p = (char*)*arg;
+
+ if (*p == '[')
+ {
+ /* This is a tree branch */
+ p++;
+ tree.left.funcs.encode = encode_tree;
+ tree.left.arg = p;
+
+ p = find_end_of_item(p);
+ tree.right.funcs.encode = encode_tree;
+ tree.right.arg = p;
+ }
+ else
+ {
+ /* This is a leaf node */
+ tree.has_leaf = true;
+ tree.leaf = atoi(p);
+ }
+
+ return pb_encode_tag_for_field(stream, field) &&
+ pb_encode_submessage(stream, TreeNode_fields, &tree);
+}
+
+/* Parse a dictionary in format {'name': value} and encode it directly to protobuf */
+static bool encode_dictionary(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ int textlen;
+ char *p = (char*)*arg;
+ if (*p == '{') p++;
+ while (*p != '}')
+ {
+ KeyValuePair pair = KeyValuePair_init_zero;
+
+ if (*p != '\'')
+ PB_RETURN_ERROR(stream, "invalid key, missing quote");
+
+ p++; /* Starting quote of key */
+ textlen = strchr(p, '\'') - p;
+ strncpy(pair.key, p, textlen);
+ pair.key[textlen] = 0;
+ p += textlen + 2;
+
+ while (*p == ' ') p++;
+
+ if (*p == '[')
+ {
+ /* Value is a tree */
+ pair.treeValue.funcs.encode = encode_tree;
+ pair.treeValue.arg = p;
+ }
+ else if (*p == '\'')
+ {
+ /* Value is a string */
+ pair.has_stringValue = true;
+ p++;
+ textlen = strchr(p, '\'') - p;
+ strncpy(pair.stringValue, p, textlen);
+ pair.stringValue[textlen] = 0;
+ }
+ else if (*p == '{')
+ {
+ /* Value is a dictionary */
+ pair.has_dictValue = true;
+ pair.dictValue.dictItem.funcs.encode = encode_dictionary;
+ pair.dictValue.dictItem.arg = p;
+ }
+ else
+ {
+ /* Value is integer */
+ pair.has_intValue = true;
+ pair.intValue = atoi(p);
+ }
+
+ p = find_end_of_item(p);
+
+ if (!pb_encode_tag_for_field(stream, field))
+ return false;
+
+ if (!pb_encode_submessage(stream, KeyValuePair_fields, &pair))
+ return false;
+ }
+
+ return true;
+}
+
+
+int main(int argc, char *argv[])
+{
+ uint8_t buffer[256];
+ pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
+ Dictionary dict = Dictionary_init_zero;
+
+ if (argc <= 1)
+ {
+ fprintf(stderr, "Usage: %s \"{'foobar': 1234, ...}\"\n", argv[0]);
+ return 1;
+ }
+
+ dict.dictItem.funcs.encode = encode_dictionary;
+ dict.dictItem.arg = argv[1];
+
+ if (!pb_encode(&stream, Dictionary_fields, &dict))
+ {
+ fprintf(stderr, "Encoding error: %s\n", PB_GET_ERROR(&stream));
+ return 1;
+ }
+
+ fwrite(buffer, 1, stream.bytes_written, stdout);
+ return 0;
+}
+
+
diff --git a/libs/nanopb/tests/decode_unittests/SConscript b/libs/nanopb/tests/decode_unittests/SConscript
new file mode 100644
index 00000000..369b9dc7
--- /dev/null
+++ b/libs/nanopb/tests/decode_unittests/SConscript
@@ -0,0 +1,4 @@
+Import('env')
+p = env.Program(["decode_unittests.c", "$COMMON/unittestproto.pb.c"])
+env.RunTest(p)
+
diff --git a/libs/nanopb/tests/decode_unittests/decode_unittests.c b/libs/nanopb/tests/decode_unittests/decode_unittests.c
new file mode 100644
index 00000000..a6f5c17e
--- /dev/null
+++ b/libs/nanopb/tests/decode_unittests/decode_unittests.c
@@ -0,0 +1,388 @@
+/* This includes the whole .c file to get access to static functions. */
+#define PB_ENABLE_MALLOC
+#include "pb_common.c"
+#include "pb_decode.c"
+
+#include <stdio.h>
+#include <string.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));
+ TEST((s = S("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"),
+ !pb_decode_varint(&s, &u)));
+ }
+
+ {
+ pb_istream_t s;
+ uint32_t u;
+
+ COMMENT("Test pb_decode_varint32");
+ TEST((s = S("\x00"), pb_decode_varint32(&s, &u) && u == 0));
+ TEST((s = S("\x01"), pb_decode_varint32(&s, &u) && u == 1));
+ TEST((s = S("\xAC\x02"), pb_decode_varint32(&s, &u) && u == 300));
+ TEST((s = S("\xFF\xFF\xFF\xFF\x0F"), pb_decode_varint32(&s, &u) && u == UINT32_MAX));
+ TEST((s = S("\xFF\xFF\xFF\xFF\xFF\x01"), !pb_decode_varint32(&s, &u)));
+ }
+
+ {
+ 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\x00");
+ 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 = 0xFFFFFFFF;
+ f.data_size = 1;
+ TEST(pb_dec_varint(&s, &f, &d) && (d == 0xFFFFFF00 || d == 0x00FFFFFF))
+ }
+
+ {
+ 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};
+ int64_t d;
+
+ COMMENT("Test pb_dec_svarint using int64_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_SVARINT, 0, 0, 4, 0, 0};
+ int32_t d;
+
+ COMMENT("Test pb_dec_svarint overflow detection using int32_t");
+ TEST((s = S("\xfe\xff\xff\xff\x0f"), pb_dec_svarint(&s, &f, &d)));
+ TEST((s = S("\xfe\xff\xff\xff\x10"), !pb_dec_svarint(&s, &f, &d)));
+ TEST((s = S("\xff\xff\xff\xff\x0f"), pb_dec_svarint(&s, &f, &d)));
+ TEST((s = S("\xff\xff\xff\xff\x10"), !pb_dec_svarint(&s, &f, &d)));
+ }
+
+ {
+ pb_istream_t s;
+ pb_field_t f = {1, PB_LTYPE_SVARINT, 0, 0, 4, 0, 0};
+ uint32_t d;
+
+ COMMENT("Test pb_dec_uvarint using uint32_t")
+ TEST((s = S("\x01"), pb_dec_uvarint(&s, &f, &d) && d == 1))
+ TEST((s = S("\x02"), pb_dec_uvarint(&s, &f, &d) && d == 2))
+ TEST((s = S("\xff\xff\xff\xff\x0f"), pb_dec_uvarint(&s, &f, &d) && d == UINT32_MAX))
+ }
+
+ {
+ pb_istream_t s;
+ pb_field_t f = {1, PB_LTYPE_SVARINT, 0, 0, 8, 0, 0};
+ uint64_t d;
+
+ COMMENT("Test pb_dec_uvarint using uint64_t")
+ TEST((s = S("\x01"), pb_dec_uvarint(&s, &f, &d) && d == 1))
+ TEST((s = S("\x02"), pb_dec_uvarint(&s, &f, &d) && d == 2))
+ TEST((s = S("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"), pb_dec_uvarint(&s, &f, &d) && d == UINT64_MAX))
+ }
+
+ {
+ pb_istream_t s;
+ pb_field_t f = {1, PB_LTYPE_SVARINT, 0, 0, 4, 0, 0};
+ uint32_t d;
+
+ COMMENT("Test pb_dec_uvarint overflow detection using int32_t");
+ TEST((s = S("\xff\xff\xff\xff\x0f"), pb_dec_uvarint(&s, &f, &d)));
+ TEST((s = S("\xff\xff\xff\xff\x10"), !pb_dec_uvarint(&s, &f, &d)));
+ }
+
+ {
+ 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 { pb_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 { pb_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 = {{0}};
+
+ 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)
+ }
+
+ {
+ pb_istream_t s = {0};
+ void *data = NULL;
+
+ COMMENT("Testing allocate_field")
+ TEST(allocate_field(&s, &data, 10, 10) && data != NULL);
+ TEST(allocate_field(&s, &data, 10, 20) && data != NULL);
+
+ {
+ void *oldvalue = data;
+ size_t very_big = (size_t)-1;
+ size_t somewhat_big = very_big / 2 + 1;
+ size_t not_so_big = (size_t)1 << (4 * sizeof(size_t));
+
+ TEST(!allocate_field(&s, &data, very_big, 2) && data == oldvalue);
+ TEST(!allocate_field(&s, &data, somewhat_big, 2) && data == oldvalue);
+ TEST(!allocate_field(&s, &data, not_so_big, not_so_big) && data == oldvalue);
+ }
+
+ pb_free(data);
+ }
+
+ if (status != 0)
+ fprintf(stdout, "\n\nSome tests FAILED!\n");
+
+ return status;
+}
diff --git a/libs/nanopb/tests/encode_unittests/SConscript b/libs/nanopb/tests/encode_unittests/SConscript
new file mode 100644
index 00000000..bf6d1404
--- /dev/null
+++ b/libs/nanopb/tests/encode_unittests/SConscript
@@ -0,0 +1,5 @@
+# Build and run the stand-alone unit tests for the nanopb encoder part.
+
+Import('env')
+p = env.Program(["encode_unittests.c", "$COMMON/unittestproto.pb.c"])
+env.RunTest(p)
diff --git a/libs/nanopb/tests/encode_unittests/encode_unittests.c b/libs/nanopb/tests/encode_unittests/encode_unittests.c
new file mode 100644
index 00000000..583af5c6
--- /dev/null
+++ b/libs/nanopb/tests/encode_unittests/encode_unittests.c
@@ -0,0 +1,355 @@
+/* This includes the whole .c file to get access to static functions. */
+#include "pb_common.c"
+#include "pb_encode.c"
+
+#include <stdio.h>
+#include <string.h>
+#include "unittests.h"
+#include "unittestproto.pb.h"
+
+bool streamcallback(pb_ostream_t *stream, const uint8_t *buf, size_t count)
+{
+ /* Allow only 'x' to be written */
+ while (count--)
+ {
+ if (*buf++ != 'x')
+ return false;
+ }
+ return true;
+}
+
+bool fieldcallback(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ int value = 0x55;
+ if (!pb_encode_tag_for_field(stream, field))
+ return false;
+ return pb_encode_varint(stream, value);
+}
+
+bool crazyfieldcallback(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ /* This callback writes different amount of data the second time. */
+ uint32_t *state = (uint32_t*)arg;
+ *state <<= 8;
+ if (!pb_encode_tag_for_field(stream, field))
+ return false;
+ return pb_encode_varint(stream, *state);
+}
+
+/* Check that expression x writes data y.
+ * Y is a string, which may contain null bytes. Null terminator is ignored.
+ */
+#define WRITES(x, y) \
+memset(buffer, 0xAA, sizeof(buffer)), \
+s = pb_ostream_from_buffer(buffer, sizeof(buffer)), \
+(x) && \
+memcmp(buffer, y, sizeof(y) - 1) == 0 && \
+buffer[sizeof(y) - 1] == 0xAA
+
+int main()
+{
+ int status = 0;
+
+ {
+ uint8_t buffer1[] = "foobartest1234";
+ uint8_t buffer2[sizeof(buffer1)];
+ pb_ostream_t stream = pb_ostream_from_buffer(buffer2, sizeof(buffer1));
+
+ COMMENT("Test pb_write and pb_ostream_t");
+ TEST(pb_write(&stream, buffer1, sizeof(buffer1)));
+ TEST(memcmp(buffer1, buffer2, sizeof(buffer1)) == 0);
+ TEST(!pb_write(&stream, buffer1, 1));
+ TEST(stream.bytes_written == sizeof(buffer1));
+ }
+
+ {
+ uint8_t buffer1[] = "xxxxxxx";
+ pb_ostream_t stream = {&streamcallback, 0, SIZE_MAX, 0};
+
+ COMMENT("Test pb_write with custom callback");
+ TEST(pb_write(&stream, buffer1, 5));
+ buffer1[0] = 'a';
+ TEST(!pb_write(&stream, buffer1, 5));
+ }
+
+ {
+ uint8_t buffer[30];
+ pb_ostream_t s;
+
+ COMMENT("Test pb_encode_varint")
+ TEST(WRITES(pb_encode_varint(&s, 0), "\0"));
+ TEST(WRITES(pb_encode_varint(&s, 1), "\1"));
+ TEST(WRITES(pb_encode_varint(&s, 0x7F), "\x7F"));
+ TEST(WRITES(pb_encode_varint(&s, 0x80), "\x80\x01"));
+ TEST(WRITES(pb_encode_varint(&s, UINT32_MAX), "\xFF\xFF\xFF\xFF\x0F"));
+ TEST(WRITES(pb_encode_varint(&s, UINT64_MAX), "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"));
+ }
+
+ {
+ uint8_t buffer[30];
+ pb_ostream_t s;
+
+ COMMENT("Test pb_encode_tag")
+ TEST(WRITES(pb_encode_tag(&s, PB_WT_STRING, 5), "\x2A"));
+ TEST(WRITES(pb_encode_tag(&s, PB_WT_VARINT, 99), "\x98\x06"));
+ }
+
+ {
+ uint8_t buffer[30];
+ pb_ostream_t s;
+ pb_field_t field = {10, PB_LTYPE_SVARINT};
+
+ COMMENT("Test pb_encode_tag_for_field")
+ TEST(WRITES(pb_encode_tag_for_field(&s, &field), "\x50"));
+
+ field.type = PB_LTYPE_FIXED64;
+ TEST(WRITES(pb_encode_tag_for_field(&s, &field), "\x51"));
+
+ field.type = PB_LTYPE_STRING;
+ TEST(WRITES(pb_encode_tag_for_field(&s, &field), "\x52"));
+
+ field.type = PB_LTYPE_FIXED32;
+ TEST(WRITES(pb_encode_tag_for_field(&s, &field), "\x55"));
+ }
+
+ {
+ uint8_t buffer[30];
+ pb_ostream_t s;
+
+ COMMENT("Test pb_encode_string")
+ TEST(WRITES(pb_encode_string(&s, (const uint8_t*)"abcd", 4), "\x04""abcd"));
+ TEST(WRITES(pb_encode_string(&s, (const uint8_t*)"abcd\x00", 5), "\x05""abcd\x00"));
+ TEST(WRITES(pb_encode_string(&s, (const uint8_t*)"", 0), "\x00"));
+ }
+
+ {
+ uint8_t buffer[30];
+ pb_ostream_t s;
+ uint8_t value = 1;
+ int32_t max = INT32_MAX;
+ int32_t min = INT32_MIN;
+ int64_t lmax = INT64_MAX;
+ int64_t lmin = INT64_MIN;
+ pb_field_t field = {1, PB_LTYPE_VARINT, 0, 0, sizeof(value)};
+
+ COMMENT("Test pb_enc_varint and pb_enc_svarint")
+ TEST(WRITES(pb_enc_varint(&s, &field, &value), "\x01"));
+
+ field.data_size = sizeof(max);
+ TEST(WRITES(pb_enc_svarint(&s, &field, &max), "\xfe\xff\xff\xff\x0f"));
+ TEST(WRITES(pb_enc_svarint(&s, &field, &min), "\xff\xff\xff\xff\x0f"));
+
+ field.data_size = sizeof(lmax);
+ TEST(WRITES(pb_enc_svarint(&s, &field, &lmax), "\xFE\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"));
+ TEST(WRITES(pb_enc_svarint(&s, &field, &lmin), "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"));
+ }
+
+ {
+ uint8_t buffer[30];
+ pb_ostream_t s;
+ float fvalue;
+ double dvalue;
+
+ COMMENT("Test pb_enc_fixed32 using float")
+ fvalue = 0.0f;
+ TEST(WRITES(pb_enc_fixed32(&s, NULL, &fvalue), "\x00\x00\x00\x00"))
+ fvalue = 99.0f;
+ TEST(WRITES(pb_enc_fixed32(&s, NULL, &fvalue), "\x00\x00\xc6\x42"))
+ fvalue = -12345678.0f;
+ TEST(WRITES(pb_enc_fixed32(&s, NULL, &fvalue), "\x4e\x61\x3c\xcb"))
+
+ COMMENT("Test pb_enc_fixed64 using double")
+ dvalue = 0.0;
+ TEST(WRITES(pb_enc_fixed64(&s, NULL, &dvalue), "\x00\x00\x00\x00\x00\x00\x00\x00"))
+ dvalue = 99.0;
+ TEST(WRITES(pb_enc_fixed64(&s, NULL, &dvalue), "\x00\x00\x00\x00\x00\xc0\x58\x40"))
+ dvalue = -12345678.0;
+ TEST(WRITES(pb_enc_fixed64(&s, NULL, &dvalue), "\x00\x00\x00\xc0\x29\x8c\x67\xc1"))
+ }
+
+ {
+ uint8_t buffer[30];
+ pb_ostream_t s;
+ struct { pb_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, &BytesMessage_fields[0], &value), "\x05xyzzy"))
+ value.size = 0;
+ TEST(WRITES(pb_enc_bytes(&s, &BytesMessage_fields[0], &value), "\x00"))
+ }
+
+ {
+ uint8_t buffer[30];
+ pb_ostream_t s;
+ char value[30] = "xyzzy";
+
+ COMMENT("Test pb_enc_string")
+ TEST(WRITES(pb_enc_string(&s, &StringMessage_fields[0], &value), "\x05xyzzy"))
+ value[0] = '\0';
+ TEST(WRITES(pb_enc_string(&s, &StringMessage_fields[0], &value), "\x00"))
+ memset(value, 'x', 30);
+ TEST(WRITES(pb_enc_string(&s, &StringMessage_fields[0], &value), "\x0Axxxxxxxxxx"))
+ }
+
+ {
+ uint8_t buffer[10];
+ pb_ostream_t s;
+ IntegerArray msg = {5, {1, 2, 3, 4, 5}};
+
+ COMMENT("Test pb_encode with int32 array")
+
+ TEST(WRITES(pb_encode(&s, IntegerArray_fields, &msg), "\x0A\x05\x01\x02\x03\x04\x05"))
+
+ msg.data_count = 0;
+ TEST(WRITES(pb_encode(&s, IntegerArray_fields, &msg), ""))
+
+ msg.data_count = 10;
+ TEST(!pb_encode(&s, IntegerArray_fields, &msg))
+ }
+
+ {
+ uint8_t buffer[10];
+ pb_ostream_t s;
+ FloatArray msg = {1, {99.0f}};
+
+ COMMENT("Test pb_encode with float array")
+
+ TEST(WRITES(pb_encode(&s, FloatArray_fields, &msg),
+ "\x0A\x04\x00\x00\xc6\x42"))
+
+ msg.data_count = 0;
+ TEST(WRITES(pb_encode(&s, FloatArray_fields, &msg), ""))
+
+ msg.data_count = 3;
+ TEST(!pb_encode(&s, FloatArray_fields, &msg))
+ }
+
+ {
+ uint8_t buffer[50];
+ pb_ostream_t s;
+ FloatArray msg = {1, {99.0f}};
+
+ COMMENT("Test array size limit in pb_encode")
+
+ s = pb_ostream_from_buffer(buffer, sizeof(buffer));
+ TEST((msg.data_count = 10) && pb_encode(&s, FloatArray_fields, &msg))
+
+ s = pb_ostream_from_buffer(buffer, sizeof(buffer));
+ TEST((msg.data_count = 11) && !pb_encode(&s, FloatArray_fields, &msg))
+ }
+
+ {
+ uint8_t buffer[10];
+ pb_ostream_t s;
+ CallbackArray msg;
+
+ msg.data.funcs.encode = &fieldcallback;
+
+ COMMENT("Test pb_encode with callback field.")
+ TEST(WRITES(pb_encode(&s, CallbackArray_fields, &msg), "\x08\x55"))
+ }
+
+ {
+ uint8_t buffer[10];
+ pb_ostream_t s;
+ IntegerContainer msg = {{5, {1,2,3,4,5}}};
+
+ COMMENT("Test pb_encode with packed array in a submessage.")
+ TEST(WRITES(pb_encode(&s, IntegerContainer_fields, &msg),
+ "\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;
+ IntegerContainer msg = {{5, {1,2,3,4,5}}};
+
+ COMMENT("Test pb_encode_delimited.")
+ TEST(WRITES(pb_encode_delimited(&s, IntegerContainer_fields, &msg),
+ "\x09\x0A\x07\x0A\x05\x01\x02\x03\x04\x05"))
+ }
+
+ {
+ IntegerContainer msg = {{5, {1,2,3,4,5}}};
+ size_t size;
+
+ COMMENT("Test pb_get_encoded_size.")
+ TEST(pb_get_encoded_size(&size, IntegerContainer_fields, &msg) &&
+ size == 9);
+ }
+
+ {
+ uint8_t buffer[10];
+ pb_ostream_t s;
+ CallbackContainer msg;
+ CallbackContainerContainer msg2;
+ uint32_t state = 1;
+
+ msg.submsg.data.funcs.encode = &fieldcallback;
+ msg2.submsg.submsg.data.funcs.encode = &fieldcallback;
+
+ COMMENT("Test pb_encode with callback field in a submessage.")
+ TEST(WRITES(pb_encode(&s, CallbackContainer_fields, &msg), "\x0A\x02\x08\x55"))
+ TEST(WRITES(pb_encode(&s, CallbackContainerContainer_fields, &msg2),
+ "\x0A\x04\x0A\x02\x08\x55"))
+
+ /* Misbehaving callback: varying output between calls */
+ msg.submsg.data.funcs.encode = &crazyfieldcallback;
+ msg.submsg.data.arg = &state;
+ msg2.submsg.submsg.data.funcs.encode = &crazyfieldcallback;
+ msg2.submsg.submsg.data.arg = &state;
+
+ TEST(!pb_encode(&s, CallbackContainer_fields, &msg))
+ state = 1;
+ TEST(!pb_encode(&s, CallbackContainerContainer_fields, &msg2))
+ }
+
+ {
+ uint8_t buffer[StringMessage_size];
+ pb_ostream_t s;
+ StringMessage msg = {"0123456789"};
+
+ s = pb_ostream_from_buffer(buffer, sizeof(buffer));
+
+ COMMENT("Test that StringMessage_size is correct")
+
+ TEST(pb_encode(&s, StringMessage_fields, &msg));
+ TEST(s.bytes_written == StringMessage_size);
+ }
+
+ {
+ uint8_t buffer[128];
+ pb_ostream_t s;
+ StringPointerContainer msg = StringPointerContainer_init_zero;
+ char *strs[1] = {NULL};
+ char zstr[] = "Z";
+
+ COMMENT("Test string pointer encoding.");
+
+ msg.rep_str = strs;
+ msg.rep_str_count = 1;
+ TEST(WRITES(pb_encode(&s, StringPointerContainer_fields, &msg), "\x0a\x00"))
+
+ strs[0] = zstr;
+ TEST(WRITES(pb_encode(&s, StringPointerContainer_fields, &msg), "\x0a\x01Z"))
+ }
+
+ if (status != 0)
+ fprintf(stdout, "\n\nSome tests FAILED!\n");
+
+ return status;
+}
diff --git a/libs/nanopb/tests/enum_sizes/SConscript b/libs/nanopb/tests/enum_sizes/SConscript
new file mode 100644
index 00000000..048592ed
--- /dev/null
+++ b/libs/nanopb/tests/enum_sizes/SConscript
@@ -0,0 +1,12 @@
+# Test that different sizes of enum fields are properly encoded and decoded.
+
+Import('env')
+
+env.NanopbProto('enumsizes')
+
+p = env.Program(["enumsizes_unittests.c",
+ "enumsizes.pb.c",
+ "$COMMON/pb_encode.o",
+ "$COMMON/pb_decode.o",
+ "$COMMON/pb_common.o"])
+env.RunTest(p)
diff --git a/libs/nanopb/tests/enum_sizes/enumsizes.proto b/libs/nanopb/tests/enum_sizes/enumsizes.proto
new file mode 100644
index 00000000..a85d4160
--- /dev/null
+++ b/libs/nanopb/tests/enum_sizes/enumsizes.proto
@@ -0,0 +1,86 @@
+/* Test handling of enums with different value ranges.
+ * Depending on compiler and the packed_enum setting, the datatypes
+ * for enums can be either signed or unsigned. In past this has caused
+ * a bit of a problem for the encoder/decoder (issue #164).
+ */
+
+syntax = "proto2";
+
+import 'nanopb.proto';
+
+option (nanopb_fileopt).long_names = false;
+
+enum UnpackedUint8 {
+ option (nanopb_enumopt).packed_enum = false;
+ UU8_MIN = 0;
+ UU8_MAX = 255;
+}
+
+enum PackedUint8 {
+ option (nanopb_enumopt).packed_enum = true;
+ PU8_MIN = 0;
+ PU8_MAX = 255;
+}
+
+enum UnpackedInt8 {
+ option (nanopb_enumopt).packed_enum = false;
+ UI8_MIN = -128;
+ UI8_MAX = 127;
+}
+
+enum PackedInt8 {
+ option (nanopb_enumopt).packed_enum = true;
+ PI8_MIN = -128;
+ PI8_MAX = 127;
+}
+
+enum UnpackedUint16 {
+ option (nanopb_enumopt).packed_enum = false;
+ UU16_MIN = 0;
+ UU16_MAX = 65535;
+}
+
+enum PackedUint16 {
+ option (nanopb_enumopt).packed_enum = true;
+ PU16_MIN = 0;
+ PU16_MAX = 65535;
+}
+
+enum UnpackedInt16 {
+ option (nanopb_enumopt).packed_enum = false;
+ UI16_MIN = -32768;
+ UI16_MAX = 32767;
+}
+
+enum PackedInt16 {
+ option (nanopb_enumopt).packed_enum = true;
+ PI16_MIN = -32768;
+ PI16_MAX = 32767;
+}
+
+/* Protobuf supports enums up to 32 bits.
+ * The 32 bit case is covered by HugeEnum in the alltypes test.
+ */
+
+message PackedEnums {
+ required PackedUint8 u8_min = 1;
+ required PackedUint8 u8_max = 2;
+ required PackedInt8 i8_min = 3;
+ required PackedInt8 i8_max = 4;
+ required PackedUint16 u16_min = 5;
+ required PackedUint16 u16_max = 6;
+ required PackedInt16 i16_min = 7;
+ required PackedInt16 i16_max = 8;
+}
+
+message UnpackedEnums {
+ required UnpackedUint8 u8_min = 1;
+ required UnpackedUint8 u8_max = 2;
+ required UnpackedInt8 i8_min = 3;
+ required UnpackedInt8 i8_max = 4;
+ required UnpackedUint16 u16_min = 5;
+ required UnpackedUint16 u16_max = 6;
+ required UnpackedInt16 i16_min = 7;
+ required UnpackedInt16 i16_max = 8;
+}
+
diff --git a/libs/nanopb/tests/enum_sizes/enumsizes_unittests.c b/libs/nanopb/tests/enum_sizes/enumsizes_unittests.c
new file mode 100644
index 00000000..5606895a
--- /dev/null
+++ b/libs/nanopb/tests/enum_sizes/enumsizes_unittests.c
@@ -0,0 +1,72 @@
+#include <stdio.h>
+#include <string.h>
+#include <pb_decode.h>
+#include <pb_encode.h>
+#include "unittests.h"
+#include "enumsizes.pb.h"
+
+int main()
+{
+ int status = 0;
+
+ UnpackedEnums msg1 = {
+ UU8_MIN, UU8_MAX,
+ UI8_MIN, UI8_MAX,
+ UU16_MIN, UU16_MAX,
+ UI16_MIN, UI16_MAX,
+ };
+
+ PackedEnums msg2;
+ UnpackedEnums msg3;
+ uint8_t buf[256];
+ size_t msgsize;
+
+ COMMENT("Step 1: unpacked enums -> protobuf");
+ {
+ pb_ostream_t s = pb_ostream_from_buffer(buf, sizeof(buf));
+ TEST(pb_encode(&s, UnpackedEnums_fields, &msg1));
+ msgsize = s.bytes_written;
+ }
+
+ COMMENT("Step 2: protobuf -> packed enums");
+ {
+ pb_istream_t s = pb_istream_from_buffer(buf, msgsize);
+ TEST(pb_decode(&s, PackedEnums_fields, &msg2));
+
+ TEST(msg1.u8_min == (int)msg2.u8_min);
+ TEST(msg1.u8_max == (int)msg2.u8_max);
+ TEST(msg1.i8_min == (int)msg2.i8_min);
+ TEST(msg1.i8_max == (int)msg2.i8_max);
+ TEST(msg1.u16_min == (int)msg2.u16_min);
+ TEST(msg1.u16_max == (int)msg2.u16_max);
+ TEST(msg1.i16_min == (int)msg2.i16_min);
+ TEST(msg1.i16_max == (int)msg2.i16_max);
+ }
+
+ COMMENT("Step 3: packed enums -> protobuf");
+ {
+ pb_ostream_t s = pb_ostream_from_buffer(buf, sizeof(buf));
+ TEST(pb_encode(&s, PackedEnums_fields, &msg2));
+ msgsize = s.bytes_written;
+ }
+
+ COMMENT("Step 4: protobuf -> unpacked enums");
+ {
+ pb_istream_t s = pb_istream_from_buffer(buf, msgsize);
+ TEST(pb_decode(&s, UnpackedEnums_fields, &msg3));
+
+ TEST(msg1.u8_min == (int)msg3.u8_min);
+ TEST(msg1.u8_max == (int)msg3.u8_max);
+ TEST(msg1.i8_min == (int)msg3.i8_min);
+ TEST(msg1.i8_max == (int)msg3.i8_max);
+ TEST(msg1.u16_min == (int)msg2.u16_min);
+ TEST(msg1.u16_max == (int)msg2.u16_max);
+ TEST(msg1.i16_min == (int)msg2.i16_min);
+ TEST(msg1.i16_max == (int)msg2.i16_max);
+ }
+
+ if (status != 0)
+ fprintf(stdout, "\n\nSome tests FAILED!\n");
+
+ return status;
+}
diff --git a/libs/nanopb/tests/enum_to_string/SConscript b/libs/nanopb/tests/enum_to_string/SConscript
new file mode 100644
index 00000000..e86fcca0
--- /dev/null
+++ b/libs/nanopb/tests/enum_to_string/SConscript
@@ -0,0 +1,7 @@
+# Test enum to string functionality
+
+Import('env')
+env.NanopbProto("enum.proto")
+p = env.Program(["enum_to_string.c", "enum.pb.c"])
+env.RunTest(p)
+
diff --git a/libs/nanopb/tests/enum_to_string/enum.proto b/libs/nanopb/tests/enum_to_string/enum.proto
new file mode 100644
index 00000000..07c67363
--- /dev/null
+++ b/libs/nanopb/tests/enum_to_string/enum.proto
@@ -0,0 +1,19 @@
+/* Test enum to string function generation */
+
+syntax = "proto2";
+
+import "nanopb.proto";
+
+option (nanopb_fileopt).enum_to_string = true;
+
+enum MyEnum {
+ VALUE1 = 1;
+ VALUE2 = 2;
+ VALUE15 = 15;
+}
+
+enum MyShortNameEnum {
+ option (nanopb_enumopt).long_names = false;
+ MSNE_VALUE256 = 256;
+}
+
diff --git a/libs/nanopb/tests/enum_to_string/enum_to_string.c b/libs/nanopb/tests/enum_to_string/enum_to_string.c
new file mode 100644
index 00000000..c4fb31d4
--- /dev/null
+++ b/libs/nanopb/tests/enum_to_string/enum_to_string.c
@@ -0,0 +1,19 @@
+#include <stdio.h>
+#include "unittests.h"
+#include "enum.pb.h"
+
+int main()
+{
+ int status = 0;
+ TEST(strcmp(MyEnum_name(MyEnum_VALUE1), "VALUE1") == 0);
+ TEST(strcmp(MyEnum_name(MyEnum_VALUE2), "VALUE2") == 0);
+ TEST(strcmp(MyEnum_name(MyEnum_VALUE15), "VALUE15") == 0);
+ TEST(strcmp(MyShortNameEnum_name(MSNE_VALUE256), "MSNE_VALUE256") == 0);
+ TEST(strcmp(MyShortNameEnum_name(9999), "unknown") == 0);
+
+ if (status != 0)
+ fprintf(stdout, "\n\nSome tests FAILED!\n");
+
+ return status;
+}
+
diff --git a/libs/nanopb/tests/extensions/SConscript b/libs/nanopb/tests/extensions/SConscript
new file mode 100644
index 00000000..a2c87428
--- /dev/null
+++ b/libs/nanopb/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 = '$BUILD/alltypes')
+incpath.Append(CPPPATH = '$BUILD/alltypes')
+
+incpath.NanopbProto(["extensions", "extensions.options"])
+enc = incpath.Program(["encode_extensions.c", "extensions.pb.c", "$BUILD/alltypes/alltypes.pb$OBJSUFFIX", "$COMMON/pb_encode.o", "$COMMON/pb_common.o"])
+dec = incpath.Program(["decode_extensions.c", "extensions.pb.c", "$BUILD/alltypes/alltypes.pb$OBJSUFFIX", "$COMMON/pb_decode.o", "$COMMON/pb_common.o"])
+
+env.RunTest(enc)
+env.RunTest([dec, "encode_extensions.output"])
+
diff --git a/libs/nanopb/tests/extensions/decode_extensions.c b/libs/nanopb/tests/extensions/decode_extensions.c
new file mode 100644
index 00000000..e4374380
--- /dev/null
+++ b/libs/nanopb/tests/extensions/decode_extensions.c
@@ -0,0 +1,60 @@
+/* Test decoding of extension fields. */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <pb_decode.h>
+#include "alltypes.pb.h"
+#include "extensions.pb.h"
+#include "test_helpers.h"
+
+#define TEST(x) if (!(x)) { \
+ printf("Test " #x " failed.\n"); \
+ return 2; \
+ }
+
+int main(int argc, char **argv)
+{
+ uint8_t buffer[1024];
+ size_t count;
+ pb_istream_t stream;
+
+ AllTypes alltypes = {0};
+ int32_t extensionfield1;
+ 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(ext1.found)
+ TEST(extensionfield1 == 12345)
+ TEST(ext2.found)
+ TEST(strcmp(extensionfield2.test1, "test") == 0)
+ TEST(extensionfield2.test2 == 54321)
+
+ return 0;
+}
+
diff --git a/libs/nanopb/tests/extensions/encode_extensions.c b/libs/nanopb/tests/extensions/encode_extensions.c
new file mode 100644
index 00000000..00745826
--- /dev/null
+++ b/libs/nanopb/tests/extensions/encode_extensions.c
@@ -0,0 +1,54 @@
+/* Tests extension fields.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pb_encode.h>
+#include "alltypes.pb.h"
+#include "extensions.pb.h"
+#include "test_helpers.h"
+
+int main(int argc, char **argv)
+{
+ uint8_t buffer[1024];
+ pb_ostream_t stream;
+
+ AllTypes alltypes = {0};
+ int32_t extensionfield1 = 12345;
+ pb_extension_t ext1;
+ ExtensionMessage extensionfield2 = {"test", 54321};
+ pb_extension_t ext2;
+
+ /* Set up 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;
+
+ /* Set up the output stream */
+ stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
+
+ /* 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 */
+ }
+ else
+ {
+ fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream));
+ return 1; /* Failure */
+ }
+
+ /* Check that the field tags are properly generated */
+ (void)AllTypes_extensionfield1_tag;
+ (void)ExtensionMessage_AllTypes_extensionfield2_tag;
+}
+
diff --git a/libs/nanopb/tests/extensions/extensions.options b/libs/nanopb/tests/extensions/extensions.options
new file mode 100644
index 00000000..a5cd61dd
--- /dev/null
+++ b/libs/nanopb/tests/extensions/extensions.options
@@ -0,0 +1 @@
+* max_size:16
diff --git a/libs/nanopb/tests/extensions/extensions.proto b/libs/nanopb/tests/extensions/extensions.proto
new file mode 100644
index 00000000..fcd5b43b
--- /dev/null
+++ b/libs/nanopb/tests/extensions/extensions.proto
@@ -0,0 +1,19 @@
+syntax = "proto2";
+
+import 'alltypes.proto';
+
+extend AllTypes {
+ optional int32 AllTypes_extensionfield1 = 255 [default = 5];
+}
+
+message ExtensionMessage {
+ extend AllTypes {
+ optional ExtensionMessage AllTypes_extensionfield2 = 254;
+ // required ExtensionMessage AllTypes_extensionfield3 = 253; // No longer allowed by protobuf 3
+ repeated ExtensionMessage AllTypes_extensionfield4 = 252;
+ }
+
+ required string test1 = 1;
+ required int32 test2 = 2;
+}
+
diff --git a/libs/nanopb/tests/extra_fields/SConscript b/libs/nanopb/tests/extra_fields/SConscript
new file mode 100644
index 00000000..75ac5c5e
--- /dev/null
+++ b/libs/nanopb/tests/extra_fields/SConscript
@@ -0,0 +1,16 @@
+# Test that the decoder properly handles unknown fields in the input.
+
+Import("env")
+
+dec = env.GetBuildPath('$BUILD/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"])
+
+dec = env.GetBuildPath('$BUILD/basic_stream/${PROGPREFIX}decode_stream${PROGSUFFIX}')
+env.RunTest('person_with_extra_field_stream.output', [dec, "person_with_extra_field.pb"])
+env.Compare(["person_with_extra_field_stream.output", "person_with_extra_field.expected"])
+
+# This uses the backwards compatibility alltypes test, so that
+# alltypes_with_extra_fields.pb doesn't have to be remade so often.
+dec2 = env.GetBuildPath('$BUILD/backwards_compatibility/${PROGPREFIX}decode_legacy${PROGSUFFIX}')
+env.RunTest('alltypes_with_extra_fields.output', [dec2, 'alltypes_with_extra_fields.pb'])
diff --git a/libs/nanopb/tests/extra_fields/alltypes_with_extra_fields.pb b/libs/nanopb/tests/extra_fields/alltypes_with_extra_fields.pb
new file mode 100644
index 00000000..f9f53941
--- /dev/null
+++ b/libs/nanopb/tests/extra_fields/alltypes_with_extra_fields.pb
Binary files differ
diff --git a/libs/nanopb/tests/extra_fields/person_with_extra_field.expected b/libs/nanopb/tests/extra_fields/person_with_extra_field.expected
new file mode 100644
index 00000000..da9c32df
--- /dev/null
+++ b/libs/nanopb/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/libs/nanopb/tests/extra_fields/person_with_extra_field.pb b/libs/nanopb/tests/extra_fields/person_with_extra_field.pb
new file mode 100644
index 00000000..ffb303dd
--- /dev/null
+++ b/libs/nanopb/tests/extra_fields/person_with_extra_field.pb
Binary files differ
diff --git a/libs/nanopb/tests/field_size_16/SConscript b/libs/nanopb/tests/field_size_16/SConscript
new file mode 100644
index 00000000..ffb29c4e
--- /dev/null
+++ b/libs/nanopb/tests/field_size_16/SConscript
@@ -0,0 +1,29 @@
+# Run the alltypes test case, but compile with PB_FIELD_16BIT=1.
+# Also the .proto file has been modified to have high indexes.
+
+Import("env")
+
+# Take copy of the files for custom build.
+c = Copy("$TARGET", "$SOURCE")
+env.Command("encode_alltypes.c", "$BUILD/alltypes/encode_alltypes.c", c)
+env.Command("decode_alltypes.c", "$BUILD/alltypes/decode_alltypes.c", c)
+
+env.NanopbProto(["alltypes", "alltypes.options"])
+
+# Define the compilation options
+opts = env.Clone()
+opts.Append(CPPDEFINES = {'PB_FIELD_16BIT': 1})
+
+# Build new version of core
+strict = opts.Clone()
+strict.Append(CFLAGS = strict['CORECFLAGS'])
+strict.Object("pb_decode_fields16.o", "$NANOPB/pb_decode.c")
+strict.Object("pb_encode_fields16.o", "$NANOPB/pb_encode.c")
+strict.Object("pb_common_fields16.o", "$NANOPB/pb_common.c")
+
+# Now build and run the test normally.
+enc = opts.Program(["encode_alltypes.c", "alltypes.pb.c", "pb_encode_fields16.o", "pb_common_fields16.o"])
+dec = opts.Program(["decode_alltypes.c", "alltypes.pb.c", "pb_decode_fields16.o", "pb_common_fields16.o"])
+
+env.RunTest(enc)
+env.RunTest([dec, "encode_alltypes.output"])
diff --git a/libs/nanopb/tests/field_size_16/alltypes.options b/libs/nanopb/tests/field_size_16/alltypes.options
new file mode 100644
index 00000000..78dd08d1
--- /dev/null
+++ b/libs/nanopb/tests/field_size_16/alltypes.options
@@ -0,0 +1,4 @@
+* max_size:16
+* max_count:5
+*.*fbytes fixed_length:true max_size:4
+
diff --git a/libs/nanopb/tests/field_size_16/alltypes.proto b/libs/nanopb/tests/field_size_16/alltypes.proto
new file mode 100644
index 00000000..46ac46af
--- /dev/null
+++ b/libs/nanopb/tests/field_size_16/alltypes.proto
@@ -0,0 +1,123 @@
+syntax = "proto2";
+
+message SubMessage {
+ required string substuff1 = 1 [default = "1"];
+ required int32 substuff2 = 2 [default = 2];
+ optional fixed32 substuff3 = 65535 [default = 3];
+}
+
+message EmptyMessage {
+
+}
+
+enum HugeEnum {
+ Negative = -2147483647; /* protoc doesn't accept -2147483648 here */
+ Positive = 2147483647;
+}
+
+message Limits {
+ required int32 int32_min = 1;
+ required int32 int32_max = 2;
+ required uint32 uint32_min = 3;
+ required uint32 uint32_max = 4;
+ required int64 int64_min = 5;
+ required int64 int64_max = 6;
+ required uint64 uint64_min = 7;
+ required uint64 uint64_max = 8;
+ required HugeEnum enum_min = 9;
+ required HugeEnum enum_max = 10;
+}
+
+enum MyEnum {
+ Zero = 0;
+ 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;
+ required bytes req_bytes = 15;
+ required SubMessage req_submsg = 16;
+ required MyEnum req_enum = 17;
+ required EmptyMessage req_emptymsg = 18;
+ required bytes req_fbytes = 19;
+
+ repeated int32 rep_int32 = 21;
+ repeated int64 rep_int64 = 22;
+ repeated uint32 rep_uint32 = 23;
+ repeated uint64 rep_uint64 = 24;
+ repeated sint32 rep_sint32 = 25;
+ repeated sint64 rep_sint64 = 26;
+ repeated bool rep_bool = 27;
+
+ repeated fixed32 rep_fixed32 = 28;
+ repeated sfixed32 rep_sfixed32= 29;
+ repeated float rep_float = 30;
+
+ repeated fixed64 rep_fixed64 = 10031;
+ repeated sfixed64 rep_sfixed64= 10032;
+ repeated double rep_double = 10033;
+
+ repeated string rep_string = 10034;
+ repeated bytes rep_bytes = 10035;
+ repeated SubMessage rep_submsg = 10036;
+ repeated MyEnum rep_enum = 10037;
+ repeated EmptyMessage rep_emptymsg = 10038;
+ repeated bytes rep_fbytes = 10039;
+
+ optional int32 opt_int32 = 10041 [default = 4041];
+ optional int64 opt_int64 = 10042 [default = 4042];
+ optional uint32 opt_uint32 = 10043 [default = 4043];
+ optional uint64 opt_uint64 = 10044 [default = 4044];
+ optional sint32 opt_sint32 = 10045 [default = 4045];
+ optional sint64 opt_sint64 = 10046 [default = 4046];
+ optional bool opt_bool = 10047 [default = false];
+
+ optional fixed32 opt_fixed32 = 10048 [default = 4048];
+ optional sfixed32 opt_sfixed32= 10049 [default = 4049];
+ optional float opt_float = 10050 [default = 4050];
+
+ optional fixed64 opt_fixed64 = 10051 [default = 4051];
+ optional sfixed64 opt_sfixed64= 10052 [default = 4052];
+ optional double opt_double = 10053 [default = 4053];
+
+ optional string opt_string = 10054 [default = "4054"];
+ optional bytes opt_bytes = 10055 [default = "4055"];
+ optional SubMessage opt_submsg = 10056;
+ optional MyEnum opt_enum = 10057 [default = Second];
+ optional EmptyMessage opt_emptymsg = 10058;
+ optional bytes opt_fbytes = 10059 [default = "4059"];
+
+ oneof oneof
+ {
+ SubMessage oneof_msg1 = 10060;
+ EmptyMessage oneof_msg2 = 10061;
+ }
+
+ // Check that extreme integer values are handled correctly
+ required Limits req_limits = 98;
+
+ // 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 = 10099;
+
+ extensions 200 to 255;
+}
+
diff --git a/libs/nanopb/tests/field_size_16_proto3/SConscript b/libs/nanopb/tests/field_size_16_proto3/SConscript
new file mode 100644
index 00000000..912c0389
--- /dev/null
+++ b/libs/nanopb/tests/field_size_16_proto3/SConscript
@@ -0,0 +1,34 @@
+# Version of AllTypes test case for protobuf 3 file format.
+
+Import("env")
+
+import re
+match = None
+if 'PROTOC_VERSION' in env:
+ match = re.search('([0-9]+).([0-9]+).([0-9]+)', env['PROTOC_VERSION'])
+
+if match:
+ version = map(int, match.groups())
+
+# proto3 syntax is supported by protoc >= 3.0.0
+if env.GetOption('clean') or (match and version[0] >= 3):
+
+ env.NanopbProto(["alltypes", "alltypes.options"])
+
+ # Define the compilation options
+ opts = env.Clone()
+ opts.Append(CPPDEFINES = {'PB_FIELD_16BIT': 1})
+
+ # Build new version of core
+ strict = opts.Clone()
+ strict.Append(CFLAGS = strict['CORECFLAGS'])
+ strict.Object("pb_decode_fields16.o", "$NANOPB/pb_decode.c")
+ strict.Object("pb_encode_fields16.o", "$NANOPB/pb_encode.c")
+ strict.Object("pb_common_fields16.o", "$NANOPB/pb_common.c")
+
+ # Now build and run the test normally.
+ enc = opts.Program(["encode_alltypes.c", "alltypes.pb.c", "pb_encode_fields16.o", "pb_common_fields16.o"])
+ dec = opts.Program(["decode_alltypes.c", "alltypes.pb.c", "pb_decode_fields16.o", "pb_common_fields16.o"])
+
+ env.RunTest(enc)
+ env.RunTest([dec, "encode_alltypes.output"])
diff --git a/libs/nanopb/tests/field_size_16_proto3/alltypes.options b/libs/nanopb/tests/field_size_16_proto3/alltypes.options
new file mode 100644
index 00000000..edfbe788
--- /dev/null
+++ b/libs/nanopb/tests/field_size_16_proto3/alltypes.options
@@ -0,0 +1,4 @@
+* max_size:16
+* max_count:5
+*.*fbytes fixed_length:true max_size:4
+SubMessage.substuff1 max_size:256
diff --git a/libs/nanopb/tests/field_size_16_proto3/alltypes.proto b/libs/nanopb/tests/field_size_16_proto3/alltypes.proto
new file mode 100644
index 00000000..f66109ec
--- /dev/null
+++ b/libs/nanopb/tests/field_size_16_proto3/alltypes.proto
@@ -0,0 +1,100 @@
+syntax = "proto3";
+// package name placeholder
+
+message SubMessage {
+ string substuff1 = 1;
+ int32 substuff2 = 2;
+ fixed32 substuff3 = 3;
+}
+
+message EmptyMessage {
+
+}
+
+enum HugeEnum {
+ HE_Zero = 0;
+ Negative = -2147483647; /* protoc doesn't accept -2147483648 here */
+ Positive = 2147483647;
+}
+
+message Limits {
+ int32 int32_min = 1;
+ int32 int32_max = 2;
+ uint32 uint32_min = 3;
+ uint32 uint32_max = 4;
+ int64 int64_min = 5;
+ int64 int64_max = 6;
+ uint64 uint64_min = 7;
+ uint64 uint64_max = 8;
+ HugeEnum enum_min = 9;
+ HugeEnum enum_max = 10;
+}
+
+enum MyEnum {
+ Zero = 0;
+ First = 1;
+ Second = 2;
+ Truth = 42;
+}
+
+message AllTypes {
+ int32 sng_int32 = 1;
+ int64 sng_int64 = 2;
+ uint32 sng_uint32 = 3;
+ uint64 sng_uint64 = 4;
+ sint32 sng_sint32 = 5;
+ sint64 sng_sint64 = 6;
+ bool sng_bool = 7;
+
+ fixed32 sng_fixed32 = 8;
+ sfixed32 sng_sfixed32= 9;
+ float sng_float = 10;
+
+ fixed64 sng_fixed64 = 11;
+ sfixed64 sng_sfixed64= 12;
+ double sng_double = 13;
+
+ string sng_string = 14;
+ bytes sng_bytes = 15;
+ SubMessage sng_submsg = 16;
+ MyEnum sng_enum = 17;
+ EmptyMessage sng_emptymsg = 18;
+ bytes sng_fbytes = 19;
+
+ repeated int32 rep_int32 = 21 [packed = true];
+ repeated int64 rep_int64 = 22 [packed = true];
+ repeated uint32 rep_uint32 = 23 [packed = true];
+ repeated uint64 rep_uint64 = 24 [packed = true];
+ repeated sint32 rep_sint32 = 25 [packed = true];
+ repeated sint64 rep_sint64 = 26 [packed = true];
+ repeated bool rep_bool = 27 [packed = true];
+
+ repeated fixed32 rep_fixed32 = 28 [packed = true];
+ repeated sfixed32 rep_sfixed32= 29 [packed = true];
+ repeated float rep_float = 30 [packed = true];
+
+ repeated fixed64 rep_fixed64 = 31 [packed = true];
+ repeated sfixed64 rep_sfixed64= 32 [packed = true];
+ repeated double rep_double = 33 [packed = true];
+
+ repeated string rep_string = 34;
+ repeated bytes rep_bytes = 35;
+ repeated SubMessage rep_submsg = 36;
+ repeated MyEnum rep_enum = 37 [packed = true];
+ repeated EmptyMessage rep_emptymsg = 38;
+ repeated bytes rep_fbytes = 39;
+
+ oneof oneof
+ {
+ SubMessage oneof_msg1 = 59;
+ EmptyMessage oneof_msg2 = 60;
+ }
+
+ // Check that extreme integer values are handled correctly
+ Limits req_limits = 98;
+
+ // 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.
+ int32 end = 99;
+}
+
diff --git a/libs/nanopb/tests/field_size_16_proto3/decode_alltypes.c b/libs/nanopb/tests/field_size_16_proto3/decode_alltypes.c
new file mode 100644
index 00000000..6611f8cc
--- /dev/null
+++ b/libs/nanopb/tests/field_size_16_proto3/decode_alltypes.c
@@ -0,0 +1,167 @@
+/* Tests the decoding of all types.
+ * This is the counterpart of test_encode3.
+ * Run e.g. ./test_encode3 | ./test_decode3
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <pb_decode.h>
+#include "alltypes.pb.h"
+#include "test_helpers.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, int mode)
+{
+ AllTypes alltypes = AllTypes_init_zero;
+
+ /* Fill with garbage to better detect initialization errors */
+ memset(&alltypes, 0xAA, sizeof(alltypes));
+
+ if (!pb_decode(stream, AllTypes_fields, &alltypes))
+ return false;
+
+ TEST(alltypes.rep_int32_count == 5 && alltypes.rep_int32[4] == -2001 && alltypes.rep_int32[0] == 0);
+ TEST(alltypes.rep_int64_count == 5 && alltypes.rep_int64[4] == -2002 && alltypes.rep_int64[0] == 0);
+ TEST(alltypes.rep_uint32_count == 5 && alltypes.rep_uint32[4] == 2003 && alltypes.rep_uint32[0] == 0);
+ TEST(alltypes.rep_uint64_count == 5 && alltypes.rep_uint64[4] == 2004 && alltypes.rep_uint64[0] == 0);
+ TEST(alltypes.rep_sint32_count == 5 && alltypes.rep_sint32[4] == -2005 && alltypes.rep_sint32[0] == 0);
+ TEST(alltypes.rep_sint64_count == 5 && alltypes.rep_sint64[4] == -2006 && alltypes.rep_sint64[0] == 0);
+ TEST(alltypes.rep_bool_count == 5 && alltypes.rep_bool[4] == true && alltypes.rep_bool[0] == false);
+
+ TEST(alltypes.rep_fixed32_count == 5 && alltypes.rep_fixed32[4] == 2008 && alltypes.rep_fixed32[0] == 0);
+ TEST(alltypes.rep_sfixed32_count == 5 && alltypes.rep_sfixed32[4] == -2009 && alltypes.rep_sfixed32[0] == 0);
+ TEST(alltypes.rep_float_count == 5 && alltypes.rep_float[4] == 2010.0f && alltypes.rep_float[0] == 0.0f);
+
+ TEST(alltypes.rep_fixed64_count == 5 && alltypes.rep_fixed64[4] == 2011 && alltypes.rep_fixed64[0] == 0);
+ TEST(alltypes.rep_sfixed64_count == 5 && alltypes.rep_sfixed64[4] == -2012 && alltypes.rep_sfixed64[0] == 0);
+ TEST(alltypes.rep_double_count == 5 && alltypes.rep_double[4] == 2013.0 && alltypes.rep_double[0] == 0.0);
+
+ TEST(alltypes.rep_string_count == 5 && strcmp(alltypes.rep_string[4], "2014") == 0 && alltypes.rep_string[0][0] == '\0');
+ TEST(alltypes.rep_bytes_count == 5 && alltypes.rep_bytes[4].size == 4 && alltypes.rep_bytes[0].size == 0);
+ TEST(memcmp(alltypes.rep_bytes[4].bytes, "2015", 4) == 0);
+
+ TEST(alltypes.rep_submsg_count == 5);
+ TEST(strcmp(alltypes.rep_submsg[4].substuff1, "2016") == 0 && alltypes.rep_submsg[0].substuff1[0] == '\0');
+ TEST(alltypes.rep_submsg[4].substuff2 == 2016 && alltypes.rep_submsg[0].substuff2 == 0);
+ TEST(alltypes.rep_submsg[4].substuff3 == 2016 && alltypes.rep_submsg[0].substuff3 == 0);
+
+ TEST(alltypes.rep_enum_count == 5 && alltypes.rep_enum[4] == MyEnum_Truth && alltypes.rep_enum[0] == MyEnum_Zero);
+ TEST(alltypes.rep_emptymsg_count == 5);
+
+ TEST(alltypes.rep_fbytes_count == 5);
+ TEST(alltypes.rep_fbytes[0][0] == 0 && alltypes.rep_fbytes[0][3] == 0);
+ TEST(memcmp(alltypes.rep_fbytes[4], "2019", 4) == 0);
+
+ if (mode == 0)
+ {
+ /* Expect default values */
+ TEST(alltypes.sng_int32 == 0);
+ TEST(alltypes.sng_int64 == 0);
+ TEST(alltypes.sng_uint32 == 0);
+ TEST(alltypes.sng_uint64 == 0);
+ TEST(alltypes.sng_sint32 == 0);
+ TEST(alltypes.sng_sint64 == 0);
+ TEST(alltypes.sng_bool == false);
+
+ TEST(alltypes.sng_fixed32 == 0);
+ TEST(alltypes.sng_sfixed32 == 0);
+ TEST(alltypes.sng_float == 0.0f);
+
+ TEST(alltypes.sng_fixed64 == 0);
+ TEST(alltypes.sng_sfixed64 == 0);
+ TEST(alltypes.sng_double == 0.0);
+
+ TEST(strcmp(alltypes.sng_string, "") == 0);
+ TEST(alltypes.sng_bytes.size == 0);
+ TEST(strcmp(alltypes.sng_submsg.substuff1, "") == 0);
+ TEST(alltypes.sng_submsg.substuff2 == 0);
+ TEST(alltypes.sng_submsg.substuff3 == 0);
+ TEST(alltypes.sng_enum == MyEnum_Zero);
+ TEST(alltypes.sng_fbytes[0] == 0 &&
+ alltypes.sng_fbytes[1] == 0 &&
+ alltypes.sng_fbytes[2] == 0 &&
+ alltypes.sng_fbytes[3] == 0);
+
+ TEST(alltypes.which_oneof == 0);
+ }
+ else
+ {
+ /* Expect filled-in values */
+ TEST(alltypes.sng_int32 == 3041);
+ TEST(alltypes.sng_int64 == 3042);
+ TEST(alltypes.sng_uint32 == 3043);
+ TEST(alltypes.sng_uint64 == 3044);
+ TEST(alltypes.sng_sint32 == 3045);
+ TEST(alltypes.sng_sint64 == 3046);
+ TEST(alltypes.sng_bool == true);
+
+ TEST(alltypes.sng_fixed32 == 3048);
+ TEST(alltypes.sng_sfixed32 == 3049);
+ TEST(alltypes.sng_float == 3050.0f);
+
+ TEST(alltypes.sng_fixed64 == 3051);
+ TEST(alltypes.sng_sfixed64 == 3052);
+ TEST(alltypes.sng_double == 3053.0);
+
+ TEST(strcmp(alltypes.sng_string, "3054") == 0);
+ TEST(alltypes.sng_bytes.size == 4);
+ TEST(memcmp(alltypes.sng_bytes.bytes, "3055", 4) == 0);
+ TEST(strcmp(alltypes.sng_submsg.substuff1, "3056") == 0);
+ TEST(alltypes.sng_submsg.substuff2 == 3056);
+ TEST(alltypes.sng_submsg.substuff3 == 0);
+ TEST(alltypes.sng_enum == MyEnum_Truth);
+ TEST(memcmp(alltypes.sng_fbytes, "3059", 4) == 0);
+
+ TEST(alltypes.which_oneof == AllTypes_oneof_msg1_tag);
+ TEST(strcmp(alltypes.oneof.oneof_msg1.substuff1, "4059") == 0);
+ TEST(alltypes.oneof.oneof_msg1.substuff2 == 4059);
+ }
+
+ TEST(alltypes.req_limits.int32_min == INT32_MIN);
+ TEST(alltypes.req_limits.int32_max == INT32_MAX);
+ TEST(alltypes.req_limits.uint32_min == 0);
+ TEST(alltypes.req_limits.uint32_max == UINT32_MAX);
+ TEST(alltypes.req_limits.int64_min == INT64_MIN);
+ TEST(alltypes.req_limits.int64_max == INT64_MAX);
+ TEST(alltypes.req_limits.uint64_min == 0);
+ TEST(alltypes.req_limits.uint64_max == UINT64_MAX);
+ TEST(alltypes.req_limits.enum_min == HugeEnum_Negative);
+ TEST(alltypes.req_limits.enum_max == HugeEnum_Positive);
+
+ TEST(alltypes.end == 1099);
+
+ return true;
+}
+
+int main(int argc, char **argv)
+{
+ uint8_t buffer[2048];
+ size_t count;
+ pb_istream_t stream;
+
+ /* Whether to expect the optional values or the default values. */
+ int mode = (argc > 1) ? atoi(argv[1]) : 0;
+
+ /* Read the data into buffer */
+ SET_BINARY_MODE(stdin);
+ count = fread(buffer, 1, sizeof(buffer), stdin);
+
+ /* Construct a pb_istream_t for reading from the buffer */
+ stream = pb_istream_from_buffer(buffer, count);
+
+ /* Decode and print out the stuff */
+ if (!check_alltypes(&stream, mode))
+ {
+ printf("Parsing failed: %s\n", PB_GET_ERROR(&stream));
+ return 1;
+ } else {
+ return 0;
+ }
+}
diff --git a/libs/nanopb/tests/field_size_16_proto3/encode_alltypes.c b/libs/nanopb/tests/field_size_16_proto3/encode_alltypes.c
new file mode 100644
index 00000000..1da06688
--- /dev/null
+++ b/libs/nanopb/tests/field_size_16_proto3/encode_alltypes.c
@@ -0,0 +1,111 @@
+/* Attempts to test all the datatypes supported by ProtoBuf3.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pb_encode.h>
+#include "alltypes.pb.h"
+#include "test_helpers.h"
+
+int main(int argc, char **argv)
+{
+ int mode = (argc > 1) ? atoi(argv[1]) : 0;
+
+ /* Initialize the structure with constants */
+ AllTypes alltypes = AllTypes_init_zero;
+
+ 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].substuff3 = 2016;
+
+ alltypes.rep_enum_count = 5; alltypes.rep_enum[4] = MyEnum_Truth;
+ alltypes.rep_emptymsg_count = 5;
+
+ alltypes.rep_fbytes_count = 5;
+ memcpy(alltypes.rep_fbytes[4], "2019", 4);
+
+ alltypes.req_limits.int32_min = INT32_MIN;
+ alltypes.req_limits.int32_max = INT32_MAX;
+ alltypes.req_limits.uint32_min = 0;
+ alltypes.req_limits.uint32_max = UINT32_MAX;
+ alltypes.req_limits.int64_min = INT64_MIN;
+ alltypes.req_limits.int64_max = INT64_MAX;
+ alltypes.req_limits.uint64_min = 0;
+ alltypes.req_limits.uint64_max = UINT64_MAX;
+ alltypes.req_limits.enum_min = HugeEnum_Negative;
+ alltypes.req_limits.enum_max = HugeEnum_Positive;
+
+ if (mode != 0)
+ {
+ /* Fill in values for singular fields */
+ alltypes.sng_int32 = 3041;
+ alltypes.sng_int64 = 3042;
+ alltypes.sng_uint32 = 3043;
+ alltypes.sng_uint64 = 3044;
+ alltypes.sng_sint32 = 3045;
+ alltypes.sng_sint64 = 3046;
+ alltypes.sng_bool = true;
+
+ alltypes.sng_fixed32 = 3048;
+ alltypes.sng_sfixed32 = 3049;
+ alltypes.sng_float = 3050.0f;
+
+ alltypes.sng_fixed64 = 3051;
+ alltypes.sng_sfixed64 = 3052;
+ alltypes.sng_double = 3053.0;
+
+ strcpy(alltypes.sng_string, "3054");
+ alltypes.sng_bytes.size = 4;
+ memcpy(alltypes.sng_bytes.bytes, "3055", 4);
+ strcpy(alltypes.sng_submsg.substuff1, "3056");
+ alltypes.sng_submsg.substuff2 = 3056;
+ alltypes.sng_enum = MyEnum_Truth;
+ memcpy(alltypes.sng_fbytes, "3059", 4);
+
+ alltypes.which_oneof = AllTypes_oneof_msg1_tag;
+ strcpy(alltypes.oneof.oneof_msg1.substuff1, "4059");
+ alltypes.oneof.oneof_msg1.substuff2 = 4059;
+ }
+
+ alltypes.end = 1099;
+
+ {
+ uint8_t buffer[AllTypes_size];
+ 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: %s\n", PB_GET_ERROR(&stream));
+ return 1; /* Failure */
+ }
+ }
+}
diff --git a/libs/nanopb/tests/field_size_32/SConscript b/libs/nanopb/tests/field_size_32/SConscript
new file mode 100644
index 00000000..0b8dc0e3
--- /dev/null
+++ b/libs/nanopb/tests/field_size_32/SConscript
@@ -0,0 +1,29 @@
+# Run the alltypes test case, but compile with PB_FIELD_32BIT=1.
+# Also the .proto file has been modified to have high indexes.
+
+Import("env")
+
+# Take copy of the files for custom build.
+c = Copy("$TARGET", "$SOURCE")
+env.Command("encode_alltypes.c", "$BUILD/alltypes/encode_alltypes.c", c)
+env.Command("decode_alltypes.c", "$BUILD/alltypes/decode_alltypes.c", c)
+
+env.NanopbProto(["alltypes", "alltypes.options"])
+
+# Define the compilation options
+opts = env.Clone()
+opts.Append(CPPDEFINES = {'PB_FIELD_32BIT': 1})
+
+# Build new version of core
+strict = opts.Clone()
+strict.Append(CFLAGS = strict['CORECFLAGS'])
+strict.Object("pb_decode_fields32.o", "$NANOPB/pb_decode.c")
+strict.Object("pb_encode_fields32.o", "$NANOPB/pb_encode.c")
+strict.Object("pb_common_fields32.o", "$NANOPB/pb_common.c")
+
+# Now build and run the test normally.
+enc = opts.Program(["encode_alltypes.c", "alltypes.pb.c", "pb_encode_fields32.o", "pb_common_fields32.o"])
+dec = opts.Program(["decode_alltypes.c", "alltypes.pb.c", "pb_decode_fields32.o", "pb_common_fields32.o"])
+
+env.RunTest(enc)
+env.RunTest([dec, "encode_alltypes.output"])
diff --git a/libs/nanopb/tests/field_size_32/alltypes.options b/libs/nanopb/tests/field_size_32/alltypes.options
new file mode 100644
index 00000000..0d5ab12b
--- /dev/null
+++ b/libs/nanopb/tests/field_size_32/alltypes.options
@@ -0,0 +1,3 @@
+* max_size:16
+* max_count:5
+*.*fbytes fixed_length:true max_size:4
diff --git a/libs/nanopb/tests/field_size_32/alltypes.proto b/libs/nanopb/tests/field_size_32/alltypes.proto
new file mode 100644
index 00000000..ac76c8ef
--- /dev/null
+++ b/libs/nanopb/tests/field_size_32/alltypes.proto
@@ -0,0 +1,123 @@
+syntax = "proto2";
+
+message SubMessage {
+ required string substuff1 = 1 [default = "1"];
+ required int32 substuff2 = 2 [default = 2];
+ optional fixed32 substuff3 = 12365535 [default = 3];
+}
+
+message EmptyMessage {
+
+}
+
+enum HugeEnum {
+ Negative = -2147483647; /* protoc doesn't accept -2147483648 here */
+ Positive = 2147483647;
+}
+
+message Limits {
+ required int32 int32_min = 1;
+ required int32 int32_max = 2;
+ required uint32 uint32_min = 3;
+ required uint32 uint32_max = 4;
+ required int64 int64_min = 5;
+ required int64 int64_max = 6;
+ required uint64 uint64_min = 7;
+ required uint64 uint64_max = 8;
+ required HugeEnum enum_min = 9;
+ required HugeEnum enum_max = 10;
+}
+
+enum MyEnum {
+ Zero = 0;
+ 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;
+ required bytes req_bytes = 15;
+ required SubMessage req_submsg = 16;
+ required MyEnum req_enum = 17;
+ required EmptyMessage req_emptymsg = 18;
+ required bytes req_fbytes = 19;
+
+ repeated int32 rep_int32 = 21;
+ repeated int64 rep_int64 = 22;
+ repeated uint32 rep_uint32 = 23;
+ repeated uint64 rep_uint64 = 24;
+ repeated sint32 rep_sint32 = 25;
+ repeated sint64 rep_sint64 = 26;
+ repeated bool rep_bool = 27;
+
+ repeated fixed32 rep_fixed32 = 28;
+ repeated sfixed32 rep_sfixed32= 29;
+ repeated float rep_float = 30;
+
+ repeated fixed64 rep_fixed64 = 10031;
+ repeated sfixed64 rep_sfixed64= 10032;
+ repeated double rep_double = 10033;
+
+ repeated string rep_string = 10034;
+ repeated bytes rep_bytes = 10035;
+ repeated SubMessage rep_submsg = 10036;
+ repeated MyEnum rep_enum = 10037;
+ repeated EmptyMessage rep_emptymsg = 10038;
+ repeated bytes rep_fbytes = 10039;
+
+ optional int32 opt_int32 = 10041 [default = 4041];
+ optional int64 opt_int64 = 10042 [default = 4042];
+ optional uint32 opt_uint32 = 10043 [default = 4043];
+ optional uint64 opt_uint64 = 10044 [default = 4044];
+ optional sint32 opt_sint32 = 10045 [default = 4045];
+ optional sint64 opt_sint64 = 10046 [default = 4046];
+ optional bool opt_bool = 10047 [default = false];
+
+ optional fixed32 opt_fixed32 = 10048 [default = 4048];
+ optional sfixed32 opt_sfixed32= 10049 [default = 4049];
+ optional float opt_float = 10050 [default = 4050];
+
+ optional fixed64 opt_fixed64 = 10051 [default = 4051];
+ optional sfixed64 opt_sfixed64= 10052 [default = 4052];
+ optional double opt_double = 10053 [default = 4053];
+
+ optional string opt_string = 10054 [default = "4054"];
+ optional bytes opt_bytes = 10055 [default = "4055"];
+ optional SubMessage opt_submsg = 10056;
+ optional MyEnum opt_enum = 10057 [default = Second];
+ optional EmptyMessage opt_emptymsg = 10058;
+ optional bytes opt_fbytes = 10059 [default = "4059"];
+
+ oneof oneof
+ {
+ SubMessage oneof_msg1 = 10060;
+ EmptyMessage oneof_msg2 = 10061;
+ }
+
+ // Check that extreme integer values are handled correctly
+ required Limits req_limits = 98;
+
+ // 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 = 13432099;
+
+ extensions 200 to 255;
+}
+
diff --git a/libs/nanopb/tests/fuzztest/SConscript b/libs/nanopb/tests/fuzztest/SConscript
new file mode 100644
index 00000000..d2fb689c
--- /dev/null
+++ b/libs/nanopb/tests/fuzztest/SConscript
@@ -0,0 +1,43 @@
+# Run a fuzz test to verify robustness against corrupted/malicious data.
+
+Import("env", "malloc_env")
+
+def set_pkgname(src, dst, pkgname):
+ data = open(str(src)).read()
+ placeholder = '// package name placeholder'
+ assert placeholder in data
+ data = data.replace(placeholder, 'package %s;' % pkgname)
+ open(str(dst), 'w').write(data)
+
+# We want both pointer and static versions of the AllTypes message
+# Prefix them with package name.
+env.Command("alltypes_static.proto", "#alltypes/alltypes.proto",
+ lambda target, source, env: set_pkgname(source[0], target[0], 'alltypes_static'))
+env.Command("alltypes_pointer.proto", "#alltypes/alltypes.proto",
+ lambda target, source, env: set_pkgname(source[0], target[0], 'alltypes_pointer'))
+
+p1 = env.NanopbProto(["alltypes_pointer", "alltypes_pointer.options"])
+p2 = env.NanopbProto(["alltypes_static", "alltypes_static.options"])
+fuzz = malloc_env.Program(["fuzztest.c",
+ "alltypes_pointer.pb.c",
+ "alltypes_static.pb.c",
+ "$COMMON/pb_encode_with_malloc.o",
+ "$COMMON/pb_decode_with_malloc.o",
+ "$COMMON/pb_common_with_malloc.o",
+ "$COMMON/malloc_wrappers.o"])
+
+env.RunTest(fuzz)
+
+fuzzstub = malloc_env.Program(["fuzzstub.c",
+ "alltypes_pointer.pb.c",
+ "alltypes_static.pb.c",
+ "$COMMON/pb_encode_with_malloc.o",
+ "$COMMON/pb_decode_with_malloc.o",
+ "$COMMON/pb_common_with_malloc.o",
+ "$COMMON/malloc_wrappers.o"])
+
+generate_message = malloc_env.Program(["generate_message.c",
+ "alltypes_static.pb.c",
+ "$COMMON/pb_encode.o",
+ "$COMMON/pb_common.o"])
+
diff --git a/libs/nanopb/tests/fuzztest/alltypes_pointer.options b/libs/nanopb/tests/fuzztest/alltypes_pointer.options
new file mode 100644
index 00000000..7e3ad1e5
--- /dev/null
+++ b/libs/nanopb/tests/fuzztest/alltypes_pointer.options
@@ -0,0 +1,3 @@
+# Generate all fields as pointers.
+* type:FT_POINTER
+*.*fbytes fixed_length:true max_size:4
diff --git a/libs/nanopb/tests/fuzztest/alltypes_static.options b/libs/nanopb/tests/fuzztest/alltypes_static.options
new file mode 100644
index 00000000..e197e1df
--- /dev/null
+++ b/libs/nanopb/tests/fuzztest/alltypes_static.options
@@ -0,0 +1,4 @@
+* max_size:32
+* max_count:8
+*.extensions type:FT_IGNORE
+*.*fbytes fixed_length:true max_size:4
diff --git a/libs/nanopb/tests/fuzztest/fuzzstub.c b/libs/nanopb/tests/fuzztest/fuzzstub.c
new file mode 100644
index 00000000..ec9e2afe
--- /dev/null
+++ b/libs/nanopb/tests/fuzztest/fuzzstub.c
@@ -0,0 +1,189 @@
+/* Fuzz testing for the nanopb core.
+ * This can be used with external fuzzers, e.g. radamsa.
+ * It performs most of the same checks as fuzztest, but does not feature data generation.
+ */
+
+#include <pb_decode.h>
+#include <pb_encode.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <time.h>
+#include <malloc_wrappers.h>
+#include "alltypes_static.pb.h"
+#include "alltypes_pointer.pb.h"
+
+#define BUFSIZE 4096
+
+static bool do_static_decode(uint8_t *buffer, size_t msglen, bool assert_success)
+{
+ pb_istream_t stream;
+ bool status;
+
+ alltypes_static_AllTypes *msg = malloc_with_check(sizeof(alltypes_static_AllTypes));
+ stream = pb_istream_from_buffer(buffer, msglen);
+ status = pb_decode(&stream, alltypes_static_AllTypes_fields, msg);
+
+ if (!status && assert_success)
+ {
+ /* Anything that was successfully encoded, should be decodeable.
+ * One exception: strings without null terminator are encoded up
+ * to end of buffer, but refused on decode because the terminator
+ * would not fit. */
+ if (strcmp(stream.errmsg, "string overflow") != 0)
+ assert(status);
+ }
+
+ free_with_check(msg);
+ return status;
+}
+
+static bool do_pointer_decode(uint8_t *buffer, size_t msglen, bool assert_success)
+{
+ pb_istream_t stream;
+ bool status;
+ alltypes_pointer_AllTypes *msg;
+
+ msg = malloc_with_check(sizeof(alltypes_pointer_AllTypes));
+ memset(msg, 0, sizeof(alltypes_pointer_AllTypes));
+ stream = pb_istream_from_buffer(buffer, msglen);
+
+ assert(get_alloc_count() == 0);
+ status = pb_decode(&stream, alltypes_pointer_AllTypes_fields, msg);
+
+ if (assert_success)
+ assert(status);
+
+ pb_release(alltypes_pointer_AllTypes_fields, msg);
+ assert(get_alloc_count() == 0);
+
+ free_with_check(msg);
+
+ return status;
+}
+
+/* Do a decode -> encode -> decode -> encode roundtrip */
+static void do_static_roundtrip(uint8_t *buffer, size_t msglen)
+{
+ bool status;
+ uint8_t *buf2 = malloc_with_check(BUFSIZE);
+ uint8_t *buf3 = malloc_with_check(BUFSIZE);
+ size_t msglen2, msglen3;
+ alltypes_static_AllTypes *msg1 = malloc_with_check(sizeof(alltypes_static_AllTypes));
+ alltypes_static_AllTypes *msg2 = malloc_with_check(sizeof(alltypes_static_AllTypes));
+ memset(msg1, 0, sizeof(alltypes_static_AllTypes));
+ memset(msg2, 0, sizeof(alltypes_static_AllTypes));
+
+ {
+ pb_istream_t stream = pb_istream_from_buffer(buffer, msglen);
+ status = pb_decode(&stream, alltypes_static_AllTypes_fields, msg1);
+ assert(status);
+ }
+
+ {
+ pb_ostream_t stream = pb_ostream_from_buffer(buf2, BUFSIZE);
+ status = pb_encode(&stream, alltypes_static_AllTypes_fields, msg1);
+ assert(status);
+ msglen2 = stream.bytes_written;
+ }
+
+ {
+ pb_istream_t stream = pb_istream_from_buffer(buf2, msglen2);
+ status = pb_decode(&stream, alltypes_static_AllTypes_fields, msg2);
+ assert(status);
+ }
+
+ {
+ pb_ostream_t stream = pb_ostream_from_buffer(buf3, BUFSIZE);
+ status = pb_encode(&stream, alltypes_static_AllTypes_fields, msg2);
+ assert(status);
+ msglen3 = stream.bytes_written;
+ }
+
+ assert(msglen2 == msglen3);
+ assert(memcmp(buf2, buf3, msglen2) == 0);
+
+ free_with_check(msg1);
+ free_with_check(msg2);
+ free_with_check(buf2);
+ free_with_check(buf3);
+}
+
+/* Do decode -> encode -> decode -> encode roundtrip */
+static void do_pointer_roundtrip(uint8_t *buffer, size_t msglen)
+{
+ bool status;
+ uint8_t *buf2 = malloc_with_check(BUFSIZE);
+ uint8_t *buf3 = malloc_with_check(BUFSIZE);
+ size_t msglen2, msglen3;
+ alltypes_pointer_AllTypes *msg1 = malloc_with_check(sizeof(alltypes_pointer_AllTypes));
+ alltypes_pointer_AllTypes *msg2 = malloc_with_check(sizeof(alltypes_pointer_AllTypes));
+ memset(msg1, 0, sizeof(alltypes_pointer_AllTypes));
+ memset(msg2, 0, sizeof(alltypes_pointer_AllTypes));
+
+ {
+ pb_istream_t stream = pb_istream_from_buffer(buffer, msglen);
+ status = pb_decode(&stream, alltypes_pointer_AllTypes_fields, msg1);
+ assert(status);
+ }
+
+ {
+ pb_ostream_t stream = pb_ostream_from_buffer(buf2, BUFSIZE);
+ status = pb_encode(&stream, alltypes_pointer_AllTypes_fields, msg1);
+ assert(status);
+ msglen2 = stream.bytes_written;
+ }
+
+ {
+ pb_istream_t stream = pb_istream_from_buffer(buf2, msglen2);
+ status = pb_decode(&stream, alltypes_pointer_AllTypes_fields, msg2);
+ assert(status);
+ }
+
+ {
+ pb_ostream_t stream = pb_ostream_from_buffer(buf3, BUFSIZE);
+ status = pb_encode(&stream, alltypes_pointer_AllTypes_fields, msg2);
+ assert(status);
+ msglen3 = stream.bytes_written;
+ }
+
+ assert(msglen2 == msglen3);
+ assert(memcmp(buf2, buf3, msglen2) == 0);
+
+ pb_release(alltypes_pointer_AllTypes_fields, msg1);
+ pb_release(alltypes_pointer_AllTypes_fields, msg2);
+ free_with_check(msg1);
+ free_with_check(msg2);
+ free_with_check(buf2);
+ free_with_check(buf3);
+}
+
+static void run_iteration()
+{
+ uint8_t *buffer = malloc_with_check(BUFSIZE);
+ size_t msglen;
+ bool status;
+
+ msglen = fread(buffer, 1, BUFSIZE, stdin);
+
+ status = do_static_decode(buffer, msglen, false);
+
+ if (status)
+ do_static_roundtrip(buffer, msglen);
+
+ status = do_pointer_decode(buffer, msglen, false);
+
+ if (status)
+ do_pointer_roundtrip(buffer, msglen);
+
+ free_with_check(buffer);
+}
+
+int main(int argc, char **argv)
+{
+ run_iteration();
+
+ return 0;
+}
+
diff --git a/libs/nanopb/tests/fuzztest/fuzztest.c b/libs/nanopb/tests/fuzztest/fuzztest.c
new file mode 100644
index 00000000..ee851ec0
--- /dev/null
+++ b/libs/nanopb/tests/fuzztest/fuzztest.c
@@ -0,0 +1,432 @@
+/* Fuzz testing for the nanopb core.
+ * Attempts to verify all the properties defined in the security model document.
+ */
+
+#include <pb_decode.h>
+#include <pb_encode.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <time.h>
+#include <malloc_wrappers.h>
+#include "alltypes_static.pb.h"
+#include "alltypes_pointer.pb.h"
+
+static uint64_t random_seed;
+
+/* Uses xorshift64 here instead of rand() for both speed and
+ * reproducibility across platforms. */
+static uint32_t rand_word()
+{
+ random_seed ^= random_seed >> 12;
+ random_seed ^= random_seed << 25;
+ random_seed ^= random_seed >> 27;
+ return random_seed * 2685821657736338717ULL;
+}
+
+/* Get a random integer in range, with approximately flat distribution. */
+static int rand_int(int min, int max)
+{
+ return rand_word() % (max + 1 - min) + min;
+}
+
+static bool rand_bool()
+{
+ return rand_word() & 1;
+}
+
+/* Get a random byte, with skewed distribution.
+ * Important corner cases like 0xFF, 0x00 and 0xFE occur more
+ * often than other values. */
+static uint8_t rand_byte()
+{
+ uint32_t w = rand_word();
+ uint8_t b = w & 0xFF;
+ if (w & 0x100000)
+ b >>= (w >> 8) & 7;
+ if (w & 0x200000)
+ b <<= (w >> 12) & 7;
+ if (w & 0x400000)
+ b ^= 0xFF;
+ return b;
+}
+
+/* Get a random length, with skewed distribution.
+ * Favors the shorter lengths, but always atleast 1. */
+static size_t rand_len(size_t max)
+{
+ uint32_t w = rand_word();
+ size_t s;
+ if (w & 0x800000)
+ w &= 3;
+ else if (w & 0x400000)
+ w &= 15;
+ else if (w & 0x200000)
+ w &= 255;
+
+ s = (w % max);
+ if (s == 0)
+ s = 1;
+
+ return s;
+}
+
+/* Fills a buffer with random data with skewed distribution. */
+static void rand_fill(uint8_t *buf, size_t count)
+{
+ while (count--)
+ *buf++ = rand_byte();
+}
+
+/* Fill with random protobuf-like data */
+static size_t rand_fill_protobuf(uint8_t *buf, size_t min_bytes, size_t max_bytes, int min_tag)
+{
+ pb_ostream_t stream = pb_ostream_from_buffer(buf, max_bytes);
+
+ while(stream.bytes_written < min_bytes)
+ {
+ pb_wire_type_t wt = rand_int(0, 3);
+ if (wt == 3) wt = 5; /* Gap in values */
+
+ if (!pb_encode_tag(&stream, wt, rand_int(min_tag, min_tag + 512)))
+ break;
+
+ if (wt == PB_WT_VARINT)
+ {
+ uint64_t value;
+ rand_fill((uint8_t*)&value, sizeof(value));
+ pb_encode_varint(&stream, value);
+ }
+ else if (wt == PB_WT_64BIT)
+ {
+ uint64_t value;
+ rand_fill((uint8_t*)&value, sizeof(value));
+ pb_encode_fixed64(&stream, &value);
+ }
+ else if (wt == PB_WT_32BIT)
+ {
+ uint32_t value;
+ rand_fill((uint8_t*)&value, sizeof(value));
+ pb_encode_fixed32(&stream, &value);
+ }
+ else if (wt == PB_WT_STRING)
+ {
+ size_t len;
+ uint8_t *buf;
+
+ if (min_bytes > stream.bytes_written)
+ len = rand_len(min_bytes - stream.bytes_written);
+ else
+ len = 0;
+
+ buf = malloc(len);
+ pb_encode_varint(&stream, len);
+ rand_fill(buf, len);
+ pb_write(&stream, buf, len);
+ free(buf);
+ }
+ }
+
+ return stream.bytes_written;
+}
+
+/* Given a buffer of data, mess it up a bit */
+static void rand_mess(uint8_t *buf, size_t count)
+{
+ int m = rand_int(0, 3);
+
+ if (m == 0)
+ {
+ /* Replace random substring */
+ int s = rand_int(0, count - 1);
+ int l = rand_len(count - s);
+ rand_fill(buf + s, l);
+ }
+ else if (m == 1)
+ {
+ /* Swap random bytes */
+ int a = rand_int(0, count - 1);
+ int b = rand_int(0, count - 1);
+ int x = buf[a];
+ buf[a] = buf[b];
+ buf[b] = x;
+ }
+ else if (m == 2)
+ {
+ /* Duplicate substring */
+ int s = rand_int(0, count - 2);
+ int l = rand_len((count - s) / 2);
+ memcpy(buf + s + l, buf + s, l);
+ }
+ else if (m == 3)
+ {
+ /* Add random protobuf noise */
+ int s = rand_int(0, count - 1);
+ int l = rand_len(count - s);
+ rand_fill_protobuf(buf + s, l, count - s, 1);
+ }
+}
+
+/* Some default data to put in the message */
+static const alltypes_static_AllTypes initval = alltypes_static_AllTypes_init_default;
+
+#define BUFSIZE 4096
+
+static bool do_static_encode(uint8_t *buffer, size_t *msglen)
+{
+ pb_ostream_t stream;
+ bool status;
+
+ /* Allocate a message and fill it with defaults */
+ alltypes_static_AllTypes *msg = malloc_with_check(sizeof(alltypes_static_AllTypes));
+ memcpy(msg, &initval, sizeof(initval));
+
+ /* Apply randomness to the data before encoding */
+ while (rand_int(0, 7))
+ rand_mess((uint8_t*)msg, sizeof(alltypes_static_AllTypes));
+
+ stream = pb_ostream_from_buffer(buffer, BUFSIZE);
+ status = pb_encode(&stream, alltypes_static_AllTypes_fields, msg);
+ assert(stream.bytes_written <= BUFSIZE);
+ assert(stream.bytes_written <= alltypes_static_AllTypes_size);
+
+ *msglen = stream.bytes_written;
+ pb_release(alltypes_static_AllTypes_fields, msg);
+ free_with_check(msg);
+
+ return status;
+}
+
+/* Append or prepend protobuf noise */
+static void do_protobuf_noise(uint8_t *buffer, size_t *msglen)
+{
+ int m = rand_int(0, 2);
+ size_t max_size = BUFSIZE - 32 - *msglen;
+ if (m == 1)
+ {
+ /* Prepend */
+ uint8_t *tmp = malloc_with_check(BUFSIZE);
+ size_t s = rand_fill_protobuf(tmp, rand_len(max_size), BUFSIZE - *msglen, 512);
+ memmove(buffer + s, buffer, *msglen);
+ memcpy(buffer, tmp, s);
+ free_with_check(tmp);
+ *msglen += s;
+ }
+ else if (m == 2)
+ {
+ /* Append */
+ size_t s = rand_fill_protobuf(buffer + *msglen, rand_len(max_size), BUFSIZE - *msglen, 512);
+ *msglen += s;
+ }
+}
+
+static bool do_static_decode(uint8_t *buffer, size_t msglen, bool assert_success)
+{
+ pb_istream_t stream;
+ bool status;
+
+ alltypes_static_AllTypes *msg = malloc_with_check(sizeof(alltypes_static_AllTypes));
+ rand_fill((uint8_t*)msg, sizeof(alltypes_static_AllTypes));
+ stream = pb_istream_from_buffer(buffer, msglen);
+ status = pb_decode(&stream, alltypes_static_AllTypes_fields, msg);
+
+ if (!status && assert_success)
+ {
+ /* Anything that was successfully encoded, should be decodeable.
+ * One exception: strings without null terminator are encoded up
+ * to end of buffer, but refused on decode because the terminator
+ * would not fit. */
+ if (strcmp(stream.errmsg, "string overflow") != 0)
+ assert(status);
+ }
+
+ free_with_check(msg);
+ return status;
+}
+
+static bool do_pointer_decode(uint8_t *buffer, size_t msglen, bool assert_success)
+{
+ pb_istream_t stream;
+ bool status;
+ alltypes_pointer_AllTypes *msg;
+
+ msg = malloc_with_check(sizeof(alltypes_pointer_AllTypes));
+ memset(msg, 0, sizeof(alltypes_pointer_AllTypes));
+ stream = pb_istream_from_buffer(buffer, msglen);
+
+ assert(get_alloc_count() == 0);
+ status = pb_decode(&stream, alltypes_pointer_AllTypes_fields, msg);
+
+ if (assert_success)
+ assert(status);
+
+ pb_release(alltypes_pointer_AllTypes_fields, msg);
+ assert(get_alloc_count() == 0);
+
+ free_with_check(msg);
+
+ return status;
+}
+
+/* Do a decode -> encode -> decode -> encode roundtrip */
+static void do_static_roundtrip(uint8_t *buffer, size_t msglen)
+{
+ bool status;
+ uint8_t *buf2 = malloc_with_check(BUFSIZE);
+ uint8_t *buf3 = malloc_with_check(BUFSIZE);
+ size_t msglen2, msglen3;
+ alltypes_static_AllTypes *msg1 = malloc_with_check(sizeof(alltypes_static_AllTypes));
+ alltypes_static_AllTypes *msg2 = malloc_with_check(sizeof(alltypes_static_AllTypes));
+ memset(msg1, 0, sizeof(alltypes_static_AllTypes));
+ memset(msg2, 0, sizeof(alltypes_static_AllTypes));
+
+ {
+ pb_istream_t stream = pb_istream_from_buffer(buffer, msglen);
+ status = pb_decode(&stream, alltypes_static_AllTypes_fields, msg1);
+ assert(status);
+ }
+
+ {
+ pb_ostream_t stream = pb_ostream_from_buffer(buf2, BUFSIZE);
+ status = pb_encode(&stream, alltypes_static_AllTypes_fields, msg1);
+ assert(status);
+ msglen2 = stream.bytes_written;
+ }
+
+ {
+ pb_istream_t stream = pb_istream_from_buffer(buf2, msglen2);
+ status = pb_decode(&stream, alltypes_static_AllTypes_fields, msg2);
+ assert(status);
+ }
+
+ {
+ pb_ostream_t stream = pb_ostream_from_buffer(buf3, BUFSIZE);
+ status = pb_encode(&stream, alltypes_static_AllTypes_fields, msg2);
+ assert(status);
+ msglen3 = stream.bytes_written;
+ }
+
+ assert(msglen2 == msglen3);
+ assert(memcmp(buf2, buf3, msglen2) == 0);
+
+ free_with_check(msg1);
+ free_with_check(msg2);
+ free_with_check(buf2);
+ free_with_check(buf3);
+}
+
+/* Do decode -> encode -> decode -> encode roundtrip */
+static void do_pointer_roundtrip(uint8_t *buffer, size_t msglen)
+{
+ bool status;
+ uint8_t *buf2 = malloc_with_check(BUFSIZE);
+ uint8_t *buf3 = malloc_with_check(BUFSIZE);
+ size_t msglen2, msglen3;
+ alltypes_pointer_AllTypes *msg1 = malloc_with_check(sizeof(alltypes_pointer_AllTypes));
+ alltypes_pointer_AllTypes *msg2 = malloc_with_check(sizeof(alltypes_pointer_AllTypes));
+ memset(msg1, 0, sizeof(alltypes_pointer_AllTypes));
+ memset(msg2, 0, sizeof(alltypes_pointer_AllTypes));
+
+ {
+ pb_istream_t stream = pb_istream_from_buffer(buffer, msglen);
+ status = pb_decode(&stream, alltypes_pointer_AllTypes_fields, msg1);
+ assert(status);
+ }
+
+ {
+ pb_ostream_t stream = pb_ostream_from_buffer(buf2, BUFSIZE);
+ status = pb_encode(&stream, alltypes_pointer_AllTypes_fields, msg1);
+ assert(status);
+ msglen2 = stream.bytes_written;
+ }
+
+ {
+ pb_istream_t stream = pb_istream_from_buffer(buf2, msglen2);
+ status = pb_decode(&stream, alltypes_pointer_AllTypes_fields, msg2);
+ assert(status);
+ }
+
+ {
+ pb_ostream_t stream = pb_ostream_from_buffer(buf3, BUFSIZE);
+ status = pb_encode(&stream, alltypes_pointer_AllTypes_fields, msg2);
+ assert(status);
+ msglen3 = stream.bytes_written;
+ }
+
+ assert(msglen2 == msglen3);
+ assert(memcmp(buf2, buf3, msglen2) == 0);
+
+ pb_release(alltypes_pointer_AllTypes_fields, msg1);
+ pb_release(alltypes_pointer_AllTypes_fields, msg2);
+ free_with_check(msg1);
+ free_with_check(msg2);
+ free_with_check(buf2);
+ free_with_check(buf3);
+}
+
+static void run_iteration()
+{
+ uint8_t *buffer = malloc_with_check(BUFSIZE);
+ size_t msglen;
+ bool status;
+
+ rand_fill(buffer, BUFSIZE);
+
+ if (do_static_encode(buffer, &msglen))
+ {
+ do_protobuf_noise(buffer, &msglen);
+
+ status = do_static_decode(buffer, msglen, true);
+
+ if (status)
+ do_static_roundtrip(buffer, msglen);
+
+ status = do_pointer_decode(buffer, msglen, true);
+
+ if (status)
+ do_pointer_roundtrip(buffer, msglen);
+
+ /* Apply randomness to the encoded data */
+ while (rand_bool())
+ rand_mess(buffer, BUFSIZE);
+
+ /* Apply randomness to encoded data length */
+ if (rand_bool())
+ msglen = rand_int(0, BUFSIZE);
+
+ status = do_static_decode(buffer, msglen, false);
+ do_pointer_decode(buffer, msglen, status);
+
+ if (status)
+ {
+ do_static_roundtrip(buffer, msglen);
+ do_pointer_roundtrip(buffer, msglen);
+ }
+ }
+
+ free_with_check(buffer);
+}
+
+int main(int argc, char **argv)
+{
+ int i;
+ if (argc > 1)
+ {
+ random_seed = atol(argv[1]);
+ }
+ else
+ {
+ random_seed = time(NULL);
+ }
+
+ fprintf(stderr, "Random seed: %llu\n", (long long unsigned)random_seed);
+
+ for (i = 0; i < 10000; i++)
+ {
+ run_iteration();
+ }
+
+ return 0;
+}
+
diff --git a/libs/nanopb/tests/fuzztest/generate_message.c b/libs/nanopb/tests/fuzztest/generate_message.c
new file mode 100644
index 00000000..6e492990
--- /dev/null
+++ b/libs/nanopb/tests/fuzztest/generate_message.c
@@ -0,0 +1,101 @@
+/* Generates a random, valid protobuf message. Useful to seed
+ * external fuzzers such as afl-fuzz.
+ */
+
+#include <pb_encode.h>
+#include <pb_common.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <time.h>
+#include "alltypes_static.pb.h"
+
+static uint64_t random_seed;
+
+/* Uses xorshift64 here instead of rand() for both speed and
+ * reproducibility across platforms. */
+static uint32_t rand_word()
+{
+ random_seed ^= random_seed >> 12;
+ random_seed ^= random_seed << 25;
+ random_seed ^= random_seed >> 27;
+ return random_seed * 2685821657736338717ULL;
+}
+
+/* Fills a buffer with random data. */
+static void rand_fill(uint8_t *buf, size_t count)
+{
+ while (count--)
+ {
+ *buf++ = rand_word() & 0xff;
+ }
+}
+
+/* Check that size/count fields do not exceed their max size.
+ * Otherwise we would have to loop pretty long in generate_message().
+ * Note that there may still be a few encoding errors from submessages.
+ */
+static void limit_sizes(alltypes_static_AllTypes *msg)
+{
+ pb_field_iter_t iter;
+ pb_field_iter_begin(&iter, alltypes_static_AllTypes_fields, msg);
+ while (pb_field_iter_next(&iter))
+ {
+ if (PB_LTYPE(iter.pos->type) == PB_LTYPE_BYTES)
+ {
+ ((pb_bytes_array_t*)iter.pData)->size %= iter.pos->data_size - PB_BYTES_ARRAY_T_ALLOCSIZE(0);
+ }
+
+ if (PB_HTYPE(iter.pos->type) == PB_HTYPE_REPEATED)
+ {
+ *((pb_size_t*)iter.pSize) %= iter.pos->array_size;
+ }
+
+ if (PB_HTYPE(iter.pos->type) == PB_HTYPE_ONEOF)
+ {
+ /* Set the oneof to this message type with 50% chance. */
+ if (rand_word() & 1)
+ {
+ *((pb_size_t*)iter.pSize) = iter.pos->tag;
+ }
+ }
+ }
+}
+
+static void generate_message()
+{
+ alltypes_static_AllTypes msg;
+ uint8_t buf[8192];
+ pb_ostream_t stream = {0};
+
+ do {
+ if (stream.errmsg)
+ fprintf(stderr, "Encoder error: %s\n", stream.errmsg);
+
+ stream = pb_ostream_from_buffer(buf, sizeof(buf));
+ rand_fill((void*)&msg, sizeof(msg));
+ limit_sizes(&msg);
+ } while (!pb_encode(&stream, alltypes_static_AllTypes_fields, &msg));
+
+ fwrite(buf, 1, stream.bytes_written, stdout);
+}
+
+int main(int argc, char **argv)
+{
+ if (argc > 1)
+ {
+ random_seed = atol(argv[1]);
+ }
+ else
+ {
+ random_seed = time(NULL);
+ }
+
+ fprintf(stderr, "Random seed: %llu\n", (long long unsigned)random_seed);
+
+ generate_message();
+
+ return 0;
+}
+
diff --git a/libs/nanopb/tests/fuzztest/run_radamsa.sh b/libs/nanopb/tests/fuzztest/run_radamsa.sh
new file mode 100755
index 00000000..52cd40a8
--- /dev/null
+++ b/libs/nanopb/tests/fuzztest/run_radamsa.sh
@@ -0,0 +1,12 @@
+#!/bin/bash
+
+TMP=`tempfile`
+
+echo $TMP
+while true
+do
+ radamsa sample_data/* > $TMP
+ $1 < $TMP
+ test $? -gt 127 && break
+done
+
diff --git a/libs/nanopb/tests/fuzztest/sample_data/sample1.pb b/libs/nanopb/tests/fuzztest/sample_data/sample1.pb
new file mode 100644
index 00000000..07527885
--- /dev/null
+++ b/libs/nanopb/tests/fuzztest/sample_data/sample1.pb
Binary files differ
diff --git a/libs/nanopb/tests/fuzztest/sample_data/sample2.pb b/libs/nanopb/tests/fuzztest/sample_data/sample2.pb
new file mode 100644
index 00000000..cc89f91b
--- /dev/null
+++ b/libs/nanopb/tests/fuzztest/sample_data/sample2.pb
Binary files differ
diff --git a/libs/nanopb/tests/inline/SConscript b/libs/nanopb/tests/inline/SConscript
new file mode 100644
index 00000000..34371fda
--- /dev/null
+++ b/libs/nanopb/tests/inline/SConscript
@@ -0,0 +1,16 @@
+# Test that inlined bytes fields work.
+
+Import("env")
+
+env.NanopbProto("inline")
+env.Object("inline.pb.c")
+
+env.Match(["inline.pb.h", "inline.expected"])
+
+p = env.Program(["inline_unittests.c",
+ "inline.pb.c",
+ "$COMMON/pb_encode.o",
+ "$COMMON/pb_decode.o",
+ "$COMMON/pb_common.o"])
+
+env.RunTest(p)
diff --git a/libs/nanopb/tests/inline/inline.expected b/libs/nanopb/tests/inline/inline.expected
new file mode 100644
index 00000000..593e972b
--- /dev/null
+++ b/libs/nanopb/tests/inline/inline.expected
@@ -0,0 +1,3 @@
+pb_byte_t data\[32\];
+bool has_data;
+pb_byte_t data\[64\];
diff --git a/libs/nanopb/tests/inline/inline.proto b/libs/nanopb/tests/inline/inline.proto
new file mode 100644
index 00000000..6e511f0a
--- /dev/null
+++ b/libs/nanopb/tests/inline/inline.proto
@@ -0,0 +1,17 @@
+/* Test nanopb option parsing.
+ * options.expected lists the patterns that are searched for in the output.
+ */
+
+syntax = "proto2";
+
+import "nanopb.proto";
+
+message Message1
+{
+ required bytes data = 1 [(nanopb).type = FT_INLINE, (nanopb).max_size = 32];
+}
+
+message Message2
+{
+ optional bytes data = 1 [(nanopb).type = FT_INLINE, (nanopb).max_size = 64];
+}
diff --git a/libs/nanopb/tests/inline/inline_unittests.c b/libs/nanopb/tests/inline/inline_unittests.c
new file mode 100644
index 00000000..b5834c7e
--- /dev/null
+++ b/libs/nanopb/tests/inline/inline_unittests.c
@@ -0,0 +1,73 @@
+#include <stdio.h>
+#include <string.h>
+#include <pb_decode.h>
+#include <pb_encode.h>
+#include "unittests.h"
+#include "inline.pb.h"
+
+int main()
+{
+ int status = 0;
+ int i = 0;
+ COMMENT("Test inline byte fields");
+
+ {
+ Message1 msg1 = Message1_init_zero;
+ TEST(sizeof(msg1.data) == 32);
+ }
+
+ {
+ Message1 msg1 = Message1_init_zero;
+ pb_byte_t msg1_buffer[Message1_size];
+ pb_ostream_t ostream = pb_ostream_from_buffer(msg1_buffer, Message1_size);
+ Message1 msg1_deserialized = Message1_init_zero;
+ pb_istream_t istream = pb_istream_from_buffer(msg1_buffer, Message1_size);
+
+ for (i = 0; i < 32; i++) {
+ msg1.data[i] = i;
+ }
+
+ TEST(pb_encode(&ostream, Message1_fields, &msg1));
+ TEST(ostream.bytes_written == Message1_size);
+
+ TEST(pb_decode(&istream, Message1_fields, &msg1_deserialized));
+
+ TEST(istream.bytes_left == 0);
+ TEST(memcmp(&msg1_deserialized, &msg1, sizeof(msg1)) == 0);
+ }
+
+ {
+ Message2 msg2 = {true, {0}};
+ Message2 msg2_no_data = {false, {1}};
+ pb_byte_t msg2_buffer[Message2_size];
+ pb_ostream_t ostream = pb_ostream_from_buffer(msg2_buffer, Message2_size);
+ Message2 msg2_deserialized = Message2_init_zero;
+ pb_istream_t istream = pb_istream_from_buffer(msg2_buffer, Message2_size);
+
+ for (i = 0; i < 64; i++) {
+ msg2.data[i] = i;
+ }
+
+ TEST(pb_encode(&ostream, Message2_fields, &msg2));
+ TEST(ostream.bytes_written == Message2_size);
+
+ TEST(pb_decode(&istream, Message2_fields, &msg2_deserialized));
+
+ TEST(istream.bytes_left == 0);
+ TEST(memcmp(&msg2_deserialized, &msg2, sizeof(msg2)) == 0);
+ TEST(msg2_deserialized.has_data);
+
+ memset(msg2_buffer, 0, sizeof(msg2_buffer));
+ ostream = pb_ostream_from_buffer(msg2_buffer, Message2_size);
+ TEST(pb_encode(&ostream, Message2_fields, &msg2_no_data));
+ istream = pb_istream_from_buffer(msg2_buffer, Message2_size);
+ TEST(pb_decode(&istream, Message2_fields, &msg2_deserialized));
+ TEST(!msg2_deserialized.has_data);
+ TEST(memcmp(&msg2_deserialized, &msg2, sizeof(msg2)) != 0);
+ }
+
+ if (status != 0)
+ fprintf(stdout, "\n\nSome tests FAILED!\n");
+
+ return status;
+}
diff --git a/libs/nanopb/tests/intsizes/SConscript b/libs/nanopb/tests/intsizes/SConscript
new file mode 100644
index 00000000..a90680bc
--- /dev/null
+++ b/libs/nanopb/tests/intsizes/SConscript
@@ -0,0 +1,12 @@
+# Test that the int_size option in .proto works.
+
+Import('env')
+
+env.NanopbProto('intsizes')
+
+p = env.Program(["intsizes_unittests.c",
+ "intsizes.pb.c",
+ "$COMMON/pb_encode.o",
+ "$COMMON/pb_decode.o",
+ "$COMMON/pb_common.o"])
+env.RunTest(p)
diff --git a/libs/nanopb/tests/intsizes/intsizes.proto b/libs/nanopb/tests/intsizes/intsizes.proto
new file mode 100644
index 00000000..91444d41
--- /dev/null
+++ b/libs/nanopb/tests/intsizes/intsizes.proto
@@ -0,0 +1,41 @@
+/* Test the integer size overriding in nanopb options.
+ * This allows to use 8- and 16-bit integer variables, which are not supported
+ * directly by Google Protobuf.
+ *
+ * The int_size setting will override the number of bits, but keep the type
+ * otherwise. E.g. uint32 + IS_8 => uint8_t
+ */
+
+syntax = "proto2";
+
+import 'nanopb.proto';
+
+message IntSizes {
+ required int32 req_int8 = 1 [(nanopb).int_size = IS_8];
+ required uint32 req_uint8 = 2 [(nanopb).int_size = IS_8];
+ required sint32 req_sint8 = 3 [(nanopb).int_size = IS_8];
+ required int32 req_int16 = 4 [(nanopb).int_size = IS_16];
+ required uint32 req_uint16 = 5 [(nanopb).int_size = IS_16];
+ required sint32 req_sint16 = 6 [(nanopb).int_size = IS_16];
+ required int32 req_int32 = 7 [(nanopb).int_size = IS_32];
+ required uint32 req_uint32 = 8 [(nanopb).int_size = IS_32];
+ required sint32 req_sint32 = 9 [(nanopb).int_size = IS_32];
+ required int32 req_int64 = 10 [(nanopb).int_size = IS_64];
+ required uint32 req_uint64 = 11 [(nanopb).int_size = IS_64];
+ required sint32 req_sint64 = 12 [(nanopb).int_size = IS_64];
+}
+
+message DefaultSizes {
+ required int32 req_int8 = 1 ;
+ required uint32 req_uint8 = 2 ;
+ required sint32 req_sint8 = 3 ;
+ required int32 req_int16 = 4 ;
+ required uint32 req_uint16 = 5 ;
+ required sint32 req_sint16 = 6 ;
+ required int32 req_int32 = 7 ;
+ required uint32 req_uint32 = 8 ;
+ required sint32 req_sint32 = 9 ;
+ required int64 req_int64 = 10;
+ required uint64 req_uint64 = 11;
+ required sint64 req_sint64 = 12;
+}
diff --git a/libs/nanopb/tests/intsizes/intsizes_unittests.c b/libs/nanopb/tests/intsizes/intsizes_unittests.c
new file mode 100644
index 00000000..79ef0369
--- /dev/null
+++ b/libs/nanopb/tests/intsizes/intsizes_unittests.c
@@ -0,0 +1,122 @@
+#include <stdio.h>
+#include <string.h>
+#include <pb_decode.h>
+#include <pb_encode.h>
+#include "unittests.h"
+#include "intsizes.pb.h"
+
+#define S(x) pb_istream_from_buffer((uint8_t*)x, sizeof(x) - 1)
+
+/* This is a macro instead of function in order to get the actual values
+ * into the TEST() lines in output */
+#define TEST_ROUNDTRIP(int8, uint8, sint8, \
+ int16, uint16, sint16, \
+ int32, uint32, sint32, \
+ int64, uint64, sint64, expected_result) \
+{ \
+ uint8_t buffer1[128], buffer2[128]; \
+ size_t msgsize; \
+ DefaultSizes msg1 = DefaultSizes_init_zero; \
+ IntSizes msg2 = IntSizes_init_zero; \
+ \
+ msg1.req_int8 = int8; \
+ msg1.req_uint8 = uint8; \
+ msg1.req_sint8 = sint8; \
+ msg1.req_int16 = int16; \
+ msg1.req_uint16 = uint16; \
+ msg1.req_sint16 = sint16; \
+ msg1.req_int32 = int32; \
+ msg1.req_uint32 = uint32; \
+ msg1.req_sint32 = sint32; \
+ msg1.req_int64 = int64; \
+ msg1.req_uint64 = uint64; \
+ msg1.req_sint64 = sint64; \
+ \
+ { \
+ pb_ostream_t s = pb_ostream_from_buffer(buffer1, sizeof(buffer1)); \
+ TEST(pb_encode(&s, DefaultSizes_fields, &msg1)); \
+ msgsize = s.bytes_written; \
+ } \
+ \
+ { \
+ pb_istream_t s = pb_istream_from_buffer(buffer1, msgsize); \
+ TEST(pb_decode(&s, IntSizes_fields, &msg2) == expected_result); \
+ if (expected_result) \
+ { \
+ TEST( (int64_t)msg2.req_int8 == int8); \
+ TEST((uint64_t)msg2.req_uint8 == uint8); \
+ TEST( (int64_t)msg2.req_sint8 == sint8); \
+ TEST( (int64_t)msg2.req_int16 == int16); \
+ TEST((uint64_t)msg2.req_uint16 == uint16); \
+ TEST( (int64_t)msg2.req_sint16 == sint16); \
+ TEST( (int64_t)msg2.req_int32 == int32); \
+ TEST((uint64_t)msg2.req_uint32 == uint32); \
+ TEST( (int64_t)msg2.req_sint32 == sint32); \
+ TEST( (int64_t)msg2.req_int64 == int64); \
+ TEST((uint64_t)msg2.req_uint64 == uint64); \
+ TEST( (int64_t)msg2.req_sint64 == sint64); \
+ } \
+ } \
+ \
+ if (expected_result) \
+ { \
+ pb_ostream_t s = pb_ostream_from_buffer(buffer2, sizeof(buffer2)); \
+ TEST(pb_encode(&s, IntSizes_fields, &msg2)); \
+ TEST(s.bytes_written == msgsize); \
+ TEST(memcmp(buffer1, buffer2, msgsize) == 0); \
+ } \
+}
+
+int main()
+{
+ int status = 0;
+
+ {
+ IntSizes msg = IntSizes_init_zero;
+
+ COMMENT("Test field sizes");
+ TEST(sizeof(msg.req_int8) == 1);
+ TEST(sizeof(msg.req_uint8) == 1);
+ TEST(sizeof(msg.req_sint8) == 1);
+ TEST(sizeof(msg.req_int16) == 2);
+ TEST(sizeof(msg.req_uint16) == 2);
+ TEST(sizeof(msg.req_sint16) == 2);
+ TEST(sizeof(msg.req_int32) == 4);
+ TEST(sizeof(msg.req_uint32) == 4);
+ TEST(sizeof(msg.req_sint32) == 4);
+ TEST(sizeof(msg.req_int64) == 8);
+ TEST(sizeof(msg.req_uint64) == 8);
+ TEST(sizeof(msg.req_sint64) == 8);
+ }
+
+ COMMENT("Test roundtrip at maximum value");
+ TEST_ROUNDTRIP(127, 255, 127,
+ 32767, 65535, 32767,
+ INT32_MAX, UINT32_MAX, INT32_MAX,
+ INT64_MAX, UINT64_MAX, INT64_MAX, true);
+
+ COMMENT("Test roundtrip at minimum value");
+ TEST_ROUNDTRIP(-128, 0, -128,
+ -32768, 0, -32768,
+ INT32_MIN, 0, INT32_MIN,
+ INT64_MIN, 0, INT64_MIN, true);
+
+ COMMENT("Test overflow detection");
+ TEST_ROUNDTRIP(-129, 0, -128,
+ -32768, 0, -32768,
+ INT32_MIN, 0, INT32_MIN,
+ INT64_MIN, 0, INT64_MIN, false);
+ TEST_ROUNDTRIP(127, 256, 127,
+ 32767, 65535, 32767,
+ INT32_MAX, UINT32_MAX, INT32_MAX,
+ INT64_MAX, UINT64_MAX, INT64_MAX, false);
+ TEST_ROUNDTRIP(-128, 0, -128,
+ -32768, 0, -32769,
+ INT32_MIN, 0, INT32_MIN,
+ INT64_MIN, 0, INT64_MIN, false);
+
+ if (status != 0)
+ fprintf(stdout, "\n\nSome tests FAILED!\n");
+
+ return status;
+} \ No newline at end of file
diff --git a/libs/nanopb/tests/io_errors/SConscript b/libs/nanopb/tests/io_errors/SConscript
new file mode 100644
index 00000000..60146cc0
--- /dev/null
+++ b/libs/nanopb/tests/io_errors/SConscript
@@ -0,0 +1,15 @@
+# Simulate io errors when encoding and decoding
+
+Import("env")
+
+c = Copy("$TARGET", "$SOURCE")
+env.Command("alltypes.proto", "#alltypes/alltypes.proto", c)
+
+env.NanopbProto(["alltypes", "alltypes.options"])
+
+ioerr = env.Program(["io_errors.c", "alltypes.pb.c",
+ "$COMMON/pb_encode.o", "$COMMON/pb_decode.o", "$COMMON/pb_common.o"])
+
+env.RunTest("io_errors.output", [ioerr, "$BUILD/alltypes/encode_alltypes.output"])
+
+
diff --git a/libs/nanopb/tests/io_errors/alltypes.options b/libs/nanopb/tests/io_errors/alltypes.options
new file mode 100644
index 00000000..0d5ab12b
--- /dev/null
+++ b/libs/nanopb/tests/io_errors/alltypes.options
@@ -0,0 +1,3 @@
+* max_size:16
+* max_count:5
+*.*fbytes fixed_length:true max_size:4
diff --git a/libs/nanopb/tests/io_errors/io_errors.c b/libs/nanopb/tests/io_errors/io_errors.c
new file mode 100644
index 00000000..76f35b08
--- /dev/null
+++ b/libs/nanopb/tests/io_errors/io_errors.c
@@ -0,0 +1,140 @@
+/* Simulate IO errors after each byte in a stream.
+ * Verifies proper error propagation.
+ */
+
+#include <stdio.h>
+#include <pb_decode.h>
+#include <pb_encode.h>
+#include "alltypes.pb.h"
+#include "test_helpers.h"
+
+typedef struct
+{
+ uint8_t *buffer;
+ size_t fail_after;
+} faulty_stream_t;
+
+bool read_callback(pb_istream_t *stream, uint8_t *buf, size_t count)
+{
+ faulty_stream_t *state = stream->state;
+
+ while (count--)
+ {
+ if (state->fail_after == 0)
+ PB_RETURN_ERROR(stream, "simulated");
+ state->fail_after--;
+ *buf++ = *state->buffer++;
+ }
+
+ return true;
+}
+bool write_callback(pb_ostream_t *stream, const uint8_t *buf, size_t count)
+{
+ faulty_stream_t *state = stream->state;
+
+ while (count--)
+ {
+ if (state->fail_after == 0)
+ PB_RETURN_ERROR(stream, "simulated");
+ state->fail_after--;
+ *state->buffer++ = *buf++;
+ }
+
+ return true;
+}
+
+int main()
+{
+ uint8_t buffer[2048];
+ size_t msglen;
+ AllTypes msg = AllTypes_init_zero;
+
+ /* Get some base data to run the tests with */
+ SET_BINARY_MODE(stdin);
+ msglen = fread(buffer, 1, sizeof(buffer), stdin);
+
+ /* Test IO errors on decoding */
+ {
+ bool status;
+ pb_istream_t stream = {&read_callback, NULL, SIZE_MAX};
+ faulty_stream_t fs;
+ size_t i;
+
+ for (i = 0; i < msglen; i++)
+ {
+ stream.bytes_left = msglen;
+ stream.state = &fs;
+ fs.buffer = buffer;
+ fs.fail_after = i;
+
+ status = pb_decode(&stream, AllTypes_fields, &msg);
+ if (status != false)
+ {
+ fprintf(stderr, "Unexpected success in decode\n");
+ return 2;
+ }
+ else if (strcmp(stream.errmsg, "simulated") != 0)
+ {
+ fprintf(stderr, "Wrong error in decode: %s\n", stream.errmsg);
+ return 3;
+ }
+ }
+
+ stream.bytes_left = msglen;
+ stream.state = &fs;
+ fs.buffer = buffer;
+ fs.fail_after = msglen;
+ status = pb_decode(&stream, AllTypes_fields, &msg);
+
+ if (!status)
+ {
+ fprintf(stderr, "Decoding failed: %s\n", stream.errmsg);
+ return 4;
+ }
+ }
+
+ /* Test IO errors on encoding */
+ {
+ bool status;
+ pb_ostream_t stream = {&write_callback, NULL, SIZE_MAX, 0};
+ faulty_stream_t fs;
+ size_t i;
+
+ for (i = 0; i < msglen; i++)
+ {
+ stream.max_size = msglen;
+ stream.bytes_written = 0;
+ stream.state = &fs;
+ fs.buffer = buffer;
+ fs.fail_after = i;
+
+ status = pb_encode(&stream, AllTypes_fields, &msg);
+ if (status != false)
+ {
+ fprintf(stderr, "Unexpected success in encode\n");
+ return 5;
+ }
+ else if (strcmp(stream.errmsg, "simulated") != 0)
+ {
+ fprintf(stderr, "Wrong error in encode: %s\n", stream.errmsg);
+ return 6;
+ }
+ }
+
+ stream.max_size = msglen;
+ stream.bytes_written = 0;
+ stream.state = &fs;
+ fs.buffer = buffer;
+ fs.fail_after = msglen;
+ status = pb_encode(&stream, AllTypes_fields, &msg);
+
+ if (!status)
+ {
+ fprintf(stderr, "Encoding failed: %s\n", stream.errmsg);
+ return 7;
+ }
+ }
+
+ return 0;
+}
+
diff --git a/libs/nanopb/tests/io_errors_pointers/SConscript b/libs/nanopb/tests/io_errors_pointers/SConscript
new file mode 100644
index 00000000..03727df9
--- /dev/null
+++ b/libs/nanopb/tests/io_errors_pointers/SConscript
@@ -0,0 +1,26 @@
+# Simulate io errors when encoding and decoding
+
+Import("env", "malloc_env")
+
+c = Copy("$TARGET", "$SOURCE")
+env.Command("alltypes.proto", "#alltypes/alltypes.proto", c)
+env.Command("io_errors.c", "#io_errors/io_errors.c", c)
+
+env.NanopbProto(["alltypes", "alltypes.options"])
+
+ioerr = env.Program(["io_errors.c", "alltypes.pb.c",
+ "$COMMON/pb_encode_with_malloc.o",
+ "$COMMON/pb_decode_with_malloc.o",
+ "$COMMON/pb_common_with_malloc.o",
+ "$COMMON/malloc_wrappers.o"])
+
+# Run tests under valgrind if available
+valgrind = env.WhereIs('valgrind')
+kwargs = {}
+if valgrind:
+ kwargs['COMMAND'] = valgrind
+ kwargs['ARGS'] = ["-q", "--error-exitcode=99", ioerr[0].abspath]
+
+env.RunTest("io_errors.output", [ioerr, "$BUILD/alltypes/encode_alltypes.output"], **kwargs)
+
+
diff --git a/libs/nanopb/tests/io_errors_pointers/alltypes.options b/libs/nanopb/tests/io_errors_pointers/alltypes.options
new file mode 100644
index 00000000..7e3ad1e5
--- /dev/null
+++ b/libs/nanopb/tests/io_errors_pointers/alltypes.options
@@ -0,0 +1,3 @@
+# Generate all fields as pointers.
+* type:FT_POINTER
+*.*fbytes fixed_length:true max_size:4
diff --git a/libs/nanopb/tests/mem_release/SConscript b/libs/nanopb/tests/mem_release/SConscript
new file mode 100644
index 00000000..6754e285
--- /dev/null
+++ b/libs/nanopb/tests/mem_release/SConscript
@@ -0,0 +1,13 @@
+Import("env", "malloc_env")
+
+env.NanopbProto("mem_release.proto")
+
+test = malloc_env.Program(["mem_release.c",
+ "mem_release.pb.c",
+ "$COMMON/pb_encode_with_malloc.o",
+ "$COMMON/pb_decode_with_malloc.o",
+ "$COMMON/pb_common_with_malloc.o",
+ "$COMMON/malloc_wrappers.o"])
+
+env.RunTest(test)
+
diff --git a/libs/nanopb/tests/mem_release/mem_release.c b/libs/nanopb/tests/mem_release/mem_release.c
new file mode 100644
index 00000000..dc6f87de
--- /dev/null
+++ b/libs/nanopb/tests/mem_release/mem_release.c
@@ -0,0 +1,187 @@
+/* Make sure that all fields are freed in various scenarios. */
+
+#include <pb_decode.h>
+#include <pb_encode.h>
+#include <malloc_wrappers.h>
+#include <stdio.h>
+#include <test_helpers.h>
+#include "mem_release.pb.h"
+
+#define TEST(x) if (!(x)) { \
+ fprintf(stderr, "Test " #x " on line %d failed.\n", __LINE__); \
+ return false; \
+ }
+
+static char *test_str_arr[] = {"1", "2", ""};
+static SubMessage test_msg_arr[] = {SubMessage_init_zero, SubMessage_init_zero};
+static pb_extension_t ext1, ext2;
+
+static void fill_TestMessage(TestMessage *msg)
+{
+ msg->static_req_submsg.dynamic_str = "12345";
+ msg->static_req_submsg.dynamic_str_arr_count = 3;
+ msg->static_req_submsg.dynamic_str_arr = test_str_arr;
+ msg->static_req_submsg.dynamic_submsg_count = 2;
+ msg->static_req_submsg.dynamic_submsg = test_msg_arr;
+ msg->static_req_submsg.dynamic_submsg[1].dynamic_str = "abc";
+ msg->static_opt_submsg.dynamic_str = "abc";
+ msg->static_rep_submsg_count = 2;
+ msg->static_rep_submsg[1].dynamic_str = "abc";
+ msg->has_static_opt_submsg = true;
+ msg->dynamic_submsg = &msg->static_req_submsg;
+
+ msg->extensions = &ext1;
+ ext1.type = &dynamic_ext;
+ ext1.dest = &msg->static_req_submsg;
+ ext1.next = &ext2;
+ ext2.type = &static_ext;
+ ext2.dest = &msg->static_req_submsg;
+ ext2.next = NULL;
+}
+
+/* Basic fields, nested submessages, extensions */
+static bool test_TestMessage()
+{
+ uint8_t buffer[256];
+ size_t msgsize;
+
+ /* Construct a message with various fields filled in */
+ {
+ TestMessage msg = TestMessage_init_zero;
+ pb_ostream_t stream;
+
+ fill_TestMessage(&msg);
+
+ stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
+ if (!pb_encode(&stream, TestMessage_fields, &msg))
+ {
+ fprintf(stderr, "Encode failed: %s\n", PB_GET_ERROR(&stream));
+ return false;
+ }
+ msgsize = stream.bytes_written;
+ }
+
+ /* Output encoded message for debug */
+ SET_BINARY_MODE(stdout);
+ fwrite(buffer, 1, msgsize, stdout);
+
+ /* Decode memory using dynamic allocation */
+ {
+ TestMessage msg = TestMessage_init_zero;
+ pb_istream_t stream;
+ SubMessage ext2_dest;
+
+ msg.extensions = &ext1;
+ ext1.type = &dynamic_ext;
+ ext1.dest = NULL;
+ ext1.next = &ext2;
+ ext2.type = &static_ext;
+ ext2.dest = &ext2_dest;
+ ext2.next = NULL;
+
+ stream = pb_istream_from_buffer(buffer, msgsize);
+ if (!pb_decode(&stream, TestMessage_fields, &msg))
+ {
+ fprintf(stderr, "Decode failed: %s\n", PB_GET_ERROR(&stream));
+ return false;
+ }
+
+ /* Make sure it encodes back to same data */
+ {
+ uint8_t buffer2[256];
+ pb_ostream_t ostream = pb_ostream_from_buffer(buffer2, sizeof(buffer2));
+ TEST(pb_encode(&ostream, TestMessage_fields, &msg));
+ TEST(ostream.bytes_written == msgsize);
+ TEST(memcmp(buffer, buffer2, msgsize) == 0);
+ }
+
+ /* Make sure that malloc counters work */
+ TEST(get_alloc_count() > 0);
+
+ /* Make sure that pb_release releases everything */
+ pb_release(TestMessage_fields, &msg);
+ TEST(get_alloc_count() == 0);
+
+ /* Check that double-free is a no-op */
+ pb_release(TestMessage_fields, &msg);
+ TEST(get_alloc_count() == 0);
+ }
+
+ return true;
+}
+
+/* Oneofs */
+static bool test_OneofMessage()
+{
+ uint8_t buffer[256];
+ size_t msgsize;
+
+ {
+ pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
+
+ /* Encode first with TestMessage */
+ {
+ OneofMessage msg = OneofMessage_init_zero;
+ msg.which_msgs = OneofMessage_msg1_tag;
+
+ fill_TestMessage(&msg.msgs.msg1);
+
+ if (!pb_encode(&stream, OneofMessage_fields, &msg))
+ {
+ fprintf(stderr, "Encode failed: %s\n", PB_GET_ERROR(&stream));
+ return false;
+ }
+ }
+
+ /* Encode second with SubMessage, invoking 'merge' behaviour */
+ {
+ OneofMessage msg = OneofMessage_init_zero;
+ msg.which_msgs = OneofMessage_msg2_tag;
+
+ msg.first = 999;
+ msg.msgs.msg2.dynamic_str = "ABCD";
+ msg.last = 888;
+
+ if (!pb_encode(&stream, OneofMessage_fields, &msg))
+ {
+ fprintf(stderr, "Encode failed: %s\n", PB_GET_ERROR(&stream));
+ return false;
+ }
+ }
+ msgsize = stream.bytes_written;
+ }
+
+ {
+ OneofMessage msg = OneofMessage_init_zero;
+ pb_istream_t stream = pb_istream_from_buffer(buffer, msgsize);
+ if (!pb_decode(&stream, OneofMessage_fields, &msg))
+ {
+ fprintf(stderr, "Decode failed: %s\n", PB_GET_ERROR(&stream));
+ return false;
+ }
+
+ TEST(msg.first == 999);
+ TEST(msg.which_msgs == OneofMessage_msg2_tag);
+ TEST(msg.msgs.msg2.dynamic_str);
+ TEST(strcmp(msg.msgs.msg2.dynamic_str, "ABCD") == 0);
+ TEST(msg.msgs.msg2.dynamic_str_arr == NULL);
+ TEST(msg.msgs.msg2.dynamic_submsg == NULL);
+ TEST(msg.last == 888);
+
+ pb_release(OneofMessage_fields, &msg);
+ TEST(get_alloc_count() == 0);
+ pb_release(OneofMessage_fields, &msg);
+ TEST(get_alloc_count() == 0);
+ }
+
+ return true;
+}
+
+int main()
+{
+ if (test_TestMessage() && test_OneofMessage())
+ return 0;
+ else
+ return 1;
+}
+
diff --git a/libs/nanopb/tests/mem_release/mem_release.proto b/libs/nanopb/tests/mem_release/mem_release.proto
new file mode 100644
index 00000000..0816dc22
--- /dev/null
+++ b/libs/nanopb/tests/mem_release/mem_release.proto
@@ -0,0 +1,35 @@
+syntax = "proto2";
+import "nanopb.proto";
+
+message SubMessage
+{
+ optional string dynamic_str = 1 [(nanopb).type = FT_POINTER];
+ repeated string dynamic_str_arr = 2 [(nanopb).type = FT_POINTER];
+ repeated SubMessage dynamic_submsg = 3 [(nanopb).type = FT_POINTER];
+}
+
+message TestMessage
+{
+ required SubMessage static_req_submsg = 1 [(nanopb).type = FT_STATIC];
+ optional SubMessage dynamic_submsg = 2 [(nanopb).type = FT_POINTER];
+ optional SubMessage static_opt_submsg = 3 [(nanopb).type = FT_STATIC];
+ repeated SubMessage static_rep_submsg = 4 [(nanopb).type = FT_STATIC, (nanopb).max_count=2];
+ extensions 100 to 200;
+}
+
+extend TestMessage
+{
+ optional SubMessage dynamic_ext = 100 [(nanopb).type = FT_POINTER];
+ optional SubMessage static_ext = 101 [(nanopb).type = FT_STATIC];
+}
+
+message OneofMessage
+{
+ required int32 first = 1;
+ oneof msgs
+ {
+ TestMessage msg1 = 2;
+ SubMessage msg2 = 3;
+ }
+ required int32 last = 4;
+}
diff --git a/libs/nanopb/tests/message_sizes/SConscript b/libs/nanopb/tests/message_sizes/SConscript
new file mode 100644
index 00000000..e7524e02
--- /dev/null
+++ b/libs/nanopb/tests/message_sizes/SConscript
@@ -0,0 +1,11 @@
+# Test the generation of message size #defines
+
+Import('env')
+
+incpath = env.Clone()
+incpath.Append(PROTOCPATH = '#message_sizes')
+
+incpath.NanopbProto("messages1")
+incpath.NanopbProto("messages2")
+
+incpath.Program(['dummy.c', 'messages1.pb.c', 'messages2.pb.c'])
diff --git a/libs/nanopb/tests/message_sizes/dummy.c b/libs/nanopb/tests/message_sizes/dummy.c
new file mode 100644
index 00000000..767ad463
--- /dev/null
+++ b/libs/nanopb/tests/message_sizes/dummy.c
@@ -0,0 +1,9 @@
+/* Just test that the file can be compiled successfully. */
+
+#include "messages2.pb.h"
+
+int main()
+{
+ return xmit_size;
+}
+
diff --git a/libs/nanopb/tests/message_sizes/messages1.proto b/libs/nanopb/tests/message_sizes/messages1.proto
new file mode 100644
index 00000000..b66fad71
--- /dev/null
+++ b/libs/nanopb/tests/message_sizes/messages1.proto
@@ -0,0 +1,29 @@
+syntax = "proto2";
+
+enum MessageStatus {
+ FAIL = 0;
+ OK = 1;
+};
+
+message MessageInfo {
+ required fixed32 msg_id = 1;
+ optional fixed32 interface_id = 2;
+}
+
+message MessageResponseInfo {
+ required fixed64 interface_id = 1;
+ required fixed32 seq = 2;
+ required fixed32 msg_id = 3;
+}
+
+message MessageHeader {
+ required MessageInfo info = 1;
+ optional MessageResponseInfo response_info = 2;
+ optional MessageResponse response = 3;
+}
+
+message MessageResponse {
+ required MessageStatus status = 1;
+ required fixed32 seq = 2;
+}
+
diff --git a/libs/nanopb/tests/message_sizes/messages2.proto b/libs/nanopb/tests/message_sizes/messages2.proto
new file mode 100644
index 00000000..67614080
--- /dev/null
+++ b/libs/nanopb/tests/message_sizes/messages2.proto
@@ -0,0 +1,10 @@
+syntax = "proto2";
+
+import 'nanopb.proto';
+import 'messages1.proto';
+
+message xmit {
+ required MessageHeader header = 1;
+ required bytes data = 2 [(nanopb).max_size = 128];
+}
+
diff --git a/libs/nanopb/tests/missing_fields/SConscript b/libs/nanopb/tests/missing_fields/SConscript
new file mode 100644
index 00000000..86ba0833
--- /dev/null
+++ b/libs/nanopb/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", "$COMMON/pb_common.o"])
+env.RunTest(test)
+
diff --git a/libs/nanopb/tests/missing_fields/missing_fields.c b/libs/nanopb/tests/missing_fields/missing_fields.c
new file mode 100644
index 00000000..8aded827
--- /dev/null
+++ b/libs/nanopb/tests/missing_fields/missing_fields.c
@@ -0,0 +1,53 @@
+/* Checks that missing required fields are detected properly */
+
+#include <stdio.h>
+#include <pb_encode.h>
+#include <pb_decode.h>
+#include "missing_fields.pb.h"
+
+int main()
+{
+ uint8_t buffer[512];
+ size_t size;
+
+ /* Create a message with one missing field */
+ {
+ 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");
+ return 1;
+ }
+
+ size = stream.bytes_written;
+ }
+
+ /* Test that it decodes properly if we don't require that field */
+ {
+ MissingField msg = {0};
+ pb_istream_t stream = pb_istream_from_buffer(buffer, size);
+
+ if (!pb_decode(&stream, MissingField_fields, &msg))
+ {
+ printf("Decode failed: %s\n", PB_GET_ERROR(&stream));
+ return 2;
+ }
+ }
+
+ /* Test that it does *not* decode properly if we require the field */
+ {
+ AllFields msg = {0};
+ pb_istream_t stream = pb_istream_from_buffer(buffer, size);
+
+ if (pb_decode(&stream, AllFields_fields, &msg))
+ {
+ printf("Decode didn't detect missing field.\n");
+ return 3;
+ }
+ }
+
+ return 0; /* All ok */
+}
+
diff --git a/libs/nanopb/tests/missing_fields/missing_fields.proto b/libs/nanopb/tests/missing_fields/missing_fields.proto
new file mode 100644
index 00000000..cc5e550b
--- /dev/null
+++ b/libs/nanopb/tests/missing_fields/missing_fields.proto
@@ -0,0 +1,140 @@
+/* Test for one missing field among many */
+
+syntax = "proto2";
+
+message AllFields
+{
+ required int32 field1 = 1;
+ required int32 field2 = 2;
+ required int32 field3 = 3;
+ required int32 field4 = 4;
+ required int32 field5 = 5;
+ required int32 field6 = 6;
+ required int32 field7 = 7;
+ required int32 field8 = 8;
+ required int32 field9 = 9;
+ required int32 field10 = 10;
+ required int32 field11 = 11;
+ required int32 field12 = 12;
+ required int32 field13 = 13;
+ required int32 field14 = 14;
+ required int32 field15 = 15;
+ required int32 field16 = 16;
+ required int32 field17 = 17;
+ required int32 field18 = 18;
+ required int32 field19 = 19;
+ required int32 field20 = 20;
+ required int32 field21 = 21;
+ required int32 field22 = 22;
+ required int32 field23 = 23;
+ required int32 field24 = 24;
+ required int32 field25 = 25;
+ required int32 field26 = 26;
+ required int32 field27 = 27;
+ required int32 field28 = 28;
+ required int32 field29 = 29;
+ required int32 field30 = 30;
+ required int32 field31 = 31;
+ required int32 field32 = 32;
+ required int32 field33 = 33;
+ required int32 field34 = 34;
+ required int32 field35 = 35;
+ required int32 field36 = 36;
+ required int32 field37 = 37;
+ required int32 field38 = 38;
+ required int32 field39 = 39;
+ required int32 field40 = 40;
+ required int32 field41 = 41;
+ required int32 field42 = 42;
+ required int32 field43 = 43;
+ required int32 field44 = 44;
+ required int32 field45 = 45;
+ required int32 field46 = 46;
+ required int32 field47 = 47;
+ required int32 field48 = 48;
+ required int32 field49 = 49;
+ required int32 field50 = 50;
+ required int32 field51 = 51;
+ required int32 field52 = 52;
+ required int32 field53 = 53;
+ required int32 field54 = 54;
+ required int32 field55 = 55;
+ required int32 field56 = 56;
+ required int32 field57 = 57;
+ required int32 field58 = 58;
+ required int32 field59 = 59;
+ required int32 field60 = 60;
+ required int32 field61 = 61;
+ required int32 field62 = 62;
+ required int32 field63 = 63;
+ required int32 field64 = 64;
+}
+
+message MissingField
+{
+ required int32 field1 = 1;
+ required int32 field2 = 2;
+ required int32 field3 = 3;
+ required int32 field4 = 4;
+ required int32 field5 = 5;
+ required int32 field6 = 6;
+ required int32 field7 = 7;
+ required int32 field8 = 8;
+ required int32 field9 = 9;
+ required int32 field10 = 10;
+ required int32 field11 = 11;
+ required int32 field12 = 12;
+ required int32 field13 = 13;
+ required int32 field14 = 14;
+ required int32 field15 = 15;
+ required int32 field16 = 16;
+ required int32 field17 = 17;
+ required int32 field18 = 18;
+ required int32 field19 = 19;
+ required int32 field20 = 20;
+ required int32 field21 = 21;
+ required int32 field22 = 22;
+ required int32 field23 = 23;
+ required int32 field24 = 24;
+ required int32 field25 = 25;
+ required int32 field26 = 26;
+ required int32 field27 = 27;
+ required int32 field28 = 28;
+ required int32 field29 = 29;
+ required int32 field30 = 30;
+ required int32 field31 = 31;
+ required int32 field32 = 32;
+ required int32 field33 = 33;
+ required int32 field34 = 34;
+ required int32 field35 = 35;
+ required int32 field36 = 36;
+ required int32 field37 = 37;
+ required int32 field38 = 38;
+ required int32 field39 = 39;
+ required int32 field40 = 40;
+ required int32 field41 = 41;
+ required int32 field42 = 42;
+ required int32 field43 = 43;
+ required int32 field44 = 44;
+ required int32 field45 = 45;
+ required int32 field46 = 46;
+ required int32 field47 = 47;
+ required int32 field48 = 48;
+ required int32 field49 = 49;
+ required int32 field50 = 50;
+ required int32 field51 = 51;
+ required int32 field52 = 52;
+ required int32 field53 = 53;
+ required int32 field54 = 54;
+ required int32 field55 = 55;
+ required int32 field56 = 56;
+ required int32 field57 = 57;
+ required int32 field58 = 58;
+ required int32 field59 = 59;
+ required int32 field60 = 60;
+ required int32 field61 = 61;
+ required int32 field62 = 62;
+/* required int32 field63 = 63; */
+ required int32 field64 = 64;
+}
+
diff --git a/libs/nanopb/tests/multiple_files/SConscript b/libs/nanopb/tests/multiple_files/SConscript
new file mode 100644
index 00000000..b1281e17
--- /dev/null
+++ b/libs/nanopb/tests/multiple_files/SConscript
@@ -0,0 +1,16 @@
+# Test that multiple .proto files don't cause name collisions.
+
+Import("env")
+
+incpath = env.Clone()
+incpath.Append(PROTOCPATH = '#multiple_files')
+incpath.Append(CPPPATH = '$BUILD/multiple_files')
+
+incpath.NanopbProto(["multifile1", "multifile1.options"])
+incpath.NanopbProto("multifile2")
+incpath.NanopbProto("subdir/multifile2")
+test = incpath.Program(["test_multiple_files.c", "multifile1.pb.c",
+ "multifile2.pb.c", "subdir/multifile2.pb.c"])
+
+env.RunTest(test)
+
diff --git a/libs/nanopb/tests/multiple_files/multifile1.options b/libs/nanopb/tests/multiple_files/multifile1.options
new file mode 100644
index 00000000..c44d2669
--- /dev/null
+++ b/libs/nanopb/tests/multiple_files/multifile1.options
@@ -0,0 +1 @@
+StaticMessage.repint32 max_count:5
diff --git a/libs/nanopb/tests/multiple_files/multifile1.proto b/libs/nanopb/tests/multiple_files/multifile1.proto
new file mode 100644
index 00000000..18f2c672
--- /dev/null
+++ b/libs/nanopb/tests/multiple_files/multifile1.proto
@@ -0,0 +1,34 @@
+syntax = "proto2";
+
+message SubMessage {
+ optional string stringvalue = 1;
+ repeated int32 int32value = 2;
+ repeated fixed32 fixed32value = 3;
+ repeated fixed64 fixed64value = 4;
+}
+
+message TestMessage {
+ optional string stringvalue = 1;
+ repeated int32 int32value = 2;
+ repeated fixed32 fixed32value = 3;
+ repeated fixed64 fixed64value = 4;
+ optional SubMessage submsg = 5;
+ repeated string repeatedstring = 6;
+}
+
+message StaticMessage {
+ repeated fixed32 repint32 = 1;
+}
+
+enum SignedEnum {
+ SE_MIN = -128;
+ SE_MAX = 127;
+}
+
+enum UnsignedEnum {
+ UE_MIN = 0;
+ UE_MAX = 255;
+}
+
+
+
diff --git a/libs/nanopb/tests/multiple_files/multifile2.proto b/libs/nanopb/tests/multiple_files/multifile2.proto
new file mode 100644
index 00000000..4af45fd9
--- /dev/null
+++ b/libs/nanopb/tests/multiple_files/multifile2.proto
@@ -0,0 +1,22 @@
+// Test if including generated header file for this file + implicit include of
+// multifile2.pb.h still compiles. Used with test_compiles.c.
+syntax = "proto2";
+
+import "multifile1.proto";
+
+message Callback2Message {
+ required TestMessage tstmsg = 1;
+ required SubMessage submsg = 2;
+}
+
+message OneofMessage {
+ oneof msgs {
+ StaticMessage tstmsg = 1;
+ }
+}
+
+message Enums {
+ required SignedEnum senum = 1;
+ required UnsignedEnum uenum = 2;
+}
+
diff --git a/libs/nanopb/tests/multiple_files/subdir/multifile2.proto b/libs/nanopb/tests/multiple_files/subdir/multifile2.proto
new file mode 100644
index 00000000..847a9290
--- /dev/null
+++ b/libs/nanopb/tests/multiple_files/subdir/multifile2.proto
@@ -0,0 +1,25 @@
+syntax = "proto2";
+
+package subdir;
+
+import "multifile1.proto";
+
+message Callback2Message {
+ required TestMessage tstmsg = 1;
+ required SubMessage submsg = 2;
+}
+
+message OneofMessage {
+ oneof msgs {
+ StaticMessage tstmsg = 1;
+ }
+}
+
+message Enums {
+ required SignedEnum senum = 1;
+ required UnsignedEnum uenum = 2;
+}
+
+message SubdirMessage {
+ required int32 foo = 1 [default = 15];
+}
diff --git a/libs/nanopb/tests/multiple_files/test_multiple_files.c b/libs/nanopb/tests/multiple_files/test_multiple_files.c
new file mode 100644
index 00000000..70a3e596
--- /dev/null
+++ b/libs/nanopb/tests/multiple_files/test_multiple_files.c
@@ -0,0 +1,30 @@
+/*
+ * Tests if this still compiles when multiple .proto files are involved.
+ */
+
+#include <stdio.h>
+#include <pb_encode.h>
+#include "unittests.h"
+#include "multifile2.pb.h"
+#include "subdir/multifile2.pb.h"
+
+int main()
+{
+ int status = 0;
+
+ /* Test that included file options are properly loaded */
+ TEST(OneofMessage_size == 27);
+
+ /* Check that enum signedness is detected properly */
+ TEST(PB_LTYPE(Enums_fields[0].type) == PB_LTYPE_VARINT);
+ TEST(PB_LTYPE(Enums_fields[1].type) == PB_LTYPE_UVARINT);
+
+ /* Test that subdir file is correctly included */
+ {
+ subdir_SubdirMessage foo = subdir_SubdirMessage_init_default;
+ TEST(foo.foo == 15);
+ /* TEST(subdir_OneofMessage_size == 27); */ /* TODO: Issue #172 */
+ }
+
+ return status;
+}
diff --git a/libs/nanopb/tests/no_errmsg/SConscript b/libs/nanopb/tests/no_errmsg/SConscript
new file mode 100644
index 00000000..629bfa68
--- /dev/null
+++ b/libs/nanopb/tests/no_errmsg/SConscript
@@ -0,0 +1,28 @@
+# Run the alltypes test case, but compile with PB_NO_ERRMSG=1
+
+Import("env")
+
+# Take copy of the files for custom build.
+c = Copy("$TARGET", "$SOURCE")
+env.Command("alltypes.pb.h", "$BUILD/alltypes/alltypes.pb.h", c)
+env.Command("alltypes.pb.c", "$BUILD/alltypes/alltypes.pb.c", c)
+env.Command("encode_alltypes.c", "$BUILD/alltypes/encode_alltypes.c", c)
+env.Command("decode_alltypes.c", "$BUILD/alltypes/decode_alltypes.c", c)
+
+# Define the compilation options
+opts = env.Clone()
+opts.Append(CPPDEFINES = {'PB_NO_ERRMSG': 1})
+
+# Build new version of core
+strict = opts.Clone()
+strict.Append(CFLAGS = strict['CORECFLAGS'])
+strict.Object("pb_decode_noerr.o", "$NANOPB/pb_decode.c")
+strict.Object("pb_encode_noerr.o", "$NANOPB/pb_encode.c")
+strict.Object("pb_common_noerr.o", "$NANOPB/pb_common.c")
+
+# Now build and run the test normally.
+enc = opts.Program(["encode_alltypes.c", "alltypes.pb.c", "pb_encode_noerr.o", "pb_common_noerr.o"])
+dec = opts.Program(["decode_alltypes.c", "alltypes.pb.c", "pb_decode_noerr.o", "pb_common_noerr.o"])
+
+env.RunTest(enc)
+env.RunTest([dec, "encode_alltypes.output"])
diff --git a/libs/nanopb/tests/no_messages/SConscript b/libs/nanopb/tests/no_messages/SConscript
new file mode 100644
index 00000000..6492e2cf
--- /dev/null
+++ b/libs/nanopb/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/libs/nanopb/tests/no_messages/no_messages.proto b/libs/nanopb/tests/no_messages/no_messages.proto
new file mode 100644
index 00000000..45bb2e66
--- /dev/null
+++ b/libs/nanopb/tests/no_messages/no_messages.proto
@@ -0,0 +1,9 @@
+/* Test that a file without any messages works. */
+
+syntax = "proto2";
+
+enum Test {
+ First = 1;
+}
+
+
diff --git a/libs/nanopb/tests/oneof/SConscript b/libs/nanopb/tests/oneof/SConscript
new file mode 100644
index 00000000..22634fb0
--- /dev/null
+++ b/libs/nanopb/tests/oneof/SConscript
@@ -0,0 +1,33 @@
+# Test the 'oneof' feature for generating C unions.
+
+Import('env')
+
+import re
+
+match = None
+if 'PROTOC_VERSION' in env:
+ match = re.search('([0-9]+).([0-9]+).([0-9]+)', env['PROTOC_VERSION'])
+
+if match:
+ version = map(int, match.groups())
+
+# Oneof is supported by protoc >= 2.6.0
+if env.GetOption('clean') or (match and (version[0] > 2 or (version[0] == 2 and version[1] >= 6))):
+ env.NanopbProto('oneof')
+
+ enc = env.Program(['encode_oneof.c',
+ 'oneof.pb.c',
+ '$COMMON/pb_encode.o',
+ '$COMMON/pb_common.o'])
+
+ dec = env.Program(['decode_oneof.c',
+ 'oneof.pb.c',
+ '$COMMON/pb_decode.o',
+ '$COMMON/pb_common.o'])
+
+ env.RunTest("message1.pb", enc, ARGS = ['1'])
+ env.RunTest("message1.txt", [dec, 'message1.pb'], ARGS = ['1'])
+ env.RunTest("message2.pb", enc, ARGS = ['2'])
+ env.RunTest("message2.txt", [dec, 'message2.pb'], ARGS = ['2'])
+ env.RunTest("message3.pb", enc, ARGS = ['3'])
+ env.RunTest("message3.txt", [dec, 'message3.pb'], ARGS = ['3'])
diff --git a/libs/nanopb/tests/oneof/decode_oneof.c b/libs/nanopb/tests/oneof/decode_oneof.c
new file mode 100644
index 00000000..37075cd6
--- /dev/null
+++ b/libs/nanopb/tests/oneof/decode_oneof.c
@@ -0,0 +1,131 @@
+/* Decode a message using oneof fields */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pb_decode.h>
+#include "oneof.pb.h"
+#include "test_helpers.h"
+#include "unittests.h"
+
+/* Test the 'OneOfMessage' */
+int test_oneof_1(pb_istream_t *stream, int option)
+{
+ OneOfMessage msg;
+ int status = 0;
+
+ /* To better catch initialization errors */
+ memset(&msg, 0xAA, sizeof(msg));
+
+ if (!pb_decode(stream, OneOfMessage_fields, &msg))
+ {
+ printf("Decoding failed: %s\n", PB_GET_ERROR(stream));
+ return 1;
+ }
+
+ /* Check that the basic fields work normally */
+ TEST(msg.prefix == 123);
+ TEST(msg.suffix == 321);
+
+ /* Check that we got the right oneof according to command line */
+ if (option == 1)
+ {
+ TEST(msg.which_values == OneOfMessage_first_tag);
+ TEST(msg.values.first == 999);
+ }
+ else if (option == 2)
+ {
+ TEST(msg.which_values == OneOfMessage_second_tag);
+ TEST(strcmp(msg.values.second, "abcd") == 0);
+ }
+ else if (option == 3)
+ {
+ TEST(msg.which_values == OneOfMessage_third_tag);
+ TEST(msg.values.third.array[0] == 1);
+ TEST(msg.values.third.array[1] == 2);
+ TEST(msg.values.third.array[2] == 3);
+ TEST(msg.values.third.array[3] == 4);
+ TEST(msg.values.third.array[4] == 5);
+ }
+
+ return status;
+}
+
+
+/* Test the 'PlainOneOfMessage' */
+int test_oneof_2(pb_istream_t *stream, int option)
+{
+ PlainOneOfMessage msg = PlainOneOfMessage_init_zero;
+ int status = 0;
+
+ if (!pb_decode(stream, PlainOneOfMessage_fields, &msg))
+ {
+ printf("Decoding failed: %s\n", PB_GET_ERROR(stream));
+ return 1;
+ }
+
+ /* Check that we got the right oneof according to command line */
+ if (option == 1)
+ {
+ TEST(msg.which_values == OneOfMessage_first_tag);
+ TEST(msg.values.first == 999);
+ }
+ else if (option == 2)
+ {
+ TEST(msg.which_values == OneOfMessage_second_tag);
+ TEST(strcmp(msg.values.second, "abcd") == 0);
+ }
+ else if (option == 3)
+ {
+ TEST(msg.which_values == OneOfMessage_third_tag);
+ TEST(msg.values.third.array[0] == 1);
+ TEST(msg.values.third.array[1] == 2);
+ TEST(msg.values.third.array[2] == 3);
+ TEST(msg.values.third.array[3] == 4);
+ TEST(msg.values.third.array[4] == 5);
+ }
+
+ return status;
+}
+
+int main(int argc, char **argv)
+{
+ uint8_t buffer[OneOfMessage_size];
+ size_t count;
+ int option;
+
+ if (argc != 2)
+ {
+ fprintf(stderr, "Usage: decode_oneof [number]\n");
+ return 1;
+ }
+ option = atoi(argv[1]);
+
+ SET_BINARY_MODE(stdin);
+ count = fread(buffer, 1, sizeof(buffer), stdin);
+
+ if (!feof(stdin))
+ {
+ printf("Message does not fit in buffer\n");
+ return 1;
+ }
+
+ {
+ int status = 0;
+ pb_istream_t stream;
+
+ stream = pb_istream_from_buffer(buffer, count);
+ status = test_oneof_1(&stream, option);
+
+ if (status != 0)
+ return status;
+
+ stream = pb_istream_from_buffer(buffer, count);
+ status = test_oneof_2(&stream, option);
+
+ if (status != 0)
+ return status;
+ }
+
+ return 0;
+}
diff --git a/libs/nanopb/tests/oneof/encode_oneof.c b/libs/nanopb/tests/oneof/encode_oneof.c
new file mode 100644
index 00000000..913d2d43
--- /dev/null
+++ b/libs/nanopb/tests/oneof/encode_oneof.c
@@ -0,0 +1,64 @@
+/* Encode a message using oneof fields */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <pb_encode.h>
+#include "oneof.pb.h"
+#include "test_helpers.h"
+
+int main(int argc, char **argv)
+{
+ uint8_t buffer[OneOfMessage_size];
+ OneOfMessage msg = OneOfMessage_init_zero;
+ pb_ostream_t stream;
+ int option;
+
+ if (argc != 2)
+ {
+ fprintf(stderr, "Usage: encode_oneof [number]\n");
+ return 1;
+ }
+ option = atoi(argv[1]);
+
+ /* Prefix and suffix are used to test that the union does not disturb
+ * other fields in the same message. */
+ msg.prefix = 123;
+
+ /* We encode one of the 'values' fields based on command line argument */
+ if (option == 1)
+ {
+ msg.which_values = OneOfMessage_first_tag;
+ msg.values.first = 999;
+ }
+ else if (option == 2)
+ {
+ msg.which_values = OneOfMessage_second_tag;
+ strcpy(msg.values.second, "abcd");
+ }
+ else if (option == 3)
+ {
+ msg.which_values = OneOfMessage_third_tag;
+ msg.values.third.array_count = 5;
+ msg.values.third.array[0] = 1;
+ msg.values.third.array[1] = 2;
+ msg.values.third.array[2] = 3;
+ msg.values.third.array[3] = 4;
+ msg.values.third.array[4] = 5;
+ }
+
+ msg.suffix = 321;
+
+ stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
+
+ if (pb_encode(&stream, OneOfMessage_fields, &msg))
+ {
+ SET_BINARY_MODE(stdout);
+ fwrite(buffer, 1, stream.bytes_written, stdout);
+ return 0;
+ }
+ else
+ {
+ fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream));
+ return 1;
+ }
+}
diff --git a/libs/nanopb/tests/oneof/oneof.proto b/libs/nanopb/tests/oneof/oneof.proto
new file mode 100644
index 00000000..b4fe56f2
--- /dev/null
+++ b/libs/nanopb/tests/oneof/oneof.proto
@@ -0,0 +1,32 @@
+syntax = "proto2";
+
+import 'nanopb.proto';
+
+message SubMessage
+{
+ repeated int32 array = 1 [(nanopb).max_count = 8];
+}
+
+/* Oneof in a message with other fields */
+message OneOfMessage
+{
+ required int32 prefix = 1;
+ oneof values
+ {
+ int32 first = 5;
+ string second = 6 [(nanopb).max_size = 8];
+ SubMessage third = 7;
+ }
+ required int32 suffix = 99;
+}
+
+/* Oneof in a message by itself */
+message PlainOneOfMessage
+{
+ oneof values
+ {
+ int32 first = 5;
+ string second = 6 [(nanopb).max_size = 8];
+ SubMessage third = 7;
+ }
+} \ No newline at end of file
diff --git a/libs/nanopb/tests/options/SConscript b/libs/nanopb/tests/options/SConscript
new file mode 100644
index 00000000..215e3bd0
--- /dev/null
+++ b/libs/nanopb/tests/options/SConscript
@@ -0,0 +1,12 @@
+# 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'])
+
+env.NanopbProto("proto3_options")
+env.Object('proto3_options.pb.c')
+env.Match(['proto3_options.pb.h', 'proto3_options.expected'])
+
diff --git a/libs/nanopb/tests/options/options.expected b/libs/nanopb/tests/options/options.expected
new file mode 100644
index 00000000..9e47e6a4
--- /dev/null
+++ b/libs/nanopb/tests/options/options.expected
@@ -0,0 +1,20 @@
+char filesize\[20\];
+char msgsize\[30\];
+char fieldsize\[40\];
+char fieldlen\[41\];
+pb_callback_t int32_callback;
+\sEnumValue1 = 1
+Message5_EnumValue1
+} pb_packed my_packed_struct;
+! skipped_field
+! SkippedMessage
+#define PB_MSG_103 Message3
+#define PB_MSG_104 Message4
+#define PB_MSG_105 Message5
+#define OPTIONS_MESSAGES \\
+\s+PB_MSG\(103,[0-9]*,Message3\) \\
+\s+PB_MSG\(104,-1,Message4\) \\
+\s+PB_MSG\(105,[0-9]*,Message5\) \\
+#define Message5_msgid 105
+! has_proto3field
+
diff --git a/libs/nanopb/tests/options/options.proto b/libs/nanopb/tests/options/options.proto
new file mode 100644
index 00000000..c6ca5e25
--- /dev/null
+++ b/libs/nanopb/tests/options/options.proto
@@ -0,0 +1,98 @@
+/* Test nanopb option parsing.
+ * options.expected lists the patterns that are searched for in the output.
+ */
+
+syntax = "proto2";
+
+import "nanopb.proto";
+
+// File level options
+option (nanopb_fileopt).max_size = 20;
+
+message Message1
+{
+ required string filesize = 1;
+}
+
+// Message level options
+message Message2
+{
+ option (nanopb_msgopt).max_size = 30;
+ required string msgsize = 1;
+}
+
+// Field level options
+message Message3
+{
+ option (nanopb_msgopt).msgid = 103;
+ required string fieldsize = 1 [(nanopb).max_size = 40];
+ required string fieldlen = 2 [(nanopb).max_length = 40];
+}
+
+// Forced callback field
+message Message4
+{
+ option (nanopb_msgopt).msgid = 104;
+ required int32 int32_callback = 1 [(nanopb).type = FT_CALLBACK];
+}
+
+// Short enum names
+enum Enum1
+{
+ option (nanopb_enumopt).long_names = false;
+ EnumValue1 = 1;
+ EnumValue2 = 2;
+}
+
+message EnumTest
+{
+ required Enum1 field = 1 [default = EnumValue2];
+}
+
+// Short enum names inside message
+message Message5
+{
+ option (nanopb_msgopt).msgid = 105;
+ enum Enum2
+ {
+ option (nanopb_enumopt).long_names = false;
+ EnumValue1 = 1;
+ }
+ required Enum2 field = 1 [default = EnumValue1];
+}
+
+// Packed structure
+message my_packed_struct
+{
+ option (nanopb_msgopt).packed_struct = true;
+ optional int32 myfield = 1;
+}
+
+// Message with ignored field
+message Message6
+{
+ required int32 field1 = 1;
+ optional int32 skipped_field = 2 [(nanopb).type = FT_IGNORE];
+}
+
+// Message that is skipped
+message SkippedMessage
+{
+ option (nanopb_msgopt).skip_message = true;
+ required int32 foo = 1;
+}
+
+// Message with oneof field
+message OneofMessage
+{
+ oneof foo {
+ int32 bar = 1;
+ }
+}
+
+// Proto3-style optional field in proto2 file
+message Proto3Field
+{
+ optional int32 proto3field = 1 [(nanopb).proto3 = true];
+}
+
diff --git a/libs/nanopb/tests/options/proto3_options.expected b/libs/nanopb/tests/options/proto3_options.expected
new file mode 100644
index 00000000..cc2f29c0
--- /dev/null
+++ b/libs/nanopb/tests/options/proto3_options.expected
@@ -0,0 +1,4 @@
+! bool has_proto3_default
+bool has_proto3_off
+! bool has_proto3_on
+
diff --git a/libs/nanopb/tests/options/proto3_options.proto b/libs/nanopb/tests/options/proto3_options.proto
new file mode 100644
index 00000000..1017f046
--- /dev/null
+++ b/libs/nanopb/tests/options/proto3_options.proto
@@ -0,0 +1,11 @@
+syntax = "proto3";
+
+import "nanopb.proto";
+
+message Message1
+{
+ int32 proto3_default = 1;
+ int32 proto3_off = 2 [(nanopb).proto3 = false];
+ int32 proto3_on = 3 [(nanopb).proto3 = true];
+}
+
diff --git a/libs/nanopb/tests/package_name/SConscript b/libs/nanopb/tests/package_name/SConscript
new file mode 100644
index 00000000..4afc5037
--- /dev/null
+++ b/libs/nanopb/tests/package_name/SConscript
@@ -0,0 +1,38 @@
+# Check that alltypes test case works also when the .proto file defines
+# a package name.
+
+Import("env")
+
+def set_pkgname(src, dst, pkgname):
+ data = open(str(src)).read()
+ placeholder = '// package name placeholder'
+ assert placeholder in data
+ data = data.replace(placeholder, 'package %s;' % pkgname)
+ open(str(dst), 'w').write(data)
+
+# Build a modified alltypes.proto
+env.Command("alltypes.proto", "#alltypes/alltypes.proto",
+ lambda target, source, env: set_pkgname(source[0], target[0], 'test.package'))
+env.Command("alltypes.options", "#alltypes/alltypes.options", Copy("$TARGET", "$SOURCE"))
+env.NanopbProto(["alltypes", "alltypes.options"])
+
+# Build a modified encode_alltypes.c
+def modify_c(target, source, env):
+ '''Add package name to type names in .c file.'''
+
+ data = open(str(source[0]), 'r').read()
+
+ type_names = ['AllTypes', 'MyEnum', 'HugeEnum']
+ for name in type_names:
+ data = data.replace(name, 'test_package_' + name)
+
+ open(str(target[0]), 'w').write(data)
+ return 0
+env.Command("encode_alltypes.c", "#alltypes/encode_alltypes.c", modify_c)
+
+# Encode and compare results to original alltypes testcase
+enc = env.Program(["encode_alltypes.c", "alltypes.pb.c", "$COMMON/pb_encode.o", "$COMMON/pb_common.o"])
+refdec = "$BUILD/alltypes/decode_alltypes$PROGSUFFIX"
+env.RunTest(enc)
+env.Compare(["encode_alltypes.output", "$BUILD/alltypes/encode_alltypes.output"])
+
diff --git a/libs/nanopb/tests/regression/issue_118/SConscript b/libs/nanopb/tests/regression/issue_118/SConscript
new file mode 100644
index 00000000..833d9dec
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_118/SConscript
@@ -0,0 +1,12 @@
+# Regression test for Issue 118: Short enum names in imported proto files are not honoured
+
+Import("env")
+env = env.Clone()
+env.Append(PROTOCPATH = "#regression/issue_118")
+
+env.NanopbProto("enumdef")
+env.Object('enumdef.pb.c')
+
+env.NanopbProto(["enumuse", "enumdef.proto"])
+env.Object('enumuse.pb.c')
+
diff --git a/libs/nanopb/tests/regression/issue_118/enumdef.proto b/libs/nanopb/tests/regression/issue_118/enumdef.proto
new file mode 100644
index 00000000..46845bc9
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_118/enumdef.proto
@@ -0,0 +1,8 @@
+syntax = "proto2";
+
+import 'nanopb.proto';
+
+enum MyEnum {
+ option (nanopb_enumopt).long_names = false;
+ FOOBAR = 1;
+}
diff --git a/libs/nanopb/tests/regression/issue_118/enumuse.proto b/libs/nanopb/tests/regression/issue_118/enumuse.proto
new file mode 100644
index 00000000..4afc4521
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_118/enumuse.proto
@@ -0,0 +1,7 @@
+syntax = "proto2";
+
+import 'enumdef.proto';
+
+message MyMessage {
+ required MyEnum myenum = 1 [default = FOOBAR];
+}
diff --git a/libs/nanopb/tests/regression/issue_125/SConscript b/libs/nanopb/tests/regression/issue_125/SConscript
new file mode 100644
index 00000000..f2155e63
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_125/SConscript
@@ -0,0 +1,9 @@
+# Regression test for Issue 125: Wrong identifier name for extension fields
+
+Import("env")
+
+env.NanopbProto(["extensionbug", "extensionbug.options"])
+env.Object('extensionbug.pb.c')
+
+env.Match(['extensionbug.pb.h', 'extensionbug.expected'])
+
diff --git a/libs/nanopb/tests/regression/issue_125/extensionbug.expected b/libs/nanopb/tests/regression/issue_125/extensionbug.expected
new file mode 100644
index 00000000..fc213354
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_125/extensionbug.expected
@@ -0,0 +1,3 @@
+pb_extension_type_t Message2_extras
+uint32_t field2
+
diff --git a/libs/nanopb/tests/regression/issue_125/extensionbug.options b/libs/nanopb/tests/regression/issue_125/extensionbug.options
new file mode 100644
index 00000000..30b464a4
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_125/extensionbug.options
@@ -0,0 +1,4 @@
+* type:FT_IGNORE
+
+Message2.extras type:FT_STATIC
+Message2.field2 type:FT_STATIC
diff --git a/libs/nanopb/tests/regression/issue_125/extensionbug.proto b/libs/nanopb/tests/regression/issue_125/extensionbug.proto
new file mode 100644
index 00000000..fd1e74f1
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_125/extensionbug.proto
@@ -0,0 +1,18 @@
+syntax = "proto2";
+
+message Message1
+{
+ optional uint32 fieldA = 1;
+ extensions 30 to max;
+}
+
+message Message2
+{
+ extend Message1
+ {
+ optional Message2 extras = 30;
+ }
+
+ optional uint32 field1 = 1;
+ optional uint32 field2 = 2;
+}
diff --git a/libs/nanopb/tests/regression/issue_141/SConscript b/libs/nanopb/tests/regression/issue_141/SConscript
new file mode 100644
index 00000000..b6526bed
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_141/SConscript
@@ -0,0 +1,8 @@
+# Regression test for issue 141: wrong encoded size #define for oneof messages
+
+Import("env")
+
+env.NanopbProto("testproto")
+env.Object('testproto.pb.c')
+env.Match(['testproto.pb.h', 'testproto.expected'])
+
diff --git a/libs/nanopb/tests/regression/issue_141/testproto.expected b/libs/nanopb/tests/regression/issue_141/testproto.expected
new file mode 100644
index 00000000..75bc195c
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_141/testproto.expected
@@ -0,0 +1,7 @@
+define SubMessage_size \s* 88
+define OneOfMessage_size \s* 113
+define topMessage_size \s* 70
+define MyMessage1_size \s* 46
+define MyMessage2_size \s* 8
+define MyMessage3_size \s* 5
+define MyMessage4_size \s* 18
diff --git a/libs/nanopb/tests/regression/issue_141/testproto.proto b/libs/nanopb/tests/regression/issue_141/testproto.proto
new file mode 100644
index 00000000..a445c68a
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_141/testproto.proto
@@ -0,0 +1,52 @@
+syntax = "proto2";
+
+import 'nanopb.proto';
+
+message SubMessage
+{
+ repeated int32 array = 1 [(nanopb).max_count = 8];
+}
+
+message OneOfMessage
+{
+ required int32 prefix = 1;
+ oneof values
+ {
+ int32 first = 5;
+ string second = 6 [(nanopb).max_size = 8];
+ SubMessage third = 7;
+ }
+ required int32 suffix = 99;
+}
+
+message topMessage {
+ required int32 start = 1;
+ oneof msg {
+ MyMessage1 msg1 = 2;
+ MyMessage2 msg2 = 3;
+ }
+ required int32 end = 4;
+}
+
+message MyMessage1 {
+ required uint32 n1 = 1;
+ required uint32 n2 = 2;
+ required string s = 3 [(nanopb).max_size = 32];
+}
+
+message MyMessage2 {
+ required uint32 num = 1;
+ required bool b = 2;
+}
+
+message MyMessage3 {
+ required bool bbb = 1;
+ required string ss = 2 [(nanopb).max_size = 1];
+}
+
+message MyMessage4 {
+ required bool bbbb = 1;
+ required string sss = 2 [(nanopb).max_size = 2];
+ required uint32 num = 3;
+ required uint32 num2 = 4;
+}
diff --git a/libs/nanopb/tests/regression/issue_145/SConscript b/libs/nanopb/tests/regression/issue_145/SConscript
new file mode 100644
index 00000000..0b793a7a
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_145/SConscript
@@ -0,0 +1,9 @@
+# Regression test for Issue 145: Allow /* */ and // comments in .options files
+
+Import("env")
+
+env.NanopbProto(["comments", "comments.options"])
+env.Object('comments.pb.c')
+
+env.Match(['comments.pb.h', 'comments.expected'])
+
diff --git a/libs/nanopb/tests/regression/issue_145/comments.expected b/libs/nanopb/tests/regression/issue_145/comments.expected
new file mode 100644
index 00000000..7f874587
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_145/comments.expected
@@ -0,0 +1,3 @@
+char foo\[5\];
+char bar\[16\];
+
diff --git a/libs/nanopb/tests/regression/issue_145/comments.options b/libs/nanopb/tests/regression/issue_145/comments.options
new file mode 100644
index 00000000..89959ba2
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_145/comments.options
@@ -0,0 +1,6 @@
+/* Block comment */
+# Line comment
+// Line comment
+DummyMessage.foo /* Block comment */ max_size:5
+DummyMessage.bar max_size:16 # Line comment ###
+
diff --git a/libs/nanopb/tests/regression/issue_145/comments.proto b/libs/nanopb/tests/regression/issue_145/comments.proto
new file mode 100644
index 00000000..621779f5
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_145/comments.proto
@@ -0,0 +1,7 @@
+syntax = "proto2";
+
+message DummyMessage {
+ required string foo = 1;
+ required string bar = 2;
+}
+
diff --git a/libs/nanopb/tests/regression/issue_166/SConscript b/libs/nanopb/tests/regression/issue_166/SConscript
new file mode 100644
index 00000000..c50b9193
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_166/SConscript
@@ -0,0 +1,13 @@
+# Verify that the maximum encoded size is calculated properly
+# for enums.
+
+Import('env')
+
+env.NanopbProto('enums')
+
+p = env.Program(["enum_encoded_size.c",
+ "enums.pb.c",
+ "$COMMON/pb_encode.o",
+ "$COMMON/pb_common.o"])
+env.RunTest(p)
+
diff --git a/libs/nanopb/tests/regression/issue_166/enum_encoded_size.c b/libs/nanopb/tests/regression/issue_166/enum_encoded_size.c
new file mode 100644
index 00000000..84e1c7de
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_166/enum_encoded_size.c
@@ -0,0 +1,43 @@
+#include <stdio.h>
+#include <string.h>
+#include <pb_encode.h>
+#include "unittests.h"
+#include "enums.pb.h"
+
+int main()
+{
+ int status = 0;
+
+ uint8_t buf[256];
+ SignedMsg msg1;
+ UnsignedMsg msg2;
+ pb_ostream_t s;
+
+ {
+ COMMENT("Test negative value of signed enum");
+ /* Negative value should take up the maximum size */
+ msg1.value = SignedEnum_SE_MIN;
+ s = pb_ostream_from_buffer(buf, sizeof(buf));
+ TEST(pb_encode(&s, SignedMsg_fields, &msg1));
+ TEST(s.bytes_written == SignedMsg_size);
+
+ COMMENT("Test positive value of signed enum");
+ /* Positive value should be smaller */
+ msg1.value = SignedEnum_SE_MAX;
+ s = pb_ostream_from_buffer(buf, sizeof(buf));
+ TEST(pb_encode(&s, SignedMsg_fields, &msg1));
+ TEST(s.bytes_written < SignedMsg_size);
+ }
+
+ {
+ COMMENT("Test positive value of unsigned enum");
+ /* This should take up the maximum size */
+ msg2.value = UnsignedEnum_UE_MAX;
+ s = pb_ostream_from_buffer(buf, sizeof(buf));
+ TEST(pb_encode(&s, UnsignedMsg_fields, &msg2));
+ TEST(s.bytes_written == UnsignedMsg_size);
+ }
+
+ return status;
+}
+
diff --git a/libs/nanopb/tests/regression/issue_166/enums.proto b/libs/nanopb/tests/regression/issue_166/enums.proto
new file mode 100644
index 00000000..36948044
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_166/enums.proto
@@ -0,0 +1,18 @@
+syntax = "proto2";
+
+enum SignedEnum {
+ SE_MIN = -1;
+ SE_MAX = 255;
+}
+
+enum UnsignedEnum {
+ UE_MAX = 65536;
+}
+
+message SignedMsg {
+ required SignedEnum value = 1;
+}
+
+message UnsignedMsg {
+ required UnsignedEnum value = 1;
+}
diff --git a/libs/nanopb/tests/regression/issue_172/SConscript b/libs/nanopb/tests/regression/issue_172/SConscript
new file mode 100644
index 00000000..49c919e8
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_172/SConscript
@@ -0,0 +1,16 @@
+# Verify that _size define is generated for messages that have
+# includes from another directory.
+
+Import('env')
+
+incpath = env.Clone()
+incpath.Append(PROTOCPATH="#regression/issue_172/submessage")
+incpath.Append(CPPPATH="$BUILD/regression/issue_172/submessage")
+incpath.NanopbProto('test')
+incpath.NanopbProto(['submessage/submessage', 'submessage/submessage.options'])
+
+p = incpath.Program(["msg_size.c",
+ "test.pb.c",
+ "submessage/submessage.pb.c"])
+
+
diff --git a/libs/nanopb/tests/regression/issue_172/msg_size.c b/libs/nanopb/tests/regression/issue_172/msg_size.c
new file mode 100644
index 00000000..be45acb4
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_172/msg_size.c
@@ -0,0 +1,9 @@
+#include "test.pb.h"
+
+PB_STATIC_ASSERT(testmessage_size >= 1+1+1+1+16, TESTMESSAGE_SIZE_IS_WRONG)
+
+int main()
+{
+ return 0;
+}
+
diff --git a/libs/nanopb/tests/regression/issue_172/submessage/submessage.options b/libs/nanopb/tests/regression/issue_172/submessage/submessage.options
new file mode 100644
index 00000000..12fb1984
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_172/submessage/submessage.options
@@ -0,0 +1 @@
+submessage.data max_size: 16
diff --git a/libs/nanopb/tests/regression/issue_172/submessage/submessage.proto b/libs/nanopb/tests/regression/issue_172/submessage/submessage.proto
new file mode 100644
index 00000000..ce6804af
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_172/submessage/submessage.proto
@@ -0,0 +1,4 @@
+syntax = "proto2";
+message submessage {
+ required bytes data = 1;
+}
diff --git a/libs/nanopb/tests/regression/issue_172/test.proto b/libs/nanopb/tests/regression/issue_172/test.proto
new file mode 100644
index 00000000..fbd97be5
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_172/test.proto
@@ -0,0 +1,6 @@
+syntax = "proto2";
+import "submessage.proto";
+
+message testmessage {
+ optional submessage sub = 1;
+}
diff --git a/libs/nanopb/tests/regression/issue_188/SConscript b/libs/nanopb/tests/regression/issue_188/SConscript
new file mode 100644
index 00000000..6bc32712
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_188/SConscript
@@ -0,0 +1,6 @@
+# Regression test for issue with Enums inside OneOf.
+
+Import('env')
+
+env.NanopbProto('oneof')
+
diff --git a/libs/nanopb/tests/regression/issue_188/oneof.proto b/libs/nanopb/tests/regression/issue_188/oneof.proto
new file mode 100644
index 00000000..e37f5c02
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_188/oneof.proto
@@ -0,0 +1,29 @@
+syntax = "proto2";
+
+message MessageOne
+{
+ required uint32 one = 1;
+ required uint32 two = 2;
+ required uint32 three = 3;
+ required int32 four = 4;
+}
+
+enum EnumTwo
+{
+ SOME_ENUM_1 = 1;
+ SOME_ENUM_2 = 5;
+ SOME_ENUM_3 = 6;
+ SOME_ENUM_4 = 9;
+ SOME_ENUM_5 = 10;
+ SOME_ENUM_6 = 12;
+ SOME_ENUM_7 = 39;
+ SOME_ENUM_8 = 401;
+}
+
+message OneofMessage
+{
+ oneof payload {
+ MessageOne message = 1;
+ EnumTwo enum = 2;
+ }
+}
diff --git a/libs/nanopb/tests/regression/issue_195/SConscript b/libs/nanopb/tests/regression/issue_195/SConscript
new file mode 100644
index 00000000..78326d32
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_195/SConscript
@@ -0,0 +1,10 @@
+# Regression test for Issue 195: Message size not calculated if a submessage includes
+# bytes. Basically a non-working #define being generated.
+
+Import("env")
+
+env.NanopbProto(["test"])
+env.Object('test.pb.c')
+
+env.Match(['test.pb.h', 'test.expected'])
+
diff --git a/libs/nanopb/tests/regression/issue_195/test.expected b/libs/nanopb/tests/regression/issue_195/test.expected
new file mode 100644
index 00000000..83ea7ab8
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_195/test.expected
@@ -0,0 +1 @@
+/\* TestMessage_size depends
diff --git a/libs/nanopb/tests/regression/issue_195/test.proto b/libs/nanopb/tests/regression/issue_195/test.proto
new file mode 100644
index 00000000..7a77d69d
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_195/test.proto
@@ -0,0 +1,8 @@
+message TestMessage {
+ required uint32 id = 1;
+ required bytes payload = 2;
+}
+message EncapsulatedMessage {
+ required uint32 id = 1;
+ required TestMessage test = 2;
+}
diff --git a/libs/nanopb/tests/regression/issue_203/SConscript b/libs/nanopb/tests/regression/issue_203/SConscript
new file mode 100644
index 00000000..8b4d6cc7
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_203/SConscript
@@ -0,0 +1,9 @@
+# Regression test for issue with multiple files generated at once
+
+Import('env')
+
+env.Command(['file1.pb.c', 'file1.pb.h', 'file2.pb.c', 'file2.pb.h'], ['file1.proto', 'file2.proto'],
+ env['NANOPB_PROTO_CMD'])
+
+env.Object('file1.pb.c')
+env.Object('file2.pb.c')
diff --git a/libs/nanopb/tests/regression/issue_203/file1.proto b/libs/nanopb/tests/regression/issue_203/file1.proto
new file mode 100644
index 00000000..dae250b8
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_203/file1.proto
@@ -0,0 +1,10 @@
+syntax = "proto2";
+
+message SubMessage1 {
+ required int32 foo = 1;
+}
+
+message Message1 {
+ required SubMessage1 bar = 1;
+}
+
diff --git a/libs/nanopb/tests/regression/issue_203/file2.proto b/libs/nanopb/tests/regression/issue_203/file2.proto
new file mode 100644
index 00000000..513b0f0d
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_203/file2.proto
@@ -0,0 +1,10 @@
+syntax = "proto2";
+
+message SubMessage2 {
+ required int32 foo = 1;
+}
+
+message Message2 {
+ required SubMessage2 bar = 1;
+}
+
diff --git a/libs/nanopb/tests/regression/issue_205/SConscript b/libs/nanopb/tests/regression/issue_205/SConscript
new file mode 100644
index 00000000..ed8899dd
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_205/SConscript
@@ -0,0 +1,14 @@
+# Check that pb_release() correctly handles corrupted size fields of
+# static arrays.
+
+Import('env', 'malloc_env')
+
+env.NanopbProto('size_corruption')
+
+p = malloc_env.Program(["size_corruption.c",
+ "size_corruption.pb.c",
+ "$COMMON/pb_decode_with_malloc.o",
+ "$COMMON/pb_common_with_malloc.o",
+ "$COMMON/malloc_wrappers.o"])
+env.RunTest(p)
+
diff --git a/libs/nanopb/tests/regression/issue_205/size_corruption.c b/libs/nanopb/tests/regression/issue_205/size_corruption.c
new file mode 100644
index 00000000..08cef457
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_205/size_corruption.c
@@ -0,0 +1,12 @@
+#include "size_corruption.pb.h"
+#include <pb_decode.h>
+
+int main()
+{
+ MainMessage msg = MainMessage_init_zero;
+ msg.bar_count = (pb_size_t)-1;
+ pb_release(MainMessage_fields, &msg);
+
+ return 0;
+}
+
diff --git a/libs/nanopb/tests/regression/issue_205/size_corruption.proto b/libs/nanopb/tests/regression/issue_205/size_corruption.proto
new file mode 100644
index 00000000..6c9c2453
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_205/size_corruption.proto
@@ -0,0 +1,11 @@
+syntax = "proto2";
+import 'nanopb.proto';
+
+message SubMessage {
+ repeated int32 foo = 1 [(nanopb).type = FT_POINTER];
+}
+
+message MainMessage {
+ repeated SubMessage bar = 1 [(nanopb).max_count = 5];
+}
+
diff --git a/libs/nanopb/tests/regression/issue_227/SConscript b/libs/nanopb/tests/regression/issue_227/SConscript
new file mode 100644
index 00000000..10741240
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_227/SConscript
@@ -0,0 +1,14 @@
+# Regression test for Issue 227:Using proto3 type fields can cause unaligned access
+# NOTE: This test will only detect problems when run with clang sanitizer (which
+# is done regularly by a jenkins run).
+
+Import('env')
+
+env.NanopbProto('unaligned_uint64')
+
+p = env.Program(["unaligned_uint64.c",
+ "unaligned_uint64.pb.c",
+ "$COMMON/pb_encode.o",
+ "$COMMON/pb_common.o"])
+env.RunTest(p)
+
diff --git a/libs/nanopb/tests/regression/issue_227/unaligned_uint64.c b/libs/nanopb/tests/regression/issue_227/unaligned_uint64.c
new file mode 100644
index 00000000..17c1d779
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_227/unaligned_uint64.c
@@ -0,0 +1,14 @@
+#include "unaligned_uint64.pb.h"
+#include <pb_encode.h>
+
+int main()
+{
+ uint8_t buf[128];
+ pb_ostream_t stream = pb_ostream_from_buffer(buf, sizeof(buf));
+ MainMessage msg = MainMessage_init_zero;
+ msg.bar[0] = 'A';
+ pb_encode(&stream, MainMessage_fields, &msg);
+
+ return 0;
+}
+
diff --git a/libs/nanopb/tests/regression/issue_227/unaligned_uint64.proto b/libs/nanopb/tests/regression/issue_227/unaligned_uint64.proto
new file mode 100644
index 00000000..f0269f60
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_227/unaligned_uint64.proto
@@ -0,0 +1,8 @@
+syntax = "proto3";
+import 'nanopb.proto';
+
+message MainMessage {
+ string foo = 1 [(nanopb).max_size = 3];
+ string bar = 2 [(nanopb).max_size = 8];
+}
+
diff --git a/libs/nanopb/tests/regression/issue_229/SConscript b/libs/nanopb/tests/regression/issue_229/SConscript
new file mode 100644
index 00000000..b0f8376d
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_229/SConscript
@@ -0,0 +1,13 @@
+# Regression test for Issue 229: problem encoding message that has
+# multiple oneof fields
+Import('env')
+
+env.NanopbProto('multiple_oneof')
+
+p = env.Program(["multiple_oneof.c",
+ "multiple_oneof.pb.c",
+ "$COMMON/pb_decode.o",
+ "$COMMON/pb_encode.o",
+ "$COMMON/pb_common.o"])
+env.RunTest(p)
+
diff --git a/libs/nanopb/tests/regression/issue_229/multiple_oneof.c b/libs/nanopb/tests/regression/issue_229/multiple_oneof.c
new file mode 100644
index 00000000..902248d0
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_229/multiple_oneof.c
@@ -0,0 +1,35 @@
+#include "multiple_oneof.pb.h"
+#include <unittests.h>
+#include <pb_encode.h>
+#include <pb_decode.h>
+
+int main()
+{
+ int status = 0;
+ uint8_t buf[128];
+ size_t msglen;
+
+ {
+ pb_ostream_t stream = pb_ostream_from_buffer(buf, sizeof(buf));
+ MainMessage msg = MainMessage_init_zero;
+ msg.which_oneof1 = MainMessage_oneof1_uint32_tag;
+ msg.oneof1.oneof1_uint32 = 1234;
+ msg.which_oneof2 = MainMessage_oneof2_uint32_tag;
+ msg.oneof2.oneof2_uint32 = 5678;
+ TEST(pb_encode(&stream, MainMessage_fields, &msg));
+ msglen = stream.bytes_written;
+ }
+
+ {
+ pb_istream_t stream = pb_istream_from_buffer(buf, msglen);
+ MainMessage msg = MainMessage_init_zero;
+ TEST(pb_decode(&stream, MainMessage_fields, &msg));
+ TEST(msg.which_oneof1 == MainMessage_oneof1_uint32_tag);
+ TEST(msg.oneof1.oneof1_uint32 == 1234);
+ TEST(msg.which_oneof2 == MainMessage_oneof2_uint32_tag);
+ TEST(msg.oneof2.oneof2_uint32 == 5678);
+ }
+
+ return status;
+}
+
diff --git a/libs/nanopb/tests/regression/issue_229/multiple_oneof.proto b/libs/nanopb/tests/regression/issue_229/multiple_oneof.proto
new file mode 100644
index 00000000..22373e1d
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_229/multiple_oneof.proto
@@ -0,0 +1,11 @@
+syntax = "proto2";
+
+message MainMessage {
+ oneof oneof1 {
+ uint32 oneof1_uint32 = 1;
+ }
+ oneof oneof2 {
+ uint32 oneof2_uint32 = 2;
+ }
+}
+
diff --git a/libs/nanopb/tests/regression/issue_242/SConscript b/libs/nanopb/tests/regression/issue_242/SConscript
new file mode 100644
index 00000000..000063ef
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_242/SConscript
@@ -0,0 +1,13 @@
+# Regression test for Issue 242: pb_encode does not encode tag for
+# extension fields that is all zeros
+Import('env')
+
+env.NanopbProto('zero_value')
+
+p = env.Program(["zero_value.c",
+ "zero_value.pb.c",
+ "$COMMON/pb_decode.o",
+ "$COMMON/pb_encode.o",
+ "$COMMON/pb_common.o"])
+env.RunTest(p)
+
diff --git a/libs/nanopb/tests/regression/issue_242/zero_value.c b/libs/nanopb/tests/regression/issue_242/zero_value.c
new file mode 100644
index 00000000..b3d96b7a
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_242/zero_value.c
@@ -0,0 +1,51 @@
+#include <unittests.h>
+#include <pb_encode.h>
+#include <pb_decode.h>
+#include <string.h>
+#include "zero_value.pb.h"
+
+int main()
+{
+ int status = 0;
+
+ COMMENT("Test extension fields with zero values");
+ {
+ uint8_t buffer[256] = {0};
+ pb_ostream_t ostream;
+ int32_t value = 0;
+ Extendable source = {0};
+
+ pb_extension_t source_ext = {0};
+ source_ext.type = &opt_int32;
+ source_ext.dest = &value;
+ source.extensions = &source_ext;
+
+ ostream = pb_ostream_from_buffer(buffer, sizeof(buffer));
+ TEST(pb_encode(&ostream, Extendable_fields, &source));
+
+ TEST(ostream.bytes_written == 2);
+ TEST(memcmp(buffer, "\x58\x00", 2) == 0);
+ }
+
+ /* Note: There never was a bug here, but this check is included
+ * in the regression test because the logic is closely related.
+ */
+ COMMENT("Test pointer fields with zero values");
+ {
+ uint8_t buffer[256] = {0};
+ pb_ostream_t ostream;
+ int32_t value = 0;
+ PointerMessage source = {0};
+
+ source.opt_int32 = &value;
+
+ ostream = pb_ostream_from_buffer(buffer, sizeof(buffer));
+ TEST(pb_encode(&ostream, PointerMessage_fields, &source));
+
+ TEST(ostream.bytes_written == 2);
+ TEST(memcmp(buffer, "\x58\x00", 2) == 0);
+ }
+
+ return status;
+}
+
diff --git a/libs/nanopb/tests/regression/issue_242/zero_value.proto b/libs/nanopb/tests/regression/issue_242/zero_value.proto
new file mode 100644
index 00000000..020a39a5
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_242/zero_value.proto
@@ -0,0 +1,15 @@
+syntax = "proto2";
+import "nanopb.proto";
+
+message Extendable {
+ extensions 10 to 100;
+}
+
+extend Extendable {
+ optional int32 opt_int32 = 11;
+}
+
+message PointerMessage {
+ optional int32 opt_int32 = 11 [(nanopb).type = FT_POINTER];
+}
+
diff --git a/libs/nanopb/tests/regression/issue_247/SConscript b/libs/nanopb/tests/regression/issue_247/SConscript
new file mode 100644
index 00000000..b41e9f29
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_247/SConscript
@@ -0,0 +1,14 @@
+# Test that pb_check_proto3_default_value() correctly skips padding
+# bytes in submessage structures.
+
+Import("env")
+
+env.NanopbProto("padding")
+
+p = env.Program(["padding.c",
+ "padding.pb.c",
+ "$COMMON/pb_encode.o",
+ "$COMMON/pb_common.o"])
+
+env.RunTest(p)
+
diff --git a/libs/nanopb/tests/regression/issue_247/padding.c b/libs/nanopb/tests/regression/issue_247/padding.c
new file mode 100644
index 00000000..8860179d
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_247/padding.c
@@ -0,0 +1,32 @@
+#include <pb_encode.h>
+#include <unittests.h>
+#include <string.h>
+#include "padding.pb.h"
+
+int main()
+{
+ int status = 0;
+
+ TestMessage msg;
+
+ /* Set padding bytes to garbage */
+ memset(&msg, 0xAA, sizeof(msg));
+
+ /* Set all meaningful fields to 0 */
+ msg.submsg.boolfield = false;
+ msg.submsg.intfield = 0;
+
+ /* Test encoding */
+ {
+ pb_byte_t buf[128] = {0};
+ pb_ostream_t stream = pb_ostream_from_buffer(buf, sizeof(buf));
+ TEST(pb_encode(&stream, TestMessage_fields, &msg));
+
+ /* Because all fields have zero values, proto3 encoder
+ * shouldn't write out anything. */
+ TEST(stream.bytes_written == 0);
+ }
+
+ return status;
+}
+
diff --git a/libs/nanopb/tests/regression/issue_247/padding.proto b/libs/nanopb/tests/regression/issue_247/padding.proto
new file mode 100644
index 00000000..20bddac3
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_247/padding.proto
@@ -0,0 +1,12 @@
+syntax = "proto3";
+import "nanopb.proto";
+
+message SubMessage {
+ bool boolfield = 1;
+ int64 intfield = 2;
+}
+
+message TestMessage {
+ SubMessage submsg = 1;
+}
+
diff --git a/libs/nanopb/tests/regression/issue_249/SConscript b/libs/nanopb/tests/regression/issue_249/SConscript
new file mode 100644
index 00000000..ba667129
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_249/SConscript
@@ -0,0 +1,12 @@
+# Regression test for Issue 249: proto3 mode pb_decode() corrupts callback fields
+Import('env')
+
+env.NanopbProto('test')
+
+p = env.Program(["test.c",
+ "test.pb.c",
+ "$COMMON/pb_decode.o",
+ "$COMMON/pb_encode.o",
+ "$COMMON/pb_common.o"])
+env.RunTest(p)
+
diff --git a/libs/nanopb/tests/regression/issue_249/test.c b/libs/nanopb/tests/regression/issue_249/test.c
new file mode 100644
index 00000000..a37180fd
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_249/test.c
@@ -0,0 +1,59 @@
+#include "test.pb.h"
+#include <unittests.h>
+#include <pb_encode.h>
+#include <pb_decode.h>
+
+static bool write_array(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ int i;
+ for (i = 0; i < 5; i++)
+ {
+ if (!pb_encode_tag_for_field(stream, field))
+ return false;
+ if (!pb_encode_varint(stream, 1000 + i))
+ return false;
+ }
+
+ return true;
+}
+
+static bool read_array(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ uint32_t i;
+ int *sum = *arg;
+
+ if (!pb_decode_varint32(stream, &i))
+ return false;
+
+ *sum += i;
+
+ return true;
+}
+
+int main()
+{
+ int status = 0;
+ pb_byte_t buf[128] = {0};
+ pb_size_t msglen;
+
+ {
+ MainMessage msg = MainMessage_init_zero;
+ pb_ostream_t stream = pb_ostream_from_buffer(buf, sizeof(buf));
+ msg.submsg.foo.funcs.encode = &write_array;
+ TEST(pb_encode(&stream, MainMessage_fields, &msg));
+ msglen = stream.bytes_written;
+ }
+
+ {
+ MainMessage msg = MainMessage_init_zero;
+ pb_istream_t stream = pb_istream_from_buffer(buf, msglen);
+ int sum = 0;
+ msg.submsg.foo.funcs.decode = &read_array;
+ msg.submsg.foo.arg = &sum;
+ TEST(pb_decode(&stream, MainMessage_fields, &msg));
+ TEST(sum == 1000 + 1001 + 1002 + 1003 + 1004);
+ }
+
+ return status;
+}
+
diff --git a/libs/nanopb/tests/regression/issue_249/test.proto b/libs/nanopb/tests/regression/issue_249/test.proto
new file mode 100644
index 00000000..eaa2abde
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_249/test.proto
@@ -0,0 +1,10 @@
+syntax = "proto3";
+
+message SubMessage {
+ repeated int32 foo = 1;
+}
+
+message MainMessage {
+ SubMessage submsg = 1;
+}
+
diff --git a/libs/nanopb/tests/regression/issue_253/SConscript b/libs/nanopb/tests/regression/issue_253/SConscript
new file mode 100644
index 00000000..5a16948c
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_253/SConscript
@@ -0,0 +1,15 @@
+# Regression test for Issue 253: Wrong calculated message maximum size
+
+Import('env')
+
+env.NanopbProto('short_array')
+
+p = env.Program(['short_array.c',
+ 'short_array.pb.c',
+ "$COMMON/pb_decode.o",
+ "$COMMON/pb_encode.o",
+ "$COMMON/pb_common.o"])
+
+env.RunTest(p)
+
+
diff --git a/libs/nanopb/tests/regression/issue_253/short_array.c b/libs/nanopb/tests/regression/issue_253/short_array.c
new file mode 100644
index 00000000..5ed6c3f7
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_253/short_array.c
@@ -0,0 +1,24 @@
+#include <unittests.h>
+#include <pb_encode.h>
+#include "short_array.pb.h"
+
+int main()
+{
+ int status = 0;
+
+ COMMENT("Test message length calculation for short arrays");
+ {
+ uint8_t buffer[TestMessage_size] = {0};
+ pb_ostream_t ostream = pb_ostream_from_buffer(buffer, TestMessage_size);
+ TestMessage msg = TestMessage_init_zero;
+
+ msg.rep_uint32_count = 1;
+ msg.rep_uint32[0] = (1 << 31);
+
+ TEST(pb_encode(&ostream, TestMessage_fields, &msg));
+ TEST(ostream.bytes_written == TestMessage_size);
+ }
+
+ return status;
+}
+
diff --git a/libs/nanopb/tests/regression/issue_253/short_array.proto b/libs/nanopb/tests/regression/issue_253/short_array.proto
new file mode 100644
index 00000000..5a5d8a3d
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_253/short_array.proto
@@ -0,0 +1,7 @@
+syntax = "proto2";
+import "nanopb.proto";
+
+message TestMessage {
+ repeated uint32 rep_uint32 = 1 [(nanopb).max_count = 1];
+}
+
diff --git a/libs/nanopb/tests/regression/issue_256/SConscript b/libs/nanopb/tests/regression/issue_256/SConscript
new file mode 100644
index 00000000..b2c3e864
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_256/SConscript
@@ -0,0 +1,16 @@
+# Regression test for Issue 256: Proto3 mode skips submessages even when
+# later array fields have non-zero value
+
+Import('env')
+
+env.NanopbProto('submsg_array')
+
+p = env.Program(['submsg_array.c',
+ 'submsg_array.pb.c',
+ "$COMMON/pb_decode.o",
+ "$COMMON/pb_encode.o",
+ "$COMMON/pb_common.o"])
+
+env.RunTest(p)
+
+
diff --git a/libs/nanopb/tests/regression/issue_256/submsg_array.c b/libs/nanopb/tests/regression/issue_256/submsg_array.c
new file mode 100644
index 00000000..c63bd30a
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_256/submsg_array.c
@@ -0,0 +1,38 @@
+#include <unittests.h>
+#include <pb_encode.h>
+#include <pb_decode.h>
+#include "submsg_array.pb.h"
+
+int main()
+{
+ int status = 0;
+
+ COMMENT("Test encoding for submessage with array");
+ {
+ uint8_t buffer[TestMessage_size] = {0};
+ pb_ostream_t ostream = pb_ostream_from_buffer(buffer, TestMessage_size);
+ TestMessage msg = TestMessage_init_zero;
+
+ msg.submsg.rep_uint32_count = 3;
+ msg.submsg.rep_uint32[0] = 0;
+ msg.submsg.rep_uint32[1] = 1;
+ msg.submsg.rep_uint32[2] = 2;
+
+ TEST(pb_encode(&ostream, TestMessage_fields, &msg));
+ TEST(ostream.bytes_written > 0);
+
+ {
+ pb_istream_t istream = pb_istream_from_buffer(buffer, ostream.bytes_written);
+ TestMessage msg2 = TestMessage_init_zero;
+
+ TEST(pb_decode(&istream, TestMessage_fields, &msg2));
+ TEST(msg2.submsg.rep_uint32_count == 3);
+ TEST(msg2.submsg.rep_uint32[0] == 0);
+ TEST(msg2.submsg.rep_uint32[1] == 1);
+ TEST(msg2.submsg.rep_uint32[2] == 2);
+ }
+ }
+
+ return status;
+}
+
diff --git a/libs/nanopb/tests/regression/issue_256/submsg_array.proto b/libs/nanopb/tests/regression/issue_256/submsg_array.proto
new file mode 100644
index 00000000..4964a05f
--- /dev/null
+++ b/libs/nanopb/tests/regression/issue_256/submsg_array.proto
@@ -0,0 +1,11 @@
+syntax = "proto3";
+import "nanopb.proto";
+
+message SubMessage {
+ repeated uint32 rep_uint32 = 1 [(nanopb).max_count = 3];
+}
+
+message TestMessage {
+ SubMessage submsg = 1;
+}
+
diff --git a/libs/nanopb/tests/site_scons/site_init.py b/libs/nanopb/tests/site_scons/site_init.py
new file mode 100644
index 00000000..da5f6d65
--- /dev/null
+++ b/libs/nanopb/tests/site_scons/site_init.py
@@ -0,0 +1,109 @@
+import subprocess
+import sys
+import re
+
+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.'''
+
+ # Build command that runs a test program and saves the output
+ def run_test(target, source, env):
+ if len(source) > 1:
+ infile = open(str(source[1]))
+ else:
+ infile = None
+
+ if env.has_key("COMMAND"):
+ args = [env["COMMAND"]]
+ else:
+ args = [str(source[0])]
+
+ if env.has_key('ARGS'):
+ args.extend(env['ARGS'])
+
+ print 'Command line: ' + str(args)
+ pipe = subprocess.Popen(args,
+ stdin = infile,
+ stdout = open(str(target[0]), 'w'),
+ stderr = sys.stderr)
+ result = pipe.wait()
+ if result == 0:
+ print '\033[32m[ OK ]\033[0m Ran ' + args[0]
+ else:
+ print '\033[31m[FAIL]\033[0m Program ' + args[0] + ' returned ' + str(result)
+ return result
+
+ run_test_builder = Builder(action = run_test,
+ suffix = '.output')
+ env.Append(BUILDERS = {'RunTest': run_test_builder})
+
+ # Build command that decodes a message using protoc
+ def decode_actions(source, target, env, for_signature):
+ esc = env['ESCAPE']
+ dirs = ' '.join(['-I' + esc(env.GetBuildPath(d)) for d in env['PROTOCPATH']])
+ return '$PROTOC $PROTOCFLAGS %s --decode=%s %s <%s >%s' % (
+ dirs, env['MESSAGE'], esc(str(source[1])), esc(str(source[0])), esc(str(target[0])))
+
+ decode_builder = Builder(generator = decode_actions,
+ suffix = '.decoded')
+ env.Append(BUILDERS = {'Decode': decode_builder})
+
+ # Build command that encodes a message using protoc
+ def encode_actions(source, target, env, for_signature):
+ esc = env['ESCAPE']
+ dirs = ' '.join(['-I' + esc(env.GetBuildPath(d)) for d in env['PROTOCPATH']])
+ return '$PROTOC $PROTOCFLAGS %s --encode=%s %s <%s >%s' % (
+ dirs, env['MESSAGE'], esc(str(source[1])), esc(str(source[0])), esc(str(target[0])))
+
+ encode_builder = Builder(generator = encode_actions,
+ suffix = '.encoded')
+ env.Append(BUILDERS = {'Encode': encode_builder})
+
+ # Build command that asserts that two files be equal
+ def compare_files(target, source, env):
+ data1 = open(str(source[0]), 'rb').read()
+ data2 = open(str(source[1]), 'rb').read()
+ if data1 == data2:
+ print '\033[32m[ OK ]\033[0m Files equal: ' + str(source[0]) + ' and ' + str(source[1])
+ return 0
+ else:
+ print '\033[31m[FAIL]\033[0m Files differ: ' + str(source[0]) + ' and ' + str(source[1])
+ return 1
+
+ compare_builder = Builder(action = compare_files,
+ 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():
+ invert = False
+ if pattern.startswith('! '):
+ invert = True
+ pattern = pattern[2:]
+
+ status = re.search(pattern.strip(), data, re.MULTILINE)
+
+ if not status and not invert:
+ print '\033[31m[FAIL]\033[0m Pattern not found in ' + str(source[0]) + ': ' + pattern
+ return 1
+ elif status and invert:
+ print '\033[31m[FAIL]\033[0m Pattern should not exist, but does 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/libs/nanopb/tests/site_scons/site_tools/nanopb.py b/libs/nanopb/tests/site_scons/site_tools/nanopb.py
new file mode 100644
index 00000000..c72a45d3
--- /dev/null
+++ b/libs/nanopb/tests/site_scons/site_tools/nanopb.py
@@ -0,0 +1,126 @@
+'''
+Scons Builder for nanopb .proto definitions.
+
+This tool will locate the nanopb generator and use it to generate .pb.c and
+.pb.h files from the .proto files.
+
+Basic example
+-------------
+# Build myproto.pb.c and myproto.pb.h from myproto.proto
+myproto = env.NanopbProto("myproto")
+
+# Link nanopb core to the program
+env.Append(CPPPATH = "$NANOB")
+myprog = env.Program(["myprog.c", myproto, "$NANOPB/pb_encode.c", "$NANOPB/pb_decode.c"])
+
+Configuration options
+---------------------
+Normally, this script is used in the test environment of nanopb and it locates
+the nanopb generator by a relative path. If this script is used in another
+application, the path to nanopb root directory has to be defined:
+
+env.SetDefault(NANOPB = "path/to/nanopb")
+
+Additionally, the path to protoc and the options to give to protoc can be
+defined manually:
+
+env.SetDefault(PROTOC = "path/to/protoc")
+env.SetDefault(PROTOCFLAGS = "--plugin=protoc-gen-nanopb=path/to/protoc-gen-nanopb")
+'''
+
+import SCons.Action
+import SCons.Builder
+import SCons.Util
+import os.path
+
+class NanopbWarning(SCons.Warnings.Warning):
+ pass
+SCons.Warnings.enableWarningClass(NanopbWarning)
+
+def _detect_nanopb(env):
+ '''Find the path to nanopb root directory.'''
+ if env.has_key('NANOPB'):
+ # Use nanopb dir given by user
+ return env['NANOPB']
+
+ p = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..'))
+ if os.path.isdir(p) and os.path.isfile(os.path.join(p, 'pb.h')):
+ # Assume we are running under tests/site_scons/site_tools
+ return p
+
+ raise SCons.Errors.StopError(NanopbWarning,
+ "Could not find the nanopb root directory")
+
+def _detect_protoc(env):
+ '''Find the path to the protoc compiler.'''
+ if env.has_key('PROTOC'):
+ # Use protoc defined by user
+ return env['PROTOC']
+
+ n = _detect_nanopb(env)
+ p1 = os.path.join(n, 'generator-bin', 'protoc' + env['PROGSUFFIX'])
+ if os.path.exists(p1):
+ # Use protoc bundled with binary package
+ return env['ESCAPE'](p1)
+
+ p = env.WhereIs('protoc')
+ if p:
+ # Use protoc from path
+ return env['ESCAPE'](p)
+
+ raise SCons.Errors.StopError(NanopbWarning,
+ "Could not find the protoc compiler")
+
+def _detect_protocflags(env):
+ '''Find the options to use for protoc.'''
+ if env.has_key('PROTOCFLAGS'):
+ return env['PROTOCFLAGS']
+
+ p = _detect_protoc(env)
+ n = _detect_nanopb(env)
+ p1 = os.path.join(n, 'generator-bin', 'protoc' + env['PROGSUFFIX'])
+ if p == env['ESCAPE'](p1):
+ # Using the bundled protoc, no options needed
+ return ''
+
+ e = env['ESCAPE']
+ if env['PLATFORM'] == 'win32':
+ return e('--plugin=protoc-gen-nanopb=' + os.path.join(n, 'generator', 'protoc-gen-nanopb.bat'))
+ else:
+ return e('--plugin=protoc-gen-nanopb=' + os.path.join(n, 'generator', 'protoc-gen-nanopb'))
+
+def _nanopb_proto_actions(source, target, env, for_signature):
+ esc = env['ESCAPE']
+ dirs = ' '.join(['-I' + esc(env.GetBuildPath(d)) for d in env['PROTOCPATH']])
+ return '$PROTOC $PROTOCFLAGS %s --nanopb_out=. %s' % (dirs, esc(str(source[0])))
+
+def _nanopb_proto_emitter(target, source, env):
+ basename = os.path.splitext(str(source[0]))[0]
+ target.append(basename + '.pb.h')
+
+ if os.path.exists(basename + '.options'):
+ source.append(basename + '.options')
+
+ return target, source
+
+_nanopb_proto_builder = SCons.Builder.Builder(
+ generator = _nanopb_proto_actions,
+ suffix = '.pb.c',
+ src_suffix = '.proto',
+ emitter = _nanopb_proto_emitter)
+
+def generate(env):
+ '''Add Builder for nanopb protos.'''
+
+ env['NANOPB'] = _detect_nanopb(env)
+ env['PROTOC'] = _detect_protoc(env)
+ env['PROTOCFLAGS'] = _detect_protocflags(env)
+
+ env.SetDefault(PROTOCPATH = ['.', os.path.join(env['NANOPB'], 'generator', 'proto')])
+
+ env.SetDefault(NANOPB_PROTO_CMD = '$PROTOC $PROTOCFLAGS --nanopb_out=. $SOURCES')
+ env['BUILDERS']['NanopbProto'] = _nanopb_proto_builder
+
+def exists(env):
+ return _detect_protoc(env) and _detect_protoc_opts(env)
+
diff --git a/libs/nanopb/tests/special_characters/SConscript b/libs/nanopb/tests/special_characters/SConscript
new file mode 100644
index 00000000..2309cf2e
--- /dev/null
+++ b/libs/nanopb/tests/special_characters/SConscript
@@ -0,0 +1,6 @@
+# Test that special characters in .proto filenames work.
+
+Import('env')
+
+env.NanopbProto("funny-proto+name has.characters.proto")
+env.Object("funny-proto+name has.characters.pb.c")
diff --git a/libs/nanopb/tests/special_characters/funny-proto+name has.characters.proto b/libs/nanopb/tests/special_characters/funny-proto+name has.characters.proto
new file mode 100644
index 00000000..26b2cb1b
--- /dev/null
+++ b/libs/nanopb/tests/special_characters/funny-proto+name has.characters.proto
@@ -0,0 +1 @@
+syntax="proto2";
diff --git a/libs/nanopb/tests/splint/SConscript b/libs/nanopb/tests/splint/SConscript
new file mode 100644
index 00000000..cd4b5b9d
--- /dev/null
+++ b/libs/nanopb/tests/splint/SConscript
@@ -0,0 +1,16 @@
+# Check the nanopb core using splint
+
+Import('env')
+
+p = env.WhereIs('splint')
+
+if p:
+ env.Command('pb_decode.splint', '$NANOPB/pb_decode.c',
+ 'splint -f splint/splint.rc $SOURCE 2> $TARGET')
+
+ env.Command('pb_encode.splint', '$NANOPB/pb_encode.c',
+ 'splint -f splint/splint.rc $SOURCE 2> $TARGET')
+
+ env.Command('pb_common.splint', '$NANOPB/pb_common.c',
+ 'splint -f splint/splint.rc $SOURCE 2> $TARGET')
+
diff --git a/libs/nanopb/tests/splint/splint.rc b/libs/nanopb/tests/splint/splint.rc
new file mode 100644
index 00000000..e47d3c21
--- /dev/null
+++ b/libs/nanopb/tests/splint/splint.rc
@@ -0,0 +1,37 @@
++checks
++partial
++matchanyintegral
++strictlib
+-nullassign
+-predboolint
+-predboolptr
++ptrnegate
+-switchloopbreak
++ignoresigns
+-infloopsuncon
+-type
+
+# splint's memory checks don't quite work without annotations
+-mustfreeonly
+-compmempass
+-nullret
+-observertrans
+-statictrans
+-compdestroy
+-nullpass
+-nullstate
+-compdef
+-usereleased
+-temptrans
+-dependenttrans
+-kepttrans
+-branchstate
+-immediatetrans
+-mustfreefresh
+
+# These tests give false positives, compiler typically has
+# better warnings for these.
+-noret
+-noeffect
+-usedef
+