btrfs-progs: check/lowmem: Fix the incorrect error message of check_extent_data_item
[platform/upstream/btrfs-progs.git] / tests / common
1 #!/bin/bash
2 #
3 # Common routines for all tests
4 #
5
6 # assert that argument is not empty and is an existing path (file or directory)
7 _assert_path()
8 {
9         local path
10
11         path="$1"
12         if [ -z "$path" ]; then
13                 echo "ASSERTION FAIL: $path is not valid"
14                 exit 1
15         fi
16
17         if [ -f "$path" -o -d "$path" -o -b "$path" ]; then
18                 return 0
19         fi
20         echo "ASSERTION FAIL: $path is not valid"
21         exit 1
22 }
23
24 # $1: this string gets matched to files, absolute or relative path, or a
25 # systemwide command available via $PATH
26 _is_file_or_command()
27 {
28         local msg
29
30         msg="$1"
31         if [ -z "$msg" ]; then
32                 return 1
33         fi
34
35         if [ -f "$msg" -o -d "$msg" -o -b "$msg" ]; then
36                 return 0
37         fi
38         msg=$(type -p -- "$msg")
39         if [ -f "$msg" ]; then
40                 return 0
41         fi
42         return 1
43 }
44
45 _fail()
46 {
47         echo "$*" | tee -a "$RESULTS"
48         exit 1
49 }
50
51 # log a message to the results file
52 _log()
53 {
54         echo "$*" | tee -a "$RESULTS"
55 }
56
57 # copy stdout to log and pass to stdout, eg. another stdout consumer, commands
58 # should redirect stderr to stdout if this is consmed by further commands
59 _log_stdout()
60 {
61         tee -a "$RESULTS"
62 }
63
64 _not_run()
65 {
66         echo "    [NOTRUN] $*"
67         exit 0
68 }
69
70 # debugging helper
71 _dump_args()
72 {
73         local i
74
75         i=1
76         echo "DUMP args for ${FUNCNAME[1]}:"
77         while [ $# -gt 0 ]; do
78                 echo "ARG[$i]: $1"
79                 i=$(($i+1))
80                 shift
81         done
82 }
83
84 # read arguments, look if we're calling btrfs and if there's a known
85 # subcommand, return argument index to insert, taking root helper into
86 # consideration, returns 2 for unknown subcommand
87 _get_spec_ins()
88 {
89         if [ "$1" = 'root_helper' ]; then
90                 if [[ $2 =~ /btrfs$ ]]; then
91                         echo -n 4
92                         return
93                 fi
94         else
95                 if [[ $1 =~ /btrfs$ ]]; then
96                         echo -n 3
97                         return
98                 fi
99         fi
100         echo -n 2
101 }
102
103 # return command-specific arguments if enabled
104 _cmd_spec()
105 {
106         if [ "$TEST_ENABLE_OVERRIDE" = 'true' ]; then
107                 # if defined via common.local, use it, otherwise pass make
108                 # arguments
109                 if [ "$(type -t _skip_spec)" = 'function' ]; then
110                         if _skip_spec "$@"; then
111                                 return
112                         fi
113                 fi
114                 case "$1" in
115                         check) echo -n "$TEST_ARGS_CHECK" ;;
116                 esac
117         fi
118 }
119
120 # Argument passing magic:
121 # the command passed to run_* helpers is inspected, if there's 'btrfs command'
122 # found and there are defined additional arguments, they're inserted just after
123 # the command name, ie. any arguments in the test could override them.
124 #
125 # The root helper is recognized.  Unrecognized subcommands or external tools
126 # are not affected.
127
128 run_check()
129 {
130         local spec
131         local ins
132         local cmd
133
134         ins=$(_get_spec_ins "$@")
135         spec=$(($ins-1))
136         cmd=$(eval echo "\${$spec}")
137         spec=$(_cmd_spec "${@:$spec}")
138         set -- "${@:1:$(($ins-1))}" $spec "${@: $ins}"
139         echo "############### $@" >> "$RESULTS" 2>&1
140         if [[ $TEST_LOG =~ tty ]]; then echo "CMD: $@" > /dev/tty; fi
141         if [ "$1" = 'root_helper' ]; then
142                 "$@" >> "$RESULTS" 2>&1 || _fail "failed: $@"
143         else
144                 $INSTRUMENT "$@" >> "$RESULTS" 2>&1 || _fail "failed: $@"
145         fi
146 }
147
148 # same as run_check but the stderr+stdout output is duplicated on stdout and
149 # can be processed further
150 run_check_stdout()
151 {
152         local spec
153         local ins
154         local cmd
155
156         ins=$(_get_spec_ins "$@")
157         spec=$(($ins-1))
158         cmd=$(eval echo "\${$spec}")
159         spec=$(_cmd_spec "${@:$spec}")
160         set -- "${@:1:$(($ins-1))}" $spec "${@: $ins}"
161         echo "############### $@" >> "$RESULTS" 2>&1
162         if [[ $TEST_LOG =~ tty ]]; then echo "CMD(stdout): $@" > /dev/tty; fi
163         if [ "$1" = 'root_helper' ]; then
164                 "$@" 2>&1 | tee -a "$RESULTS"
165         else
166                 $INSTRUMENT "$@" 2>&1 | tee -a "$RESULTS"
167         fi
168         if [ ${PIPESTATUS[0]} -ne 0 ]; then
169                 _fail "failed: $@"
170         fi
171 }
172
173 # same as run_check but does not fail the test if it's handled gracefully by
174 # the tool, unexpected failure like segfault or abor will exit forcibly
175 # output is logged
176 run_mayfail()
177 {
178         local spec
179         local ins
180         local cmd
181         local ret
182
183         ins=$(_get_spec_ins "$@")
184         spec=$(($ins-1))
185         cmd=$(eval echo "\${$spec}")
186         spec=$(_cmd_spec "${@:$spec}")
187         set -- "${@:1:$(($ins-1))}" $spec "${@: $ins}"
188         echo "############### $@" >> "$RESULTS" 2>&1
189         if [[ $TEST_LOG =~ tty ]]; then echo "CMD(mayfail): $@" > /dev/tty; fi
190         if [ "$1" = 'root_helper' ]; then
191                 "$@" >> "$RESULTS" 2>&1
192         else
193                 $INSTRUMENT "$@" >> "$RESULTS" 2>&1
194         fi
195         ret=$?
196         if [ $ret != 0 ]; then
197                 echo "failed (ignored, ret=$ret): $@" >> "$RESULTS"
198                 if [ $ret == 139 ]; then
199                         _fail "mayfail: returned code 139 (SEGFAULT), not ignored"
200                 elif [ $ret == 134 ]; then
201                         _fail "mayfail: returned code 134 (SIGABRT), not ignored"
202                 fi
203                 return $ret
204         fi
205 }
206
207 # first argument is error message to print if it fails, otherwise
208 # same as run_check but expects the command to fail, output is logged
209 run_mustfail()
210 {
211         local spec
212         local ins
213         local cmd
214         local msg
215
216         msg="$1"
217         shift
218
219         if _is_file_or_command "$msg"; then
220                 echo "ASSERTION FAIL: 1st argument of run_mustfail must be a message"
221                 exit 1
222         fi
223
224         ins=$(_get_spec_ins "$@")
225         spec=$(($ins-1))
226         cmd=$(eval echo "\${$spec}")
227         spec=$(_cmd_spec "${@:$spec}")
228         set -- "${@:1:$(($ins-1))}" $spec "${@: $ins}"
229         echo "############### $@" >> "$RESULTS" 2>&1
230         if [[ $TEST_LOG =~ tty ]]; then echo "CMD(mustfail): $@" > /dev/tty; fi
231         if [ "$1" = 'root_helper' ]; then
232                 "$@" >> "$RESULTS" 2>&1
233         else
234                 $INSTRUMENT "$@" >> "$RESULTS" 2>&1
235         fi
236         if [ $? != 0 ]; then
237                 echo "failed (expected): $@" >> "$RESULTS"
238                 return 0
239         else
240                 echo "succeeded (unexpected!): $@" >> "$RESULTS"
241                 _fail "unexpected success: $msg"
242                 return 1
243         fi
244 }
245
246 # The first parameter is error message to print if it fails, just like
247 # run_must_fail().
248 # NOTE: we don't use pipefail to avoid disturbing other script, so here we
249 # use a temporary output file.
250 # So it doesn't support pipeline in the @cmd
251 run_mustfail_stdout()
252 {
253         local spec
254         local ins
255         local cmd
256         local msg
257         local ret
258         local tmp_output
259
260         tmp_output=$(mktemp --tmpdir btrfs-progs-test--mustfail-stdtout.XXXXXX)
261
262         msg="$1"
263         shift
264
265         if _is_file_or_command "$msg"; then
266                 echo "ASSERTION FAIL: 1st argument of run_mustfail_stdout must be a message"
267                 exit 1
268         fi
269
270         ins=$(_get_spec_ins "$@")
271         spec=$(($ins-1))
272         cmd=$(eval echo "\${$spec}")
273         spec=$(_cmd_spec "${@:$spec}")
274         set -- "${@:1:$(($ins-1))}" $spec "${@: $ins}"
275         echo "############### $@" >> "$RESULTS" 2>&1
276         if [[ $TEST_LOG =~ tty ]]; then echo "CMD(mustfail): $@" > /dev/tty; fi
277         if [ "$1" = 'root_helper' ]; then
278                 "$@" 2>&1 > "$tmp_output"
279         else
280                 $INSTRUMENT "$@" 2>&1 > "$tmp_output"
281         fi
282         ret=$?
283
284         cat "$tmp_output" >> "$RESULTS"
285         cat "$tmp_output"
286         rm "$tmp_output"
287
288         if [ "$ret" != 0 ]; then
289                 echo "failed (expected): $@" >> "$RESULTS"
290                 return 0
291         else
292                 echo "succeeded (unexpected!): $@" >> "$RESULTS"
293                 _fail "unexpected success: $msg"
294                 return 1
295         fi
296 }
297
298 check_prereq()
299 {
300         if [ "$1" = "btrfs-corrupt-block" -o "$1" = "fssum" ]; then
301                 if ! [ -f "$INTERNAL_BIN/$1" ]; then
302                         _fail "Failed prerequisites: $INTERNAL_BIN/$1";
303                 fi
304         elif ! [ -f "$TOP/$1" ]; then
305                 _fail "Failed prerequisites: $TOP/$1";
306         fi
307 }
308
309 check_global_prereq()
310 {
311         which $1 &> /dev/null
312         if [ $? -ne 0 ]; then
313                 _fail "Failed system wide prerequisities: $1";
314         fi
315 }
316
317 check_image()
318 {
319         local image
320
321         image=$1
322         echo "testing image $(basename $image)" >> "$RESULTS"
323         "$TOP/btrfs" check "$image" >> "$RESULTS" 2>&1
324         [ $? -eq 0 ] && _fail "btrfs check should have detected corruption"
325
326         run_check "$TOP/btrfs" check --repair "$image"
327         run_check "$TOP/btrfs" check "$image"
328 }
329
330 # Extract a usable image from packed formats
331 # - raw btrfs filesystem images, suffix .raw
332 # - dtto compressed by XZ, suffix .raw.xz
333 # - meta-dump images with suffix .img
334 # - dtto compressed by XZ, suffix .img.xz
335 # - compressed send stream, .stream.xz
336 extract_image()
337 {
338         local image
339         local cleanme
340
341         image="$1"
342         case "$image" in
343         *.img)
344                 rm -f "$image.restored"
345                 ;;
346         *.img.xz)
347                 xz --decompress --keep "$image" || \
348                         _fail "failed to decompress image $image" >&2
349                 image=${image%%.xz}
350                 rm -f "$image.restored"
351                 cleanme=$image
352                 ;;
353         *.raw)
354                 cp --sparse=auto "$image" "$image.restored"
355                 ;;
356         *.raw.xz)
357                 xz --decompress --keep "$image" || \
358                         _fail "failed to decompress image $image" >&2
359                 image=${image%%.xz}
360                 mv "$image" "$image.restored"
361                 ;;
362         *.stream.xz)
363                 xz --decompress --keep "$image" || \
364                         _fail "failed to decompress file $image" >&2
365                 image=${image%%.xz}
366                 mv "$image" "$image.restored"
367                 ;;
368         esac
369
370         if ! [ -f "$image.restored" ]; then
371                 echo "restoring image $(basename $image)" >> "$RESULTS"
372                 "$TOP/btrfs-image" -r "$image" "$image.restored" \
373                         &>> "$RESULTS" \
374                         || _fail "failed to restore image $image" >&2
375         fi
376
377         [ -f "$cleanme" ] && rm -f "$cleanme"
378
379         echo "$image.restored"
380 }
381
382 # Process all image dumps in a given directory
383 check_all_images()
384 {
385         local dir
386         local extracted
387
388         dir="$1"
389         if [ -z "$dir" ]; then
390                 dir=.
391         fi
392         _assert_path "$dir"
393         for image in $(find "$dir" \( -iname '*.img' -o \
394                                 -iname '*.img.xz' -o    \
395                                 -iname '*.raw' -o       \
396                                 -iname '*.raw.xz' \) | sort)
397         do
398                 extracted=$(extract_image "$image")
399                 check_image "$extracted"
400                 rm -f "$extracted"
401         done
402 }
403
404 # some tests need to mount the recovered image and do verifications call
405 # 'setup_root_helper' and then check for have_root_helper == 1 if the test
406 # needs to fail otherwise; using sudo by default for now
407 SUDO_HELPER=
408 NEED_SUDO_VALIDATE=unknown
409 export SUDO_HELPER
410 export NEED_SUDO_VALIDATE
411 root_helper()
412 {
413         if [ $UID -eq 0 ]; then
414                 "$@"
415         else
416                 if [ "$NEED_SUDO_VALIDATE" = 'yes' ]; then
417                         sudo -v -n &>/dev/null || \
418                                 _not_run "Need to validate sudo credentials"
419                         sudo -n "$@"
420                 elif [ "$NEED_SUDO_VALIDATE" = 'no' ]; then
421                         sudo -n /bin/true &> /dev/null || \
422                                 _not_run "Need to validate sudo user settings"
423                         sudo -n "$@"
424                 else
425                         # should not happen
426                         _not_run "Need to validate root privileges"
427                 fi
428         fi
429 }
430
431 setup_root_helper()
432 {
433         if [ $UID -eq 0 -o -n "$SUDO_HELPER" ]; then
434                 return
435         fi
436
437         # Test for old sudo or special settings, which make sudo -v fail even
438         # if user setting is NOPASSWD
439         sudo -n /bin/true &>/dev/null && NEED_SUDO_VALIDATE=no
440
441         # Newer sudo or default sudo setting
442         sudo -v -n &>/dev/null && NEED_SUDO_VALIDATE=yes
443
444         if [ "$NEED_SUDO_VALIDATE" = 'unknown' ]; then
445                 _not_run "Need to validate root privileges"
446         fi
447         SUDO_HELPER=root_helper
448 }
449
450 prepare_test_dev()
451 {
452         # num[K/M/G/T...]
453         local size="$1"
454
455         [[ "$size" ]] || size='2G'
456         # Still truncate it to new size
457         if [ -n "$TEST_DEV" ]; then
458                 truncate -s 0 "$TEST_DEV"
459                 truncate -s "$size" "$TEST_DEV"
460                 return;
461         fi
462
463         echo "\$TEST_DEV not given, using $TEST_TOP/test.img as fallback" >> \
464                 "$RESULTS"
465         TEST_DEV="$TEST_TOP/test.img"
466
467         truncate -s 0 "$TEST_DEV"
468         truncate -s "$size" "$TEST_DEV" || _not_run "create file for loop device failed"
469 }
470
471 run_check_mount_test_dev()
472 {
473         setup_root_helper
474
475         local loop_opt
476         if [[ -b "$TEST_DEV" ]]; then
477                 loop_opt=""
478         elif [[ -f "$TEST_DEV" ]]; then
479                 loop_opt="-o loop"
480         else
481                 _fail "Invalid \$TEST_DEV: $TEST_DEV"
482         fi
483
484         [[ -d "$TEST_MNT" ]] || {
485                 _fail "Invalid \$TEST_MNT: $TEST_MNT"
486         }
487
488         run_check $SUDO_HELPER mount -t btrfs $loop_opt "$@" "$TEST_DEV" "$TEST_MNT"
489 }
490
491 # $1-$n: optional paths to unmount, otherwise fallback to TEST_DEV
492 run_check_umount_test_dev()
493 {
494         setup_root_helper
495         if [ "$#" = 0 ]; then
496                 set -- "$TEST_DEV"
497         fi
498         run_check $SUDO_HELPER umount "$@"
499 }
500
501 check_kernel_support()
502 {
503         if ! grep -iq 'btrfs' /proc/filesystems; then
504                 run_check $SUDO_HELPER modprobe btrfs
505                         if ! grep -iq 'btrfs' /proc/filesystems; then
506                                 echo \
507 "WARNING: btrfs filesystem not found in /proc/filesystems, some tests might fail"
508                                 return 1
509                         fi
510         fi
511         return 0
512 }
513
514 # how many files to create.
515 DATASET_SIZE=50
516
517 generate_dataset() {
518
519         dataset_type="$1"
520         dirpath=$TEST_MNT/$dataset_type
521         run_check $SUDO_HELPER mkdir -p "$dirpath"
522
523         case "$dataset_type" in
524                 small)
525                         for num in $(seq 1 "$DATASET_SIZE"); do
526                                 run_check $SUDO_HELPER dd if=/dev/urandom of="$dirpath/$dataset_type.$num" bs=10K \
527                                 count=1 >/dev/null 2>&1
528                         done
529                         ;;
530
531                 hardlink)
532                         for num in $(seq 1 "$DATASET_SIZE"); do
533                                 run_check $SUDO_HELPER touch "$dirpath/$dataset_type.$num"
534                                 run_check $SUDO_HELPER ln "$dirpath/$dataset_type.$num" "$dirpath/hlink.$num"
535                         done
536                         ;;
537
538                 fast_symlink)
539                         for num in $(seq 1 "$DATASET_SIZE"); do
540                                 run_check $SUDO_HELPER touch "$dirpath/$dataset_type.$num"
541                                 run_check cd "$dirpath" && \
542                                         $SUDO_HELPER ln -s "$dataset_type.$num" "$dirpath/slink.$num" && \
543                                         cd /
544                         done
545                         ;;
546
547                 brokenlink)
548                         for num in $(seq 1 "$DATASET_SIZE"); do
549                                 run_check $SUDO_HELPER ln -s "$dirpath/$dataset_type.$num" "$dirpath/blink.$num"
550                         done
551                         ;;
552
553                 perm)
554                         for modes in 777 775 755 750 700 666 664 644 640 600 444 440 400 000            \
555                                 1777 1775 1755 1750 1700 1666 1664 1644 1640 1600 1444 1440 1400 1000   \
556                                 2777 2775 2755 2750 2700 2666 2664 2644 2640 2600 2444 2440 2400 2000   \
557                                 4777 4775 4755 4750 4700 4666 4664 4644 4640 4600 4444 4440 4400 4000; do
558                                 run_check $SUDO_HELPER touch "$dirpath/$dataset_type.$modes"
559                                 run_check $SUDO_HELPER chmod "$modes" "$dirpath/$dataset_type.$modes"
560                         done
561                         ;;
562
563                 sparse)
564                         for num in $(seq 1 "$DATASET_SIZE"); do
565                                 run_check $SUDO_HELPER dd if=/dev/urandom of="$dirpath/$dataset_type.$num" bs=10K \
566                                 count=1 >/dev/null 2>&1
567                                 run_check $SUDO_HELPER truncate -s 500K "$dirpath/$dataset_type.$num"
568                                 run_check $SUDO_HELPER dd if=/dev/urandom of="$dirpath/$dataset_type.$num" bs=10K \
569                                 oflag=append conv=notrunc count=1 >/dev/null 2>&1
570                                 run_check $SUDO_HELPER truncate -s 800K "$dirpath/$dataset_type.$num"
571                         done
572                         ;;
573
574                 acls)
575                         for num in $(seq 1 "$DATASET_SIZE"); do
576                                 run_check $SUDO_HELPER touch "$dirpath/$dataset_type.$num"
577                                 run_check $SUDO_HELPER setfacl -m "u:root:x" "$dirpath/$dataset_type.$num"
578                                 run_check $SUDO_HELPER setfattr -n user.foo -v "bar$num" "$dirpath/$dataset_type.$num"
579                         done
580                         ;;
581
582                 fifo)
583                         for num in $(seq 1 "$DATASET_SIZE"); do
584                                 run_check $SUDO_HELPER mkfifo "$dirpath/$dataset_type.$num"
585                         done
586                         ;;
587
588                 slow_symlink)
589                         long_filename=`date +%s | sha256sum | cut -f1 -d ' '`
590                         run_check $SUDO_HELPER touch "$dirpath/$long_filename"
591                         for num in $(seq 1 "$DATASET_SIZE"); do
592                                 run_check $SUDO_HELPER ln -s "$dirpath/$long_filename" "$dirpath/slow_slink.$num"
593                         done
594                         ;;
595                 large)
596                         run_check $SUDO_HELPER dd if=/dev/urandom bs=32M count=1 \
597                                 of="$dirpath/$dataset_type" >/dev/null 2>&1
598                         ;;
599         esac
600 }
601
602 # prepare environment for loop devices, set up the following variables
603 # - nloopdevs -- number of desired devices
604 # - loopdevs  -- array containing paths to all devices (after prepare is called)
605 # - loopdev_prefix -- file backed images starting with this string, 'img' by default
606 #
607 # $1: number of loop devices to be set up
608 setup_loopdevs()
609 {
610         if [ -z "$1" ]; then
611                 _fail "setup_loopdevs needs a number"
612         fi
613         nloopdevs="$1"
614         loopdev_prefix=img
615         declare -a loopdevs
616
617 }
618
619 # create all loop devices from a given loopdev environment
620 prepare_loopdevs()
621 {
622         for i in `seq $nloopdevs`; do
623                 touch $loopdev_prefix$i
624                 chmod a+rw $loopdev_prefix$i
625                 truncate -s0 $loopdev_prefix$i
626                 truncate -s2g $loopdev_prefix$i
627                 loopdevs[$i]=`run_check_stdout $SUDO_HELPER losetup --find --show $loopdev_prefix$i`
628         done
629 }
630
631 # detach loop devices and reset their size to 0, delete the files afterwards
632 cleanup_loopdevs()
633 {
634         for dev in ${loopdevs[@]}; do
635                 run_check $SUDO_HELPER losetup -d $dev
636         done
637         for i in `seq $nloopdevs`; do
638                 truncate -s0 $loopdev_prefix$i
639                 rm -- "$loopdev_prefix$i"
640         done
641         run_check $SUDO_HELPER losetup --all
642 }
643
644 init_env()
645 {
646         TEST_MNT="${TEST_MNT:-$TEST_TOP/mnt}"
647         export TEST_MNT
648         mkdir -p "$TEST_MNT" || { echo "Failed mkdir -p $TEST_MNT"; exit 1; }
649
650         source $TEST_TOP/common.local
651
652         if [ "$TEST_ENABLE_OVERRIDE" = 'true' -a -n "$RESULTS" ]; then
653                 echo "INCLUDE common.local" >> "$RESULTS"
654                 echo "  check: $TEST_ARGS_CHECK" >> "$RESULTS"
655         fi
656 }
657 init_env