]> wimlib.net Git - wimlib/blob - tests/test-imagex
mount_image.c: add fallback definitions of RENAME_* constants
[wimlib] / tests / test-imagex
1 #!/usr/bin/env bash
2
3 # This script does some sanity testing of the 'wimlib-imagex' program.  It by no
4 # means tests every aspect of wimlib comprehensively.
5
6 set -e
7 cd tests
8 srcdir="${srcdir:-.}/.."
9 srcdir="$(cd $srcdir; pwd)"
10 . "$srcdir/tests/test_utils.sh"
11
12 TEST_SUBDIR=tmpdir_test-imagex
13
14 # Execute the tests in a subdirectory to avoid conflicts with concurrent tests
15 default_cleanup
16 mkdir $TEST_SUBDIR
17 cd $TEST_SUBDIR
18
19 # Make test directory
20 mkdir dir
21 cp $srcdir/src/*.c dir
22 mkdir dir/subdir
23 echo 'hello' > dir/subdir/hello
24 echo 'hello' > dir/subdir/hello2
25 ln dir/subdir/hello dir/subdir/hellolink
26 echo -n > dir/subdir/empty_file
27 ln -s hello dir/subdir/rel_symlink
28
29 mkdir dir2
30 echo 'testing' > dir2/file
31 dd if=/dev/zero of=dir2/zeroes bs=4096 count=5
32
33 # Capturing and applying WIM with None, LZX, and XPRESS compression
34
35 for comp_type in None LZX XPRESS; do
36         echo "Testing capture and application of $comp_type-compressed WIM"
37         if ! wimcapture dir dir.wim --compress=$comp_type; then
38                 error "'wimcapture' failed"
39         fi
40         if ! wimapply dir.wim tmp; then
41                 error "'wimapply' failed"
42         fi
43         if ! test `wim_ctype dir.wim` = "$comp_type"; then
44                 error "'wiminfo' didn't report the compression type correctly"
45         fi
46         if ! diff -q -r dir tmp; then
47                 error "Recursive diff of extracted directory with original failed"
48         fi
49         if ! test `get_link_count tmp/subdir/hello` = 2; then
50                 error "Incorrect number of hard links in extracted file"
51         fi
52         if ! test `get_inode_number tmp/subdir/hello` != `get_inode_number tmp/subdir/hello2`; then
53                 error "Expected different inode numbers in files not hard-linked"
54         fi
55         if ! test "`get_inode_number tmp/subdir/hello`" = "`get_inode_number tmp/subdir/hellolink`"; then
56                 error "Expected same inode numbers in hard-linked files"
57         fi
58         if ! test -L tmp/subdir/rel_symlink; then
59                 error "Symlink not extracted correctly"
60         fi
61         if ! test "`readlink tmp/subdir/rel_symlink`" = "hello"; then
62                 error "Symlink target not correct"
63         fi
64
65         rm -rf dir.wim tmp
66 done
67
68 # Test wimappend --create
69 rm -f dir.wim
70 if wimappend dir dir.wim; then
71         error "wimappend to nonexisting file unexpectedly succeeded"
72 fi
73 if ! wimappend dir dir.wim --create; then
74         error "wimappend --create to nonexisting file failed"
75 fi
76 if ! wimappend dir dir.wim --create; then
77         error "wimappend --create to existing file failed"
78 fi
79 if ! test "`wiminfo dir.wim | grep 'Image Count' | awk '{print $3}'`" = 2; then
80         error "Incorrect WIM image count after wimappend --create"
81 fi
82
83 # Capturing and modifying name, description, and bootable flag
84
85 echo "Testing capture of WIM with default name and description"
86 wimcapture dir dir.wim
87 if ! test "`wiminfo dir.wim | grep Name | awk '{print $2}'`" = "dir"; then
88         error "WIM name not set correctly"
89 fi
90 if ! test "`wiminfo dir.wim | grep Description | awk '{print $2}'`" = ""; then
91         error "WIM description not set correctly"
92 fi
93
94 echo "Testing capture of WIM with default boot flag"
95 wimcapture dir dir.wim
96 if ! test "`wiminfo dir.wim | grep '^Boot Index' | awk '{print $3}'`" = "0"; then
97         error "WIM boot flag not set correctly"
98 fi
99
100 echo "Testing changing image bootable flag"
101 if ! wiminfo dir.wim 1 --boot; then
102         error "Failed to change bootable image"
103 fi
104 if ! test "`wiminfo dir.wim | grep '^Boot Index' | awk '{print $3}'`" = "1"; then
105         error "Bootable image not changed correctly"
106 fi
107 echo "Testing changing image bootable flag"
108 if ! wiminfo dir.wim 0 --boot; then
109         error "Failed to reset bootable image"
110 fi
111 if ! test "`wiminfo dir.wim | grep '^Boot Index' | awk '{print $3}'`" = "0"; then
112         error "Bootable image not reset correctly"
113 fi
114 echo "Testing changing image bootable flag to invalid image (this should generate errors)"
115 if wiminfo dir.wim 2 --boot; then
116         error "Succeeded in changing bootable image to invalid number"
117 fi
118 if ! test "`wiminfo dir.wim | grep '^Boot Index' | awk '{print $3}'`" = "0"; then
119         error "Boot flag was changed even though the change command was supposed to fail"
120 fi
121 rm -rf dir.wim tmp
122
123 echo "Testing capture of WIM with name and description"
124 if ! wimcapture dir dir.wim "myname" "mydesc"; then
125         error "Failed to capture WIM with specified name and description"
126 fi
127 if ! test "`wiminfo dir.wim | grep Name | awk '{print $2}'`" = "myname"; then
128         error "WIM name not set correctly"
129 fi
130 if ! test "`wiminfo dir.wim | grep Description | awk '{print $2}'`" = "mydesc"; then
131         error "WIM name not set correctly"
132 fi
133 echo "Testing printing WIM lookup table"
134 if ! wiminfo --lookup-table dir.wim > /dev/null; then
135         error "Failed to print WIM lookup table"
136 fi
137 echo "Testing printing WIM header"
138 if ! wiminfo --header dir.wim > /dev/null; then
139         error "Failed to print WIM header"
140 fi
141 echo "Testing printing WIM XML info"
142 if ! wiminfo --xml dir.wim > /dev/null; then
143         error "Failed to print WIM XML data"
144 fi
145 echo "Testing extracting WIM XML info"
146 if ! wiminfo --extract-xml=dir.xml dir.wim; then
147         error "Failed to extract WIM XML data"
148 fi
149 echo "Testing printing WIM metadata"
150 if ! wimdir --detailed dir.wim > /dev/null; then
151         error "Failed to print WIM metadata"
152 fi
153 rm -rf dir.wim tmp dir.xml
154
155 echo "Testing capture of bootable WIM"
156 if ! wimcapture dir dir.wim --boot; then
157         error "Failed to capture bootable WIM"
158 fi
159 if ! test "`wiminfo dir.wim | grep '^Boot Index' | awk '{print $3}'`" = "1"; then
160         error "Boot flag on bootable WIM not set correctly"
161 fi
162 rm -rf dir.wim tmp
163
164 # Integrity table
165
166 echo "Testing capture of WIM with integrity table"
167 if ! wimcapture dir dir.wim --check; then
168         error "Failed to capture WIM with integrity table"
169 fi
170 if ! wiminfo dir.wim | grep -q Integrity; then
171         error "Integrity table on WIM not made"
172 fi
173 if ! wimapply --check dir.wim tmp; then
174         error "Integrity table on WIM not made correctly"
175 fi
176 if ! diff -q -r dir tmp; then
177         error "Recursive diff of applied WIM with original directory failed"
178 fi
179 rm -rf dir.wim tmp
180
181 # Appending and deleting images
182
183 echo "Testing appending WIM image"
184 wimcapture dir dir.wim
185 if ! wimappend dir2 dir.wim; then
186         error "Appending WIM image failed"
187 fi
188 if ! test "`wiminfo dir.wim | grep 'Image Count' | awk '{print $3}'`" = 2; then
189         error "WIM image count not correct"
190 fi
191
192 echo "Testing appending WIM image with existing name (this should generate errors)"
193 if wimappend dir2 dir.wim "dir"; then
194         error "Adding duplicate image name didn't fail"
195 fi
196 echo "Testing appending WIM image with new name"
197 if ! wimappend dir2 dir.wim "newname"; then
198         error "Appending WIM image failed"
199 fi
200 echo "Testing appending WIM image with integrity check"
201 if ! wimappend dir2 dir.wim "newname2" --check; then
202         error "Appending WIM image failed"
203 fi
204 if ! wiminfo dir.wim | grep -q Integrity; then
205         error "Integrity table not set correctly on image append"
206 fi
207 echo "Testing appending WIM image with no integrity check"
208 if ! wimappend dir2 dir.wim "newname3" --nocheck; then
209         error "Appending WIM image failed"
210 fi
211 if wiminfo dir.wim | grep -q Integrity; then
212         error "WIM integrity table not removed"
213 fi
214 # 5 images at this point
215 if ! test "`wiminfo dir.wim | grep 'Image Count' | awk '{print $3}'`" = 5; then
216         error "WIM does not contain the expected 5 images"
217 fi
218 echo "Testing deleting first WIM image"
219 if ! wimdelete dir.wim 1; then
220         error "Failed to delete WIM image"
221 fi
222 if ! test "`wiminfo dir.wim | grep 'Image Count' | awk '{print $3}'`" = 4; then
223         error "WIM image not deleted correctly"
224 fi
225 echo "Testing deleting last WIM image"
226 if ! wimdelete dir.wim 4; then
227         error "Failed to delete WIM image"
228 fi
229 if ! test "`wiminfo dir.wim | grep 'Image Count' | awk '{print $3}'`" = 3; then
230         error "WIM image not deleted correctly"
231 fi
232 echo "Testing deleting invalid WIM image (this should generate errors)"
233 if wimdelete dir.wim 4; then
234         error "Expected to fail to delete non-existent WIM image"
235 fi
236 if ! test "`wiminfo dir.wim | grep 'Image Count' | awk '{print $3}'`" = 3; then
237         error "Image count changed even though we intentionally failed to delete an image"
238 fi
239 echo "Testing deleting all WIM images"
240 if ! wimdelete dir.wim all; then
241         error "Failed to delete all images from WIM"
242 fi
243 if ! test "`wiminfo dir.wim | grep 'Image Count' | awk '{print $3}'`" = 0; then
244         error "Couldn't delete all WIM images correctly"
245 fi
246 echo "Testing appending directory to empty WIM and making it bootable"
247 if ! wimappend dir dir.wim "myname" "mydesc" --check --boot; then
248         error "Couldn't append named, described, bootable image to empty WIM with integrity check"
249 fi
250 if ! wiminfo dir.wim | grep -q Integrity; then
251         error "Integrity check not found"
252 fi
253 if ! test "`wiminfo dir.wim | grep '^Boot Index' | awk '{print $3}'`" = "1"; then
254         error "Bootable image not set correctly"
255 fi
256 echo "Testing appending non-directory (should generate errors)"
257 if wimappend dir.wim dir.wim; then
258         error "Incorrectly succeeded to append non-directory to WIM"
259 fi
260 echo "Testing appending non-existent file (should generate errors)"
261 if wimappend SOME_NONEXISTENT_FILE dir.wim; then
262         error "Incorrectly succeeded to append non-existent file to WIM"
263 fi
264 if [ `id -u` != 0 ]; then
265         echo "Testing appending directory containing unreadable file (should generate errors)"
266         mkdir -p dir3
267         echo 1 > dir3/file
268         chmod -r dir3/file
269         if wimappend dir3 dir.wim; then
270                 error "Incorrectly succeeded in capturing directory with unreadable file"
271         fi
272 fi
273 rm -rf dir3 dir.wim
274
275 # Applying multiple images, applying with hardlinks/symlinks
276
277 echo "Testing application of multiple images"
278 if ! wimcapture dir dir.wim; then
279         error "Failed to prepare test WIM"
280 fi
281 if ! wimappend dir dir.wim "myname"; then
282         error "Failed to append image to test WIM"
283 fi
284 if ! wimapply dir.wim all tmp; then
285         error "Applying multiple images failed"
286 fi
287 if ! diff -q -r tmp/dir tmp/myname || ! diff -q -r dir tmp/dir; then
288         error "Recursive diff of applied WIM with original directory failed"
289 fi
290 if test "`get_link_count tmp/dir/write.c`" != 1; then
291         error "Incorrect link count on extracted file"
292 fi
293 if test "`get_link_count tmp/myname/write.c`" != 1; then
294         error "Incorrect link count on extracted file"
295 fi
296 if test "`get_inode_number tmp/myname/write.c`" = "`get_inode_number tmp/dir/write.c`"; then
297         error "Incorrect inode number"
298 fi
299 rm -rf tmp
300
301 echo "Testing application of single image containing identical files"
302 if ! wimapply dir.wim 1 tmp; then
303         error "Failed to apply WIM"
304 fi
305 if test "`get_link_count tmp/subdir/hello`" != 2; then
306         error "Incorrect link count on extracted file"
307 fi
308 if test "`get_link_count tmp/subdir/hello2`" != 1; then
309         error "Incorrect link count on extracted file"
310 fi
311 if test "`get_inode_number tmp/subdir/hello`" = "`get_inode_number tmp/subdir/hello2`"; then
312         error "Inode numbers on non-hard-linked files are the same"
313 fi
314 if test "`get_inode_number tmp/subdir/hello`" != "`get_inode_number tmp/subdir/hellolink`"; then
315         error "Inode numbers on hard-linked files are different"
316 fi
317 rm -rf tmp
318
319 # wimsplit, wimjoin
320
321 echo "Creating random files to test WIM splitting on"
322 mkdir tmp
323 for ((i = 0; i < 100; i++)); do
324         dd if=/dev/urandom of=tmp/file$i bs=4096 count=10 &> /dev/null
325 done
326 for flag in "--compress=none" "--compress=maximum" "--compress=fast"; do
327         echo "Using flag $flag"
328         if ! wimcapture tmp tmp.wim $flag; then
329                 error "Failed to capture test WIM"
330         fi
331         echo "Splitting WIM into 1 MiB chunks"
332         if ! wimsplit tmp.wim tmp.swm 1; then
333                 error "Failed to split WIM"
334         fi
335         echo "Verifying the split WIMs (some errors expected)"
336         if test "`wiminfo tmp.swm | grep 'Part Number' | awk '{print $3}'`" != "1/4"; then
337                 error "Part number of split WIM not correct"
338         fi
339         if ! wimdir tmp.swm > /dev/null; then
340                 error "Failed to list files in split WIM"
341         fi
342         if ! test -e tmp2.swm; then
343                 error "Could not find split-WIM part 2"
344         fi
345         if wimdir tmp2.swm > /dev/null; then
346                 error "Listed files in part 2 of split WIM (this should have failed)"
347         fi
348
349         # Unsupported, should fail
350         if wiminfo tmp.swm --boot 1; then
351                 error "Should not have been able to change boot index of split WIM"
352         fi
353         echo "Joining the split WIMs and applying the result"
354         if ! wimjoin tmp2.wim tmp*.wim; then
355                 error "Failed to join split WIMs"
356         fi
357         if ! wimapply tmp2.wim tmp2; then
358                 error "Failed to apply joined split WIM"
359         fi
360         if ! wimapply tmp.wim tmp3; then
361                 error "Failed to apply test WIM"
362         fi
363         if ! diff -q -r tmp tmp2 || ! diff -q -r tmp tmp3; then
364                 error "Recursive diff of applied joined split WIM with original directory failed"
365         fi
366         rm -f *.wim *.swm
367         rm -rf tmp2 tmp3
368 done
369 rm -rf tmp
370
371 # wimexport
372 echo "Testing export of single image to new WIM"
373 if ! wimcapture dir dir.wim; then
374         error "Failed to capture test WIM"
375 fi
376 if ! wimappend dir2 dir.wim; then
377         error "Failed to append image to test WIM"
378 fi
379 if ! wimexport dir.wim dir new.wim; then
380         error "Failed to export single image to new WIM"
381 fi
382 if test "`wiminfo new.wim | grep 'Image Count' | awk '{print $3}'`" != 1; then
383         error "Exporting single image to new WIM wasn't done correctly"
384 fi
385 echo "Testing export of single image to existing WIM"
386 if ! wimexport dir.wim dir2 new.wim; then
387         error "Failed to export single image to existing WIM"
388 fi
389 if test "`wiminfo new.wim | grep 'Image Count' | awk '{print $3}'`" != 2; then
390         error "Exporting single image to existing WIM wasn't done correctly"
391 fi
392 echo "Testing export of single image to existing WIM using wrong compression type"
393 if wimexport dir.wim dir2 new.wim newname --compress=xpress; then
394         error "Successfully exported image using wrong compression type"
395 fi
396 rm -f new.wim
397 echo "Testing export of multiple images to new WIM"
398 if ! wimexport dir.wim all new.wim; then
399         error "Failed to export multiple images to new WIM"
400 fi
401 if test "`wiminfo new.wim | grep 'Image Count' | awk '{print $3}'`" != 2; then
402         error "Exporting multiple images to new WIM wasn't done correctly"
403 fi
404 if ! wimcapture dir2 new.wim newname; then
405         error "Failed to capture test WIM"
406 fi
407 echo "Testing export of multiple images to existing WIM"
408 if ! wimexport dir.wim all new.wim; then
409         error "Failed to export multiple images to existing WIM"
410 fi
411 echo "Testing export of multiple images to existing WIM with --boot"
412 if ! wimcapture dir2 new.wim newname; then
413         error "Failed to capture test WIM"
414 fi
415 if ! wiminfo dir.wim --boot 1; then
416         error "Failed to set boot index on test WIM"
417 fi
418 if ! wimexport dir.wim all new.wim --boot; then
419         error "Failed to export multiple images to existing WIM with bootable image"
420 fi
421 echo "Testing export of multiple images to existing WIM with --boot, but no bootable image (errors expected)"
422 if ! wimcapture dir2 new.wim newname; then
423         error "Failed to capture test WIM"
424 fi
425 if ! wiminfo dir.wim --boot 0; then
426         error "Failed to clear boot index on test WIM"
427 fi
428 if wimexport dir.wim all new.wim --boot; then
429         error "Successfully exported multiple images with --boot but with no bootable images"
430 fi
431
432 # Test exporting an image to another WIM, then applying it.
433 # We try with 5 different combinations of compression types to make sure we go
434 # through all paths in the resource-handling code.
435 for i in 1 2 3 4 5; do
436         case $i in
437         1)
438                 cflag1="--compress=none";
439                 cflag2="--compress=none";
440                 ;;
441         2)
442                 cflag1="--compress=xpress";
443                 cflag2="--compress=xpress";
444                 ;;
445         3)
446                 cflag1="--compress=xpress"
447                 cflag2="--compress=lzx"
448                 ;;
449         4)
450                 cflag1="--compress=none"
451                 cflag2="--compress=xpress"
452                 ;;
453         5)
454                 cflag1="--compress=xpress"
455                 cflag2="--compress=none"
456                 ;;
457         esac
458         echo "Testing exporting then applying an image (\"$cflag1\" => \"$cflag2\")"
459         rm -rf dir.wim new.wim tmp tmp2
460         wimcapture dir dir.wim $cflag1
461         wimcapture dir2 dir2.wim $cflag2
462         wimexport dir.wim dir dir2.wim
463         wimapply dir.wim dir tmp
464         if ! wimapply dir2.wim dir tmp2; then
465                 error "Failed to apply image that was exported to a WIM"
466         fi
467         if ! diff -r tmp tmp2; then
468                 error "Image that was exported to a WIM was not applied correctly"
469         fi
470 done
471
472 echo "**********************************************************"
473 echo "             Basic wimlib-imagex tests passed             "
474 echo "**********************************************************"
475
476 # Leave test subdirectory and cleanup
477 cd ..
478 default_cleanup