Merge branch 'jc/transport-do-not-use-connect-twice-in-fetch'
[git] / t / lib-httpd.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2008 Clemens Buchacher <drizzd@aon.at>
4 #
5
6 if test -z "$GIT_TEST_HTTPD"
7 then
8         skip_all="Network testing disabled (define GIT_TEST_HTTPD to enable)"
9         test_done
10 fi
11
12 HTTPD_PARA=""
13
14 for DEFAULT_HTTPD_PATH in '/usr/sbin/httpd' '/usr/sbin/apache2'
15 do
16         if test -x "$DEFAULT_HTTPD_PATH"
17         then
18                 break
19         fi
20 done
21
22 for DEFAULT_HTTPD_MODULE_PATH in '/usr/libexec/apache2' \
23                                  '/usr/lib/apache2/modules' \
24                                  '/usr/lib64/httpd/modules' \
25                                  '/usr/lib/httpd/modules'
26 do
27         if test -d "$DEFAULT_HTTPD_MODULE_PATH"
28         then
29                 break
30         fi
31 done
32
33 case $(uname) in
34         Darwin)
35                 HTTPD_PARA="$HTTPD_PARA -DDarwin"
36         ;;
37 esac
38
39 LIB_HTTPD_PATH=${LIB_HTTPD_PATH-"$DEFAULT_HTTPD_PATH"}
40 LIB_HTTPD_PORT=${LIB_HTTPD_PORT-'8111'}
41
42 TEST_PATH="$TEST_DIRECTORY"/lib-httpd
43 HTTPD_ROOT_PATH="$PWD"/httpd
44 HTTPD_DOCUMENT_ROOT_PATH=$HTTPD_ROOT_PATH/www
45
46 # hack to suppress apache PassEnv warnings
47 GIT_VALGRIND=$GIT_VALGRIND; export GIT_VALGRIND
48 GIT_VALGRIND_OPTIONS=$GIT_VALGRIND_OPTIONS; export GIT_VALGRIND_OPTIONS
49
50 if ! test -x "$LIB_HTTPD_PATH"
51 then
52         skip_all="skipping test, no web server found at '$LIB_HTTPD_PATH'"
53         test_done
54 fi
55
56 HTTPD_VERSION=`$LIB_HTTPD_PATH -v | \
57         sed -n 's/^Server version: Apache\/\([0-9]*\)\..*$/\1/p; q'`
58
59 if test -n "$HTTPD_VERSION"
60 then
61         if test -z "$LIB_HTTPD_MODULE_PATH"
62         then
63                 if ! test $HTTPD_VERSION -ge 2
64                 then
65                         skip_all="skipping test, at least Apache version 2 is required"
66                         test_done
67                 fi
68                 if ! test -d "$DEFAULT_HTTPD_MODULE_PATH"
69                 then
70                         skip_all="Apache module directory not found.  Skipping tests."
71                         test_done
72                 fi
73
74                 LIB_HTTPD_MODULE_PATH="$DEFAULT_HTTPD_MODULE_PATH"
75         fi
76 else
77         error "Could not identify web server at '$LIB_HTTPD_PATH'"
78 fi
79
80 prepare_httpd() {
81         mkdir -p "$HTTPD_DOCUMENT_ROOT_PATH"
82         cp "$TEST_PATH"/passwd "$HTTPD_ROOT_PATH"
83         cp "$TEST_PATH"/broken-smart-http.sh "$HTTPD_ROOT_PATH"
84
85         ln -s "$LIB_HTTPD_MODULE_PATH" "$HTTPD_ROOT_PATH/modules"
86
87         if test -n "$LIB_HTTPD_SSL"
88         then
89                 HTTPD_PROTO=https
90
91                 RANDFILE_PATH="$HTTPD_ROOT_PATH"/.rnd openssl req \
92                         -config "$TEST_PATH/ssl.cnf" \
93                         -new -x509 -nodes \
94                         -out "$HTTPD_ROOT_PATH/httpd.pem" \
95                         -keyout "$HTTPD_ROOT_PATH/httpd.pem"
96                 GIT_SSL_NO_VERIFY=t
97                 export GIT_SSL_NO_VERIFY
98                 HTTPD_PARA="$HTTPD_PARA -DSSL"
99         else
100                 HTTPD_PROTO=http
101         fi
102         HTTPD_DEST=127.0.0.1:$LIB_HTTPD_PORT
103         HTTPD_URL=$HTTPD_PROTO://$HTTPD_DEST
104         HTTPD_URL_USER=$HTTPD_PROTO://user%40host@$HTTPD_DEST
105         HTTPD_URL_USER_PASS=$HTTPD_PROTO://user%40host:user%40host@$HTTPD_DEST
106
107         if test -n "$LIB_HTTPD_DAV" -o -n "$LIB_HTTPD_SVN"
108         then
109                 HTTPD_PARA="$HTTPD_PARA -DDAV"
110
111                 if test -n "$LIB_HTTPD_SVN"
112                 then
113                         HTTPD_PARA="$HTTPD_PARA -DSVN"
114                         rawsvnrepo="$HTTPD_ROOT_PATH/svnrepo"
115                         svnrepo="http://127.0.0.1:$LIB_HTTPD_PORT/svn"
116                 fi
117         fi
118 }
119
120 start_httpd() {
121         prepare_httpd >&3 2>&4
122
123         trap 'code=$?; stop_httpd; (exit $code); die' EXIT
124
125         "$LIB_HTTPD_PATH" -d "$HTTPD_ROOT_PATH" \
126                 -f "$TEST_PATH/apache.conf" $HTTPD_PARA \
127                 -c "Listen 127.0.0.1:$LIB_HTTPD_PORT" -k start \
128                 >&3 2>&4
129         if test $? -ne 0
130         then
131                 skip_all="skipping test, web server setup failed"
132                 trap 'die' EXIT
133                 test_done
134         fi
135 }
136
137 stop_httpd() {
138         trap 'die' EXIT
139
140         "$LIB_HTTPD_PATH" -d "$HTTPD_ROOT_PATH" \
141                 -f "$TEST_PATH/apache.conf" $HTTPD_PARA -k stop
142 }
143
144 test_http_push_nonff () {
145         REMOTE_REPO=$1
146         LOCAL_REPO=$2
147         BRANCH=$3
148         EXPECT_CAS_RESULT=${4-failure}
149
150         test_expect_success 'non-fast-forward push fails' '
151                 cd "$REMOTE_REPO" &&
152                 HEAD=$(git rev-parse --verify HEAD) &&
153
154                 cd "$LOCAL_REPO" &&
155                 git checkout $BRANCH &&
156                 echo "changed" > path2 &&
157                 git commit -a -m path2 --amend &&
158
159                 test_must_fail git push -v origin >output 2>&1 &&
160                 (cd "$REMOTE_REPO" &&
161                  test $HEAD = $(git rev-parse --verify HEAD))
162         '
163
164         test_expect_success 'non-fast-forward push show ref status' '
165                 grep "^ ! \[rejected\][ ]*$BRANCH -> $BRANCH (non-fast-forward)$" output
166         '
167
168         test_expect_success 'non-fast-forward push shows help message' '
169                 test_i18ngrep "Updates were rejected because" output
170         '
171
172         test_expect_failure 'force with lease aka cas' '
173                 HEAD=$( cd "$REMOTE_REPO" && git rev-parse --verify HEAD ) &&
174                 test_when_finished '\''
175                         (cd "$REMOTE_REPO" && git update-ref HEAD "$HEAD")
176                 '\'' &&
177                 (
178                         cd "$LOCAL_REPO" &&
179                         git push -v --force-with-lease=$BRANCH:$HEAD origin
180                 ) &&
181                 git rev-parse --verify "$BRANCH" >expect &&
182                 (
183                         cd "$REMOTE_REPO" && git rev-parse --verify HEAD
184                 ) >actual &&
185                 test_cmp expect actual
186         '
187 }
188
189 setup_askpass_helper() {
190         test_expect_success 'setup askpass helper' '
191                 write_script "$TRASH_DIRECTORY/askpass" <<-\EOF &&
192                 echo >>"$TRASH_DIRECTORY/askpass-query" "askpass: $*" &&
193                 cat "$TRASH_DIRECTORY/askpass-response"
194                 EOF
195                 GIT_ASKPASS="$TRASH_DIRECTORY/askpass" &&
196                 export GIT_ASKPASS &&
197                 export TRASH_DIRECTORY
198         '
199 }
200
201 set_askpass() {
202         >"$TRASH_DIRECTORY/askpass-query" &&
203         echo "$*" >"$TRASH_DIRECTORY/askpass-response"
204 }
205
206 expect_askpass() {
207         dest=$HTTPD_DEST
208         {
209                 case "$1" in
210                 none)
211                         ;;
212                 pass)
213                         echo "askpass: Password for 'http://$2@$dest': "
214                         ;;
215                 both)
216                         echo "askpass: Username for 'http://$dest': "
217                         echo "askpass: Password for 'http://$2@$dest': "
218                         ;;
219                 *)
220                         false
221                         ;;
222                 esac
223         } >"$TRASH_DIRECTORY/askpass-expect" &&
224         test_cmp "$TRASH_DIRECTORY/askpass-expect" \
225                  "$TRASH_DIRECTORY/askpass-query"
226 }