]> wimlib.net Git - wimlib/blob - tests/test-imagex-ntfs
test-imagex-ntfs: fix empty xattr creation
[wimlib] / tests / test-imagex-ntfs
1 #!/usr/bin/env bash
2
3 # This script does some sanity testing of the 'imagex' program, specifically
4 # checking the NTFS capture and apply features.
5 #
6 # This test will fail if wimlib was compiled with --without-ntfs-3g.
7 #
8 # Please note that cleanup is not done if a test fails, and NTFS volumes may
9 # remain mounted.
10
11 set -e
12 cd tests
13 srcdir="${srcdir:-.}/.."
14 srcdir="$(cd $srcdir; pwd)"
15 . "$srcdir/tests/tests-common.sh"
16
17 TEST_SUBDIR=tmpdir_test-imagex-ntfs
18
19 __do_unmount() {
20         for ((i = 0; i < 10; i++)); do
21                 if fusermount -z -u $1; then
22                         return 0
23                 else
24                         sleep 1
25                 fi
26         done
27         error "Failed to unmount \"$1\""
28 }
29
30 do_unmount() {
31         if mountpoint $1 &> /dev/null; then
32                 __do_unmount $1
33         fi
34 }
35
36 skip_test() {
37         cd ..
38         cleanup
39         exit 77
40 }
41
42 __do_mount() {
43         if ! ntfs-3g -o "no_def_opts,silent" $1 $2; then
44                 if [ $UID -ne 0 ] && [ "$3" = "nofail" ]; then
45                         echo "WARNING: skipping NTFS tests because we aren't able to "
46                         echo "mount an NTFS volume (perhaps ntfs-3g is not installed setuid root?)"
47                         skip_test
48                 else
49                         error "Could not mount NTFS volume \"$1\" on \"$2\"!  Make sure ntfs-3g is "\
50                               "installed, and that you are either running the tests as root or have ntfs-3g "\
51                               "installed setuid root, so that we can mount an NTFS volume."
52                 fi
53         fi
54 }
55
56 do_mount() {
57         do_unmount $2
58         __do_mount $1 $2 $3
59 }
60
61 do_mkntfs() {
62         if ! mkntfs --force --fast $1 > /dev/null; then
63                 error "Could not create NTFS volume \"$1\"!  Make sure ntfsprogs are installed."
64         fi
65 }
66
67 init() {
68         echo "Creating NTFS volumes and empty directories to use as mountpoints"
69         dd if=/dev/zero of=in.ntfs bs=4096 count=1000 &> /dev/null
70         dd if=/dev/zero of=out.ntfs bs=4096 count=1000 &> /dev/null
71         mkdir in.mnt out.mnt
72         do_mkntfs in.ntfs
73         do_mkntfs out.ntfs
74         do_mount in.ntfs in.mnt nofail
75 }
76
77 cleanup() {
78         do_unmount $TEST_SUBDIR/in.mnt
79         do_unmount $TEST_SUBDIR/out.mnt
80         rm -rf $TEST_SUBDIR
81 }
82
83 do_test() {
84         cd in.mnt
85         eval "$1"
86         cd ..
87         __do_unmount in.mnt
88         if ! wimcapture in.ntfs ntfs.wim; then
89                 error "Failed to capture NTFS volume into a WIM"
90         fi
91         if ! wimapply ntfs.wim 1 out.ntfs; then
92                 error "Failed to apply WIM to NTFS volume"
93         fi
94         __do_mount in.ntfs in.mnt noatime
95         __do_mount out.ntfs out.mnt noatime
96         if [ -x /usr/bin/tree ]; then
97                 tree in.mnt --inodes -F -s --noreport
98         fi
99         if ! ../tree-cmp in.mnt out.mnt NTFS; then
100                 if [ -x /usr/bin/tree ]; then
101                         echo "Dumping tree of applied image"
102                         tree out.mnt --inodes -F -s --noreport
103                         error 'Information was lost or corrupted while capturing
104                                 and then applying an NTFS volume'
105                 fi
106         fi
107         rm -rf out.mnt/* in.mnt/*
108         __do_unmount out.mnt
109 }
110 msg() {
111         echo "--------------------------------------------------------------------"
112         echo "Testing image capture and application of NTFS volume containing $1"
113         echo "--------------------------------------------------------------------"
114 }
115
116 cleanup
117 mkdir $TEST_SUBDIR
118 cd $TEST_SUBDIR
119 init
120
121 msg "Empty NTFS volume"
122 do_test ""
123
124 msg "a single file"
125 do_test "echo 1 > file"
126
127 msg "a single directory"
128 do_test "mkdir dir"
129
130 msg "subdirectory with file"
131 do_test "mkdir dir; echo 1 > dir/file"
132
133 msg "empty file"
134 do_test "echo -n > empty_file"
135
136 msg "two empty files"
137 do_test "echo -n > empty_file_1; echo -n > empty_file_2"
138
139 msg "hard link in same directory"
140 do_test "echo 1 > file; ln file link"
141
142 msg "hard link between empty files"
143 do_test "echo -n > empty_file; ln empty_file link"
144
145 msg "relative symbolic link"
146 do_test "echo 1 > file; ln -s file symlink"
147
148 msg "absolute symbolic link"
149 do_test "echo 1 > file; ln -s /some/absolute/target symlink"
150
151 msg "large file"
152 do_test "dd if=/dev/zero of=file bs=4096 count=10 &> /dev/null"
153
154 msg "file with DOS name"
155 do_test "echo 1 > file; setfattr -v file -n system.ntfs_dos_name file"
156
157 msg "many nested directories"
158 do_test 'mkdir dir; mkdir dir/subdir; mkdir dir/subdir/subdir2; mkdir dir/subdir/subdir3'
159
160 msg "identical files and symlinks in subdirectory"
161 do_test 'mkdir dir;
162          echo 888 > dir/file;
163          echo 888 > dir/idfile2;
164          ln -s ../dir dir/circle; ln -s file dir/filelink'
165
166 msg "hard link group and identical files not hard linked"
167 do_test 'echo 888 > file;
168          echo 888 > file2;
169          ln file link;
170          ln file link2;
171          echo 888 > file3'
172
173 msg "file with named data stream"
174 do_test 'echo 1 > file;
175          setfattr -n user.ads -v 2 file'
176
177 msg "file with multiple named data streams"
178 do_test 'echo 1 > file;
179          setfattr -n user.a -v 1 file;
180          setfattr -n user.aa -v 11 file;
181          setfattr -n user.aaa -v 111 file;
182          setfattr -n user.aaaa -v 1111 file'
183
184 msg "file with multiple named data streams with same contents"
185 do_test 'echo 1 > file;
186          setfattr -n user.a -v 1111 file;
187          setfattr -n user.aa -v 1111 file;
188          setfattr -n user.aaa -v 1111 file;
189          setfattr -n user.aaaa -v 1111 file;'
190
191 msg "file with named data streams with same contents as other file"
192 do_test 'echo -n > file;
193          setfattr -n user.a -v 1111 file;
194          echo -n 1111 > otherfile;'
195
196 msg "file with empty named data stream and non-empty unnamed data stream"
197 do_test 'echo 1 > file;
198          setfattr -n user.ads -v 0x file;'
199
200 msg "file with empty named data stream and empty unnamed data stream"
201 do_test 'echo -n > file;
202          setfattr -n user.ads -v 0x file;'
203
204 msg "file with named data stream with hardlink"
205 do_test 'echo 999 > file;
206          setfattr -n user.ads -v 0x123456 file;
207          ln file link;'
208
209 msg "C source code of wimlib"
210 do_test 'cp $srcdir/src/*.c .'
211
212 msg "file with security descriptor"
213 do_test 'touch file;
214          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_1.base64` file'
215
216 msg "files with different security descriptors"
217 do_test 'touch file;
218          touch file2;
219          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_1.base64` file
220          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_2.base64` file'
221
222 msg "files with different security descriptors and some with the same security descriptor"
223 do_test 'touch file;
224          touch file2;
225          touch file3;
226          mkdir dir;
227          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_1.base64` file
228          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_2.base64` file
229          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_1.base64` dir
230          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_1.base64` file3'
231
232 msg "tons of random stuff"
233 do_test 'echo -n 8 > file;
234          ln file hardlink;
235          ln -s hardlink symlink;
236          echo -n 8 > identical file;
237          dd if=/dev/urandom of=randomfile bs=4096 count=10 &>/dev/null;
238          mkdir dir;
239          setfattr -n system.ntfs_dos_name -v DOSNAME dir;
240          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_1.base64` dir
241          mkdir anotherdir;
242          cp file anotherdir;
243          ln file anotherdir/anotherhardlink;
244          ln -s .. anotherdir/anothersymlink;
245          ln -s anothersymlink anotherdir/symlinktosymlink;
246          echo -n 33 > anotherfile;
247          setfattr -n user.ads anotherfile -v 33;
248          setfattr -n user.ads2 anotherfile -v 8;
249          setfattr -n user.ads3 anotherfile -v 33;
250          echo -n > emptyfile;
251          setfattr -n user.ads emptyfile -v 8;
252          setfattr -n user.ads5 emptyfile -v"`cat $srcdir/src/sha1.c`"
253          mkdir dir/subdir;
254          ln file dir/subdir/file;
255          echo -n 8 > dir/subdir/file2;
256          ln dir/subdir/file dir/subdir/link;
257          echo -n > dir/subdir/empty;
258          setfattr -n system.ntfs_dos_name -v 123 dir/subdir/empty;
259          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_1.base64` dir/subdir/link;
260          setfattr -n user.yet_another_ads -v "" dir/subdir/link;
261          setfattr -n user.yet_another_ads2 -v "" dir/subdir/link;
262          setfattr -n user.yet_another_ads3 -v "abc" dir/subdir/link;
263          setfattr -n user.yet_another_ads4 -v "" dir/subdir/link;'
264
265 echo "**********************************************************"
266 echo "           NTFS capture/apply tests passed                "
267 echo "**********************************************************"
268
269 cd ..
270 cleanup
271