blob: 30944f26adc1fbd31c0372079ad523e9b2d8df8a [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
11
12usage() {
13 echo "Usage:"
14 echo " $0 -h"
15 echo " $0 -t \"<ASN.1 definition for type T, in string form>\""
16 echo " $0 bundles/<bundle-name.txt> [<line>]"
17 echo "Examples:"
18 echo " $0 -t UTF8String"
19 echo " $0 -t \"T ::= INTEGER (0..1)\""
20 echo " $0 bundles/01-INTEGER-bundle.txt 3"
21 exit 1
22}
23
24tests_succeeded=0
25tests_failed=0
26
27# Get all the type-bearding lines in file and process them individually
28verify_asn_types_in_file() {
29 local filename="$1"
30 local need_line="$2"
31 test "x$filename" != "x" || usage
32 echo "Open [$filename]"
33 local line=0
34 while read asn; do
35 line=$((line+1))
36 if echo "$asn" | sed -e 's/--.*//;' | grep -vqi "[A-Z]"; then
37 # Ignore lines consisting of just comments.
38 continue;
39 fi
40 if [ "x$need_line" != "x" -a "$need_line" != "$line" ]; then
41 # We need a different line.
42 continue;
43 fi
44 verify_asn_type "$asn" "in $filename $line"
45 done < "$filename"
46}
47
48verify_asn_type() {
49 local asn="$1"
50 shift
51 local where="$*"
52 test "x$asn" != "x" || usage
53 if echo "$asn" | grep -qv "::="; then
54 asn="T ::= $asn"
55 fi
56 echo "Testing [$asn] ${where}"
57
58 mkdir -p .tmp.random
59 if (cd .tmp.random && compile_and_test "$asn" "$@"); then
60 echo "OK [$asn] ${where}"
61 tests_succeeded=$((tests_succeeded+1))
62 else
63 tests_failed=$((tests_failed+1))
64 echo "FAIL [$asn] ${where}"
65 fi
66}
67
68compile_and_test() {
69 local asn="$1"
70 shift
71
72 if ! asn_compile "$asn" "$@"; then
73 echo "Cannot compile ASN.1 $asn"
74 return 1
75 fi
76
77 rm -f random-test-driver.o
78 rm -f random-test-driver
79 if ! make -j4; then
80 echo "Cannot compile C for $asn in .tmp.random"
81 return 2
82 fi
83
84 echo "Checking random data encode-decode"
85 if ! eval ${ASAN_ENV_FLAGS} ./random-test-driver -c; then
86 return 3
87 fi
88
89 # If LIBFUZZER_CFLAGS are properly defined, do the fuzz test as well
90 if echo "${LIBFUZZER_CFLAGS}" | grep -qi "[a-z]"; then
91 echo "Generating new random data"
92 rm -rf random-data
93 ./random-test-driver -g random-data
94
95 echo "Recompiling for fuzzing..."
96 rm -f random-test-driver.o
97 rm -f random-test-driver
98 CFLAGS="${LIBFUZZER_CFLAGS} ${CFLAGS}" make
99
100 # Do a LibFuzzer based testing
101 fuzz_time=10
102 echo "Fuzzing will take $fuzz_time seconds..."
103 set -x
104 eval ${ASAN_ENV_FLAGS} UBSAN_OPTIONS=print_stacktrace=1 \
105 ./random-test-driver \
106 -timeout=3 -max_total_time=${fuzz_time} -max_len=128
107 fi
108
109 return 0;
110}
111
112srcdir="${srcdir:-.}"
113abs_top_srcdir="${abs_top_srcdir:-$(pwd)/../../}"
114abs_top_builddir="${abs_top_builddir:-$(pwd)/../../}"
115
116asn_compile() {
117 local asn="$1"
118 shift
119 test ! -f Makefile.am # Protection from accidental clobbering
120 echo "Test DEFINITIONS ::= BEGIN $asn" > test.asn1
121 echo "END" >> test.asn1
122 ${abs_top_builddir}/asn1c/asn1c -S ${abs_top_srcdir}/skeletons \
123 -gen-OER -gen-PER test.asn1
124 rm -f converter-example.c
125 ln -sf ../random-test-driver.c || cp ../random-test-driver.c .
126 sed -e 's/converter-example/random-test-driver/' \
127 < Makefile.am.example > Makefile
128 echo "Makefile.am.example -> Makefile"
129}
130
131# Command line parsing
132case "$1" in
133 -h) usage ;;
134 -t) verify_asn_type "$2" || exit 1;;
135 "")
136 for bundle in bundles/*txt; do
137 verify_asn_types_in_file "$bundle"
138 done
139 ;;
140 *)
141 verify_asn_types_in_file "$@"
142 ;;
143esac
144
145if [ "$tests_succeeded" != "0" -a "$tests_failed" = "0" ]; then
146 echo "OK $tests_succeeded tests"
147else
148 echo "FAILED $tests_failed tests, OK $tests_succeeded tests"
149 exit 1
150fi