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 return
31 else
32 echo "$1 passed"
33 fi
35 if [ "$dont_check_server_alive" != 'no' ]; then
36 return
37 fi
39 if ! check; then
40 echo "gmid crashed?"
41 failed="$failed $1"
42 failed_no=$((failed_no + 1))
43 fi
44 }
46 tests_done() {
47 ok=$((ran_no - failed_no))
48 echo
49 echo "tests: $ran_no / passed: $ok / failed: $failed_no"
50 if [ "$failed" != "" ]; then
51 echo
52 echo "failed tests:$failed"
53 exit 1
54 fi
55 exit 0
56 }
58 # usage: gen_config <global config> <server config>
59 # generates a configuration file reg.conf
60 gen_config() {
61 cat <<EOF > reg.conf
62 $config_common
63 $1
64 server "localhost" {
65 cert "$PWD/cert.pem"
66 key "$PWD/key.pem"
67 root "$PWD/testdata"
68 $2
69 }
70 EOF
71 if ! checkconf; then
72 echo "failed to parse the config" >&2
73 return 1
74 fi
75 }
77 set_proxy() {
78 cat <<EOF >>reg.conf
79 server "localhost.local" {
80 cert "$PWD/cert.pem"
81 key "$PWD/key.pem"
82 proxy {
83 relay-to "localhost:$port"
84 $1
85 }
86 }
87 EOF
89 if ! checkconf; then
90 echo "failed to parse the config" >&2
91 return 1
92 fi
93 }
95 checkconf() {
96 if ! $gmid -n -f reg.conf >/dev/null 2>&1; then
97 $gmid -n -f reg.conf
98 fi
99 }
101 # usage: setup_simple_test <global config> <server config>
102 # generates a configuration file with `gen_config', validates it and
103 # launches the daemon
104 setup_simple_test() {
105 gen_config "$1" "$2"
106 run
109 # usage: get <path>
110 # return the body of the request on stdout
111 get() {
112 $gg -T10 $ggflags "gemini://localhost:10965/$1" || true
115 # usage: head <path>
116 # return the meta response line on stdout
117 head() {
118 $gg -T10 -d header $ggflags "gemini://localhost:10965/$1" || true
121 # usage: fetch <path>
122 # fetches the header and the body. They're returned in $hdr and
123 # $body.
124 fetch() {
125 if ! hdr="$(head $1)" || ! body="$(get $1)"; then
126 return 1
127 fi
130 # usage: fetch_hdr <path>
131 # fetches the header into $hdr
132 fetch_hdr() {
133 hdr="$(head $1)"
134 body=""
137 # usage: check_reply header body
138 # checks that $hdr and $body are equal to the given strings
139 check_reply() {
140 if [ "$hdr" != "$1" ]; then
141 echo "Header mismatch" >&2
142 echo "wants : $1" >&2
143 echo "got : $hdr" >&2
144 return 1
145 fi
147 if [ "$body" != "$2" ]; then
148 echo "Body mismatch" >&2
149 echo "wants : $2" >&2
150 echo "got : $body" >&2
151 return 1
152 fi
155 run() {
156 if check; then
157 kill -HUP "$(cat gmid.pid)"
158 sleep 1
159 return
160 fi
162 $gmid -P gmid.pid -f reg.conf
164 # give gmid time to bind the port, otherwise we end up
165 # executing gg when gmid isn't ready yet.
166 sleep 1
169 check() {
170 if [ ! -f gmid.pid ]; then
171 return 1
172 fi
174 pid="$(cat gmid.pid || true)"
175 if [ "$pid" = "" ]; then
176 return 1
177 fi
179 # remember: we're running under ``set -e''
180 if ps $pid >/dev/null; then
181 return 0
182 fi
184 return 1
187 count() {
188 wc -l | xargs
191 quit() {
192 pid="$(cat gmid.pid || true)"
193 if [ "$pid" != "" ]; then
194 kill $pid || true
195 wait || true
196 fi
199 onexit() {
200 rm -f bigfile bigfile.sha
201 quit