1#!/bin/sh
23
test_description='test git-http-backend respects CONTENT_LENGTH'
4. ./test-lib.sh
56
test_lazy_prereq GZIP 'gzip --version'
78
verify_http_result() {
9# some fatal errors still produce status 200
10# so check if there is the error message
11if grep 'fatal:' act.err
12then
13return 1
14fi
1516
if ! grep "Status" act.out >act
17then
18printf "Status: 200 OK\r\n" >act
19fi
20printf "Status: $1\r\n" >exp &&
21test_cmp exp act
22}
2324
test_http_env() {
25handler_type="$1"
26request_body="$2"
27shift
28env \
29CONTENT_TYPE="application/x-git-$handler_type-pack-request" \
30QUERY_STRING="/repo.git/git-$handler_type-pack" \
31PATH_TRANSLATED="$PWD/.git/git-$handler_type-pack" \
32GIT_HTTP_EXPORT_ALL=TRUE \
33REQUEST_METHOD=POST \
34"$TEST_DIRECTORY"/t5562/invoke-with-content-length.pl \
35"$request_body" git http-backend >act.out 2>act.err
36}
3738
ssize_b100dots() {
39# hardcoded ((size_t) SSIZE_MAX) + 1
40case "$(build_option sizeof-size_t)" in
418) echo 9223372036854775808;;
424) echo 2147483648;;
43*) die "Unexpected ssize_t size: $(build_option sizeof-size_t)";;
44esac
45}
4647
test_expect_success 'setup' '
48HTTP_CONTENT_ENCODING="identity" &&
49export HTTP_CONTENT_ENCODING &&
50git config http.receivepack true &&
51test_commit c0 &&
52test_commit c1 &&
53hash_head=$(git rev-parse HEAD) &&
54hash_prev=$(git rev-parse HEAD~1) &&
55printf "want %s" "$hash_head" | packetize >fetch_body &&
56printf 0000 >>fetch_body &&
57printf "have %s" "$hash_prev" | packetize >>fetch_body &&
58printf done | packetize >>fetch_body &&
59test_copy_bytes 10 <fetch_body >fetch_body.trunc &&
60hash_next=$(git commit-tree -p HEAD -m next HEAD^{tree}) &&
61printf "%s %s refs/heads/newbranch\\0report-status\\n" "$_z40" "$hash_next" | packetize >push_body &&
62printf 0000 >>push_body &&
63echo "$hash_next" | git pack-objects --stdout >>push_body &&
64test_copy_bytes 10 <push_body >push_body.trunc &&
65: >empty_body
66'
6768
test_expect_success GZIP 'setup, compression related' '
69gzip -c fetch_body >fetch_body.gz &&
70test_copy_bytes 10 <fetch_body.gz >fetch_body.gz.trunc &&
71gzip -c push_body >push_body.gz &&
72test_copy_bytes 10 <push_body.gz >push_body.gz.trunc
73'
7475
test_expect_success 'fetch plain' '
76test_http_env upload fetch_body &&
77verify_http_result "200 OK"
78'
7980
test_expect_success 'fetch plain truncated' '
81test_http_env upload fetch_body.trunc &&
82! verify_http_result "200 OK"
83'
8485
test_expect_success 'fetch plain empty' '
86test_http_env upload empty_body &&
87! verify_http_result "200 OK"
88'
8990
test_expect_success GZIP 'fetch gzipped' '
91test_env HTTP_CONTENT_ENCODING="gzip" test_http_env upload fetch_body.gz &&
92verify_http_result "200 OK"
93'
9495
test_expect_success GZIP 'fetch gzipped truncated' '
96test_env HTTP_CONTENT_ENCODING="gzip" test_http_env upload fetch_body.gz.trunc &&
97! verify_http_result "200 OK"
98'
99100
test_expect_success GZIP 'fetch gzipped empty' '
101test_env HTTP_CONTENT_ENCODING="gzip" test_http_env upload empty_body &&
102! verify_http_result "200 OK"
103'
104105
test_expect_success GZIP 'push plain' '
106test_when_finished "git branch -D newbranch" &&
107test_http_env receive push_body &&
108verify_http_result "200 OK" &&
109git rev-parse newbranch >act.head &&
110echo "$hash_next" >exp.head &&
111test_cmp act.head exp.head
112'
113114
test_expect_success 'push plain truncated' '
115test_http_env receive push_body.trunc &&
116! verify_http_result "200 OK"
117'
118119
test_expect_success 'push plain empty' '
120test_http_env receive empty_body &&
121! verify_http_result "200 OK"
122'
123124
test_expect_success GZIP 'push gzipped' '
125test_when_finished "git branch -D newbranch" &&
126test_env HTTP_CONTENT_ENCODING="gzip" test_http_env receive push_body.gz &&
127verify_http_result "200 OK" &&
128git rev-parse newbranch >act.head &&
129echo "$hash_next" >exp.head &&
130test_cmp act.head exp.head
131'
132133
test_expect_success GZIP 'push gzipped truncated' '
134test_env HTTP_CONTENT_ENCODING="gzip" test_http_env receive push_body.gz.trunc &&
135! verify_http_result "200 OK"
136'
137138
test_expect_success GZIP 'push gzipped empty' '
139test_env HTTP_CONTENT_ENCODING="gzip" test_http_env receive empty_body &&
140! verify_http_result "200 OK"
141'
142143
test_expect_success 'CONTENT_LENGTH overflow ssite_t' '
144NOT_FIT_IN_SSIZE=$(ssize_b100dots) &&
145env \
146CONTENT_TYPE=application/x-git-upload-pack-request \
147QUERY_STRING=/repo.git/git-upload-pack \
148PATH_TRANSLATED="$PWD"/.git/git-upload-pack \
149GIT_HTTP_EXPORT_ALL=TRUE \
150REQUEST_METHOD=POST \
151CONTENT_LENGTH="$NOT_FIT_IN_SSIZE" \
152git http-backend </dev/zero >/dev/null 2>err &&
153grep "fatal:.*CONTENT_LENGTH" err
154'
155156
test_done