]> git.ipfire.org Git - thirdparty/git.git/blame - t/t5570-git-daemon.sh
The third batch
[thirdparty/git.git] / t / t5570-git-daemon.sh
CommitLineData
71039fb9
CB
1#!/bin/sh
2
3test_description='test fetching over git protocol'
028cb644 4GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
334afbc7
JS
5export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
6
71039fb9
CB
7. ./test-lib.sh
8
71039fb9
CB
9. "$TEST_DIRECTORY"/lib-git-daemon.sh
10start_git_daemon
11
25bb90b1 12check_verbose_connect () {
6789275d
JH
13 test_grep -F "Looking up 127.0.0.1 ..." stderr &&
14 test_grep -F "Connecting to 127.0.0.1 (port " stderr &&
15 test_grep -F "done." stderr
25bb90b1
EW
16}
17
71039fb9 18test_expect_success 'setup repository' '
ac47a22a 19 git config push.default matching &&
71039fb9
CB
20 echo content >file &&
21 git add file &&
22 git commit -m one
23'
24
25test_expect_success 'create git-accessible bare repository' '
26 mkdir "$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git" &&
27 (cd "$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git" &&
28 git --bare init &&
29 : >git-daemon-export-ok
30 ) &&
31 git remote add public "$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git" &&
028cb644 32 git push public main:main
71039fb9
CB
33'
34
35test_expect_success 'clone git repository' '
25bb90b1
EW
36 git clone -v "$GIT_DAEMON_URL/repo.git" clone 2>stderr &&
37 check_verbose_connect &&
71039fb9
CB
38 test_cmp file clone/file
39'
40
41test_expect_success 'fetch changes via git protocol' '
42 echo content >>file &&
43 git commit -a -m two &&
44 git push public &&
25bb90b1
EW
45 (cd clone && git pull -v) 2>stderr &&
46 check_verbose_connect &&
71039fb9
CB
47 test_cmp file clone/file
48'
49
25bb90b1
EW
50test_expect_success 'no-op fetch -v stderr is as expected' '
51 (cd clone && git fetch -v) 2>stderr &&
52 check_verbose_connect
53'
54
55test_expect_success 'no-op fetch without "-v" is quiet' '
91538d0c 56 (cd clone && git fetch 2>../stderr) &&
ec10b018 57 test_must_be_empty stderr
25bb90b1
EW
58'
59
2ecb573b 60test_expect_success 'remote detects correct HEAD' '
028cb644 61 git push public main:other &&
71039fb9
CB
62 (cd clone &&
63 git remote set-head -d origin &&
64 git remote set-head -a origin &&
65 git symbolic-ref refs/remotes/origin/HEAD > output &&
028cb644 66 echo refs/remotes/origin/main > expect &&
71039fb9
CB
67 test_cmp expect output
68 )
69'
70
71test_expect_success 'prepare pack objects' '
72 cp -R "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo.git "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_pack.git &&
73 (cd "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_pack.git &&
74 git --bare repack -a -d
75 )
76'
77
78test_expect_success 'fetch notices corrupt pack' '
79 cp -R "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_pack.git "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_bad1.git &&
80 (cd "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_bad1.git &&
c747cf33 81 p=$(ls objects/pack/pack-*.pack) &&
71039fb9
CB
82 chmod u+w $p &&
83 printf %0256d 0 | dd of=$p bs=256 count=1 seek=1 conv=notrunc
84 ) &&
85 mkdir repo_bad1.git &&
86 (cd repo_bad1.git &&
87 git --bare init &&
88 test_must_fail git --bare fetch "$GIT_DAEMON_URL/repo_bad1.git" &&
c747cf33 89 test 0 = $(ls objects/pack/pack-*.pack | wc -l)
71039fb9
CB
90 )
91'
92
93test_expect_success 'fetch notices corrupt idx' '
94 cp -R "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_pack.git "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_bad2.git &&
95 (cd "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_bad2.git &&
fc789156 96 rm -f objects/pack/multi-pack-index &&
c747cf33 97 p=$(ls objects/pack/pack-*.idx) &&
71039fb9
CB
98 chmod u+w $p &&
99 printf %0256d 0 | dd of=$p bs=256 count=1 seek=1 conv=notrunc
100 ) &&
101 mkdir repo_bad2.git &&
102 (cd repo_bad2.git &&
103 git --bare init &&
104 test_must_fail git --bare fetch "$GIT_DAEMON_URL/repo_bad2.git" &&
c747cf33 105 test 0 = $(ls objects/pack | wc -l)
71039fb9
CB
106 )
107'
108
a02ea577
JK
109test_expect_success 'client refuses to ask for repo with newline' '
110 test_must_fail git clone "$GIT_DAEMON_URL/repo$LF.git" dst 2>stderr &&
6789275d 111 test_grep newline.is.forbidden stderr
a02ea577
JK
112'
113
71039fb9
CB
114test_remote_error()
115{
116 do_export=YesPlease
117 while test $# -gt 0
118 do
119 case $1 in
120 -x)
121 shift
122 chmod -x "$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git"
123 ;;
124 -n)
125 shift
126 do_export=
127 ;;
128 *)
129 break
130 esac
131 done
132
aecff47d
CB
133 msg=$1
134 shift
71039fb9 135 cmd=$1
aecff47d
CB
136 shift
137 repo=$1
138 shift || error "invalid number of arguments"
71039fb9
CB
139
140 if test -x "$GIT_DAEMON_DOCUMENT_ROOT_PATH/$repo"
141 then
142 if test -n "$do_export"
143 then
144 : >"$GIT_DAEMON_DOCUMENT_ROOT_PATH/$repo/git-daemon-export-ok"
145 else
146 rm -f "$GIT_DAEMON_DOCUMENT_ROOT_PATH/$repo/git-daemon-export-ok"
147 fi
148 fi
149
aecff47d 150 test_must_fail git "$cmd" "$GIT_DAEMON_URL/$repo" "$@" 2>output &&
6789275d 151 test_grep "fatal: remote error: $msg: /$repo" output &&
71039fb9
CB
152 ret=$?
153 chmod +x "$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git"
154 (exit $ret)
155}
156
157msg="access denied or repository not exported"
d98f2726 158test_expect_success 'clone non-existent' "test_remote_error '$msg' clone nowhere.git"
028cb644 159test_expect_success 'push disabled' "test_remote_error '$msg' push repo.git main"
d98f2726
JS
160test_expect_success 'read access denied' "test_remote_error -x '$msg' fetch repo.git"
161test_expect_success 'not exported' "test_remote_error -n '$msg' fetch repo.git"
71039fb9
CB
162
163stop_git_daemon
164start_git_daemon --informative-errors
165
d98f2726 166test_expect_success 'clone non-existent' "test_remote_error 'no such repository' clone nowhere.git"
028cb644 167test_expect_success 'push disabled' "test_remote_error 'service not enabled' push repo.git main"
d98f2726
JS
168test_expect_success 'read access denied' "test_remote_error -x 'no such repository' fetch repo.git"
169test_expect_success 'not exported' "test_remote_error -n 'repository not exported' fetch repo.git"
71039fb9 170
5248f2dd
JK
171stop_git_daemon
172start_git_daemon --interpolated-path="$GIT_DAEMON_DOCUMENT_ROOT_PATH/%H%D"
173
174test_expect_success 'access repo via interpolated hostname' '
175 repo="$GIT_DAEMON_DOCUMENT_ROOT_PATH/localhost/interp.git" &&
176 git init --bare "$repo" &&
177 git push "$repo" HEAD &&
178 >"$repo"/git-daemon-export-ok &&
5248f2dd 179 GIT_OVERRIDE_VIRTUAL_HOST=localhost \
02adf84a 180 git ls-remote "$GIT_DAEMON_URL/interp.git" &&
5248f2dd 181 GIT_OVERRIDE_VIRTUAL_HOST=LOCALHOST \
02adf84a 182 git ls-remote "$GIT_DAEMON_URL/interp.git"
5248f2dd
JK
183'
184
b4853730 185test_expect_success 'hostname cannot break out of directory' '
b4853730
JK
186 repo="$GIT_DAEMON_DOCUMENT_ROOT_PATH/../escape.git" &&
187 git init --bare "$repo" &&
188 git push "$repo" HEAD &&
189 >"$repo"/git-daemon-export-ok &&
190 test_must_fail \
191 env GIT_OVERRIDE_VIRTUAL_HOST=.. \
02adf84a 192 git ls-remote "$GIT_DAEMON_URL/escape.git"
b4853730
JK
193'
194
ed15e58e
JK
195test_expect_success FAKENC 'hostname interpolation works after LF-stripping' '
196 {
7abcbcb7 197 printf "git-upload-pack /interp.git\n\0host=localhost" | packetize_raw &&
ed15e58e
JK
198 printf "0000"
199 } >input &&
200 fake_nc "$GIT_DAEMON_HOST_PORT" <input >output &&
201 depacketize <output >output.raw &&
202
028cb644 203 # just pick out the value of main, which avoids any protocol
ed15e58e 204 # particulars
028cb644
JS
205 perl -lne "print \$1 if m{^(\\S+) refs/heads/main}" <output.raw >actual &&
206 git -C "$repo" rev-parse main >expect &&
ed15e58e
JK
207 test_cmp expect actual
208'
209
71039fb9 210test_done