blob: d83bafeab32d40d6fd373e084757221ac7181d06 [file] [log] [blame]
Jonathan Niederc74c7202013-11-25 13:03:06 -08001# Shell library to run an HTTP server for use in tests.
2# Ends the test early if httpd tests should not be run,
3# for example because the user has not enabled them.
4#
5# Usage:
6#
7# . ./test-lib.sh
8# . "$TEST_DIRECTORY"/lib-httpd.sh
9# start_httpd
10#
11# test_expect_success '...' '
12# ...
13# '
14#
15# test_expect_success ...
16#
Jonathan Niederc74c7202013-11-25 13:03:06 -080017# test_done
18#
19# Can be configured using the following variables.
20#
21# GIT_TEST_HTTPD enable HTTPD tests
22# LIB_HTTPD_PATH web server path
23# LIB_HTTPD_MODULE_PATH web server modules path
24# LIB_HTTPD_PORT listening port
25# LIB_HTTPD_DAV enable DAV
Eric Wonga8a5d252016-07-23 04:26:08 +000026# LIB_HTTPD_SVN enable SVN at given location (e.g. "svn")
Jonathan Niederc74c7202013-11-25 13:03:06 -080027# LIB_HTTPD_SSL enable SSL
Jeff King29ae2c92023-02-16 15:56:32 -050028# LIB_HTTPD_PROXY enable proxy
Clemens Buchacherfaa4bc32008-02-27 20:28:45 +010029#
30# Copyright (c) 2008 Clemens Buchacher <drizzd@aon.at>
31#
32
Ævar Arnfjörð Bjarmason7596fe92022-06-15 12:36:32 +020033if ! test_have_prereq LIBCURL
Jeff Kingc9d441a2015-05-06 13:42:29 -040034then
35 skip_all='skipping test, git built without http support'
36 test_done
37fi
38
Junio C Hamano309a9e32015-05-07 09:06:14 -070039if test -n "$NO_EXPAT" && test -n "$LIB_HTTPD_DAV"
40then
41 skip_all='skipping test, git built without expat support'
42 test_done
43fi
44
SZEDER Gábor43a2afe2019-11-22 14:14:36 +010045if ! test_bool_env GIT_TEST_HTTPD true
Clemens Buchacherfaa4bc32008-02-27 20:28:45 +010046then
Jeff King83d842d2014-02-10 16:29:37 -050047 skip_all="Network testing disabled (unset GIT_TEST_HTTPD to enable)"
Clemens Buchacherfaa4bc32008-02-27 20:28:45 +010048 test_done
Clemens Buchacherfaa4bc32008-02-27 20:28:45 +010049fi
50
Jeff King1767c512015-01-16 04:16:49 -050051if ! test_have_prereq NOT_ROOT; then
Ævar Arnfjörð Bjarmason3b072c52019-06-21 12:18:11 +020052 test_skip_or_die GIT_TEST_HTTPD \
Jeff Kinga1a30112014-05-10 10:02:59 -040053 "Cannot run httpd tests as root"
54fi
55
Jay Soffian69707d62009-02-25 03:28:15 -050056HTTPD_PARA=""
57
Patrick Steinhardt7d059742023-11-10 09:17:00 +010058for DEFAULT_HTTPD_PATH in '/usr/sbin/httpd' \
59 '/usr/sbin/apache2' \
60 "$(command -v httpd)" \
61 "$(command -v apache2)"
Tarmigan Casebolt0d344732010-01-02 14:04:25 -080062do
Patrick Steinhardt7d059742023-11-10 09:17:00 +010063 if test -n "$DEFAULT_HTTPD_PATH" && test -x "$DEFAULT_HTTPD_PATH"
Tarmigan Casebolt0d344732010-01-02 14:04:25 -080064 then
65 break
66 fi
67done
68
Patrick Steinhardt7d059742023-11-10 09:17:00 +010069if test -x "$DEFAULT_HTTPD_PATH"
70then
71 DETECTED_HTTPD_ROOT="$("$DEFAULT_HTTPD_PATH" -V 2>/dev/null | sed -n 's/^ -D HTTPD_ROOT="\(.*\)"$/\1/p')"
72fi
73
Tarmigan Casebolt0d344732010-01-02 14:04:25 -080074for DEFAULT_HTTPD_MODULE_PATH in '/usr/libexec/apache2' \
75 '/usr/lib/apache2/modules' \
76 '/usr/lib64/httpd/modules' \
Eric Sunshine1c7dc232022-11-21 03:01:35 +000077 '/usr/lib/httpd/modules' \
Patrick Steinhardt7d059742023-11-10 09:17:00 +010078 '/usr/libexec/httpd' \
Junio C Hamano14a44452023-12-09 16:37:48 -080079 '/usr/lib/apache2' \
Patrick Steinhardt7d059742023-11-10 09:17:00 +010080 "${DETECTED_HTTPD_ROOT:+${DETECTED_HTTPD_ROOT}/modules}"
Tarmigan Casebolt0d344732010-01-02 14:04:25 -080081do
Patrick Steinhardt7d059742023-11-10 09:17:00 +010082 if test -n "$DEFAULT_HTTPD_MODULE_PATH" && test -d "$DEFAULT_HTTPD_MODULE_PATH"
Tarmigan Casebolt0d344732010-01-02 14:04:25 -080083 then
84 break
85 fi
86done
87
Jay Soffian69707d62009-02-25 03:28:15 -050088case $(uname) in
89 Darwin)
Jay Soffian69707d62009-02-25 03:28:15 -050090 HTTPD_PARA="$HTTPD_PARA -DDarwin"
91 ;;
Jay Soffian69707d62009-02-25 03:28:15 -050092esac
93
94LIB_HTTPD_PATH=${LIB_HTTPD_PATH-"$DEFAULT_HTTPD_PATH"}
SZEDER Gáborfa840582019-01-05 02:08:58 +010095test_set_port LIB_HTTPD_PORT
Clemens Buchacherfaa4bc32008-02-27 20:28:45 +010096
Junio C Hamanobfdbee92008-08-08 02:26:28 -070097TEST_PATH="$TEST_DIRECTORY"/lib-httpd
Clemens Buchacherfaa4bc32008-02-27 20:28:45 +010098HTTPD_ROOT_PATH="$PWD"/httpd
99HTTPD_DOCUMENT_ROOT_PATH=$HTTPD_ROOT_PATH/www
100
Jeff Kingf6288252012-07-24 09:43:59 -0400101# hack to suppress apache PassEnv warnings
102GIT_VALGRIND=$GIT_VALGRIND; export GIT_VALGRIND
103GIT_VALGRIND_OPTIONS=$GIT_VALGRIND_OPTIONS; export GIT_VALGRIND_OPTIONS
Todd Zullingere18edc72019-02-14 01:35:13 -0500104GIT_TEST_SIDEBAND_ALL=$GIT_TEST_SIDEBAND_ALL; export GIT_TEST_SIDEBAND_ALL
Jeff King89c57ab2015-03-13 00:51:15 -0400105GIT_TRACE=$GIT_TRACE; export GIT_TRACE
Jeff Kingf6288252012-07-24 09:43:59 -0400106
Clemens Buchacherfaa4bc32008-02-27 20:28:45 +0100107if ! test -x "$LIB_HTTPD_PATH"
108then
Ævar Arnfjörð Bjarmason3b072c52019-06-21 12:18:11 +0200109 test_skip_or_die GIT_TEST_HTTPD "no web server found at '$LIB_HTTPD_PATH'"
Clemens Buchacherfaa4bc32008-02-27 20:28:45 +0100110fi
111
Elia Pintoe429dfd2015-12-22 15:10:30 +0100112HTTPD_VERSION=$($LIB_HTTPD_PATH -v | \
Jeff Kingd7626172023-02-01 06:37:21 -0500113 sed -n 's/^Server version: Apache\/\([0-9.]*\).*$/\1/p; q')
114HTTPD_VERSION_MAJOR=$(echo $HTTPD_VERSION | cut -d. -f1)
115HTTPD_VERSION_MINOR=$(echo $HTTPD_VERSION | cut -d. -f2)
Clemens Buchacherfaa4bc32008-02-27 20:28:45 +0100116
Jeff Kingd7626172023-02-01 06:37:21 -0500117if test -n "$HTTPD_VERSION_MAJOR"
Clemens Buchacherfaa4bc32008-02-27 20:28:45 +0100118then
119 if test -z "$LIB_HTTPD_MODULE_PATH"
120 then
Jeff Kingd7626172023-02-01 06:37:21 -0500121 if ! test "$HTTPD_VERSION_MAJOR" -eq 2 ||
Jeff Kingedd060d2023-02-01 06:38:24 -0500122 ! test "$HTTPD_VERSION_MINOR" -ge 4
Clemens Buchacherfaa4bc32008-02-27 20:28:45 +0100123 then
Ævar Arnfjörð Bjarmason3b072c52019-06-21 12:18:11 +0200124 test_skip_or_die GIT_TEST_HTTPD \
Jeff Kingedd060d2023-02-01 06:38:24 -0500125 "at least Apache version 2.4 is required"
Clemens Buchacherfaa4bc32008-02-27 20:28:45 +0100126 fi
Tarmigan Casebolt0d344732010-01-02 14:04:25 -0800127 if ! test -d "$DEFAULT_HTTPD_MODULE_PATH"
128 then
Ævar Arnfjörð Bjarmason3b072c52019-06-21 12:18:11 +0200129 test_skip_or_die GIT_TEST_HTTPD \
Jeff King83d842d2014-02-10 16:29:37 -0500130 "Apache module directory not found"
Tarmigan Casebolt0d344732010-01-02 14:04:25 -0800131 fi
Clemens Buchacherfaa4bc32008-02-27 20:28:45 +0100132
Jay Soffian69707d62009-02-25 03:28:15 -0500133 LIB_HTTPD_MODULE_PATH="$DEFAULT_HTTPD_MODULE_PATH"
Clemens Buchacherfaa4bc32008-02-27 20:28:45 +0100134 fi
135else
Ævar Arnfjörð Bjarmason3b072c52019-06-21 12:18:11 +0200136 test_skip_or_die GIT_TEST_HTTPD \
Jeff King83d842d2014-02-10 16:29:37 -0500137 "Could not identify web server at '$LIB_HTTPD_PATH'"
Clemens Buchacherfaa4bc32008-02-27 20:28:45 +0100138fi
139
Patrick Steinhardt0d3911a2023-11-09 09:05:50 +0100140if test -n "$LIB_HTTPD_DAV" && test -f /etc/os-release
141then
142 case "$(grep "^ID=" /etc/os-release | cut -d= -f2-)" in
143 alpine)
144 # The WebDAV module in Alpine Linux is broken at least up to
145 # Alpine v3.16 as the default DBM driver is missing.
146 #
147 # https://gitlab.alpinelinux.org/alpine/aports/-/issues/13112
148 test_skip_or_die GIT_TEST_HTTPD \
149 "Apache WebDAV module does not have default DBM backend driver"
150 ;;
151 esac
152fi
153
Jeff Kingc7db2d12014-05-22 05:28:56 -0400154install_script () {
155 write_script "$HTTPD_ROOT_PATH/$1" <"$TEST_PATH/$1"
156}
157
Clemens Buchacherfaa4bc32008-02-27 20:28:45 +0100158prepare_httpd() {
Mike Hommey13b54812008-07-07 21:02:37 +0200159 mkdir -p "$HTTPD_DOCUMENT_ROOT_PATH"
Gabriel Corona3cf8fe12010-11-14 02:51:14 +0100160 cp "$TEST_PATH"/passwd "$HTTPD_ROOT_PATH"
Jeff King29ae2c92023-02-16 15:56:32 -0500161 cp "$TEST_PATH"/proxy-passwd "$HTTPD_ROOT_PATH"
Denton Liu74b082a2020-05-19 06:53:58 -0400162 install_script incomplete-length-upload-pack-v2-http.sh
163 install_script incomplete-body-upload-pack-v2-http.sh
Jeff Kinge4c95382021-10-18 15:43:47 -0400164 install_script error-no-report.sh
Jeff Kingc7db2d12014-05-22 05:28:56 -0400165 install_script broken-smart-http.sh
Josh Steadmon30dea562019-02-06 14:19:10 -0500166 install_script error-smart-http.sh
Jeff Kingdbcf2bd2014-05-22 05:29:03 -0400167 install_script error.sh
Johannes Schindelineafff6e2020-02-27 13:23:11 +0000168 install_script apply-one-time-perl.sh
Matthew John Cheetham988aad92023-02-27 17:20:18 +0000169 install_script nph-custom-auth.sh
Clemens Buchacherfaa4bc32008-02-27 20:28:45 +0100170
Mike Hommey13b54812008-07-07 21:02:37 +0200171 ln -s "$LIB_HTTPD_MODULE_PATH" "$HTTPD_ROOT_PATH/modules"
Clemens Buchacherfaa4bc32008-02-27 20:28:45 +0100172
173 if test -n "$LIB_HTTPD_SSL"
174 then
Jeff King52325862011-07-18 03:49:12 -0400175 HTTPD_PROTO=https
Clemens Buchacherfaa4bc32008-02-27 20:28:45 +0100176
177 RANDFILE_PATH="$HTTPD_ROOT_PATH"/.rnd openssl req \
Mike Hommey13b54812008-07-07 21:02:37 +0200178 -config "$TEST_PATH/ssl.cnf" \
Clemens Buchacherfaa4bc32008-02-27 20:28:45 +0100179 -new -x509 -nodes \
Mike Hommey13b54812008-07-07 21:02:37 +0200180 -out "$HTTPD_ROOT_PATH/httpd.pem" \
181 -keyout "$HTTPD_ROOT_PATH/httpd.pem"
Bryan Donlan0e46e702008-05-04 01:37:58 -0400182 GIT_SSL_NO_VERIFY=t
183 export GIT_SSL_NO_VERIFY
Clemens Buchacherfaa4bc32008-02-27 20:28:45 +0100184 HTTPD_PARA="$HTTPD_PARA -DSSL"
185 else
Jeff King52325862011-07-18 03:49:12 -0400186 HTTPD_PROTO=http
Clemens Buchacherfaa4bc32008-02-27 20:28:45 +0100187 fi
Jeff King52325862011-07-18 03:49:12 -0400188 HTTPD_DEST=127.0.0.1:$LIB_HTTPD_PORT
189 HTTPD_URL=$HTTPD_PROTO://$HTTPD_DEST
190 HTTPD_URL_USER=$HTTPD_PROTO://user%40host@$HTTPD_DEST
Jeff Kingafbf5ca2014-01-02 02:38:35 -0500191 HTTPD_URL_USER_PASS=$HTTPD_PROTO://user%40host:pass%40host@$HTTPD_DEST
Clemens Buchacherfaa4bc32008-02-27 20:28:45 +0100192
Elia Pintoce5dadb2014-06-06 07:55:54 -0700193 if test -n "$LIB_HTTPD_DAV" || test -n "$LIB_HTTPD_SVN"
Clemens Buchacherfaa4bc32008-02-27 20:28:45 +0100194 then
195 HTTPD_PARA="$HTTPD_PARA -DDAV"
196
197 if test -n "$LIB_HTTPD_SVN"
198 then
199 HTTPD_PARA="$HTTPD_PARA -DSVN"
Eric Wonga8a5d252016-07-23 04:26:08 +0000200 LIB_HTTPD_SVNPATH="$rawsvnrepo"
201 svnrepo="http://127.0.0.1:$LIB_HTTPD_PORT/"
202 svnrepo="$svnrepo$LIB_HTTPD_SVN"
203 export LIB_HTTPD_SVN LIB_HTTPD_SVNPATH
Clemens Buchacherfaa4bc32008-02-27 20:28:45 +0100204 fi
205 fi
Jeff King29ae2c92023-02-16 15:56:32 -0500206
207 if test -n "$LIB_HTTPD_PROXY"
208 then
209 HTTPD_PARA="$HTTPD_PARA -DPROXY"
210 fi
Clemens Buchacherfaa4bc32008-02-27 20:28:45 +0100211}
212
Jeff King73c49a42022-11-11 22:35:05 +0000213enable_http2 () {
214 HTTPD_PARA="$HTTPD_PARA -DHTTP2"
215 test_set_prereq HTTP2
216}
217
Jeff Kingeb1c42d2023-05-18 15:21:02 -0400218enable_cgipassauth () {
219 # We are looking for 2.4.13 or more recent. Since we only support
220 # 2.4 and up, no need to check for older major/minor.
221 if test "$HTTPD_VERSION_MAJOR" = 2 &&
222 test "$HTTPD_VERSION_MINOR" = 4 &&
223 test "$(echo $HTTPD_VERSION | cut -d. -f3)" -lt 13
224 then
225 echo >&4 "apache $HTTPD_VERSION too old for CGIPassAuth"
226 return
227 fi
228 HTTPD_PARA="$HTTPD_PARA -DUSE_CGIPASSAUTH"
229 test_set_prereq CGIPASSAUTH
230}
231
Clemens Buchacherfaa4bc32008-02-27 20:28:45 +0100232start_httpd() {
Jeff King75318a32009-02-25 03:32:08 -0500233 prepare_httpd >&3 2>&4
Clemens Buchacherfaa4bc32008-02-27 20:28:45 +0100234
SZEDER Gábor8c3b9f72019-03-13 13:24:13 +0100235 test_atexit stop_httpd
Clemens Buchacherfaa4bc32008-02-27 20:28:45 +0100236
Mike Hommey13b54812008-07-07 21:02:37 +0200237 "$LIB_HTTPD_PATH" -d "$HTTPD_ROOT_PATH" \
238 -f "$TEST_PATH/apache.conf" $HTTPD_PARA \
Jeff King75318a32009-02-25 03:32:08 -0500239 -c "Listen 127.0.0.1:$LIB_HTTPD_PORT" -k start \
240 >&3 2>&4
Clemens Buchachera5adcbe2009-06-01 14:28:25 +0200241 if test $? -ne 0
242 then
Nguyễn Thái Ngọc Duy44f243d2016-06-13 19:35:09 +0700243 cat "$HTTPD_ROOT_PATH"/error.log >&4 2>/dev/null
Ævar Arnfjörð Bjarmason3b072c52019-06-21 12:18:11 +0200244 test_skip_or_die GIT_TEST_HTTPD "web server setup failed"
Jeff King75318a32009-02-25 03:32:08 -0500245 fi
Clemens Buchacherfaa4bc32008-02-27 20:28:45 +0100246}
247
248stop_httpd() {
Mike Hommey13b54812008-07-07 21:02:37 +0200249 "$LIB_HTTPD_PATH" -d "$HTTPD_ROOT_PATH" \
Jay Soffian69707d62009-02-25 03:28:15 -0500250 -f "$TEST_PATH/apache.conf" $HTTPD_PARA -k stop
Clemens Buchacherfaa4bc32008-02-27 20:28:45 +0100251}
Tay Ray Chuan6cbd6e92010-03-02 18:49:26 +0800252
Junio C Hamano05c1eb12013-08-02 15:14:50 -0700253test_http_push_nonff () {
Tay Ray Chuan6cbd6e92010-03-02 18:49:26 +0800254 REMOTE_REPO=$1
255 LOCAL_REPO=$2
256 BRANCH=$3
Junio C Hamano05c1eb12013-08-02 15:14:50 -0700257 EXPECT_CAS_RESULT=${4-failure}
Tay Ray Chuan6cbd6e92010-03-02 18:49:26 +0800258
259 test_expect_success 'non-fast-forward push fails' '
260 cd "$REMOTE_REPO" &&
261 HEAD=$(git rev-parse --verify HEAD) &&
262
263 cd "$LOCAL_REPO" &&
264 git checkout $BRANCH &&
265 echo "changed" > path2 &&
266 git commit -a -m path2 --amend &&
267
Brandon Casey77b5be22010-07-20 13:27:55 -0500268 test_must_fail git push -v origin >output 2>&1 &&
Ævar Arnfjörð Bjarmason0cd1a882023-02-06 23:44:30 +0100269 (
270 cd "$REMOTE_REPO" &&
271 echo "$HEAD" >expect &&
272 git rev-parse --verify HEAD >actual &&
273 test_cmp expect actual
274 )
Tay Ray Chuan6cbd6e92010-03-02 18:49:26 +0800275 '
276
277 test_expect_success 'non-fast-forward push show ref status' '
278 grep "^ ! \[rejected\][ ]*$BRANCH -> $BRANCH (non-fast-forward)$" output
279 '
280
Junio C Hamanod3bd0422011-04-12 16:12:47 -0700281 test_expect_success 'non-fast-forward push shows help message' '
Junio C Hamano67892752023-10-31 14:23:30 +0900282 test_grep "Updates were rejected because" output
Tay Ray Chuan6cbd6e92010-03-02 18:49:26 +0800283 '
Junio C Hamanoe69fa702013-08-01 11:05:02 -0700284
Jeff King42aa29e2013-09-20 06:25:00 -0400285 test_expect_${EXPECT_CAS_RESULT} 'force with lease aka cas' '
Junio C Hamanoe69fa702013-08-01 11:05:02 -0700286 HEAD=$( cd "$REMOTE_REPO" && git rev-parse --verify HEAD ) &&
287 test_when_finished '\''
288 (cd "$REMOTE_REPO" && git update-ref HEAD "$HEAD")
289 '\'' &&
290 (
291 cd "$LOCAL_REPO" &&
292 git push -v --force-with-lease=$BRANCH:$HEAD origin
293 ) &&
294 git rev-parse --verify "$BRANCH" >expect &&
295 (
296 cd "$REMOTE_REPO" && git rev-parse --verify HEAD
297 ) >actual &&
298 test_cmp expect actual
299 '
Tay Ray Chuan6cbd6e92010-03-02 18:49:26 +0800300}
Jeff Kinge8379362012-08-27 09:24:31 -0400301
302setup_askpass_helper() {
303 test_expect_success 'setup askpass helper' '
304 write_script "$TRASH_DIRECTORY/askpass" <<-\EOF &&
305 echo >>"$TRASH_DIRECTORY/askpass-query" "askpass: $*" &&
Jeff Kingafbf5ca2014-01-02 02:38:35 -0500306 case "$*" in
307 *Username*)
308 what=user
309 ;;
310 *Password*)
311 what=pass
312 ;;
313 esac &&
314 cat "$TRASH_DIRECTORY/askpass-$what"
Jeff Kinge8379362012-08-27 09:24:31 -0400315 EOF
316 GIT_ASKPASS="$TRASH_DIRECTORY/askpass" &&
317 export GIT_ASKPASS &&
318 export TRASH_DIRECTORY
319 '
320}
321
322set_askpass() {
323 >"$TRASH_DIRECTORY/askpass-query" &&
Jeff Kingafbf5ca2014-01-02 02:38:35 -0500324 echo "$1" >"$TRASH_DIRECTORY/askpass-user" &&
325 echo "$2" >"$TRASH_DIRECTORY/askpass-pass"
Jeff Kinge8379362012-08-27 09:24:31 -0400326}
327
328expect_askpass() {
Jeff King050ef362013-09-28 04:35:35 -0400329 dest=$HTTPD_DEST${3+/$3}
330
Jeff Kinge8379362012-08-27 09:24:31 -0400331 {
332 case "$1" in
333 none)
334 ;;
335 pass)
Jeff King9d15b1e2023-02-23 06:05:55 -0500336 echo "askpass: Password for '$HTTPD_PROTO://$2@$dest': "
Jeff Kinge8379362012-08-27 09:24:31 -0400337 ;;
338 both)
Jeff King9d15b1e2023-02-23 06:05:55 -0500339 echo "askpass: Username for '$HTTPD_PROTO://$dest': "
340 echo "askpass: Password for '$HTTPD_PROTO://$2@$dest': "
Jeff Kinge8379362012-08-27 09:24:31 -0400341 ;;
342 *)
343 false
344 ;;
345 esac
346 } >"$TRASH_DIRECTORY/askpass-expect" &&
347 test_cmp "$TRASH_DIRECTORY/askpass-expect" \
348 "$TRASH_DIRECTORY/askpass-query"
349}
SZEDER Gábor6940a062018-07-12 14:22:15 +0200350
351strip_access_log() {
352 sed -e "
353 s/^.* \"//
354 s/\"//
355 s/ [1-9][0-9]*\$//
356 s/^GET /GET /
357 " "$HTTPD_ROOT_PATH"/access.log
358}
SZEDER Gábore8b3b2e2018-07-12 14:22:16 +0200359
360# Requires one argument: the name of a file containing the expected stripped
361# access log entries.
362check_access_log() {
363 sort "$1" >"$1".sorted &&
364 strip_access_log >access.log.stripped &&
365 sort access.log.stripped >access.log.sorted &&
366 if ! test_cmp "$1".sorted access.log.sorted
367 then
368 test_cmp "$1" access.log.stripped
369 fi
370}