Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 1 | #!/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 | |
| 9 | set -e |
| 10 | |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 11 | usage() { |
| 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 Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 23 | RNDTEMP=.tmp.random |
| 24 | |
| 25 | srcdir="${srcdir:-.}" |
| 26 | abs_top_srcdir="${abs_top_srcdir:-$(pwd)/../../}" |
| 27 | abs_top_builddir="${abs_top_builddir:-$(pwd)/../../}" |
| 28 | |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 29 | tests_succeeded=0 |
| 30 | tests_failed=0 |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 31 | stop_after_failed=1 # We stop after 3 failures. |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 32 | |
| 33 | # Get all the type-bearding lines in file and process them individually |
| 34 | verify_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 Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 51 | if [ "${tests_failed}" = "${stop_after_failed}" ]; then |
| 52 | echo "STOP after ${tests_failed} failures, OK ${tests_succeeded}" |
| 53 | exit 1 |
| 54 | fi |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 55 | done < "$filename" |
| 56 | } |
| 57 | |
| 58 | verify_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 Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 68 | mkdir -p ${RNDTEMP} |
Lev Walkin | 791d3b7 | 2017-10-02 16:24:28 -0700 | [diff] [blame] | 69 | if (set -e && cd ${RNDTEMP} && compile_and_test "$asn" "${where}"); then |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 70 | 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 | |
| 78 | compile_and_test() { |
| 79 | local asn="$1" |
| 80 | shift |
| 81 | |
Lev Walkin | 791d3b7 | 2017-10-02 16:24:28 -0700 | [diff] [blame] | 82 | if ! asn_compile "$asn" "$*"; then |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 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 Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 90 | echo "Cannot compile C for $asn in ${RNDTEMP}" |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 91 | return 2 |
| 92 | fi |
| 93 | |
Lev Walkin | 791d3b7 | 2017-10-02 16:24:28 -0700 | [diff] [blame] | 94 | # Maximum size of the random data |
| 95 | local rmax=$(echo "$asn" | sed -Ee '/RMAX/!d;s/.*RMAX=([0-9]+).*/\1/') |
| 96 | if [ "0${rmax}" -lt 1 ]; then rmax=128; fi |
| 97 | |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 98 | echo "Checking random data encode-decode" |
Lev Walkin | 791d3b7 | 2017-10-02 16:24:28 -0700 | [diff] [blame] | 99 | if ! eval ${ASAN_ENV_FLAGS} ./random-test-driver -s ${rmax} -c; then |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 100 | echo "RETRY:" |
Lev Walkin | 791d3b7 | 2017-10-02 16:24:28 -0700 | [diff] [blame] | 101 | echo "(cd ${RNDTEMP} && CC=${CC} CFLAGS=\"${LIBFUZZER_CFLAGS} ${CFLAGS}\" make && ${ASAN_ENV_FLAGS} ./random-test-driver -s ${rmax} -c)" |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 102 | return 3 |
| 103 | fi |
| 104 | |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 105 | echo "Generating new random data" |
| 106 | rm -rf random-data |
| 107 | cmd="${ASAN_ENV_FLAGS} UBSAN_OPTIONS=print_stacktrace=1" |
Lev Walkin | 791d3b7 | 2017-10-02 16:24:28 -0700 | [diff] [blame] | 108 | cmd+=" ./random-test-driver -s ${rmax} -g random-data" |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 109 | if ! eval "$cmd" ; then |
| 110 | echo "RETRY:" |
| 111 | echo "(cd ${RNDTEMP} && $cmd)" |
| 112 | return 4 |
| 113 | fi |
| 114 | |
Lev Walkin | 233d14d | 2017-10-02 01:09:01 -0700 | [diff] [blame] | 115 | # Do a LibFuzzer based testing |
| 116 | local fuzz_time=10 |
| 117 | local fuzz_cmd="${ASAN_ENV_FLAGS} UBSAN_OPTIONS=print_stacktrace=1" |
| 118 | fuzz_cmd+=" ./random-test-driver" |
| 119 | fuzz_cmd+=" -timeout=3 -max_total_time=${fuzz_time} -max_len=128" |
| 120 | |
| 121 | if ! grep -q "^fuzz:" Makefile ; then |
| 122 | local fuzz_targets=$(echo random-data/* | sed -e 's/random-data./fuzz-/g') |
| 123 | echo "fuzz: $fuzz_targets" >> Makefile |
| 124 | echo "fuzz-%: random-data/% random-test-driver" >> Makefile |
| 125 | echo " ASN1_DATA_DIR=\$< ${fuzz_cmd} \$<" >> Makefile |
| 126 | fi |
| 127 | |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 128 | # If LIBFUZZER_CFLAGS are properly defined, do the fuzz test as well |
| 129 | if echo "${LIBFUZZER_CFLAGS}" | grep -qi "[a-z]"; then |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 130 | |
| 131 | echo "Recompiling for fuzzing..." |
| 132 | rm -f random-test-driver.o |
| 133 | rm -f random-test-driver |
Lev Walkin | 233d14d | 2017-10-02 01:09:01 -0700 | [diff] [blame] | 134 | CFLAGS="${LIBFUZZER_CFLAGS} ${CFLAGS}" make -j4 |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 135 | |
Lev Walkin | 233d14d | 2017-10-02 01:09:01 -0700 | [diff] [blame] | 136 | echo "Fuzzing $data_dir will take $fuzz_time seconds..." |
| 137 | if ! make -j4 fuzz ; then |
| 138 | echo "RETRY:" |
| 139 | echo "(cd ${RNDTEMP} && CC=${CC} CFLAGS=\"${LIBFUZZER_CFLAGS} ${CFLAGS}\" make fuzz)" |
| 140 | return 5 |
| 141 | fi |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 142 | fi |
| 143 | |
Lev Walkin | a5b0288 | 2017-10-01 22:48:44 -0700 | [diff] [blame] | 144 | return 0 |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 147 | asn_compile() { |
| 148 | local asn="$1" |
| 149 | shift |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 150 | |
Lev Walkin | 5d947a8 | 2017-10-03 01:04:03 -0700 | [diff] [blame^] | 151 | # Create "INTEGER (1..2)" from "T ::= INTEGER (1..2) -- RMAX=5" |
Lev Walkin | 791d3b7 | 2017-10-02 16:24:28 -0700 | [diff] [blame] | 152 | local short_asn=$(echo "$asn" | sed -e 's/ *--.*//;s/RMAX=[^ ]* //;') |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 153 | if [ $(echo "$short_asn" | grep -c "::=") = 1 ]; then |
| 154 | short_asn=$(echo "$short_asn" | sed -e 's/.*::= *//') |
| 155 | fi |
| 156 | |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 157 | test ! -f Makefile.am # Protection from accidental clobbering |
| 158 | echo "Test DEFINITIONS ::= BEGIN $asn" > test.asn1 |
Lev Walkin | 791d3b7 | 2017-10-02 16:24:28 -0700 | [diff] [blame] | 159 | echo "-- $*" >> test.asn1 |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 160 | echo "END" >> test.asn1 |
Lev Walkin | a5b0288 | 2017-10-01 22:48:44 -0700 | [diff] [blame] | 161 | if ! ${abs_top_builddir}/asn1c/asn1c -S ${abs_top_srcdir}/skeletons \ |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 162 | -gen-OER -gen-PER test.asn1 |
Lev Walkin | a5b0288 | 2017-10-01 22:48:44 -0700 | [diff] [blame] | 163 | then |
| 164 | return 1 |
| 165 | fi |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 166 | rm -f converter-example.c |
| 167 | ln -sf ../random-test-driver.c || cp ../random-test-driver.c . |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 168 | echo "CFLAGS+= -DASN1_TEXT='$short_asn'" > Makefile |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 169 | sed -e 's/converter-example/random-test-driver/' \ |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 170 | < Makefile.am.example >> Makefile |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 171 | echo "Makefile.am.example -> Makefile" |
| 172 | } |
| 173 | |
| 174 | # Command line parsing |
| 175 | case "$1" in |
| 176 | -h) usage ;; |
| 177 | -t) verify_asn_type "$2" || exit 1;; |
| 178 | "") |
| 179 | for bundle in bundles/*txt; do |
| 180 | verify_asn_types_in_file "$bundle" |
| 181 | done |
| 182 | ;; |
| 183 | *) |
| 184 | verify_asn_types_in_file "$@" |
| 185 | ;; |
| 186 | esac |
| 187 | |
| 188 | if [ "$tests_succeeded" != "0" -a "$tests_failed" = "0" ]; then |
| 189 | echo "OK $tests_succeeded tests" |
| 190 | else |
| 191 | echo "FAILED $tests_failed tests, OK $tests_succeeded tests" |
| 192 | exit 1 |
| 193 | fi |