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