File: run_tests.sh

package info (click to toggle)
libdeflate 1.23-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,072 kB
  • sloc: ansic: 11,716; sh: 1,388; python: 169; makefile: 31
file content (416 lines) | stat: -rwxr-xr-x 9,382 bytes parent folder | download
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#!/bin/bash
#
# Test script for libdeflate
#
# Usage:
#    Run all tests:
#	./run_tests.sh
#    Run only the given tests:
#	./run_tests.sh asan valgrind
#    Run all tests other than the given ones:
#	./run_tests.sh ^asan ^valgrind
#
# See TEST_FUNCS for the available tests.

set -eu -o pipefail
cd "$(dirname "$0")/.."

# Use CC if specified in environment, else default to "cc".
: "${CC:=cc}"

export CFLAGS="-Werror -DLIBDEFLATE_ENABLE_ASSERTIONS"

# No wrapper by default; overridden by valgrind tests
export WRAPPER=

TEST_FUNCS=()

CLEANUP_CMDS=()
cleanup() {
	for cmd in "${CLEANUP_CMDS[@]}"; do
		eval "$cmd"
	done
}
trap cleanup EXIT

CLEANUP_CMDS+=("rm -rf build")

# Use TESTDATA if specified in environment, else generate it.
if [ -z "${TESTDATA:-}" ]; then
	# Generate default TESTDATA file.
	TESTDATA=$(mktemp -t libdeflate_testdata.XXXXXXXXXX)
	export TESTDATA
	CLEANUP_CMDS+=("rm -f '$TESTDATA'")
	find . '(' -name '*.c' -o -name '*.h' -o -name '*.sh' ')' \
		-exec cat '{}' ';' | head -c 1000000 > "$TESTDATA"
fi

TMPDIR=$(mktemp -d -t libdeflate_test.XXXXXXXXX)
CLEANUP_CMDS+=("rm -r '$TMPDIR'")

MAKE="make -j$(getconf _NPROCESSORS_ONLN)"

UNAME=$(uname)
ARCH=$(uname -m)

SHLIB=build/libdeflate.so
if [ "$UNAME" = Darwin ]; then
	SHLIB=build/libdeflate.dylib
fi

###############################################################################

INDENT=0

log()
{
	echo -n "[$(date)] "
	if (( INDENT != 0 )); then
		head -c $(( INDENT * 4 )) /dev/zero | tr '\0' ' '
	fi
	echo "$@"
}

begin()
{
	log "$@"
	(( INDENT++ )) || true
}

end()
{
	(( INDENT-- )) || true
}

run_cmd()
{
	log "$@"
	"$@" > /dev/null
}

fail()
{
	echo 1>&2 "$@"
	exit 1
}

file_count()
{
	local dir=$1

	find "$dir" -type f -o -type l | wc -l
}

cflags_supported()
{
	# -Werror is needed here in order for old versions of clang to reject
	# invalid options.
	echo 'int main(void){ return 0; }' \
		| $CC "$@" -Werror -x c - -o /dev/null 2>/dev/null
}

# Build libdeflate, including the test programs.  Set the special test support
# flag to get support for LIBDEFLATE_DISABLE_CPU_FEATURES.
build()
{
	CFLAGS="$CFLAGS -DTEST_SUPPORT__DO_NOT_USE=1" scripts/cmake-helper.sh \
		-DLIBDEFLATE_BUILD_TESTS=1 "$@" > /dev/null
	$MAKE -C build > /dev/null
}

