]> wimlib.net Git - wimlib/blob - tests/test-imagex-ntfs
Version 1.2.1
[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 imagex() {
18         ../imagex $@ > /dev/null
19 }
20
21 __do_unmount() {
22         if !  fusermount -z -u $1; then
23                 error "Failed to unmount \"$1\""
24         fi
25
26 }
27
28 do_unmount() {
29         if mountpoint $1 &> /dev/null; then
30                 __do_unmount $1
31         fi
32 }
33
34 __do_mount() {
35         options="$3"
36         if [ -z "$options" ]; then
37                 options="no_def_opts,silent"
38         else
39                 options="$options,no_def_opts,silent"
40         fi
41         if ! ntfs-3g -o $options $1 $2; then
42                 error "Could not mount NTFS volume \"$1\" on \"$2\"!  Make sure ntfs-3g is "\
43                       "installed, and that you are either running the tests as root or have ntfs-3g "\
44                       "installed setuid root, so that we can mount a NTFS volume."
45         fi
46 }
47
48 do_mount() {
49         do_unmount $2
50         __do_mount $1 $2 $3
51 }
52
53 do_mkntfs() {
54         if ! mkntfs --force --fast $1 &> /dev/null; then
55                 error "Could not create NTFS volume \"$1\"!  Make sure ntfsprogs are installed."
56         fi
57 }
58
59 init() {
60         echo "Creating NTFS volumes and empty directories to use as mountpoints"
61         dd if=/dev/zero of=in.ntfs bs=4096 count=500 &> /dev/null
62         dd if=/dev/zero of=out.ntfs bs=4096 count=500 &> /dev/null
63         mkdir in.mnt out.mnt
64         do_mkntfs in.ntfs
65         do_mkntfs out.ntfs
66         do_mount in.ntfs in.mnt
67 }
68
69 cleanup() {
70         do_unmount in.mnt
71         do_unmount out.mnt
72         rm -rf in.ntfs out.ntfs in.mnt out.mnt in.xattr out.xattr ntfs.wim
73 }
74 #trap cleanup exit
75
76
77 error() {
78         echo "****************************************************************"
79         echo "                         Test failure                           "
80         while [ $# -gt 0 ]; do
81                 echo $1
82                 shift
83         done
84         echo "****************************************************************"
85         exit 1
86 }
87
88 do_test() {
89         cd in.mnt
90         eval "$1"
91         cd ..
92         __do_unmount in.mnt
93         if ! imagex capture in.ntfs ntfs.wim; then
94                 error "Failed to capture NTFS volume into a WIM"
95         fi
96         if ! imagex apply ntfs.wim 1 out.ntfs; then
97                 error "Failed to apply WIM to NTFS volume"
98         fi
99         __do_mount in.ntfs in.mnt noatime
100         __do_mount out.ntfs out.mnt noatime
101         if [ -x /usr/bin/tree ]; then
102                 tree in.mnt --inodes -F -s --noreport
103         fi
104         if ! ./tree-cmp in.mnt out.mnt NTFS; then
105                 if [ -x /usr/bin/tree ]; then
106                         echo "Dumping tree of applied image"
107                         tree out.mnt --inodes -F -s --noreport
108                         error 'Information was lost or corrupted while capturing
109                                 and then applying a NTFS volume'
110                 fi
111         fi
112         rm -rf out.mnt/* in.mnt/*
113         __do_unmount out.mnt
114 }
115 msg() {
116         echo "--------------------------------------------------------------------"
117         echo "Testing image capture and application of NTFS volume containing $1"
118         echo "--------------------------------------------------------------------"
119 }
120
121 cleanup
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 "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 msg "file with named data stream with hardlink and DOS name"
224 do_test 'echo 999 > file;
225          setfattr -n user.ads -v "888" file;
226          ln file link;
227          setfattr -v DOSNAME -n system.ntfs_dos_name file;'
228
229 msg "C source code of wimlib"
230 do_test 'cp $srcdir/src/*.{c,h} .'
231
232 msg "file with security descriptor"
233 do_test 'touch file;
234          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_1.base64` file'
235
236 msg "files with different security descriptors"
237 do_test 'touch file;
238          touch file2;
239          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_1.base64` file
240          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_2.base64` file'
241
242 msg "files with different security descriptors and some with the same security descriptor"
243 do_test 'touch file;
244          touch file2;
245          touch file3;
246          mkdir dir;
247          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_1.base64` file
248          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_2.base64` file
249          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_1.base64` dir
250          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_1.base64` file3'
251
252 msg "tons of random stuff"
253 do_test 'echo -n 8 > file;
254          ln file hardlink;
255          ln -s hardlink symlink;
256          echo -n 8 > identical file;
257          dd if=/dev/urandom of=randomfile bs=4096 count=10 &>/dev/null;
258          mkdir dir;
259          setfattr -n system.ntfs_dos_name -v DOSNAME dir;
260          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_1.base64` dir
261          mkdir anotherdir;
262          cp file anotherdir;
263          ln file anotherdir/anotherhardlink;
264          ln -s .. anotherdir/anothersymlink;
265          ln -s anothersymlink anotherdir/symlinktosymlink;
266          echo -n 33 > anotherfile;
267          setfattr -n user.ads anotherfile -v 33;
268          setfattr -n user.ads2 anotherfile -v 8;
269          setfattr -n user.ads3 anotherfile -v 33;
270          echo -n > emptyfile;
271          setfattr -n user.ads emptyfile -v 8;
272          setfattr -n user.ads5 emptyfile -v"`cat $srcdir/src/hardlink.c`"
273          mkdir dir/subdir;
274          ln file dir/subdir/file;
275          echo -n 8 > dir/subdir/file2;
276          ln dir/subdir/file dir/subdir/link;
277          setfattr -n system.ntfs_dos_name -v 123 dir/subdir/link;
278          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_1.base64` dir/subdir/link;
279          setfattr -n user.yet_another_ads -v "" dir/subdir/link;
280          setfattr -n user.yet_another_ads2 -v "" dir/subdir/link;
281          setfattr -n user.yet_another_ads3 -v "abc" dir/subdir/link;
282          setfattr -n user.yet_another_ads4 -v "" dir/subdir/link;'
283
284 cleanup
285
286 echo "**********************************************************"
287 echo "           NTFS capture/apply tests passed                "
288 echo "**********************************************************"