t5551: mark half-auth no-op fetch test as v0-only
[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 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
91 test_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
99 test_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
109 test_expect_success 'follow redirects (301)' '
110         git clone $HTTPD_URL/smart-redir-perm/repo.git --quiet repo-p
111 '
112
113 test_expect_success 'follow redirects (302)' '
114         git clone $HTTPD_URL/smart-redir-temp/repo.git --quiet repo-t
115 '
116
117 test_expect_success 'redirects re-root further requests' '
118         git clone $HTTPD_URL/smart-redir-limited/repo.git repo-redir-limited
119 '
120
121 test_expect_success 're-rooting dies on insane schemes' '
122         test_must_fail git clone $HTTPD_URL/insane-redir/repo.git insane
123 '
124
125 test_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
134 test_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
143 test_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
152 test_expect_success 'no-op half-auth fetch does not require a password' '
153         set_askpass wrong &&
154
155         # NEEDSWORK: When using HTTP(S), protocol v0 supports a "half-auth"
156         # configuration with authentication required only when downloading
157         # objects and not refs, by having the HTTP server only require
158         # authentication for the "git-upload-pack" path and not "info/refs".
159         # This is not possible with protocol v2, since both objects and refs
160         # are obtained from the "git-upload-pack" path. A solution to this is
161         # to teach the server and client to be able to inline ls-refs requests
162         # as an Extra Parameter (see pack-protocol.txt), so that "info/refs"
163         # can serve refs, just like it does in protocol v0.
164         GIT_TEST_PROTOCOL_VERSION=0 git --git-dir=half-auth fetch &&
165         expect_askpass none
166 '
167
168 test_expect_success 'redirects send auth to new location' '
169         set_askpass user@host pass@host &&
170         git -c credential.useHttpPath=true \
171           clone $HTTPD_URL/smart-redir-auth/repo.git repo-redir-auth &&
172         expect_askpass both user@host auth/smart/repo.git
173 '
174
175 test_expect_success 'disable dumb http on server' '
176         git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
177                 config http.getanyfile false
178 '
179
180 test_expect_success 'GIT_SMART_HTTP can disable smart http' '
181         (GIT_SMART_HTTP=0 &&
182          export GIT_SMART_HTTP &&
183          cd clone &&
184          test_must_fail git fetch)
185 '
186
187 test_expect_success 'invalid Content-Type rejected' '
188         test_must_fail git clone $HTTPD_URL/broken_smart/repo.git 2>actual &&
189         grep "not valid:" actual
190 '
191
192 test_expect_success 'create namespaced refs' '
193         test_commit namespaced &&
194         git push public HEAD:refs/namespaces/ns/refs/heads/master &&
195         git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
196                 symbolic-ref refs/namespaces/ns/HEAD refs/namespaces/ns/refs/heads/master
197 '
198
199 test_expect_success 'smart clone respects namespace' '
200         git clone "$HTTPD_URL/smart_namespace/repo.git" ns-smart &&
201         echo namespaced >expect &&
202         git --git-dir=ns-smart/.git log -1 --format=%s >actual &&
203         test_cmp expect actual
204 '
205
206 test_expect_success 'dumb clone via http-backend respects namespace' '
207         git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
208                 config http.getanyfile true &&
209         GIT_SMART_HTTP=0 git clone \
210                 "$HTTPD_URL/smart_namespace/repo.git" ns-dumb &&
211         echo namespaced >expect &&
212         git --git-dir=ns-dumb/.git log -1 --format=%s >actual &&
213         test_cmp expect actual
214 '
215
216 test_expect_success 'cookies stored in http.cookiefile when http.savecookies set' '
217         cat >cookies.txt <<-\EOF &&
218         127.0.0.1       FALSE   /smart_cookies/ FALSE   0       othername       othervalue
219         EOF
220         sort >expect_cookies.txt <<-\EOF &&
221
222         127.0.0.1       FALSE   /smart_cookies/ FALSE   0       othername       othervalue
223         127.0.0.1       FALSE   /smart_cookies/repo.git/info/   FALSE   0       name    value
224         EOF
225         git config http.cookiefile cookies.txt &&
226         git config http.savecookies true &&
227         git ls-remote $HTTPD_URL/smart_cookies/repo.git master &&
228         tail -3 cookies.txt | sort >cookies_tail.txt &&
229         test_cmp expect_cookies.txt cookies_tail.txt
230 '
231
232 test_expect_success 'transfer.hiderefs works over smart-http' '
233         test_commit hidden &&
234         test_commit visible &&
235         git push public HEAD^:refs/heads/a HEAD:refs/heads/b &&
236         git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
237                 config transfer.hiderefs refs/heads/a &&
238         git clone --bare "$HTTPD_URL/smart/repo.git" hidden.git &&
239         test_must_fail git -C hidden.git rev-parse --verify a &&
240         git -C hidden.git rev-parse --verify b
241 '
242
243 # create an arbitrary number of tags, numbered from tag-$1 to tag-$2
244 create_tags () {
245         rm -f marks &&
246         for i in $(test_seq "$1" "$2")
247         do
248                 # don't use here-doc, because it requires a process
249                 # per loop iteration
250                 echo "commit refs/heads/too-many-refs-$1" &&
251                 echo "mark :$i" &&
252                 echo "committer git <git@example.com> $i +0000" &&
253                 echo "data 0" &&
254                 echo "M 644 inline bla.txt" &&
255                 echo "data 4" &&
256                 echo "bla" &&
257                 # make every commit dangling by always
258                 # rewinding the branch after each commit
259                 echo "reset refs/heads/too-many-refs-$1" &&
260                 echo "from :$1"
261         done | git fast-import --export-marks=marks &&
262
263         # now assign tags to all the dangling commits we created above
264         tag=$(perl -e "print \"bla\" x 30") &&
265         sed -e "s|^:\([^ ]*\) \(.*\)$|\2 refs/tags/$tag-\1|" <marks >>packed-refs
266 }
267
268 test_expect_success 'create 2,000 tags in the repo' '
269         (
270                 cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
271                 create_tags 1 2000
272         )
273 '
274
275 test_expect_success CMDLINE_LIMIT \
276         'clone the 2,000 tag repo to check OS command line overflow' '
277         run_with_limited_cmdline git clone $HTTPD_URL/smart/repo.git too-many-refs &&
278         (
279                 cd too-many-refs &&
280                 git for-each-ref refs/tags >actual &&
281                 test_line_count = 2000 actual
282         )
283 '
284
285 test_expect_success 'large fetch-pack requests can be split across POSTs' '
286         GIT_TRACE_CURL=true git -c http.postbuffer=65536 \
287                 clone --bare "$HTTPD_URL/smart/repo.git" split.git 2>err &&
288         grep "^=> Send header: POST" err >posts &&
289         test_line_count = 2 posts
290 '
291
292 test_expect_success 'test allowreachablesha1inwant' '
293         test_when_finished "rm -rf test_reachable.git" &&
294         server="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
295         master_sha=$(git -C "$server" rev-parse refs/heads/master) &&
296         git -C "$server" config uploadpack.allowreachablesha1inwant 1 &&
297
298         git init --bare test_reachable.git &&
299         git -C test_reachable.git remote add origin "$HTTPD_URL/smart/repo.git" &&
300         git -C test_reachable.git fetch origin "$master_sha"
301 '
302
303 test_expect_success 'test allowreachablesha1inwant with unreachable' '
304         test_when_finished "rm -rf test_reachable.git; git reset --hard $(git rev-parse HEAD)" &&
305
306         #create unreachable sha
307         echo content >file2 &&
308         git add file2 &&
309         git commit -m two &&
310         git push public HEAD:refs/heads/doomed &&
311         git push public :refs/heads/doomed &&
312
313         server="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
314         master_sha=$(git -C "$server" rev-parse refs/heads/master) &&
315         git -C "$server" config uploadpack.allowreachablesha1inwant 1 &&
316
317         git init --bare test_reachable.git &&
318         git -C test_reachable.git remote add origin "$HTTPD_URL/smart/repo.git" &&
319         test_must_fail git -C test_reachable.git fetch origin "$(git rev-parse HEAD)"
320 '
321
322 test_expect_success 'test allowanysha1inwant with unreachable' '
323         test_when_finished "rm -rf test_reachable.git; git reset --hard $(git rev-parse HEAD)" &&
324
325         #create unreachable sha
326         echo content >file2 &&
327         git add file2 &&
328         git commit -m two &&
329         git push public HEAD:refs/heads/doomed &&
330         git push public :refs/heads/doomed &&
331
332         server="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
333         master_sha=$(git -C "$server" rev-parse refs/heads/master) &&
334         git -C "$server" config uploadpack.allowreachablesha1inwant 1 &&
335
336         git init --bare test_reachable.git &&
337         git -C test_reachable.git remote add origin "$HTTPD_URL/smart/repo.git" &&
338         test_must_fail git -C test_reachable.git fetch origin "$(git rev-parse HEAD)" &&
339
340         git -C "$server" config uploadpack.allowanysha1inwant 1 &&
341         git -C test_reachable.git fetch origin "$(git rev-parse HEAD)"
342 '
343
344 test_expect_success EXPENSIVE 'http can handle enormous ref negotiation' '
345         (
346                 cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
347                 create_tags 2001 50000
348         ) &&
349         git -C too-many-refs fetch -q --tags &&
350         (
351                 cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
352                 create_tags 50001 100000
353         ) &&
354         git -C too-many-refs fetch -q --tags &&
355         git -C too-many-refs for-each-ref refs/tags >tags &&
356         test_line_count = 100000 tags
357 '
358
359 test_expect_success 'custom http headers' '
360         test_must_fail git -c http.extraheader="x-magic-two: cadabra" \
361                 fetch "$HTTPD_URL/smart_headers/repo.git" &&
362         git -c http.extraheader="x-magic-one: abra" \
363             -c http.extraheader="x-magic-two: cadabra" \
364             fetch "$HTTPD_URL/smart_headers/repo.git" &&
365         git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
366         git config -f .gitmodules submodule.sub.path sub &&
367         git config -f .gitmodules submodule.sub.url \
368                 "$HTTPD_URL/smart_headers/repo.git" &&
369         git submodule init sub &&
370         test_must_fail git submodule update sub &&
371         git -c http.extraheader="x-magic-one: abra" \
372             -c http.extraheader="x-magic-two: cadabra" \
373                 submodule update sub
374 '
375
376 test_expect_success 'using fetch command in remote-curl updates refs' '
377         SERVER="$HTTPD_DOCUMENT_ROOT_PATH/twobranch" &&
378         rm -rf "$SERVER" client &&
379
380         git init "$SERVER" &&
381         test_commit -C "$SERVER" foo &&
382         git -C "$SERVER" update-ref refs/heads/anotherbranch foo &&
383
384         git clone $HTTPD_URL/smart/twobranch client &&
385
386         test_commit -C "$SERVER" bar &&
387         git -C client -c protocol.version=0 fetch &&
388
389         git -C "$SERVER" rev-parse master >expect &&
390         git -C client rev-parse origin/master >actual &&
391         test_cmp expect actual
392 '
393
394 test_expect_success 'fetch by SHA-1 without tag following' '
395         SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
396         rm -rf "$SERVER" client &&
397
398         git init "$SERVER" &&
399         test_commit -C "$SERVER" foo &&
400
401         git clone $HTTPD_URL/smart/server client &&
402
403         test_commit -C "$SERVER" bar &&
404         git -C "$SERVER" rev-parse bar >bar_hash &&
405         git -C client -c protocol.version=0 fetch \
406                 --no-tags origin $(cat bar_hash)
407 '
408
409 test_expect_success 'GIT_REDACT_COOKIES redacts cookies' '
410         rm -rf clone &&
411         echo "Set-Cookie: Foo=1" >cookies &&
412         echo "Set-Cookie: Bar=2" >>cookies &&
413         GIT_TRACE_CURL=true GIT_REDACT_COOKIES=Bar,Baz \
414                 git -c "http.cookieFile=$(pwd)/cookies" clone \
415                 $HTTPD_URL/smart/repo.git clone 2>err &&
416         grep "Cookie:.*Foo=1" err &&
417         grep "Cookie:.*Bar=<redacted>" err &&
418         ! grep "Cookie:.*Bar=2" err
419 '
420
421 test_expect_success 'GIT_REDACT_COOKIES handles empty values' '
422         rm -rf clone &&
423         echo "Set-Cookie: Foo=" >cookies &&
424         GIT_TRACE_CURL=true GIT_REDACT_COOKIES=Foo \
425                 git -c "http.cookieFile=$(pwd)/cookies" clone \
426                 $HTTPD_URL/smart/repo.git clone 2>err &&
427         grep "Cookie:.*Foo=<redacted>" err
428 '
429
430 test_expect_success 'GIT_TRACE_CURL_NO_DATA prevents data from being traced' '
431         rm -rf clone &&
432         GIT_TRACE_CURL=true \
433                 git clone $HTTPD_URL/smart/repo.git clone 2>err &&
434         grep "=> Send data" err &&
435
436         rm -rf clone &&
437         GIT_TRACE_CURL=true GIT_TRACE_CURL_NO_DATA=1 \
438                 git clone $HTTPD_URL/smart/repo.git clone 2>err &&
439         ! grep "=> Send data" err
440 '
441
442 test_expect_success 'server-side error detected' '
443         test_must_fail git clone $HTTPD_URL/error_smart/repo.git 2>actual &&
444         grep "server-side error" actual
445 '
446
447 stop_httpd
448 test_done