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 | 40b8a7a | 2017-10-08 22:36:29 -0700 | [diff] [blame] | 29 | RNDTEMP="${RNDTEMP:-.tmp.random}" |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 30 | |
| 31 | srcdir="${srcdir:-.}" |
Lev Walkin | 39936ce | 2017-10-05 23:56:39 -0700 | [diff] [blame] | 32 | abs_top_srcdir="${abs_top_srcdir:-`pwd`/../../}" |
| 33 | abs_top_builddir="${abs_top_builddir:-`pwd`/../../}" |
Lev Walkin | d0d6392 | 2017-10-10 01:27:37 -0700 | [diff] [blame] | 34 | MAKE="${MAKE:-make}" |
| 35 | FUZZ_TIME="${FUZZ_TIME:-10}" |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 36 | |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 37 | tests_succeeded=0 |
| 38 | tests_failed=0 |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 39 | stop_after_failed=1 # We stop after 3 failures. |
Lev Walkin | 8cec523 | 2017-10-03 15:05:54 -0700 | [diff] [blame] | 40 | need_clean_before_bundle=1 # Clean before testing a bundle file |
Lev Walkin | 7c470c7 | 2017-10-08 04:01:45 -0700 | [diff] [blame] | 41 | need_clean_before_test=0 # Before each line in a bundle file |
Lev Walkin | 68619bf | 2017-10-03 18:40:36 -0700 | [diff] [blame] | 42 | encodings="" # Default is to verify all supported ASN.1 transfer syntaxes |
Lev Walkin | 40b8a7a | 2017-10-08 22:36:29 -0700 | [diff] [blame] | 43 | parallelism=1 |
Lev Walkin | 4329272 | 2017-10-05 00:33:32 -0700 | [diff] [blame] | 44 | asn1c_flags="" |
Lev Walkin | 8cec523 | 2017-10-03 15:05:54 -0700 | [diff] [blame] | 45 | |
| 46 | make_clean_before_bundle() { |
Lev Walkin | ab25f19 | 2017-10-08 12:50:43 -0700 | [diff] [blame] | 47 | if [ "${need_clean_before_bundle}" = "1" ] ; then |
Lev Walkin | ac6db17 | 2017-10-10 00:31:06 -0700 | [diff] [blame] | 48 | (cd "${RNDTEMP}" && Make clean) || : |
Lev Walkin | ab25f19 | 2017-10-08 12:50:43 -0700 | [diff] [blame] | 49 | fi |
Lev Walkin | 8cec523 | 2017-10-03 15:05:54 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | make_clean_before_test() { |
Lev Walkin | 60d6267 | 2017-10-08 03:16:10 -0700 | [diff] [blame] | 53 | if [ "${need_clean_before_test}" = "1" ] ; then |
| 54 | Make clean |
| 55 | fi |
Lev Walkin | 8cec523 | 2017-10-03 15:05:54 -0700 | [diff] [blame] | 56 | } |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 57 | |
| 58 | # Get all the type-bearding lines in file and process them individually |
| 59 | verify_asn_types_in_file() { |
Lev Walkin | 39936ce | 2017-10-05 23:56:39 -0700 | [diff] [blame] | 60 | filename="$1" |
| 61 | need_line="$2" |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 62 | test "x$filename" != "x" || usage |
Lev Walkin | 8cec523 | 2017-10-03 15:05:54 -0700 | [diff] [blame] | 63 | |
| 64 | make_clean_before_bundle |
| 65 | |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 66 | echo "Open [$filename]" |
Lev Walkin | 39936ce | 2017-10-05 23:56:39 -0700 | [diff] [blame] | 67 | line=0 |
| 68 | asn="" |
| 69 | while read asn; do |
Lev Walkin | 61b4be0 | 2017-10-18 16:37:51 -0700 | [diff] [blame] | 70 | line=`expr ${line} + 1` |
Lev Walkin | 39936ce | 2017-10-05 23:56:39 -0700 | [diff] [blame] | 71 | if echo "$asn" | sed -e 's/--.*//;' | grep -vi "[A-Z]" > /dev/null; then |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 72 | # Ignore lines consisting of just comments. |
| 73 | continue; |
| 74 | fi |
Lev Walkin | d28b45f | 2017-10-05 22:09:19 -0700 | [diff] [blame] | 75 | if [ "x$need_line" != "x" ] && [ "$need_line" != "$line" ]; then |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 76 | # We need a different line. |
| 77 | continue; |
| 78 | fi |
| 79 | verify_asn_type "$asn" "in $filename $line" |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 80 | if [ "${tests_failed}" = "${stop_after_failed}" ]; then |
| 81 | echo "STOP after ${tests_failed} failures, OK ${tests_succeeded}" |
| 82 | exit 1 |
| 83 | fi |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 84 | done < "$filename" |
| 85 | } |
| 86 | |
| 87 | verify_asn_type() { |
Lev Walkin | 39936ce | 2017-10-05 23:56:39 -0700 | [diff] [blame] | 88 | asn="$1" |
Lev Walkin | 7c470c7 | 2017-10-08 04:01:45 -0700 | [diff] [blame] | 89 | where="$2" |
| 90 | shift 2 |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 91 | test "x$asn" != "x" || usage |
Lev Walkin | 8cec523 | 2017-10-03 15:05:54 -0700 | [diff] [blame] | 92 | |
Lev Walkin | 39936ce | 2017-10-05 23:56:39 -0700 | [diff] [blame] | 93 | if echo "$asn" | grep -v "::=" > /dev/null; then |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 94 | asn="T ::= $asn" |
| 95 | fi |
| 96 | echo "Testing [$asn] ${where}" |
| 97 | |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 98 | mkdir -p ${RNDTEMP} |
Lev Walkin | ac6db17 | 2017-10-10 00:31:06 -0700 | [diff] [blame] | 99 | if (set -e && cd "${RNDTEMP}" && compile_and_test "$asn" "${where}"); then |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 100 | echo "OK [$asn] ${where}" |
Lev Walkin | 61b4be0 | 2017-10-18 16:37:51 -0700 | [diff] [blame] | 101 | tests_succeeded=`expr ${tests_succeeded} + 1` |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 102 | else |
Lev Walkin | 61b4be0 | 2017-10-18 16:37:51 -0700 | [diff] [blame] | 103 | tests_failed=`expr ${tests_failed} + 1` |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 104 | echo "FAIL [$asn] ${where}" |
| 105 | fi |
| 106 | } |
| 107 | |
Lev Walkin | 39936ce | 2017-10-05 23:56:39 -0700 | [diff] [blame] | 108 | Make() { |
Lev Walkin | d0d6392 | 2017-10-10 01:27:37 -0700 | [diff] [blame] | 109 | ${MAKE} -j "${parallelism}" "$@" || return $? |
Lev Walkin | 39936ce | 2017-10-05 23:56:39 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | get_param() { |
| 113 | param="$1" |
| 114 | default="$2" |
Lev Walkin | ebeb401 | 2017-10-09 19:51:26 -0700 | [diff] [blame] | 115 | asn="$3" |
Lev Walkin | 39936ce | 2017-10-05 23:56:39 -0700 | [diff] [blame] | 116 | |
Lev Walkin | ebeb401 | 2017-10-09 19:51:26 -0700 | [diff] [blame] | 117 | if nawk '' >/dev/null 2>&1 ; then |
| 118 | AWK=nawk |
| 119 | else |
| 120 | AWK=awk |
| 121 | fi |
Lev Walkin | 39936ce | 2017-10-05 23:56:39 -0700 | [diff] [blame] | 122 | |
Lev Walkin | ebeb401 | 2017-10-09 19:51:26 -0700 | [diff] [blame] | 123 | echo "$asn" | ${AWK} "BEGIN{FS=\"[^${param}=0-9]+\"};/$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 Walkin | 39936ce | 2017-10-05 23:56:39 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Lev Walkin | 7c470c7 | 2017-10-08 04:01:45 -0700 | [diff] [blame] | 126 | # compile_and_test "<text>" "<where found>" |
Lev Walkin | ac6db17 | 2017-10-10 00:31:06 -0700 | [diff] [blame] | 127 | # This function is executed in the temporary test directory ${RNDTEMP}. |
Lev Walkin | 7c470c7 | 2017-10-08 04:01:45 -0700 | [diff] [blame] | 128 | compile_and_test() { |
Lev Walkin | 39936ce | 2017-10-05 23:56:39 -0700 | [diff] [blame] | 129 | asn="$1" |
| 130 | where="$2" |
Lev Walkin | 4329272 | 2017-10-05 00:33:32 -0700 | [diff] [blame] | 131 | |
Lev Walkin | ac6db17 | 2017-10-10 00:31:06 -0700 | [diff] [blame] | 132 | if [ "x$CC" = "x" ]; then CCSTR=""; else CCSTR="CC=${CC} "; fi |
Lev Walkin | d0d6392 | 2017-10-10 01:27:37 -0700 | [diff] [blame] | 133 | reproduce_make="cd \"${RNDTEMP}\" && ${CCSTR}CFLAGS=\"${CFLAGS}\" ${MAKE}" |
Lev Walkin | ac6db17 | 2017-10-10 00:31:06 -0700 | [diff] [blame] | 134 | |
| 135 | env > .test-environment |
| 136 | set > .test-set |
| 137 | |
Lev Walkin | 4329272 | 2017-10-05 00:33:32 -0700 | [diff] [blame] | 138 | make_clean_before_test |
| 139 | |
Lev Walkin | 7c470c7 | 2017-10-08 04:01:45 -0700 | [diff] [blame] | 140 | asn_compile "$asn" "$where" |
Lev Walkin | 39936ce | 2017-10-05 23:56:39 -0700 | [diff] [blame] | 141 | if [ $? -ne 0 ]; then |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 142 | echo "Cannot compile ASN.1 $asn" |
| 143 | return 1 |
| 144 | fi |
| 145 | |
| 146 | rm -f random-test-driver.o |
| 147 | rm -f random-test-driver |
Lev Walkin | 60d6267 | 2017-10-08 03:16:10 -0700 | [diff] [blame] | 148 | CFLAGS="${CFLAGS}" Make |
Lev Walkin | 39936ce | 2017-10-05 23:56:39 -0700 | [diff] [blame] | 149 | if [ $? -ne 0 ] ; then |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 150 | echo "Cannot compile C for $asn in ${RNDTEMP}" |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 151 | return 2 |
| 152 | fi |
| 153 | |
Lev Walkin | 791d3b7 | 2017-10-02 16:24:28 -0700 | [diff] [blame] | 154 | # Maximum size of the random data |
Lev Walkin | 39936ce | 2017-10-05 23:56:39 -0700 | [diff] [blame] | 155 | rmax=`get_param RMAX 128 "$asn"` |
Lev Walkin | 791d3b7 | 2017-10-02 16:24:28 -0700 | [diff] [blame] | 156 | if [ "0${rmax}" -lt 1 ]; then rmax=128; fi |
| 157 | |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 158 | echo "Checking random data encode-decode" |
Lev Walkin | 39936ce | 2017-10-05 23:56:39 -0700 | [diff] [blame] | 159 | round_trip_check_cmd="${ASAN_ENV_FLAGS} ./random-test-driver -s ${rmax} ${encodings} -c" |
Lev Walkin | ac6db17 | 2017-10-10 00:31:06 -0700 | [diff] [blame] | 160 | echo "(${reproduce_make} && ${round_trip_check_cmd})" > .test-reproduce |
Lev Walkin | 39936ce | 2017-10-05 23:56:39 -0700 | [diff] [blame] | 161 | if eval "$round_trip_check_cmd"; then |
| 162 | echo "Random test OK" |
| 163 | else |
Lev Walkin | ac6db17 | 2017-10-10 00:31:06 -0700 | [diff] [blame] | 164 | { echo "RETRY:"; cat .test-reproduce ; } |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 165 | return 3 |
| 166 | fi |
| 167 | |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 168 | echo "Generating new random data" |
| 169 | rm -rf random-data |
| 170 | cmd="${ASAN_ENV_FLAGS} UBSAN_OPTIONS=print_stacktrace=1" |
Lev Walkin | d28b45f | 2017-10-05 22:09:19 -0700 | [diff] [blame] | 171 | cmd="${cmd} ./random-test-driver -s ${rmax} ${encodings} -g random-data" |
Lev Walkin | ac6db17 | 2017-10-10 00:31:06 -0700 | [diff] [blame] | 172 | echo "(${reproduce_make} && ${cmd})" > .test-reproduce |
Lev Walkin | 39936ce | 2017-10-05 23:56:39 -0700 | [diff] [blame] | 173 | if eval "$cmd" ; then |
| 174 | echo "Random data generated OK" |
| 175 | else |
Lev Walkin | ac6db17 | 2017-10-10 00:31:06 -0700 | [diff] [blame] | 176 | { echo "RETRY:"; cat .test-reproduce ; } |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 177 | return 4 |
| 178 | fi |
| 179 | |
Lev Walkin | 233d14d | 2017-10-02 01:09:01 -0700 | [diff] [blame] | 180 | # Do a LibFuzzer based testing |
Lev Walkin | 39936ce | 2017-10-05 23:56:39 -0700 | [diff] [blame] | 181 | fuzz_cmd="${ASAN_ENV_FLAGS} UBSAN_OPTIONS=print_stacktrace=1" |
Lev Walkin | d28b45f | 2017-10-05 22:09:19 -0700 | [diff] [blame] | 182 | fuzz_cmd="${fuzz_cmd} ./random-test-driver" |
Lev Walkin | d0d6392 | 2017-10-10 01:27:37 -0700 | [diff] [blame] | 183 | 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] | 184 | |
Lev Walkin | 39936ce | 2017-10-05 23:56:39 -0700 | [diff] [blame] | 185 | if grep "^fuzz:" Makefile >/dev/null ; then |
| 186 | echo "No fuzzer defined, skipping fuzzing" |
| 187 | else |
| 188 | fuzz_targets=`echo random-data/* | sed -e 's/random-data./fuzz-/g'` |
Lev Walkin | d28b45f | 2017-10-05 22:09:19 -0700 | [diff] [blame] | 189 | { |
| 190 | echo "fuzz: $fuzz_targets" |
| 191 | echo "fuzz-%: random-data/% random-test-driver" |
| 192 | echo " ASN1_DATA_DIR=\$< ${fuzz_cmd} \$<" |
| 193 | } >> Makefile |
Lev Walkin | 233d14d | 2017-10-02 01:09:01 -0700 | [diff] [blame] | 194 | fi |
| 195 | |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 196 | # If LIBFUZZER_CFLAGS are properly defined, do the fuzz test as well |
Lev Walkin | 39936ce | 2017-10-05 23:56:39 -0700 | [diff] [blame] | 197 | if echo "${LIBFUZZER_CFLAGS}" | grep -i "[a-z]" > /dev/null; then |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 198 | |
| 199 | echo "Recompiling for fuzzing..." |
| 200 | rm -f random-test-driver.o |
| 201 | rm -f random-test-driver |
Lev Walkin | d0d6392 | 2017-10-10 01:27:37 -0700 | [diff] [blame] | 202 | reproduce_make="cd \"${RNDTEMP}\" && ${CCSTR}CFLAGS=\"${LIBFUZZER_CFLAGS} ${CFLAGS}\" ${MAKE}" |
Lev Walkin | ac6db17 | 2017-10-10 00:31:06 -0700 | [diff] [blame] | 203 | echo "(${reproduce_make})" > .test-reproduce |
Lev Walkin | 39936ce | 2017-10-05 23:56:39 -0700 | [diff] [blame] | 204 | CFLAGS="${LIBFUZZER_CFLAGS} ${CFLAGS}" Make |
| 205 | if [ $? -ne 0 ]; then |
| 206 | echo "Recompile failed" |
| 207 | return 4 |
| 208 | fi |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 209 | |
Lev Walkin | d0d6392 | 2017-10-10 01:27:37 -0700 | [diff] [blame] | 210 | echo "Fuzzing will take a multiple of ${FUZZ_TIME} seconds..." |
Lev Walkin | ac6db17 | 2017-10-10 00:31:06 -0700 | [diff] [blame] | 211 | echo "(${reproduce_make} fuzz)" > .test-reproduce |
| 212 | CFLAGS="${LIBFUZZER_CFLAGS} ${CFLAGS}" Make fuzz |
Lev Walkin | 39936ce | 2017-10-05 23:56:39 -0700 | [diff] [blame] | 213 | if [ $? -ne 0 ]; then |
Lev Walkin | ac6db17 | 2017-10-10 00:31:06 -0700 | [diff] [blame] | 214 | { echo "RETRY:"; cat .test-reproduce ; } |
Lev Walkin | 233d14d | 2017-10-02 01:09:01 -0700 | [diff] [blame] | 215 | return 5 |
| 216 | fi |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 217 | fi |
| 218 | |
Lev Walkin | a5b0288 | 2017-10-01 22:48:44 -0700 | [diff] [blame] | 219 | return 0 |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 222 | asn_compile() { |
Lev Walkin | 39936ce | 2017-10-05 23:56:39 -0700 | [diff] [blame] | 223 | asn="$1" |
| 224 | where="$2" |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 225 | |
Lev Walkin | 5d947a8 | 2017-10-03 01:04:03 -0700 | [diff] [blame] | 226 | # Create "INTEGER (1..2)" from "T ::= INTEGER (1..2) -- RMAX=5" |
Lev Walkin | ebeb401 | 2017-10-09 19:51:26 -0700 | [diff] [blame] | 227 | short_asn=`echo "$asn" | sed -e 's/ *--.*//;s/RMAX=[0-9]//;'` |
Lev Walkin | 39936ce | 2017-10-05 23:56:39 -0700 | [diff] [blame] | 228 | if [ `echo "$short_asn" | grep -c "::="` = 1 ]; then |
| 229 | short_asn=`echo "$short_asn" | sed -e 's/.*::= *//'` |
Lev Walkin | d3cce46 | 2017-10-01 13:43:36 -0700 | [diff] [blame] | 230 | fi |
| 231 | |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 232 | test ! -f Makefile.am # Protection from accidental clobbering |
Lev Walkin | ac6db17 | 2017-10-10 00:31:06 -0700 | [diff] [blame] | 233 | { |
| 234 | echo "Test DEFINITIONS ::= BEGIN $asn" |
| 235 | echo "-- ${where}" |
| 236 | echo "END" |
| 237 | } > test.asn1 |
| 238 | echo "${abs_top_builddir}/asn1c/asn1c -S ${abs_top_srcdir}/skeletons" |
Lev Walkin | 39936ce | 2017-10-05 23:56:39 -0700 | [diff] [blame] | 239 | if "${abs_top_builddir}/asn1c/asn1c" -S "${abs_top_srcdir}/skeletons" \ |
Lev Walkin | 5754545 | 2017-10-17 18:37:42 -0700 | [diff] [blame] | 240 | -gen-OER -gen-PER ${asn1c_flags} -flink-skeletons test.asn1 |
Lev Walkin | a5b0288 | 2017-10-01 22:48:44 -0700 | [diff] [blame] | 241 | then |
Lev Walkin | 39936ce | 2017-10-05 23:56:39 -0700 | [diff] [blame] | 242 | echo "ASN.1 compiled OK" |
| 243 | else |
Lev Walkin | a5b0288 | 2017-10-01 22:48:44 -0700 | [diff] [blame] | 244 | return 1 |
| 245 | fi |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 246 | rm -f converter-example.c |
Lev Walkin | 09be69c | 2017-10-08 16:28:11 -0700 | [diff] [blame] | 247 | ln -sf "../${srcdir}/random-test-driver.c" || cp "../${srcdir}/random-test-driver.c" . |
Lev Walkin | 40b8a7a | 2017-10-08 22:36:29 -0700 | [diff] [blame] | 248 | { |
| 249 | echo "CFLAGS+= -DASN1_TEXT='$short_asn'"; |
| 250 | echo "ASN_PROGRAM = random-test-driver" |
| 251 | echo "ASN_PROGRAM_SOURCES = random-test-driver.c" |
Lev Walkin | ac6db17 | 2017-10-10 00:31:06 -0700 | [diff] [blame] | 252 | echo |
Lev Walkin | 40b8a7a | 2017-10-08 22:36:29 -0700 | [diff] [blame] | 253 | echo "include Makefile.am.example" |
Lev Walkin | ac6db17 | 2017-10-10 00:31:06 -0700 | [diff] [blame] | 254 | echo |
| 255 | echo "all-tests-succeeded: ${abs_top_builddir}/asn1c/asn1c \$(ASN_PROGRAM_SOURCES) \$(ASN_MODULE_SOURCES) \$(ASN_MODULE_HEADERS)" |
| 256 | echo " @rm -f \$@" |
| 257 | echo " @echo Previous try did not go correctly. To reproduce:" |
| 258 | echo " @cat .test-reproduce" |
| 259 | echo " @exit 1" |
| 260 | echo |
Lev Walkin | 40b8a7a | 2017-10-08 22:36:29 -0700 | [diff] [blame] | 261 | } > Makefile |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 262 | echo "Makefile.am.example -> Makefile" |
| 263 | } |
| 264 | |
Lev Walkin | 7c470c7 | 2017-10-08 04:01:45 -0700 | [diff] [blame] | 265 | # Make up to four different passes: |
| 266 | # CFLAGS: | asn1c_flags: |
| 267 | # -m64 | -fnative-types |
| 268 | # -m32 | -fnative-types |
| 269 | # -m64 | -fwide-types |
| 270 | # -m32 | -fwide-types |
| 271 | # *) Of course, -m64 and -fnative-types are just implied. |
| 272 | test_drive() { |
| 273 | func="$1" |
| 274 | shift |
| 275 | |
| 276 | if [ "x${asn1c_flags}" = "x" ] ; then |
| 277 | # Test for native types and wide types |
| 278 | asn1c_flags=" " test_drive "${func}" "$@" |
| 279 | asn1c_flags="-fnative-types" test_drive "${func}" "$@" |
| 280 | return 0 |
| 281 | fi |
| 282 | |
Lev Walkin | ac6db17 | 2017-10-10 00:31:06 -0700 | [diff] [blame] | 283 | # Can't reuse object code. |
| 284 | rm -rf ${RNDTEMP} |
| 285 | |
Lev Walkin | 7c470c7 | 2017-10-08 04:01:45 -0700 | [diff] [blame] | 286 | echo "MODE: default" |
| 287 | # Default (likely 64-bit) mode |
| 288 | ${func} "$@" |
| 289 | |
| 290 | # 32-bit mode, if available |
| 291 | if echo "${CFLAGS_M32}" | grep -i '[a-z]' > /dev/null ; then |
| 292 | echo "MODE: 32-bit" |
Lev Walkin | ac6db17 | 2017-10-10 00:31:06 -0700 | [diff] [blame] | 293 | |
| 294 | # Can't reuse object code between modes. |
| 295 | rm -rf ${RNDTEMP} |
| 296 | |
Lev Walkin | 7c470c7 | 2017-10-08 04:01:45 -0700 | [diff] [blame] | 297 | # -m32 doesn't support fuzzing (no such library), so we remove fuzzer. |
| 298 | # -m32 doesn't support leak sanitizing (it hangs), so we remove |
Lev Walkin | ac6db17 | 2017-10-10 00:31:06 -0700 | [diff] [blame] | 299 | # ASAN_ENV_FLAGS which enable leak check in runtime. |
Lev Walkin | 7c470c7 | 2017-10-08 04:01:45 -0700 | [diff] [blame] | 300 | CFLAGS="${CFLAGS} ${CFLAGS_M32}" CFLAGS_M32="" \ |
| 301 | LIBFUZZER_CFLAGS="" ASAN_ENV_FLAGS="" \ |
| 302 | ${func} "$@" |
| 303 | fi |
| 304 | } |
| 305 | |
Lev Walkin | 40b8a7a | 2017-10-08 22:36:29 -0700 | [diff] [blame] | 306 | if echo "$*" | grep ' -- ' > /dev/null; then |
| 307 | TEST_DRIVER=`echo "$*" | sed -e 's/ -- .*/ -- /g'` |
| 308 | args=`echo "$*" | sed -e 's/.* //g'` |
| 309 | set "${args}" |
| 310 | else |
| 311 | TEST_DRIVER="" |
| 312 | fi |
| 313 | |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 314 | # Command line parsing |
Lev Walkin | 68619bf | 2017-10-03 18:40:36 -0700 | [diff] [blame] | 315 | while :; do |
| 316 | case "$1" in |
| 317 | -h) usage ;; |
Lev Walkin | d28b45f | 2017-10-05 22:09:19 -0700 | [diff] [blame] | 318 | --asn1c) asn1c_flags="${asn1c_flags} $2"; shift 2; continue ;; |
Lev Walkin | 40b8a7a | 2017-10-08 22:36:29 -0700 | [diff] [blame] | 319 | --bundle) |
| 320 | shift |
Lev Walkin | ac6db17 | 2017-10-10 00:31:06 -0700 | [diff] [blame] | 321 | |
| 322 | # Look for the transcript in bundles/NN-*-bundles.txt.log |
| 323 | set -x |
| 324 | |
Lev Walkin | 40b8a7a | 2017-10-08 22:36:29 -0700 | [diff] [blame] | 325 | base=`basename "$1" | sed -e 's/.txt$//'` |
| 326 | RNDTEMP=".tmp.${base}" |
Lev Walkin | ac6db17 | 2017-10-10 00:31:06 -0700 | [diff] [blame] | 327 | |
| 328 | if Make -C "${RNDTEMP}" all-tests-succeeded >/dev/null 2>&1 ; then |
| 329 | echo "Test succeeded before. Not rechecking." |
| 330 | tests_succeeded=1 |
| 331 | break |
| 332 | fi |
| 333 | |
Lev Walkin | 40b8a7a | 2017-10-08 22:36:29 -0700 | [diff] [blame] | 334 | test_drive verify_asn_types_in_file "$@" |
Lev Walkin | ac6db17 | 2017-10-10 00:31:06 -0700 | [diff] [blame] | 335 | |
| 336 | touch "${RNDTEMP}/all-tests-succeeded" |
| 337 | |
Lev Walkin | 40b8a7a | 2017-10-08 22:36:29 -0700 | [diff] [blame] | 338 | break |
| 339 | ;; |
Lev Walkin | 68619bf | 2017-10-03 18:40:36 -0700 | [diff] [blame] | 340 | --dirty) |
| 341 | need_clean_before_bundle=0 |
| 342 | need_clean_before_test=0 |
| 343 | shift |
| 344 | continue |
| 345 | ;; |
Lev Walkin | d28b45f | 2017-10-05 22:09:19 -0700 | [diff] [blame] | 346 | -e) encodings="${encodings} -e $2"; shift 2; continue;; |
Lev Walkin | 68619bf | 2017-10-03 18:40:36 -0700 | [diff] [blame] | 347 | -j) parallelism="$1"; shift 2; continue;; |
| 348 | -t) |
Lev Walkin | 7c470c7 | 2017-10-08 04:01:45 -0700 | [diff] [blame] | 349 | test_drive verify_asn_type "$2" "(command line)" || exit 1 ;; |
Lev Walkin | 68619bf | 2017-10-03 18:40:36 -0700 | [diff] [blame] | 350 | "") |
Lev Walkin | 09be69c | 2017-10-08 16:28:11 -0700 | [diff] [blame] | 351 | for bundle in `ls -1 ${srcdir}/bundles/*.txt | sort -nr`; do |
Lev Walkin | 7c470c7 | 2017-10-08 04:01:45 -0700 | [diff] [blame] | 352 | test_drive verify_asn_types_in_file "$bundle" |
Lev Walkin | 68619bf | 2017-10-03 18:40:36 -0700 | [diff] [blame] | 353 | done |
| 354 | ;; |
| 355 | *) |
Lev Walkin | 40b8a7a | 2017-10-08 22:36:29 -0700 | [diff] [blame] | 356 | exec ${TEST_DRIVER} $0 --bundle "$@" |
Lev Walkin | 68619bf | 2017-10-03 18:40:36 -0700 | [diff] [blame] | 357 | ;; |
| 358 | esac |
| 359 | break |
| 360 | done |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 361 | |
Lev Walkin | d28b45f | 2017-10-05 22:09:19 -0700 | [diff] [blame] | 362 | if [ "$tests_succeeded" != "0" ] && [ "$tests_failed" = "0" ]; then |
Lev Walkin | cb52391 | 2017-09-30 19:33:23 -0700 | [diff] [blame] | 363 | echo "OK $tests_succeeded tests" |
| 364 | else |
| 365 | echo "FAILED $tests_failed tests, OK $tests_succeeded tests" |
| 366 | exit 1 |
| 367 | fi |