aboutsummaryrefslogtreecommitdiff
path: root/regress/tests.sh
blob: f4dc0ebf49053f999ee49f0b79f108fe33c63c38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
test_punycode() {
	dont_check_server_alive=yes
	./puny-test
}

test_iri() {
	dont_check_server_alive=yes
	./iri_test
}

test_gg_n_flag() {
	dont_check_server_alive=yes
	$gg -n gemini://omarpolo.com/ || return 1

	# XXX this fails on macos in the CI, while in
	# test_iri passes successfully.  Unfortunately,
	# I can't debug stuff on darwin (lacking hardware.)
	#$gg -n "foo://bar.com/cafè.gmi" || return 1

	$gg -n gemini://omarpolo.com/../ || return 1
}

test_parse_comments_at_start() {
	dont_check_server_alive=yes

	cat <<EOF >reg.conf
# a comment

server "$server_name" {
	cert "$PWD/localhost.pem"
	key  "$PWD/localhost.key"
	root "$PWD/testdata"
	listen on $host port $port
}
EOF

	$gmid -n -c reg.conf >/dev/null
}

test_dump_config() {
	dont_check_server_alive=yes
	gen_config '' ''

        exp="$(mktemp)"
	got="$(mktemp)"
	cat <<EOF >$exp
prefork 3

server "localhost" {
	cert "$PWD/localhost.pem"
	key "$PWD/localhost.key"
}
EOF

	$gmid -nn -c reg.conf > $got 2>/dev/null

	ret=0
	if ! cmp -s "$exp" "$got"; then
    		echo "config differs!" >&2
    		diff -u "$exp" "$got" >&2
    		ret=1
	fi

	rm "$exp" "$got"
	return $ret
}

test_gemexp() {
	dont_check_server_alive=yes

	$gemexp -p $port -d . testdata &
	pid=$!
	sleep 1

	fetch /
	kill $pid
	check_reply "20 text/gemini" "# hello world" || return 1
}

test_static_files() {
	setup_simple_test

	fetch /
	check_reply "20 text/gemini" "# hello world" || return 1

	fetch /foo
	check_reply "51 not found" || return 1

	fetch /dir/foo.gmi
	check_reply "20 text/gemini" "# hello world" || return 1
}

test_alias() {
	alias="foo.example"
	setup_simple_test '' "alias '$alias'"
	gghost="$alias"
	ggflags="-P localhost:$port -H $alias"

	fetch /
	check_reply "20 text/gemini" "# hello world" || return 1
}

test_alias_long_hostname() {
	# longer than 64 chars to detect issues with glibc
	# posix-violating tiny HOST_NAME_MAX.
	alias=foo.jjijhrngx5ZxWY0p3o9jag3fOEuZQ64Iuler243oAkkCCqq8pFufw43DBLgVX.test

	setup_simple_test '' "alias '$alias'"

	fetch /
	check_reply "20 text/gemini" "# hello world" || return 1

	# now the same but against the alias

	gghost=$alias
	ggflags="-P localhost:$port -H $alias"

	fetch /
	check_reply "20 text/gemini" "# hello world" || return 1
}

test_directory_redirect() {
	setup_simple_test

	fetch /dir
	check_reply "30 /dir/" || return 1

	fetch /dir/
	check_reply "51 not found" || return 1
}

test_serve_big_files() {
	setup_simple_test

	hdr="$(head /bigfile)"
	get /bigfile > bigfile

	want="20 application/octet-stream"
	if [ "$hdr" != "$want" ]; then
		echo "Header mismatch" >&2
		echo "wants : $want"   >&2
		echo "got   : $hdr"    >&2
		return 1
	fi

	if ! cmp -s bigfile testdata/bigfile; then
		echo "received bigfile is not as expected"
		cmp bigfile testdata/bigfile
		return 1
	fi
}

