blob: 6d83525a4d6ff387ea049affe7cc1e0580808caf [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 Walkind3cce462017-10-01 13:43:36 -070034
Lev Walkincb523912017-09-30 19:33:23 -070035tests_succeeded=0
36tests_failed=0
Lev Walkind3cce462017-10-01 13:43:36 -070037stop_after_failed=1 # We stop after 3 failures.
Lev Walkin8cec5232017-10-03 15:05:54 -070038need_clean_before_bundle=1 # Clean before testing a bundle file
Lev Walkin7c470c72017-10-08 04:01:45 -070039need_clean_before_test=0 # Before each line in a bundle file
Lev Walkin68619bf2017-10-03 18:40:36 -070040encodings="" # Default is to verify all supported ASN.1 transfer syntaxes
Lev Walkin40b8a7a2017-10-08 22:36:29 -070041parallelism=1
Lev Walkin43292722017-10-05 00:33:32 -070042asn1c_flags=""
Lev Walkin8cec5232017-10-03 15:05:54 -070043
44make_clean_before_bundle() {
Lev Walkinab25f192017-10-08 12:50:43 -070045 if [ "${need_clean_before_bundle}" = "1" ] ; then
Lev Walkinac6db172017-10-10 00:31:06 -070046 (cd "${RNDTEMP}" && Make clean) || :
Lev Walkinab25f192017-10-08 12:50:43 -070047 fi
Lev Walkin8cec5232017-10-03 15:05:54 -070048}
49
50make_clean_before_test() {
Lev Walkin60d62672017-10-08 03:16:10 -070051 if [ "${need_clean_before_test}" = "1" ] ; then
52 Make clean
53 fi
Lev Walkin8cec5232017-10-03 15:05:54 -070054}
Lev Walkincb523912017-09-30 19:33:23 -070055
56# Get all the type-bearding lines in file and process them individually
57verify_asn_types_in_file() {
Lev Walkin39936ce2017-10-05 23:56:39 -070058 filename="$1"
59 need_line="$2"
Lev Walkincb523912017-09-30 19:33:23 -070060 test "x$filename" != "x" || usage
Lev Walkin8cec5232017-10-03 15:05:54 -070061
62 make_clean_before_bundle
63
Lev Walkincb523912017-09-30 19:33:23 -070064 echo "Open [$filename]"
Lev Walkin39936ce2017-10-05 23:56:39 -070065 line=0
66 asn=""
67 while read asn; do
68 line=`echo "$line+1" | bc`
69 if echo "$asn" | sed -e 's/--.*//;' | grep -vi "[A-Z]" > /dev/null; then
Lev Walkincb523912017-09-30 19:33:23 -070070 # Ignore lines consisting of just comments.
71 continue;
72 fi
Lev Walkind28b45f2017-10-05 22:09:19 -070073 if [ "x$need_line" != "x" ] && [ "$need_line" != "$line" ]; then
Lev Walkincb523912017-09-30 19:33:23 -070074 # We need a different line.
75 continue;
76 fi
77 verify_asn_type "$asn" "in $filename $line"
Lev Walkind3cce462017-10-01 13:43:36 -070078 if [ "${tests_failed}" = "${stop_after_failed}" ]; then
79 echo "STOP after ${tests_failed} failures, OK ${tests_succeeded}"
80 exit 1
81 fi
Lev Walkincb523912017-09-30 19:33:23 -070082 done < "$filename"
83}
84
85verify_asn_type() {
Lev Walkin39936ce2017-10-05 23:56:39 -070086 asn="$1"
Lev Walkin7c470c72017-10-08 04:01:45 -070087 where="$2"
88 shift 2
Lev Walkincb523912017-09-30 19:33:23 -070089 test "x$asn" != "x" || usage
Lev Walkin8cec5232017-10-03 15:05:54 -070090
Lev Walkin39936ce2017-10-05 23:56:39 -070091 if echo "$asn" | grep -v "::=" > /dev/null; then
Lev Walkincb523912017-09-30 19:33:23 -070092 asn="T ::= $asn"
93 fi
94 echo "Testing [$asn] ${where}"
95
Lev Walkind3cce462017-10-01 13:43:36 -070096 mkdir -p ${RNDTEMP}
Lev Walkinac6db172017-10-10 00:31:06 -070097 if (set -e && cd "${RNDTEMP}" && compile_and_test "$asn" "${where}"); then
Lev Walkincb523912017-09-30 19:33:23 -070098 echo "OK [$asn] ${where}"
Lev Walkin39936ce2017-10-05 23:56:39 -070099 tests_succeeded=`echo "$tests_succeeded + 1" | bc`
Lev Walkincb523912017-09-30 19:33:23 -0700100 else
Lev Walkin39936ce2017-10-05 23:56:39 -0700101 tests_failed=`echo "$tests_failed + 1" | bc`
Lev Walkincb523912017-09-30 19:33:23 -0700102 echo "FAIL [$asn] ${where}"
103 fi
104}
105
Lev Walkin39936ce2017-10-05 23:56:39 -0700106Make() {
107 ${MAKE:-make} -j "${parallelism}" "$@" || return $?
108}
109
110get_param() {
111 param="$1"
112 default="$2"
Lev Walkinebeb4012017-10-09 19:51:26 -0700113 asn="$3"
Lev Walkin39936ce2017-10-05 23:56:39 -0700114
Lev Walkinebeb4012017-10-09 19:51:26 -0700115 if nawk '' >/dev/null 2>&1 ; then
116 AWK=nawk
117 else
118 AWK=awk
119 fi
Lev Walkin39936ce2017-10-05 23:56:39 -0700120
Lev Walkinebeb4012017-10-09 19:51:26 -0700121 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 -0700122}
123
Lev Walkin7c470c72017-10-08 04:01:45 -0700124# compile_and_test "<text>" "<where found>"
Lev Walkinac6db172017-10-10 00:31:06 -0700125# This function is executed in the temporary test directory ${RNDTEMP}.
Lev Walkin7c470c72017-10-08 04:01:45 -0700126compile_and_test() {
Lev Walkin39936ce2017-10-05 23:56:39 -0700127 asn="$1"
128 where="$2"
Lev Walkin43292722017-10-05 00:33:32 -0700129
Lev Walkinac6db172017-10-10 00:31:06 -0700130 if [ "x$CC" = "x" ]; then CCSTR=""; else CCSTR="CC=${CC} "; fi
131 reproduce_make="cd \"${RNDTEMP}\" && ${CCSTR}CFLAGS=\"${CFLAGS}\" ${MAKE:-make}"
132
133 env > .test-environment
134 set > .test-set
135
Lev Walkin43292722017-10-05 00:33:32 -0700136 make_clean_before_test
137
Lev Walkin7c470c72017-10-08 04:01:45 -0700138 asn_compile "$asn" "$where"
Lev Walkin39936ce2017-10-05 23:56:39 -0700139 if [ $? -ne 0 ]; then
Lev Walkincb523912017-09-30 19:33:23 -0700140 echo "Cannot compile ASN.1 $asn"
141 return 1
142 fi
143
144 rm -f random-test-driver.o
145 rm -f random-test-driver
Lev Walkin60d62672017-10-08 03:16:10 -0700146 CFLAGS="${CFLAGS}" Make
Lev Walkin39936ce2017-10-05 23:56:39 -0700147 if [ $? -ne 0 ] ; then
Lev Walkind3cce462017-10-01 13:43:36 -0700148 echo "Cannot compile C for $asn in ${RNDTEMP}"
Lev Walkincb523912017-09-30 19:33:23 -0700149 return 2
150 fi
151
Lev Walkin791d3b72017-10-02 16:24:28 -0700152 # Maximum size of the random data
Lev Walkin39936ce2017-10-05 23:56:39 -0700153 rmax=`get_param RMAX 128 "$asn"`
Lev Walkin791d3b72017-10-02 16:24:28 -0700154 if [ "0${rmax}" -lt 1 ]; then rmax=128; fi
155
Lev Walkincb523912017-09-30 19:33:23 -0700156 echo "Checking random data encode-decode"
Lev Walkin39936ce2017-10-05 23:56:39 -0700157 round_trip_check_cmd="${ASAN_ENV_FLAGS} ./random-test-driver -s ${rmax} ${encodings} -c"
Lev Walkinac6db172017-10-10 00:31:06 -0700158 echo "(${reproduce_make} && ${round_trip_check_cmd})" > .test-reproduce
Lev Walkin39936ce2017-10-05 23:56:39 -0700159 if eval "$round_trip_check_cmd"; then
160 echo "Random test OK"
161 else
Lev Walkinac6db172017-10-10 00:31:06 -0700162 { echo "RETRY:"; cat .test-reproduce ; }
Lev Walkincb523912017-09-30 19:33:23 -0700163 return 3
164 fi
165
Lev Walkind3cce462017-10-01 13:43:36 -0700166 echo "Generating new random data"
167 rm -rf random-data
168 cmd="${ASAN_ENV_FLAGS} UBSAN_OPTIONS=print_stacktrace=1"
Lev Walkind28b45f2017-10-05 22:09:19 -0700169 cmd="${cmd} ./random-test-driver -s ${rmax} ${encodings} -g random-data"
Lev Walkinac6db172017-10-10 00:31:06 -0700170 echo "(${reproduce_make} && ${cmd})" > .test-reproduce
Lev Walkin39936ce2017-10-05 23:56:39 -0700171 if eval "$cmd" ; then
172 echo "Random data generated OK"
173 else
Lev Walkinac6db172017-10-10 00:31:06 -0700174 { echo "RETRY:"; cat .test-reproduce ; }
Lev Walkind3cce462017-10-01 13:43:36 -0700175 return 4
176 fi
177
Lev Walkin233d14d2017-10-02 01:09:01 -0700178 # Do a LibFuzzer based testing
Lev Walkin39936ce2017-10-05 23:56:39 -0700179 fuzz_time=10
180 fuzz_cmd="${ASAN_ENV_FLAGS} UBSAN_OPTIONS=print_stacktrace=1"
Lev Walkind28b45f2017-10-05 22:09:19 -0700181 fuzz_cmd="${fuzz_cmd} ./random-test-driver"
182 fuzz_cmd="${fuzz_cmd} -timeout=3 -max_total_time=${fuzz_time} -max_len=128"
Lev Walkin233d14d2017-10-02 01:09:01 -0700183
Lev Walkin39936ce2017-10-05 23:56:39 -0700184 if grep "^fuzz:" Makefile >/dev/null ; then
185 echo "No fuzzer defined, skipping fuzzing"
186 else
187 fuzz_targets=`echo random-data/* | sed -e 's/random-data./fuzz-/g'`
Lev Walkind28b45f2017-10-05 22:09:19 -0700188 {
189 echo "fuzz: $fuzz_targets"
190 echo "fuzz-%: random-data/% random-test-driver"
191 echo " ASN1_DATA_DIR=\$< ${fuzz_cmd} \$<"
192 } >> Makefile
Lev Walkin233d14d2017-10-02 01:09:01 -0700193 fi
194
Lev Walkincb523912017-09-30 19:33:23 -0700195 # If LIBFUZZER_CFLAGS are properly defined, do the fuzz test as well
Lev Walkin39936ce2017-10-05 23:56:39 -0700196 if echo "${LIBFUZZER_CFLAGS}" | grep -i "[a-z]" > /dev/null; then
Lev Walkincb523912017-09-30 19:33:23 -0700197
198 echo "Recompiling for fuzzing..."
199 rm -f random-test-driver.o
200 rm -f random-test-driver
Lev Walkinac6db172017-10-10 00:31:06 -0700201 reproduce_make="cd \"${RNDTEMP}\" && ${CCSTR}CFLAGS=\"${LIBFUZZER_CFLAGS} ${CFLAGS}\" ${MAKE:-make}"
202 echo "(${reproduce_make})" > .test-reproduce
Lev Walkin39936ce2017-10-05 23:56:39 -0700203 CFLAGS="${LIBFUZZER_CFLAGS} ${CFLAGS}" Make
204 if [ $? -ne 0 ]; then
205 echo "Recompile failed"
206 return 4
207 fi
Lev Walkincb523912017-09-30 19:33:23 -0700208
Lev Walkind28b45f2017-10-05 22:09:19 -0700209 echo "Fuzzing will take a multiple of $fuzz_time seconds..."
Lev Walkinac6db172017-10-10 00:31:06 -0700210 echo "(${reproduce_make} fuzz)" > .test-reproduce
211 CFLAGS="${LIBFUZZER_CFLAGS} ${CFLAGS}" Make fuzz
Lev Walkin39936ce2017-10-05 23:56:39 -0700212 if [ $? -ne 0 ]; then
Lev Walkinac6db172017-10-10 00:31:06 -0700213 { echo "RETRY:"; cat .test-reproduce ; }
Lev Walkin233d14d2017-10-02 01:09:01 -0700214 return 5
215 fi
Lev Walkincb523912017-09-30 19:33:23 -0700216 fi
217
Lev Walkina5b02882017-10-01 22:48:44 -0700218 return 0
Lev Walkincb523912017-09-30 19:33:23 -0700219}
220
Lev Walkincb523912017-09-30 19:33:23 -0700221asn_compile() {
Lev Walkin39936ce2017-10-05 23:56:39 -0700222 asn="$1"
223 where="$2"
Lev Walkind3cce462017-10-01 13:43:36 -0700224
Lev Walkin5d947a82017-10-03 01:04:03 -0700225 # Create "INTEGER (1..2)" from "T ::= INTEGER (1..2) -- RMAX=5"
Lev Walkinebeb4012017-10-09 19:51:26 -0700226 short_asn=`echo "$asn" | sed -e 's/ *--.*//;s/RMAX=[0-9]//;'`
Lev Walkin39936ce2017-10-05 23:56:39 -0700227 if [ `echo "$short_asn" | grep -c "::="` = 1 ]; then
228 short_asn=`echo "$short_asn" | sed -e 's/.*::= *//'`
Lev Walkind3cce462017-10-01 13:43:36 -0700229 fi
230
Lev Walkincb523912017-09-30 19:33:23 -0700231 test ! -f Makefile.am # Protection from accidental clobbering
Lev Walkinac6db172017-10-10 00:31:06 -0700232 {
233 echo "Test DEFINITIONS ::= BEGIN $asn"
234 echo "-- ${where}"
235 echo "END"
236 } > test.asn1
237 echo "${abs_top_builddir}/asn1c/asn1c -S ${abs_top_srcdir}/skeletons"
Lev Walkin39936ce2017-10-05 23:56:39 -0700238 if "${abs_top_builddir}/asn1c/asn1c" -S "${abs_top_srcdir}/skeletons" \
Lev Walkin7c470c72017-10-08 04:01:45 -0700239 -gen-OER -gen-PER ${asn1c_flags} test.asn1
Lev Walkina5b02882017-10-01 22:48:44 -0700240 then
Lev Walkin39936ce2017-10-05 23:56:39 -0700241 echo "ASN.1 compiled OK"
242 else
Lev Walkina5b02882017-10-01 22:48:44 -0700243 return 1
244 fi
Lev Walkincb523912017-09-30 19:33:23 -0700245 rm -f converter-example.c
Lev Walkin09be69c2017-10-08 16:28:11 -0700246 ln -sf "../${srcdir}/random-test-driver.c" || cp "../${srcdir}/random-test-driver.c" .
Lev Walkin40b8a7a2017-10-08 22:36:29 -0700247 {
248 echo "CFLAGS+= -DASN1_TEXT='$short_asn'";
249 echo "ASN_PROGRAM = random-test-driver"
250 echo "ASN_PROGRAM_SOURCES = random-test-driver.c"
Lev Walkinac6db172017-10-10 00:31:06 -0700251 echo
Lev Walkin40b8a7a2017-10-08 22:36:29 -0700252 echo "include Makefile.am.example"
Lev Walkinac6db172017-10-10 00:31:06 -0700253 echo
254 echo "all-tests-succeeded: ${abs_top_builddir}/asn1c/asn1c \$(ASN_PROGRAM_SOURCES) \$(ASN_MODULE_SOURCES) \$(ASN_MODULE_HEADERS)"
255 echo " @rm -f \$@"
256 echo " @echo Previous try did not go correctly. To reproduce:"
257 echo " @cat .test-reproduce"
258 echo " @exit 1"
259 echo
Lev Walkin40b8a7a2017-10-08 22:36:29 -0700260 } > Makefile
Lev Walkincb523912017-09-30 19:33:23 -0700261 echo "Makefile.am.example -> Makefile"
262}
263
Lev Walkin7c470c72017-10-08 04:01:45 -0700264# Make up to four different passes:
265# CFLAGS: | asn1c_flags:
266# -m64 | -fnative-types
267# -m32 | -fnative-types
268# -m64 | -fwide-types
269# -m32 | -fwide-types
270# *) Of course, -m64 and -fnative-types are just implied.
271test_drive() {
272 func="$1"
273 shift
274
275 if [ "x${asn1c_flags}" = "x" ] ; then
276 # Test for native types and wide types
277 asn1c_flags=" " test_drive "${func}" "$@"
278 asn1c_flags="-fnative-types" test_drive "${func}" "$@"
279 return 0
280 fi
281
Lev Walkinac6db172017-10-10 00:31:06 -0700282 # Can't reuse object code.
283 rm -rf ${RNDTEMP}
284
Lev Walkin7c470c72017-10-08 04:01:45 -0700285 echo "MODE: default"
286 # Default (likely 64-bit) mode
287 ${func} "$@"
288
289 # 32-bit mode, if available
290 if echo "${CFLAGS_M32}" | grep -i '[a-z]' > /dev/null ; then
291 echo "MODE: 32-bit"
Lev Walkinac6db172017-10-10 00:31:06 -0700292
293 # Can't reuse object code between modes.
294 rm -rf ${RNDTEMP}
295
Lev Walkin7c470c72017-10-08 04:01:45 -0700296 # -m32 doesn't support fuzzing (no such library), so we remove fuzzer.
297 # -m32 doesn't support leak sanitizing (it hangs), so we remove
Lev Walkinac6db172017-10-10 00:31:06 -0700298 # ASAN_ENV_FLAGS which enable leak check in runtime.
Lev Walkin7c470c72017-10-08 04:01:45 -0700299 CFLAGS="${CFLAGS} ${CFLAGS_M32}" CFLAGS_M32="" \
300 LIBFUZZER_CFLAGS="" ASAN_ENV_FLAGS="" \
301 ${func} "$@"
302 fi
303}
304
Lev Walkin40b8a7a2017-10-08 22:36:29 -0700305if echo "$*" | grep ' -- ' > /dev/null; then
306 TEST_DRIVER=`echo "$*" | sed -e 's/ -- .*/ -- /g'`
307 args=`echo "$*" | sed -e 's/.* //g'`
308 set "${args}"
309else
310 TEST_DRIVER=""
311fi
312
Lev Walkincb523912017-09-30 19:33:23 -0700313# Command line parsing
Lev Walkin68619bf2017-10-03 18:40:36 -0700314while :; do
315 case "$1" in
316 -h) usage ;;
Lev Walkind28b45f2017-10-05 22:09:19 -0700317 --asn1c) asn1c_flags="${asn1c_flags} $2"; shift 2; continue ;;
Lev Walkin40b8a7a2017-10-08 22:36:29 -0700318 --bundle)
319 shift
Lev Walkinac6db172017-10-10 00:31:06 -0700320
321 # Look for the transcript in bundles/NN-*-bundles.txt.log
322 set -x
323
Lev Walkin40b8a7a2017-10-08 22:36:29 -0700324 base=`basename "$1" | sed -e 's/.txt$//'`
325 RNDTEMP=".tmp.${base}"
Lev Walkinac6db172017-10-10 00:31:06 -0700326
327 if Make -C "${RNDTEMP}" all-tests-succeeded >/dev/null 2>&1 ; then
328 echo "Test succeeded before. Not rechecking."
329 tests_succeeded=1
330 break
331 fi
332
Lev Walkin40b8a7a2017-10-08 22:36:29 -0700333 test_drive verify_asn_types_in_file "$@"
Lev Walkinac6db172017-10-10 00:31:06 -0700334
335 touch "${RNDTEMP}/all-tests-succeeded"
336
Lev Walkin40b8a7a2017-10-08 22:36:29 -0700337 break
338 ;;
Lev Walkin68619bf2017-10-03 18:40:36 -0700339 --dirty)
340 need_clean_before_bundle=0
341 need_clean_before_test=0
342 shift
343 continue
344 ;;
Lev Walkind28b45f2017-10-05 22:09:19 -0700345 -e) encodings="${encodings} -e $2"; shift 2; continue;;
Lev Walkin68619bf2017-10-03 18:40:36 -0700346 -j) parallelism="$1"; shift 2; continue;;
347 -t)
Lev Walkin7c470c72017-10-08 04:01:45 -0700348 test_drive verify_asn_type "$2" "(command line)" || exit 1 ;;
Lev Walkin68619bf2017-10-03 18:40:36 -0700349 "")
Lev Walkin09be69c2017-10-08 16:28:11 -0700350 for bundle in `ls -1 ${srcdir}/bundles/*.txt | sort -nr`; do
Lev Walkin7c470c72017-10-08 04:01:45 -0700351 test_drive verify_asn_types_in_file "$bundle"
Lev Walkin68619bf2017-10-03 18:40:36 -0700352 done
353 ;;
354 *)
Lev Walkin40b8a7a2017-10-08 22:36:29 -0700355 exec ${TEST_DRIVER} $0 --bundle "$@"
Lev Walkin68619bf2017-10-03 18:40:36 -0700356 ;;
357 esac
358 break
359done
Lev Walkincb523912017-09-30 19:33:23 -0700360
Lev Walkind28b45f2017-10-05 22:09:19 -0700361if [ "$tests_succeeded" != "0" ] && [ "$tests_failed" = "0" ]; then
Lev Walkincb523912017-09-30 19:33:23 -0700362 echo "OK $tests_succeeded tests"
363else
364 echo "FAILED $tests_failed tests, OK $tests_succeeded tests"
365 exit 1
366fi