aboutsummaryrefslogtreecommitdiff
path: root/regress/tests.sh
blob: ccb7ea7430f25ef9902b5e2ad1796be8ac9be2f7 (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
test_configless_mode() {
	dont_check=yes

	$gmid -p $port -H localhost -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_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
	./sha bigfile bigfile.sha
	body="$(cat bigfile.sha)"

	check_reply "20 application/octet-stream" "$(cat testdata/bigfile.sha)"
}

test_dont_execute_scripts() {
	setup_simple_test

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

test_custom_mime() {
	setup_simple_test 'map "text/x-funny" to-ext "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_cgi_scripts() {
	setup_simple_test '' 'cgi "*"'

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

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

	fetch /err
	check_reply "42 CGI error" || return 1

	fetch /invalid
	check_reply "42 CGI error" || return 1
}

test_cgi_big_replies() {
	setup_simple_test '' 'cgi "*"'

	hdr="$(head /serve-bigfile)"
	get /bigfile > bigfile
	./sha bigfile bigfile.sha
	body="$(cat bigfile.sha)"
	check_reply "20 application/octet-stream" "$(cat testdata/bigfile.sha)"
}

test_cgi_split_query() {
	setup_simple_test '' 'cgi "*"'

	for s in "1" "2 ?foo" "3 ?foo+bar" "1 ?foo+bar=5" "3 ?foo+bar%3d5"; do
		exp="$(echo $s | sed 's/ .*//')"
		qry="$(echo $s | sed 's/^..//')"

		if [ "$exp" = "$qry" ]; then
			# the "1" case yields exp == qry
			qry=''
		fi

		url="/env$qry"

		n="$(get "$url" | awk /^-/ | count)"
		if [ $? -ne 0 ]; then
			echo "failed to get /$url"
			return 1
		fi

		if [ "$n" -ne $exp ]; then
			echo "Unexpected number of args"
			echo "want : $exp"
			echo "got  : $n"
			return 1
		fi
	done
}

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"

	# we expect 5 lines from the auto index

	body="$(get /dir/ | count)"
	if [ $? -ne 0 ]; then
		echo 'failed to get /dir/'
		return 1
	fi

	if [ "$body" -ne 5 ]; then
		echo "expected five lines from the auto index, got $body"
		return 1
	fi
}

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_entrypoint() {
	setup_simple_test '' 'entrypoint "/env"'

	fetch_hdr /foo/bar
	check_reply "20 text/plain; lang=en" || return 1

	# TODO: test something similar with plain cgi too

	body="$(get /foo/bar|grep PATH_INFO)"
	if [ $? -ne 0 ]; then
		echo "failed to get /foo/bar"
		return 1
	fi

	if [ "$body" != "PATH_INFO=/foo/bar" ]; then
		echo "Invalid PATH_INFO generated"
		echo "want : PATH_INFO=/foo/bar"
		echo "got  : $body"
		return 1
	fi
}

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.cert.pem -K invalid.key.pem"
	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() {
	# XXX: prefork 1 for testing
	setup_simple_test 'prefork 1' 'fastcgi spawn "'$PWD'/fcgi-test"'

	fetch /
	check_reply "20 text/gemini" "# Hello, world!"
}

test_macro_expansion() {
	cat <<EOF > reg.conf
pwd = "$PWD"
$config_common

server "localhost" {
	# the quoting of \$ is for sh
	cert \$pwd "/cert.pem"
	key  \$pwd "/key.pem"
	root \$pwd "/testdata"
}
EOF

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

	run

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

# 1.7.4 bugfix: check_for_cgi goes out-of-bound processing a string
# that doesn't contain a '/'
test_174_bugfix() {
	setup_simple_test '' 'cgi "*"'

	# thanks cage :)
	for i in 0 1 2 3 4 5 6 7 8 9; do
		fetch /favicon.txt
		check_reply "51 not found" || return 1
	done
}