asn1c/tests/tests-randomized/check-bundles.sh

308 lines
9.1 KiB
Bash
Raw Normal View History

2017-10-01 02:33:23 +00:00
#!/bin/sh
#
# Create an ASN.1 source code project for each line in each of the
# bundles/*.txt files, compile and run that it can be encoded, decoded,
# and fuzzed (if fuzzing is available).
#
set -e
usage() {
echo "Usage:"
echo " $0 -h"
2017-10-03 23:39:38 +00:00
echo " $0 [--dirty] -t \"<ASN.1 text defining type T, in string form>\""
echo " $0 [--dirty] bundles/<bundle-name.txt> [<line>]"
2017-10-03 22:05:54 +00:00
echo "Where options are:"
echo " -h Show this help screen"
echo " -e <syntax> Verify a given encoding explicitly (default is ALL)"
echo " --asn1c <flag> Add this flag to asn1c"
echo " --dirty Reuse compile results from the previous run(s)"
echo " -t <ASN.1> Run this particular typel"
2017-10-01 02:33:23 +00:00
echo "Examples:"
echo " $0 -t UTF8String"
echo " $0 -t \"T ::= INTEGER (0..1)\""
echo " $0 bundles/01-INTEGER-bundle.txt 3"
exit 1
}
2017-10-01 20:43:36 +00:00
RNDTEMP=.tmp.random
srcdir="${srcdir:-.}"
2017-10-06 06:56:39 +00:00
abs_top_srcdir="${abs_top_srcdir:-`pwd`/../../}"
abs_top_builddir="${abs_top_builddir:-`pwd`/../../}"
2017-10-01 20:43:36 +00:00
2017-10-01 02:33:23 +00:00
tests_succeeded=0
tests_failed=0
2017-10-01 20:43:36 +00:00
stop_after_failed=1 # We stop after 3 failures.
2017-10-03 22:05:54 +00:00
need_clean_before_bundle=1 # Clean before testing a bundle file
need_clean_before_test=0 # Before each line in a bundle file
2017-10-04 01:40:36 +00:00
encodings="" # Default is to verify all supported ASN.1 transfer syntaxes
parallelism=4
asn1c_flags=""
2017-10-03 22:05:54 +00:00
make_clean_before_bundle() {
if [ "${need_clean_before_bundle}" = "1" ] ; then
(cd ${RNDTEMP} && make clean) || :
fi
2017-10-03 22:05:54 +00:00
}
make_clean_before_test() {
2017-10-08 10:16:10 +00:00
if [ "${need_clean_before_test}" = "1" ] ; then
Make clean
fi
2017-10-03 22:05:54 +00:00
}
2017-10-01 02:33:23 +00:00
# Get all the type-bearding lines in file and process them individually
verify_asn_types_in_file() {
2017-10-06 06:56:39 +00:00
filename="$1"
need_line="$2"
2017-10-01 02:33:23 +00:00
test "x$filename" != "x" || usage
2017-10-03 22:05:54 +00:00
make_clean_before_bundle
2017-10-01 02:33:23 +00:00
echo "Open [$filename]"
2017-10-06 06:56:39 +00:00
line=0
asn=""
while read asn; do
line=`echo "$line+1" | bc`
if echo "$asn" | sed -e 's/--.*//;' | grep -vi "[A-Z]" > /dev/null; then
2017-10-01 02:33:23 +00:00
# Ignore lines consisting of just comments.
continue;
fi
2017-10-06 05:09:19 +00:00
if [ "x$need_line" != "x" ] && [ "$need_line" != "$line" ]; then
2017-10-01 02:33:23 +00:00
# We need a different line.
continue;
fi
verify_asn_type "$asn" "in $filename $line"
2017-10-01 20:43:36 +00:00
if [ "${tests_failed}" = "${stop_after_failed}" ]; then
echo "STOP after ${tests_failed} failures, OK ${tests_succeeded}"
exit 1
fi
2017-10-01 02:33:23 +00:00
done < "$filename"
}
verify_asn_type() {
2017-10-06 06:56:39 +00:00
asn="$1"
where="$2"
shift 2
2017-10-01 02:33:23 +00:00
test "x$asn" != "x" || usage
2017-10-03 22:05:54 +00:00
2017-10-06 06:56:39 +00:00
if echo "$asn" | grep -v "::=" > /dev/null; then
2017-10-01 02:33:23 +00:00
asn="T ::= $asn"
fi
echo "Testing [$asn] ${where}"
2017-10-01 20:43:36 +00:00
mkdir -p ${RNDTEMP}
if (set -e && cd ${RNDTEMP} && compile_and_test "$asn" "${where}"); then
2017-10-01 02:33:23 +00:00
echo "OK [$asn] ${where}"
2017-10-06 06:56:39 +00:00
tests_succeeded=`echo "$tests_succeeded + 1" | bc`
2017-10-01 02:33:23 +00:00
else
2017-10-06 06:56:39 +00:00
tests_failed=`echo "$tests_failed + 1" | bc`
2017-10-01 02:33:23 +00:00
echo "FAIL [$asn] ${where}"
fi
}
2017-10-06 06:56:39 +00:00
Make() {
${MAKE:-make} -j "${parallelism}" "$@" || return $?
}
get_param() {
param="$1"
default="$2"
text="$3"
echo "$asn" | awk "/$param=/{for(i=1;i<=NF;i++)if(substr(\$i,0,length(\"${param}=\"))==\"${param}=\")PARAM=substr(\$i,length(\"${param}=\")+1)}END{if(PARAM)print PARAM;else print \"${default}\";}"
2017-10-06 06:56:39 +00:00
}
# compile_and_test "<text>" "<where found>"
compile_and_test() {
2017-10-06 06:56:39 +00:00
asn="$1"
where="$2"
make_clean_before_test
2017-10-01 02:33:23 +00:00
asn_compile "$asn" "$where"
2017-10-06 06:56:39 +00:00
if [ $? -ne 0 ]; then
2017-10-01 02:33:23 +00:00
echo "Cannot compile ASN.1 $asn"
return 1
fi
rm -f random-test-driver.o
rm -f random-test-driver
2017-10-08 10:16:10 +00:00
CFLAGS="${CFLAGS}" Make
2017-10-06 06:56:39 +00:00
if [ $? -ne 0 ] ; then
2017-10-01 20:43:36 +00:00
echo "Cannot compile C for $asn in ${RNDTEMP}"
2017-10-01 02:33:23 +00:00
return 2
fi
# Maximum size of the random data
2017-10-06 06:56:39 +00:00
rmax=`get_param RMAX 128 "$asn"`
if [ "0${rmax}" -lt 1 ]; then rmax=128; fi
2017-10-01 02:33:23 +00:00
echo "Checking random data encode-decode"
2017-10-06 06:56:39 +00:00
round_trip_check_cmd="${ASAN_ENV_FLAGS} ./random-test-driver -s ${rmax} ${encodings} -c"
if eval "$round_trip_check_cmd"; then
echo "Random test OK"
else
2017-10-03 23:36:17 +00:00
if [ "x$CC" = "x" ]; then CCSTR=""; else CCSTR="CC=${CC} "; fi
2017-10-01 20:43:36 +00:00
echo "RETRY:"
2017-10-06 06:56:39 +00:00
echo "(cd ${RNDTEMP} && ${CCSTR}CFLAGS=\"${LIBFUZZER_CFLAGS} ${CFLAGS}\" ${MAKE:-make} && ${ASAN_ENV_FLAGS} ./random-test-driver -s ${rmax} ${encodings} -c)"
2017-10-01 02:33:23 +00:00
return 3
fi
2017-10-01 20:43:36 +00:00
echo "Generating new random data"
rm -rf random-data
cmd="${ASAN_ENV_FLAGS} UBSAN_OPTIONS=print_stacktrace=1"
2017-10-06 05:09:19 +00:00
cmd="${cmd} ./random-test-driver -s ${rmax} ${encodings} -g random-data"
2017-10-06 06:56:39 +00:00
if eval "$cmd" ; then
echo "Random data generated OK"
else
2017-10-01 20:43:36 +00:00
echo "RETRY:"
echo "(cd ${RNDTEMP} && $cmd)"
return 4
fi
2017-10-02 08:09:01 +00:00
# Do a LibFuzzer based testing
2017-10-06 06:56:39 +00:00
fuzz_time=10
fuzz_cmd="${ASAN_ENV_FLAGS} UBSAN_OPTIONS=print_stacktrace=1"
2017-10-06 05:09:19 +00:00
fuzz_cmd="${fuzz_cmd} ./random-test-driver"
fuzz_cmd="${fuzz_cmd} -timeout=3 -max_total_time=${fuzz_time} -max_len=128"
2017-10-02 08:09:01 +00:00
2017-10-06 06:56:39 +00:00
if grep "^fuzz:" Makefile >/dev/null ; then
echo "No fuzzer defined, skipping fuzzing"
else
fuzz_targets=`echo random-data/* | sed -e 's/random-data./fuzz-/g'`
2017-10-06 05:09:19 +00:00
{
echo "fuzz: $fuzz_targets"
echo "fuzz-%: random-data/% random-test-driver"
echo " ASN1_DATA_DIR=\$< ${fuzz_cmd} \$<"
} >> Makefile
2017-10-02 08:09:01 +00:00
fi
2017-10-01 02:33:23 +00:00
# If LIBFUZZER_CFLAGS are properly defined, do the fuzz test as well
2017-10-06 06:56:39 +00:00
if echo "${LIBFUZZER_CFLAGS}" | grep -i "[a-z]" > /dev/null; then
2017-10-01 02:33:23 +00:00
echo "Recompiling for fuzzing..."
rm -f random-test-driver.o
rm -f random-test-driver
2017-10-06 06:56:39 +00:00
CFLAGS="${LIBFUZZER_CFLAGS} ${CFLAGS}" Make
if [ $? -ne 0 ]; then
echo "Recompile failed"
return 4
fi
2017-10-02 08:09:01 +00:00
2017-10-06 05:09:19 +00:00
echo "Fuzzing will take a multiple of $fuzz_time seconds..."
2017-10-06 06:56:39 +00:00
Make fuzz
if [ $? -ne 0 ]; then
2017-10-02 08:09:01 +00:00
echo "RETRY:"
2017-10-06 06:56:39 +00:00
echo "(cd ${RNDTEMP} && CC=${CC} CFLAGS=\"${LIBFUZZER_CFLAGS} ${CFLAGS}\" ${MAKE:-make} fuzz)"
2017-10-02 08:09:01 +00:00
return 5
fi
2017-10-01 02:33:23 +00:00
fi
return 0
2017-10-01 02:33:23 +00:00
}
asn_compile() {
2017-10-06 06:56:39 +00:00
asn="$1"
where="$2"
2017-10-01 20:43:36 +00:00
# Create "INTEGER (1..2)" from "T ::= INTEGER (1..2) -- RMAX=5"
2017-10-06 06:56:39 +00:00
short_asn=`echo "$asn" | sed -e 's/ *--.*//;s/RMAX=[^ ]* //;'`
if [ `echo "$short_asn" | grep -c "::="` = 1 ]; then
short_asn=`echo "$short_asn" | sed -e 's/.*::= *//'`
2017-10-01 20:43:36 +00:00
fi
2017-10-01 02:33:23 +00:00
test ! -f Makefile.am # Protection from accidental clobbering
echo "Test DEFINITIONS ::= BEGIN $asn" > test.asn1
echo "-- ${where}" >> test.asn1
2017-10-01 02:33:23 +00:00
echo "END" >> test.asn1
2017-10-08 10:16:10 +00:00
echo "${abs_top_builddir}/asn1c/asn1c" -S "${abs_top_srcdir}/skeletons"
2017-10-06 06:56:39 +00:00
if "${abs_top_builddir}/asn1c/asn1c" -S "${abs_top_srcdir}/skeletons" \
-gen-OER -gen-PER ${asn1c_flags} test.asn1
then
2017-10-06 06:56:39 +00:00
echo "ASN.1 compiled OK"
else
return 1
fi
2017-10-01 02:33:23 +00:00
rm -f converter-example.c
ln -sf "../${srcdir}/random-test-driver.c" || cp "../${srcdir}/random-test-driver.c" .
2017-10-01 20:43:36 +00:00
echo "CFLAGS+= -DASN1_TEXT='$short_asn'" > Makefile
2017-10-01 02:33:23 +00:00
sed -e 's/converter-example/random-test-driver/' \
2017-10-01 20:43:36 +00:00
< Makefile.am.example >> Makefile
2017-10-01 02:33:23 +00:00
echo "Makefile.am.example -> Makefile"
}
# Make up to four different passes:
# CFLAGS: | asn1c_flags:
# -m64 | -fnative-types
# -m32 | -fnative-types
# -m64 | -fwide-types
# -m32 | -fwide-types
# *) Of course, -m64 and -fnative-types are just implied.
test_drive() {
func="$1"
shift
if [ "x${asn1c_flags}" = "x" ] ; then
# Test for native types and wide types
asn1c_flags=" " test_drive "${func}" "$@"
asn1c_flags="-fnative-types" test_drive "${func}" "$@"
return 0
fi
echo "MODE: default"
# Default (likely 64-bit) mode
${func} "$@"
# 32-bit mode, if available
if echo "${CFLAGS_M32}" | grep -i '[a-z]' > /dev/null ; then
echo "MODE: 32-bit"
# Unconditional clean, can't reuse object code.
(cd ${RNDTEMP} && Make clean)
# -m32 doesn't support fuzzing (no such library), so we remove fuzzer.
# -m32 doesn't support leak sanitizing (it hangs), so we remove
# ASAN_ENV_FLAGS which enable leak check.
CFLAGS="${CFLAGS} ${CFLAGS_M32}" CFLAGS_M32="" \
LIBFUZZER_CFLAGS="" ASAN_ENV_FLAGS="" \
${func} "$@"
fi
}
2017-10-01 02:33:23 +00:00
# Command line parsing
2017-10-04 01:40:36 +00:00
while :; do
case "$1" in
-h) usage ;;
2017-10-06 05:09:19 +00:00
--asn1c) asn1c_flags="${asn1c_flags} $2"; shift 2; continue ;;
2017-10-04 01:40:36 +00:00
--dirty)
need_clean_before_bundle=0
need_clean_before_test=0
shift
continue
;;
2017-10-06 05:09:19 +00:00
-e) encodings="${encodings} -e $2"; shift 2; continue;;
2017-10-04 01:40:36 +00:00
-j) parallelism="$1"; shift 2; continue;;
-t)
parallelism=1 # Better for debuggability
test_drive verify_asn_type "$2" "(command line)" || exit 1 ;;
2017-10-04 01:40:36 +00:00
"")
for bundle in `ls -1 ${srcdir}/bundles/*.txt | sort -nr`; do
test_drive verify_asn_types_in_file "$bundle"
2017-10-04 01:40:36 +00:00
done
;;
*)
test_drive verify_asn_types_in_file "$@"
2017-10-04 01:40:36 +00:00
;;
esac
break
done
2017-10-01 02:33:23 +00:00
2017-10-06 05:09:19 +00:00
if [ "$tests_succeeded" != "0" ] && [ "$tests_failed" = "0" ]; then
2017-10-01 02:33:23 +00:00
echo "OK $tests_succeeded tests"
else
echo "FAILED $tests_failed tests, OK $tests_succeeded tests"
exit 1
fi