1#!/bin/sh
23
test_description='test fetching over git protocol'
4. ./test-lib.sh
56
. "$TEST_DIRECTORY"/lib-git-daemon.sh
7start_git_daemon
89
test_expect_success 'setup repository' '
10git config push.default matching &&
11echo content >file &&
12git add file &&
13git commit -m one
14'
1516
test_expect_success 'create git-accessible bare repository' '
17mkdir "$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git" &&
18(cd "$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git" &&
19git --bare init &&
20: >git-daemon-export-ok
21) &&
22git remote add public "$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git" &&
23git push public master:master
24'
2526
test_expect_success 'clone git repository' '
27git clone "$GIT_DAEMON_URL/repo.git" clone &&
28test_cmp file clone/file
29'
3031
test_expect_success 'fetch changes via git protocol' '
32echo content >>file &&
33git commit -a -m two &&
34git push public &&
35(cd clone && git pull) &&
36test_cmp file clone/file
37'
3839
test_expect_success 'remote detects correct HEAD' '
40git push public master:other &&
41(cd clone &&
42git remote set-head -d origin &&
43git remote set-head -a origin &&
44git symbolic-ref refs/remotes/origin/HEAD > output &&
45echo refs/remotes/origin/master > expect &&
46test_cmp expect output
47)
48'
4950
test_expect_success 'prepare pack objects' '
51cp -R "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo.git "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_pack.git &&
52(cd "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_pack.git &&
53git --bare repack -a -d
54)
55'
5657
test_expect_success 'fetch notices corrupt pack' '
58cp -R "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_pack.git "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_bad1.git &&
59(cd "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_bad1.git &&
60p=`ls objects/pack/pack-*.pack` &&
61chmod u+w $p &&
62printf %0256d 0 | dd of=$p bs=256 count=1 seek=1 conv=notrunc
63) &&
64mkdir repo_bad1.git &&
65(cd repo_bad1.git &&
66git --bare init &&
67test_must_fail git --bare fetch "$GIT_DAEMON_URL/repo_bad1.git" &&
68test 0 = `ls objects/pack/pack-*.pack | wc -l`
69)
70'
7172
test_expect_success 'fetch notices corrupt idx' '
73cp -R "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_pack.git "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_bad2.git &&
74(cd "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_bad2.git &&
75p=`ls objects/pack/pack-*.idx` &&
76chmod u+w $p &&
77printf %0256d 0 | dd of=$p bs=256 count=1 seek=1 conv=notrunc
78) &&
79mkdir repo_bad2.git &&
80(cd repo_bad2.git &&
81git --bare init &&
82test_must_fail git --bare fetch "$GIT_DAEMON_URL/repo_bad2.git" &&
83test 0 = `ls objects/pack | wc -l`
84)
85'
8687
test_remote_error()
88{
89do_export=YesPlease
90while test $# -gt 0
91do
92case $1 in
93-x)
94shift
95chmod -x "$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git"
96;;
97-n)
98shift
99do_export=
100;;
101*)
102break
103esac
104done
105106
msg=$1
107shift
108cmd=$1
109shift
110repo=$1
111shift || error "invalid number of arguments"
112113
if test -x "$GIT_DAEMON_DOCUMENT_ROOT_PATH/$repo"
114then
115if test -n "$do_export"
116then
117: >"$GIT_DAEMON_DOCUMENT_ROOT_PATH/$repo/git-daemon-export-ok"
118else
119rm -f "$GIT_DAEMON_DOCUMENT_ROOT_PATH/$repo/git-daemon-export-ok"
120fi
121fi
122123
test_must_fail git "$cmd" "$GIT_DAEMON_URL/$repo" "$@" 2>output &&
124test_i18ngrep "fatal: remote error: $msg: /$repo" output &&
125ret=$?
126chmod +x "$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git"
127(exit $ret)
128}
129130
msg="access denied or repository not exported"
131test_expect_success 'clone non-existent' "test_remote_error '$msg' clone nowhere.git "
132test_expect_success 'push disabled' "test_remote_error '$msg' push repo.git master"
133test_expect_success 'read access denied' "test_remote_error -x '$msg' fetch repo.git "
134test_expect_success 'not exported' "test_remote_error -n '$msg' fetch repo.git "
135136
stop_git_daemon
137start_git_daemon --informative-errors
138139
test_expect_success 'clone non-existent' "test_remote_error 'no such repository' clone nowhere.git "
140test_expect_success 'push disabled' "test_remote_error 'service not enabled' push repo.git master"
141test_expect_success 'read access denied' "test_remote_error -x 'no such repository' fetch repo.git "
142test_expect_success 'not exported' "test_remote_error -n 'repository not exported' fetch repo.git "
143144
stop_git_daemon
145start_git_daemon --interpolated-path="$GIT_DAEMON_DOCUMENT_ROOT_PATH/%H%D"
146147
test_expect_success 'access repo via interpolated hostname' '
148repo="$GIT_DAEMON_DOCUMENT_ROOT_PATH/localhost/interp.git" &&
149git init --bare "$repo" &&
150git push "$repo" HEAD &&
151>"$repo"/git-daemon-export-ok &&
152rm -rf tmp.git &&
153GIT_OVERRIDE_VIRTUAL_HOST=localhost \
154git clone --bare "$GIT_DAEMON_URL/interp.git" tmp.git &&
155rm -rf tmp.git &&
156GIT_OVERRIDE_VIRTUAL_HOST=LOCALHOST \
157git clone --bare "$GIT_DAEMON_URL/interp.git" tmp.git
158'
159160
stop_git_daemon
161test_done