btrfs-progs: tests: fssum, fix memory leak
[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 _fail()
25 {
26         echo "$*" | tee -a "$RESULTS"
27         exit 1
28 }
29
30 # log a message to the results file
31 _log()
32 {
33         echo "$*" | tee -a "$RESULTS"
34 }
35
36 _not_run()
37 {
38         echo "    [NOTRUN] $*"
39         exit 0
40 }
41
42 # debugging helper
43 _dump_args()
44 {
45         local i
46
47         i=1
48         echo "DUMP args for ${FUNCNAME[1]}:"
49         while [ $# -gt 0 ]; do
50                 echo "ARG[$i]: $1"
51                 i=$(($i+1))
52                 shift
53         done
54 }
55
56 # read arguments, look if we're calling btrfs and if there's a known
57 # subcommand, return argument index to insert, taking root helper into
58 # consideration, returns 2 for unknown subcommand
59 _get_spec_ins()
60 {
61         if [ "$1" = 'root_helper' ]; then
62                 if [[ $2 =~ /btrfs$ ]]; then
63                         echo -n 4
64                         return
65                 fi
66         else
67                 if [[ $1 =~ /btrfs$ ]]; then
68                         echo -n 3
69                         return
70                 fi
71         fi
72         echo -n 2
73 }
74
75 # return command-specific arguments if enabled
76 _cmd_spec()
77 {
78         if [ "$TEST_ENABLE_OVERRIDE" = 'true' ]; then
79                 # if defined via common.local, use it, otherwise pass make
80                 # arguments
81                 if [ "$(type -t _skip_spec)" = 'function' ]; then
82                         if _skip_spec "$@"; then
83                                 return
84                         fi
85                 fi
86                 case "$1" in
87                         check) echo -n "$TEST_ARGS_CHECK" ;;
88                 esac
89         fi
90 }
91
92 # Argument passing magic:
93 # the command passed to run_* helpers is inspected, if there's 'btrfs command'
94 # found and there are defined additional arguments, they're inserted just after
95 # the command name, ie. any arguments in the test could override them.
96 #
97 # The root helper is recognized.  Unrecognized subcommands or external tools
98 # are not affected.
99
100 run_check()
101 {
102         local spec
103         local ins
104         local cmd
105
106         ins=$(_get_spec_ins "$@")
107         spec=$(($ins-1))
108         cmd=$(eval echo "\${$spec}")
109         spec=$(_cmd_spec "${@:$spec}")
110         set -- "${@:1:$(($ins-1))}" $spec "${@: $ins}"
111         echo "############### $@" >> "$RESULTS" 2>&1
112         if [[ $TEST_LOG =~ tty ]]; then echo "CMD: $@" > /dev/tty; fi
113         if [ "$1" = 'root_helper' ]; then
114                 "$@" >> "$RESULTS" 2>&1 || _fail "failed: $@"
115         else
116                 $INSTRUMENT "$@" >> "$RESULTS" 2>&1 || _fail "failed: $@"
117         fi
118 }
119
120 # same as run_check but the stderr+stdout output is duplicated on stdout and
121 # can be processed further
122 run_check_stdout()
123 {
124         local spec
125         local ins
126         local cmd
127
128         ins=$(_get_spec_ins "$@")
129         spec=$(($ins-1))
130         cmd=$(eval echo "\${$spec}")
131         spec=$(_cmd_spec "${@:$spec}")
132         set -- "${@:1:$(($ins-1))}" $spec "${@: $ins}"
133         echo "############### $@" >> "$RESULTS" 2>&1
134         if [[ $TEST_LOG =~ tty ]]; then echo "CMD(stdout): $@" > /dev/tty; fi
135         if [ "$1" = 'root_helper' ]; then
136                 "$@" 2>&1 | tee -a "$RESULTS" || _fail "failed: $@"
137         else
138                 $INSTRUMENT "$@" 2>&1 | tee -a "$RESULTS" || _fail "failed: $@"
139         fi
140 }
141
142 # same as run_check but does not fail the test if it's handled gracefully by
143 # the tool, unexpected failure like segfault or abor will exit forcibly
144 # output is logged
145 run_mayfail()
146 {
147         local spec
148         local ins
149         local cmd
150         local ret
151
152         ins=$(_get_spec_ins "$@")
153         spec=$(($ins-1))
154         cmd=$(eval echo "\${$spec}")
155         spec=$(_cmd_spec "${@:$spec}")
156         set -- "${@:1:$(($ins-1))}" $spec "${@: $ins}"
157         echo "############### $@" >> "$RESULTS" 2>&1
158         if [[ $TEST_LOG =~ tty ]]; then echo "CMD(mayfail): $@" > /dev/tty; fi
159         if [ "$1" = 'root_helper' ]; then
160                 "$@" >> "$RESULTS" 2>&1
161         else
162                 $INSTRUMENT "$@" >> "$RESULTS" 2>&1
163         fi
164         ret=$?
165         if [ $ret != 0 ]; then
166                 echo "failed (ignored, ret=$ret): $@" >> "$RESULTS"
167                 if [ $ret == 139 ]; then
168                         _fail "mayfail: returned code 139 (SEGFAULT), not ignored"
169                 elif [ $ret == 134 ]; then
170                         _fail "mayfail: returned code 134 (SIGABRT), not ignored"
171                 fi
172                 return $ret
173         fi
174 }
175
176 # first argument is error message to print if it fails, otherwise
177 # same as run_check but expects the command to fail, output is logged
178 run_mustfail()
179 {
180         local spec
181         local ins
182         local cmd
183         local msg
184
185         msg="$1"
186         shift
187
188         ins=$(_get_spec_ins "$@")
189         spec=$(($ins-1))
190         cmd=$(eval echo "\${$spec}")
191         spec=$(_cmd_spec "${@:$spec}")
192         set -- "${@:1:$(($ins-1))}" $spec "${@: $ins}"
193         echo "############### $@" >> "$RESULTS" 2>&1
194         if [[ $TEST_LOG =~ tty ]]; then echo "CMD(mustfail): $@" > /dev/tty; fi
195         if [ "$1" = 'root_helper' ]; then
196                 "$@" >> "$RESULTS" 2>&1
197         else
198                 $INSTRUMENT "$@" >> "$RESULTS" 2>&1
199         fi
200         if [ $? != 0 ]; then
201                 echo "failed (expected): $@" >> "$RESULTS"
202                 return 0
203         else
204                 echo "succeeded (unexpected!): $@" >> "$RESULTS"
205                 _fail "unexpected success: $msg"
206                 return 1
207         fi
208 }
209
210 check_prereq()
211 {
212         if ! [ -f "$TOP/$1" ]; then
213                 _fail "Failed prerequisites: $1";
214         fi
215 }
216
217 check_global_prereq()
218 {
219         which $1 &> /dev/null
220         if [ $? -ne 0 ]; then
221                 _fail "Failed system wide prerequisities: $1";
222         fi
223 }
224
225 check_image()
226 {
227         local image
228
229         image=$1
230         echo "testing image $(basename $image)" >> "$RESULTS"
231         "$TOP/btrfs" check "$image" >> "$RESULTS" 2>&1
232         [ $? -eq 0 ] && _fail "btrfs check should have detected corruption"
233
234         run_check "$TOP/btrfs" check --repair "$image"
235         run_check "$TOP/btrfs" check "$image"
236 }
237
238 # Extract a usable image from packed formats
239 # - raw btrfs filesystem images, suffix .raw
240 # - dtto compressed by XZ, suffix .raw.xz
241 # - meta-dump images with suffix .img
242 # - dtto compressed by XZ, suffix .img.xz
243 # - compressed send stream, .stream.xz
244 extract_image()
245 {
246         local image
247         local cleanme
248
249         image="$1"
250         case "$image" in
251         *.img)
252                 rm -f "$image.restored"
253                 : ;;
254         *.img.xz)
255                 xz --decompress --keep "$image" || \
256                         _fail "failed to decompress image $image" >&2
257                 image=${image%%.xz}
258                 rm -f "$image.restored"
259                 cleanme=$image
260                 ;;
261         *.raw)
262                 cp --sparse=auto "$image" "$image.restored"
263                 ;;
264         *.raw.xz)
265                 xz --decompress --keep "$image" || \
266                         _fail "failed to decompress image $image" >&2
267                 image=${image%%.xz}
268                 mv "$image" "$image.restored"
269                 ;;
270         *.stream.xz)
271                 xz --decompress --keep "$image" || \
272                         _fail "failed to decompress file $image" >&2
273                 image=${image%%.xz}
274                 mv "$image" "$image.restored"
275                 ;;
276         esac
277
278         if ! [ -f "$image.restored" ]; then
279                 echo "restoring image $(basename $image)" >> "$RESULTS"
280                 "$TOP/btrfs-image" -r "$image" "$image.restored" \
281                         &>> "$RESULTS" \
282                         || _fail "failed to restore image $image" >&2
283         fi
284
285         [ -f "$cleanme" ] && rm -f "$cleanme"
286
287         echo "$image.restored"
288 }
289
290 # Process all image dumps in a given directory
291 check_all_images()
292 {
293         local dir
294         local extracted
295
296         dir="$1"
297         if [ -z "$dir" ]; then
298                 dir=.
299         fi
300         _assert_path "$dir"
301         for image in $(find "$dir" \( -iname '*.img' -o \
302                                 -iname '*.img.xz' -o    \
303                                 -iname '*.raw' -o       \
304                                 -iname '*.raw.xz' \) | sort)
305         do
306                 extracted=$(extract_image "$image")
307                 check_image "$extracted"
308                 rm -f "$extracted"
309         done
310 }
311
312 # some tests need to mount the recovered image and do verifications call
313 # 'setup_root_helper' and then check for have_root_helper == 1 if the test
314 # needs to fail otherwise; using sudo by default for now
315 SUDO_HELPER=
316 NEED_SUDO_VALIDATE=unknown
317 export SUDO_HELPER
318 export NEED_SUDO_VALIDATE
319 root_helper()
320 {
321         if [ $UID -eq 0 ]; then
322                 "$@"
323         else
324                 if [ "$NEED_SUDO_VALIDATE" = 'yes' ]; then
325                         sudo -v -n &>/dev/null || \
326                                 _not_run "Need to validate sudo credentials"
327                         sudo -n "$@"
328                 elif [ "$NEED_SUDO_VALIDATE" = 'no' ]; then
329                         sudo -n /bin/true &> /dev/null || \
330                                 _not_run "Need to validate sudo user settings"
331                         sudo -n "$@"
332                 else
333                         # should not happen
334                         _not_run "Need to validate root privileges"
335                 fi
336         fi
337 }
338
339 setup_root_helper()
340 {
341         if [ $UID -eq 0 -o -n "$SUDO_HELPER" ]; then
342                 return
343         fi
344
345         # Test for old sudo or special settings, which make sudo -v fail even
346         # if user setting is NOPASSWD
347         sudo -n /bin/true &>/dev/null && NEED_SUDO_VALIDATE=no
348
349         # Newer sudo or default sudo setting
350         sudo -v -n &>/dev/null && NEED_SUDO_VALIDATE=yes
351
352         if [ "$NEED_SUDO_VALIDATE" = 'unknown' ]; then
353                 _not_run "Need to validate root privileges"
354         fi
355         SUDO_HELPER=root_helper
356 }
357
358 prepare_test_dev()
359 {
360         # num[K/M/G/T...]
361         local size="$1"
362
363         [[ "$TEST_DEV" ]] && return
364         [[ "$size" ]] || size='2G'
365
366         echo "\$TEST_DEV not given, use $TOP/test/test.img as fallback" >> \
367                 "$RESULTS"
368         TEST_DEV="$TOP/tests/test.img"
369
370         truncate -s "$size" "$TEST_DEV" || _not_run "create file for loop device failed"
371 }
372
373 run_check_mount_test_dev()
374 {
375         setup_root_helper
376
377         local loop_opt
378         if [[ -b "$TEST_DEV" ]]; then
379                 loop_opt=""
380         elif [[ -f "$TEST_DEV" ]]; then
381                 loop_opt="-o loop"
382         else
383                 _fail "Invalid \$TEST_DEV: $TEST_DEV"
384         fi
385
386         [[ -d "$TEST_MNT" ]] || {
387                 _fail "Invalid \$TEST_MNT: $TEST_MNT"
388         }
389
390         run_check $SUDO_HELPER mount $loop_opt "$@" "$TEST_DEV" "$TEST_MNT"
391 }
392
393 run_check_umount_test_dev()
394 {
395         setup_root_helper
396         run_check $SUDO_HELPER umount "$@" "$TEST_DEV"
397 }
398
399 check_kernel_support()
400 {
401         if ! grep -iq 'btrfs' /proc/filesystems; then
402                 echo "WARNING: btrfs filesystem not listed in /proc/filesystems, some tests might fail"
403                 return 1
404         fi
405         return 0
406 }
407
408 # how many files to create.
409 DATASET_SIZE=50
410
411 generate_dataset() {
412
413         dataset_type="$1"
414         dirpath=$TEST_MNT/$dataset_type
415         run_check $SUDO_HELPER mkdir -p "$dirpath"
416
417         case "$dataset_type" in
418                 small)
419                         for num in $(seq 1 "$DATASET_SIZE"); do
420                                 run_check $SUDO_HELPER dd if=/dev/urandom of="$dirpath/$dataset_type.$num" bs=10K \
421                                 count=1 >/dev/null 2>&1
422                         done
423                         ;;
424
425                 hardlink)
426                         for num in $(seq 1 "$DATASET_SIZE"); do
427                                 run_check $SUDO_HELPER touch "$dirpath/$dataset_type.$num"
428                                 run_check $SUDO_HELPER ln "$dirpath/$dataset_type.$num" "$dirpath/hlink.$num"
429                         done
430                         ;;
431
432                 fast_symlink)
433                         for num in $(seq 1 "$DATASET_SIZE"); do
434                                 run_check $SUDO_HELPER touch "$dirpath/$dataset_type.$num"
435                                 run_check cd "$dirpath" && \
436                                         $SUDO_HELPER ln -s "$dataset_type.$num" "$dirpath/slink.$num" && \
437                                         cd /
438                         done
439                         ;;
440
441                 brokenlink)
442                         for num in $(seq 1 "$DATASET_SIZE"); do
443                                 run_check $SUDO_HELPER ln -s "$dirpath/$dataset_type.$num" "$dirpath/blink.$num"
444                         done
445                         ;;
446
447                 perm)
448                         for modes in 777 775 755 750 700 666 664 644 640 600 444 440 400 000            \
449                                 1777 1775 1755 1750 1700 1666 1664 1644 1640 1600 1444 1440 1400 1000   \
450                                 2777 2775 2755 2750 2700 2666 2664 2644 2640 2600 2444 2440 2400 2000   \
451                                 4777 4775 4755 4750 4700 4666 4664 4644 4640 4600 4444 4440 4400 4000; do
452                                 run_check $SUDO_HELPER touch "$dirpath/$dataset_type.$modes"
453                                 run_check $SUDO_HELPER chmod "$modes" "$dirpath/$dataset_type.$modes"
454                         done
455                         ;;
456
457                 sparse)
458                         for num in $(seq 1 "$DATASET_SIZE"); do
459                                 run_check $SUDO_HELPER dd if=/dev/urandom of="$dirpath/$dataset_type.$num" bs=10K \
460                                 count=1 >/dev/null 2>&1
461                                 run_check $SUDO_HELPER truncate -s 500K "$dirpath/$dataset_type.$num"
462                                 run_check $SUDO_HELPER dd if=/dev/urandom of="$dirpath/$dataset_type.$num" bs=10K \
463                                 oflag=append conv=notrunc count=1 >/dev/null 2>&1
464                                 run_check $SUDO_HELPER truncate -s 800K "$dirpath/$dataset_type.$num"
465                         done
466                         ;;
467
468                 acls)
469                         for num in $(seq 1 "$DATASET_SIZE"); do
470                                 run_check $SUDO_HELPER touch "$dirpath/$dataset_type.$num"
471                                 run_check $SUDO_HELPER setfacl -m "u:root:x" "$dirpath/$dataset_type.$num"
472                                 run_check $SUDO_HELPER setfattr -n user.foo -v "bar$num" "$dirpath/$dataset_type.$num"
473                         done
474                         ;;
475
476                 fifo)
477                         for num in $(seq 1 "$DATASET_SIZE"); do
478                                 run_check $SUDO_HELPER mkfifo "$dirpath/$dataset_type.$num"
479                         done
480                         ;;
481
482                 slow_symlink)
483                         long_filename=`date +%s | sha256sum | cut -f1 -d'-'`
484                         run_check $SUDO_HELPER touch "$dirpath/$long_filename"
485                         for num in $(seq 1 "$DATASET_SIZE"); do
486                                 run_check $SUDO_HELPER ln -s "$dirpath/$long_filename" "$dirpath/slow_slink.$num"
487                         done
488                         ;;
489                 large)
490                         run_check $SUDO_HELPER dd if=/dev/urandom bs=32M count=1 \
491                                 of="$dirpath/$dataset_type" >/dev/null 2>&1
492                         ;;
493         esac
494 }
495
496 init_env()
497 {
498         TEST_MNT="${TEST_MNT:-$TOP/tests/mnt}"
499         export TEST_MNT
500         mkdir -p "$TEST_MNT" || { echo "Failed mkdir -p $TEST_MNT"; exit 1; }
501
502         source $TOP/tests/common.local
503
504         if [ "$TEST_ENABLE_OVERRIDE" = 'true' -a -n "$RESULTS" ]; then
505                 echo "INCLUDE common.local" >> "$RESULTS"
506                 echo "  check: $TEST_ARGS_CHECK" >> "$RESULTS"
507         fi
508 }
509 init_env