Imported Upstream version 0.10.1
[platform/upstream/libzip.git] / regress / runtest
1 #!/bin/sh
2
3 #  from ckmame:runtest,v 1.22 2005/12/27 09:41:51 dillo Exp
4 #
5 #  runtest -- run regression tests
6 #  Copyright (C) 2002-2007 Dieter Baron and Thomas Klausner
7 #
8 #  This file is part of libzip, a library to manipulate ZIP archives.
9 #  The authors can be contacted at <libzip@nih.at>
10 #
11 #  Redistribution and use in source and binary forms, with or without
12 #  modification, are permitted provided that the following conditions
13 #  are met:
14 #  1. Redistributions of source code must retain the above copyright
15 #     notice, this list of conditions and the following disclaimer.
16 #  2. Redistributions in binary form must reproduce the above copyright
17 #     notice, this list of conditions and the following disclaimer in
18 #     the documentation and/or other materials provided with the
19 #     distribution.
20 #  3. The names of the authors may not be used to endorse or promote
21 #     products derived from this software without specific prior
22 #     written permission.
23
24 #  THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
25 #  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 #  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 #  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
28 #  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 #  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
30 #  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 #  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
32 #  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33 #  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
34 #  IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36 # runtest TESTNAME
37 #
38 # files: 
39 #   TESTNAME.test: test scenario
40 #
41 # test scenario:
42 #    Lines beginning with # are comments.
43 #
44 #    The following commands are recognized; return and args must
45 #    appear exactly once, the others are optional.
46 #
47 #       args ARGS
48 #           run program with command line arguments ARGS
49 #       
50 #       description TEXT
51 #           description of what test is for
52 #
53 #       file TEST IN OUT
54 #           copy file IN as TEST, compare against OUT after program run.
55 #
56 #       file-del TEST IN
57 #           copy file IN as TEST, check that it is removed by program.
58 #
59 #       file-new TEST OUT
60 #           check that file TEST is created by program and compare
61 #           against OUT.
62 #
63 #       mkdir MODE NAME
64 #           create directory NAME with permissions MODE.
65 #
66 #       program PRG
67 #           run PRG.
68 #
69 #       return RET
70 #           RET is the expected exit code
71 #
72 #       stderr TEXT
73 #           program is expected to produce the error message TEXT.  If
74 #           multiple stderr commands are used, the messages are
75 #           expected in the order given.
76 #
77 #       stdout TEXT
78 #           program is expected to print TEXT to stdout.  If multiple
79 #           stdout commands are used, the messages are expected in
80 #           the order given. 
81 #   
82 # exit status
83 #       runtest uses the following exit codes:
84 #           0: test passed
85 #           1: test failed
86 #           2: other error
87 #          77: test was skipped
88
89 # environment variables:
90 #   VERBOSE: if set, be more verbose (e. g., output diffs)
91 #   NOCLEANUP: if set, don't delete directory test is run in
92
93 die() {
94         echo "$0: $*" >&2;
95         cleanup;
96         exit 2;
97 }
98
99 fail() {
100         if [ ! -z "${VERBOSE}" ]
101         then
102             echo "${TEST} -- FAILED: $*";
103         fi;
104         cleanup;
105         exit 1;
106 }
107
108 skip() {
109         if [ ! -z "${VERBOSE}" ]
110         then
111                 echo "${TEST} -- skipped: $*";
112         fi;
113         cleanup;
114         exit 77;
115 }
116
117 succeed() {
118         if [ ! -z "${VERBOSE}" ]
119         then
120                 echo "${TEST} -- passed";
121         fi
122         cleanup;
123         exit 0;
124 }
125
126 cleanup() {
127         cd ..;
128         if [ -z "${NOCLEANUP}" ]
129         then
130                 chmod -R u+rw ${DIR};
131                 rm -r ${DIR};
132         fi
133 }
134
135 check_in_out_exists() {
136     if [ ! -f "$2" ]
137     then
138         fail "missing output file: '$2'"
139     elif [ ! -f "$1" ]
140     then
141         die "cannot find input file '$1'"
142     fi
143 }
144
145 checkdb() {
146     check_in_out_exists "$1" "$2"
147     out=`../dbdump "$2" | sort | diff ${DIFF_FLAGS} "$1" -`
148     if [ $? -ne 0 ]
149     then
150         if [ $VERBOSE ]
151         then
152             echo "$out"
153         fi
154         fail "$3"
155     fi
156
157 }
158
159 checkfile() {
160     check_in_out_exists "$1" "$2"
161     out=`diff ${DIFF_FLAGS} "$1" "$2"`
162     if [ $? -ne 0 ]
163     then
164         if [ $VERBOSE ]
165         then
166             echo "$out"
167         fi
168         fail "$3"
169     fi
170 }
171
172 checkzip() {
173     check_in_out_exists "$1" "$2"
174     # quiet CRC errors
175     ${ZIPCMP} -t ${ZIPCMP_FLAGS} "$1" "$2" 2>/dev/null
176     if [ $? -ne 0 ]
177     then
178         fail "$3"
179     fi
180 }
181
182 test_empty() {
183     if [ ! -z "$1" ]
184     then
185         die "directive $2 appeared twice in test file"
186     fi
187 }
188
189 test_set() {
190     if [ -z "$1" ]
191     then
192         die "required directive $2 missing in test file"
193     fi
194 }
195
196 copy_file() {
197     src="${srcdir}/$1"
198
199     if [ ! -f "$src" ]
200     then
201         die "source file '$src' does not exist"
202     fi
203     dir=`dirname "$2"`
204     if [ ! -d "$dir" ]
205     then
206         mkdir -p "$dir"
207     fi
208     cp "$src" "$2"
209 }
210
211 # GNU sort behaves differently on locales other than C, breaking tests
212 LC_ALL=C
213 export LC_ALL
214
215 #testdir=`dirname $1`
216 TEST=`echo $1 | sed -e s',.*/,,' -e 's/\.test$//'`
217 shift
218
219 DIR=${TEST}.d$$
220 if [ -z "${srcdir}" ]
221 then
222     srcdir=..
223 else
224     # XXX: fix for absolute srcdir?
225     srcdir=../${srcdir}
226 fi
227
228 if [ -z "${ZIPCMP}" ]
229 then
230     ZIPCMP=zipcmp
231 else
232     if expr "${ZIPCMP}" : '[^/].*/' > /dev/null
233     then
234         ZIPCMP="../${ZIPCMP}"
235     fi
236 fi
237
238 if [ -z "${VERBOSE}" ]
239 then
240     DIFF_FLAGS=''
241     ZIPCMP_FLAGS='-q'
242 else
243     DIFF_FLAGS='-u'
244     ZIPCMP_FLAGS='-v'
245 fi
246
247 # XXX: set up trap to cleanup
248
249 mkdir ${DIR} || ( die "cannot create test directory ${DIR}" )
250 cd ${DIR} || ( die "cannot cd to test directory ${DIR}" )
251
252 {
253
254 RET=''
255 ARGS=''
256 FILES=''
257 FILES_SHOULD=''
258 DESCR=''
259
260 touch stderr stdout
261
262 while read cmd arg
263 do
264   case $cmd in
265   \#*) ;;
266   args)
267     test_empty "${ARGS}" args
268     ARGS="$arg";;
269   description)
270     test_empty "${DESCR}" description
271     DESCR="$arg";;
272   file)
273     set $arg
274     copy_file "$2" "$1"
275     FILES="${FILES} ${srcdir}/$3!$1";;
276   file-del)
277     set $arg
278     copy_file "$2" "$1";;
279   file-new)
280     set $arg
281     FILES="${FILES} ${srcdir}/$2!$1";;
282   mkdir)
283     set $arg
284     mkdir "$2" && chmod "$1" "$2";;
285   program)
286     PROGRAM=../"$arg";;
287   return)
288     test_empty "${RET}" return
289     RET="$arg";;
290   stderr)
291     echo "${PROGRAM}: $arg" >> stderr;;
292   stdout)
293     echo "$arg" >> stdout;;
294   *)
295     die "unknown directive '$cmd'"
296   esac
297 done
298
299 test_set "${RET}" return
300 test_set "${ARGS}" args
301
302 if [ -z "${PROGRAM}" ]
303 then
304     die no program to run given
305 fi
306
307 if [ ! -z "${SETUP_ONLY}" ]
308 then
309     echo ${DIR}
310     exit 0
311 fi
312
313 if [ ! -z "${VERBOSE}" ]
314 then
315         echo "${TEST}: ${DESCR}"
316         echo "running: ${PROGRAM} ${ARGS}"
317 fi
318
319 ${PROGRAM} ${ARGS} > gotout 2> goterr
320 ret=$?
321
322 if [ $ret -ne ${RET} ]
323 then
324     if [ ! -z "${VERBOSE}" ]
325     then
326         cat gotout
327         cat goterr
328     fi
329     fail "unexpected exit status: got $ret, expected ${RET}"
330 fi
331
332 FILES_SHOULD="${FILES_SHOULD} stderr stdout gotout goterr"
333
334 checkfile stderr goterr "unexpected error output"
335 checkfile stdout gotout "unexpected output"
336
337 if [ ! -z "${FILES}" ]
338 then
339     for fs in ${FILES}
340     do
341         set -- `echo ${fs} | tr '!' ' '`
342         FILES_SHOULD="${FILES_SHOULD} $2"
343         case "$2" in
344         *.db)
345             checkdb "$1" "$2" "database $2 wrong";;
346         *.zip)
347             checkzip "$1" "$2" "zip file $2 wrong";;
348         *)
349             checkfile "$1" "$2" "file $2 wrong";;
350         esac 
351     done
352 fi
353
354 # check that no additional files exist
355 echo gotfiles shouldfiles ${FILES_SHOULD} | tr ' ' '\012' | sort > shouldfiles
356 touch gotfiles
357 find . -type f -print | sed 's!^./!!' | sort > gotfiles
358
359 checkfile shouldfiles gotfiles "unexpected/missing files"
360
361 succeed
362
363 } < ${srcdir}/${TEST}.test