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: raw <path>
111 # return both header and body
112 raw() {
113 $gg -T10 -dwhole $ggflags "gemini://localhost:10965/$1" || true
116 # usage: fetch <path>
117 # fetches the header and the body. They're returned in $hdr and
118 # $body.
119 fetch() {
120 if ! hdr="$(head $1)" || ! body="$(get $1)"; then
121 return 1
122 fi
125 # usage: fetch_hdr <path>
126 # fetches the header into $hdr
127 fetch_hdr() {
128 hdr="$(head $1)"
129 body=""
132 # usage: check_reply header body
133 # checks that $hdr and $body are equal to the given strings
134 check_reply() {
135 if [ "$hdr" != "$1" ]; then
136 echo "Header mismatch" >&2
137 echo "wants : $1" >&2
138 echo "got : $hdr" >&2
139 return 1
140 fi
142 if [ "$body" != "$2" ]; then
143 echo "Body mismatch" >&2
144 echo "wants : $2" >&2
145 echo "got : $body" >&2
146 return 1
147 fi
150 run() {
151 if check; then
152 kill -HUP "$(cat gmid.pid)"
153 sleep 1
154 return
155 fi
157 $gmid -P gmid.pid -c reg.conf
159 # give gmid time to bind the port, otherwise we end up
160 # executing gg when gmid isn't ready yet.
161 sleep 1
164 check() {
165 if [ ! -f gmid.pid ]; then
166 return 1
167 fi
169 pid="$(cat gmid.pid || true)"
170 if [ "$pid" == "" ]; then
171 return 1
172 fi
174 # remember: we're running under ``set -e''
175 if ps $pid >/dev/null; then
176 return 0
177 fi
179 return 1
182 count() {
183 wc -l | xargs
186 quit() {
187 pid="$(cat gmid.pid || true)"
188 if [ "$pid" != "" ]; then
189 kill $pid || true
190 wait || true
191 fi
194 onexit() {
195 rm -f bigfile bigfile.sha
196 quit