blob: 0ac29fb6cfb3ed072713a07c6d87370aa5926b92 [file] [log] [blame]
Lev Walkincb523912017-09-30 19:33:23 -07001#!/bin/sh
2
3#
4# Create an ASN.1 source code project for each line in each of the
5# bundles/*.txt files, compile and run that it can be encoded, decoded,
6# and fuzzed (if fuzzing is available).
7#
8
9set -e
10
Lev Walkincb523912017-09-30 19:33:23 -070011usage() {
12 echo "Usage:"
13 echo " $0 -h"
Lev Walkin93f372b2017-10-03 16:39:38 -070014 echo " $0 [--dirty] -t \"<ASN.1 text defining type T, in string form>\""
15 echo " $0 [--dirty] bundles/<bundle-name.txt> [<line>]"
Lev Walkin8cec5232017-10-03 15:05:54 -070016 echo "Where options are:"
Lev Walkin43292722017-10-05 00:33:32 -070017 echo " -h Show this help screen"
18 echo " -e <syntax> Verify a given encoding explicitly (default is ALL)"
19 echo " --asn1c <flag> Add this flag to asn1c"
20 echo " --dirty Reuse compile results from the previous run(s)"
21 echo " -t <ASN.1> Run this particular typel"
Lev Walkincb523912017-09-30 19:33:23 -070022 echo "Examples:"
23 echo " $0 -t UTF8String"
24 echo " $0 -t \"T ::= INTEGER (0..1)\""
25 echo " $0 bundles/01-INTEGER-bundle.txt 3"
26 exit 1
27}
28
Lev Walkin40b8a7a2017-10-08 22:36:29 -070029RNDTEMP="${RNDTEMP:-.tmp.random}"
Lev Walkind3cce462017-10-01 13:43:36 -070030
31srcdir="${srcdir:-.}"
Lev Walkin39936ce2017-10-05 23:56:39 -070032abs_top_srcdir="${abs_top_srcdir:-`pwd`/../../}"
33abs_top_builddir="${abs_top_builddir:-`pwd`/../../}"
Lev Walkind0d63922017-10-10 01:27:37 -070034MAKE="${MAKE:-make}"
35FUZZ_TIME="${FUZZ_TIME:-10}"
Lev Walkind3cce462017-10-01 13:43:36 -070036
Lev Walkincb523912017-09-30 19:33:23 -070037tests_succeeded=0
38tests_failed=0
Lev Walkind3cce462017-10-01 13:43:36 -070039stop_after_failed=1 # We stop after 3 failures.
Lev Walkin8cec5232017-10-03 15:05:54 -070040need_clean_before_bundle=1 # Clean before testing a bundle file
Lev Walkin7c470c72017-10-08 04:01:45 -070041need_clean_before_test=0 # Before each line in a bundle file
Lev Walkin68619bf2017-10-03 18:40:36 -070042encodings="" # Default is to verify all supported ASN.1 transfer syntaxes
Lev Walkin40b8a7a2017-10-08 22:36:29 -070043parallelism=1
Lev Walkin43292722017-10-05 00:33:32 -070044asn1c_flags=""
Lev Walkin8cec5232017-10-03 15:05:54 -070045
46make_clean_before_bundle() {
Lev Walkinab25f192017-10-08 12:50:43 -070047 if [ "${need_clean_before_bundle}" = "1" ] ; then
Lev Walkinac6db172017-10-10 00:31:06 -070048 (cd "${RNDTEMP}" && Make clean) || :
Lev Walkinab25f192017-10-08 12:50:43 -070049 fi
Lev Walkin8cec5232017-10-03 15:05:54 -070050}
51
52make_clean_before_test() {
Lev Walkin60d62672017-10-08 03:16:10 -070053 if [ "${need_clean_before_test}" = "1" ] ; then
54 Make clean
55 fi
Lev Walkin8cec5232017-10-03 15:05:54 -070056}
Lev Walkincb523912017-09-30 19:33:23 -070057
58# Get all the type-bearding lines in file and process them individually
59verify_asn_types_in_file() {
Lev Walkin39936ce2017-10-05 23:56:39 -070060 filename="$1"
61 need_line="$2"
Lev Walkincb523912017-09-30 19:33:23 -070062 test "x$filename" != "x" || usage
Lev Walkin8cec5232017-10-03 15:05:54 -070063
64 make_clean_before_bundle
65
Lev Walkincb523912017-09-30 19:33:23 -070066 echo "Open [$filename]"
Lev Walkin39936ce2017-10-05 23:56:39 -070067 line=0
68 asn=""
69 while read asn; do
70 line=`echo "$line+1" | bc`
71 if echo "$asn" | sed -e 's/--.*//;' | grep -vi "[A-Z]" > /dev/null; then
Lev Walkincb523912017-09-30 19:33:23 -070072 # Ignore lines consisting of just comments.
73 continue;
74 fi
Lev Walkind28b45f2017-10-05 22:09:19 -070075 if [ "x$need_line" != "x" ] && [ "$need_line" != "$line" ]; then
Lev Walkincb523912017-09-30 19:33:23 -070076 # We need a different line.
77 continue;
78 fi
79 verify_asn_type "$asn" "in $filename $line"
Lev Walkind3cce462017-10-01 13:43:36 -070080 if [ "${tests_failed}" = "${stop_after_failed}" ]; then
81 echo "STOP after ${tests_failed} failures, OK ${tests_succeeded}"
82 exit 1
83 fi
Lev Walkincb523912017-09-30 19:33:23 -070084 done < "$filename"
85}
86
87verify_asn_type() {
Lev Walkin39936ce2017-10-05 23:56:39 -070088 asn="$1"
Lev Walkin7c470c72017-10-08 04:01:45 -070089 where="$2"
90 shift 2
Lev Walkincb523912017-09-30 19:33:23 -070091 test "x$asn" != "x" || usage
Lev Walkin8cec5232017-10-03 15:05:54 -070092
Lev Walkin39936ce2017-10-05 23:56:39 -070093 if echo "$asn" | grep -v "::=" > /dev/null; then
Lev Walkincb523912017-09-30 19:33:23 -070094 asn="T ::= $asn"
95 fi
96 echo "Testing [$asn] ${where}"
97
Lev Walkind3cce462017-10-01 13:43:36 -070098 mkdir -p ${RNDTEMP}
Lev Walkinac6db172017-10-10 00:31:06 -070099 if (set -e && cd "${RNDTEMP}" && compile_and_test "$asn" "${where}"); then
Lev Walkincb523912017-09-30 19:33:23 -0700100 echo "OK [$asn] ${where}"
Lev Walkin39936ce2017-10-05 23:56:39 -0700101 tests_succeeded=`echo "$tests_succeeded + 1" | bc`
Lev Walkincb523912017-09-30 19:33:23 -0700102 else
Lev Walkin39936ce2017-10-05 23:56:39 -0700103 tests_failed=`echo "$tests_failed + 1" | bc`
Lev Walkincb523912017-09-30 19:33:23 -0700104 echo "FAIL [$asn] ${where}"
105 fi
106}
107
Lev Walkin39936ce2017-10-05 23:56:39 -0700108Make() {
Lev Walkind0d63922017-10-10 01:27:37 -0700109 ${MAKE} -j "${parallelism}" "$@" || return $?
Lev Walkin39936ce2017-10-05 23:56:39 -0700110}
111
112get_param() {
113 param="$1"
114 default="$2"
Lev Walkinebeb4012017-10-09 19:51:26 -0700115 asn="$3"
Lev Walkin39936ce2017-10-05 23:56:39 -0700116
Lev Walkinebeb4012017-10-09 19:51:26 -0700117 if nawk '' >/dev/null 2>&1 ; then
118 AWK=nawk
119 else
120 AWK=awk
121 fi
Lev Walkin39936ce2017-10-05 23:56:39 -0700122
Lev Walkinebeb4012017-10-09 19:51:26 -0700123 echo "$asn" | ${AWK} "BEGIN{FS=\"[^${param}=0-9]+\"};/$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}\";}"
Lev Walkin39936ce2017-10-05 23:56:39 -0700124}
125
Lev Walkin7c470c72017-10-08 04:01:45 -0700126# compile_and_test "<text>" "<where found>"
Lev Walkinac6db172017-10-10 00:31:06 -0700127# This function is executed in the temporary test directory ${RNDTEMP}.
Lev Walkin7c470c72017-10-08 04:01:45 -0700128compile_and_test() {
Lev Walkin39936ce2017-10-05 23:56:39 -0700129 asn="$1"
130 where="$2"
Lev Walkin43292722017-10-05 00:33:32 -0700131
Lev Walkinac6db172017-10-10 00:31:06 -0700132 if [ "x$CC" = "x" ]; then CCSTR=""; else CCSTR="CC=${CC} "; fi
Lev Walkind0d63922017-10-10 01:27:37 -0700133 reproduce_make="cd \"${RNDTEMP}\" && ${CCSTR}CFLAGS=\"${CFLAGS}\" ${MAKE}"
Lev Walkinac6db172017-10-10 00:31:06 -0700134
135 env > .test-environment
136 set > .test-set
137
Lev Walkin43292722017-10-05 00:33:32 -0700138 make_clean_before_test
139
Lev Walkin7c470c72017-10-08 04:01:45 -0700140 asn_compile "$asn" "$where"
Lev Walkin39936ce2017-10-05 23:56:39 -0700141 if [ $? -ne 0 ]; then
Lev Walkincb523912017-09-30 19:33:23 -0700142 echo "Cannot compile ASN.1 $asn"
143 return 1
144 fi
145
146 rm -f random-test-driver.o
147 rm -f random-test-driver
Lev Walkin60d62672017-10-08 03:16:10 -0700148 CFLAGS="${CFLAGS}" Make
Lev Walkin39936ce2017-10-05 23:56:39 -0700149 if [ $? -ne 0 ] ; then
Lev Walkind3cce462017-10-01 13:43:36 -0700150 echo "Cannot compile C for $asn in ${RNDTEMP}"
Lev Walkincb523912017-09-30 19:33:23 -0700151 return 2
152 fi
153
Lev Walkin791d3b72017-10-02 16:24:28 -0700154 # Maximum size of the random data
Lev Walkin39936ce2017-10-05 23:56:39 -0700155 rmax=`get_param RMAX 128 "$asn"`
Lev Walkin791d3b72017-10-02 16:24:28 -0700156 if [ "0${rmax}" -lt 1 ]; then rmax=128; fi
157
Lev Walkincb523912017-09-30 19:33:23 -0700158 echo "Checking random data encode-decode"
Lev Walkin39936ce2017-10-05 23:56:39 -0700159 round_trip_check_cmd="${ASAN_ENV_FLAGS} ./random-test-driver -s ${rmax} ${encodings} -c"
Lev Walkinac6db172017-10-10 00:31:06 -0700160 echo "(${reproduce_make} && ${round_trip_check_cmd})" > .test-reproduce
Lev Walkin39936ce2017-10-05 23:56:39 -0700161 if eval "$round_trip_check_cmd"; then
162 echo "Random test OK"
163 else
Lev Walkinac6db172017-10-10 00:31:06 -0700164 { echo "RETRY:"; cat .test-reproduce ; }
Lev Walkincb523912017-09-30 19:33:23 -0700165 return 3
166 fi
167
Lev Walkind3cce462017-10-01 13:43:36 -0700168 echo "Generating new random data"
169 rm -rf random-data
170 cmd="${ASAN_ENV_FLAGS} UBSAN_OPTIONS=print_stacktrace=1"
Lev Walkind28b45f2017-10-05 22:09:19 -0700171 cmd="${cmd} ./random-test-driver -s ${rmax} ${encodings} -g random-data"
Lev Walkinac6db172017-10-10 00:31:06 -0700172 echo "(${reproduce_make} && ${cmd})" > .test-reproduce
Lev Walkin39936ce2017-10-05 23:56:39 -0700173 if eval "$cmd" ; then
174 echo "Random data generated OK"
175 else
Lev Walkinac6db172017-10-10 00:31:06 -0700176 { echo "RETRY:"; cat .test-reproduce ; }
Lev Walkind3cce462017-10-01 13:43:36 -0700177 return 4
178 fi
179
Lev Walkin233d14d2017-10-02 01:09:01 -0700180 # Do a LibFuzzer based testing
Lev Walkin39936ce2017-10-05 23:56:39 -0700181 fuzz_cmd="${ASAN_ENV_FLAGS} UBSAN_OPTIONS=print_stacktrace=1"
Lev Walkind28b45f2017-10-05 22:09:19 -0700182 fuzz_cmd="${fuzz_cmd} ./random-test-driver"
Lev Walkind0d63922017-10-10 01:27:37 -0700183 fuzz_cmd="${fuzz_cmd} -timeout=3 -max_total_time=${FUZZ_TIME} -max_len=128"
Lev Walkin233d14d2017-10-02 01:09:01 -0700184
Lev Walkin39936ce2017-10-05 23:56:39 -0700185 if grep "^fuzz:" Makefile >/dev/null ; then
186 echo "No fuzzer defined, skipping fuzzing"
187 else
188 fuzz_targets=`echo random-data/* | sed -e 's/random-data./fuzz-/g'`
Lev Walkind28b45f2017-10-05 22:09:19 -0700189 {
190 echo "fuzz: $fuzz_targets"
191 echo "fuzz-%: random-data/% random-test-driver"
192 echo " ASN1_DATA_DIR=\$< ${fuzz_cmd} \$<"
193 } >> Makefile
Lev Walkin233d14d2017-10-02 01:09:01 -0700194 fi
195
Lev Walkincb523912017-09-30 19:33:23 -0700196 # If LIBFUZZER_CFLAGS are properly defined, do the fuzz test as well
Lev Walkin39936ce2017-10-05 23:56:39 -0700197 if echo "${LIBFUZZER_CFLAGS}" | grep -i "[a-z]" > /dev/null; then
Lev Walkincb523912017-09-30 19:33:23 -0700198
199 echo "Recompiling for fuzzing..."
200 rm -f random-test-driver.o
201 rm -f random-test-driver
Lev Walkind0d63922017-10-10 01:27:37 -0700202 reproduce_make="cd \"${RNDTEMP}\" && ${CCSTR}CFLAGS=\"${LIBFUZZER_CFLAGS} ${CFLAGS}\" ${MAKE}"
Lev Walkinac6db172017-10-10 00:31:06 -0700203 echo "(${reproduce_make})" > .test-reproduce
Lev Walkin39936ce2017-10-05 23:56:39 -0700204 CFLAGS="${LIBFUZZER_CFLAGS} ${CFLAGS}" Make
205 if [ $? -ne 0 ]; then
206 echo "Recompile failed"
207 return 4
208 fi
Lev Walkincb523912017-09-30 19:33:23 -0700209
Lev Walkind0d63922017-10-10 01:27:37 -0700210 echo "Fuzzing will take a multiple of ${FUZZ_TIME} seconds..."
Lev Walkinac6db172017-10-10 00:31:06 -0700211 echo "(${reproduce_make} fuzz)" > .test-reproduce
212 CFLAGS="${LIBFUZZER_CFLAGS} ${CFLAGS}" Make fuzz
Lev Walkin39936ce2017-10-05 23:56:39 -0700213 if [ $? -ne 0 ]; then
Lev Walkinac6db172017-10-10 00:31:06 -0700214 { echo "RETRY:"; cat .test-reproduce ; }
Lev Walkin233d14d2017-10-02 01:09:01 -0700215 return 5
216 fi
Lev Walkincb523912017-09-30 19:33:23 -0700217 fi
218
Lev Walkina5b02882017-10-01 22:48:44 -0700219 return 0
Lev Walkincb523912017-09-30 19:33:23 -0700220}
221
Lev Walkincb523912017-09-30 19:33:23 -0700222asn_compile() {
Lev Walkin39936ce2017-10-05 23:56:39 -0700223 asn="$1"
224 where="$2"
Lev Walkind3cce462017-10-01 13:43:36 -0700225
Lev Walkin5d947a82017-10-03 01:04:03 -0700226 # Create "INTEGER (1..2)" from "T ::= INTEGER (1..2) -- RMAX=5"
Lev Walkinebeb4012017-10-09 19:51:26 -0700227 short_asn=`echo "$asn" | sed -e 's/ *--.*//;s/RMAX=[0-9]//;'`
Lev Walkin39936ce2017-10-05 23:56:39 -0700228 if [ `echo "$short_asn" | grep -c "::="` = 1 ]; then
229 short_asn=`echo "$short_asn" | sed -e 's/.*::= *//'`
Lev Walkind3cce462017-10-01 13:43:36 -0700230 fi
231
Lev Walkincb523912017-09-30 19:33:23 -0700232 test ! -f Makefile.am # Protection from accidental clobbering
Lev Walkinac6db172017-10-10 00:31:06 -0700233 {
234 echo "Test DEFINITIONS ::= BEGIN $asn"
235 echo "-- ${where}"
236 echo "END"
237 } > test.asn1
238 echo "${abs_top_builddir}/asn1c/asn1c -S ${abs_top_srcdir}/skeletons"
Lev Walkin39936ce2017-10-05 23:56:39 -0700239 if "${abs_top_builddir}/asn1c/asn1c" -S "${abs_top_srcdir}/skeletons" \
Lev Walkin7c470c72017-10-08 04:01:45 -0700240 -gen-OER -gen-PER ${asn1c_flags} test.asn1
Lev Walkina5b02882017-10-01 22:48:44 -0700241 then
Lev Walkin39936ce2017-10-05 23:56:39 -0700242 echo "ASN.1 compiled OK"
243 else
Lev Walkina5b02882017-10-01 22:48:44 -0700244 return 1
245 fi
Lev Walkincb523912017-09-30 19:33:23 -0700246 rm -f converter-example.c
Lev Walkin09be69c2017-10-08 16:28:11 -0700247 ln -sf "../${srcdir}/random-test-driver.c" || cp "../${srcdir}/random-test-driver.c" .
Lev Walkin40b8a7a2017-10-08 22:36:29 -0700248 {
249 echo "CFLAGS+= -DASN1_TEXT='$short_asn'";
250 echo "ASN_PROGRAM = random-test-driver"
251 echo "ASN_PROGRAM_SOURCES = random-test-driver.c"
Lev Walkinac6db172017-10-10 00:31:06 -0700252 echo
Lev Walkin40b8a7a2017-10-08 22:36:29 -0700253 echo "include Makefile.am.example"
Lev Walkinac6db172017-10-10 00:31:06 -0700254 echo
255 echo "all-tests-succeeded: ${abs_top_builddir}/asn1c/asn1c \$(ASN_PROGRAM_SOURCES) \$(ASN_MODULE_SOURCES) \$(ASN_MODULE_HEADERS)"
256 echo " @rm -f \$@"
257 echo " @echo Previous try did not go correctly. To reproduce:"
258 echo " @cat .test-reproduce"
259 echo " @exit 1"
260 echo
Lev Walkin40b8a7a2017-10-08 22:36:29 -0700261 } > Makefile
Lev Walkincb523912017-09-30 19:33:23 -0700262 echo "Makefile.am.example -> Makefile"
263}
264
Lev Walkin7c470c72017-10-08 04:01:45 -0700265# Make up to four different passes:
266# CFLAGS: | asn1c_flags:
267# -m64 | -fnative-types
268# -m32 | -fnative-types
269# -m64 | -fwide-types
270# -m32 | -fwide-types
271# *) Of course, -m64 and -fnative-types are just implied.
272test_drive() {
273 func="$1"
274 shift
275
276 if [ "x${asn1c_flags}" = "x" ] ; then
277 # Test for native types and wide types
278 asn1c_flags=" " test_drive "${func}" "$@"
279 asn1c_flags="-fnative-types" test_drive "${func}" "$@"
280 return 0
281 fi
282
Lev Walkinac6db172017-10-10 00:31:06 -0700283 # Can't reuse object code.
284 rm -rf ${RNDTEMP}
285
Lev Walkin7c470c72017-10-08 04:01:45 -0700286 echo "MODE: default"
287 # Default (likely 64-bit) mode
288 ${func} "$@"
289
290 # 32-bit mode, if available
291 if echo "${CFLAGS_M32}" | grep -i '[a-z]' > /dev/null ; then
292 echo "MODE: 32-bit"
Lev Walkinac6db172017-10-10 00:31:06 -0700293
294 # Can't reuse object code between modes.
295 rm -rf ${RNDTEMP}
296
Lev Walkin7c470c72017-10-08 04:01:45 -0700297 # -m32 doesn't support fuzzing (no such library), so we remove fuzzer.
298 # -m32 doesn't support leak sanitizing (it hangs), so we remove
Lev Walkinac6db172017-10-10 00:31:06 -0700299 # ASAN_ENV_FLAGS which enable leak check in runtime.
Lev Walkin7c470c72017-10-08 04:01:45 -0700300 CFLAGS="${CFLAGS} ${CFLAGS_M32}" CFLAGS_M32="" \
301 LIBFUZZER_CFLAGS="" ASAN_ENV_FLAGS="" \
302 ${func} "$@"
303 fi
304}
305
Lev Walkin40b8a7a2017-10-08 22:36:29 -0700306if echo "$*" | grep ' -- ' > /dev/null; then
307 TEST_DRIVER=`echo "$*" | sed -e 's/ -- .*/ -- /g'`
308 args=`echo "$*" | sed -e 's/.* //g'`
309 set "${args}"
310else
311 TEST_DRIVER=""
312fi
313
Lev Walkincb523912017-09-30 19:33:23 -0700314# Command line parsing
Lev Walkin68619bf2017-10-03 18:40:36 -0700315while :; do
316 case "$1" in
317 -h) usage ;;
Lev Walkind28b45f2017-10-05 22:09:19 -0700318 --asn1c) asn1c_flags="${asn1c_flags} $2"; shift 2; continue ;;
Lev Walkin40b8a7a2017-10-08 22:36:29 -0700319 --bundle)
320 shift
Lev Walkinac6db172017-10-10 00:31:06 -0700321
322 # Look for the transcript in bundles/NN-*-bundles.txt.log
323 set -x
324
Lev Walkin40b8a7a2017-10-08 22:36:29 -0700325 base=`basename "$1" | sed -e 's/.txt$//'`
326 RNDTEMP=".tmp.${base}"
Lev Walkinac6db172017-10-10 00:31:06 -0700327
328 if Make -C "${RNDTEMP}" all-tests-succeeded >/dev/null 2>&1 ; then
329 echo "Test succeeded before. Not rechecking."
330 tests_succeeded=1
331 break
332 fi
333
Lev Walkin40b8a7a2017-10-08 22:36:29 -0700334 test_drive verify_asn_types_in_file "$@"
Lev Walkinac6db172017-10-10 00:31:06 -0700335
336 touch "${RNDTEMP}/all-tests-succeeded"
337
Lev Walkin40b8a7a2017-10-08 22:36:29 -0700338 break
339 ;;
Lev Walkin68619bf2017-10-03 18:40:36 -0700340 --dirty)
341 need_clean_before_bundle=0
342 need_clean_before_test=0
343 shift
344 continue
345 ;;
Lev Walkind28b45f2017-10-05 22:09:19 -0700346 -e) encodings="${encodings} -e $2"; shift 2; continue;;
Lev Walkin68619bf2017-10-03 18:40:36 -0700347 -j) parallelism="$1"; shift 2; continue;;
348 -t)
Lev Walkin7c470c72017-10-08 04:01:45 -0700349 test_drive verify_asn_type "$2" "(command line)" || exit 1 ;;
Lev Walkin68619bf2017-10-03 18:40:36 -0700350 "")
Lev Walkin09be69c2017-10-08 16:28:11 -0700351 for bundle in `ls -1 ${srcdir}/bundles/*.txt | sort -nr`; do
Lev Walkin7c470c72017-10-08 04:01:45 -0700352 test_drive verify_asn_types_in_file "$bundle"
Lev Walkin68619bf2017-10-03 18:40:36 -0700353 done
354 ;;
355 *)
Lev Walkin40b8a7a2017-10-08 22:36:29 -0700356 exec ${TEST_DRIVER} $0 --bundle "$@"
Lev Walkin68619bf2017-10-03 18:40:36 -0700357 ;;
358 esac
359 break
360done
Lev Walkincb523912017-09-30 19:33:23 -0700361
Lev Walkind28b45f2017-10-05 22:09:19 -0700362if [ "$tests_succeeded" != "0" ] && [ "$tests_failed" = "0" ]; then
Lev Walkincb523912017-09-30 19:33:23 -0700363 echo "OK $tests_succeeded tests"
364else
365 echo "FAILED $tests_failed tests, OK $tests_succeeded tests"
366 exit 1
367fi