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