test_dont_execute_scripts() {
	setup_simple_test

	fetch_hdr /hello
	check_reply "20 application/octet-stream" "" || return 1
}

test_custom_mime() {
	setup_simple_test '
types {
	text/x-funny gmi
}
' ''

	fetch_hdr /
	check_reply "20 text/x-funny"
}

test_default_type() {
	setup_simple_test '' 'default type "application/x-foo"'

	fetch_hdr /hello
	check_reply "20 application/x-foo"
}

test_custom_lang() {
	setup_simple_test '' 'lang it'

	fetch_hdr /
	check_reply "20 text/gemini;lang=it"
}

test_parse_custom_lang_per_location() {
	setup_simple_test '' \
	    'lang it location "/en/*" {lang en} location "/de/*" {lang de}'
	# can parse multiple locations
}

test_custom_index() {
	setup_simple_test '' 'index "foo.gmi"'

	fetch /dir/
	check_reply "20 text/gemini" "# hello world"
}

test_custom_index_default_type_per_location() {
	setup_simple_test '' 'location "/dir/*" { default type "text/plain" index "hello" }'

	fetch /dir/
	check_reply "20 text/plain" "$(cat hello)"
}

test_auto_index() {
	setup_simple_test '' 'location "/dir/*" { auto index on }'

	fetch /
	check_reply "20 text/gemini" "# hello world" || return 1

	fetch_hdr /dir
	check_reply "30 /dir/" || return 1

	fetch_hdr /dir/
	check_reply "20 text/gemini" || return 1

	get /dir/ > listing || return 1
	cat <<EOF > listing.expected
# Index of /dir/

=> ./../
=> ./current%20date
=> ./foo.gmi
=> ./hello
EOF

	cmp -s listing.expected listing
	ret=$?
	if [ $ret -ne 0 ]; then
		echo 'unexpected dir content:'
		diff -u listing.expected listing
	fi
	rm listing listing.expected

	return $ret
}

test_block() {
	setup_simple_test '' 'location "*" { block }'

	fetch /
	check_reply "40 temporary failure" || return 1

	fetch /nonexists
	check_reply "40 temporary failure" || return 1
}

test_block_return_fmt() {
	setup_simple_test '' '
location "/dir" {
	strip 1
	block return 40 "%% %p %q %P %N test"
}
location "*" {
	strip 99
	block return 40 "%% %p %q %P %N test"
}'

	fetch_hdr /dir/foo.gmi
	check_reply "40 % /foo.gmi  10965 localhost test" || return 1

	fetch_hdr /bigfile
	check_reply "40 % /  10965 localhost test" || return 1
}

test_require_client_ca() {
	setup_simple_test '' 'require client ca "'$PWD'/testca.pem"'

	fetch /
	check_reply "60 client certificate required" || return 1

	ggflags="-C valid.crt -K valid.key"
	fetch_hdr /
	check_reply "20 text/gemini" || return 1

	ggflags="-C invalid.pem -K invalid.key"
	fetch_hdr /
	check_reply "61 certificate not authorised" || return 1
}

test_root_inside_location() {
	setup_simple_test '' 'location "/foo/*" { root "'$PWD'/testdata" strip 1 }'

	fetch /foo
	check_reply "51 not found" || return 1

	fetch_hdr /foo/
	check_reply "20 text/gemini"
}

test_root_inside_location_with_redirect() {
	setup_simple_test '' '
location "/foo"   { block return 31 "%p/" }
location "/foo/*" { root "'$PWD'/testdata" strip 1 }'

	fetch /foo
	check_reply "31 /foo/" || return 1

	fetch_hdr /foo/
	check_reply "20 text/gemini"
}

test_fastcgi() {
	./fcgi-test fcgi.sock &
	fcgi_pid=$!

	setup_simple_test 'prefork 1' 'fastcgi socket "'$PWD'/fcgi.sock"'

	i=0
	while [ $i -lt 10 ]; do
		fetch /
		check_reply "20 text/gemini" "$(fcgi_content)"
		if [ $? -ne 0 ]; then
			kill $fcgi_pid
			return 1
		fi

		i=$(($i + 1))
	done

	kill $fcgi_pid
	return 0
}

