summaryrefslogtreecommitdiffstats
path: root/tests/pkits/common.sh
blob: 697f28f67cca579c2a93ebef54c08805246e7965 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# common.sh - common defs for all tests         -*- sh -*-
# Copyright (C) 2004, 2008 Free Software Foundation, Inc.
#
# This file is part of GnuPG.
# 
# GnuPG is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
# 
# GnuPG is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.

# reset some environment variables because we do not want to test locals
LANG=C
LANGUAGE=C
LC_ALL=C
export LANG LANGUAGE LC_ALL

pgmname=`basename $0`

if [ "$1" = "--debug" ]; then
  debug=yes
  set -x
else
  debug=
fi
[ -z "$srcdir" ] && srcdir="."
[ -z "$top_srcdir" ] && top_srcdir=".."
[ -z "$GPGSM" ] && GPGSM="../../sm/gpgsm"
[ -z "$silent" ] && silent=no

AWK=awk
SCRATCH="scratch.$$.tmp"

# We use this as the faked system time for certain tests.
MYTIME="20080508T120000"


if [ "$GNUPGHOME" != "`/bin/pwd`" ]; then
    echo "inittests: please set GNUPGHOME to the tests/pkits directory" >&2
    exit 1
fi

if [ -n "$GPG_AGENT_INFO" ]; then
    echo "inittests: please unset GPG_AGENT_INFO" >&2
    exit 1
fi

if [ -f "$srcdir/PKITS_data.tar.bz2" ]; then
  :
else
    if [ "$pgmname" = "import-all-certs" ]; then
        if [ "$silent" = "yes" ]; then tmp1="Note: "; tmp2='      '
        else tmp1="- ____ "; tmp2="$tmp1"
        fi
        echo "${tmp1}PKITS_data.tar.bz2 is not installed"
        echo "${tmp2}All tests will be skipped (this is not an error)"
    fi
    # Exit code 77 is used by the Makefile for skipping a tests.
    exit 77
fi

#--------------------------------
#------ utility functions -------
#--------------------------------

echo_n_init=no
echo_n () {
  if test "$echo_n_init" = "no"; then
    if (echo "testing\c"; echo 1,2,3) | grep c >/dev/null; then
      if (echo -n testing; echo 1,2,3) | sed s/-n/xn/ | grep xn >/dev/null; then
	echo_n_n=
	echo_n_c='
'
      else
	echo_n_n='-n'
	echo_n_c=
      fi
    else
      echo_n_n=
      echo_n_c='\c'
    fi
    echo_n_init=yes
  fi
  echo $echo_n_n "${1}$echo_n_c"
}

setup_output () {
  if [ -z "$first_section_set" ]; then
      first_section_set=$section
  fi
  section_out="$(echo $section)"
  if [ -z "$section_out" ]; then
      section_out="-"
  fi
}

fatal () {
    echo "$pgmname: fatal:" $* >&2
    if [ "$silent" != "yes" ]; then
        echo "$section_out ERROR: $* (fatal)"
    fi
    exit 1;
}

error () {
    echo "$pgmname:" $* >&2
    if [ "$silent" != "yes" ]; then
        echo "$section_out ERROR: $*"
    fi
    exit 1
}

info () {
    setup_output
    echo "$pgmname:" $* >&2
    if [ "$silent" != "yes" ]; then
        echo "$section_out ____ $*"
    fi
}

info_n () {
    setup_output
    echo_n "$pgmname:" $* >&2
}

pass () {
    setup_output
    echo "PASS: " $* >&2
    pass_count=`expr ${pass_count} + 1`
    if [ "$silent" != "yes" ]; then
        echo_n "$section_out PASS"
        if [ -n "$1" ]; then echo_n " $1"
        elif [ -n "$description" ]; then echo_n " ($description)"
        fi
        echo
    fi
}

fail () {
    setup_output
    echo "FAIL: " $* >&2
    fail_count=`expr ${fail_count} + 1`
    if [ "$silent" != "yes" ]; then
        echo_n "$section_out FAIL"
        if [ -n "$1" ]; then echo_n " $1"
        elif [ -n "$description" ]; then echo_n " ($description)"
        fi
        echo
    fi
}

skip () {
    setup_output
    echo "SKIP: " $* >&2
    skip_count=`expr ${skip_count} + 1`
    if [ "$silent" != "yes" ]; then
        echo_n "$section_out SKIP"
        if [ -n "$1" ]; then echo_n " $1"
        elif [ -n "$description" ]; then echo_n " ($description)"
        fi
        echo
    fi
}

unresolved () {
    setup_output
    echo "UNRESOLVED: " $* >&2
    unresolved_count=`expr ${unresolved_count} + 1`
    if [ "$silent" != "yes" ]; then
        echo_n "$section_out UNRESOLVED"
        if [ -n "$1" ]; then echo_n " $1"
        elif [ -n "$description" ]; then echo_n " ($description)"
        fi
        echo
    fi
}


final_result () {
    section=$first_section_set
    [ $pass_count = 0 ]        || info "$pass_count tests passed"
    [ $fail_count = 0 ]        || info "$fail_count tests failed"
    [ $skip_count = 0 ]        || info "$unsupported_count tests skipped"
    [ $unresolved_count = 0 ]  || info "$unresolved_count tests unresolved"
    [ -z "$debug" -a -f "$SCRATCH" ] && rm "$SCRATCH"
    if [ $fail_count = 0 ]; then
        info "all tests passed"
    else
        exit 1
    fi
}


clean_homedir () {
    [ -f pubring.kbx ] && rm pubring.kbx
    if [ -d private-keys-v1.d ]; then
        rm private-keys-v1.d/* 2>/dev/null || true
        rmdir private-keys-v1.d
    fi
}

start_test () {
    section="$1"
    description="$2"
    test_status=none
    echo "BEGIN TEST $section ($description)" >&2
}

end_test () {
   case "$test_status" in
      none) skip "($description) - test not implemented";;
      pass) pass "($description)";;
      fail) fail "($description)";;
     setup) fail "($description) - setup failed";;
        ns) skip "($description) - not supported";;
       nys) skip "($description) - not yet supported";;
         *) unresolved "$(description)";; 
   esac
   echo "END TEST $section" >&2
}

set_status () {
    if [ "$test_status" = "none" ]; then
        test_status=$1
    fi
}

need_cert () {
    if [ "$2" = "--import-anyway" ]; then
        if ! ${GPGSM} -q --debug-no-chain-validation --import certs/$1.crt
          then 
            set_status setup
        fi
    else
        if ! ${GPGSM} -q --import certs/$1.crt; then 
            set_status setup
        fi
    fi
}

need_crl () {
    # CRL are not yet implemented
    #set_status setup
    :
}



set -e

pass_count=0
fail_count=0
skip_count=0
unresolved_count=0
first_section_set=""
section_out=""
test_status=none

# User settable variables
section=""
description=""


#trap cleanup SIGHUP SIGINT SIGQUIT
[ -z "$debug" ] && exec 2> ${pgmname}.log

:
# end