Blob


1 ran_no=0
2 failed_no=0
3 failed=
5 ge="./../ge"
6 gg="./../gg"
7 gmid="./../gmid"
8 current_test=
10 run_test() {
11 ggflags=
12 port=10965
13 config_common="prefork 1
14 ipv6 off
15 port $port
16 "
17 hdr=
18 body=
19 dont_check_server_alive=no
21 ran_no=$((ran_no + 1))
23 current_test=$1
24 rm -f reg.conf
26 if ! $1; then
27 echo "$1 failed"
28 failed="$failed $1"
29 failed_no=$((failed_no + 1))
30 else
31 echo "$1 passed"
32 fi
34 if [ "$dont_check_server_alive" != 'no' ]; then
35 return
36 fi
38 if ! check; then
39 echo "gmid crashed?"
40 failed="$failed $1"
41 failed_no=$((failed_no + 1))
42 fi
43 }
45 tests_done() {
46 ok=$((ran_no - failed_no))
47 echo
48 echo "tests: $ran_no / passed: $ok / failed: $failed_no"
49 if [ "$failed" != "" ]; then
50 echo
51 echo "failed tests:$failed"
52 exit 1
53 fi
54 exit 0
55 }
57 # usage: gen_config <global config> <server config>
58 # generates a configuration file reg.conf
59 gen_config() {
60 cat <<EOF > reg.conf
61 $config_common
62 $1
63 server "localhost" {
64 cert "$PWD/cert.pem"
65 key "$PWD/key.pem"
66 root "$PWD/testdata"
67 $2
68 }
69 EOF
70 if ! checkconf; then
71 echo "failed to parse the config" >&2
72 return 1
73 fi
74 }
76 set_proxy() {
77 cat <<EOF >>reg.conf
78 server "localhost.local" {
79 cert "$PWD/cert.pem"
80 key "$PWD/key.pem"
81 proxy {
82 relay-to "localhost:$port"
83 $1
84 }
85 }
86 EOF
88 if ! checkconf; then
89 echo "failed to parse the config" >&2
90 return 1
91 fi
92 }
94 checkconf() {
95 if ! $gmid -n -f reg.conf >/dev/null 2>&1; then
96 $gmid -n -f reg.conf
97 fi
98 }
100 # usage: setup_simple_test <global config> <server config>
101 # generates a configuration file with `gen_config', validates it and
102 # launches the daemon
103 setup_simple_test() {
104 gen_config "$1" "$2"
105 run
108 # usage: get <path>
109 # return the body of the request on stdout
110 get() {
111 $gg -T10 $ggflags "gemini://localhost:10965/$1" || true
114 # usage: head <path>
115 # return the meta response line on stdout
116 head() {
117 $gg -T10 -d header $ggflags "gemini://localhost:10965/$1" || true
120 # usage: fetch <path>
121 # fetches the header and the body. They're returned in $hdr and
122 # $body.
123 fetch() {
124 if ! hdr="$(head $1)" || ! body="$(get $1)"; then
125 return 1
126 fi
129 # usage: fetch_hdr <path>
130 # fetches the header into $hdr
131 fetch_hdr() {
132 hdr="$(head $1)"
133 body=""
136 # usage: check_reply header body
137 # checks that $hdr and $body are equal to the given strings
138 check_reply() {
139 if [ "$hdr" != "$1" ]; then
140 echo "Header mismatch" >&2
141 echo "wants : $1" >&2
142 echo "got : $hdr" >&2
143 return 1
144 fi
146 if [ "$body" != "$2" ]; then
147 echo "Body mismatch" >&2
148 echo "wants : $2" >&2
149 echo "got : $body" >&2
150 return 1
151 fi
154 run() {
155 if check; then
156 kill -HUP "$(cat gmid.pid)"
157 sleep 1
158 return
159 fi
161 $gmid -P gmid.pid -f reg.conf
163 # give gmid time to bind the port, otherwise we end up
164 # executing gg when gmid isn't ready yet.
165 sleep 1
168 check() {
169 if [ ! -f gmid.pid ]; then
170 return 1
171 fi
173 pid="$(cat gmid.pid || true)"
174 if [ "$pid" = "" ]; then
175 return 1
176 fi
178 # remember: we're running under ``set -e''
179 if ps $pid >/dev/null; then
180 return 0
181 fi
183 return 1
186 count() {
187 wc -l | xargs
190 quit() {
191 pid="$(cat gmid.pid || true)"
192 if [ "$pid" != "" ]; then
193 kill $pid || true
194 wait || true
195 fi
198 onexit() {
199 rm -f bigfile bigfile.sha
200 quit