blob: 3280f7550b004fbb3396b237a6ff7b4d07e8217a [file] [log] [blame]
# Run a fuzz test to verify robustness against corrupted/malicious data.
import time
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"])
# Do the same for proto3 versions
env.Command("alltypes_proto3_static.proto", "#alltypes_proto3/alltypes.proto",
lambda target, source, env: set_pkgname(source[0], target[0], 'alltypes_proto3_static'))
env.Command("alltypes_proto3_pointer.proto", "#alltypes_proto3/alltypes.proto",
lambda target, source, env: set_pkgname(source[0], target[0], 'alltypes_proto3_pointer'))
p1 = env.NanopbProto(["alltypes_proto3_pointer", "alltypes_proto3_pointer.options"])
p2 = env.NanopbProto(["alltypes_proto3_static", "alltypes_proto3_static.options"])
fuzz = malloc_env.Program(["fuzztest.c",
"random_data.c",
"validation.c",
"flakystream.c",
"alltypes_pointer.pb.c",
"alltypes_static.pb.c",
"alltypes_proto3_pointer.pb.c",
"alltypes_proto3_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"])
seed = int(time.time())
if env.get('EMBEDDED'):
iterations = 100
else:
iterations = 10000
env.RunTest(fuzz, ARGS = [str(seed), str(iterations)])
generate_message = malloc_env.Program(["generate_message.c",
"random_data.c",
"alltypes_static.pb.c",
"$COMMON/pb_encode.o",
"$COMMON/pb_common.o",
"$COMMON/malloc_wrappers.o"])