]> wimlib.net Git - wimlib/blob - tests/test-imagex-ntfs
1f3d1c57c88fb5aefd974fa2aa6ce77c099295df
[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 # To be replaced/removed: files with DOS names can no longer be hard linked as
158 # of NTFS-3g 2013.1.13.
159
160 #msg "file with DOS name with alphabetically smaller hardlink in same directory"
161 #do_test "echo 1 > file; setfattr -v file -n system.ntfs_dos_name file; ln file aaa_link"
162
163 #msg "file with DOS name with alphabetically larger hardlink in same directory"
164 #do_test "echo 1 > file; setfattr -v file -n system.ntfs_dos_name file; ln file zzz_link"
165
166 #msg "file with long name and with DOS name with alphabetically smaller hardlink in same directory"
167 #do_test 'echo 1 > file_with_a_long_name;
168          #setfattr -v "file~1" -n system.ntfs_dos_name file_with_a_long_name;
169          #ln file_with_a_long_name aaa_link'
170
171 msg "many nested directories"
172 do_test 'mkdir dir; mkdir dir/subdir; mkdir dir/subdir/subdir2; mkdir dir/subdir/subdir3'
173
174 msg "identical files and symlinks in subdirectory"
175 do_test 'mkdir dir;
176          echo 888 > dir/file;
177          echo 888 > dir/idfile2;
178          ln -s ../dir dir/circle; ln -s file dir/filelink'
179
180 msg "hard link group and identical files not hard linked"
181 do_test 'echo 888 > file;
182          echo 888 > file2;
183          ln file link;
184          ln file link2;
185          echo 888 > file3'
186
187 msg "file with named data stream"
188 do_test 'echo 1 > file;
189          setfattr -n user.ads -v 2 file'
190
191 msg "file with multiple named data streams"
192 do_test 'echo 1 > file;
193          setfattr -n user.a -v 1 file;
194          setfattr -n user.aa -v 11 file;
195          setfattr -n user.aaa -v 111 file;
196          setfattr -n user.aaaa -v 1111 file'
197
198 msg "file with multiple named data streams with same contents"
199 do_test 'echo 1 > file;
200          setfattr -n user.a -v 1111 file;
201          setfattr -n user.aa -v 1111 file;
202          setfattr -n user.aaa -v 1111 file;
203          setfattr -n user.aaaa -v 1111 file;'
204
205 msg "file with named data streams with same contents as other file"
206 do_test 'echo -n > file;
207          setfattr -n user.a -v 1111 file;
208          echo -n 1111 > otherfile;'
209
210 msg "file with empty named data stream and non-empty unnamed data stream"
211 do_test 'echo 1 > file;
212          setfattr -n user.ads -v "" file;'
213
214 msg "file with empty named data stream and empty unnamed data stream"
215 do_test 'echo -n > file;
216          setfattr -n user.ads -v "" file;'
217
218 msg "file with named data stream with hardlink"
219 do_test 'echo 999 > file;
220          setfattr -n user.ads -v "888" file;
221          ln file link;'
222
223 # To be replaced/removed: files with DOS names can no longer be hard linked as
224 # of NTFS-3g 2013.1.13.
225
226 #msg "file with named data stream with hardlink and DOS name"
227 #do_test 'echo 999 > file;
228          #setfattr -n user.ads -v "888" file;
229          #ln file link;
230          #setfattr -v DOSNAME -n system.ntfs_dos_name file;'
231
232 msg "C source code of wimlib"
233 do_test 'cp $srcdir/src/*.c .'
234
235 msg "file with security descriptor"
236 do_test 'touch file;
237          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_1.base64` file'
238
239 msg "files with different security descriptors"
240 do_test 'touch file;
241          touch file2;
242          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_1.base64` file
243          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_2.base64` file'
244
245 msg "files with different security descriptors and some with the same security descriptor"
246 do_test 'touch file;
247          touch file2;
248          touch file3;
249          mkdir dir;
250          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_1.base64` file
251          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_2.base64` file
252          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_1.base64` dir
253          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_1.base64` file3'
254
255 msg "tons of random stuff"
256 do_test 'echo -n 8 > file;
257          ln file hardlink;
258          ln -s hardlink symlink;
259          echo -n 8 > identical file;
260          dd if=/dev/urandom of=randomfile bs=4096 count=10 &>/dev/null;
261          mkdir dir;
262          setfattr -n system.ntfs_dos_name -v DOSNAME dir;
263          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_1.base64` dir
264          mkdir anotherdir;
265          cp file anotherdir;
266          ln file anotherdir/anotherhardlink;
267          ln -s .. anotherdir/anothersymlink;
268          ln -s anothersymlink anotherdir/symlinktosymlink;
269          echo -n 33 > anotherfile;
270          setfattr -n user.ads anotherfile -v 33;
271          setfattr -n user.ads2 anotherfile -v 8;
272          setfattr -n user.ads3 anotherfile -v 33;
273          echo -n > emptyfile;
274          setfattr -n user.ads emptyfile -v 8;
275          setfattr -n user.ads5 emptyfile -v"`cat $srcdir/src/sha1.c`"
276          mkdir dir/subdir;
277          ln file dir/subdir/file;
278          echo -n 8 > dir/subdir/file2;
279          ln dir/subdir/file dir/subdir/link;
280          echo -n > dir/subdir/empty;
281          setfattr -n system.ntfs_dos_name -v 123 dir/subdir/empty;
282          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_1.base64` dir/subdir/link;
283          setfattr -n user.yet_another_ads -v "" dir/subdir/link;
284          setfattr -n user.yet_another_ads2 -v "" dir/subdir/link;
285          setfattr -n user.yet_another_ads3 -v "abc" dir/subdir/link;
286          setfattr -n user.yet_another_ads4 -v "" dir/subdir/link;'
287
288 echo "**********************************************************"
289 echo "           NTFS capture/apply tests passed                "
290 echo "**********************************************************"
291
292 cd ..
293 cleanup
294