]> git.ipfire.org Git - thirdparty/util-linux.git/blob - tests/functions.sh
blkdiscard: (man) add note about fdisk
[thirdparty/util-linux.git] / tests / functions.sh
1 #
2 # Copyright (C) 2007 Karel Zak <kzak@redhat.com>
3 #
4 # This file is part of util-linux.
5 #
6 # This file is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This file is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16
17 TS_EXIT_NOTSUPP=2
18
19 function ts_abspath {
20 cd $1
21 pwd
22 }
23
24 function ts_canonicalize {
25 P="$1"
26 C=$(readlink -f $P)
27
28 if [ -n "$C" ]; then
29 echo "$C"
30 else
31 echo "$P"
32 fi
33 }
34
35 function ts_cd {
36 if [ $# -eq 0 ]; then
37 ts_failed "ul_cd: not enough arguments"
38 fi
39 DEST=$(readlink -f "$1" 2>/dev/null)
40 if [ "x$DEST" = "x" ] || [ ! -d "$DEST" ]; then
41 ts_failed "ul_cd: $1: no such directory"
42 fi
43 cd "$DEST" 2>/dev/null || ts_failed "ul_cd: $1: cannot change directory"
44 if [ "$PWD" != "$DEST" ]; then
45 ts_failed "ul_cd: $PWD is not $DEST"
46 fi
47 }
48
49 function ts_separator {
50 local header="$1"
51 echo >> $TS_OUTPUT
52 if [ -z "$header" ]; then
53 echo "============================================" >> $TS_OUTPUT
54 else
55 echo "=====$header================================" >> $TS_OUTPUT
56 fi
57 }
58
59 function ts_report {
60 local desc=
61
62 if [ "$TS_PARSABLE" != "yes" ]; then
63 if [ $TS_NSUBTESTS -ne 0 ] && [ -z "$TS_SUBNAME" ]; then
64 desc=$(printf "%11s...")
65 fi
66 echo "$desc$1"
67 return
68 fi
69
70 if [ -n "$TS_SUBNAME" ]; then
71 desc=$(printf "%s: [%02d] %s" "$TS_DESC" "$TS_NSUBTESTS" "$TS_SUBNAME")
72 else
73 desc=$TS_DESC
74 fi
75 printf "%13s: %-45s ...%s\n" "$TS_COMPONENT" "$desc" "$1"
76 }
77
78 function ts_check_test_command {
79 case "$1" in
80 "")
81 ts_failed "invalid test_command requested"
82 ;;
83 */*)
84 # paths
85 if [ ! -x "$1" ]; then
86 ts_skip "${1##*/} not found"
87 fi
88 ;;
89 *)
90 # just command names (e.g. --use-system-commands)
91 local cmd=$1
92 type "$cmd" >/dev/null 2>&1
93 if [ $? -ne 0 ]; then
94 if [ "$TS_NOSKIP_COMMANDS" = "yes" ]; then
95 ts_failed "missing in PATH: $cmd"
96 fi
97 ts_skip "missing in PATH: $cmd"
98 fi
99 ;;
100 esac
101 }
102
103 function ts_check_prog {
104 local cmd=$1
105 [ -z "$cmd" ] && ts_failed "invalid prog requested"
106 type "$cmd" >/dev/null 2>&1 || ts_skip "missing in PATH: $cmd"
107 }
108
109 function ts_check_losetup {
110 local tmp
111 ts_check_test_command "$TS_CMD_LOSETUP"
112
113 if [ "$TS_SKIP_LOOPDEVS" = "yes" ]; then
114 ts_skip "loop-device tests disabled"
115 fi
116
117 # assuming that losetup -f works ... to be checked somewhere else
118 tmp=$($TS_CMD_LOSETUP -f 2>/dev/null)
119 if test -b "$tmp"; then
120 return 0
121 fi
122 ts_skip "no loop-device support"
123 }
124
125 function ts_check_wcsspn {
126 # https://gitlab.com/qemu-project/qemu/-/issues/1248
127 if [ -e "$TS_HELPER_SYSINFO" ] &&
128 [ "$("$TS_HELPER_SYSINFO" wcsspn-ok)" = "0" ]; then
129
130 ts_skip "non-functional widestring functions"
131 fi
132 }
133
134 function ts_check_native_byteorder {
135 if [ "$QEMU_USER" == "1" ] && [ ! -e /sys/kernel/cpu_byteorder ]; then
136 ts_skip "non-native byteorder"
137 fi
138 }
139
140 function ts_check_enotty {
141 # https://lore.kernel.org/qemu-devel/20230426070659.80649-1-thomas@t-8ch.de/
142 if [ -e "$TS_HELPER_SYSINFO" ] &&
143 [ "$("$TS_HELPER_SYSINFO" enotty-ok)" = "0" ]; then
144
145 ts_skip "broken ENOTTY return"
146 fi
147 }
148
149 function ts_report_skip {
150 ts_report " SKIPPED ($1)"
151 }
152
153 function ts_skip {
154 ts_report_skip "$1"
155
156 ts_cleanup_on_exit
157 exit 0
158 }
159
160 function ts_skip_nonroot {
161 if [ $UID -ne 0 ]; then
162 ts_skip "no root permissions"
163 fi
164 }
165
166 # Specify the capability needed in your test case like:
167 #
168 # ts_skip_capability cap_wake_alarm
169 #
170 function ts_skip_capability {
171 ts_check_prog "$TS_HELPER_CAP"
172
173 if ! "$TS_HELPER_CAP" "$1"; then
174 ts_skip "no capability: $1"
175 fi
176 }
177
178 function ts_skip_qemu_user {
179 if [ "$QEMU_USER" == "1" ]; then
180 ts_skip "running under qemu-user emulation"
181 fi
182 }
183
184 function ts_failed_subtest {
185 local msg="FAILED"
186 local ret=1
187 if [ "$TS_KNOWN_FAIL" = "yes" ]; then
188 msg="KNOWN FAILED"
189 ret=0
190 fi
191
192 if [ x"$1" == x"" ]; then
193 ts_report " $msg ($TS_NS)"
194 else
195 ts_report " $msg ($1)"
196 fi
197
198 return $ret
199 }
200
201 function ts_failed {
202 ts_failed_subtest "$1"
203 exit $?
204 }
205
206 function ts_report_ok {
207 if [ x"$1" == x"" ]; then
208 ts_report " OK"
209 else
210 ts_report " OK ($1)"
211 fi
212 }
213
214 function ts_ok {
215 ts_report_ok "$1"
216 exit 0
217 }
218
219 function ts_log {
220 echo "$1" >> $TS_OUTPUT
221 [ "$TS_VERBOSE" == "yes" ] && echo "$1"
222 }
223
224 function ts_logerr {
225 echo "$1" >> $TS_ERRLOG
226 [ "$TS_VERBOSE" == "yes" ] && echo "$1"
227 }
228
229 function ts_log_both {
230 echo "$1" >> $TS_OUTPUT
231 echo "$1" >> $TS_ERRLOG
232 [ "$TS_VERBOSE" == "yes" ] && echo "$1"
233 }
234
235 function ts_has_option {
236 NAME="$1"
237 ALL="$2"
238
239 # user may set options by env for a single test or whole component
240 # e.g. TS_OPT_ipcs_limits2_fake="yes" or TS_OPT_ipcs_fake="yes"
241 local v_test=${TS_TESTNAME//[-.]/_}
242 local v_comp=${TS_COMPONENT//[-.]/_}
243 local v_name=${NAME//[-.]/_}
244 eval local env_opt_test=\$TS_OPT_${v_comp}_${v_test}_${v_name}
245 eval local env_opt_comp=\$TS_OPT_${v_comp}_${v_name}
246 if [ "$env_opt_test" = "yes" \
247 -o "$env_opt_comp" = "yes" -a "$env_opt_test" != "no" ]; then
248 echo "yes"
249 return
250 elif [ "$env_opt_test" = "no" \
251 -o "$env_opt_comp" = "no" -a "$env_opt_test" != "yes" ]; then
252 return
253 fi
254
255 # or just check the global command line options
256 if [[ $ALL =~ ([$' \t\n']|^)--$NAME([$'= \t\n']|$) ]]; then
257 echo yes
258 return
259 fi
260
261 # or the _global_ env, e.g TS_OPT_parsable="yes"
262 eval local env_opt=\$TS_OPT_${v_name}
263 if [ "$env_opt" = "yes" ]; then echo "yes"; fi
264 }
265
266 function ts_option_argument {
267 NAME="$1"
268 ALL="$2"
269
270 # last option wins!
271 echo "$ALL" | sed -n "s/.*[ \t\n]--$NAME=\([^ \t\n]*\).*/\1/p" | tail -n 1
272 }
273
274 function ts_init_core_env {
275 TS_SUBNAME=""
276 TS_NS="$TS_COMPONENT/$TS_TESTNAME"
277 TS_OUTPUT="$TS_OUTDIR/$TS_TESTNAME"
278 TS_ERRLOG="$TS_OUTDIR/$TS_TESTNAME.err"
279 TS_VGDUMP="$TS_OUTDIR/$TS_TESTNAME.vgdump"
280 TS_EXIT_CODE="$TS_OUTDIR/$TS_TESTNAME.exit_code"
281 TS_DIFF="$TS_DIFFDIR/$TS_TESTNAME"
282 TS_EXPECTED="$TS_TOPDIR/expected/$TS_NS"
283 TS_EXPECTED_ERR="$TS_TOPDIR/expected/$TS_NS.err"
284 TS_MOUNTPOINT="$TS_OUTDIR/${TS_TESTNAME}-mnt"
285 }
286
287 function ts_init_core_subtest_env {
288 TS_NS="$TS_COMPONENT/$TS_TESTNAME-$TS_SUBNAME"
289 TS_OUTPUT="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME"
290 TS_ERRLOG="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME.err"
291 TS_VGDUMP="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME.vgdump"
292 TS_EXIT_CODE="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME.exit_code"
293 TS_DIFF="$TS_DIFFDIR/$TS_TESTNAME-$TS_SUBNAME"
294 TS_EXPECTED="$TS_TOPDIR/expected/$TS_NS"
295 TS_EXPECTED_ERR="$TS_TOPDIR/expected/$TS_NS.err"
296 TS_MOUNTPOINT="$TS_OUTDIR/${TS_TESTNAME}-${TS_SUBNAME}-mnt"
297
298 rm -f "$TS_OUTPUT" "$TS_ERRLOG" "$TS_VGDUMP" "$TS_EXIT_CODE"
299 [ -d "$TS_OUTDIR" ] || mkdir -p "$TS_OUTDIR"
300
301 touch "$TS_OUTPUT" "$TS_ERRLOG" "$TS_EXIT_CODE"
302 [ -n "$TS_VALGRIND_CMD" ] && touch $TS_VGDUMP
303 }
304
305 function ts_init_env {
306 local mydir=$(ts_abspath ${0%/*})
307 local tmp
308
309 LANG="POSIX"
310 LANGUAGE="POSIX"
311 LC_ALL="POSIX"
312 CHARSET="UTF-8"
313 ASAN_OPTIONS="detect_leaks=0"
314 UBSAN_OPTIONS="print_stacktrace=1:print_summary=1:halt_on_error=1"
315
316 export LANG LANGUAGE LC_ALL CHARSET ASAN_OPTIONS UBSAN_OPTIONS
317
318 mydir=$(ts_canonicalize "$mydir")
319
320 # automake directories
321 top_srcdir=$(ts_option_argument "srcdir" "$*")
322 top_builddir=$(ts_option_argument "builddir" "$*")
323
324 # where is this script
325 TS_TOPDIR=$(ts_abspath $mydir/../../)
326
327 # default
328 if [ -z "$top_srcdir" ]; then
329 top_srcdir="$TS_TOPDIR/.."
330 fi
331 if [ -z "$top_builddir" ]; then
332 top_builddir="$TS_TOPDIR/.."
333 fi
334
335 top_srcdir=$(ts_abspath $top_srcdir)
336 top_builddir=$(ts_abspath $top_builddir)
337
338 if [ -e "$top_builddir/meson.conf" ]; then
339 . "$top_builddir/meson.conf"
340 fi
341
342 # We use helpser always from build tree
343 ts_helpersdir="${top_builddir}/"
344
345 TS_USE_SYSTEM_COMMANDS=$(ts_has_option "use-system-commands" "$*")
346 if [ "$TS_USE_SYSTEM_COMMANDS" == "yes" ]; then
347 # Don't define anything, just follow current PATH
348 ts_commandsdir=""
349 else
350 # The default is to use commands from build tree
351 ts_commandsdir="${top_builddir}/"
352
353 # some ul commands search other ul commands in $PATH
354 export PATH="$ts_commandsdir:$PATH"
355 fi
356
357 TS_SCRIPT="$mydir/$(basename $0)"
358 TS_SUBDIR=$(dirname $TS_SCRIPT)
359 TS_TESTNAME=$(basename $TS_SCRIPT)
360 TS_COMPONENT=$(basename $TS_SUBDIR)
361 TS_DESC=${TS_DESC:-$TS_TESTNAME}
362
363 TS_NSUBTESTS=0
364 TS_NSUBFAILED=0
365
366 TS_SELF="$TS_SUBDIR"
367
368 TS_OUTDIR="$top_builddir/tests/output/$TS_COMPONENT"
369 TS_DIFFDIR="$top_builddir/tests/diff/$TS_COMPONENT"
370
371 TS_NOLOCKS=$(ts_has_option "nolocks" "$*")
372 TS_LOCKDIR="$top_builddir/tests/output"
373
374 # Don't lock if flock(1) is missing
375 type "flock" >/dev/null 2>&1 || TS_NOLOCKS="yes"
376
377 ts_init_core_env
378
379 TS_NOSKIP_COMMANDS=$(ts_has_option "noskip-commands" "$*")
380 TS_VERBOSE=$(ts_has_option "verbose" "$*")
381 TS_SHOWDIFF=$(ts_has_option "show-diff" "$*")
382 TS_PARALLEL=$(ts_has_option "parallel" "$*")
383 TS_KNOWN_FAIL=$(ts_has_option "known-fail" "$*")
384 TS_SKIP_LOOPDEVS=$(ts_has_option "skip-loopdevs" "$*")
385 TS_PARSABLE=$(ts_has_option "parsable" "$*")
386 [ "$TS_PARSABLE" = "yes" ] || TS_PARSABLE="$TS_PARALLEL"
387
388 tmp=$( ts_has_option "memcheck-valgrind" "$*")
389 if [ "$tmp" == "yes" -a -f /usr/bin/valgrind ]; then
390 TS_VALGRIND_CMD="/usr/bin/valgrind"
391 fi
392 tmp=$( ts_has_option "memcheck-asan" "$*")
393 if [ "$tmp" == "yes" ]; then
394 TS_ENABLE_ASAN="yes"
395 fi
396 tmp=$( ts_has_option "memcheck-ubsan" "$*")
397 if [ "$tmp" == "yes" ]; then
398 TS_ENABLE_UBSAN="yes"
399 fi
400
401 TS_FSTAB="$TS_OUTDIR/${TS_TESTNAME}.fstab"
402 BLKID_FILE="$TS_OUTDIR/${TS_TESTNAME}.blkidtab"
403
404 declare -a TS_SUID_PROGS
405 declare -a TS_SUID_USER
406 declare -a TS_SUID_GROUP
407 declare -a TS_LOOP_DEVS
408 declare -a TS_LOCKFILE_FD
409
410 if [ -f $TS_TOPDIR/commands.sh ]; then
411 . $TS_TOPDIR/commands.sh
412 fi
413
414 export BLKID_FILE
415
416 rm -f $TS_OUTPUT $TS_ERRLOG $TS_VGDUMP $TS_EXIT_CODE
417 [ -d "$TS_OUTDIR" ] || mkdir -p "$TS_OUTDIR"
418
419 touch $TS_OUTPUT $TS_ERRLOG $TS_EXIT_CODE
420 [ -n "$TS_VALGRIND_CMD" ] && touch $TS_VGDUMP
421
422 if [ "$TS_VERBOSE" == "yes" ]; then
423 echo
424 echo " script: $TS_SCRIPT"
425 echo " commands: $ts_commandsdir"
426 echo " helpers: $ts_helpersdir"
427 echo " sub dir: $TS_SUBDIR"
428 echo " top dir: $TS_TOPDIR"
429 echo " self: $TS_SELF"
430 echo " test name: $TS_TESTNAME"
431 echo " test desc: $TS_DESC"
432 echo " component: $TS_COMPONENT"
433 echo " namespace: $TS_NS"
434 echo " verbose: $TS_VERBOSE"
435 echo " output: $TS_OUTPUT"
436 echo " error log: $TS_ERRLOG"
437 echo " exit code: $TS_EXIT_CODE"
438 echo " valgrind: $TS_VGDUMP"
439 echo " expected: $TS_EXPECTED{.err}"
440 echo " mountpoint: $TS_MOUNTPOINT"
441 echo
442 fi
443 }
444
445 function ts_init_subtest {
446
447 TS_SUBNAME="$1"
448 ts_init_core_subtest_env
449 TS_NSUBTESTS=$(( $TS_NSUBTESTS + 1 ))
450
451 if [ "$TS_PARSABLE" != "yes" ]; then
452 [ $TS_NSUBTESTS -eq 1 ] && echo
453 printf "%16s: %-27s ..." "" "$TS_SUBNAME"
454 fi
455 }
456
457 function ts_init {
458 ts_init_env "$*"
459
460 local is_fake=$( ts_has_option "fake" "$*")
461 local is_force=$( ts_has_option "force" "$*")
462
463 if [ "$TS_PARSABLE" != "yes" ]; then
464 printf "%13s: %-30s ..." "$TS_COMPONENT" "$TS_DESC"
465 fi
466
467 [ "$is_fake" == "yes" ] && ts_skip "fake mode"
468 [ "$TS_OPTIONAL" == "yes" -a "$is_force" != "yes" ] && ts_skip "optional"
469 }
470
471 function ts_init_suid {
472 PROG="$1"
473 ct=${#TS_SUID_PROGS[*]}
474
475 # Save info about original setting
476 TS_SUID_PROGS[$ct]=$PROG
477 TS_SUID_USER[$ct]=$(stat --printf="%U" $PROG)
478 TS_SUID_GROUP[$ct]=$(stat --printf="%G" $PROG)
479
480 chown root:root $PROG &> /dev/null
481 chmod u+s $PROG &> /dev/null
482 }
483
484 function ts_init_py {
485 LIBNAME="$1"
486
487 if [ -f "$top_builddir/py${LIBNAME}.la" ]; then
488 # autotoolz build
489 export LD_LIBRARY_PATH="$top_builddir/.libs:$LD_LIBRARY_PATH"
490 export PYTHONPATH="$top_builddir/$LIBNAME/python:$top_builddir/.libs:$PYTHONPATH"
491
492 PYTHON_VERSION=$(awk '/^PYTHON_VERSION/ { print $3 }' $top_builddir/Makefile)
493 PYTHON_MAJOR_VERSION=$(echo $PYTHON_VERSION | sed 's/\..*//')
494
495 export PYTHON="python${PYTHON_MAJOR_VERSION}"
496
497 elif compgen -G "$top_builddir/$LIBNAME/python/py$LIBNAME*.so" >/dev/null; then
498 # mezon!
499 export PYTHONPATH="$top_builddir/$LIBNAME/python:$PYTHONPATH"
500
501 else
502 ts_skip "py${LIBNAME} not compiled"
503 fi
504 }
505
506 function ts_run {
507 declare -a args
508 local asan_options="strict_string_checks=1:detect_stack_use_after_return=1:check_initialization_order=1:strict_init_order=1"
509
510 #
511 # ASAN mode
512 #
513 if [ "$TS_ENABLE_ASAN" == "yes" -o "$TS_ENABLE_UBSAN" == "yes" ]; then
514 args+=(env)
515 if [ "$TS_ENABLE_ASAN" == "yes" ]; then
516 # detect_leaks isn't supported on s390x: https://github.com/llvm/llvm-project/blob/master/compiler-rt/lib/lsan/lsan_common.h
517 if [ "$(uname -m)" != "s390x" ]; then
518 asan_options="$asan_options:detect_leaks=1"
519 fi
520 args+=(ASAN_OPTIONS=$asan_options)
521 fi
522 if [ "$TS_ENABLE_UBSAN" == "yes" ]; then
523 args+=(UBSAN_OPTIONS=print_stacktrace=1:print_summary=1:halt_on_error=1)
524 fi
525 fi
526
527 #
528 # valgrind mode
529 #
530 if [ -n "$TS_VALGRIND_CMD" ]; then
531 args+=(libtool --mode=execute "$TS_VALGRIND_CMD" --tool=memcheck --leak-check=full)
532 args+=(--leak-resolution=high --num-callers=20 --log-file="$TS_VGDUMP")
533 fi
534
535 "${args[@]}" "$@"
536 echo $? >$TS_EXIT_CODE
537 }
538
539 function ts_gen_diff_from {
540 local res=0
541 local expected="$1"
542 local output="$2"
543 local difffile="$3"
544
545 diff -u "$expected" "$output" > "$difffile"
546
547 if [ $? -ne 0 ] || [ -s "$difffile" ]; then
548 res=1
549 if [ "$TS_SHOWDIFF" == "yes" -a "$TS_KNOWN_FAIL" != "yes" ]; then
550 echo
551 echo "diff-{{{"
552 cat "$difffile"
553 echo "}}}-diff"
554 echo
555 fi
556 else
557 rm -f "$difffile";
558 fi
559
560 return $res
561 }
562
563 function ts_gen_diff {
564 local status_out=0
565 local status_err=0
566 local exit_code=0
567
568 [ -f "$TS_OUTPUT" ] || return 1
569 [ -f "$TS_EXPECTED" ] || TS_EXPECTED=/dev/null
570
571 # remove libtool lt- prefixes
572 sed --in-place 's/^lt\-\(.*\: \)/\1/g' "$TS_OUTPUT"
573 sed --in-place 's/^lt\-\(.*\: \)/\1/g' "$TS_ERRLOG"
574
575 [ -d "$TS_DIFFDIR" ] || mkdir -p "$TS_DIFFDIR"
576
577 # error log is fully optional
578 [ -f "$TS_EXPECTED_ERR" ] || TS_EXPECTED_ERR=/dev/null
579 [ -f "$TS_ERRLOG" ] || TS_ERRLOG=/dev/null
580
581 if [ "$TS_COMPONENT" != "fuzzers" ]; then
582 ts_gen_diff_from "$TS_EXPECTED" "$TS_OUTPUT" "$TS_DIFF"
583 status_out=$?
584
585 ts_gen_diff_from "$TS_EXPECTED_ERR" "$TS_ERRLOG" "$TS_DIFF.err"
586 status_err=$?
587 else
588 # TS_EXIT_CODE is empty when tests aren't run with ts_run: https://github.com/util-linux/util-linux/issues/1072
589 # or when ts_finalize is called right after ts_finalize_subtest.
590 exit_code="$(cat $TS_EXIT_CODE)"
591 if [ -z "$exit_code" ]; then
592 exit_code=0
593 fi
594
595 if [ $exit_code -ne 0 ]; then
596 ts_gen_diff_from "$TS_EXPECTED" "$TS_OUTPUT" "$TS_DIFF"
597 ts_gen_diff_from "$TS_EXPECTED_ERR" "$TS_ERRLOG" "$TS_DIFF.err"
598 fi
599 fi
600
601 if [ $status_out -ne 0 -o $status_err -ne 0 -o $exit_code -ne 0 ]; then
602 return 1
603 fi
604 return 0
605 }
606
607 function tt_gen_mem_report {
608 if [ -n "$TS_VALGRIND_CMD" ]; then
609 grep -q -E 'ERROR SUMMARY: [1-9]' $TS_VGDUMP &> /dev/null
610 if [ $? -eq 0 ]; then
611 echo "mem-error detected!"
612 fi
613 else
614 echo "$1"
615 fi
616 }
617
618 function ts_finalize_subtest {
619 local res=0
620
621 ts_gen_diff
622 if [ $? -eq 1 ]; then
623 ts_failed_subtest "$1"
624 res=1
625 else
626 ts_report_ok "$(tt_gen_mem_report "$1")"
627 fi
628
629 [ $res -ne 0 ] && TS_NSUBFAILED=$(( $TS_NSUBFAILED + 1 ))
630
631 # reset environment back to parental test
632 ts_init_core_env
633
634 return $res
635 }
636
637 function ts_skip_subtest {
638 ts_report_skip "$1"
639 # reset environment back to parental test
640 ts_init_core_env
641
642 }
643
644 function ts_finalize {
645 ts_cleanup_on_exit
646
647 if [ $TS_NSUBTESTS -ne 0 ]; then
648 if ! ts_gen_diff || [ $TS_NSUBFAILED -ne 0 ]; then
649 ts_failed "$TS_NSUBFAILED from $TS_NSUBTESTS sub-tests"
650 else
651 ts_ok "all $TS_NSUBTESTS sub-tests PASSED"
652 fi
653 fi
654
655 ts_gen_diff || ts_failed "$1"
656 ts_ok "$1"
657 }
658
659 function ts_die {
660 ts_log "$1"
661 ts_finalize
662 }
663
664 function ts_cleanup_on_exit {
665
666 for idx in $(seq 0 $((${#TS_SUID_PROGS[*]} - 1))); do
667 PROG=${TS_SUID_PROGS[$idx]}
668 chmod a-s $PROG &> /dev/null
669 chown ${TS_SUID_USER[$idx]}:${TS_SUID_GROUP[$idx]} $PROG &> /dev/null
670 done
671
672 for dev in "${TS_LOOP_DEVS[@]}"; do
673 ts_device_deinit "$dev"
674 done
675 unset TS_LOOP_DEVS
676
677 ts_scsi_debug_rmmod
678 }
679
680 function ts_image_md5sum {
681 local img=${1:-"$TS_OUTDIR/${TS_TESTNAME}.img"}
682 echo $("$TS_HELPER_MD5" < "$img") $(basename "$img")
683 }
684
685 function ts_image_init {
686 local mib=${1:-"5"} # size in MiBs
687 local img=${2:-"$TS_OUTDIR/${TS_TESTNAME}.img"}
688
689 rm -f $img
690 truncate -s "${mib}M" "$img"
691 echo "$img"
692 return 0
693 }
694
695 function ts_register_loop_device {
696 local ct=${#TS_LOOP_DEVS[*]}
697 TS_LOOP_DEVS[$ct]=$1
698 }
699
700 function ts_device_init {
701 local img
702 local dev
703
704 img=$(ts_image_init $1 $2)
705 dev=$($TS_CMD_LOSETUP --show --partscan -f "$img")
706 if [ "$?" != "0" -o "$dev" = "" ]; then
707 ts_die "Cannot init device"
708 fi
709
710 ts_register_loop_device "$dev"
711 TS_LODEV=$dev
712 }
713
714 # call from ts_cleanup_on_exit() only because of TS_LOOP_DEVS maintenance
715 function ts_device_deinit {
716 local DEV="$1"
717
718 if [ -b "$DEV" ]; then
719 $TS_CMD_UMOUNT "$DEV" &> /dev/null
720 $TS_CMD_LOSETUP -d "$DEV" &> /dev/null
721 fi
722 }
723
724 function ts_blkidtag_by_devname()
725 {
726 local tag=$1
727 local dev=$2
728 local out
729 local rval
730
731 out=$($TS_CMD_BLKID -p -s "$tag" -o value "$dev")
732 rval=$?
733 printf "%s\n" "$out"
734
735 test -n "$out" -a "$rval" = "0"
736 return $?
737 }
738
739 function ts_uuid_by_devname {
740 ts_blkidtag_by_devname "UUID" "$1"
741 return $?
742 }
743
744 function ts_label_by_devname {
745 ts_blkidtag_by_devname "LABEL" "$1"
746 return $?
747 }
748
749 function ts_fstype_by_devname {
750 ts_blkidtag_by_devname "TYPE" "$1"
751 return $?
752 }
753
754 function ts_vfs_dump {
755 if [ "$TS_SHOWDIFF" == "yes" -a "$TS_KNOWN_FAIL" != "yes" ]; then
756 echo
757 echo "{{{{ VFS dump:"
758 findmnt
759 echo "}}}}"
760 fi
761 }
762
763 function ts_blk_dump {
764 if [ "$TS_SHOWDIFF" == "yes" -a "$TS_KNOWN_FAIL" != "yes" ]; then
765 echo
766 echo "{{{{ blkdevs dump:"
767 lsblk -o+FSTYPE
768 echo "}}}}"
769 fi
770 }
771
772 function ts_device_has {
773 local TAG="$1"
774 local VAL="$2"
775 local DEV="$3"
776 local vl=""
777 local res=""
778
779 vl=$(ts_blkidtag_by_devname "$TAG" "$DEV")
780 test $? = 0 -a "$vl" = "$VAL"
781 res=$?
782
783 if [ "$res" != 0 ]; then
784 ts_vfs_dump
785 ts_blk_dump
786 fi
787
788 return $res
789 }
790
791 # Based on __SYSMACROS_DEFINE_MAKEDEV macro
792 # defined in /usr/include/bits/sysmacros.h of GNU libc.
793 function ts_makedev
794 {
795 local major="$1"
796 local minor="$2"
797 local dev
798
799 dev=$(( ( major & 0x00000fff ) << 8))
800 dev=$((dev | ( major & 0xfffff000 ) << 32))
801 dev=$((dev | ( minor & 0x000000ff ) << 0))
802 dev=$((dev | ( minor & 0xffffff00 ) << 12))
803 echo $dev
804 }
805
806 function ts_is_uuid()
807 {
808 printf "%s\n" "$1" | grep -E -q '^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$'
809 return $?
810 }
811
812 function ts_udevadm_settle()
813 {
814 local dev=$1 # optional, might be empty
815 shift # all other args are tags, LABEL, UUID, ...
816 udevadm settle
817 }
818
819 function ts_mount {
820 local out
821 local result
822 local msg
823 local fs
824 local fs_exp=$1
825 shift
826
827 out=$($TS_CMD_MOUNT "$@" 2>&1)
828 result=$?
829 echo -n "$out" >> $TS_OUTPUT
830
831 if [ $result != 0 ] \
832 && msg=$(echo "$out" | grep -m1 "unknown filesystem type")
833 then
834 # skip only if reported fs correctly and if it's not available
835 fs=$(echo "$msg" | sed -n "s/.*type '\(.*\)'$/\1/p")
836 [ "$fs" = "fs_exp" ] \
837 && grep -qe "[[:space:]]${fs}$" /proc/filesystems &>/dev/null \
838 || ts_skip "$msg"
839 fi
840 return $result
841 }
842
843 function ts_is_mounted {
844 local DEV=$(ts_canonicalize "$1")
845
846 grep -q "\(^\| \)$DEV " /proc/mounts && return 0
847
848 if [ "${DEV#/dev/loop/}" != "$DEV" ]; then
849 grep -q "^/dev/loop${DEV#/dev/loop/} " /proc/mounts && return 0
850 fi
851 return 1
852 }
853
854 function ts_fstab_open {
855 echo "# <!-- util-linux test entry" >> "$TS_FSTAB"
856 }
857
858 function ts_fstab_close {
859 echo "# -->" >> "$TS_FSTAB"
860 sync "$TS_FSTAB" 2>/dev/null
861 }
862
863 function ts_fstab_addline {
864 local SPEC="$1"
865 local MNT=${2:-"$TS_MOUNTPOINT"}
866 local FS=${3:-"auto"}
867 local OPT=${4:-"defaults"}
868
869 echo "$SPEC $MNT $FS $OPT 0 0" >> "$TS_FSTAB"
870 }
871
872 function ts_fstab_lock {
873 ts_lock "fstab"
874 }
875
876 function ts_fstab_add {
877 ts_fstab_lock
878 ts_fstab_open
879 ts_fstab_addline $*
880 ts_fstab_close
881 }
882
883 function ts_fstab_clean {
884 ts_have_lock "fstab" || return 0
885 sed --in-place "
886 /# <!-- util-linux/!b
887 :a
888 /# -->/!{
889 N
890 ba
891 }
892 s/# <!-- util-linux.*-->//;
893 /^$/d" "$TS_FSTAB"
894
895 sync "$TS_FSTAB" 2>/dev/null
896 ts_unlock "fstab"
897 }
898
899 function ts_fdisk_clean {
900 local DEVNAME=$1
901
902 # remove non comparable parts of fdisk output
903 if [ -n "${DEVNAME}" ]; then
904 # escape "@" with "\@" in $DEVNAME. This way sed correctly
905 # replaces paths containing "@" characters
906 sed -i -e "s@${DEVNAME//\@/\\\@}@<removed>@;" $TS_OUTPUT $TS_ERRLOG
907 fi
908
909 sed -i \
910 -e 's/Disk identifier:.*/Disk identifier: <removed>/' \
911 -e 's/Created a new partition.*/Created a new partition <removed>./' \
912 -e 's/Created a new .* disklabel .*/Created a new disklabel./' \
913 -e 's/^Device[[:blank:]]*Start/Device Start/' \
914 -e 's/^Device[[:blank:]]*Boot/Device Boot/' \
915 -e 's/Welcome to fdisk.*/Welcome to fdisk <removed>./' \
916 -e 's/typescript file.*/typescript file <removed>./' \
917 -e 's@^\(I/O size (minimum/op.* bytes /\) [1-9][0-9]* @\1 <removed> @' \
918 $TS_OUTPUT $TS_ERRLOG
919 }
920
921
922 # https://stackoverflow.com/questions/41603787/how-to-find-next-available-file-descriptor-in-bash
923 function ts_find_free_fd()
924 {
925 local rco
926 local rci
927 for fd in {3..200}; do
928 rco="$(true 2>/dev/null >&${fd}; echo $?)"
929 rci="$(true 2>/dev/null <&${fd}; echo $?)"
930 if [[ "${rco}${rci}" = "11" ]]; then
931 echo "$fd"
932 return 0
933 fi
934 done
935 return 1
936 }
937
938 function ts_get_lock_fd {
939 local resource=$1
940 local fd
941
942 for fd in "${!TS_LOCKFILE_FD[@]}"; do
943 if [ "${TS_LOCKFILE_FD["$fd"]}" = "$resource" ]; then
944 echo "$fd"
945 return 0
946 fi
947 done
948 return 1
949 }
950
951 function ts_have_lock {
952 local resource=$1
953
954 test "$TS_NOLOCKS" = "yes" && return 0
955 ts_get_lock_fd "$resource" >/dev/null && return 0
956 return 1
957 }
958
959 function ts_lock {
960 local resource="$1"
961 local lockfile="${TS_LOCKDIR}/${resource}.lock"
962 local fd
963
964 if [ "$TS_NOLOCKS" == "yes" ]; then
965 return 0
966 fi
967
968 # Don't lock again
969 fd=$(ts_get_lock_fd "$resource")
970 if [ -n "$fd" ]; then
971 echo "[$$ $TS_TESTNAME] ${resource} already locked!"
972 return 0
973 fi
974
975 fd=$(ts_find_free_fd) || ts_skip "failed to find lock fd"
976
977 eval "exec $fd>$lockfile"
978 flock --exclusive "$fd" || ts_skip "failed to lock $resource"
979
980 TS_LOCKFILE_FD["$fd"]="$resource"
981 ###echo "[$$ $TS_TESTNAME] Locked $resource"
982 }
983
984 function ts_unlock {
985 local resource="$1"
986 local lockfile="${TS_LOCKDIR}/${resource}.lock"
987 local fd
988
989 if [ "$TS_NOLOCKS" == "yes" ]; then
990 return 0
991 fi
992
993 fd=$(ts_get_lock_fd "$resource")
994 if [ -n "$fd" ]; then
995 eval "exec $fd<&-"
996 TS_LOCKFILE_FD["$fd"]=""
997 ###echo "[$$ $TS_TESTNAME] Unlocked $resource"
998 else
999 echo "[$$ $TS_TESTNAME] unlocking unlocked $resource!?"
1000 fi
1001 }
1002
1003 function ts_scsi_debug_init {
1004 local devname
1005 local t
1006 TS_DEVICE="none"
1007
1008 ts_lock "scsi_debug"
1009
1010 # dry run is not really reliable, real modprobe may still fail
1011 modprobe --dry-run --quiet scsi_debug &>/dev/null \
1012 || ts_skip "missing scsi_debug module (dry-run)"
1013
1014 # skip if still in use or removal of modules not supported at all
1015 # We don't want a slow timeout here so we don't use ts_scsi_debug_rmmod!
1016 modprobe -r scsi_debug &>/dev/null
1017 if [ "$?" -eq 1 ]; then
1018 ts_unlock "scsi_debug"
1019 ts_skip "cannot remove scsi_debug module (rmmod)"
1020 fi
1021
1022 modprobe -b scsi_debug "$@" &>/dev/null \
1023 || ts_skip "cannot load scsi_debug module (modprobe)"
1024
1025 # it might be still not loaded, modprobe.conf or whatever
1026 lsmod 2>/dev/null | grep -q "^scsi_debug " \
1027 || ts_skip "scsi_debug module not loaded (lsmod)"
1028
1029 udevadm settle
1030
1031 # wait for device if udevadm settle does not work
1032 for t in 0 0.02 0.05 0.1 1; do
1033 sleep $t
1034 devname=$(grep --no-messages --with-filename scsi_debug /sys/block/*/device/model) && break
1035 done
1036 [ -n "${devname}" ] || ts_skip "timeout waiting for scsi_debug device"
1037
1038 devname=$(echo $devname | awk -F '/' '{print $4}')
1039 TS_DEVICE="/dev/${devname}"
1040
1041 # TODO validate that device is really up, for now just a warning on stderr
1042 test -b $TS_DEVICE || echo "warning: scsi_debug device is still down" >&2
1043 }
1044
1045 # automatically called once in ts_cleanup_on_exit()
1046 function ts_scsi_debug_rmmod {
1047 local err=1
1048 local t
1049 local lastmsg
1050
1051 # We must not run if we don't have the lock
1052 ts_have_lock "scsi_debug" || return 0
1053
1054 # Return early most importantly in case we are not root or the module does
1055 # not exist at all.
1056 [ $UID -eq 0 ] || return 0
1057 [ -n "$TS_DEVICE" ] || return 0
1058 lsmod 2>/dev/null | grep -q "^scsi_debug " || return 0
1059
1060 udevadm settle
1061
1062 # wait for successful rmmod if udevadm settle does not work
1063 for t in 0 0.02 0.05 0.1 1; do
1064 sleep $t
1065 lastmsg="$(modprobe -r scsi_debug 2>&1)" && err=0 && break
1066 done
1067
1068 if [ "$err" = "1" ]; then
1069 ts_log "rmmod failed: '$lastmsg'"
1070 ts_log "timeout removing scsi_debug module (rmmod)"
1071 return 1
1072 fi
1073 if lsmod | grep -q "^scsi_debug "; then
1074 ts_log "BUG! scsi_debug still loaded"
1075 return 1
1076 fi
1077
1078 # TODO Do we need to validate that all devices are gone?
1079 udevadm settle
1080 test -b "$TS_DEVICE" && echo "warning: scsi_debug device is still up" >&2
1081
1082 # TODO unset TS_DEVICE, check that nobody uses it later, e.g. ts_fdisk_clean
1083
1084 ts_unlock "scsi_debug"
1085 return 0
1086 }
1087
1088 function ts_resolve_host {
1089 local host="$1"
1090 local tmp
1091
1092 # currently we just resolve default records (might be "A", ipv4 only)
1093 if type "dig" >/dev/null 2>&1; then
1094 tmp=$(dig "$host" +short 2>/dev/null) || return 1
1095 elif type "nslookup" >/dev/null 2>&1; then
1096 tmp=$(nslookup "$host" 2>/dev/null) || return 1
1097 tmp=$(echo "$tmp"| grep -A1 "^Name:"| grep "^Address:"| cut -d" " -f2)
1098 elif type "host" >/dev/null 2>&1; then
1099 tmp=$(host "$host" 2>/dev/null) || return 1
1100 tmp=$(echo "$tmp" | grep " has address " | cut -d " " -f4)
1101 elif type "getent" >/dev/null 2>&1; then
1102 tmp=$(getent ahosts "$host" 2>/dev/null) || return 1
1103 tmp=$(echo "$tmp" | cut -d " " -f 1 | sort -u)
1104 fi
1105
1106 # we return 1 if tmp is empty
1107 test -n "$tmp" || return 1
1108 echo "$tmp" | sort -R | head -n 1
1109 }
1110
1111 # listen to unix socket (background socat)
1112 function ts_init_socket_to_file {
1113 local socket=$1
1114 local outfile=$2
1115 local pid="0"
1116
1117 ts_check_prog "socat"
1118 rm -f "$socket" "$outfile"
1119
1120 # if socat is too old for these options we'll skip it below
1121 socat -u UNIX-LISTEN:$socket,fork,max-children=1,backlog=128 \
1122 STDOUT > "$outfile" 2>/dev/null &
1123 pid=$!
1124
1125 # check for running background process
1126 if [ "$pid" -le "0" ] || ! kill -s 0 "$pid" &>/dev/null; then
1127 ts_skip "unable to run socat"
1128 fi
1129 # wait for the socket listener
1130 if ! socat -u /dev/null UNIX-CONNECT:$socket,retry=30,interval=0.1 &>/dev/null; then
1131 kill -9 "$pid" &>/dev/null
1132 ts_skip "timeout waiting for socat socket"
1133 fi
1134 # check socket again
1135 if ! socat -u /dev/null UNIX-CONNECT:$socket &>/dev/null; then
1136 kill -9 "$pid" &>/dev/null
1137 ts_skip "socat socket stopped listening"
1138 fi
1139 }
1140
1141 function ts_has_ncurses_support {
1142 grep -q '#define HAVE_LIBNCURSES' ${top_builddir}/config.h
1143 if [ $? == 0 ]; then
1144 echo "yes"
1145 else
1146 echo "no"
1147 fi
1148 }
1149
1150 # Get path to the ASan runtime DSO the given binary was compiled with
1151 function ts_get_asan_rt_path {
1152 local binary="${1?}"
1153 local rt_path
1154
1155 ts_check_prog "ldd"
1156 ts_check_prog "awk"
1157
1158 rt_path="$(ldd "$binary" | awk '/lib.+asan.*.so/ {print $3; exit}')"
1159 if [ -n "$rt_path" -a -f "$rt_path" ]; then
1160 echo "$rt_path"
1161 fi
1162 }
1163
1164 function ts_skip_exitcode_not_supported {
1165 if [ $? -eq $TS_EXIT_NOTSUPP ]; then
1166 ts_skip "functionality not implemented by system"
1167 fi
1168 }
1169
1170 function ts_inhibit_custom_colorscheme {
1171 export XDG_CONFIG_HOME=/dev/null
1172 }
1173
1174 function ts_is_virt {
1175 type "systemd-detect-virt" >/dev/null 2>&1
1176 if [ $? -ne 0 ]; then
1177 return 1
1178 fi
1179
1180 virt="$(systemd-detect-virt)"
1181 for arg in "$@"; do
1182 if [ "$virt" = "$arg" ]; then
1183 return 0;
1184 fi
1185 done
1186 return 1
1187 }
1188
1189 function ts_check_enosys_syscalls {
1190 ts_check_test_command "$TS_CMD_ENOSYS"
1191 "$TS_CMD_ENOSYS" ${@/#/-s } true 2> /dev/null
1192 [ $? -ne 0 ] && ts_skip "test_enosys does not work: $*"
1193 }
1194
1195 function ts_skip_docker {
1196 test -e /.dockerenv && ts_skip "unsupported in docker environment"
1197 }