Blob


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