Blob


1 failed=
3 gg="./../gg"
4 gmid="./../gmid"
5 current_test=
7 run_test() {
8 ggflags=
9 port=10965
10 config_common="prefork 1
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 set_proxy() {
67 cat <<EOF >>reg.conf
68 server "localhost.local" {
69 cert "$PWD/cert.pem"
70 key "$PWD/key.pem"
71 proxy {
72 relay-to "localhost:$port"
73 $1
74 }
75 }
76 EOF
78 if ! checkconf; then
79 echo "failed to parse the config" >&2
80 return 1
81 fi
82 }
84 checkconf() {
85 if ! $gmid -n -c reg.conf >/dev/null 2>&1; then
86 $gmid -n -c reg.conf
87 fi
88 }
90 # usage: setup_simple_test <global config> <server config>
91 # generates a configuration file with `gen_config', validates it and
92 # launches the daemon
93 setup_simple_test() {
94 gen_config "$1" "$2"
95 run
96 }
98 # usage: get <path>
99 # return the body of the request on stdout
100 get() {
101 $gg -T10 $ggflags "gemini://localhost:10965/$1" || true
104 # usage: head <path>
105 # return the meta response line on stdout
106 head() {
107 $gg -T10 -d header $ggflags "gemini://localhost:10965/$1" || true
110 # usage: fetch <path>
111 # fetches the header and the body. They're returned in $hdr and
112 # $body.
113 fetch() {
114 if ! hdr="$(head $1)" || ! body="$(get $1)"; then
115 return 1
116 fi
119 # usage: fetch_hdr <path>
120 # fetches the header into $hdr
121 fetch_hdr() {
122 hdr="$(head $1)"
123 body=""
126 # usage: check_reply header body
127 # checks that $hdr and $body are equal to the given strings
128 check_reply() {
129 if [ "$hdr" != "$1" ]; then
130 echo "Header mismatch" >&2
131 echo "wants : $1" >&2
132 echo "got : $hdr" >&2
133 return 1
134 fi
136 if [ "$body" != "$2" ]; then
137 echo "Body mismatch" >&2
138 echo "wants : $2" >&2
139 echo "got : $body" >&2
140 return 1
141 fi
144 run() {
145 if check; then
146 kill -HUP "$(cat gmid.pid)"
147 sleep 1
148 return
149 fi
151 $gmid -P gmid.pid -c reg.conf
153 # give gmid time to bind the port, otherwise we end up
154 # executing gg when gmid isn't ready yet.
155 sleep 1
158 check() {
159 if [ ! -f gmid.pid ]; then
160 return 1
161 fi
163 pid="$(cat gmid.pid || true)"
164 if [ "$pid" = "" ]; then
165 return 1
166 fi
168 # remember: we're running under ``set -e''
169 if ps $pid >/dev/null; then
170 return 0
171 fi
173 return 1
176 count() {
177 wc -l | xargs
180 quit() {
181 pid="$(cat gmid.pid || true)"
182 if [ "$pid" != "" ]; then
183 kill $pid || true
184 wait || true
185 fi
188 onexit() {
189 rm -f bigfile bigfile.sha
190 quit