test_fastcgi_inside_location() {
	./fcgi-test fcgi.sock &
	fcgi_pid=$!
	fcgi_path_info="/foo"
	fcgi_path="$PWD/testdata/foo"

	setup_simple_test 'prefork 1' 'fastcgi socket "'$PWD'/fcgi.sock"
	location "/dir/*" {
		fastcgi off
	}'

	fetch /foo
	if ! check_reply "20 text/gemini" "$(fcgi_content)"; then
		kill $fcgi_pid
		return 1
	fi

	fetch /dir/foo.gmi
	if ! check_reply "20 text/gemini" "# hello world"; then
		kill $fcgi_pid
		return 1
	fi

	kill $fcgi_pid
	return 0
}

test_fastcgi_location_match() {
	./fcgi-test fcgi.sock &
	fcgi_pid=$!
	fcgi_path_info="/foo"
	fcgi_path="$PWD/testdata/foo"

	setup_simple_test 'prefork 1' '
	location "/dir/*" {
		fastcgi off
	}
	location "/*" {
		fastcgi socket "'$PWD'/fcgi.sock"
	}'

	fetch /foo
	if ! check_reply "20 text/gemini" "$(fcgi_content)"; then
		kill $fcgi_pid
		return 1
	fi

	fetch /dir/foo.gmi
	if ! check_reply "20 text/gemini" "# hello world"; then
		kill $fcgi_pid
		return 1
	fi

	kill $fcgi_pid
	return 0
}

test_fastcgi_deprecated_syntax() {
	./fcgi-test fcgi.sock &
	fcgi_pid=$!

	# the old syntax will eventually go away, but check that the
	# backward compatibility works.
	setup_simple_test 'prefork 1' 'fastcgi "'$PWD'/fcgi.sock"'

	fetch /
	check_reply "20 text/gemini" "$(fcgi_content)"
	if [ $? -ne 0 ]; then
		kill $fcgi_pid
		return 1
	fi

	kill $fcgi_pid
	return 0
}

test_macro_expansion() {
	cat <<EOF > reg.conf
pwd = "$PWD"
common = "lang it; auto index on"

server "localhost" {
	# the quoting of \$ is for sh
	cert \$pwd "/localhost.pem"
	key  \$pwd "/localhost.key"
	root \$pwd "/testdata"
	listen on $host port $port
	@common
}
EOF

	if ! checkconf; then
		echo "failed to parse the config"
		return 1
	fi

	run

	fetch /
	check_reply "20 text/gemini;lang=it" "# hello world"
}

test_proxy_relay_to() {
	gen_config '' ''
	set_proxy ''

	run

	ggflags="-P localhost:$proxy_port -H localhost.local"

	fetch /
	check_reply "20 text/gemini" "# hello world"
}

test_proxy_with_certs() {
	ggflags="-P localhost:$proxy_port -H localhost.local"

	# first test using the valid keys

	gen_config '' 'require client ca "'$PWD'/testca.pem"'
	set_proxy "
		cert \"$PWD/valid.crt\"
		key \"$PWD/valid.key\"
	"
	run

	fetch /
	check_reply "20 text/gemini" "# hello world" || return 1

	# then using some invalid keys

	gen_config '' 'require client ca "'$PWD'/testca.pem"'
	set_proxy "
		cert \"$PWD/invalid.pem\"
		key \"$PWD/invalid.key\"
	"
	run

	fetch /
	check_reply "61 certificate not authorised" || return 1

	# and finally without keys

	gen_config '' 'require client ca "'$PWD'/testca.pem"'
	set_proxy ''
	run

	fetch /
	check_reply "60 client certificate required" || return 1
}

