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