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=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" != 'no' ]; then
29 return
30 fi
32 if ! check; then
33 echo "gmid crashed?"
34 failed="$failed $1"
35 fi
36 }
38 # usage: gen_config <global config> <server config>
39 # generates a configuration file reg.conf
40 gen_config() {
41 cat <<EOF > reg.conf
42 $config_common
43 $1
44 server "localhost" {
45 cert "$PWD/cert.pem"
46 key "$PWD/key.pem"
47 root "$PWD/testdata"
48 $2
49 }
50 EOF
51 if ! checkconf; then
52 echo "failed to parse the config" >&2
53 return 1
54 fi
55 }
57 checkconf() {
58 $gmid -n -c reg.conf >/dev/null
59 }
61 # usage: setup_simple_test <global config> <server config>
62 # generates a configuration file with `gen_config', validates it and
63 # launches the daemon
64 setup_simple_test() {
65 gen_config "$1" "$2"
66 run
67 }
69 # usage: get <path>
70 # return the body of the request on stdout
71 get() {
72 $gg -T30 -b $ggflags "gemini://localhost:10965/$1"
73 }
75 # usage: head <path>
76 # return the meta response line on stdout
77 head() {
78 $gg -T30 -h $ggflags "gemini://localhost:10965/$1"
79 }
81 # usage: raw <path>
82 # return both header and body
83 raw() {
84 $gg -T30 $ggflags "gemini://localhost:10965/$1"
85 }
87 # usage: fetch <path>
88 # fetches the header and the body. They're returned in $hdr and
89 # $body.
90 fetch() {
91 if ! hdr="$(head $1)" || ! body="$(get $1)"; then
92 return 1
93 fi
94 }
96 # usage: fetch_hdr <path>
97 # fetches the header into $hdr
98 fetch_hdr() {
99 hdr="$(head $1)"
100 body=""
103 # usage: check_reply header body
104 # checks that $hdr and $body are equal to the given strings
105 check_reply() {
106 if [ "$hdr" != "$1" ]; then
107 echo "Header mismatch" >&2
108 echo "wants : $1" >&2
109 echo "got : $hdr" >&2
110 return 1
111 fi
113 if [ "$body" != "$2" ]; then
114 echo "Body mismatch" >&2
115 echo "wants : $1" >&2
116 echo "got : $body" >&2
117 return 1
118 fi
121 run() {
122 if check; then
123 kill -HUP "$(cat gmid.pid)"
124 sleep 1
125 return
126 fi
128 $gmid -P gmid.pid -c reg.conf
130 # give gmid time to bind the port, otherwise we end up
131 # executing gg when gmid isn't ready yet.
132 sleep 1
135 check() {
136 if [ ! -f gmid.pid ]; then
137 return 1
138 fi
140 pid="$(cat gmid.pid || true)"
141 if [ "$pid" == "" ]; then
142 return 1
143 fi
145 # remember: we're running under ``set -e''
146 if ps $pid >/dev/null; then
147 return 0
148 fi
150 return 1
153 # usage: sha in out
154 # writes the sha256 of `in' to `out'
155 sha() {
156 if which sha256 >/dev/null 2>&1; then
157 sha256 < "$1" > "$2"
158 return $?
159 fi
161 if which sha256sum >/dev/null 2>&1; then
162 sha256sum "$1" | awk '{print $1}' > "$2"
163 return $?
164 fi
166 echo "No sha binary found" >&2
167 exit 1
170 count() {
171 wc -l | xargs
174 quit() {
175 pid="$(cat gmid.pid || true)"
176 if [ "$pid" != "" ]; then
177 kill $pid || true
178 wait || true
179 fi
182 onexit() {
183 rm -f bigfile bigfile.sha
184 quit