1#!/bin/sh
2
3test_description='test smart fetching over http via http-backend'
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 content >file &&
11	git add file &&
12	git commit -m one
13'
14
15test_expect_success 'create http-accessible bare repository' '
16	mkdir "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
17	(cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
18	 git --bare init
19	) &&
20	git remote add public "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
21	git push public master:master
22'
23
24setup_askpass_helper
25
26test_expect_success 'clone http repository' '
27	cat >exp <<-\EOF &&
28	> GET /smart/repo.git/info/refs?service=git-upload-pack HTTP/1.1
29	> Accept: */*
30	> Accept-Encoding: ENCODINGS
31	> Pragma: no-cache
32	< HTTP/1.1 200 OK
33	< Pragma: no-cache
34	< Cache-Control: no-cache, max-age=0, must-revalidate
35	< Content-Type: application/x-git-upload-pack-advertisement
36	> POST /smart/repo.git/git-upload-pack HTTP/1.1
37	> Accept-Encoding: ENCODINGS
38	> Content-Type: application/x-git-upload-pack-request
39	> Accept: application/x-git-upload-pack-result
40	> Content-Length: xxx
41	< HTTP/1.1 200 OK
42	< Pragma: no-cache
43	< Cache-Control: no-cache, max-age=0, must-revalidate
44	< Content-Type: application/x-git-upload-pack-result
45	EOF
46	GIT_TRACE_CURL=true GIT_TEST_PROTOCOL_VERSION= \
47		git clone --quiet $HTTPD_URL/smart/repo.git clone 2>err &&
48	test_cmp file clone/file &&
49	tr '\''\015'\'' Q <err |
50	sed -e "
51		s/Q\$//
52		/^[*] /d
53		/^== Info:/d
54		/^=> Send header, /d
55		/^=> Send header:$/d
56		/^<= Recv header, /d
57		/^<= Recv header:$/d
58		s/=> Send header: //
59		s/= Recv header://
60		/^<= Recv data/d
61		/^=> Send data/d
62		/^$/d
63		/^< $/d
64
65		/^[^><]/{
66			s/^/> /
67		}
68
69		/^> User-Agent: /d
70		/^> Host: /d
71		/^> POST /,$ {
72			/^> Accept: [*]\\/[*]/d
73		}
74		s/^> Content-Length: .*/> Content-Length: xxx/
75		/^> 00..want /d
76		/^> 00.*done/d
77
78		/^< Server: /d
79		/^< Expires: /d
80		/^< Date: /d
81		/^< Content-Length: /d
82		/^< Transfer-Encoding: /d
83	" >actual &&
84
85	# NEEDSWORK: If the overspecification of the expected result is reduced, we
86	# might be able to run this test in all protocol versions.
87	if test -z "$GIT_TEST_PROTOCOL_VERSION"
88	then
89		sed -e "s/^> Accept-Encoding: .*/> Accept-Encoding: ENCODINGS/" \
90				actual >actual.smudged &&
91		test_cmp exp actual.smudged &&
92
93		grep "Accept-Encoding:.*gzip" actual >actual.gzip &&
94		test_line_count = 2 actual.gzip
95	fi
96'
97
98test_expect_success 'fetch changes via http' '
99	echo content >>file &&
100	git commit -a -m two &&
101	git push public &&
102	(cd clone && git pull) &&
103	test_cmp file clone/file
104'
105
106test_expect_success 'used upload-pack service' '
107	cat >exp <<-\EOF &&
108	GET  /smart/repo.git/info/refs?service=git-upload-pack HTTP/1.1 200
109	POST /smart/repo.git/git-upload-pack HTTP/1.1 200
110	GET  /smart/repo.git/info/refs?service=git-upload-pack HTTP/1.1 200
111	POST /smart/repo.git/git-upload-pack HTTP/1.1 200
112	EOF
113
114	# NEEDSWORK: If the overspecification of the expected result is reduced, we
115	# might be able to run this test in all protocol versions.
116	if test -z "$GIT_TEST_PROTOCOL_VERSION"
117	then
118		check_access_log exp
119	fi
120'
121
122test_expect_success 'follow redirects (301)' '
123	git clone $HTTPD_URL/smart-redir-perm/repo.git --quiet repo-p
124'
125
126test_expect_success 'follow redirects (302)' '
127	git clone $HTTPD_URL/smart-redir-temp/repo.git --quiet repo-t
128'
129
130test_expect_success 'redirects re-root further requests' '
131	git clone $HTTPD_URL/smart-redir-limited/repo.git repo-redir-limited
132'
133
134test_expect_success 're-rooting dies on insane schemes' '
135	test_must_fail git clone $HTTPD_URL/insane-redir/repo.git insane
136'
137
138test_expect_success 'clone from password-protected repository' '
139	echo two >expect &&
140	set_askpass user@host pass@host &&
141	git clone --bare "$HTTPD_URL/auth/smart/repo.git" smart-auth &&
142	expect_askpass both user@host &&
143	git --git-dir=smart-auth log -1 --format=%s >actual &&
144	test_cmp expect actual
145'
146
147test_expect_success 'clone from auth-only-for-push repository' '
148	echo two >expect &&
149	set_askpass wrong &&
150	git clone --bare "$HTTPD_URL/auth-push/smart/repo.git" smart-noauth &&
151	expect_askpass none &&
152	git --git-dir=smart-noauth log -1 --format=%s >actual &&
153	test_cmp expect actual
154'
155
156test_expect_success 'clone from auth-only-for-objects repository' '
157	echo two >expect &&
158	set_askpass user@host pass@host &&
159	git clone --bare "$HTTPD_URL/auth-fetch/smart/repo.git" half-auth &&
160	expect_askpass both user@host &&
161	git --git-dir=half-auth log -1 --format=%s >actual &&
162	test_cmp expect actual
163'
164
165test_expect_success 'no-op half-auth fetch does not require a password' '
166	set_askpass wrong &&
167
168	# NEEDSWORK: When using HTTP(S), protocol v0 supports a "half-auth"
169	# configuration with authentication required only when downloading
170	# objects and not refs, by having the HTTP server only require
171	# authentication for the "git-upload-pack" path and not "info/refs".
172	# This is not possible with protocol v2, since both objects and refs
173	# are obtained from the "git-upload-pack" path. A solution to this is
174	# to teach the server and client to be able to inline ls-refs requests
175	# as an Extra Parameter (see pack-protocol.txt), so that "info/refs"
176	# can serve refs, just like it does in protocol v0.
177	GIT_TEST_PROTOCOL_VERSION=0 git --git-dir=half-auth fetch &&
178	expect_askpass none
179'
180
181test_expect_success 'redirects send auth to new location' '
182	set_askpass user@host pass@host &&
183	git -c credential.useHttpPath=true \
184	  clone $HTTPD_URL/smart-redir-auth/repo.git repo-redir-auth &&
185	expect_askpass both user@host auth/smart/repo.git
186'
187
188test_expect_success 'disable dumb http on server' '
189	git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
190		config http.getanyfile false
191'
192
193test_expect_success 'GIT_SMART_HTTP can disable smart http' '
194	(GIT_SMART_HTTP=0 &&
195	 export GIT_SMART_HTTP &&
196	 cd clone &&
197	 test_must_fail git fetch)
198'
199
200test_expect_success 'invalid Content-Type rejected' '
201	test_must_fail git clone $HTTPD_URL/broken_smart/repo.git 2>actual &&
202	test_i18ngrep "not valid:" actual
203'
204
205test_expect_success 'create namespaced refs' '
206	test_commit namespaced &&
207	git push public HEAD:refs/namespaces/ns/refs/heads/master &&
208	git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
209		symbolic-ref refs/namespaces/ns/HEAD refs/namespaces/ns/refs/heads/master
210'
211
212test_expect_success 'smart clone respects namespace' '
213	git clone "$HTTPD_URL/smart_namespace/repo.git" ns-smart &&
214	echo namespaced >expect &&
215	git --git-dir=ns-smart/.git log -1 --format=%s >actual &&
216	test_cmp expect actual
217'
218
219test_expect_success 'dumb clone via http-backend respects namespace' '
220	git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
221		config http.getanyfile true &&
222	GIT_SMART_HTTP=0 git clone \
223		"$HTTPD_URL/smart_namespace/repo.git" ns-dumb &&
224	echo namespaced >expect &&
225	git --git-dir=ns-dumb/.git log -1 --format=%s >actual &&
226	test_cmp expect actual
227'
228
229test_expect_success 'cookies stored in http.cookiefile when http.savecookies set' '
230	cat >cookies.txt <<-\EOF &&
231	127.0.0.1	FALSE	/smart_cookies/	FALSE	0	othername	othervalue
232	EOF
233	sort >expect_cookies.txt <<-\EOF &&
234
235	127.0.0.1	FALSE	/smart_cookies/	FALSE	0	othername	othervalue
236	127.0.0.1	FALSE	/smart_cookies/repo.git/info/	FALSE	0	name	value
237	EOF
238	git config http.cookiefile cookies.txt &&
239	git config http.savecookies true &&
240	git ls-remote $HTTPD_URL/smart_cookies/repo.git master &&
241
242	# NEEDSWORK: If the overspecification of the expected result is reduced, we
243	# might be able to run this test in all protocol versions.
244	if test -z "$GIT_TEST_PROTOCOL_VERSION"
245	then
246		tail -3 cookies.txt | sort >cookies_tail.txt &&
247		test_cmp expect_cookies.txt cookies_tail.txt
248	fi
249'
250
251test_expect_success 'transfer.hiderefs works over smart-http' '
252	test_commit hidden &&
253	test_commit visible &&
254	git push public HEAD^:refs/heads/a HEAD:refs/heads/b &&
255	git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
256		config transfer.hiderefs refs/heads/a &&
257	git clone --bare "$HTTPD_URL/smart/repo.git" hidden.git &&
258	test_must_fail git -C hidden.git rev-parse --verify a &&
259	git -C hidden.git rev-parse --verify b
260'
261
262# create an arbitrary number of tags, numbered from tag-$1 to tag-$2
263create_tags () {
264	rm -f marks &&
265	for i in $(test_seq "$1" "$2")
266	do
267		# don't use here-doc, because it requires a process
268		# per loop iteration
269		echo "commit refs/heads/too-many-refs-$1" &&
270		echo "mark :$i" &&
271		echo "committer git <git@example.com> $i +0000" &&
272		echo "data 0" &&
273		echo "M 644 inline bla.txt" &&
274		echo "data 4" &&
275		echo "bla" &&
276		# make every commit dangling by always
277		# rewinding the branch after each commit
278		echo "reset refs/heads/too-many-refs-$1" &&
279		echo "from :$1"
280	done | git fast-import --export-marks=marks &&
281
282	# now assign tags to all the dangling commits we created above
283	tag=$(perl -e "print \"bla\" x 30") &&
284	sed -e "s|^:\([^ ]*\) \(.*\)$|\2 refs/tags/$tag-\1|" <marks >>packed-refs
285}
286
287test_expect_success 'create 2,000 tags in the repo' '
288	(
289		cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
290		create_tags 1 2000
291	)
292'
293
294test_expect_success CMDLINE_LIMIT \
295	'clone the 2,000 tag repo to check OS command line overflow' '
296	run_with_limited_cmdline git clone $HTTPD_URL/smart/repo.git too-many-refs &&
297	(
298		cd too-many-refs &&
299		git for-each-ref refs/tags >actual &&
300		test_line_count = 2000 actual
301	)
302'
303
304test_expect_success 'large fetch-pack requests can be sent using chunked encoding' '
305	GIT_TRACE_CURL=true git -c http.postbuffer=65536 \
306		clone --bare "$HTTPD_URL/smart/repo.git" split.git 2>err &&
307	grep "^=> Send header: Transfer-Encoding: chunked" err
308'
309
310test_expect_success 'test allowreachablesha1inwant' '
311	test_when_finished "rm -rf test_reachable.git" &&
312	server="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
313	master_sha=$(git -C "$server" rev-parse refs/heads/master) &&
314	git -C "$server" config uploadpack.allowreachablesha1inwant 1 &&
315
316	git init --bare test_reachable.git &&
317	git -C test_reachable.git remote add origin "$HTTPD_URL/smart/repo.git" &&
318	git -C test_reachable.git fetch origin "$master_sha"
319'
320
321test_expect_success 'test allowreachablesha1inwant with unreachable' '
322	test_when_finished "rm -rf test_reachable.git; git reset --hard $(git rev-parse HEAD)" &&
323
324	#create unreachable sha
325	echo content >file2 &&
326	git add file2 &&
327	git commit -m two &&
328	git push public HEAD:refs/heads/doomed &&
329	git push public :refs/heads/doomed &&
330
331	server="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
332	master_sha=$(git -C "$server" rev-parse refs/heads/master) &&
333	git -C "$server" config uploadpack.allowreachablesha1inwant 1 &&
334
335	git init --bare test_reachable.git &&
336	git -C test_reachable.git remote add origin "$HTTPD_URL/smart/repo.git" &&
337	# Some protocol versions (e.g. 2) support fetching
338	# unadvertised objects, so restrict this test to v0.
339	test_must_fail env GIT_TEST_PROTOCOL_VERSION= \
340		git -C test_reachable.git fetch origin "$(git rev-parse HEAD)"
341'
342
343test_expect_success 'test allowanysha1inwant with unreachable' '
344	test_when_finished "rm -rf test_reachable.git; git reset --hard $(git rev-parse HEAD)" &&
345
346	#create unreachable sha
347	echo content >file2 &&
348	git add file2 &&
349	git commit -m two &&
350	git push public HEAD:refs/heads/doomed &&
351	git push public :refs/heads/doomed &&
352
353	server="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
354	master_sha=$(git -C "$server" rev-parse refs/heads/master) &&
355	git -C "$server" config uploadpack.allowreachablesha1inwant 1 &&
356
357	git init --bare test_reachable.git &&
358	git -C test_reachable.git remote add origin "$HTTPD_URL/smart/repo.git" &&
359	# Some protocol versions (e.g. 2) support fetching
360	# unadvertised objects, so restrict this test to v0.
361	test_must_fail env GIT_TEST_PROTOCOL_VERSION= \
362		git -C test_reachable.git fetch origin "$(git rev-parse HEAD)" &&
363
364	git -C "$server" config uploadpack.allowanysha1inwant 1 &&
365	git -C test_reachable.git fetch origin "$(git rev-parse HEAD)"
366'
367
368test_expect_success EXPENSIVE 'http can handle enormous ref negotiation' '
369	(
370		cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
371		create_tags 2001 50000
372	) &&
373	git -C too-many-refs fetch -q --tags &&
374	(
375		cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
376		create_tags 50001 100000
377	) &&
378	git -C too-many-refs fetch -q --tags &&
379	git -C too-many-refs for-each-ref refs/tags >tags &&
380	test_line_count = 100000 tags
381'
382
383test_expect_success 'custom http headers' '
384	test_must_fail git -c http.extraheader="x-magic-two: cadabra" \
385		fetch "$HTTPD_URL/smart_headers/repo.git" &&
386	git -c http.extraheader="x-magic-one: abra" \
387	    -c http.extraheader="x-magic-two: cadabra" \
388	    fetch "$HTTPD_URL/smart_headers/repo.git" &&
389	git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
390	git config -f .gitmodules submodule.sub.path sub &&
391	git config -f .gitmodules submodule.sub.url \
392		"$HTTPD_URL/smart_headers/repo.git" &&
393	git submodule init sub &&
394	test_must_fail git submodule update sub &&
395	git -c http.extraheader="x-magic-one: abra" \
396	    -c http.extraheader="x-magic-two: cadabra" \
397		submodule update sub
398'
399
400test_expect_success 'using fetch command in remote-curl updates refs' '
401	SERVER="$HTTPD_DOCUMENT_ROOT_PATH/twobranch" &&
402	rm -rf "$SERVER" client &&
403
404	git init "$SERVER" &&
405	test_commit -C "$SERVER" foo &&
406	git -C "$SERVER" update-ref refs/heads/anotherbranch foo &&
407
408	git clone $HTTPD_URL/smart/twobranch client &&
409
410	test_commit -C "$SERVER" bar &&
411	git -C client -c protocol.version=0 fetch &&
412
413	git -C "$SERVER" rev-parse master >expect &&
414	git -C client rev-parse origin/master >actual &&
415	test_cmp expect actual
416'
417
418test_expect_success 'fetch by SHA-1 without tag following' '
419	SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
420	rm -rf "$SERVER" client &&
421
422	git init "$SERVER" &&
423	test_commit -C "$SERVER" foo &&
424
425	git clone $HTTPD_URL/smart/server client &&
426
427	test_commit -C "$SERVER" bar &&
428	git -C "$SERVER" rev-parse bar >bar_hash &&
429	git -C client -c protocol.version=0 fetch \
430		--no-tags origin $(cat bar_hash)
431'
432
433test_expect_success 'GIT_REDACT_COOKIES redacts cookies' '
434	rm -rf clone &&
435	echo "Set-Cookie: Foo=1" >cookies &&
436	echo "Set-Cookie: Bar=2" >>cookies &&
437	GIT_TRACE_CURL=true GIT_REDACT_COOKIES=Bar,Baz \
438		git -c "http.cookieFile=$(pwd)/cookies" clone \
439		$HTTPD_URL/smart/repo.git clone 2>err &&
440	grep "Cookie:.*Foo=1" err &&
441	grep "Cookie:.*Bar=<redacted>" err &&
442	! grep "Cookie:.*Bar=2" err
443'
444
445test_expect_success 'GIT_REDACT_COOKIES handles empty values' '
446	rm -rf clone &&
447	echo "Set-Cookie: Foo=" >cookies &&
448	GIT_TRACE_CURL=true GIT_REDACT_COOKIES=Foo \
449		git -c "http.cookieFile=$(pwd)/cookies" clone \
450		$HTTPD_URL/smart/repo.git clone 2>err &&
451	grep "Cookie:.*Foo=<redacted>" err
452'
453
454test_expect_success 'GIT_TRACE_CURL_NO_DATA prevents data from being traced' '
455	rm -rf clone &&
456	GIT_TRACE_CURL=true \
457		git clone $HTTPD_URL/smart/repo.git clone 2>err &&
458	grep "=> Send data" err &&
459
460	rm -rf clone &&
461	GIT_TRACE_CURL=true GIT_TRACE_CURL_NO_DATA=1 \
462		git clone $HTTPD_URL/smart/repo.git clone 2>err &&
463	! grep "=> Send data" err
464'
465
466test_expect_success 'server-side error detected' '
467	test_must_fail git clone $HTTPD_URL/error_smart/repo.git 2>actual &&
468	test_i18ngrep "server-side error" actual
469'
470
471test_done
472