chiark / gitweb /
asshelp.c: add a lot of debug logging
[gnupg2.git] / tests / pkits / common.sh
1 # common.sh - common defs for all tests         -*- sh -*-
2 # Copyright (C) 2004, 2008 Free Software Foundation, Inc.
3 #
4 # This file is part of GnuPG.
5
6 # GnuPG is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10
11 # GnuPG is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, see <http://www.gnu.org/licenses/>.
18
19 # reset some environment variables because we do not want to test locals
20 LANG=C
21 LANGUAGE=C
22 LC_ALL=C
23 export LANG LANGUAGE LC_ALL
24
25 pgmname=`basename $0`
26
27 if [ "$1" = "--debug" ]; then
28   debug=yes
29   set -x
30 else
31   debug=
32 fi
33 [ -z "$srcdir" ] && srcdir="."
34 [ -z "$top_srcdir" ] && top_srcdir=".."
35 [ -z "$GPGSM" ] && GPGSM="../../sm/gpgsm"
36 [ -z "$silent" ] && silent=no
37
38 AWK=awk
39 SCRATCH="scratch.$$.tmp"
40
41 # We use this as the faked system time for certain tests.
42 MYTIME="20080508T120000"
43
44
45 if [ "$GNUPGHOME" != "`/bin/pwd`" ]; then
46     echo "inittests: please set GNUPGHOME to the tests/pkits directory" >&2
47     exit 1
48 fi
49
50 if [ -n "$GPG_AGENT_INFO" ]; then
51     echo "inittests: please unset GPG_AGENT_INFO" >&2
52     exit 1
53 fi
54
55 if [ -f PKITS_data.tar.bz2 ]; then
56   :
57 else
58     if [ "$pgmname" = "import-all-certs" ]; then
59         if [ "$silent" = "yes" ]; then tmp1="Note: "; tmp2='      '
60         else tmp1="- ____ "; tmp2="$tmp1"
61         fi
62         echo "${tmp1}PKITS_data.tar.bz2 is not installed"
63         echo "${tmp2}All tests will be skipped (this is not an error)"
64     fi
65     # Exit code 77 is used by the Makefile for skipping a tests.
66     exit 77
67 fi
68
69 #--------------------------------
70 #------ utility functions -------
71 #--------------------------------
72
73 echo_n_init=no
74 echo_n () {
75   if test "$echo_n_init" = "no"; then
76     if (echo "testing\c"; echo 1,2,3) | grep c >/dev/null; then
77       if (echo -n testing; echo 1,2,3) | sed s/-n/xn/ | grep xn >/dev/null; then
78         echo_n_n=
79         echo_n_c='
80 '
81       else
82         echo_n_n='-n'
83         echo_n_c=
84       fi
85     else
86       echo_n_n=
87       echo_n_c='\c'
88     fi
89     echo_n_init=yes
90   fi
91   echo $echo_n_n "${1}$echo_n_c"
92 }
93
94 setup_output () {
95   if [ -z "$first_section_set" ]; then
96       first_section_set=$section
97   fi
98   section_out="$(echo $section)"
99   if [ -z "$section_out" ]; then
100       section_out="-"
101   fi
102 }
103
104 fatal () {
105     echo "$pgmname: fatal:" $* >&2
106     if [ "$silent" != "yes" ]; then
107         echo "$section_out ERROR: $* (fatal)"
108     fi
109     exit 1;
110 }
111
112 error () {
113     echo "$pgmname:" $* >&2
114     if [ "$silent" != "yes" ]; then
115         echo "$section_out ERROR: $*"
116     fi
117     exit 1
118 }
119
120 info () {
121     setup_output
122     echo "$pgmname:" $* >&2
123     if [ "$silent" != "yes" ]; then
124         echo "$section_out ____ $*"
125     fi
126 }
127
128 info_n () {
129     setup_output
130     echo_n "$pgmname:" $* >&2
131 }
132
133 pass () {
134     setup_output
135     echo "PASS: " $* >&2
136     pass_count=`expr ${pass_count} + 1`
137     if [ "$silent" != "yes" ]; then
138         echo_n "$section_out PASS"
139         if [ -n "$1" ]; then echo_n " $1"
140         elif [ -n "$description" ]; then echo_n " ($description)"
141         fi
142         echo
143     fi
144 }
145
146 fail () {
147     setup_output
148     echo "FAIL: " $* >&2
149     fail_count=`expr ${fail_count} + 1`
150     if [ "$silent" != "yes" ]; then
151         echo_n "$section_out FAIL"
152         if [ -n "$1" ]; then echo_n " $1"
153         elif [ -n "$description" ]; then echo_n " ($description)"
154         fi
155         echo
156     fi
157 }
158
159 skip () {
160     setup_output
161     echo "SKIP: " $* >&2
162     skip_count=`expr ${skip_count} + 1`
163     if [ "$silent" != "yes" ]; then
164         echo_n "$section_out SKIP"
165         if [ -n "$1" ]; then echo_n " $1"
166         elif [ -n "$description" ]; then echo_n " ($description)"
167         fi
168         echo
169     fi
170 }
171
172 unresolved () {
173     setup_output
174     echo "UNRESOLVED: " $* >&2
175     unresolved_count=`expr ${unresolved_count} + 1`
176     if [ "$silent" != "yes" ]; then
177         echo_n "$section_out UNRESOLVED"
178         if [ -n "$1" ]; then echo_n " $1"
179         elif [ -n "$description" ]; then echo_n " ($description)"
180         fi
181         echo
182     fi
183 }
184
185
186 final_result () {
187     section=$first_section_set
188     [ $pass_count = 0 ]        || info "$pass_count tests passed"
189     [ $fail_count = 0 ]        || info "$fail_count tests failed"
190     [ $skip_count = 0 ]        || info "$unsupported_count tests skipped"
191     [ $unresolved_count = 0 ]  || info "$unresolved_count tests unresolved"
192     [ -z "$debug" -a -f "$SCRATCH" ] && rm "$SCRATCH"
193     if [ $fail_count = 0 ]; then
194         info "all tests passed"
195     else
196         exit 1
197     fi
198 }
199
200
201 clean_homedir () {
202     [ -f pubring.kbx ] && rm pubring.kbx
203     if [ -d private-keys-v1.d ]; then
204         rm private-keys-v1.d/* 2>/dev/null || true
205         rmdir private-keys-v1.d
206     fi
207 }
208
209 start_test () {
210     section="$1"
211     description="$2"
212     test_status=none
213     echo "BEGIN TEST $section ($description)" >&2
214 }
215
216 end_test () {
217    case "$test_status" in
218       none) skip "($description) - test not implemented";;
219       pass) pass "($description)";;
220       fail) fail "($description)";;
221      setup) fail "($description) - setup failed";;
222         ns) skip "($description) - not supported";;
223        nys) skip "($description) - not yet supported";;
224          *) unresolved "$(description)";; 
225    esac
226    echo "END TEST $section" >&2
227 }
228
229 set_status () {
230     if [ "$test_status" = "none" ]; then
231         test_status=$1
232     fi
233 }
234
235 need_cert () {
236     if [ "$2" = "--import-anyway" ]; then
237         if ! ${GPGSM} -q --debug-no-chain-validation --import certs/$1.crt
238           then 
239             set_status setup
240         fi
241     else
242         if ! ${GPGSM} -q --import certs/$1.crt; then 
243             set_status setup
244         fi
245     fi
246 }
247
248 need_crl () {
249     # CRL are not yet implemented
250     #set_status setup
251     :
252 }
253
254
255
256 set -e
257
258 pass_count=0
259 fail_count=0
260 skip_count=0
261 unresolved_count=0
262 first_section_set=""
263 section_out=""
264 test_status=none
265
266 # User settable variables
267 section=""
268 description=""
269
270
271 #trap cleanup SIGHUP SIGINT SIGQUIT
272 [ -z "$debug" ] && exec 2> ${pgmname}.log
273
274 :
275 # end