]> git.ipfire.org Git - thirdparty/git.git/blob - t/lib-httpd.sh
Merge branch 'sh/completion-with-reftable' into maint-2.43
[thirdparty/git.git] / t / lib-httpd.sh
1 # 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 #
17 # 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
26 # LIB_HTTPD_SVN enable SVN at given location (e.g. "svn")
27 # LIB_HTTPD_SSL enable SSL
28 # LIB_HTTPD_PROXY enable proxy
29 #
30 # Copyright (c) 2008 Clemens Buchacher <drizzd@aon.at>
31 #
32
33 if ! test_have_prereq LIBCURL
34 then
35 skip_all='skipping test, git built without http support'
36 test_done
37 fi
38
39 if test -n "$NO_EXPAT" && test -n "$LIB_HTTPD_DAV"
40 then
41 skip_all='skipping test, git built without expat support'
42 test_done
43 fi
44
45 if ! test_bool_env GIT_TEST_HTTPD true
46 then
47 skip_all="Network testing disabled (unset GIT_TEST_HTTPD to enable)"
48 test_done
49 fi
50
51 if ! test_have_prereq NOT_ROOT; then
52 test_skip_or_die GIT_TEST_HTTPD \
53 "Cannot run httpd tests as root"
54 fi
55
56 HTTPD_PARA=""
57
58 for DEFAULT_HTTPD_PATH in '/usr/sbin/httpd' '/usr/sbin/apache2'
59 do
60 if test -x "$DEFAULT_HTTPD_PATH"
61 then
62 break
63 fi
64 done
65
66 for DEFAULT_HTTPD_MODULE_PATH in '/usr/libexec/apache2' \
67 '/usr/lib/apache2/modules' \
68 '/usr/lib64/httpd/modules' \
69 '/usr/lib/httpd/modules' \
70 '/usr/libexec/httpd' \
71 '/usr/lib/apache2'
72 do
73 if test -d "$DEFAULT_HTTPD_MODULE_PATH"
74 then
75 break
76 fi
77 done
78
79 case $(uname) in
80 Darwin)
81 HTTPD_PARA="$HTTPD_PARA -DDarwin"
82 ;;
83 esac
84
85 LIB_HTTPD_PATH=${LIB_HTTPD_PATH-"$DEFAULT_HTTPD_PATH"}
86 test_set_port LIB_HTTPD_PORT
87
88 TEST_PATH="$TEST_DIRECTORY"/lib-httpd
89 HTTPD_ROOT_PATH="$PWD"/httpd
90 HTTPD_DOCUMENT_ROOT_PATH=$HTTPD_ROOT_PATH/www
91
92 # hack to suppress apache PassEnv warnings
93 GIT_VALGRIND=$GIT_VALGRIND; export GIT_VALGRIND
94 GIT_VALGRIND_OPTIONS=$GIT_VALGRIND_OPTIONS; export GIT_VALGRIND_OPTIONS
95 GIT_TEST_SIDEBAND_ALL=$GIT_TEST_SIDEBAND_ALL; export GIT_TEST_SIDEBAND_ALL
96 GIT_TRACE=$GIT_TRACE; export GIT_TRACE
97
98 if ! test -x "$LIB_HTTPD_PATH"
99 then
100 test_skip_or_die GIT_TEST_HTTPD "no web server found at '$LIB_HTTPD_PATH'"
101 fi
102
103 HTTPD_VERSION=$($LIB_HTTPD_PATH -v | \
104 sed -n 's/^Server version: Apache\/\([0-9.]*\).*$/\1/p; q')
105 HTTPD_VERSION_MAJOR=$(echo $HTTPD_VERSION | cut -d. -f1)
106 HTTPD_VERSION_MINOR=$(echo $HTTPD_VERSION | cut -d. -f2)
107
108 if test -n "$HTTPD_VERSION_MAJOR"
109 then
110 if test -z "$LIB_HTTPD_MODULE_PATH"
111 then
112 if ! test "$HTTPD_VERSION_MAJOR" -eq 2 ||
113 ! test "$HTTPD_VERSION_MINOR" -ge 4
114 then
115 test_skip_or_die GIT_TEST_HTTPD \
116 "at least Apache version 2.4 is required"
117 fi
118 if ! test -d "$DEFAULT_HTTPD_MODULE_PATH"
119 then
120 test_skip_or_die GIT_TEST_HTTPD \
121 "Apache module directory not found"
122 fi
123
124 LIB_HTTPD_MODULE_PATH="$DEFAULT_HTTPD_MODULE_PATH"
125 fi
126 else
127 test_skip_or_die GIT_TEST_HTTPD \
128 "Could not identify web server at '$LIB_HTTPD_PATH'"
129 fi
130
131 if test -n "$LIB_HTTPD_DAV" && test -f /etc/os-release
132 then
133 case "$(grep "^ID=" /etc/os-release | cut -d= -f2-)" in
134 alpine)
135 # The WebDAV module in Alpine Linux is broken at least up to
136 # Alpine v3.16 as the default DBM driver is missing.
137 #
138 # https://gitlab.alpinelinux.org/alpine/aports/-/issues/13112
139 test_skip_or_die GIT_TEST_HTTPD \
140 "Apache WebDAV module does not have default DBM backend driver"
141 ;;
142 esac
143 fi
144
145 install_script () {
146 write_script "$HTTPD_ROOT_PATH/$1" <"$TEST_PATH/$1"
147 }
148
149 prepare_httpd() {
150 mkdir -p "$HTTPD_DOCUMENT_ROOT_PATH"
151 cp "$TEST_PATH"/passwd "$HTTPD_ROOT_PATH"
152 cp "$TEST_PATH"/proxy-passwd "$HTTPD_ROOT_PATH"
153 install_script incomplete-length-upload-pack-v2-http.sh
154 install_script incomplete-body-upload-pack-v2-http.sh
155 install_script error-no-report.sh
156 install_script broken-smart-http.sh
157 install_script error-smart-http.sh
158 install_script error.sh
159 install_script apply-one-time-perl.sh
160 install_script nph-custom-auth.sh
161
162 ln -s "$LIB_HTTPD_MODULE_PATH" "$HTTPD_ROOT_PATH/modules"
163
164 if test -n "$LIB_HTTPD_SSL"
165 then
166 HTTPD_PROTO=https
167
168 RANDFILE_PATH="$HTTPD_ROOT_PATH"/.rnd openssl req \
169 -config "$TEST_PATH/ssl.cnf" \
170 -new -x509 -nodes \
171 -out "$HTTPD_ROOT_PATH/httpd.pem" \
172 -keyout "$HTTPD_ROOT_PATH/httpd.pem"
173 GIT_SSL_NO_VERIFY=t
174 export GIT_SSL_NO_VERIFY
175 HTTPD_PARA="$HTTPD_PARA -DSSL"
176 else
177 HTTPD_PROTO=http
178 fi
179 HTTPD_DEST=127.0.0.1:$LIB_HTTPD_PORT
180 HTTPD_URL=$HTTPD_PROTO://$HTTPD_DEST
181 HTTPD_URL_USER=$HTTPD_PROTO://user%40host@$HTTPD_DEST
182 HTTPD_URL_USER_PASS=$HTTPD_PROTO://user%40host:pass%40host@$HTTPD_DEST
183
184 if test -n "$LIB_HTTPD_DAV" || test -n "$LIB_HTTPD_SVN"
185 then
186 HTTPD_PARA="$HTTPD_PARA -DDAV"
187
188 if test -n "$LIB_HTTPD_SVN"
189 then
190 HTTPD_PARA="$HTTPD_PARA -DSVN"
191 LIB_HTTPD_SVNPATH="$rawsvnrepo"
192 svnrepo="http://127.0.0.1:$LIB_HTTPD_PORT/"
193 svnrepo="$svnrepo$LIB_HTTPD_SVN"
194 export LIB_HTTPD_SVN LIB_HTTPD_SVNPATH
195 fi
196 fi
197
198 if test -n "$LIB_HTTPD_PROXY"
199 then
200 HTTPD_PARA="$HTTPD_PARA -DPROXY"
201 fi
202 }
203
204 enable_http2 () {
205 HTTPD_PARA="$HTTPD_PARA -DHTTP2"
206 test_set_prereq HTTP2
207 }
208
209 enable_cgipassauth () {
210 # We are looking for 2.4.13 or more recent. Since we only support
211 # 2.4 and up, no need to check for older major/minor.
212 if test "$HTTPD_VERSION_MAJOR" = 2 &&
213 test "$HTTPD_VERSION_MINOR" = 4 &&
214 test "$(echo $HTTPD_VERSION | cut -d. -f3)" -lt 13
215 then
216 echo >&4 "apache $HTTPD_VERSION too old for CGIPassAuth"
217 return
218 fi
219 HTTPD_PARA="$HTTPD_PARA -DUSE_CGIPASSAUTH"
220 test_set_prereq CGIPASSAUTH
221 }
222
223 start_httpd() {
224 prepare_httpd >&3 2>&4
225
226 test_atexit stop_httpd
227
228 "$LIB_HTTPD_PATH" -d "$HTTPD_ROOT_PATH" \
229 -f "$TEST_PATH/apache.conf" $HTTPD_PARA \
230 -c "Listen 127.0.0.1:$LIB_HTTPD_PORT" -k start \
231 >&3 2>&4
232 if test $? -ne 0
233 then
234 cat "$HTTPD_ROOT_PATH"/error.log >&4 2>/dev/null
235 test_skip_or_die GIT_TEST_HTTPD "web server setup failed"
236 fi
237 }
238
239 stop_httpd() {
240 "$LIB_HTTPD_PATH" -d "$HTTPD_ROOT_PATH" \
241 -f "$TEST_PATH/apache.conf" $HTTPD_PARA -k stop
242 }
243
244 test_http_push_nonff () {
245 REMOTE_REPO=$1
246 LOCAL_REPO=$2
247 BRANCH=$3
248 EXPECT_CAS_RESULT=${4-failure}
249
250 test_expect_success 'non-fast-forward push fails' '
251 cd "$REMOTE_REPO" &&
252 HEAD=$(git rev-parse --verify HEAD) &&
253
254 cd "$LOCAL_REPO" &&
255 git checkout $BRANCH &&
256 echo "changed" > path2 &&
257 git commit -a -m path2 --amend &&
258
259 test_must_fail git push -v origin >output 2>&1 &&
260 (
261 cd "$REMOTE_REPO" &&
262 echo "$HEAD" >expect &&
263 git rev-parse --verify HEAD >actual &&
264 test_cmp expect actual
265 )
266 '
267
268 test_expect_success 'non-fast-forward push show ref status' '
269 grep "^ ! \[rejected\][ ]*$BRANCH -> $BRANCH (non-fast-forward)$" output
270 '
271
272 test_expect_success 'non-fast-forward push shows help message' '
273 test_grep "Updates were rejected because" output
274 '
275
276 test_expect_${EXPECT_CAS_RESULT} 'force with lease aka cas' '
277 HEAD=$( cd "$REMOTE_REPO" && git rev-parse --verify HEAD ) &&
278 test_when_finished '\''
279 (cd "$REMOTE_REPO" && git update-ref HEAD "$HEAD")
280 '\'' &&
281 (
282 cd "$LOCAL_REPO" &&
283 git push -v --force-with-lease=$BRANCH:$HEAD origin
284 ) &&
285 git rev-parse --verify "$BRANCH" >expect &&
286 (
287 cd "$REMOTE_REPO" && git rev-parse --verify HEAD
288 ) >actual &&
289 test_cmp expect actual
290 '
291 }
292
293 setup_askpass_helper() {
294 test_expect_success 'setup askpass helper' '
295 write_script "$TRASH_DIRECTORY/askpass" <<-\EOF &&
296 echo >>"$TRASH_DIRECTORY/askpass-query" "askpass: $*" &&
297 case "$*" in
298 *Username*)
299 what=user
300 ;;
301 *Password*)
302 what=pass
303 ;;
304 esac &&
305 cat "$TRASH_DIRECTORY/askpass-$what"
306 EOF
307 GIT_ASKPASS="$TRASH_DIRECTORY/askpass" &&
308 export GIT_ASKPASS &&
309 export TRASH_DIRECTORY
310 '
311 }
312
313 set_askpass() {
314 >"$TRASH_DIRECTORY/askpass-query" &&
315 echo "$1" >"$TRASH_DIRECTORY/askpass-user" &&
316 echo "$2" >"$TRASH_DIRECTORY/askpass-pass"
317 }
318
319 expect_askpass() {
320 dest=$HTTPD_DEST${3+/$3}
321
322 {
323 case "$1" in
324 none)
325 ;;
326 pass)
327 echo "askpass: Password for '$HTTPD_PROTO://$2@$dest': "
328 ;;
329 both)
330 echo "askpass: Username for '$HTTPD_PROTO://$dest': "
331 echo "askpass: Password for '$HTTPD_PROTO://$2@$dest': "
332 ;;
333 *)
334 false
335 ;;
336 esac
337 } >"$TRASH_DIRECTORY/askpass-expect" &&
338 test_cmp "$TRASH_DIRECTORY/askpass-expect" \
339 "$TRASH_DIRECTORY/askpass-query"
340 }
341
342 strip_access_log() {
343 sed -e "
344 s/^.* \"//
345 s/\"//
346 s/ [1-9][0-9]*\$//
347 s/^GET /GET /
348 " "$HTTPD_ROOT_PATH"/access.log
349 }
350
351 # Requires one argument: the name of a file containing the expected stripped
352 # access log entries.
353 check_access_log() {
354 sort "$1" >"$1".sorted &&
355 strip_access_log >access.log.stripped &&
356 sort access.log.stripped >access.log.sorted &&
357 if ! test_cmp "$1".sorted access.log.sorted
358 then
359 test_cmp "$1" access.log.stripped
360 fi
361 }