]> wimlib.net Git - wimlib/blob - tests/test-imagex-ntfs
Various portability fixes
[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
16 imagex() {
17         ../imagex $@ > /dev/null
18 }
19
20 __do_unmount() {
21         if !  fusermount -z -u $1; then
22                 error "Failed to unmount \"$1\""
23         fi
24
25 }
26 do_unmount() {
27         if mountpoint $1 &> /dev/null; then
28                 __do_unmount $1
29         fi
30 }
31
32 __do_mount() {
33         options="$3"
34         if [ -z "$options" ]; then
35                 options="no_def_opts,silent"
36         else
37                 options="$options,no_def_opts,silent"
38         fi
39         if ! ntfs-3g -o $options $1 $2; then
40                 error "Could not mount NTFS volume \"$1\" on \"$2\"!  Make sure ntfs-3g is "\
41                       "installed, and that you are either running the tests as root or have ntfs-3g "\
42                       "installed setuid root, so that we can mount a NTFS volume."
43         fi
44 }
45
46 do_mount() {
47         do_unmount $2
48         __do_mount $1 $2 $3
49 }
50
51 do_mkntfs() {
52         if ! mkntfs --force --fast $1 &> /dev/null; then
53                 error "Could not create NTFS volume \"$1\"!  Make sure ntfsprogs are installed."
54         fi
55 }
56
57 init() {
58         echo "Creating NTFS volumes and empty directories to use as mountpoints"
59         dd if=/dev/zero of=in.ntfs bs=4096 count=500 &> /dev/null
60         dd if=/dev/zero of=out.ntfs bs=4096 count=500 &> /dev/null
61         mkdir in.mnt out.mnt
62         do_mkntfs in.ntfs
63         do_mkntfs out.ntfs
64         do_mount in.ntfs in.mnt
65 }
66
67 cleanup() {
68         do_unmount in.mnt
69         do_unmount out.mnt
70         rm -rf in.ntfs out.ntfs in.mnt out.mnt in.xattr out.xattr ntfs.wim
71 }
72 #trap cleanup exit
73
74
75 error() {
76         echo "****************************************************************"
77         echo "                         Test failure                           "
78         while [ $# -gt 0 ]; do
79                 echo $1
80                 shift
81         done
82         echo "****************************************************************"
83         exit 1
84 }
85
86 do_test() {
87         cd in.mnt
88         eval "$1"
89         cd ..
90         __do_unmount in.mnt
91         if ! imagex capture in.ntfs ntfs.wim; then
92                 error "Failed to capture NTFS volume into a WIM"
93         fi
94         if ! imagex apply 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 a 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 init
121
122 msg "Empty NTFS volume"
123 do_test ""
124
125 msg "a single file"
126 do_test "echo 1 > file"
127
128 msg "a single directory"
129 do_test "mkdir dir"
130
131 msg "subdirectory with file"
132 do_test "mkdir dir; echo 1 > dir/file"
133
134 msg "empty file"
135 do_test "echo -n > empty_file"
136
137 msg "two empty files"
138 do_test "echo -n > empty_file_1; echo -n > empty_file_2"
139
140 msg "hard link in same directory"
141 do_test "echo 1 > file; ln file link"
142
143 msg "hard link between empty files"
144 do_test "echo -n > empty_file; ln empty_file link"
145
146 msg "relative symbolic link"
147 do_test "echo 1 > file; ln -s file symlink"
148
149 msg "absolute symbolic link"
150 do_test "echo 1 > file; ln -s /some/absolute/target symlink"
151
152 msg "large file"
153 do_test "dd if=/dev/zero of=file bs=4096 count=10 &> /dev/null"
154
155 msg "file with DOS name"
156 do_test "echo 1 > file; setfattr -v file -n system.ntfs_dos_name file"
157
158 msg "file with DOS name with alphabetically smaller hardlink in same directory"
159 do_test "echo 1 > file; setfattr -v file -n system.ntfs_dos_name file; ln file aaa_link"
160
161 msg "file with DOS name with alphabetically larger hardlink in same directory"
162 do_test "echo 1 > file; setfattr -v file -n system.ntfs_dos_name file; ln file zzz_link"
163
164 msg "file with long name and with DOS name with alphabetically smaller hardlink in same directory"
165 do_test 'echo 1 > file_with_a_long_name;
166          setfattr -v "file~1" -n system.ntfs_dos_name file_with_a_long_name;
167          ln file_with_a_long_name aaa_link'
168
169 msg "many nested directories"
170 do_test 'mkdir dir; mkdir dir/subdir; mkdir dir/subdir/subdir2; mkdir dir/subdir/subdir3'
171
172 msg "identical files and symlinks in subdirectory"
173 do_test 'mkdir dir;
174          echo 888 > dir/file;
175          echo 888 > dir/idfile2;
176          ln -s ../dir dir/circle; ln -s file dir/filelink'
177
178 msg "hard link group and identical files not hard linked"
179 do_test 'echo 888 > file;
180          echo 888 > file2;
181          ln file link;
182          ln file link2;
183          echo 888 > file3'
184
185 msg "file with named data stream"
186 do_test 'echo 1 > file;
187          setfattr -n user.ads -v 2 file'
188
189 msg "file with multiple named data streams"
190 do_test 'echo 1 > file;
191          setfattr -n user.a -v 1 file;
192          setfattr -n user.aa -v 11 file;
193          setfattr -n user.aaa -v 111 file;
194          setfattr -n user.aaaa -v 1111 file'
195
196 msg "file with multiple named data streams with same contents"
197 do_test 'echo 1 > file;
198          setfattr -n user.a -v 1111 file;
199          setfattr -n user.aa -v 1111 file;
200          setfattr -n user.aaa -v 1111 file;
201          setfattr -n user.aaaa -v 1111 file;'
202
203 msg "file with named data streams with same contents as other file"
204 do_test 'echo -n > file;
205          setfattr -n user.a -v 1111 file;
206          echo -n 1111 > otherfile;'
207
208 msg "file with empty named data stream and non-empty unnamed data stream"
209 do_test 'echo 1 > file;
210          setfattr -n user.ads -v "" file;'
211
212 msg "file with empty named data stream and empty unnamed data stream"
213 do_test 'echo -n > file;
214          setfattr -n user.ads -v "" file;'
215
216 msg "file with named data stream with hardlink"
217 do_test 'echo 999 > file;
218          setfattr -n user.ads -v "888" file;
219          ln file link;'
220
221 msg "file with named data stream with hardlink and DOS name"
222 do_test 'echo 999 > file;
223          setfattr -n user.ads -v "888" file;
224          ln file link;
225          setfattr -v DOSNAME -n system.ntfs_dos_name file;'
226
227 msg "C source code of wimlib"
228 do_test 'cp $srcdir/src/*.{c,h} .'
229
230 msg "file with security descriptor"
231 do_test 'touch file;
232          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_1.base64` file'
233
234 msg "files with different security descriptors"
235 do_test 'touch file;
236          touch file2;
237          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_1.base64` file
238          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_2.base64` file'
239
240 msg "files with different security descriptors and some with the same security descriptor"
241 do_test 'touch file;
242          touch file2;
243          touch file3;
244          mkdir dir;
245          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_1.base64` file
246          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_2.base64` file
247          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_1.base64` dir
248          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_1.base64` file3'
249
250 msg "tons of random stuff"
251 do_test 'echo -n 8 > file;
252          ln file hardlink;
253          ln -s hardlink symlink;
254          echo -n 8 > identical file;
255          dd if=/dev/urandom of=randomfile bs=4096 count=10 &>/dev/null;
256          mkdir dir;
257          setfattr -n system.ntfs_dos_name -v DOSNAME dir;
258          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_1.base64` dir
259          mkdir anotherdir;
260          cp file anotherdir;
261          ln file anotherdir/anotherhardlink;
262          ln -s .. anotherdir/anothersymlink;
263          ln -s anothersymlink anotherdir/symlinktosymlink;
264          echo -n 33 > anotherfile;
265          setfattr -n user.ads anotherfile -v 33;
266          setfattr -n user.ads2 anotherfile -v 8;
267          setfattr -n user.ads3 anotherfile -v 33;
268          echo -n > emptyfile;
269          setfattr -n user.ads emptyfile -v 8;
270          setfattr -n user.ads5 emptyfile -v"`cat $srcdir/src/hardlink.c`"
271          mkdir dir/subdir;
272          ln file dir/subdir/file;
273          echo -n 8 > dir/subdir/file2;
274          ln dir/subdir/file dir/subdir/link;
275          setfattr -n system.ntfs_dos_name -v 123 dir/subdir/link;
276          setfattr -n system.ntfs_acl -v 0s`cat $srcdir/tests/security_descriptor_1.base64` dir/subdir/link;
277          setfattr -n user.yet_another_ads -v "" dir/subdir/link;
278          setfattr -n user.yet_another_ads2 -v "" dir/subdir/link;
279          setfattr -n user.yet_another_ads3 -v "abc" dir/subdir/link;
280          setfattr -n user.yet_another_ads4 -v "" dir/subdir/link;'
281
282 cleanup
283
284 echo "**********************************************************"
285 echo "           NTFS capture/apply tests passed                "
286 echo "**********************************************************"