-
Notifications
You must be signed in to change notification settings - Fork 227
/
tests.sh
303 lines (270 loc) · 7.46 KB
/
tests.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/do/not/execute
# Copyright (C) 2011-2013 pancake<nopcode.org>
# Copyright (C) 2011-2012 Edd Barrett <[email protected]>
# Copyright (C) 2012 Simon Ruderich <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
GREP="$1"
GREP=""
cd `dirname $0` 2>/dev/null
# ignore encoding in sed
export LANG=C
export LC_CTYPE=C
printdiff() {
if [ -n "${VERBOSE}" ]; then
echo
print_label Command:
echo "${R2CMD}"
print_label Script:
cat ${TMP_RAD}
fi
}
run_test() {
# TODO: remove which dependency
[ -z "${R2}" ] && R2=$(which radare2)
PD="/tmp/r2-regressions/" # XXX
if [ -z "${R2}" ]; then
echo "ERROR: Cannot find radare2 program in PATH"
exit 1
fi
if [ -n "${GREP}" ]; then
if [ -z "`echo \"${NAME}\" | grep \"${GREP}\"`" ]; then
return
fi
fi
# Set by run_tests.sh if all tests are run - otherwise get it from test
# name.
if [ -z "${TEST_NAME}" ]; then
TEST_NAME=$(basename "$0" | sed 's/\.sh$//')
fi
NAME_TMP="${TEST_NAME}" #`basename $NAME` #"${TEST_NAME}"
if [ -n "${NAME}" ]; then
if [ "$NAME_TMP" = "$NAME" ]; then
NAME_A="${NAME_TMP}"
NAME_B=""
NAME_TMP="${NAME_TMP}:"
else
NAME_A="${NAME_TMP}"
NAME_B="${NAME}"
NAME_TMP="${NAME_TMP}: ${NAME}"
fi
fi
[ -n "${VALGRIND}" ] && NAME_TMP="${NAME_TMP} (valgrind)"
if [ -n "${NOCOLOR}" ]; then
printf "[ ] %s: %-30s" "${NAME_A}" "${NAME_B}"
else
printf "\033[33m[ ] %s: \033[0m%-30s" "${NAME_A}" "${NAME_B}" #"${NAME_TMP}"
fi
# Check required variables.
if [ -z "${FILE}" ]; then
test_failed "FILE missing!"
test_reset
return
fi
if [ -z "${SHELLCMD}" -a -z "${CMDS}" ]; then
test_failed "CMDS missing!"
test_reset
return
fi
# ${EXPECT} can be empty. Don't check it.
# Verbose mode is always used if only a single test is run.
if [ -z "${R2_SOURCED}" ]; then
VERBOSE=1
fi
mkdir -p ${PD}
TMP_RAD=`mktemp "${PD}/${TEST_NAME}-rad.XXXXXX"`
TMP_OUT=`mktemp "${PD}/${TEST_NAME}-out.XXXXXX"`
TMP_ERR=`mktemp "${PD}/${TEST_NAME}-err.XXXXXX"`
TMP_EXR=`mktemp "${PD}/${TEST_NAME}-exr.XXXXXX"` # expected error
TMP_VAL=`mktemp "${PD}/${TEST_NAME}-val.XXXXXX"`
TMP_EXP=`mktemp "${PD}/${TEST_NAME}-exp.XXXXXX"`
if [ -n "${SHELLCMD}" ]; then
R2CMD="$SHELLCMD"
else
# No colors and no user configs.
R2ARGS="${R2} -e scr.color=0 -N -q -i ${TMP_RAD} ${ARGS} ${FILE} > ${TMP_OUT} 2> ${TMP_ERR}"
R2CMD=
# Valgrind to detect memory corruption.
if [ -n "${VALGRIND}" ]; then
R2CMD="valgrind --error-exitcode=47 --log-file=${TMP_VAL}"
fi
R2CMD="${R2CMD} ${R2ARGS}"
#if [ -n "${VERBOSE}" ]; then
#echo #$R2CMD
#fi
fi
# Put expected outcome and program to run in files and run the test.
printf "%s\n" "${CMDS}" > ${TMP_RAD}
printf "%s" "${EXPECT}" > ${TMP_EXP}
printf "%s" "${EXPECT_ERR}" > ${TMP_EXR}
eval "${R2CMD}"
CODE=$?
if [ -n "${R2_SOURCED}" ]; then
TESTS_TOTAL=$(( TESTS_TOTAL + 1 ))
fi
# ${FILTER} can be used to filter out random results to create stable
# tests.
if [ -n "${FILTER}" ]; then
# Filter stdout.
FILTER_CMD="cat ${TMP_OUT} | ${FILTER} > ${TMP_OUT}.filter"
#if [ -n "${VERBOSE}" ]; then
# echo "Filter (stdout): ${FILTER}"
#fi
eval "${FILTER_CMD}"
mv "${TMP_OUT}.filter" "${TMP_OUT}"
# Filter stderr.
FILTER_CMD="cat ${TMP_ERR} | ${FILTER} > ${TMP_ERR}.filter"
#if [ -n "${VERBOSE}" ]; then
# echo "Filter (stderr): ${FILTER}"
#fi
eval "${FILTER_CMD}"
mv "${TMP_ERR}.filter" "${TMP_ERR}"
fi
# Check if radare2 exited with correct exit code.
if [ -n "${EXITCODE}" ]; then
if [ ${CODE} -eq "${EXITCODE}" ]; then
CODE=0
EXITCODE=
else
EXITCODE=${CODE}
fi
fi
# Check if the output matched.
diff "${TMP_OUT}" "${TMP_EXP}" >/dev/null
OUT_CODE=$?
if [ "${IGNORE_ERR}" = 1 ]; then
ERR_CODE=0
else
diff "${TMP_ERR}" "${TMP_EXR}" >/dev/null
ERR_CODE=$?
fi
if [ ${CODE} -eq 47 ]; then
test_failed "valgrind error"
if [ -n "${VERBOSE}" ]; then
cat "${TMP_VAL}"
echo
fi
elif [ -n "${EXITCODE}" ]; then
test_failed "wrong exit code: ${EXITCODE}"
printdiff
elif [ ${CODE} -ne 0 ]; then
test_failed "radare2 crashed"
printdiff
if [ -n "${VERBOSE}" ]; then
cat "${TMP_OUT}"
cat "${TMP_ERR}"
echo
fi
elif [ ${OUT_CODE} -ne 0 ]; then
test_failed "out"
printdiff
if [ -n "${VERBOSE}" ]; then
print_label Diff:
diff -u "${TMP_EXP}" "${TMP_OUT}"
echo
fi
elif [ ${ERR_CODE} -ne 0 ]; then
test_failed "err"
printdiff
if [ -n "${VERBOSE}" ]; then
diff -u "${TMP_EXR}" "${TMP_ERR}"
echo
fi
else
test_success
fi
rm -f "${TMP_RAD}" "${TMP_OUT}" "${TMP_ERR}" "${TMP_VAL}" \
"${TMP_EXP}" "${TMP_EXR}"
# Reset most variables in case the next test script doesn't set them.
test_reset
return $OUT_CODE
}
test_reset() {
[ -z "$NAME" ] && NAME=$0
FILE="-"
ARGS=
CMDS=
EXPECT=
EXPECT_ERR=
IGNORE_ERR=0
FILTER=
EXITCODE=
BROKEN=
SHELLCMD=
}
test_reset
test_success() {
if [ -z "${BROKEN}" ]; then
print_success "OK"
else
print_fixed "FX"
fi
if [ -n "${R2_SOURCED}" ]; then
if [ -z "${BROKEN}" ]; then
TESTS_SUCCESS=$(( TESTS_SUCCESS + 1 ))
else
TESTS_FIXED=$(( TESTS_FIXED + 1 ))
fi
fi
}
test_failed() {
if [ -z "${BROKEN}" ]; then
print_failed "XX"
else
print_broken "BR"
fi
FAILED="${FAILED}${TEST_NAME}:"
if [ -n "${R2_SOURCED}" ]; then
if [ -z "${BROKEN}" ]; then
TESTS_FAILED=$(( TESTS_FAILED + 1 ))
else
TESTS_BROKEN=$(( TESTS_BROKEN + 1 ))
fi
fi
}
print_success() {
if [ -n "${NOCOLOR}" ]; then
printf "%b" "\r[${*}]\n"
else
printf "%b" "\r\033[32m[${*}]\033[0m\n"
fi
}
print_broken() {
if [ -n "${NOCOLOR}" ]; then
printf "%b" "\r[${*}]\n"
else
printf "%b" "\r\033[34m[${*}]\033[0m\n"
fi
}
print_failed() {
if [ -n "${NOCOLOR}" ]; then
printf "%b" "\r[${*}]\n"
else
printf "%b" "\r\033[31m[${*}]\033[0m\n"
fi
}
print_fixed() {
if [ -n "${NOCOLOR}" ]; then
printf "%b" "\r[${*}]\n"
else
printf "%b" "\r\033[33m[${*}]\033[0m\n"
fi
}
print_label() {
if [ -n "${NOCOLOR}" ]; then
printf "%s\n" $@
else
printf "\033[35m%s \033[0m" $@
fi
}