blob: f9e2b4dd2579ab46e27a67510c749efa0dea00c7 [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"
14 echo " $0 -t \"<ASN.1 definition for type T, in string form>\""
15 echo " $0 bundles/<bundle-name.txt> [<line>]"
16 echo "Examples:"
17 echo " $0 -t UTF8String"
18 echo " $0 -t \"T ::= INTEGER (0..1)\""
19 echo " $0 bundles/01-INTEGER-bundle.txt 3"
20 exit 1
21}
22
Lev Walkind3cce462017-10-01 13:43:36 -070023RNDTEMP=.tmp.random
24
25srcdir="${srcdir:-.}"
26abs_top_srcdir="${abs_top_srcdir:-$(pwd)/../../}"
27abs_top_builddir="${abs_top_builddir:-$(pwd)/../../}"
28
Lev Walkincb523912017-09-30 19:33:23 -070029tests_succeeded=0
30tests_failed=0
Lev Walkind3cce462017-10-01 13:43:36 -070031stop_after_failed=1 # We stop after 3 failures.
Lev Walkincb523912017-09-30 19:33:23 -070032
33# Get all the type-bearding lines in file and process them individually
34verify_asn_types_in_file() {
35 local filename="$1"
36 local need_line="$2"
37 test "x$filename" != "x" || usage
38 echo "Open [$filename]"
39 local line=0
40 while read asn; do
41 line=$((line+1))
42 if echo "$asn" | sed -e 's/--.*//;' | grep -vqi "[A-Z]"; then
43 # Ignore lines consisting of just comments.
44 continue;
45 fi
46 if [ "x$need_line" != "x" -a "$need_line" != "$line" ]; then
47 # We need a different line.
48 continue;
49 fi
50 verify_asn_type "$asn" "in $filename $line"
Lev Walkind3cce462017-10-01 13:43:36 -070051 if [ "${tests_failed}" = "${stop_after_failed}" ]; then
52 echo "STOP after ${tests_failed} failures, OK ${tests_succeeded}"
53 exit 1
54 fi
Lev Walkincb523912017-09-30 19:33:23 -070055 done < "$filename"
56}
57
58verify_asn_type() {
59 local asn="$1"
60 shift
61 local where="$*"
62 test "x$asn" != "x" || usage
63 if echo "$asn" | grep -qv "::="; then
64 asn="T ::= $asn"
65 fi
66 echo "Testing [$asn] ${where}"
67
Lev Walkind3cce462017-10-01 13:43:36 -070068 mkdir -p ${RNDTEMP}
69 if (cd ${RNDTEMP} && compile_and_test "$asn" "$@"); then
Lev Walkincb523912017-09-30 19:33:23 -070070 echo "OK [$asn] ${where}"
71 tests_succeeded=$((tests_succeeded+1))
72 else
73 tests_failed=$((tests_failed+1))
74 echo "FAIL [$asn] ${where}"
75 fi
76}
77
78compile_and_test() {
79 local asn="$1"
80 shift
81
82 if ! asn_compile "$asn" "$@"; then
83 echo "Cannot compile ASN.1 $asn"
84 return 1
85 fi
86
87 rm -f random-test-driver.o
88 rm -f random-test-driver
89 if ! make -j4; then
Lev Walkind3cce462017-10-01 13:43:36 -070090 echo "Cannot compile C for $asn in ${RNDTEMP}"
Lev Walkincb523912017-09-30 19:33:23 -070091 return 2
92 fi
93
94 echo "Checking random data encode-decode"
95 if ! eval ${ASAN_ENV_FLAGS} ./random-test-driver -c; then
Lev Walkind3cce462017-10-01 13:43:36 -070096 echo "RETRY:"
97 echo "(cd ${RNDTEMP} && CC=${CC} CFLAGS=\"${LIBFUZZER_CFLAGS} ${CFLAGS}\" make && ${ASAN_ENV_FLAGS} ./random-test-driver -c)"
Lev Walkincb523912017-09-30 19:33:23 -070098 return 3
99 fi
100
Lev Walkind3cce462017-10-01 13:43:36 -0700101 echo "Generating new random data"
102 rm -rf random-data
103 cmd="${ASAN_ENV_FLAGS} UBSAN_OPTIONS=print_stacktrace=1"
104 cmd+=" ./random-test-driver -g random-data"
105 if ! eval "$cmd" ; then
106 echo "RETRY:"
107 echo "(cd ${RNDTEMP} && $cmd)"
108 return 4
109 fi
110
Lev Walkincb523912017-09-30 19:33:23 -0700111 # If LIBFUZZER_CFLAGS are properly defined, do the fuzz test as well
112 if echo "${LIBFUZZER_CFLAGS}" | grep -qi "[a-z]"; then
Lev Walkincb523912017-09-30 19:33:23 -0700113
114 echo "Recompiling for fuzzing..."
115 rm -f random-test-driver.o
116 rm -f random-test-driver
117 CFLAGS="${LIBFUZZER_CFLAGS} ${CFLAGS}" make
118
119 # Do a LibFuzzer based testing
120 fuzz_time=10
Lev Walkind3cce462017-10-01 13:43:36 -0700121 for data_dir in random-data/*; do
122 echo "Fuzzing $data_dir will take $fuzz_time seconds..."
123 local cmd="${ASAN_ENV_FLAGS} UBSAN_OPTIONS=print_stacktrace=1"
124 cmd+=" ASN1_DATA_DIR=$data_dir" # Dir for our code
125 cmd+=" ./random-test-driver"
126 cmd+=" -timeout=3 -max_total_time=${fuzz_time} -max_len=128"
127 cmd+=" $data_dir" # Dir for LLVM fuzzer driver
128 echo "$cmd"
129 if ! eval "$cmd" ; then
130 echo "RETRY $data_dir:"
131 echo "(cd ${RNDTEMP} && CC=${CC} CFLAGS=\"${LIBFUZZER_CFLAGS} ${CFLAGS}\" make && $cmd)"
132 return 5
133 fi
134 done
Lev Walkincb523912017-09-30 19:33:23 -0700135 fi
136
137 return 0;
138}
139
Lev Walkincb523912017-09-30 19:33:23 -0700140asn_compile() {
141 local asn="$1"
142 shift
Lev Walkind3cce462017-10-01 13:43:36 -0700143
144 # Create "INTEGER (1..2)" from "T ::= INTEGER (1..2) -- Comment"
145 local short_asn=$(echo "$asn" | sed -e 's/ *--.*//')
146 if [ $(echo "$short_asn" | grep -c "::=") = 1 ]; then
147 short_asn=$(echo "$short_asn" | sed -e 's/.*::= *//')
148 fi
149
Lev Walkincb523912017-09-30 19:33:23 -0700150 test ! -f Makefile.am # Protection from accidental clobbering
151 echo "Test DEFINITIONS ::= BEGIN $asn" > test.asn1
152 echo "END" >> test.asn1
153 ${abs_top_builddir}/asn1c/asn1c -S ${abs_top_srcdir}/skeletons \
154 -gen-OER -gen-PER test.asn1
155 rm -f converter-example.c
156 ln -sf ../random-test-driver.c || cp ../random-test-driver.c .
Lev Walkind3cce462017-10-01 13:43:36 -0700157 echo "CFLAGS+= -DASN1_TEXT='$short_asn'" > Makefile
Lev Walkincb523912017-09-30 19:33:23 -0700158 sed -e 's/converter-example/random-test-driver/' \
Lev Walkind3cce462017-10-01 13:43:36 -0700159 < Makefile.am.example >> Makefile
Lev Walkincb523912017-09-30 19:33:23 -0700160 echo "Makefile.am.example -> Makefile"
161}
162
163# Command line parsing
164case "$1" in
165 -h) usage ;;
166 -t) verify_asn_type "$2" || exit 1;;
167 "")
168 for bundle in bundles/*txt; do
169 verify_asn_types_in_file "$bundle"
170 done
171 ;;
172 *)
173 verify_asn_types_in_file "$@"
174 ;;
175esac
176
177if [ "$tests_succeeded" != "0" -a "$tests_failed" = "0" ]; then
178 echo "OK $tests_succeeded tests"
179else
180 echo "FAILED $tests_failed tests, OK $tests_succeeded tests"
181 exit 1
182fi