tests: mark tests relying on the current default for `init.defaultBranch`
[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=master
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 master:master
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 master: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/master > 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_remote_error()
110 {
111         do_export=YesPlease
112         while test $# -gt 0
113         do
114                 case $1 in
115                 -x)
116                         shift
117                         chmod -x "$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git"
118                         ;;
119                 -n)
120                         shift
121                         do_export=
122                         ;;
123                 *)
124                         break
125                 esac
126         done
127
128         msg=$1
129         shift
130         cmd=$1
131         shift
132         repo=$1
133         shift || error "invalid number of arguments"
134
135         if test -x "$GIT_DAEMON_DOCUMENT_ROOT_PATH/$repo"
136         then
137                 if test -n "$do_export"
138                 then
139                         : >"$GIT_DAEMON_DOCUMENT_ROOT_PATH/$repo/git-daemon-export-ok"
140                 else
141                         rm -f "$GIT_DAEMON_DOCUMENT_ROOT_PATH/$repo/git-daemon-export-ok"
142                 fi
143         fi
144
145         test_must_fail git "$cmd" "$GIT_DAEMON_URL/$repo" "$@" 2>output &&
146         test_i18ngrep "fatal: remote error: $msg: /$repo" output &&
147         ret=$?
148         chmod +x "$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git"
149         (exit $ret)
150 }
151
152 msg="access denied or repository not exported"
153 test_expect_success 'clone non-existent' "test_remote_error    '$msg' clone nowhere.git"
154 test_expect_success 'push disabled'      "test_remote_error    '$msg' push  repo.git master"
155 test_expect_success 'read access denied' "test_remote_error -x '$msg' fetch repo.git"
156 test_expect_success 'not exported'       "test_remote_error -n '$msg' fetch repo.git"
157
158 stop_git_daemon
159 start_git_daemon --informative-errors
160
161 test_expect_success 'clone non-existent' "test_remote_error    'no such repository'      clone nowhere.git"
162 test_expect_success 'push disabled'      "test_remote_error    'service not enabled'     push  repo.git master"
163 test_expect_success 'read access denied' "test_remote_error -x 'no such repository'      fetch repo.git"
164 test_expect_success 'not exported'       "test_remote_error -n 'repository not exported' fetch repo.git"
165
166 stop_git_daemon
167 start_git_daemon --interpolated-path="$GIT_DAEMON_DOCUMENT_ROOT_PATH/%H%D"
168
169 test_expect_success 'access repo via interpolated hostname' '
170         repo="$GIT_DAEMON_DOCUMENT_ROOT_PATH/localhost/interp.git" &&
171         git init --bare "$repo" &&
172         git push "$repo" HEAD &&
173         >"$repo"/git-daemon-export-ok &&
174         GIT_OVERRIDE_VIRTUAL_HOST=localhost \
175                 git ls-remote "$GIT_DAEMON_URL/interp.git" &&
176         GIT_OVERRIDE_VIRTUAL_HOST=LOCALHOST \
177                 git ls-remote "$GIT_DAEMON_URL/interp.git"
178 '
179
180 test_expect_success 'hostname cannot break out of directory' '
181         repo="$GIT_DAEMON_DOCUMENT_ROOT_PATH/../escape.git" &&
182         git init --bare "$repo" &&
183         git push "$repo" HEAD &&
184         >"$repo"/git-daemon-export-ok &&
185         test_must_fail \
186                 env GIT_OVERRIDE_VIRTUAL_HOST=.. \
187                 git ls-remote "$GIT_DAEMON_URL/escape.git"
188 '
189
190 test_expect_success FAKENC 'hostname interpolation works after LF-stripping' '
191         {
192                 printf "git-upload-pack /interp.git\n\0host=localhost" | packetize
193                 printf "0000"
194         } >input &&
195         fake_nc "$GIT_DAEMON_HOST_PORT" <input >output &&
196         depacketize <output >output.raw &&
197
198         # just pick out the value of master, which avoids any protocol
199         # particulars
200         perl -lne "print \$1 if m{^(\\S+) refs/heads/master}" <output.raw >actual &&
201         git -C "$repo" rev-parse master >expect &&
202         test_cmp expect actual
203 '
204
205 test_done