1#!/bin/sh
2
3test_description='test dumb fetching over http via static file'
4. ./test-lib.sh
5. "$TEST_DIRECTORY"/lib-httpd.sh
6start_httpd
7
8test_expect_success 'setup repository' '
9 git config push.default matching &&
10 echo content1 >file &&
11 git add file &&
12 git commit -m one &&
13 echo content2 >file &&
14 git add file &&
15 git commit -m two
16'
17
18test_expect_success 'create http-accessible bare repository with loose objects' '
19 cp -R .git "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
20 (cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
21 git config core.bare true &&
22 mkdir -p hooks &&
23 echo "exec git update-server-info" >hooks/post-update &&
24 chmod +x hooks/post-update &&
25 hooks/post-update
26 ) &&
27 git remote add public "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
28 git push public master:master
29'
30
31test_expect_success 'clone http repository' '
32 git clone $HTTPD_URL/dumb/repo.git clone-tmpl &&
33 cp -R clone-tmpl clone &&
34 test_cmp file clone/file
35'
36
37test_expect_success 'create password-protected repository' '
38 mkdir -p "$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb/" &&
39 cp -Rf "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
40 "$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb/repo.git"
41'
42
43setup_askpass_helper
44
45test_expect_success 'cloning password-protected repository can fail' '
46 set_askpass wrong &&
47 test_must_fail git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-fail &&
48 expect_askpass both wrong
49'
50
51test_expect_success 'http auth can use user/pass in URL' '
52 set_askpass wrong &&
53 git clone "$HTTPD_URL_USER_PASS/auth/dumb/repo.git" clone-auth-none &&
54 expect_askpass none
55'
56
57test_expect_success 'http auth can use just user in URL' '
58 set_askpass wrong pass@host &&
59 git clone "$HTTPD_URL_USER/auth/dumb/repo.git" clone-auth-pass &&
60 expect_askpass pass user@host
61'
62
63test_expect_success 'http auth can request both user and pass' '
64 set_askpass user@host pass@host &&
65 git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-both &&
66 expect_askpass both user@host
67'
68
69test_expect_success 'http auth respects credential helper config' '
70 test_config_global credential.helper "!f() {
71 cat >/dev/null
72 echo username=user@host
73 echo password=pass@host
74 }; f" &&
75 set_askpass wrong &&
76 git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-helper &&
77 expect_askpass none
78'
79
80test_expect_success 'http auth can get username from config' '
81 test_config_global "credential.$HTTPD_URL.username" user@host &&
82 set_askpass wrong pass@host &&
83 git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-user &&
84 expect_askpass pass user@host
85'
86
87test_expect_success 'configured username does not override URL' '
88 test_config_global "credential.$HTTPD_URL.username" wrong &&
89 set_askpass wrong pass@host &&
90 git clone "$HTTPD_URL_USER/auth/dumb/repo.git" clone-auth-user2 &&
91 expect_askpass pass user@host
92'
93
94test_expect_success 'cmdline credential config passes into submodules' '
95 git init super &&
96 set_askpass user@host pass@host &&
97 (
98 cd super &&
99 git submodule add "$HTTPD_URL/auth/dumb/repo.git" sub &&
100 git commit -m "add submodule"
101 ) &&
102 set_askpass wrong pass@host &&
103 test_must_fail git clone --recursive super super-clone &&
104 rm -rf super-clone &&
105 set_askpass wrong pass@host &&
106 git -c "credential.$HTTP_URL.username=user@host" \
107 clone --recursive super super-clone &&
108 expect_askpass pass user@host
109'
110
111test_expect_success 'fetch changes via http' '
112 echo content >>file &&
113 git commit -a -m two &&
114 git push public &&
115 (cd clone && git pull) &&
116 test_cmp file clone/file
117'
118
119test_expect_success 'fetch changes via manual http-fetch' '
120 cp -R clone-tmpl clone2 &&
121
122 HEAD=$(git rev-parse --verify HEAD) &&
123 (cd clone2 &&
124 git http-fetch -a -w heads/master-new $HEAD $(git config remote.origin.url) &&
125 git checkout master-new &&
126 test $HEAD = $(git rev-parse --verify HEAD)) &&
127 test_cmp file clone2/file
128'
129
130test_expect_success 'http remote detects correct HEAD' '
131 git push public master:other &&
132 (cd clone &&
133 git remote set-head origin -d &&
134 git remote set-head origin -a &&
135 git symbolic-ref refs/remotes/origin/HEAD > output &&
136 echo refs/remotes/origin/master > expect &&
137 test_cmp expect output
138 )
139'
140
141test_expect_success 'fetch packed objects' '
142 cp -R "$HTTPD_DOCUMENT_ROOT_PATH"/repo.git "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git &&
143 (cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git &&
144 git --bare repack -a -d
145 ) &&
146 git clone $HTTPD_URL/dumb/repo_pack.git
147'
148
149test_expect_success 'fetch notices corrupt pack' '
150 cp -R "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad1.git &&
151 (cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad1.git &&
152 p=$(ls objects/pack/pack-*.pack) &&
153 chmod u+w $p &&
154 printf %0256d 0 | dd of=$p bs=256 count=1 seek=1 conv=notrunc
155 ) &&
156 mkdir repo_bad1.git &&
157 (cd repo_bad1.git &&
158 git --bare init &&
159 test_must_fail git --bare fetch $HTTPD_URL/dumb/repo_bad1.git &&
160 test 0 = $(ls objects/pack/pack-*.pack | wc -l)
161 )
162'
163
164test_expect_success 'fetch notices corrupt idx' '
165 cp -R "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad2.git &&
166 (cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad2.git &&
167 p=$(ls objects/pack/pack-*.idx) &&
168 chmod u+w $p &&
169 printf %0256d 0 | dd of=$p bs=256 count=1 seek=1 conv=notrunc
170 ) &&
171 mkdir repo_bad2.git &&
172 (cd repo_bad2.git &&
173 git --bare init &&
174 test_must_fail git --bare fetch $HTTPD_URL/dumb/repo_bad2.git &&
175 test 0 = $(ls objects/pack | wc -l)
176 )
177'
178
179test_expect_success 'fetch can handle previously-fetched .idx files' '
180 git checkout --orphan branch1 &&
181 echo base >file &&
182 git add file &&
183 git commit -m base &&
184 git --bare init "$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git &&
185 git push "$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git branch1 &&
186 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git repack -d &&
187 git checkout -b branch2 branch1 &&
188 echo b2 >>file &&
189 git commit -a -m b2 &&
190 git push "$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git branch2 &&
191 git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git repack -d &&
192 git --bare init clone_packed_branches.git &&
193 git --git-dir=clone_packed_branches.git fetch "$HTTPD_URL"/dumb/repo_packed_branches.git branch1:branch1 &&
194 git --git-dir=clone_packed_branches.git fetch "$HTTPD_URL"/dumb/repo_packed_branches.git branch2:branch2
195'
196
197test_expect_success 'did not use upload-pack service' '
198 test_might_fail grep '/git-upload-pack' <"$HTTPD_ROOT_PATH"/access.log >act &&
199 : >exp &&
200 test_cmp exp act
201'
202
203test_expect_success 'git client shows text/plain errors' '
204 test_must_fail git clone "$HTTPD_URL/error/text" 2>stderr &&
205 grep "this is the error message" stderr
206'
207
208test_expect_success 'git client does not show html errors' '
209 test_must_fail git clone "$HTTPD_URL/error/html" 2>stderr &&
210 ! grep "this is the error message" stderr
211'
212
213test_expect_success 'git client shows text/plain with a charset' '
214 test_must_fail git clone "$HTTPD_URL/error/charset" 2>stderr &&
215 grep "this is the error message" stderr
216'
217
218test_expect_success 'http error messages are reencoded' '
219 test_must_fail git clone "$HTTPD_URL/error/utf16" 2>stderr &&
220 grep "this is the error message" stderr
221'
222
223test_expect_success 'reencoding is robust to whitespace oddities' '
224 test_must_fail git clone "$HTTPD_URL/error/odd-spacing" 2>stderr &&
225 grep "this is the error message" stderr
226'
227
228check_language () {
229 case "$2" in
230 '')
231 >expect
232 ;;
233 ?*)
234 echo "Accept-Language: $1" >expect
235 ;;
236 esac &&
237 GIT_CURL_VERBOSE=1 \
238 LANGUAGE=$2 \
239 git ls-remote "$HTTPD_URL/dumb/repo.git" >output 2>&1 &&
240 tr -d '\015' <output |
241 sort -u |
242 sed -ne '/^Accept-Language:/ p' >actual &&
243 test_cmp expect actual
244}
245
246test_expect_success 'git client sends Accept-Language based on LANGUAGE' '
247 check_language "ko-KR, *;q=0.9" ko_KR.UTF-8'
248
249test_expect_success 'git client sends Accept-Language correctly with unordinary LANGUAGE' '
250 check_language "ko-KR, *;q=0.9" "ko_KR:" &&
251 check_language "ko-KR, en-US;q=0.9, *;q=0.8" "ko_KR::en_US" &&
252 check_language "ko-KR, *;q=0.9" ":::ko_KR" &&
253 check_language "ko-KR, en-US;q=0.9, *;q=0.8" "ko_KR!!:en_US" &&
254 check_language "ko-KR, ja-JP;q=0.9, *;q=0.8" "ko_KR en_US:ja_JP"'
255
256test_expect_success 'git client sends Accept-Language with many preferred languages' '
257 check_language "ko-KR, en-US;q=0.9, fr-CA;q=0.8, de;q=0.7, sr;q=0.6, \
258ja;q=0.5, zh;q=0.4, sv;q=0.3, pt;q=0.2, *;q=0.1" \
259 ko_KR.EUC-KR:en_US.UTF-8:fr_CA:de.UTF-8@euro:sr@latin:ja:zh:sv:pt &&
260 check_language "ko-KR, en-US;q=0.99, fr-CA;q=0.98, de;q=0.97, sr;q=0.96, \
261ja;q=0.95, zh;q=0.94, sv;q=0.93, pt;q=0.92, nb;q=0.91, *;q=0.90" \
262 ko_KR.EUC-KR:en_US.UTF-8:fr_CA:de.UTF-8@euro:sr@latin:ja:zh:sv:pt:nb
263'
264
265test_expect_success 'git client does not send an empty Accept-Language' '
266 GIT_CURL_VERBOSE=1 LANGUAGE= git ls-remote "$HTTPD_URL/dumb/repo.git" 2>stderr &&
267 ! grep "^Accept-Language:" stderr
268'
269
270stop_httpd
271test_done