git-prompt: use here-doc instead of here-string
[git/gitster.git] / t / t5550-http-fetch-dumb.sh
blobea8e48f627315f115a2dbcaf08b47f704c755eaa
1 #!/bin/sh
3 test_description='test dumb fetching over http via static file'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7 . ./test-lib.sh
9 if test_have_prereq !REFFILES
10 then
11 skip_all='skipping test; dumb HTTP protocol not supported with reftable.'
12 test_done
15 . "$TEST_DIRECTORY"/lib-httpd.sh
16 start_httpd
18 test_expect_success 'setup repository' '
19 git config push.default matching &&
20 echo content1 >file &&
21 git add file &&
22 git commit -m one &&
23 echo content2 >file &&
24 git add file &&
25 git commit -m two
28 test_expect_success 'packfile without repository does not crash' '
29 echo "fatal: not a git repository" >expect &&
30 test_must_fail nongit git http-fetch --packfile=abc 2>err &&
31 test_cmp expect err
34 setup_post_update_server_info_hook () {
35 test_hook --setup -C "$1" post-update <<-\EOF &&
36 exec git update-server-info
37 EOF
38 git -C "$1" update-server-info
41 test_expect_success 'create http-accessible bare repository with loose objects' '
42 cp -R .git "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
43 git -C "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" config core.bare true &&
44 setup_post_update_server_info_hook "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
45 git remote add public "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
46 git push public main:main
49 test_expect_success 'clone http repository' '
50 git clone $HTTPD_URL/dumb/repo.git clone-tmpl &&
51 cp -R clone-tmpl clone &&
52 test_cmp file clone/file
55 test_expect_success 'list refs from outside any repository' '
56 cat >expect <<-EOF &&
57 $(git rev-parse main) HEAD
58 $(git rev-parse main) refs/heads/main
59 EOF
60 nongit git ls-remote "$HTTPD_URL/dumb/repo.git" >actual &&
61 test_cmp expect actual
65 test_expect_success 'list detached HEAD from outside any repository' '
66 git clone --mirror "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
67 "$HTTPD_DOCUMENT_ROOT_PATH/repo-detached.git" &&
68 git -C "$HTTPD_DOCUMENT_ROOT_PATH/repo-detached.git" \
69 update-ref --no-deref HEAD refs/heads/main &&
70 git -C "$HTTPD_DOCUMENT_ROOT_PATH/repo-detached.git" update-server-info &&
71 cat >expect <<-EOF &&
72 $(git rev-parse main) HEAD
73 $(git rev-parse main) refs/heads/main
74 EOF
75 nongit git ls-remote "$HTTPD_URL/dumb/repo-detached.git" >actual &&
76 test_cmp expect actual
79 test_expect_success 'create password-protected repository' '
80 mkdir -p "$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb/" &&
81 cp -Rf "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
82 "$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb/repo.git"
85 test_expect_success 'create empty remote repository' '
86 git init --bare "$HTTPD_DOCUMENT_ROOT_PATH/empty.git" &&
87 setup_post_update_server_info_hook "$HTTPD_DOCUMENT_ROOT_PATH/empty.git"
90 test_expect_success 'empty dumb HTTP repository falls back to SHA1' '
91 test_when_finished "rm -fr clone-empty" &&
92 git clone $HTTPD_URL/dumb/empty.git clone-empty &&
93 git -C clone-empty rev-parse --show-object-format >empty-format &&
94 test "$(cat empty-format)" = sha1
97 setup_askpass_helper
99 test_expect_success 'cloning password-protected repository can fail' '
100 set_askpass wrong &&
101 test_must_fail git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-fail &&
102 expect_askpass both wrong
105 test_expect_success 'http auth can use user/pass in URL' '
106 set_askpass wrong &&
107 git clone "$HTTPD_URL_USER_PASS/auth/dumb/repo.git" clone-auth-none &&
108 expect_askpass none
111 test_expect_success 'http auth can use just user in URL' '
112 set_askpass wrong pass@host &&
113 git clone "$HTTPD_URL_USER/auth/dumb/repo.git" clone-auth-pass &&
114 expect_askpass pass user@host
117 test_expect_success 'http auth can request both user and pass' '
118 set_askpass user@host pass@host &&
119 git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-both &&
120 expect_askpass both user@host
123 test_expect_success 'http auth respects credential helper config' '
124 test_config_global credential.helper "!f() {
125 cat >/dev/null
126 echo username=user@host
127 echo password=pass@host
128 }; f" &&
129 set_askpass wrong &&
130 git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-helper &&
131 expect_askpass none
134 test_expect_success 'http auth can get username from config' '
135 test_config_global "credential.$HTTPD_URL.username" user@host &&
136 set_askpass wrong pass@host &&
137 git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-user &&
138 expect_askpass pass user@host
141 test_expect_success 'configured username does not override URL' '
142 test_config_global "credential.$HTTPD_URL.username" wrong &&
143 set_askpass wrong pass@host &&
144 git clone "$HTTPD_URL_USER/auth/dumb/repo.git" clone-auth-user2 &&
145 expect_askpass pass user@host
148 test_expect_success 'set up repo with http submodules' '
149 git init super &&
150 set_askpass user@host pass@host &&
152 cd super &&
153 git submodule add "$HTTPD_URL/auth/dumb/repo.git" sub &&
154 git commit -m "add submodule"
158 test_expect_success 'cmdline credential config passes to submodule via clone' '
159 set_askpass wrong pass@host &&
160 test_must_fail git clone --recursive super super-clone &&
161 rm -rf super-clone &&
163 set_askpass wrong pass@host &&
164 git -c "credential.$HTTPD_URL.username=user@host" \
165 clone --recursive super super-clone &&
166 expect_askpass pass user@host
169 test_expect_success 'cmdline credential config passes submodule via fetch' '
170 set_askpass wrong pass@host &&
171 test_must_fail git -C super-clone fetch --recurse-submodules &&
173 set_askpass wrong pass@host &&
174 git -C super-clone \
175 -c "credential.$HTTPD_URL.username=user@host" \
176 fetch --recurse-submodules &&
177 expect_askpass pass user@host
180 test_expect_success 'cmdline credential config passes submodule update' '
181 # advance the submodule HEAD so that a fetch is required
182 git commit --allow-empty -m foo &&
183 git push "$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb/repo.git" HEAD &&
184 sha1=$(git rev-parse HEAD) &&
185 git -C super-clone update-index --cacheinfo 160000,$sha1,sub &&
187 set_askpass wrong pass@host &&
188 test_must_fail git -C super-clone submodule update &&
190 set_askpass wrong pass@host &&
191 git -C super-clone \
192 -c "credential.$HTTPD_URL.username=user@host" \
193 submodule update &&
194 expect_askpass pass user@host
197 test_expect_success 'fetch changes via http' '
198 echo content >>file &&
199 git commit -a -m two &&
200 git push public &&
201 (cd clone && git pull) &&
202 test_cmp file clone/file
205 test_expect_success 'fetch changes via manual http-fetch' '
206 cp -R clone-tmpl clone2 &&
208 HEAD=$(git rev-parse --verify HEAD) &&
209 (cd clone2 &&
210 git http-fetch -a -w heads/main-new $HEAD $(git config remote.origin.url) &&
211 git checkout main-new &&
212 test $HEAD = $(git rev-parse --verify HEAD)) &&
213 test_cmp file clone2/file
216 test_expect_success 'manual http-fetch without -a works just as well' '
217 cp -R clone-tmpl clone3 &&
219 HEAD=$(git rev-parse --verify HEAD) &&
220 (cd clone3 &&
221 git http-fetch -w heads/main-new $HEAD $(git config remote.origin.url) &&
222 git checkout main-new &&
223 test $HEAD = $(git rev-parse --verify HEAD)) &&
224 test_cmp file clone3/file
227 test_expect_success 'http remote detects correct HEAD' '
228 git push public main:other &&
229 (cd clone &&
230 git remote set-head origin -d &&
231 git remote set-head origin -a &&
232 git symbolic-ref refs/remotes/origin/HEAD > output &&
233 echo refs/remotes/origin/main > expect &&
234 test_cmp expect output
238 test_expect_success 'fetch packed objects' '
239 cp -R "$HTTPD_DOCUMENT_ROOT_PATH"/repo.git "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git &&
240 (cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git &&
241 git --bare repack -a -d
242 ) &&
243 git clone $HTTPD_URL/dumb/repo_pack.git
246 test_expect_success 'http-fetch --packfile' '
247 # Arbitrary hash. Use rev-parse so that we get one of the correct
248 # length.
249 ARBITRARY=$(git -C "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git rev-parse HEAD) &&
251 git init packfileclient &&
252 p=$(cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git && ls objects/pack/pack-*.pack) &&
253 git -C packfileclient http-fetch --packfile=$ARBITRARY \
254 --index-pack-arg=index-pack --index-pack-arg=--stdin \
255 --index-pack-arg=--keep \
256 "$HTTPD_URL"/dumb/repo_pack.git/$p >out &&
258 grep -E "^keep.[0-9a-f]{16,}$" out &&
259 cut -c6- out >packhash &&
261 # Ensure that the expected files are generated
262 test -e "packfileclient/.git/objects/pack/pack-$(cat packhash).pack" &&
263 test -e "packfileclient/.git/objects/pack/pack-$(cat packhash).idx" &&
264 test -e "packfileclient/.git/objects/pack/pack-$(cat packhash).keep" &&
266 # Ensure that it has the HEAD of repo_pack, at least
267 HASH=$(git -C "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git rev-parse HEAD) &&
268 git -C packfileclient cat-file -e "$HASH"
271 test_expect_success 'fetch notices corrupt pack' '
272 cp -R "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad1.git &&
273 (cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad1.git &&
274 p=$(ls objects/pack/pack-*.pack) &&
275 chmod u+w $p &&
276 printf %0256d 0 | dd of=$p bs=256 count=1 seek=1 conv=notrunc
277 ) &&
278 mkdir repo_bad1.git &&
279 (cd repo_bad1.git &&
280 git --bare init &&
281 test_must_fail git --bare fetch $HTTPD_URL/dumb/repo_bad1.git &&
282 test 0 = $(ls objects/pack/pack-*.pack | wc -l)
286 test_expect_success 'http-fetch --packfile with corrupt pack' '
287 rm -rf packfileclient &&
288 git init packfileclient &&
289 p=$(cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad1.git && ls objects/pack/pack-*.pack) &&
290 test_must_fail git -C packfileclient http-fetch --packfile \
291 "$HTTPD_URL"/dumb/repo_bad1.git/$p
294 test_expect_success 'fetch notices corrupt idx' '
295 cp -R "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad2.git &&
296 (cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad2.git &&
297 p=$(ls objects/pack/pack-*.idx) &&
298 chmod u+w $p &&
299 printf %0256d 0 | dd of=$p bs=256 count=1 seek=1 conv=notrunc
300 ) &&
301 mkdir repo_bad2.git &&
302 (cd repo_bad2.git &&
303 git --bare init &&
304 test_must_fail git --bare fetch $HTTPD_URL/dumb/repo_bad2.git &&
305 test 0 = $(ls objects/pack | wc -l)
309 test_expect_success 'fetch can handle previously-fetched .idx files' '
310 git checkout --orphan branch1 &&
311 echo base >file &&
312 git add file &&
313 git commit -m base &&
314 git --bare init "$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git &&
315 git push "$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git branch1 &&
316 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git repack -d &&
317 git checkout -b branch2 branch1 &&
318 echo b2 >>file &&
319 git commit -a -m b2 &&
320 git push "$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git branch2 &&
321 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git repack -d &&
322 git --bare init clone_packed_branches.git &&
323 git --git-dir=clone_packed_branches.git fetch "$HTTPD_URL"/dumb/repo_packed_branches.git branch1:branch1 &&
324 git --git-dir=clone_packed_branches.git fetch "$HTTPD_URL"/dumb/repo_packed_branches.git branch2:branch2
327 test_expect_success 'did not use upload-pack service' '
328 ! grep "/git-upload-pack" "$HTTPD_ROOT_PATH/access.log"
331 test_expect_success 'git client shows text/plain errors' '
332 test_must_fail git clone "$HTTPD_URL/error/text" 2>stderr &&
333 grep "this is the error message" stderr
336 test_expect_success 'git client does not show html errors' '
337 test_must_fail git clone "$HTTPD_URL/error/html" 2>stderr &&
338 ! grep "this is the error message" stderr
341 test_expect_success 'git client shows text/plain with a charset' '
342 test_must_fail git clone "$HTTPD_URL/error/charset" 2>stderr &&
343 grep "this is the error message" stderr
346 test_expect_success 'http error messages are reencoded' '
347 test_must_fail git clone "$HTTPD_URL/error/utf16" 2>stderr &&
348 grep "this is the error message" stderr
351 test_expect_success 'reencoding is robust to whitespace oddities' '
352 test_must_fail git clone "$HTTPD_URL/error/odd-spacing" 2>stderr &&
353 grep "this is the error message" stderr
356 check_language () {
357 case "$2" in
359 >expect
362 echo "=> Send header: Accept-Language: $1" >expect
364 esac &&
365 GIT_TRACE_CURL=true \
366 LANGUAGE=$2 \
367 git ls-remote "$HTTPD_URL/dumb/repo.git" >output 2>&1 &&
368 tr -d '\015' <output |
369 sort -u |
370 sed -ne '/^=> Send header: Accept-Language:/ p' >actual &&
371 test_cmp expect actual
374 test_expect_success 'git client sends Accept-Language based on LANGUAGE' '
375 check_language "ko-KR, *;q=0.9" ko_KR.UTF-8'
377 test_expect_success 'git client sends Accept-Language correctly with unordinary LANGUAGE' '
378 check_language "ko-KR, *;q=0.9" "ko_KR:" &&
379 check_language "ko-KR, en-US;q=0.9, *;q=0.8" "ko_KR::en_US" &&
380 check_language "ko-KR, *;q=0.9" ":::ko_KR" &&
381 check_language "ko-KR, en-US;q=0.9, *;q=0.8" "ko_KR!!:en_US" &&
382 check_language "ko-KR, ja-JP;q=0.9, *;q=0.8" "ko_KR en_US:ja_JP"'
384 test_expect_success 'git client sends Accept-Language with many preferred languages' '
385 check_language "ko-KR, en-US;q=0.9, fr-CA;q=0.8, de;q=0.7, sr;q=0.6, \
386 ja;q=0.5, zh;q=0.4, sv;q=0.3, pt;q=0.2, *;q=0.1" \
387 ko_KR.EUC-KR:en_US.UTF-8:fr_CA:de.UTF-8@euro:sr@latin:ja:zh:sv:pt &&
388 check_language "ko-KR, en-US;q=0.99, fr-CA;q=0.98, de;q=0.97, sr;q=0.96, \
389 ja;q=0.95, zh;q=0.94, sv;q=0.93, pt;q=0.92, nb;q=0.91, *;q=0.90" \
390 ko_KR.EUC-KR:en_US.UTF-8:fr_CA:de.UTF-8@euro:sr@latin:ja:zh:sv:pt:nb
393 test_expect_success 'git client send an empty Accept-Language' '
394 GIT_TRACE_CURL=true LANGUAGE= git ls-remote "$HTTPD_URL/dumb/repo.git" 2>stderr &&
395 ! grep "^=> Send header: Accept-Language:" stderr
398 test_expect_success 'remote-http complains cleanly about malformed urls' '
399 test_must_fail git remote-http http::/example.com/repo.git 2>stderr &&
400 test_grep "url has no scheme" stderr
403 # NEEDSWORK: Writing commands to git-remote-curl can race against the latter
404 # erroring out, producing SIGPIPE. Remove "ok=sigpipe" once transport-helper has
405 # learned to handle early remote helper failures more cleanly.
406 test_expect_success 'remote-http complains cleanly about empty scheme' '
407 test_must_fail ok=sigpipe git ls-remote \
408 http::${HTTPD_URL#http}/dumb/repo.git 2>stderr &&
409 test_grep "url has no scheme" stderr
412 test_expect_success 'redirects can be forbidden/allowed' '
413 test_must_fail git -c http.followRedirects=false \
414 clone $HTTPD_URL/dumb-redir/repo.git dumb-redir &&
415 git -c http.followRedirects=true \
416 clone $HTTPD_URL/dumb-redir/repo.git dumb-redir 2>stderr
419 test_expect_success 'redirects are reported to stderr' '
420 # just look for a snippet of the redirected-to URL
421 test_grep /dumb/ stderr
424 test_expect_success 'non-initial redirects can be forbidden' '
425 test_must_fail git -c http.followRedirects=initial \
426 clone $HTTPD_URL/redir-objects/repo.git redir-objects &&
427 git -c http.followRedirects=true \
428 clone $HTTPD_URL/redir-objects/repo.git redir-objects
431 test_expect_success 'http.followRedirects defaults to "initial"' '
432 test_must_fail git clone $HTTPD_URL/redir-objects/repo.git default
435 # The goal is for a clone of the "evil" repository, which has no objects
436 # itself, to cause the client to fetch objects from the "victim" repository.
437 test_expect_success 'set up evil alternates scheme' '
438 victim=$HTTPD_DOCUMENT_ROOT_PATH/victim.git &&
439 git init --bare "$victim" &&
440 git -C "$victim" --work-tree=. commit --allow-empty -m secret &&
441 git -C "$victim" repack -ad &&
442 git -C "$victim" update-server-info &&
443 sha1=$(git -C "$victim" rev-parse HEAD) &&
445 evil=$HTTPD_DOCUMENT_ROOT_PATH/evil.git &&
446 git init --template= --bare "$evil" &&
447 mkdir "$evil/info" &&
448 # do this by hand to avoid object existence check
449 printf "%s\\t%s\\n" $sha1 refs/heads/main >"$evil/info/refs"
452 # Here we'll just redirect via HTTP. In a real-world attack these would be on
453 # different servers, but we should reject it either way.
454 test_expect_success 'http-alternates is a non-initial redirect' '
455 echo "$HTTPD_URL/dumb/victim.git/objects" \
456 >"$evil/objects/info/http-alternates" &&
457 test_must_fail git -c http.followRedirects=initial \
458 clone $HTTPD_URL/dumb/evil.git evil-initial &&
459 git -c http.followRedirects=true \
460 clone $HTTPD_URL/dumb/evil.git evil-initial
463 # Curl supports a lot of protocols that we'd prefer not to allow
464 # http-alternates to use, but it's hard to test whether curl has
465 # accessed, say, the SMTP protocol, because we are not running an SMTP server.
466 # But we can check that it does not allow access to file://, which would
467 # otherwise allow this clone to complete.
468 test_expect_success 'http-alternates cannot point at funny protocols' '
469 echo "file://$victim/objects" >"$evil/objects/info/http-alternates" &&
470 test_must_fail git -c http.followRedirects=true \
471 clone "$HTTPD_URL/dumb/evil.git" evil-file
474 test_expect_success 'http-alternates triggers not-from-user protocol check' '
475 echo "$HTTPD_URL/dumb/victim.git/objects" \
476 >"$evil/objects/info/http-alternates" &&
477 test_config_global http.followRedirects true &&
478 test_must_fail git -c protocol.http.allow=user \
479 clone $HTTPD_URL/dumb/evil.git evil-user &&
480 git -c protocol.http.allow=always \
481 clone $HTTPD_URL/dumb/evil.git evil-user
484 test_expect_success 'can redirect through non-"info/refs?service=git-upload-pack" URL' '
485 git clone "$HTTPD_URL/redir-to/dumb/repo.git"
488 test_expect_success 'print HTTP error when any intermediate redirect throws error' '
489 test_must_fail git clone "$HTTPD_URL/redir-to/502" 2> stderr &&
490 test_grep "unable to access.*/redir-to/502" stderr
493 test_expect_success 'fetching via http alternates works' '
494 parent=$HTTPD_DOCUMENT_ROOT_PATH/alt-parent.git &&
495 git init --bare "$parent" &&
496 git -C "$parent" --work-tree=. commit --allow-empty -m foo &&
497 git -C "$parent" update-server-info &&
498 commit=$(git -C "$parent" rev-parse HEAD) &&
500 child=$HTTPD_DOCUMENT_ROOT_PATH/alt-child.git &&
501 git init --bare "$child" &&
502 echo "../../alt-parent.git/objects" >"$child/objects/info/alternates" &&
503 git -C "$child" update-ref HEAD $commit &&
504 git -C "$child" update-server-info &&
506 git -c http.followredirects=true clone "$HTTPD_URL/dumb/alt-child.git"
509 test_done