btrfs-progs: tests: add path assertions to post-checks
[platform/upstream/btrfs-progs.git] / tests / common.convert
1 #!/bin/bash
2 # helpers for btrfs-convert tests
3
4 # how many files to create.
5 DATASET_SIZE=50
6
7 generate_dataset() {
8
9         dataset_type="$1"
10         dirpath=$TEST_MNT/$dataset_type
11         run_check $SUDO_HELPER mkdir -p $dirpath
12
13         case $dataset_type in
14                 small)
15                         for num in $(seq 1 $DATASET_SIZE); do
16                                 run_check $SUDO_HELPER dd if=/dev/urandom of=$dirpath/$dataset_type.$num bs=10K \
17                                 count=1 >/dev/null 2>&1
18                         done
19                         ;;
20
21                 hardlink)
22                         for num in $(seq 1 $DATASET_SIZE); do
23                                 run_check $SUDO_HELPER touch $dirpath/$dataset_type.$num
24                                 run_check $SUDO_HELPER ln $dirpath/$dataset_type.$num $dirpath/hlink.$num
25                         done
26                         ;;
27
28                 fast_symlink)
29                         for num in $(seq 1 $DATASET_SIZE); do
30                                 run_check $SUDO_HELPER touch $dirpath/$dataset_type.$num
31                                 run_check cd $dirpath && $SUDO_HELPER ln -s $dataset_type.$num $dirpath/slink.$num && cd /
32                         done
33                         ;;
34
35                 brokenlink)
36                         for num in $(seq 1 $DATASET_SIZE); do
37                                 run_check $SUDO_HELPER ln -s $dirpath/$dataset_type.$num $dirpath/blink.$num
38                         done
39                         ;;
40
41                 perm)
42                         for modes in 777 775 755 750 700 666 664 644 640 600 444 440 400 000            \
43                                 1777 1775 1755 1750 1700 1666 1664 1644 1640 1600 1444 1440 1400 1000   \
44                                 2777 2775 2755 2750 2700 2666 2664 2644 2640 2600 2444 2440 2400 2000   \
45                                 4777 4775 4755 4750 4700 4666 4664 4644 4640 4600 4444 4440 4400 4000; do
46                                 if [[ "$modes" == *9* ]] || [[ "$modes" == *8* ]]
47                                 then
48                                         continue;
49                                 else
50                                         run_check $SUDO_HELPER touch $dirpath/$dataset_type.$modes
51                                         run_check $SUDO_HELPER chmod $modes $dirpath/$dataset_type.$modes
52                                 fi
53                         done
54                         ;;
55
56                 sparse)
57                         for num in $(seq 1 $DATASET_SIZE); do
58                                 run_check $SUDO_HELPER dd if=/dev/urandom of=$dirpath/$dataset_type.$num bs=10K \
59                                 count=1 >/dev/null 2>&1
60                                 run_check $SUDO_HELPER truncate -s 500K $dirpath/$dataset_type.$num
61                                 run_check $SUDO_HELPER dd if=/dev/urandom of=$dirpath/$dataset_type.$num bs=10K \
62                                 oflag=append conv=notrunc count=1 >/dev/null 2>&1
63                                 run_check $SUDO_HELPER truncate -s 800K $dirpath/$dataset_type.$num
64                         done
65                         ;;
66
67                 acls)
68                         for num in $(seq 1 $DATASET_SIZE); do
69                                 run_check $SUDO_HELPER touch $dirpath/$dataset_type.$num
70                                 run_check $SUDO_HELPER setfacl -m "u:root:x" $dirpath/$dataset_type.$num
71                                 run_check $SUDO_HELPER setfattr -n user.foo -v bar$num $dirpath/$dataset_type.$num
72                         done
73                         ;;
74
75                 fifo)
76                         for num in $(seq 1 $DATASET_SIZE); do
77                                 run_check $SUDO_HELPER mkfifo $dirpath/$dataset_type.$num
78                         done
79                         ;;
80
81                 slow_symlink)
82                         long_filename=`date +%s | sha256sum | cut -f1 -d'-'`
83                         run_check $SUDO_HELPER touch $dirpath/$long_filename
84                         for num in $(seq 1 $DATASET_SIZE); do
85                                 run_check $SUDO_HELPER ln -s $dirpath/$long_filename $dirpath/slow_slink.$num
86                         done
87                         ;;
88         esac
89 }
90
91 populate_fs() {
92
93         for dataset_type in 'small' 'hardlink' 'fast_symlink' 'brokenlink' 'perm' 'sparse' 'acls' 'fifo' 'slow_symlink'; do
94                 generate_dataset "$dataset_type"
95         done
96 }
97
98 # verbose message before the test, same arguments as convert_test
99 convert_test_preamble() {
100         local features
101         local msg
102
103         features="$1"
104         msg="$2"
105         shift 3
106         echo "    [TEST/conv]     $msg, btrfs" "${features:-defaults}"
107         echo "creating ext image with: $@" >> $RESULTS
108 }
109
110 #  prepare TEST_DEV before conversion, create filesystem and mount it, image
111 #  size is 512MB
112 #  $@: free form, command to create the filesystem, with appended -F
113 convert_test_prep_fs() {
114         # TEST_DEV not removed as the file might have special permissions, eg.
115         # when test image is on NFS and would not be writable for root
116         run_check truncate -s 0 $TEST_DEV
117         # 256MB is the smallest acceptable btrfs image.
118         run_check truncate -s 512M $TEST_DEV
119         run_check "$@" -F $TEST_DEV
120
121         # create a file to check btrfs-convert can convert regular file correct
122         run_check_mount_test_dev
123
124         # create a file inside the fs before convert, to make sure there is
125         # data covering btrfs backup superblock range (64M)
126         run_check $SUDO_HELPER dd if=/dev/zero bs=1M count=64 \
127                 of=$TEST_MNT/convert_space_holder
128 }
129
130 # generate md5 checksums of files on $TEST_MNT
131 # $1: path where the checksums will be stored
132 convert_test_gen_checksums() {
133         _assert_path "$1"
134
135         run_check $SUDO_HELPER dd if=/dev/zero of=$TEST_MNT/test bs=$nodesize \
136                 count=1 >/dev/null 2>&1
137         run_check_stdout $SUDO_HELPER find $TEST_MNT -type f ! -name 'image' -exec md5sum {} \+ > "$1"
138 }
139 # list $TEST_MNT data set file permissions.
140 # $1: path where the permissions will be stored
141 convert_test_perm() {
142         local PERMTMP
143
144         _assert_path "$1"
145         PERMTMP="$1"
146         FILES_LIST=$(mktemp --tmpdir btrfs-progs-convert.fileslistXXXXXX)
147
148         run_check $SUDO_HELPER dd if=/dev/zero of=$TEST_MNT/test bs=$nodesize \
149                 count=1 >/dev/null 2>&1
150         run_check_stdout $SUDO_HELPER find $TEST_MNT -type f ! -name 'image' -fprint $FILES_LIST
151         # Fix directory entries order
152         sort $FILES_LIST -o $FILES_LIST
153         for file in `cat $FILES_LIST` ;do
154                 run_check_stdout $SUDO_HELPER getfacl --absolute-names $file >> "$PERMTMP"
155         done
156         rm -- $FILES_LIST
157 }
158 # list acls of files on $TEST_MNT
159 # $1: path where the acls will be stored
160 convert_test_acl() {
161         local ACLSTMP
162         ACLTMP="$1"
163         FILES_LIST=$(mktemp --tmpdir btrfs-progs-convert.fileslistXXXXXX)
164
165         run_check_stdout $SUDO_HELPER find $TEST_MNT/acls -type f -fprint $FILES_LIST
166         # Fix directory entries order
167         sort $FILES_LIST -o $FILES_LIST
168         for file in `cat $FILES_LIST`;do
169                 run_check_stdout $SUDO_HELPER getfattr --absolute-names -d $file >> "$ACLTMP"
170         done
171         rm -- $FILES_LIST
172 }
173
174 # do conversion with given features and nodesize, fsck afterwards
175 # $1: features, argument of -O, can be empty
176 # $2: nodesize, argument of -N, can be empty
177 convert_test_do_convert() {
178         run_check $TOP/btrfs-convert ${1:+-O "$1"} ${2:+-N "$2"} $TEST_DEV
179         run_check $TOP/btrfs check $TEST_DEV
180         run_check $TOP/btrfs inspect-internal dump-super -Ffa $TEST_DEV
181 }
182
183 # post conversion check, verify file permissions.
184 # $1: file with ext permissions.
185 convert_test_post_check_permissions() {
186         local EXT_PERMTMP
187         local BTRFS_PERMTMP
188
189         _assert_path "$1"
190         EXT_PERMTMP="$1"
191         BTRFS_PERMTMP=$(mktemp --tmpdir btrfs-progs-convert.permXXXXXX)
192         convert_test_perm "$BTRFS_PERMTMP"
193
194         btrfs_perm=`md5sum $BTRFS_PERMTMP | cut -f1 -d' '`
195         ext_perm=`md5sum $EXT_PERMTMP | cut -f1 -d' '`
196
197         if [ "$btrfs_perm" != "$ext_perm" ];
198         then
199                 btrfs_perm_file=`md5sum $BTRFS_PERMTMP | cut -f2 -d' '`
200                 ext_perm_file=`md5sum $EXT_PERMTMP | cut -f2 -d' '`
201                 _fail "file permission failed. Mismatched BTRFS:$btrfs_perm_file:$btrfs_perm EXT:$ext_perm_file:$ext_perm"
202         fi
203
204         rm -- $BTRFS_PERMTMP
205 }
206 # post conversion check, compare BTRFS file acls against EXT.
207 # $1: file with ext acls.
208 convert_test_post_check_acl() {
209         local EXT_ACLTMP
210         local BTRFS_ACLTMP
211
212         _assert_path "$1"
213         EXT_ACLTMP="$1"
214         BTRFS_ACLTMP=$(mktemp --tmpdir btrfs-progs-convert.aclsXXXXXXX)
215         convert_test_acl "$BTRFS_ACLTMP"
216
217         btrfs_acl=`md5sum $BTRFS_ACLTMP | cut -f1 -d' '`
218         ext_acl=`md5sum $EXT_ACLTMP | cut -f1 -d' '`
219
220         if [ "$btrfs_acl" != "$ext_acl" ]
221         then
222                 btrfs_acl_file=`md5sum $BTRFS_ACLTMP | cut -f2 -d' '`
223                 ext_acl_file=`md5sum $EXT_ACLTMP | cut -f2 -d' '`
224                 _fail "file acl failed. Mismatched BTRFS:$btrfs_acl_file:$btrfs_acl EXT:$ext_acl_file:$ext_acl"
225         fi
226
227         rm -- $BTRFS_ACLTMP
228 }
229
230 # post conversion checks, verify md5sums
231 convert_test_post_check_checksums() {
232         _assert_path "$1"
233         run_check_stdout $SUDO_HELPER md5sum -c "$1" |
234                 grep -q 'FAILED' && _fail "file validation failed"
235 }
236
237 # post conversion checks, all three in one call, on an unmounted image
238 # $1: file with checksums
239 # $2: file with permissions.
240 # $3: file with acl entries.
241 convert_test_post_checks_all() {
242         _assert_path "$1"
243         _assert_path "$2"
244         _assert_path "$3"
245
246         run_check_mount_test_dev
247         convert_test_post_check_checksums "$1"
248         convert_test_post_check_permissions "$2"
249         convert_test_post_check_acl "$3"
250         run_check_umount_test_dev
251 }
252
253 # do rollback and fsck
254 convert_test_post_rollback() {
255         run_check $TOP/btrfs-convert --rollback $TEST_DEV
256         run_check fsck -n -t ext2,ext3,ext4 $TEST_DEV
257 }
258
259 # simple wrapper for a convert test
260 # $1: btrfs features, argument to -O
261 # $2: description of the test "ext2 8k nodesize"
262 # $3: nodesize value
263 # $4 + rest: command to create the ext2 image
264 convert_test() {
265         local features
266         local nodesize
267         local msg
268         local CHECKSUMTMP
269         local EXT_PERMTMP
270         local EXT_ACLTMP
271
272         features="$1"
273         msg="$2"
274         nodesize="$3"
275         shift 3
276         convert_test_preamble "$features" "$msg" "$nodesize" "$@"
277         convert_test_prep_fs "$@"
278         populate_fs
279         CHECKSUMTMP=$(mktemp --tmpdir btrfs-progs-convert.XXXXXXXXXX)
280         EXT_PERMTMP=$(mktemp --tmpdir btrfs-progs-convert.permXXXXXX)
281         EXT_ACLTMP=$(mktemp --tmpdir btrfs-progs-convert.aclsXXXXXXX)
282         convert_test_gen_checksums "$CHECKSUMTMP"
283         convert_test_perm "$EXT_PERMTMP"
284         convert_test_acl "$EXT_ACLTMP"
285
286         run_check_umount_test_dev
287
288         convert_test_do_convert "$features" "$nodesize"
289         convert_test_post_checks_all "$CHECKSUMTMP" "$EXT_PERMTMP" "$EXT_ACLTMP"
290         rm $CHECKSUMTMP
291         rm $EXT_PERMTMP
292         rm $EXT_ACLTMP
293
294         convert_test_post_rollback
295 }