test_unknown_host() {
	setup_simple_test '' ''

	ggflags="-N -H foobar"
	fetch /
	check_reply '59 Wrong/malformed host or missing SNI'
}

test_include_mime() {
	setup_simple_test "types { include '$PWD/example.mime.types' }" ""

	fetch_hdr /
	check_reply '20 text/gemini' || return 1

	fetch_hdr /test.m3u8
	check_reply '20 application/vnd.apple.mpegurl' || return 1

	fetch_hdr /foo.1
	check_reply '20 text/x-mandoc' || return 1
}

test_log_file() {
	rm -f log log.edited
	setup_simple_test '
log access "'$PWD'/log"
log style legacy'

	fetch_hdr /
	check_reply '20 text/gemini'

	# remove the ip
	awk '{$1 = ""; print substr($0, 2)}' log > log.edited

	printf '%s\n' 'GET gemini://localhost/ 20 text/gemini' \
		| cmp -s - log.edited
	if [ $? -ne 0 ]; then
		# keep the log for post-mortem analysis
		return 1
	fi

	rm -f log log.edited
	return 0
}

test_log_common() {
	rm -f log log.edited
	setup_simple_test '
log access "'$PWD'/log"
log style common'

	fetch_hdr /
	check_reply '20 text/gemini'

	# remove the ip and timestamp
	awk '{$2 = ""; $5 = "timestamp"; print $0}' log > log.edited

	printf '%s\n' 'localhost  - - timestamp +0200 "gemini://localhost/" 20 0' \
		| cmp -s - log.edited
	if [ $? -ne 0 ]; then
		# keep the log for post-mortem analysis
		return 1
	fi

	rm -f log log.edited
	return 0
}

test_log_combined() {
	rm -f log log.edited
	setup_simple_test '
log access "'$PWD'/log"
log style combined'

	fetch_hdr /
	check_reply '20 text/gemini'

	# remove the ip and timestamp
	awk '{$1 = ""; print gensub("\\[.*\\]", "[timestamp]", 1)}' log > log.edited

	printf '%s\n' ' - - [timestamp] "gemini://localhost/" 20 0 "-" ""' \
		| cmp -s - log.edited
	if [ $? -ne 0 ]; then
		# keep the log for post-mortem analysis
		return 1
	fi

	rm -f log log.edited
	return 0
}

test_ipv4_addr() {
	server_name="*"
	host="127.0.0.1"
	gghost=127.0.0.1
	ggflags=-N
	setup_simple_test

	fetch /
	check_reply "20 text/gemini" "# hello world" || return 1
}

test_ipv6_addr() {
	server_name="*"
	host="::1"
	gghost="[::1]"
	ggflags=-N
	setup_simple_test

	fetch /
	check_reply "20 text/gemini" "# hello world" || return 1
}

test_ipv6_server() {
	server_name="::1"
	host="::1"
	gghost="[::1]"
	ggflags=-N
	setup_simple_test

	fetch /
	check_reply "20 text/gemini" "# hello world" || return 1
}

test_high_prefork() {
	quit

	setup_simple_test 'prefork 12'

	fetch /
	check_reply "20 text/gemini" "# hello world" || return 1

	dont_check_server_alive=yes
	quit
}

test_proxy_protocol_v1() {
	rm -f log
	proxy=proxy-v1
	gen_config '
log access "'$PWD'/log"
log style legacy' ''
	set_proxy 'proxy-v1'
	run

	ggflags="-P localhost:$proxy_port -H localhost.local"

	# This will generate two log entry: one for the "frontend"
	# and one for the proxied request.  Both should have exactly
	# the same IP and port.
	fetch_hdr /
	check_reply '20 text/gemini'

	n=$(awk '{print $1}' log | uniq | wc -l)
	if [ "$n" -ne 1 ]; then
		# keep the log for post-mortem analysis
		echo
		echo "n is $n"
		return 1
	fi

	rm -f log
	return 0
}