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