setup: split "extensions found" messages into singular and plural
[git] / t / t5570-git-daemon.sh
1 #!/bin/sh
2
3 test_description='test fetching over git protocol'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
6
7 . ./test-lib.sh
8
9 . "$TEST_DIRECTORY"/lib-git-daemon.sh
10 start_git_daemon
11
12 check_verbose_connect () {
13         test_i18ngrep -F "Looking up 127.0.0.1 ..." stderr &&
14         test_i18ngrep -F "Connecting to 127.0.0.1 (port " stderr &&
15         test_i18ngrep -F "done." stderr
16 }
17
18 test_expect_success 'setup repository' '
19         git config push.default matching &&
20         echo content >file &&
21         git add file &&
22         git commit -m one
23 '
24
25 test_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" &&
32         git push public main:main
33 '
34
35 test_expect_success 'clone git repository' '
36         git clone -v "$GIT_DAEMON_URL/repo.git" clone 2>stderr &&
37         check_verbose_connect &&
38         test_cmp file clone/file
39 '
40
41 test_expect_success 'fetch changes via git protocol' '
42         echo content >>file &&
43         git commit -a -m two &&
44         git push public &&
45         (cd clone && git pull -v) 2>stderr &&
46         check_verbose_connect &&
47         test_cmp file clone/file
48 '
49
50 test_expect_success 'no-op fetch -v stderr is as expected' '
51         (cd clone && git fetch -v) 2>stderr &&
52         check_verbose_connect
53 '
54
55 test_expect_success 'no-op fetch without "-v" is quiet' '
56         (cd clone && git fetch 2>../stderr) &&
57         test_must_be_empty stderr
58 '
59
60 test_expect_success 'remote detects correct HEAD' '
61         git push public main:other &&
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 &&
66          echo refs/remotes/origin/main > expect &&
67          test_cmp expect output
68         )
69 '
70
71 test_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
78 test_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 &&
81          p=$(ls objects/pack/pack-*.pack) &&
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" &&
89          test 0 = $(ls objects/pack/pack-*.pack | wc -l)
90         )
91 '
92
93 test_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 &&
96          rm -f objects/pack/multi-pack-index &&
97          p=$(ls objects/pack/pack-*.idx) &&
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" &&
105          test 0 = $(ls objects/pack | wc -l)
106         )
107 '
108
109 test_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 &&
111         test_i18ngrep newline.is.forbidden stderr
112 '
113
114 test_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
133         msg=$1
134         shift
135         cmd=$1
136         shift
137         repo=$1
138         shift || error "invalid number of arguments"
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
150         test_must_fail git "$cmd" "$GIT_DAEMON_URL/$repo" "$@" 2>output &&
151         test_i18ngrep "fatal: remote error: $msg: /$repo" output &&
152         ret=$?
153         chmod +x "$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git"
154         (exit $ret)
155 }
156
157 msg="access denied or repository not exported"
158 test_expect_success 'clone non-existent' "test_remote_error    '$msg' clone nowhere.git"
159 test_expect_success 'push disabled'      "test_remote_error    '$msg' push  repo.git main"
160 test_expect_success 'read access denied' "test_remote_error -x '$msg' fetch repo.git"
161 test_expect_success 'not exported'       "test_remote_error -n '$msg' fetch repo.git"
162
163 stop_git_daemon
164 start_git_daemon --informative-errors
165
166 test_expect_success 'clone non-existent' "test_remote_error    'no such repository'      clone nowhere.git"
167 test_expect_success 'push disabled'      "test_remote_error    'service not enabled'     push  repo.git main"
168 test_expect_success 'read access denied' "test_remote_error -x 'no such repository'      fetch repo.git"
169 test_expect_success 'not exported'       "test_remote_error -n 'repository not exported' fetch repo.git"
170
171 stop_git_daemon
172 start_git_daemon --interpolated-path="$GIT_DAEMON_DOCUMENT_ROOT_PATH/%H%D"
173
174 test_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 &&
179         GIT_OVERRIDE_VIRTUAL_HOST=localhost \
180                 git ls-remote "$GIT_DAEMON_URL/interp.git" &&
181         GIT_OVERRIDE_VIRTUAL_HOST=LOCALHOST \
182                 git ls-remote "$GIT_DAEMON_URL/interp.git"
183 '
184
185 test_expect_success 'hostname cannot break out of directory' '
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=.. \
192                 git ls-remote "$GIT_DAEMON_URL/escape.git"
193 '
194
195 test_expect_success FAKENC 'hostname interpolation works after LF-stripping' '
196         {
197                 printf "git-upload-pack /interp.git\n\0host=localhost" | packetize
198                 printf "0000"
199         } >input &&
200         fake_nc "$GIT_DAEMON_HOST_PORT" <input >output &&
201         depacketize <output >output.raw &&
202
203         # just pick out the value of main, which avoids any protocol
204         # particulars
205         perl -lne "print \$1 if m{^(\\S+) refs/heads/main}" <output.raw >actual &&
206         git -C "$repo" rev-parse main >expect &&
207         test_cmp expect actual
208 '
209
210 test_done