1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-12-24 10:39:57 +01:00
gnupg/tests/pkits/common.sh

276 lines
6.2 KiB
Bash
Raw Normal View History

# 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
2008-02-22 15:47:18 +00:00
LANG=C
LANGUAGE=C
LC_ALL=C
export LANG LANGUAGE LC_ALL
2008-02-22 15:47:18 +00:00
pgmname=`basename $0`
2008-02-22 15:47:18 +00:00
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
2008-02-22 15:47:18 +00:00
AWK=awk
SCRATCH="scratch.$$.tmp"
# We use this as the faked system time for certain tests.
MYTIME="20080508T120000"
2009-02-19 16:19:16 +00:00
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
2008-02-19 12:58:34 +00:00
:
else
2008-02-22 15:47:18 +00:00
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.
2008-02-19 12:58:34 +00:00
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"
2008-02-22 15:47:18 +00:00
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"
2008-02-22 15:47:18 +00:00
if [ -n "$1" ]; then echo_n " $1"
elif [ -n "$description" ]; then echo_n " ($description)"
fi
echo
fi
}
2008-02-22 15:47:18 +00:00
skip () {
setup_output
2008-02-22 15:47:18 +00:00
echo "SKIP: " $* >&2
skip_count=`expr ${skip_count} + 1`
if [ "$silent" != "yes" ]; then
2008-02-22 15:47:18 +00:00
echo_n "$section_out SKIP"
if [ -n "$1" ]; then echo_n " $1"
elif [ -n "$description" ]; then echo_n " ($description)"
fi
echo
fi
}
2008-02-22 15:47:18 +00:00
unresolved () {
setup_output
2008-02-22 15:47:18 +00:00
echo "UNRESOLVED: " $* >&2
unresolved_count=`expr ${unresolved_count} + 1`
if [ "$silent" != "yes" ]; then
2008-02-22 15:47:18 +00:00
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"
2008-02-22 15:47:18 +00:00
[ $skip_count = 0 ] || info "$unsupported_count tests skipped"
[ $unresolved_count = 0 ] || info "$unresolved_count tests unresolved"
2008-02-22 15:47:18 +00:00
[ -z "$debug" -a -f "$SCRATCH" ] && rm "$SCRATCH"
if [ $fail_count = 0 ]; then
info "all tests passed"
else
exit 1
fi
}
2008-02-22 15:47:18 +00:00
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
2008-02-22 15:47:18 +00:00
skip_count=0
unresolved_count=0
first_section_set=""
section_out=""
2008-02-22 15:47:18 +00:00
test_status=none
# User settable variables
section=""
description=""
2008-02-22 15:47:18 +00:00
#trap cleanup SIGHUP SIGINT SIGQUIT
2008-02-22 15:47:18 +00:00
[ -z "$debug" ] && exec 2> ${pgmname}.log
:
# end