]> wimlib.net Git - wimlib/blob - tests/test-imagex-capture_and_apply
Make absolute ExclusionException patterns include ancestor directories
[wimlib] / tests / test-imagex-capture_and_apply
1 #!/usr/bin/env bash
2
3 # Test capturing and applying a WIM image in the normal (non-NTFS) capture mode
4 #
5 # Add in some tests with WIM splitting, joining, and exporting as well.
6 #
7 # Test all three compression modes (None, XPRESS, and LZX).
8 #
9 # Also, test if the capture configuration file works correctly.
10
11 set -e
12 cd tests
13 srcdir="${srcdir:-.}/.."
14 srcdir="$(cd $srcdir; pwd)"
15 . "$srcdir/tests/test_utils.sh"
16
17 TEST_SUBDIR=tmpdir_test-imagex-capture_and_apply
18
19 do_tree_cmp() {
20         if ! ../tree-cmp in.dir out.dir; then
21                 if [ -x /usr/bin/tree ]; then
22                         echo "Dumping tree of applied image"
23                         echo "(Note: compression type was $ctype)"
24                         tree out.dir --inodes -F -s --noreport
25                         error 'Information was lost or corrupted while capturing
26                                 and then applying a directory tree'
27                 fi
28         fi
29 }
30
31 image_name=0
32 do_test() {
33         for ctype in None XPRESS LZX; do
34
35                 # Can we capture the WIM, apply it, and get the same result?
36                 cd in.dir
37                 eval "$1"
38                 cd ..
39                 if [ -x /usr/bin/tree -a "$ctype" = "None" ]; then
40                         tree in.dir --inodes -F -s --noreport
41                 fi
42                 if ! wimcapture in.dir test.wim --compress=$ctype --norpfix; then
43                         error "Failed to capture directory tree into a WIM"
44                 fi
45                 if ! wimapply test.wim 1 out.dir; then
46                         error "Failed to apply WIM to directory"
47                 fi
48                 if [ `wim_ctype test.wim` != $ctype ]; then
49                         error "'wiminfo' didn't report the compression type on the captured WIM correctly"
50                 fi
51                 do_tree_cmp
52                 rm -rf out.dir/*
53
54                 # Can we split the WIM, apply the split WIM, join the split WIM,
55                 # and apply the joined WIM, and get the same results every time?
56                 #
57                 # LC_ALL=C avoids locale-dependent floating point number
58                 # parsing.
59                 if ! LC_ALL=C wimsplit test.wim test.swm 0.01; then
60                         error "Failed to split WIM"
61                 fi
62                 if ! wimapply test.swm 1 out.dir --ref "test*.swm" ; then
63                         error "Failed to apply split WIM"
64                 fi
65                 do_tree_cmp
66                 rm -rf out.dir/* test.wim
67                 if ! wimjoin test.wim test*.swm; then
68                         error "Failed to join split WIM"
69                 fi
70                 if ! wimapply test.wim out.dir; then
71                         error "Failed to apply joined WIM"
72                 fi
73                 do_tree_cmp
74                 rm -rf out.dir/*
75
76                 # Can we export the image to another WIM, apply it, and get the
77                 # same results?
78                 (( image_name++ )) || true
79                 if ! wimexport test.wim 1 test2.wim "$image_name"; then
80                         error "Failed to export WIM image"
81                 fi
82
83                 if ! wimapply test2.wim "$image_name" out.dir; then
84                         error "Failed to apply exported WIM image"
85                 fi
86                 do_tree_cmp
87                 rm -rf out.dir/*
88
89                 # Try pipable WIM (don't bother testing all compression types
90                 # though, it shouldn't make a difference).
91                 if [ "$ctype" = "None" ]; then
92                         # Capture pipable WIM (not writing to pipe)
93                         if ! wimcapture in.dir test.wim \
94                                         --compress=$ctype --norpfix --pipable; then
95                                 error "Failed to capture directory tree into a pipable WIM"
96                         fi
97
98                         # Apply pipable WIM (reading from pipe)
99                         if ! cat test.wim | wimapply - 1 out.dir; then
100                                 error "Failed to apply pipable WIM to directory (from pipe)"
101                         fi
102                         do_tree_cmp
103                         rm -rf out.dir/*
104
105                         # Apply pipable WIM (not reading from pipe)
106                         if ! wimapply test.wim 1 out.dir; then
107                                 error "Failed to apply pipable WIM to directory (not from pipe)"
108                         fi
109                         do_tree_cmp
110                         rm -rf out.dir/*
111
112                         # Capture pipable WIM (writing to pipe) and read pipable
113                         # WIM (reading from pipe)
114                         if ! wimlib_imagex capture --pipable --compress=$ctype \
115                                                 --norpfix --pipable         \
116                                             in.dir - | wimapply - 1 out.dir; then
117                                 error "Failed to capture directory tree into a pipable WIM"
118                         fi
119                         do_tree_cmp
120                         rm -rf out.dir/*
121                 fi
122
123                 rm -rf out.dir/* in.dir/* test.wim test*.swm
124
125         done
126 }
127
128 __msg() {
129         echo "--------------------------------------------------------------------"
130         echo $1
131         echo "--------------------------------------------------------------------"
132 }
133
134 msg() {
135         __msg "Testing image capture and application of directory containing $1"
136 }
137
138 default_cleanup
139 mkdir $TEST_SUBDIR
140 cd $TEST_SUBDIR
141 mkdir in.dir out.dir
142
143 . $srcdir/tests/common_tests.sh
144
145 # Make sure exclusion list works
146 __msg "Testing default capture configuration file"
147 touch in.dir/hiberfil.sys
148 mkdir -p "in.dir/System Volume Information/subdir"
149 wimcapture in.dir test.wim
150 wimapply test.wim out.dir
151 if [ -e out.dir/hiberfil.sys -o -e "out.dir/System Volume Information" ]; then
152         error "Files were not excluded from capture as expected"
153 fi
154
155 #
156 # Execute one of the exclusion list test cases in tests/exclusionlists/.
157 # The file is divided into two sections, separated by a line containing "@@@".
158 # The first is the capture configuration file.  The second is a list of files
159 # and directories, where the ones that should be excluded are marked with "- ".
160 #
161 exclusionlist_test() {
162         local t_file="$1"
163         local in_paths_section=false
164         local path
165         local include
166
167         __msg "Testing exclusion list: ${t_file##*/}"
168
169         echo "/" > expected_out
170         sed -n -e '/^@@@/q;p' "$t_file" > config.txt
171         rm -rf in.dir
172         mkdir in.dir
173         cd in.dir
174         while read -r path; do
175                 path="$(echo "$path" | sed -e 's/[[:space:]]*$//')"
176                 if ! $in_paths_section; then
177                         if [ "$path" = "# case insensitive" ]; then
178                                 export WIMLIB_IMAGEX_IGNORE_CASE=1
179                         elif [ "$path" = "@@@" ]; then
180                                 in_paths_section=true
181                         fi
182                         continue
183                 fi
184                 if [ -z "$path" ]; then
185                         continue
186                 fi
187                 include=true
188                 if [ "${path:0:2}" = "- " ]; then
189                         include=false
190                         path="${path:2}"
191                 fi
192                 if [ "${path: -1}" = "/" ]; then
193                         path="${path:0:$(( ${#path} - 1))}"
194                         mkdir "$path"
195                 else
196                         touch "$path"
197                 fi
198                 if $include; then
199                         echo "/$path" >> ../expected_out
200                 fi
201         done < "$t_file"
202         cd ..
203         $in_paths_section || error "malformed test file: $t_file (never found separator)"
204         wimcapture in.dir test.wim --compress=none --config=config.txt
205         unset WIMLIB_IMAGEX_IGNORE_CASE
206         wimdir test.wim 1 > actual_out
207         diff expected_out actual_out
208 }
209
210 for t_file in "$srcdir/tests/exclusionlists"/*; do
211         exclusionlist_test "$t_file"
212 done
213
214 # Make sure reparse point fixups are working as expected
215 __msg "Testing --rpfix"
216 rm -r in.dir out.dir
217 mkdir in.dir
218 ln -s $PWD/in.dir          in.dir/absrootlink
219 ln -s $PWD/in.dir////      in.dir/absrootlinkslashes
220 ln -s /___NONEXISTENT___   in.dir/absnonexistent
221 ln -s /usr/bin/env         in.dir/absoutoftree
222 ln -s file                 in.dir/relalink
223 ln -s $PWD/in.dir/file     in.dir/abslink
224 ln -s $PWD/in.dir/file///  in.dir/abslinkslashes
225 wimcapture --rpfix in.dir test.wim
226 wimapply --norpfix test.wim out.dir
227 if [[ `readlink out.dir/absrootlink` != "/" ]] ||
228    [[ `readlink out.dir/absrootlinkslashes` != "////" ]]; then
229         error "wimcapture --rpfix failed to fix absolute link to capture root"
230 fi
231
232 if [[ ! -L out.dir/absnonexistent ]] ||
233    [[ ! -L out.dir/absoutoftree ]]; then
234         error "wimcapture --rpfix failed to also capture out of tree absolute links"
235 fi
236 if [[ `readlink out.dir/relalink` != "file" ]]; then
237         error "wimcapture --rpfix failed to capture relative symlink"
238 fi
239 if [[ `readlink out.dir/abslink` != "/file" ]] ||
240    [[ `readlink out.dir/abslinkslashes` != "/file///" ]]; then
241         error "wimcapture --rpfix did fix absolute link properly"
242 fi
243 rm -rf out.dir
244
245 wimapply test.wim out.dir
246 if [[ $(get_inode_number $(readlink out.dir/absrootlink)) != \
247         $(get_inode_number out.dir) ]];
248 then
249         error "wimapply failed to apply fixed absolute symlinks"
250 fi
251
252 # Make sure source list mode is working as expected
253 __msg "Testing source list capture mode"
254 rm -rf in.dir out.dir
255 mkdir in.dir
256 echo 1 > in.dir/1
257 ln in.dir/1 in.dir/1link
258 echo 5 > 5
259 mkdir otherdir
260 cp $srcdir/src/add_image.c otherdir
261 cat > srclist << EOF
262 in.dir /
263 5      /5
264 otherdir /otherdir
265 EOF
266 wimcapture srclist --source-list test.wim
267 wimapply test.wim out.dir
268 if [[ ! -f out.dir/5 || ! -f out.dir/1 || ! -f out.dir/1link || \
269       ! -d out.dir/otherdir ]]; then
270         error "source list capture failed to work as expected"
271 fi
272
273 # Still testing source list capture: add quoted name, and try overlay
274 rm -rf out.dir
275 cat > srclist << EOF
276 in.dir /
277 5      /5
278 otherdir /otherdir
279  "overlay dir 1"                'otherdir'      
280  "overlay dir 2"                'otherdir'  
281 EOF
282 mkdir "overlay dir 1"
283 mkdir "overlay dir 2"
284 echo A > "overlay dir 1"/A
285 echo B > "overlay dir 2"/B
286 wimcapture srclist --source-list test.wim
287 wimapply test.wim out.dir
288 if [[ ! -f out.dir/5 || ! -f out.dir/1 || ! -f out.dir/1link || \
289       ! -f out.dir/otherdir/A || ! -f out.dir/otherdir/B ]]; then
290         error "source list capture (with quoted names and basic overlay) failed to work as expected"
291 fi
292
293 # Try deep overlay
294 rm -rf in.dir out.dir "overlay dir 1" "overlay dir 2"
295 mkdir -p in.dir.1/subdir/subdir2 in.dir.2/subdir/subdir2
296 cat > srclist << EOF
297 in.dir.1        /
298 in.dir.2        /
299 EOF
300 echo 1 > in.dir.1/subdir/1
301 echo 2 > in.dir.2/subdir/2
302 echo 3 > in.dir.1/subdir/subdir2/3
303 echo 4 > in.dir.2/subdir/subdir2/4
304 wimcapture srclist --source-list test.wim
305 wimapply test.wim out.dir
306 if [[ ! -f out.dir/subdir/1 || ! -f out.dir/subdir/2 || \
307         ! -f out.dir/subdir/subdir2/3 || ! -f out.dir/subdir/subdir2/4 ]]; then
308         error "source list capture (with deep overlay) failed to work as expected"
309 fi
310
311 # Try bad overlay
312 __msg "Testing bad overlay (errors expected)"
313 rm -rf out.dir
314 echo 5 > 5
315 cat > srclist << EOF
316 in.dir.1        /
317 in.dir.2        /
318 5               /subdir
319 EOF
320 if wimcapture srclist --source-list test.wim; then
321         error "unexpected success in bad overlay with --source-list!"
322 fi
323
324 echo "**********************************************************"
325 echo "          wimcapture/apply tests passed               "
326 echo "**********************************************************"
327
328 cd ..
329 default_cleanup