Blob


1 test_punycode() {
2 dont_check_server_alive=yes
3 ./puny-test
4 }
6 test_iri() {
7 dont_check_server_alive=yes
8 ./iri_test
9 }
11 test_dump_config() {
12 dont_check_server_alive=yes
13 gen_config '' ''
15 exp="$(mktemp)"
16 got="$(mktemp)"
17 cat <<EOF >$exp
18 prefork 3
20 server "localhost" {
21 cert "$PWD/localhost.pem"
22 key "$PWD/localhost.key"
23 }
24 EOF
26 $gmid -nn -c reg.conf > $got 2>/dev/null
28 ret=0
29 if ! cmp -s "$exp" "$got"; then
30 echo "config differs!" >&2
31 diff -u "$exp" "$got" >&2
32 ret=1
33 fi
35 rm "$exp" "$got"
36 return $ret
37 }
39 test_gemexp() {
40 dont_check_server_alive=yes
42 $gemexp -p $port -d . testdata &
43 pid=$!
44 sleep 1
46 fetch /
47 kill $pid
48 check_reply "20 text/gemini" "# hello world" || return 1
49 }
51 test_static_files() {
52 setup_simple_test
54 fetch /
55 check_reply "20 text/gemini" "# hello world" || return 1
57 fetch /foo
58 check_reply "51 not found" || return 1
60 fetch /dir/foo.gmi
61 check_reply "20 text/gemini" "# hello world" || return 1
62 }
64 test_directory_redirect() {
65 setup_simple_test
67 fetch /dir
68 check_reply "30 /dir/" || return 1
70 fetch /dir/
71 check_reply "51 not found" || return 1
72 }
74 test_serve_big_files() {
75 setup_simple_test
77 hdr="$(head /bigfile)"
78 get /bigfile > bigfile
80 want="20 application/octet-stream"
81 if [ "$hdr" != "$want" ]; then
82 echo "Header mismatch" >&2
83 echo "wants : $want" >&2
84 echo "got : $hdr" >&2
85 return 1
86 fi
88 if ! cmp -s bigfile testdata/bigfile; then
89 echo "received bigfile is not as expected"
90 cmp bigfile testdata/bigfile
91 return 1
92 fi
93 }
95 test_dont_execute_scripts() {
96 setup_simple_test
98 fetch_hdr /hello
99 check_reply "20 application/octet-stream" "" || return 1
102 test_custom_mime() {
103 setup_simple_test '
104 types {
105 text/x-funny gmi
107 ' ''
109 fetch_hdr /
110 check_reply "20 text/x-funny"
113 test_default_type() {
114 setup_simple_test '' 'default type "application/x-foo"'
116 fetch_hdr /hello
117 check_reply "20 application/x-foo"
120 test_custom_lang() {
121 setup_simple_test '' 'lang it'
123 fetch_hdr /
124 check_reply "20 text/gemini;lang=it"
127 test_parse_custom_lang_per_location() {
128 setup_simple_test '' \
129 'lang it location "/en/*" {lang en} location "/de/*" {lang de}'
130 # can parse multiple locations
133 test_custom_index() {
134 setup_simple_test '' 'index "foo.gmi"'
136 fetch /dir/
137 check_reply "20 text/gemini" "# hello world"
140 test_custom_index_default_type_per_location() {
141 setup_simple_test '' 'location "/dir/*" { default type "text/plain" index "hello" }'
143 fetch /dir/
144 check_reply "20 text/plain" "$(cat hello)"
147 test_auto_index() {
148 setup_simple_test '' 'location "/dir/*" { auto index on }'
150 fetch /
151 check_reply "20 text/gemini" "# hello world" || return 1
153 fetch_hdr /dir
154 check_reply "30 /dir/" || return 1
156 fetch_hdr /dir/
157 check_reply "20 text/gemini" || return 1
159 get /dir/ > listing || return 1
160 cat <<EOF > listing.expected
161 # Index of /dir/
163 => ./../
164 => ./current%20date
165 => ./foo.gmi
166 => ./hello
167 EOF
169 cmp -s listing.expected listing
170 ret=$?
171 if [ $ret -ne 0 ]; then
172 echo 'unexpected dir content:'
173 diff -u listing.expected listing
174 fi
175 rm listing listing.expected
177 return $ret
180 test_block() {
181 setup_simple_test '' 'location "*" { block }'
183 fetch /
184 check_reply "40 temporary failure" || return 1
186 fetch /nonexists
187 check_reply "40 temporary failure" || return 1
190 test_block_return_fmt() {
191 setup_simple_test '' '
192 location "/dir" {
193 strip 1
194 block return 40 "%% %p %q %P %N test"
196 location "*" {
197 strip 99
198 block return 40 "%% %p %q %P %N test"
199 }'
201 fetch_hdr /dir/foo.gmi
202 check_reply "40 % /foo.gmi 10965 localhost test" || return 1
204 fetch_hdr /bigfile
205 check_reply "40 % / 10965 localhost test" || return 1
208 test_require_client_ca() {
209 setup_simple_test '' 'require client ca "'$PWD'/testca.pem"'
211 fetch /
212 check_reply "60 client certificate required" || return 1
214 ggflags="-C valid.crt -K valid.key"
215 fetch_hdr /
216 check_reply "20 text/gemini" || return 1
218 ggflags="-C invalid.pem -K invalid.key"
219 fetch_hdr /
220 check_reply "61 certificate not authorised" || return 1
223 test_root_inside_location() {
224 setup_simple_test '' 'location "/foo/*" { root "'$PWD'/testdata" strip 1 }'
226 fetch /foo
227 check_reply "51 not found" || return 1
229 fetch_hdr /foo/
230 check_reply "20 text/gemini"
233 test_root_inside_location_with_redirect() {
234 setup_simple_test '' '
235 location "/foo" { block return 31 "%p/" }
236 location "/foo/*" { root "'$PWD'/testdata" strip 1 }'
238 fetch /foo
239 check_reply "31 /foo/" || return 1
241 fetch_hdr /foo/
242 check_reply "20 text/gemini"
245 test_fastcgi() {
246 ./fcgi-test fcgi.sock &
247 fcgi_pid=$!
249 setup_simple_test 'prefork 1' 'fastcgi socket "'$PWD'/fcgi.sock"'
251 msg=$(printf "# hello from fastcgi!\nsome more content in the page...")
253 i=0
254 while [ $i -lt 10 ]; do
255 fetch /
256 check_reply "20 text/gemini" "$msg"
257 if [ $? -ne 0 ]; then
258 kill $fcgi_pid
259 return 1
260 fi
262 i=$(($i + 1))
263 done
265 kill $fcgi_pid
266 return 0
269 test_fastcgi_inside_location() {
270 ./fcgi-test fcgi.sock &
271 fcgi_pid=$!
273 setup_simple_test 'prefork 1' 'fastcgi socket "'$PWD'/fcgi.sock"
274 location "/dir/*" {
275 fastcgi off
276 }'
278 msg=$(printf "# hello from fastcgi!\nsome more content in the page...")
279 fetch /foo
280 if ! check_reply "20 text/gemini" "$msg"; then
281 kill $fcgi_pid
282 return 1
283 fi
285 fetch /dir/foo.gmi
286 if ! check_reply "20 text/gemini" "# hello world"; then
287 kill $fcgi_pid
288 return 1
289 fi
291 kill $fcgi_pid
292 return 0
295 test_fastcgi_deprecated_syntax() {
296 ./fcgi-test fcgi.sock &
297 fcgi_pid=$!
299 # the old syntax will eventually go away, but check that the
300 # backward compatibility works.
301 setup_simple_test 'prefork 1' 'fastcgi "'$PWD'/fcgi.sock"'
303 msg=$(printf "# hello from fastcgi!\nsome more content in the page...")
304 fetch /
305 check_reply "20 text/gemini" "$msg"
306 if [ $? -ne 0 ]; then
307 kill $fcgi_pid
308 return 1
309 fi
311 kill $fcgi_pid
312 return 0
315 test_macro_expansion() {
316 cat <<EOF > reg.conf
317 pwd = "$PWD"
318 common = "lang it; auto index on"
320 server "localhost" {
321 # the quoting of \$ is for sh
322 cert \$pwd "/localhost.pem"
323 key \$pwd "/localhost.key"
324 root \$pwd "/testdata"
325 listen on $REGRESS_HOST port $port
326 @common
328 EOF
330 if ! checkconf; then
331 echo "failed to parse the config"
332 return 1
333 fi
335 run
337 fetch /
338 check_reply "20 text/gemini;lang=it" "# hello world"
341 test_proxy_relay_to() {
342 gen_config '' ''
343 set_proxy ''
345 run
347 ggflags="-P localhost:$port -H localhost.local"
349 fetch /
350 check_reply "20 text/gemini" "# hello world"
353 test_proxy_with_certs() {
354 ggflags="-P localhost:$port -H localhost.local"
356 # first test using the valid keys
358 gen_config '' 'require client ca "'$PWD'/testca.pem"'
359 set_proxy "
360 cert \"$PWD/valid.crt\"
361 key \"$PWD/valid.key\"
363 run
365 fetch /
366 check_reply "20 text/gemini" "# hello world" || return 1
368 # then using some invalid keys
370 gen_config '' 'require client ca "'$PWD'/testca.pem"'
371 set_proxy "
372 cert \"$PWD/invalid.pem\"
373 key \"$PWD/invalid.key\"
375 run
377 fetch /
378 check_reply "61 certificate not authorised" || return 1
380 # and finally without keys
382 gen_config '' 'require client ca "'$PWD'/testca.pem"'
383 set_proxy ''
384 run
386 fetch /
387 check_reply "60 client certificate required" || return 1
390 test_unknown_host() {
391 setup_simple_test '' ''
393 ggflags="-N -H foobar"
394 fetch /
395 check_reply '59 Wrong/malformed host or missing SNI'
398 test_include_mime() {
399 setup_simple_test "types { include '$PWD/example.mime.types' }" ""
401 fetch_hdr /
402 check_reply '20 text/gemini' || return 1
404 fetch_hdr /test.m3u8
405 check_reply '20 application/vnd.apple.mpegurl' || return 1
407 fetch_hdr /foo.1
408 check_reply '20 text/x-mandoc' || return 1
411 test_log_file() {
412 rm -f log log.edited
413 setup_simple_test '
414 log access "'$PWD'/log"
415 log style legacy'
417 fetch_hdr /
418 check_reply '20 text/gemini'
420 # remove the ip
421 awk '{$1 = ""; print substr($0, 2)}' log > log.edited
423 printf '%s\n' 'GET gemini://localhost/ 20 text/gemini' \
424 | cmp -s - log.edited
425 if [ $? -ne 0 ]; then
426 # keep the log for post-mortem analysis
427 return 1
428 fi
430 rm -f log log.edited
431 return 0