blob: 48440a55100a02d02ac74f1b41d76c0b830e8ced [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 Walkind3cce462017-10-01 13:43:36 -070029RNDTEMP=.tmp.random
30
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
41parallelism=4
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 Walkin226d1072017-10-08 17:37:11 -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 Walkin791d3b72017-10-02 16:24:28 -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"
113 text="$3"
114
115
Lev Walkin7c470c72017-10-08 04:01:45 -0700116 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}\";}"
Lev Walkin39936ce2017-10-05 23:56:39 -0700117}
118
Lev Walkin7c470c72017-10-08 04:01:45 -0700119# compile_and_test "<text>" "<where found>"
120compile_and_test() {
Lev Walkin39936ce2017-10-05 23:56:39 -0700121 asn="$1"
122 where="$2"
Lev Walkin43292722017-10-05 00:33:32 -0700123
124 make_clean_before_test
125
Lev Walkin7c470c72017-10-08 04:01:45 -0700126 asn_compile "$asn" "$where"
Lev Walkin39936ce2017-10-05 23:56:39 -0700127 if [ $? -ne 0 ]; then
Lev Walkincb523912017-09-30 19:33:23 -0700128 echo "Cannot compile ASN.1 $asn"
129 return 1
130 fi
131
132 rm -f random-test-driver.o
133 rm -f random-test-driver
Lev Walkin60d62672017-10-08 03:16:10 -0700134 CFLAGS="${CFLAGS}" Make
Lev Walkin39936ce2017-10-05 23:56:39 -0700135 if [ $? -ne 0 ] ; then
Lev Walkind3cce462017-10-01 13:43:36 -0700136 echo "Cannot compile C for $asn in ${RNDTEMP}"
Lev Walkincb523912017-09-30 19:33:23 -0700137 return 2
138 fi
139
Lev Walkin791d3b72017-10-02 16:24:28 -0700140 # Maximum size of the random data
Lev Walkin39936ce2017-10-05 23:56:39 -0700141 rmax=`get_param RMAX 128 "$asn"`
Lev Walkin791d3b72017-10-02 16:24:28 -0700142 if [ "0${rmax}" -lt 1 ]; then rmax=128; fi
143
Lev Walkincb523912017-09-30 19:33:23 -0700144 echo "Checking random data encode-decode"
Lev Walkin39936ce2017-10-05 23:56:39 -0700145 round_trip_check_cmd="${ASAN_ENV_FLAGS} ./random-test-driver -s ${rmax} ${encodings} -c"
146 if eval "$round_trip_check_cmd"; then
147 echo "Random test OK"
148 else
Lev Walkincec390d2017-10-03 16:36:17 -0700149 if [ "x$CC" = "x" ]; then CCSTR=""; else CCSTR="CC=${CC} "; fi
Lev Walkind3cce462017-10-01 13:43:36 -0700150 echo "RETRY:"
Lev Walkin39936ce2017-10-05 23:56:39 -0700151 echo "(cd ${RNDTEMP} && ${CCSTR}CFLAGS=\"${LIBFUZZER_CFLAGS} ${CFLAGS}\" ${MAKE:-make} && ${ASAN_ENV_FLAGS} ./random-test-driver -s ${rmax} ${encodings} -c)"
Lev Walkincb523912017-09-30 19:33:23 -0700152 return 3
153 fi
154
Lev Walkind3cce462017-10-01 13:43:36 -0700155 echo "Generating new random data"
156 rm -rf random-data
157 cmd="${ASAN_ENV_FLAGS} UBSAN_OPTIONS=print_stacktrace=1"
Lev Walkind28b45f2017-10-05 22:09:19 -0700158 cmd="${cmd} ./random-test-driver -s ${rmax} ${encodings} -g random-data"
Lev Walkin39936ce2017-10-05 23:56:39 -0700159 if eval "$cmd" ; then
160 echo "Random data generated OK"
161 else
Lev Walkind3cce462017-10-01 13:43:36 -0700162 echo "RETRY:"
163 echo "(cd ${RNDTEMP} && $cmd)"
164 return 4
165 fi
166
Lev Walkin233d14d2017-10-02 01:09:01 -0700167 # Do a LibFuzzer based testing
Lev Walkin39936ce2017-10-05 23:56:39 -0700168 fuzz_time=10
169 fuzz_cmd="${ASAN_ENV_FLAGS} UBSAN_OPTIONS=print_stacktrace=1"
Lev Walkind28b45f2017-10-05 22:09:19 -0700170 fuzz_cmd="${fuzz_cmd} ./random-test-driver"
171 fuzz_cmd="${fuzz_cmd} -timeout=3 -max_total_time=${fuzz_time} -max_len=128"
Lev Walkin233d14d2017-10-02 01:09:01 -0700172
Lev Walkin39936ce2017-10-05 23:56:39 -0700173 if grep "^fuzz:" Makefile >/dev/null ; then
174 echo "No fuzzer defined, skipping fuzzing"
175 else
176 fuzz_targets=`echo random-data/* | sed -e 's/random-data./fuzz-/g'`
Lev Walkind28b45f2017-10-05 22:09:19 -0700177 {
178 echo "fuzz: $fuzz_targets"
179 echo "fuzz-%: random-data/% random-test-driver"
180 echo " ASN1_DATA_DIR=\$< ${fuzz_cmd} \$<"
181 } >> Makefile
Lev Walkin233d14d2017-10-02 01:09:01 -0700182 fi
183
Lev Walkincb523912017-09-30 19:33:23 -0700184 # If LIBFUZZER_CFLAGS are properly defined, do the fuzz test as well
Lev Walkin39936ce2017-10-05 23:56:39 -0700185 if echo "${LIBFUZZER_CFLAGS}" | grep -i "[a-z]" > /dev/null; then
Lev Walkincb523912017-09-30 19:33:23 -0700186
187 echo "Recompiling for fuzzing..."
188 rm -f random-test-driver.o
189 rm -f random-test-driver
Lev Walkin39936ce2017-10-05 23:56:39 -0700190 CFLAGS="${LIBFUZZER_CFLAGS} ${CFLAGS}" Make
191 if [ $? -ne 0 ]; then
192 echo "Recompile failed"
193 return 4
194 fi
Lev Walkincb523912017-09-30 19:33:23 -0700195
Lev Walkind28b45f2017-10-05 22:09:19 -0700196 echo "Fuzzing will take a multiple of $fuzz_time seconds..."
Lev Walkin39936ce2017-10-05 23:56:39 -0700197 Make fuzz
198 if [ $? -ne 0 ]; then
Lev Walkin233d14d2017-10-02 01:09:01 -0700199 echo "RETRY:"
Lev Walkin39936ce2017-10-05 23:56:39 -0700200 echo "(cd ${RNDTEMP} && CC=${CC} CFLAGS=\"${LIBFUZZER_CFLAGS} ${CFLAGS}\" ${MAKE:-make} fuzz)"
Lev Walkin233d14d2017-10-02 01:09:01 -0700201 return 5
202 fi
Lev Walkincb523912017-09-30 19:33:23 -0700203 fi
204
Lev Walkina5b02882017-10-01 22:48:44 -0700205 return 0
Lev Walkincb523912017-09-30 19:33:23 -0700206}
207
Lev Walkincb523912017-09-30 19:33:23 -0700208asn_compile() {
Lev Walkin39936ce2017-10-05 23:56:39 -0700209 asn="$1"
210 where="$2"
Lev Walkind3cce462017-10-01 13:43:36 -0700211
Lev Walkin5d947a82017-10-03 01:04:03 -0700212 # Create "INTEGER (1..2)" from "T ::= INTEGER (1..2) -- RMAX=5"
Lev Walkin39936ce2017-10-05 23:56:39 -0700213 short_asn=`echo "$asn" | sed -e 's/ *--.*//;s/RMAX=[^ ]* //;'`
214 if [ `echo "$short_asn" | grep -c "::="` = 1 ]; then
215 short_asn=`echo "$short_asn" | sed -e 's/.*::= *//'`
Lev Walkind3cce462017-10-01 13:43:36 -0700216 fi
217
Lev Walkincb523912017-09-30 19:33:23 -0700218 test ! -f Makefile.am # Protection from accidental clobbering
219 echo "Test DEFINITIONS ::= BEGIN $asn" > test.asn1
Lev Walkin43292722017-10-05 00:33:32 -0700220 echo "-- ${where}" >> test.asn1
Lev Walkincb523912017-09-30 19:33:23 -0700221 echo "END" >> test.asn1
Lev Walkin60d62672017-10-08 03:16:10 -0700222 echo "${abs_top_builddir}/asn1c/asn1c" -S "${abs_top_srcdir}/skeletons"
Lev Walkin39936ce2017-10-05 23:56:39 -0700223 if "${abs_top_builddir}/asn1c/asn1c" -S "${abs_top_srcdir}/skeletons" \
Lev Walkin7c470c72017-10-08 04:01:45 -0700224 -gen-OER -gen-PER ${asn1c_flags} test.asn1
Lev Walkina5b02882017-10-01 22:48:44 -0700225 then
Lev Walkin39936ce2017-10-05 23:56:39 -0700226 echo "ASN.1 compiled OK"
227 else
Lev Walkina5b02882017-10-01 22:48:44 -0700228 return 1
229 fi
Lev Walkincb523912017-09-30 19:33:23 -0700230 rm -f converter-example.c
Lev Walkin09be69c2017-10-08 16:28:11 -0700231 ln -sf "../${srcdir}/random-test-driver.c" || cp "../${srcdir}/random-test-driver.c" .
Lev Walkind3cce462017-10-01 13:43:36 -0700232 echo "CFLAGS+= -DASN1_TEXT='$short_asn'" > Makefile
Lev Walkincb523912017-09-30 19:33:23 -0700233 sed -e 's/converter-example/random-test-driver/' \
Lev Walkind3cce462017-10-01 13:43:36 -0700234 < Makefile.am.example >> Makefile
Lev Walkincb523912017-09-30 19:33:23 -0700235 echo "Makefile.am.example -> Makefile"
236}
237
Lev Walkin7c470c72017-10-08 04:01:45 -0700238# Make up to four different passes:
239# CFLAGS: | asn1c_flags:
240# -m64 | -fnative-types
241# -m32 | -fnative-types
242# -m64 | -fwide-types
243# -m32 | -fwide-types
244# *) Of course, -m64 and -fnative-types are just implied.
245test_drive() {
246 func="$1"
247 shift
248
249 if [ "x${asn1c_flags}" = "x" ] ; then
250 # Test for native types and wide types
251 asn1c_flags=" " test_drive "${func}" "$@"
252 asn1c_flags="-fnative-types" test_drive "${func}" "$@"
253 return 0
254 fi
255
256 echo "MODE: default"
257 # Default (likely 64-bit) mode
258 ${func} "$@"
259
260 # 32-bit mode, if available
261 if echo "${CFLAGS_M32}" | grep -i '[a-z]' > /dev/null ; then
262 echo "MODE: 32-bit"
263 # Unconditional clean, can't reuse object code.
264 (cd ${RNDTEMP} && Make clean)
265 # -m32 doesn't support fuzzing (no such library), so we remove fuzzer.
266 # -m32 doesn't support leak sanitizing (it hangs), so we remove
267 # ASAN_ENV_FLAGS which enable leak check.
268 CFLAGS="${CFLAGS} ${CFLAGS_M32}" CFLAGS_M32="" \
269 LIBFUZZER_CFLAGS="" ASAN_ENV_FLAGS="" \
270 ${func} "$@"
271 fi
272}
273
Lev Walkincb523912017-09-30 19:33:23 -0700274# Command line parsing
Lev Walkin68619bf2017-10-03 18:40:36 -0700275while :; do
276 case "$1" in
277 -h) usage ;;
Lev Walkind28b45f2017-10-05 22:09:19 -0700278 --asn1c) asn1c_flags="${asn1c_flags} $2"; shift 2; continue ;;
Lev Walkin68619bf2017-10-03 18:40:36 -0700279 --dirty)
280 need_clean_before_bundle=0
281 need_clean_before_test=0
282 shift
283 continue
284 ;;
Lev Walkind28b45f2017-10-05 22:09:19 -0700285 -e) encodings="${encodings} -e $2"; shift 2; continue;;
Lev Walkin68619bf2017-10-03 18:40:36 -0700286 -j) parallelism="$1"; shift 2; continue;;
287 -t)
288 parallelism=1 # Better for debuggability
Lev Walkin7c470c72017-10-08 04:01:45 -0700289 test_drive verify_asn_type "$2" "(command line)" || exit 1 ;;
Lev Walkin68619bf2017-10-03 18:40:36 -0700290 "")
Lev Walkin09be69c2017-10-08 16:28:11 -0700291 for bundle in `ls -1 ${srcdir}/bundles/*.txt | sort -nr`; do
Lev Walkin7c470c72017-10-08 04:01:45 -0700292 test_drive verify_asn_types_in_file "$bundle"
Lev Walkin68619bf2017-10-03 18:40:36 -0700293 done
294 ;;
295 *)
Lev Walkin7c470c72017-10-08 04:01:45 -0700296 test_drive verify_asn_types_in_file "$@"
Lev Walkin68619bf2017-10-03 18:40:36 -0700297 ;;
298 esac
299 break
300done
Lev Walkincb523912017-09-30 19:33:23 -0700301
Lev Walkind28b45f2017-10-05 22:09:19 -0700302if [ "$tests_succeeded" != "0" ] && [ "$tests_failed" = "0" ]; then
Lev Walkincb523912017-09-30 19:33:23 -0700303 echo "OK $tests_succeeded tests"
304else
305 echo "FAILED $tests_failed tests, OK $tests_succeeded tests"
306 exit 1
307fi