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