chiark / gitweb /
Apply OpenSSL exception to Hippotat files
[hippotat.git] / test / common
1 # -*- shell-script -*-
2 # Copyright 2021-2022 Ian Jackson and contributors to Hippotat
3 # SPDX-License-Identifier: GPL-3.0-or-later WITH LicenseRef-Hippotat-OpenSSL-Exception
4 # There is NO WARRANTY.
5
6 set -x
7
8 ssrc="${0%/*}"
9 src="${ssrc%/*}"
10 test="${ssrc%/*}/test"
11
12 fail () { echo >&2 "$0: fail: $*"; exit 1; }
13
14 test-prep () {
15
16     case "${0##*/}" in
17     t-*) tname="${0##*/t-}" ;;
18     *) fail "bad test script name $0" ;;
19     esac
20
21     tmp=tmp/$tname
22     rm -rf "$tmp"
23     mkdir -p $tmp
24
25     $test/netns-setup "$tname"
26
27     trap '
28         rc=$?
29         shutdown
30         if [ $rc = 0 ]; then echo "OK $tname"; fi
31     ' 0
32 }
33
34 kill-pids () {
35     for p in $pids; do kill -9 $p; done
36 }
37
38 shutdown () {
39     kill-pids
40 }
41
42 in-ns () {
43     local client_server=$1; shift
44     $exec ip netns exec hippotat-t-$tname-$client_server "$@"
45 }
46
47 run-client () {
48     in-ns client \
49     target/debug/hippotat --config $test/test.cfg -DD "$@"
50 }
51 run-server () {
52     in-ns server \
53     target/debug/hippotatd --config $test/test.cfg -DD "$@"
54 }
55 spawn () {
56     { exec=exec; "$@"; } &
57     pids+=" $!"
58 }
59
60 in-ns-await-up () {
61     local sc="$1"; shift
62     local addr="$1"; shift
63     local t=1
64     while sleep $(( $t / 10 )).$(( $t % 10 )); do
65         if in-ns $sc ip -o addr show | fgrep " inet $addr "; then
66             return
67         fi
68         t=$(( $t + 1 ))
69         if [ $t -gt 10 ]; then fail "$sc did not come up $addr"; fi
70     done
71 }
72
73 start-server () {
74     spawn run-server
75     in-ns-await-up server 192.0.2.1
76 }
77 start-client () {
78     spawn run-client
79     in-ns-await-up client 192.0.2.3
80 }