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