build_and_run_tests()
{
	local quick=false
	if [ "${1:-}" = "--quick" ]; then
		quick=true
		shift
	fi

	begin "CC=$CC CFLAGS=\"$CFLAGS\" WRAPPER=\"$WRAPPER\" $*"

	build "$@"

	# When not using -march=native, run the tests multiple times with
	# different combinations of CPU features disabled.  This is needed to
	# test all variants of dynamically-dispatched code.
	#
	# For now, we aren't super exhausive in which combinations of features
	# we test disabling.  We just disable the features roughly in order from
	# newest to oldest for each architecture, cumulatively.  In practice,
	# that's good enough to cover all the code.
	local features=('')
	if ! [[ "$CFLAGS" =~ "-march=native" ]] && ! $quick; then
		case "$ARCH" in
		i386|x86_64)
			features+=(zmm avx512_vnni avx512vl avx_vnni vpclmulqdq
				   avx2 avx bmi2 pclmulqdq sse2)
			;;
		arm*|aarch*)
			features+=(dotprod sha3 prefer_pmull crc32 pmull neon)
			;;
		esac
	fi
	local disable_str=""
	local feature
	for feature in "${features[@]}"; do
		if [ -n "$feature" ]; then
			if [ -n "$disable_str" ]; then
				disable_str+=","
			fi
			disable_str+="$feature"
		fi
		log "Using LIBDEFLATE_DISABLE_CPU_FEATURES=$disable_str"
		LIBDEFLATE_DISABLE_CPU_FEATURES="$disable_str" \
		    sh ./scripts/exec_tests.sh build/programs/ > /dev/null
	done
	end
}

is_compatible_system_gzip()
{
	local prog=$1

	# Needs to exist.
	if ! [ -e "$prog" ]; then
		return 1
	fi
	# Needs to be GNU gzip.
	if ! "$prog" -V 2>&1 | grep -q 'Free Software Foundation'; then
		return 1
	fi
	# Needs to support the -k option, i.e. be v1.6 or later.
	if echo | { "$prog" -k 2>&1 >/dev/null || true; } \
				| grep -q 'invalid option'; then
		return 1
	fi
	return 0
}

gzip_tests()
{
	local gzips=("$PWD/build/programs/libdeflate-gzip")
	local gunzips=("$PWD/build/programs/libdeflate-gzip -d")
	if [ "${1:-}" != "--quick" ]; then
		if is_compatible_system_gzip /bin/gzip; then
			gzips+=(/bin/gzip)
			gunzips+=(/bin/gunzip)
		elif is_compatible_system_gzip /usr/bin/gzip; then
			gzips+=(/usr/bin/gzip)
			gunzips+=(/usr/bin/gunzip)
		else
			log "Unsupported system gzip; skipping comparison with system gzip"
		fi
	fi
	local gzip gunzip

	begin "Running gzip program tests with CC=\"$CC\" CFLAGS=\"$CFLAGS\""
	build
	for gzip in "${gzips[@]}"; do
		for gunzip in "${gunzips[@]}"; do
			log "GZIP=$gzip, GUNZIP=$gunzip"
			GZIP="$gzip" GUNZIP="$gunzip" TESTDATA="$TESTDATA" \
				./scripts/gzip_tests.sh
		done
	done
	end
}

do_run_tests()
{
	build_and_run_tests "$@"
	gzip_tests "$@"
}

################################################################################

regular_test()
{
	do_run_tests
}
TEST_FUNCS+=(regular_test)

O3_test()
{
	CFLAGS="$CFLAGS -O3" do_run_tests
}
TEST_FUNCS+=(O3_test)

march_native_test()
{
	if ! cflags_supported "-march=native"; then
		log "Compiler doesn't support -march=native; skipping test"
		return
	fi
	CFLAGS="$CFLAGS -march=native" do_run_tests
}
TEST_FUNCS+=(march_native_test)

valgrind_version_at_least()
{
	local want_vers=$1
	local vers

	if ! type -P valgrind &> /dev/null; then
		return 1
	fi

	vers=$(valgrind --version | grep -E -o '[0-9\.]+' | head -1)

	[ "$want_vers" = "$(echo -e "$vers\n$want_vers" | sort -V | head -1)" ]
}

valgrind_test()
{
	# Need valgrind 3.9.0 for '--errors-for-leak-kinds=all'
	# Need valgrind 3.12.0 for armv8 crypto and crc instructions
	if ! valgrind_version_at_least 3.12.0; then
		log "valgrind not found; skipping test"
		return
	fi
	WRAPPER="valgrind --quiet --error-exitcode=100 --leak-check=full --errors-for-leak-kinds=all" \
		do_run_tests --quick
}
TEST_FUNCS+=(valgrind_test)

ubsan_test()
{
	local cflags=("-fsanitize=undefined" "-fno-sanitize-recover=undefined")
	if ! cflags_supported "${cflags[@]}"; then
		log "Compiler doesn't support UBSAN; skipping test"
		return
	fi
	CFLAGS="$CFLAGS ${cflags[*]}" do_run_tests --quick
}
TEST_FUNCS+=(ubsan_test)

