zip-reader: Report corrupted Zip archives.
[pspp] / tests / libpspp / zip.at
1 dnl PSPP - a program for statistical analysis.
2 dnl Copyright (C) 2017 Free Software Foundation, Inc.
3 dnl
4 dnl This program is free software: you can redistribute it and/or modify
5 dnl it under the terms of the GNU General Public License as published by
6 dnl the Free Software Foundation, either version 3 of the License, or
7 dnl (at your option) any later version.
8 dnl
9 dnl This program is distributed in the hope that it will be useful,
10 dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
11 dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 dnl GNU General Public License for more details.
13 dnl
14 dnl You should have received a copy of the GNU General Public License
15 dnl along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 dnl
17 AT_BANNER([zip])
18
19 AT_SETUP([basic zip - unzip test])
20 AT_KEYWORDS([compression])
21 here=`pwd`
22 dir1=$here/original
23 dir2=$here/recovered
24
25 mkdir -p "$dir1"
26
27 # Generate files of differing sizes with random data in them
28 names=""
29 s=1;
30 while test $s -le 8192 ; do
31         name="$dir1/$s"
32         dd if=/dev/urandom of="$name" count=1 bs=$s 2> /dev/null
33         s=$(($s * 2));
34         bn=`basename "$name"`;
35         names="$names $bn";
36 done
37
38 AT_CHECK([cd "$dir1" && zip-test w foo.zip $names])
39
40 # If zipinfo is installed, make sure it can read the zipfile.
41 if (zipinfo -v) >/dev/null 2>&1; then
42     AT_CHECK([zipinfo "$dir1/foo.zip"], [0], [ignore])
43 fi
44
45 mkdir -p "$dir2"
46 cp "$dir1/foo.zip" "$dir2"
47 cd "$dir2"
48
49 AT_CHECK([zip-test r foo.zip $names])
50
51 AT_CHECK([
52     # Compare the files to their originals
53     for f in $names; do
54      diff "$dir1/$f" "$dir2/$f";
55      if test $? -ne 0 ; then exit 1; fi;
56     done
57 ])
58 AT_CLEANUP
59
60 AT_SETUP([zip - detect corruption on unzip])
61 AT_KEYWORDS([compression])
62 mkdir write
63 cd write
64 AT_DATA([data.txt], [xyzzy
65 ])
66 AT_CHECK([zip-test w ../foo.zip data.txt])
67 cd ..
68
69 mkdir extract
70 cd extract
71 AT_CHECK([zip-test r ../foo.zip data.txt])
72 AT_CHECK([cat data.txt], [0], [xyzzy
73 ])
74 cd ..
75
76 mkdir error
77 cd error
78 sed 's/xyzzy/XYZZY/' < ../foo.zip > ../corrupted.zip
79 AT_CHECK([zip-test r ../corrupted.zip data.txt], [1], [], [dnl
80 Unzip failed: ../corrupted.zip: corrupt archive reading member "data.txt": bad CRC 0x2a2bcd20 (expected e1bd2a6e)
81 ])
82 AT_CLEANUP
83
84 AT_SETUP([zip to pipe])
85 AT_KEYWORDS([compression])
86 here=`pwd`
87 dir1=$here/original
88 dir2=$here/recovered
89
90 mkdir -p "$dir1"
91
92 # Generate files of differing sizes with random data in them
93 names=""
94 s=1;
95 while test $s -le 8192 ; do
96         name="$dir1/$s"
97         dd if=/dev/urandom of="$name" count=1 bs=$s 2> /dev/null
98         s=$(($s * 2));
99         bn=`basename "$name"`;
100         names="$names $bn";
101 done
102
103 # The pipe through "cat" below is essential because it makes the
104 # output file un-seekable.
105 AT_CHECK([cd "$dir1" && zip-test w - $names | cat > foo.zip])
106
107 # If zipinfo is installed, make sure it can read the zipfile.
108 if (zipinfo -v) >/dev/null 2>&1; then
109     AT_CHECK([zipinfo "$dir1/foo.zip"], [0], [ignore])
110 fi
111
112 mkdir -p "$dir2"
113 cp "$dir1/foo.zip" "$dir2"
114 cd "$dir2"
115
116 AT_CHECK([zip-test r foo.zip $names])
117
118 AT_CHECK([
119     # Compare the files to their originals
120     for f in $names; do
121      diff "$dir1/$f" "$dir2/$f";
122      if test $? -ne 0 ; then exit 1; fi;
123     done
124 ])
125 AT_CLEANUP