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" |
Lev Walkin | 93f372b | 2017-10-03 16:39:38 -0700 | [diff] [blame] | 14 | echo " $0 [--dirty] -t \"<ASN.1 text defining type T, in string form>\"" |
| 15 | echo " $0 [--dirty] bundles/<bundle-name.txt> [<line>]" |
Lev Walkin | 8cec523 | 2017-10-03 15:05:54 -0700 | [diff] [blame] | 16 | echo "Where options are:" |
Lev Walkin | 4329272 | 2017-10-05 00:33:32 -0700 | [diff] [blame] | 17 | 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 Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 22 | 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 Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 29 | RNDTEMP=.tmp.random |
| 30 | |
| 31 | srcdir="${srcdir:-.}" |
| 32 | abs_top_srcdir="${abs_top_srcdir:-$(pwd)/../../}" |
| 33 | abs_top_builddir="${abs_top_builddir:-$(pwd)/../../}" |
| 34 | |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 35 | tests_succeeded=0 |
| 36 | tests_failed=0 |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 37 | stop_after_failed=1 # We stop after 3 failures. |
Lev Walkin | 8cec523 | 2017-10-03 15:05:54 -0700 | [diff] [blame] | 38 | need_clean_before_bundle=1 # Clean before testing a bundle file |
| 39 | need_clean_before_test=1 # Before each line in a bundle file |
Lev Walkin | 68619bf | 2017-10-03 18:40:36 -0700 | [diff] [blame] | 40 | encodings="" # Default is to verify all supported ASN.1 transfer syntaxes |
| 41 | parallelism=4 |
Lev Walkin | 4329272 | 2017-10-05 00:33:32 -0700 | [diff] [blame] | 42 | asn1c_flags="" |
Lev Walkin | 8cec523 | 2017-10-03 15:05:54 -0700 | [diff] [blame] | 43 | |
| 44 | make_clean_before_bundle() { |
Lev Walkin | 945958e | 2017-10-05 01:09:23 -0700 | [diff] [blame] | 45 | test "${need_clean_before_bundle}" = "1" && rm -rf ${RNDTEMP} |
Lev Walkin | 8cec523 | 2017-10-03 15:05:54 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | make_clean_before_test() { |
Lev Walkin | 945958e | 2017-10-05 01:09:23 -0700 | [diff] [blame] | 49 | test "${need_clean_before_test}" = "1" && rm -rf ${RNDTEMP} |
Lev Walkin | 8cec523 | 2017-10-03 15:05:54 -0700 | [diff] [blame] | 50 | } |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 51 | |
| 52 | # Get all the type-bearding lines in file and process them individually |
| 53 | verify_asn_types_in_file() { |
| 54 | local filename="$1" |
| 55 | local need_line="$2" |
| 56 | test "x$filename" != "x" || usage |
Lev Walkin | 8cec523 | 2017-10-03 15:05:54 -0700 | [diff] [blame] | 57 | |
| 58 | make_clean_before_bundle |
| 59 | |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 60 | echo "Open [$filename]" |
| 61 | local line=0 |
Lev Walkin | d28b45f | 2017-10-05 22:09:19 -0700 | [diff] [blame^] | 62 | while read -r asn; do |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 63 | line=$((line+1)) |
| 64 | if echo "$asn" | sed -e 's/--.*//;' | grep -vqi "[A-Z]"; then |
| 65 | # Ignore lines consisting of just comments. |
| 66 | continue; |
| 67 | fi |
Lev Walkin | d28b45f | 2017-10-05 22:09:19 -0700 | [diff] [blame^] | 68 | if [ "x$need_line" != "x" ] && [ "$need_line" != "$line" ]; then |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 69 | # We need a different line. |
| 70 | continue; |
| 71 | fi |
| 72 | verify_asn_type "$asn" "in $filename $line" |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 73 | if [ "${tests_failed}" = "${stop_after_failed}" ]; then |
| 74 | echo "STOP after ${tests_failed} failures, OK ${tests_succeeded}" |
| 75 | exit 1 |
| 76 | fi |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 77 | done < "$filename" |
| 78 | } |
| 79 | |
| 80 | verify_asn_type() { |
| 81 | local asn="$1" |
| 82 | shift |
| 83 | local where="$*" |
| 84 | test "x$asn" != "x" || usage |
Lev Walkin | 8cec523 | 2017-10-03 15:05:54 -0700 | [diff] [blame] | 85 | |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 86 | if echo "$asn" | grep -qv "::="; then |
| 87 | asn="T ::= $asn" |
| 88 | fi |
| 89 | echo "Testing [$asn] ${where}" |
| 90 | |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 91 | mkdir -p ${RNDTEMP} |
Lev Walkin | 791d3b7 | 2017-10-02 16:24:28 -0700 | [diff] [blame] | 92 | if (set -e && cd ${RNDTEMP} && compile_and_test "$asn" "${where}"); then |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 93 | echo "OK [$asn] ${where}" |
| 94 | tests_succeeded=$((tests_succeeded+1)) |
| 95 | else |
| 96 | tests_failed=$((tests_failed+1)) |
| 97 | echo "FAIL [$asn] ${where}" |
| 98 | fi |
| 99 | } |
| 100 | |
Lev Walkin | 4329272 | 2017-10-05 00:33:32 -0700 | [diff] [blame] | 101 | # compile_and_test "<text>" "<where found>" [<asn.1 flags>] |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 102 | compile_and_test() { |
Lev Walkin | 4329272 | 2017-10-05 00:33:32 -0700 | [diff] [blame] | 103 | if [ "x${asn1c_flags}" != "x" ]; then |
| 104 | if ! compile_and_test_with "$@" ${asn1c_flags} ; then |
| 105 | return 1 |
| 106 | fi |
| 107 | else |
| 108 | if ! compile_and_test_with "$@" ; then |
| 109 | echo "Can't compile and test narrow" |
| 110 | return 1 |
| 111 | fi |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 112 | |
Lev Walkin | 4329272 | 2017-10-05 00:33:32 -0700 | [diff] [blame] | 113 | if ! compile_and_test_with "$@" "-fwide-types"; then |
| 114 | echo "Can't compile and test wide" |
| 115 | return 2 |
| 116 | fi |
| 117 | fi |
| 118 | |
| 119 | return 0 |
| 120 | } |
| 121 | |
| 122 | compile_and_test_with() { |
| 123 | local asn="$1" |
| 124 | local where="$2" |
| 125 | shift 2 |
| 126 | |
| 127 | make_clean_before_test |
| 128 | |
| 129 | if ! asn_compile "$asn" "$where" "$@"; then |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 130 | echo "Cannot compile ASN.1 $asn" |
| 131 | return 1 |
| 132 | fi |
| 133 | |
| 134 | rm -f random-test-driver.o |
| 135 | rm -f random-test-driver |
Lev Walkin | 68619bf | 2017-10-03 18:40:36 -0700 | [diff] [blame] | 136 | if ! make -j${parallelism}; then |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 137 | echo "Cannot compile C for $asn in ${RNDTEMP}" |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 138 | return 2 |
| 139 | fi |
| 140 | |
Lev Walkin | 791d3b7 | 2017-10-02 16:24:28 -0700 | [diff] [blame] | 141 | # Maximum size of the random data |
| 142 | local rmax=$(echo "$asn" | sed -Ee '/RMAX/!d;s/.*RMAX=([0-9]+).*/\1/') |
| 143 | if [ "0${rmax}" -lt 1 ]; then rmax=128; fi |
| 144 | |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 145 | echo "Checking random data encode-decode" |
Lev Walkin | d28b45f | 2017-10-05 22:09:19 -0700 | [diff] [blame^] | 146 | local round_trip_check_cmd="${ASAN_ENV_FLAGS} ./random-test-driver -s ${rmax} ${encodings} -c" |
| 147 | if ! eval "$round_trip_check_cmd"; then |
Lev Walkin | cec390d | 2017-10-03 16:36:17 -0700 | [diff] [blame] | 148 | if [ "x$CC" = "x" ]; then CCSTR=""; else CCSTR="CC=${CC} "; fi |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 149 | echo "RETRY:" |
Lev Walkin | 68619bf | 2017-10-03 18:40:36 -0700 | [diff] [blame] | 150 | echo "(cd ${RNDTEMP} && ${CCSTR}CFLAGS=\"${LIBFUZZER_CFLAGS} ${CFLAGS}\" make && ${ASAN_ENV_FLAGS} ./random-test-driver -s ${rmax} ${encodings} -c)" |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 151 | return 3 |
| 152 | fi |
| 153 | |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 154 | echo "Generating new random data" |
| 155 | rm -rf random-data |
| 156 | cmd="${ASAN_ENV_FLAGS} UBSAN_OPTIONS=print_stacktrace=1" |
Lev Walkin | d28b45f | 2017-10-05 22:09:19 -0700 | [diff] [blame^] | 157 | cmd="${cmd} ./random-test-driver -s ${rmax} ${encodings} -g random-data" |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 158 | if ! eval "$cmd" ; then |
| 159 | echo "RETRY:" |
| 160 | echo "(cd ${RNDTEMP} && $cmd)" |
| 161 | return 4 |
| 162 | fi |
| 163 | |
Lev Walkin | 233d14d | 2017-10-02 01:09:01 -0700 | [diff] [blame] | 164 | # Do a LibFuzzer based testing |
| 165 | local fuzz_time=10 |
| 166 | local fuzz_cmd="${ASAN_ENV_FLAGS} UBSAN_OPTIONS=print_stacktrace=1" |
Lev Walkin | d28b45f | 2017-10-05 22:09:19 -0700 | [diff] [blame^] | 167 | fuzz_cmd="${fuzz_cmd} ./random-test-driver" |
| 168 | fuzz_cmd="${fuzz_cmd} -timeout=3 -max_total_time=${fuzz_time} -max_len=128" |
Lev Walkin | 233d14d | 2017-10-02 01:09:01 -0700 | [diff] [blame] | 169 | |
| 170 | if ! grep -q "^fuzz:" Makefile ; then |
| 171 | local fuzz_targets=$(echo random-data/* | sed -e 's/random-data./fuzz-/g') |
Lev Walkin | d28b45f | 2017-10-05 22:09:19 -0700 | [diff] [blame^] | 172 | { |
| 173 | echo "fuzz: $fuzz_targets" |
| 174 | echo "fuzz-%: random-data/% random-test-driver" |
| 175 | echo " ASN1_DATA_DIR=\$< ${fuzz_cmd} \$<" |
| 176 | } >> Makefile |
Lev Walkin | 233d14d | 2017-10-02 01:09:01 -0700 | [diff] [blame] | 177 | fi |
| 178 | |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 179 | # If LIBFUZZER_CFLAGS are properly defined, do the fuzz test as well |
| 180 | if echo "${LIBFUZZER_CFLAGS}" | grep -qi "[a-z]"; then |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 181 | |
| 182 | echo "Recompiling for fuzzing..." |
| 183 | rm -f random-test-driver.o |
| 184 | rm -f random-test-driver |
Lev Walkin | 68619bf | 2017-10-03 18:40:36 -0700 | [diff] [blame] | 185 | CFLAGS="${LIBFUZZER_CFLAGS} ${CFLAGS}" make -j${parallelism} |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 186 | |
Lev Walkin | d28b45f | 2017-10-05 22:09:19 -0700 | [diff] [blame^] | 187 | echo "Fuzzing will take a multiple of $fuzz_time seconds..." |
Lev Walkin | 68619bf | 2017-10-03 18:40:36 -0700 | [diff] [blame] | 188 | if ! make -j${parallelism} fuzz ; then |
Lev Walkin | 233d14d | 2017-10-02 01:09:01 -0700 | [diff] [blame] | 189 | echo "RETRY:" |
| 190 | echo "(cd ${RNDTEMP} && CC=${CC} CFLAGS=\"${LIBFUZZER_CFLAGS} ${CFLAGS}\" make fuzz)" |
| 191 | return 5 |
| 192 | fi |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 193 | fi |
| 194 | |
Lev Walkin | a5b0288 | 2017-10-01 22:48:44 -0700 | [diff] [blame] | 195 | return 0 |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 198 | asn_compile() { |
| 199 | local asn="$1" |
Lev Walkin | 4329272 | 2017-10-05 00:33:32 -0700 | [diff] [blame] | 200 | local where="$2" |
| 201 | shift 2 |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 202 | |
Lev Walkin | 5d947a8 | 2017-10-03 01:04:03 -0700 | [diff] [blame] | 203 | # Create "INTEGER (1..2)" from "T ::= INTEGER (1..2) -- RMAX=5" |
Lev Walkin | 791d3b7 | 2017-10-02 16:24:28 -0700 | [diff] [blame] | 204 | local short_asn=$(echo "$asn" | sed -e 's/ *--.*//;s/RMAX=[^ ]* //;') |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 205 | if [ $(echo "$short_asn" | grep -c "::=") = 1 ]; then |
| 206 | short_asn=$(echo "$short_asn" | sed -e 's/.*::= *//') |
| 207 | fi |
| 208 | |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 209 | test ! -f Makefile.am # Protection from accidental clobbering |
| 210 | echo "Test DEFINITIONS ::= BEGIN $asn" > test.asn1 |
Lev Walkin | 4329272 | 2017-10-05 00:33:32 -0700 | [diff] [blame] | 211 | echo "-- ${where}" >> test.asn1 |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 212 | echo "END" >> test.asn1 |
Lev Walkin | d28b45f | 2017-10-05 22:09:19 -0700 | [diff] [blame^] | 213 | if ! "${abs_top_builddir}/asn1c/asn1c" -S "${abs_top_srcdir}/skeletons" \ |
Lev Walkin | 4329272 | 2017-10-05 00:33:32 -0700 | [diff] [blame] | 214 | -gen-OER -gen-PER "$@" test.asn1 |
Lev Walkin | a5b0288 | 2017-10-01 22:48:44 -0700 | [diff] [blame] | 215 | then |
| 216 | return 1 |
| 217 | fi |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 218 | rm -f converter-example.c |
| 219 | ln -sf ../random-test-driver.c || cp ../random-test-driver.c . |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 220 | echo "CFLAGS+= -DASN1_TEXT='$short_asn'" > Makefile |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 221 | sed -e 's/converter-example/random-test-driver/' \ |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 222 | < Makefile.am.example >> Makefile |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 223 | echo "Makefile.am.example -> Makefile" |
| 224 | } |
| 225 | |
| 226 | # Command line parsing |
Lev Walkin | 68619bf | 2017-10-03 18:40:36 -0700 | [diff] [blame] | 227 | while :; do |
| 228 | case "$1" in |
| 229 | -h) usage ;; |
Lev Walkin | d28b45f | 2017-10-05 22:09:19 -0700 | [diff] [blame^] | 230 | --asn1c) asn1c_flags="${asn1c_flags} $2"; shift 2; continue ;; |
Lev Walkin | 68619bf | 2017-10-03 18:40:36 -0700 | [diff] [blame] | 231 | --dirty) |
| 232 | need_clean_before_bundle=0 |
| 233 | need_clean_before_test=0 |
| 234 | shift |
| 235 | continue |
| 236 | ;; |
Lev Walkin | d28b45f | 2017-10-05 22:09:19 -0700 | [diff] [blame^] | 237 | -e) encodings="${encodings} -e $2"; shift 2; continue;; |
Lev Walkin | 68619bf | 2017-10-03 18:40:36 -0700 | [diff] [blame] | 238 | -j) parallelism="$1"; shift 2; continue;; |
| 239 | -t) |
| 240 | parallelism=1 # Better for debuggability |
| 241 | verify_asn_type "$2" || exit 1 ;; |
| 242 | "") |
| 243 | for bundle in $(echo bundles/*txt | sort -nr); do |
| 244 | verify_asn_types_in_file "$bundle" |
| 245 | done |
| 246 | ;; |
| 247 | *) |
| 248 | verify_asn_types_in_file "$@" |
| 249 | ;; |
| 250 | esac |
| 251 | break |
| 252 | done |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 253 | |
Lev Walkin | d28b45f | 2017-10-05 22:09:19 -0700 | [diff] [blame^] | 254 | if [ "$tests_succeeded" != "0" ] && [ "$tests_failed" = "0" ]; then |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 255 | echo "OK $tests_succeeded tests" |
| 256 | else |
| 257 | echo "FAILED $tests_failed tests, OK $tests_succeeded tests" |
| 258 | exit 1 |
| 259 | fi |