asan_test()
{
	local cflags=("-fsanitize=address" "-fno-sanitize-recover=address")
	if ! cflags_supported "${cflags[@]}"; then
		log "Compiler doesn't support ASAN; skipping test"
		return
	fi
	CFLAGS="$CFLAGS ${cflags[*]}" do_run_tests --quick
}
TEST_FUNCS+=(asan_test)

cfi_test()
{
	local cflags=("-fsanitize=cfi" "-fno-sanitize-recover=cfi" "-flto"
		      "-fvisibility=hidden")
	if ! cflags_supported "${cflags[@]}"; then
		log "Compiler doesn't support CFI; skipping test"
		return
	fi
	CFLAGS="$CFLAGS ${cflags[*]}" AR=llvm-ar do_run_tests --quick
}
TEST_FUNCS+=(cfi_test)

install_test()
{
	build
	$MAKE -C build install DESTDIR=inst > /dev/null
}
TEST_FUNCS+=(install_test)

symbol_prefix_test()
{
	build
	log "Checking that all global symbols are prefixed with \"libdeflate_\""
	if nm build/libdeflate.a | grep ' T ' | grep -E -v " _?libdeflate_"
	then
		fail "Some global symbols aren't prefixed with \"libdeflate_\""
	fi
	log "Checking that all exported symbols are prefixed with \"libdeflate\""
	if nm $SHLIB | grep ' T ' \
			| grep -E -v " _?(libdeflate_|_init\>|_fini\>)"; then
		fail "Some exported symbols aren't prefixed with \"libdeflate_\""
	fi
}
TEST_FUNCS+=(symbol_prefix_test)

is_dynamically_linked()
{
	local prog=$1

	if [ "$UNAME" = Darwin ]; then
		otool -L "$prog" | grep -q libdeflate
	else
		ldd "$prog" | grep -q libdeflate
	fi
}

use_shared_lib_test()
{
	log "Testing USE_SHARED_LIB=1"
	build
	if is_dynamically_linked build/programs/libdeflate-gzip; then
		fail "Binary should be statically linked by default"
	fi
	build -DLIBDEFLATE_USE_SHARED_LIB=1 > /dev/null
	if ! is_dynamically_linked build/programs/libdeflate-gzip; then
		fail "Binary isn't dynamically linked"
	fi
}
TEST_FUNCS+=(use_shared_lib_test)

freestanding_test()
{
	if [ "$UNAME" = Darwin ]; then
		log "Skipping freestanding build tests due to unsupported OS"
		return
	fi
	build_and_run_tests --quick -DLIBDEFLATE_FREESTANDING=1
	if nm $SHLIB | grep -v '\<__stack_chk_fail\>' | grep -q ' U '; then
		echo 1>&2 "Freestanding lib links to external functions!:"
		nm $SHLIB | grep ' U '
		return 1
	fi
	if ldd $SHLIB | grep -q -v '\<statically linked\>'; then
		echo 1>&2 "Freestanding lib links to external libraries!:"
		ldd $SHLIB
		return 1
	fi
}
TEST_FUNCS+=(freestanding_test)

###############################################################################

declare -A all_tests
for test_func in "${TEST_FUNCS[@]}"; do
	all_tests["${test_func%_test}"]=true
done
declare -A tests_to_run

# Determine the set of tests to run by applying any inclusions and exclusions
# given on the command line.  If no inclusions were given, then default to all
# tests (subject to exclusions).
all=true
for arg; do
	if [[ $arg != ^* ]]; then
		all=false
	fi
done
if $all; then
	for t in "${!all_tests[@]}"; do
		tests_to_run[$t]=true
	done
fi
for arg; do
	if [[ $arg == ^* ]]; then
		unset "tests_to_run[${arg#^}]"
	elif [[ -z ${all_tests["$arg"]:-} ]]; then
		fail "Unknown test '$arg'.  Options are: ${!all_tests[*]}"
	else
		tests_to_run["$arg"]=true
	fi
done

# Actually run the tests.
log "Running libdeflate tests: ${!tests_to_run[*]}"
for t in "${!tests_to_run[@]}"; do
	begin "Running ${t}_test"
	eval "${t}_test"
	end
done
log "All tests passed!"