Blob


1 ran_no=0
2 failed_no=0
3 failed=
5 gemexp="./../gemexp"
6 gg="./../gg"
7 gmid="./../gmid"
8 current_test=
10 run_test() {
11 ggflags=
12 port=10965
13 config_common="log syslog off"
14 hdr=
15 body=
16 dont_check_server_alive=no
18 ran_no=$((ran_no + 1))
20 current_test=$1
21 rm -f reg.conf
23 if ! $1; then
24 echo "$1 failed"
25 failed="$failed $1"
26 failed_no=$((failed_no + 1))
27 return
28 else
29 echo "$1 passed"
30 fi
32 if [ "$dont_check_server_alive" != 'no' ]; then
33 return
34 fi
36 if ! check; then
37 echo "gmid crashed?"
38 failed="$failed $1"
39 failed_no=$((failed_no + 1))
40 fi
41 }
43 tests_done() {
44 ok=$((ran_no - failed_no))
45 echo
46 echo "tests: $ran_no / passed: $ok / failed: $failed_no"
47 if [ "$failed" != "" ]; then
48 echo
49 echo "failed tests:$failed"
50 exit 1
51 fi
52 exit 0
53 }
55 # usage: gen_config <global config> <server config>
56 # generates a configuration file reg.conf
57 gen_config() {
58 cat <<EOF > reg.conf
59 $config_common
60 $1
61 server "localhost" {
62 cert "$PWD/localhost.pem"
63 key "$PWD/localhost.key"
64 root "$PWD/testdata"
65 listen on $REGRESS_HOST port $port
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/localhost.pem"
79 key "$PWD/localhost.key"
80 listen on $REGRESS_HOST port $port
81 proxy {
82 relay-to localhost port $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 -c reg.conf >/dev/null 2>&1; then
96 $gmid -n -c 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 $PWD/gmid.pid -c 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