Blob


1 failed=
3 gg="./../gg"
4 gmid="./../gmid"
5 current_test=
7 run_test() {
8 ggflags=
9 port=10965
10 config_common="
11 ipv6 off
12 port $port
13 "
14 hdr=
15 body=
16 dont_check_server_alive=no
18 current_test=$1
19 rm -f reg.conf
21 if ! $1; then
22 echo "$1 failed"
23 failed="$failed $1"
24 else
25 echo "$1 passed"
26 fi
28 if [ "$dont_check_server_alive" != 'no' ]; then
29 return
30 fi
32 if ! check; then
33 echo "gmid crashed?"
34 failed="$failed $1"
35 fi
36 }
38 tests_done() {
39 if [ "$failed" != "" ]; then
40 echo
41 echo "failed tests:$failed"
42 exit 1
43 fi
44 exit 0
45 }
47 # usage: gen_config <global config> <server config>
48 # generates a configuration file reg.conf
49 gen_config() {
50 cat <<EOF > reg.conf
51 $config_common
52 $1
53 server "localhost" {
54 cert "$PWD/cert.pem"
55 key "$PWD/key.pem"
56 root "$PWD/testdata"
57 $2
58 }
59 EOF
60 if ! checkconf; then
61 echo "failed to parse the config" >&2
62 return 1
63 fi
64 }
66 checkconf() {
67 if ! $gmid -n -c reg.conf >/dev/null 2>&1; then
68 $gmid -n -c reg.conf
69 fi
70 }
72 # usage: setup_simple_test <global config> <server config>
73 # generates a configuration file with `gen_config', validates it and
74 # launches the daemon
75 setup_simple_test() {
76 gen_config "$1" "$2"
77 run
78 }
80 # usage: get <path>
81 # return the body of the request on stdout
82 get() {
83 $gg -T10 $ggflags "gemini://localhost:10965/$1" || true
84 }
86 # usage: head <path>
87 # return the meta response line on stdout
88 head() {
89 $gg -T10 -d header $ggflags "gemini://localhost:10965/$1" || true
90 }
92 # usage: raw <path>
93 # return both header and body
94 raw() {
95 $gg -T10 -dwhole $ggflags "gemini://localhost:10965/$1" || true
96 }
98 # usage: fetch <path>
99 # fetches the header and the body. They're returned in $hdr and
100 # $body.
101 fetch() {
102 if ! hdr="$(head $1)" || ! body="$(get $1)"; then
103 return 1
104 fi
107 # usage: fetch_hdr <path>
108 # fetches the header into $hdr
109 fetch_hdr() {
110 hdr="$(head $1)"
111 body=""
114 # usage: check_reply header body
115 # checks that $hdr and $body are equal to the given strings
116 check_reply() {
117 if [ "$hdr" != "$1" ]; then
118 echo "Header mismatch" >&2
119 echo "wants : $1" >&2
120 echo "got : $hdr" >&2
121 return 1
122 fi
124 if [ "$body" != "$2" ]; then
125 echo "Body mismatch" >&2
126 echo "wants : $2" >&2
127 echo "got : $body" >&2
128 return 1
129 fi
132 run() {
133 if check; then
134 kill -HUP "$(cat gmid.pid)"
135 sleep 1
136 return
137 fi
139 $gmid -P gmid.pid -c reg.conf
141 # give gmid time to bind the port, otherwise we end up
142 # executing gg when gmid isn't ready yet.
143 sleep 1
146 check() {
147 if [ ! -f gmid.pid ]; then
148 return 1
149 fi
151 pid="$(cat gmid.pid || true)"
152 if [ "$pid" == "" ]; then
153 return 1
154 fi
156 # remember: we're running under ``set -e''
157 if ps $pid >/dev/null; then
158 return 0
159 fi
161 return 1
164 count() {
165 wc -l | xargs
168 quit() {
169 pid="$(cat gmid.pid || true)"
170 if [ "$pid" != "" ]; then
171 kill $pid || true
172 wait || true
173 fi
176 onexit() {
177 rm -f bigfile bigfile.sha
178 quit