Blob


1 failed=
3 gg="./../gg"
4 gmid="./../gmid"
5 current_test=
7 run_test() {
8 ggflags=
9 port=10965
10 config_common="
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 checkconf() {
67 $gmid -n -c reg.conf >/dev/null
68 }
70 # usage: setup_simple_test <global config> <server config>
71 # generates a configuration file with `gen_config', validates it and
72 # launches the daemon
73 setup_simple_test() {
74 gen_config "$1" "$2"
75 run
76 }
78 # usage: get <path>
79 # return the body of the request on stdout
80 get() {
81 $gg -T10 $ggflags "gemini://localhost:10965/$1" || true
82 }
84 # usage: head <path>
85 # return the meta response line on stdout
86 head() {
87 $gg -T10 -d header $ggflags "gemini://localhost:10965/$1" || true
88 }
90 # usage: raw <path>
91 # return both header and body
92 raw() {
93 $gg -T10 -dwhole $ggflags "gemini://localhost:10965/$1" || true
94 }
96 # usage: fetch <path>
97 # fetches the header and the body. They're returned in $hdr and
98 # $body.
99 fetch() {
100 if ! hdr="$(head $1)" || ! body="$(get $1)"; then
101 return 1
102 fi
105 # usage: fetch_hdr <path>
106 # fetches the header into $hdr
107 fetch_hdr() {
108 hdr="$(head $1)"
109 body=""
112 # usage: check_reply header body
113 # checks that $hdr and $body are equal to the given strings
114 check_reply() {
115 if [ "$hdr" != "$1" ]; then
116 echo "Header mismatch" >&2
117 echo "wants : $1" >&2
118 echo "got : $hdr" >&2
119 return 1
120 fi
122 if [ "$body" != "$2" ]; then
123 echo "Body mismatch" >&2
124 echo "wants : $2" >&2
125 echo "got : $body" >&2
126 return 1
127 fi
130 run() {
131 if check; then
132 kill -HUP "$(cat gmid.pid)"
133 sleep 1
134 return
135 fi
137 $gmid -P gmid.pid -c reg.conf
139 # give gmid time to bind the port, otherwise we end up
140 # executing gg when gmid isn't ready yet.
141 sleep 1
144 check() {
145 if [ ! -f gmid.pid ]; then
146 return 1
147 fi
149 pid="$(cat gmid.pid || true)"
150 if [ "$pid" == "" ]; then
151 return 1
152 fi
154 # remember: we're running under ``set -e''
155 if ps $pid >/dev/null; then
156 return 0
157 fi
159 return 1
162 count() {
163 wc -l | xargs
166 quit() {
167 pid="$(cat gmid.pid || true)"
168 if [ "$pid" != "" ]; then
169 kill $pid || true
170 wait || true
171 fi
174 onexit() {
175 rm -f bigfile bigfile.sha
176 quit