1#!/bin/sh
2
3test_description='test smart fetching over http via http-backend'
4. ./test-lib.sh
5. "$TEST_DIRECTORY"/lib-httpd.sh
6start_httpd
7
8test_expect_success 'setup repository' '
9 git config push.default matching &&
10 echo content >file &&
11 git add file &&
12 git commit -m one
13'
14
15test_expect_success 'create http-accessible bare repository' '
16 mkdir "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
17 (cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
18 git --bare init
19 ) &&
20 git remote add public "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
21 git push public master:master
22'
23
24setup_askpass_helper
25
26test_expect_success 'clone http repository' '
27 cat >exp <<-\EOF &&
28 > GET /smart/repo.git/info/refs?service=git-upload-pack HTTP/1.1
29 > Accept: */*
30 > Accept-Encoding: ENCODINGS
31 > Pragma: no-cache
32 < HTTP/1.1 200 OK
33 < Pragma: no-cache
34 < Cache-Control: no-cache, max-age=0, must-revalidate
35 < Content-Type: application/x-git-upload-pack-advertisement
36 > POST /smart/repo.git/git-upload-pack HTTP/1.1
37 > Accept-Encoding: ENCODINGS
38 > Content-Type: application/x-git-upload-pack-request
39 > Accept: application/x-git-upload-pack-result
40 > Content-Length: xxx
41 < HTTP/1.1 200 OK
42 < Pragma: no-cache
43 < Cache-Control: no-cache, max-age=0, must-revalidate
44 < Content-Type: application/x-git-upload-pack-result
45 EOF
46 GIT_TRACE_CURL=true git clone --quiet $HTTPD_URL/smart/repo.git clone 2>err &&
47 test_cmp file clone/file &&
48 tr '\''\015'\'' Q <err |
49 sed -e "
50 s/Q\$//
51 /^[*] /d
52 /^== Info:/d
53 /^=> Send header, /d
54 /^=> Send header:$/d
55 /^<= Recv header, /d
56 /^<= Recv header:$/d
57 s/=> Send header: //
58 s/= Recv header://
59 /^<= Recv data/d
60 /^=> Send data/d
61 /^$/d
62 /^< $/d
63
64 /^[^><]/{
65 s/^/> /
66 }
67
68 /^> User-Agent: /d
69 /^> Host: /d
70 /^> POST /,$ {
71 /^> Accept: [*]\\/[*]/d
72 }
73 s/^> Content-Length: .*/> Content-Length: xxx/
74 /^> 00..want /d
75 /^> 00.*done/d
76
77 /^< Server: /d
78 /^< Expires: /d
79 /^< Date: /d
80 /^< Content-Length: /d
81 /^< Transfer-Encoding: /d
82 " >actual &&
83 sed -e "s/^> Accept-Encoding: .*/> Accept-Encoding: ENCODINGS/" \
84 actual >actual.smudged &&
85 test_cmp exp actual.smudged &&
86
87 grep "Accept-Encoding:.*gzip" actual >actual.gzip &&
88 test_line_count = 2 actual.gzip
89'
90
91test_expect_success 'fetch changes via http' '
92 echo content >>file &&
93 git commit -a -m two &&
94 git push public &&
95 (cd clone && git pull) &&
96 test_cmp file clone/file
97'
98
99test_expect_success 'used upload-pack service' '
100 cat >exp <<-\EOF &&
101 GET /smart/repo.git/info/refs?service=git-upload-pack HTTP/1.1 200
102 POST /smart/repo.git/git-upload-pack HTTP/1.1 200
103 GET /smart/repo.git/info/refs?service=git-upload-pack HTTP/1.1 200
104 POST /smart/repo.git/git-upload-pack HTTP/1.1 200
105 EOF
106 check_access_log exp
107'
108
109test_expect_success 'follow redirects (301)' '
110 git clone $HTTPD_URL/smart-redir-perm/repo.git --quiet repo-p
111'
112
113test_expect_success 'follow redirects (302)' '
114 git clone $HTTPD_URL/smart-redir-temp/repo.git --quiet repo-t
115'
116
117test_expect_success 'redirects re-root further requests' '
118 git clone $HTTPD_URL/smart-redir-limited/repo.git repo-redir-limited
119'
120
121test_expect_success 're-rooting dies on insane schemes' '
122 test_must_fail git clone $HTTPD_URL/insane-redir/repo.git insane
123'
124
125test_expect_success 'clone from password-protected repository' '
126 echo two >expect &&
127 set_askpass user@host pass@host &&
128 git clone --bare "$HTTPD_URL/auth/smart/repo.git" smart-auth &&
129 expect_askpass both user@host &&
130 git --git-dir=smart-auth log -1 --format=%s >actual &&
131 test_cmp expect actual
132'
133
134test_expect_success 'clone from auth-only-for-push repository' '
135 echo two >expect &&
136 set_askpass wrong &&
137 git clone --bare "$HTTPD_URL/auth-push/smart/repo.git" smart-noauth &&
138 expect_askpass none &&
139 git --git-dir=smart-noauth log -1 --format=%s >actual &&
140 test_cmp expect actual
141'
142
143test_expect_success 'clone from auth-only-for-objects repository' '
144 echo two >expect &&
145 set_askpass user@host pass@host &&
146 git clone --bare "$HTTPD_URL/auth-fetch/smart/repo.git" half-auth &&
147 expect_askpass both user@host &&
148 git --git-dir=half-auth log -1 --format=%s >actual &&
149 test_cmp expect actual
150'
151
152test_expect_success 'no-op half-auth fetch does not require a password' '
153 set_askpass wrong &&
154 git --git-dir=half-auth fetch &&
155 expect_askpass none
156'
157
158test_expect_success 'redirects send auth to new location' '
159 set_askpass user@host pass@host &&
160 git -c credential.useHttpPath=true \
161 clone $HTTPD_URL/smart-redir-auth/repo.git repo-redir-auth &&
162 expect_askpass both user@host auth/smart/repo.git
163'
164
165test_expect_success 'disable dumb http on server' '
166 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
167 config http.getanyfile false
168'
169
170test_expect_success 'GIT_SMART_HTTP can disable smart http' '
171 (GIT_SMART_HTTP=0 &&
172 export GIT_SMART_HTTP &&
173 cd clone &&
174 test_must_fail git fetch)
175'
176
177test_expect_success 'invalid Content-Type rejected' '
178 test_must_fail git clone $HTTPD_URL/broken_smart/repo.git 2>actual &&
179 grep "not valid:" actual
180'
181
182test_expect_success 'create namespaced refs' '
183 test_commit namespaced &&
184 git push public HEAD:refs/namespaces/ns/refs/heads/master &&
185 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
186 symbolic-ref refs/namespaces/ns/HEAD refs/namespaces/ns/refs/heads/master
187'
188
189test_expect_success 'smart clone respects namespace' '
190 git clone "$HTTPD_URL/smart_namespace/repo.git" ns-smart &&
191 echo namespaced >expect &&
192 git --git-dir=ns-smart/.git log -1 --format=%s >actual &&
193 test_cmp expect actual
194'
195
196test_expect_success 'dumb clone via http-backend respects namespace' '
197 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
198 config http.getanyfile true &&
199 GIT_SMART_HTTP=0 git clone \
200 "$HTTPD_URL/smart_namespace/repo.git" ns-dumb &&
201 echo namespaced >expect &&
202 git --git-dir=ns-dumb/.git log -1 --format=%s >actual &&
203 test_cmp expect actual
204'
205
206test_expect_success 'cookies stored in http.cookiefile when http.savecookies set' '
207 cat >cookies.txt <<-\EOF &&
208 127.0.0.1 FALSE /smart_cookies/ FALSE 0 othername othervalue
209 EOF
210 sort >expect_cookies.txt <<-\EOF &&
211
212 127.0.0.1 FALSE /smart_cookies/ FALSE 0 othername othervalue
213 127.0.0.1 FALSE /smart_cookies/repo.git/info/ FALSE 0 name value
214 EOF
215 git config http.cookiefile cookies.txt &&
216 git config http.savecookies true &&
217 git ls-remote $HTTPD_URL/smart_cookies/repo.git master &&
218 tail -3 cookies.txt | sort >cookies_tail.txt &&
219 test_cmp expect_cookies.txt cookies_tail.txt
220'
221
222test_expect_success 'transfer.hiderefs works over smart-http' '
223 test_commit hidden &&
224 test_commit visible &&
225 git push public HEAD^:refs/heads/a HEAD:refs/heads/b &&
226 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
227 config transfer.hiderefs refs/heads/a &&
228 git clone --bare "$HTTPD_URL/smart/repo.git" hidden.git &&
229 test_must_fail git -C hidden.git rev-parse --verify a &&
230 git -C hidden.git rev-parse --verify b
231'
232
233# create an arbitrary number of tags, numbered from tag-$1 to tag-$2
234create_tags () {
235 rm -f marks &&
236 for i in $(test_seq "$1" "$2")
237 do
238 # don't use here-doc, because it requires a process
239 # per loop iteration
240 echo "commit refs/heads/too-many-refs-$1" &&
241 echo "mark :$i" &&
242 echo "committer git <git@example.com> $i +0000" &&
243 echo "data 0" &&
244 echo "M 644 inline bla.txt" &&
245 echo "data 4" &&
246 echo "bla" &&
247 # make every commit dangling by always
248 # rewinding the branch after each commit
249 echo "reset refs/heads/too-many-refs-$1" &&
250 echo "from :$1"
251 done | git fast-import --export-marks=marks &&
252
253 # now assign tags to all the dangling commits we created above
254 tag=$(perl -e "print \"bla\" x 30") &&
255 sed -e "s|^:\([^ ]*\) \(.*\)$|\2 refs/tags/$tag-\1|" <marks >>packed-refs
256}
257
258test_expect_success 'create 2,000 tags in the repo' '
259 (
260 cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
261 create_tags 1 2000
262 )
263'
264
265test_expect_success CMDLINE_LIMIT \
266 'clone the 2,000 tag repo to check OS command line overflow' '
267 run_with_limited_cmdline git clone $HTTPD_URL/smart/repo.git too-many-refs &&
268 (
269 cd too-many-refs &&
270 git for-each-ref refs/tags >actual &&
271 test_line_count = 2000 actual
272 )
273'
274
275test_expect_success 'large fetch-pack requests can be split across POSTs' '
276 GIT_TRACE_CURL=true git -c http.postbuffer=65536 \
277 clone --bare "$HTTPD_URL/smart/repo.git" split.git 2>err &&
278 grep "^=> Send header: POST" err >posts &&
279 test_line_count = 2 posts
280'
281
282test_expect_success 'test allowreachablesha1inwant' '
283 test_when_finished "rm -rf test_reachable.git" &&
284 server="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
285 master_sha=$(git -C "$server" rev-parse refs/heads/master) &&
286 git -C "$server" config uploadpack.allowreachablesha1inwant 1 &&
287
288 git init --bare test_reachable.git &&
289 git -C test_reachable.git remote add origin "$HTTPD_URL/smart/repo.git" &&
290 git -C test_reachable.git fetch origin "$master_sha"
291'
292
293test_expect_success 'test allowreachablesha1inwant with unreachable' '
294 test_when_finished "rm -rf test_reachable.git; git reset --hard $(git rev-parse HEAD)" &&
295
296 #create unreachable sha
297 echo content >file2 &&
298 git add file2 &&
299 git commit -m two &&
300 git push public HEAD:refs/heads/doomed &&
301 git push public :refs/heads/doomed &&
302
303 server="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
304 master_sha=$(git -C "$server" rev-parse refs/heads/master) &&
305 git -C "$server" config uploadpack.allowreachablesha1inwant 1 &&
306
307 git init --bare test_reachable.git &&
308 git -C test_reachable.git remote add origin "$HTTPD_URL/smart/repo.git" &&
309 test_must_fail git -C test_reachable.git fetch origin "$(git rev-parse HEAD)"
310'
311
312test_expect_success 'test allowanysha1inwant with unreachable' '
313 test_when_finished "rm -rf test_reachable.git; git reset --hard $(git rev-parse HEAD)" &&
314
315 #create unreachable sha
316 echo content >file2 &&
317 git add file2 &&
318 git commit -m two &&
319 git push public HEAD:refs/heads/doomed &&
320 git push public :refs/heads/doomed &&
321
322 server="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
323 master_sha=$(git -C "$server" rev-parse refs/heads/master) &&
324 git -C "$server" config uploadpack.allowreachablesha1inwant 1 &&
325
326 git init --bare test_reachable.git &&
327 git -C test_reachable.git remote add origin "$HTTPD_URL/smart/repo.git" &&
328 test_must_fail git -C test_reachable.git fetch origin "$(git rev-parse HEAD)" &&
329
330 git -C "$server" config uploadpack.allowanysha1inwant 1 &&
331 git -C test_reachable.git fetch origin "$(git rev-parse HEAD)"
332'
333
334test_expect_success EXPENSIVE 'http can handle enormous ref negotiation' '
335 (
336 cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
337 create_tags 2001 50000
338 ) &&
339 git -C too-many-refs fetch -q --tags &&
340 (
341 cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
342 create_tags 50001 100000
343 ) &&
344 git -C too-many-refs fetch -q --tags &&
345 git -C too-many-refs for-each-ref refs/tags >tags &&
346 test_line_count = 100000 tags
347'
348
349test_expect_success 'custom http headers' '
350 test_must_fail git -c http.extraheader="x-magic-two: cadabra" \
351 fetch "$HTTPD_URL/smart_headers/repo.git" &&
352 git -c http.extraheader="x-magic-one: abra" \
353 -c http.extraheader="x-magic-two: cadabra" \
354 fetch "$HTTPD_URL/smart_headers/repo.git" &&
355 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
356 git config -f .gitmodules submodule.sub.path sub &&
357 git config -f .gitmodules submodule.sub.url \
358 "$HTTPD_URL/smart_headers/repo.git" &&
359 git submodule init sub &&
360 test_must_fail git submodule update sub &&
361 git -c http.extraheader="x-magic-one: abra" \
362 -c http.extraheader="x-magic-two: cadabra" \
363 submodule update sub
364'
365
366test_expect_success 'using fetch command in remote-curl updates refs' '
367 SERVER="$HTTPD_DOCUMENT_ROOT_PATH/twobranch" &&
368 rm -rf "$SERVER" client &&
369
370 git init "$SERVER" &&
371 test_commit -C "$SERVER" foo &&
372 git -C "$SERVER" update-ref refs/heads/anotherbranch foo &&
373
374 git clone $HTTPD_URL/smart/twobranch client &&
375
376 test_commit -C "$SERVER" bar &&
377 git -C client -c protocol.version=0 fetch &&
378
379 git -C "$SERVER" rev-parse master >expect &&
380 git -C client rev-parse origin/master >actual &&
381 test_cmp expect actual
382'
383
384test_expect_success 'fetch by SHA-1 without tag following' '
385 SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
386 rm -rf "$SERVER" client &&
387
388 git init "$SERVER" &&
389 test_commit -C "$SERVER" foo &&
390
391 git clone $HTTPD_URL/smart/server client &&
392
393 test_commit -C "$SERVER" bar &&
394 git -C "$SERVER" rev-parse bar >bar_hash &&
395 git -C client -c protocol.version=0 fetch \
396 --no-tags origin $(cat bar_hash)
397'
398
399test_expect_success 'GIT_REDACT_COOKIES redacts cookies' '
400 rm -rf clone &&
401 echo "Set-Cookie: Foo=1" >cookies &&
402 echo "Set-Cookie: Bar=2" >>cookies &&
403 GIT_TRACE_CURL=true GIT_REDACT_COOKIES=Bar,Baz \
404 git -c "http.cookieFile=$(pwd)/cookies" clone \
405 $HTTPD_URL/smart/repo.git clone 2>err &&
406 grep "Cookie:.*Foo=1" err &&
407 grep "Cookie:.*Bar=<redacted>" err &&
408 ! grep "Cookie:.*Bar=2" err
409'
410
411test_expect_success 'GIT_REDACT_COOKIES handles empty values' '
412 rm -rf clone &&
413 echo "Set-Cookie: Foo=" >cookies &&
414 GIT_TRACE_CURL=true GIT_REDACT_COOKIES=Foo \
415 git -c "http.cookieFile=$(pwd)/cookies" clone \
416 $HTTPD_URL/smart/repo.git clone 2>err &&
417 grep "Cookie:.*Foo=<redacted>" err
418'
419
420test_expect_success 'GIT_TRACE_CURL_NO_DATA prevents data from being traced' '
421 rm -rf clone &&
422 GIT_TRACE_CURL=true \
423 git clone $HTTPD_URL/smart/repo.git clone 2>err &&
424 grep "=> Send data" err &&
425
426 rm -rf clone &&
427 GIT_TRACE_CURL=true GIT_TRACE_CURL_NO_DATA=1 \
428 git clone $HTTPD_URL/smart/repo.git clone 2>err &&
429 ! grep "=> Send data" err
430'
431
432stop_httpd
433test_done