Create a pkcs#10 request directly from a card.
[gnupg.git] / tests / pkits / common.sh
blobca18b9501e183d2596967d384d2210797bd2d7e2
1 # common.sh - common defs for all tests -*- sh -*-
2 # Copyright (C) 2004, 2008 Free Software Foundation, Inc.
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.
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.
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/>.
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
25 pgmname=`basename $0`
27 if [ "$1" = "--debug" ]; then
28 debug=yes
29 set -x
30 else
31 debug=
33 [ -z "$srcdir" ] && srcdir="."
34 [ -z "$top_srcdir" ] && top_srcdir=".."
35 [ -z "$GPGSM" ] && GPGSM="../../sm/gpgsm"
36 [ -z "$silent" ] && silent=no
38 AWK=awk
39 SCRATCH="scratch.$$.tmp"
41 # We use this as the faked system time for certain tests.
42 MYTIME="20080508T120000"
45 if [ "$GNUPGHOME" != "`/bin/pwd`" ]; then
46 echo "inittests: please set GNUPGHOME to the tests/pkits directory" >&2
47 exit 1
50 if [ -n "$GPG_AGENT_INFO" ]; then
51 echo "inittests: please unset GPG_AGENT_INFO" >&2
52 exit 1
55 if [ -f PKITS_data.tar.bz2 ]; then
57 else
58 if [ "$pgmname" = "import-all-certs" ]; then
59 if [ "$silent" = "yes" ]; then tmp1="Note: "; tmp2=' '
60 else tmp1="- ____ "; tmp2="$tmp1"
62 echo "${tmp1}PKITS_data.tar.bz2 is not installed"
63 echo "${tmp2}All tests will be skipped (this is not an error)"
65 # Exit code 77 is used by the Makefile for skipping a tests.
66 exit 77
69 #--------------------------------
70 #------ utility functions -------
71 #--------------------------------
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='
81 else
82 echo_n_n='-n'
83 echo_n_c=
85 else
86 echo_n_n=
87 echo_n_c='\c'
89 echo_n_init=yes
91 echo $echo_n_n "${1}$echo_n_c"
94 setup_output () {
95 if [ -z "$first_section_set" ]; then
96 first_section_set=$section
98 section_out="$(echo $section)"
99 if [ -z "$section_out" ]; then
100 section_out="-"
104 fatal () {
105 echo "$pgmname: fatal:" $* >&2
106 if [ "$silent" != "yes" ]; then
107 echo "$section_out ERROR: $* (fatal)"
109 exit 1;
112 error () {
113 echo "$pgmname:" $* >&2
114 if [ "$silent" != "yes" ]; then
115 echo "$section_out ERROR: $*"
117 exit 1
120 info () {
121 setup_output
122 echo "$pgmname:" $* >&2
123 if [ "$silent" != "yes" ]; then
124 echo "$section_out ____ $*"
128 info_n () {
129 setup_output
130 echo_n "$pgmname:" $* >&2
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)"
142 echo
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)"
155 echo
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)"
168 echo
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)"
181 echo
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
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
209 start_test () {
210 section="$1"
211 description="$2"
212 test_status=none
213 echo "BEGIN TEST $section ($description)" >&2
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
229 set_status () {
230 if [ "$test_status" = "none" ]; then
231 test_status=$1
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
241 else
242 if ! ${GPGSM} -q --import certs/$1.crt; then
243 set_status setup
248 need_crl () {
249 # CRL are not yet implemented
250 #set_status setup
256 set -e
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
266 # User settable variables
267 section=""
268 description=""
271 #trap cleanup SIGHUP SIGINT SIGQUIT
272 [ -z "$debug" ] && exec 2> ${pgmname}.log
275 # end