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} |
| 69 | if (cd ${RNDTEMP} && compile_and_test "$asn" "$@"); 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 | |
| 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 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 | |
| 94 | echo "Checking random data encode-decode" |
| 95 | if ! eval ${ASAN_ENV_FLAGS} ./random-test-driver -c; then |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame^] | 96 | echo "RETRY:" |
| 97 | echo "(cd ${RNDTEMP} && CC=${CC} CFLAGS=\"${LIBFUZZER_CFLAGS} ${CFLAGS}\" make && ${ASAN_ENV_FLAGS} ./random-test-driver -c)" |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 98 | return 3 |
| 99 | fi |
| 100 | |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame^] | 101 | 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 Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 111 | # If LIBFUZZER_CFLAGS are properly defined, do the fuzz test as well |
| 112 | if echo "${LIBFUZZER_CFLAGS}" | grep -qi "[a-z]"; then |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 113 | |
| 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 Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame^] | 121 | 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 Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 135 | fi |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 140 | asn_compile() { |
| 141 | local asn="$1" |
| 142 | shift |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame^] | 143 | |
| 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 Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 150 | 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 Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame^] | 157 | echo "CFLAGS+= -DASN1_TEXT='$short_asn'" > Makefile |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 158 | sed -e 's/converter-example/random-test-driver/' \ |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame^] | 159 | < Makefile.am.example >> Makefile |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 160 | echo "Makefile.am.example -> Makefile" |
| 161 | } |
| 162 | |
| 163 | # Command line parsing |
| 164 | case "$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 | ;; |
| 175 | esac |
| 176 | |
| 177 | if [ "$tests_succeeded" != "0" -a "$tests_failed" = "0" ]; then |
| 178 | echo "OK $tests_succeeded tests" |
| 179 | else |
| 180 | echo "FAILED $tests_failed tests, OK $tests_succeeded tests" |
| 181 | exit 1 |
| 182 | fi |