tests: if pwmc missing, skip rather than fail tests.
[pwmd.git] / tests / common.sh
blob61e0ed49755037a3aedd626f2997f4a09cae2aa1
1 bail_out() {
2 echo "Bail out! $@"
3 exit 1
6 skip_test() {
7 echo "1..0 # SKIP $@"
8 exit 0
11 on_exit() {
12 if [ $PID ]; then
13 kill $PID 2>/dev/null
16 agent_socket="`gpgconf --homedir $OUTDIR/.gnupg --list-dirs | grep '^agent-socket:' | awk -F: '{print $2}'`"
17 if [ -S "$agent_socket" ]; then
18 agent_pid="`echo GETINFO pid | gpg-connect-agent -S $agent_socket 2>/dev/null`"
19 agent_pid="`echo $agent_pid | awk '{print $2}' | sed -ne '1p'`"
20 if [ $agent_pid ]; then
21 kill $agent_pid 2>/dev/null
23 unset agent_pid
26 rm -f config
28 if [ x"$DEBUG" = "x" ]; then
29 rm -f pwmc.out
33 wait_for_socket() {
34 local pid="$1"
35 local socket="$2"
36 local remaining=10
37 local abort=0
39 until [ -S $OUTDIR/$socket ]; do
40 kill -0 $pid 2>/dev/null || bail_out "Could not launch pwmd"
41 if [ $remaining -eq 0 ]; then
42 bail_out "Could not connect to pwmd socket"
45 echo "# Waiting for socket. Timeout in ${remaining}s"
46 remaining=$((remaining - 1))
47 sleep 1
48 done
50 unset pid
51 unset socket
52 unset remaining
55 launch_pwmd () {
56 args="--homedir $OUTDIR -n $@"
58 if [ x"$TESTS" != "x" -o x"$DEBUG" = "x" ]; then
59 $PWMD $args 2>/dev/null >/dev/null &
60 else
61 $PWMD $args &
64 PID=$!
65 wait_for_socket $PID socket
68 begin_test() {
69 test_n=$(($1 + 1))
72 test_result() {
73 e=$2
74 msg="$3"
76 if [ $# -eq 4 -a $e -eq 0 ]; then
77 if [ "$3" = "save" ]; then
78 # The SAVE tests generate dynamic results. The following is needed
79 # for 'make distcheck'.
80 cmp $OUTDIR/${3}.result${test_n} result
81 else
82 cmp $WDIR/${3}.result${test_n} result
84 e=$?
85 rm -f result
86 msg="$4"
89 test_success $1 $e "$msg"
92 test_success() {
93 if [ $2 -ne 0 ]; then
94 echo "not ok $1 - $3"
95 return 1
96 else
97 echo "ok $1 - $3"
98 return 0
102 test_failure() {
103 if [ $# -eq 4 ]; then
104 desc="$4"
105 else
106 desc="$3"
109 if [ $2 -eq 0 ]; then
110 echo "# Return code indicated success but should have failed."
111 echo "not ok $2 - $desc"
112 return 1
115 rc=0
116 if [ $# -eq 4 -a x"$TESTS" != "x" -o x"$DEBUG" != "x" ]; then
117 if [ -s "pwmc.out" ]; then
118 rc="`grep '^ERR [0-9]*:' < pwmc.out | cut -d ' ' -f 2 | cut -b -9`"
119 if [ "$rc" != "$3" ]; then
120 rc=1
121 else
122 rc=0
124 else
125 rc=1
129 test_success $1 $rc "$desc"
132 run_tests() {
133 test_n=0
134 if [ "$1" = "-h" ]; then
135 echo "Run all or the specified tests from 1 to $MAX_TESTS. Note that" \
136 "some tests depend on"
137 echo "the result of previous tests."
138 echo "Usage: $0 [testN] [...]"
139 return 0
142 init_tests
144 if [ $# -eq 0 ]; then
145 for t in $(seq 1 $MAX_TESTS); do
146 begin_test $test_n
147 test_${t}
148 done
149 else
150 local ret=0
151 for t in $@; do
152 if [ $t -gt $MAX_TESTS ]; then
153 echo "$0: No such test $t." 1>&2
154 exit 1
156 test_n=$t
157 test_${t}
158 r=$?
159 if [ $ret -eq 0 ]; then
160 ret=$r
162 done
163 exit $ret
167 # Common in some tests.
168 list_recurse() {
169 e=$?
170 if [ $e -eq 0 ]; then
171 $PWMC $PWMC_ARGS $1 >result $DEVNULL <<EOF
172 LIST --recurse $2
174 e=$?
176 return $e
179 run_pwmc() {
180 eval "pwmc $PWMC_ARGS $@"
183 WDIR="$AM_SRCDIR"
184 OUTDIR="$AM_BUILDDIR"
185 PWMD="../src/pwmd"
186 PWMC="pwmc"
187 PWMC_ARGS="--url $OUTDIR/socket"
188 PID=
189 DEVNULL=
191 $PWMC --version >/dev/null
192 if [ $? != 0 ]; then
193 skip_test "Could not run $PWMC. Please make sure libpwmd is installed."
196 if [ x"$TESTS" = "x" -a x"$DEBUG" = "x" ]; then
197 PWMC_ARGS="$PWMC_ARGS --quiet"
198 DEVNULL="2>/dev/null"
199 else
200 DEVNULL="2>pwmc.out"
203 trap on_exit EXIT
205 if [ $WDIR != $OUTDIR ]; then
206 cp -Rf $WDIR/.gnupg $OUTDIR 2>/dev/null \
207 || bail_out "Could not copy .gnupg."
208 chmod -R u+w $OUTDIR/.gnupg
211 cp -f $WDIR/.gnupg/trustdb.gpg.dist $OUTDIR/.gnupg/trustdb.gpg 2>/dev/null \
212 || bail_out "Could not copy trustdb.gpg."
213 cp -f $WDIR/.gnupg/pubring.kbx.dist $OUTDIR/.gnupg/pubring.kbx 2>/dev/null \
214 || bail_out "Could not copy pubring.kbx."
216 echo "1..${MAX_TESTS}"