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