Raw
1 #!/bin/sh
2
3 test_description='test git-http-backend respects CONTENT_LENGTH'
4
5 . ./test-lib.sh
6
7 if ! test_have_prereq PERL_TEST_HELPERS
8 then
9 skip_all='skipping http backend content tests; Perl not available'
10 test_done
11 fi
12
13 test_lazy_prereq GZIP 'gzip --version'
14
15 verify_http_result() {
16 # some fatal errors still produce status 200
17 # so check if there is the error message
18 if grep 'fatal:' act.err.$test_count
19 then
20 return 1
21 fi
22
23 if ! grep "Status" act.out.$test_count >act
24 then
25 printf "Status: 200 OK\r\n" >act
26 fi
27 printf "Status: $1\r\n" >exp &&
28 test_cmp exp act
29 }
30
31 test_http_env() {
32 handler_type="$1"
33 request_body="$2"
34 shift
35 env \
36 CONTENT_TYPE="application/x-git-$handler_type-pack-request" \
37 QUERY_STRING="/repo.git/git-$handler_type-pack" \
38 PATH_TRANSLATED="$PWD/.git/git-$handler_type-pack" \
39 GIT_HTTP_EXPORT_ALL=TRUE \
40 REQUEST_METHOD=POST \
41 "$PERL_PATH" \
42 "$TEST_DIRECTORY"/t5562/invoke-with-content-length.pl \
43 "$request_body" git http-backend >act.out.$test_count 2>act.err.$test_count
44 }
45
46 ssize_b100dots() {
47 # hardcoded ((size_t) SSIZE_MAX) + 1
48 case "$(build_option sizeof-size_t)" in
49 8) echo 9223372036854775808;;
50 4) echo 2147483648;;
51 *) die "Unexpected ssize_t size: $(build_option sizeof-size_t)";;
52 esac
53 }
54
55 test_expect_success 'setup' '
56 HTTP_CONTENT_ENCODING="identity" &&
57 export HTTP_CONTENT_ENCODING &&
58 git config http.receivepack true &&
59 test_commit c0 &&
60 test_commit c1 &&
61 hash_head=$(git rev-parse HEAD) &&
62 hash_prev=$(git rev-parse HEAD~1) &&
63 {
64 packetize "want $hash_head" &&
65 printf 0000 &&
66 packetize "have $hash_prev" &&
67 packetize "done"
68 } >fetch_body &&
69 test_copy_bytes 10 <fetch_body >fetch_body.trunc &&
70 hash_next=$(git commit-tree -p HEAD -m next HEAD^{tree}) &&
71 {
72 printf "%s %s refs/heads/newbranch\\0report-status object-format=%s\\n" \
73 "$ZERO_OID" "$hash_next" "$(test_oid algo)" | packetize_raw &&
74 printf 0000 &&
75 echo "$hash_next" | git pack-objects --stdout
76 } >push_body &&
77 test_copy_bytes 10 <push_body >push_body.trunc &&
78 : >empty_body
79 '
80
81 test_expect_success GZIP 'setup, compression related' '
82 gzip -c fetch_body >fetch_body.gz &&
83 test_copy_bytes 10 <fetch_body.gz >fetch_body.gz.trunc &&
84 gzip -c push_body >push_body.gz &&
85 test_copy_bytes 10 <push_body.gz >push_body.gz.trunc
86 '
87
88 test_expect_success 'fetch plain' '
89 test_http_env upload fetch_body &&
90 verify_http_result "200 OK"
91 '
92
93 test_expect_success 'fetch plain truncated' '
94 test_http_env upload fetch_body.trunc &&
95 ! verify_http_result "200 OK"
96 '
97
98 test_expect_success 'fetch plain empty' '
99 test_http_env upload empty_body &&
100 ! verify_http_result "200 OK"
101 '
102
103 test_expect_success GZIP 'fetch gzipped' '
104 test_env HTTP_CONTENT_ENCODING="gzip" test_http_env upload fetch_body.gz &&
105 verify_http_result "200 OK"
106 '
107
108 test_expect_success GZIP 'fetch gzipped truncated' '
109 test_env HTTP_CONTENT_ENCODING="gzip" test_http_env upload fetch_body.gz.trunc &&
110 ! verify_http_result "200 OK"
111 '
112
113 test_expect_success GZIP 'fetch gzipped empty' '
114 test_env HTTP_CONTENT_ENCODING="gzip" test_http_env upload empty_body &&
115 ! verify_http_result "200 OK"
116 '
117
118 test_expect_success GZIP 'push plain' '
119 test_when_finished "git branch -D newbranch" &&
120 test_http_env receive push_body &&
121 verify_http_result "200 OK" &&
122 git rev-parse newbranch >act.head &&
123 echo "$hash_next" >exp.head &&
124 test_cmp act.head exp.head
125 '
126
127 test_expect_success 'push plain truncated' '
128 test_http_env receive push_body.trunc &&
129 ! verify_http_result "200 OK"
130 '
131
132 test_expect_success 'push plain empty' '
133 test_http_env receive empty_body &&
134 ! verify_http_result "200 OK"
135 '
136
137 test_expect_success GZIP 'push gzipped' '
138 test_when_finished "git branch -D newbranch" &&
139 test_env HTTP_CONTENT_ENCODING="gzip" test_http_env receive push_body.gz &&
140 verify_http_result "200 OK" &&
141 git rev-parse newbranch >act.head &&
142 echo "$hash_next" >exp.head &&
143 test_cmp act.head exp.head
144 '
145
146 test_expect_success GZIP 'push gzipped truncated' '
147 test_env HTTP_CONTENT_ENCODING="gzip" test_http_env receive push_body.gz.trunc &&
148 ! verify_http_result "200 OK"
149 '
150
151 test_expect_success GZIP 'push gzipped empty' '
152 test_env HTTP_CONTENT_ENCODING="gzip" test_http_env receive empty_body &&
153 ! verify_http_result "200 OK"
154 '
155
156 test_expect_success 'CONTENT_LENGTH overflow ssite_t' '
157 NOT_FIT_IN_SSIZE=$(ssize_b100dots) &&
158 env \
159 CONTENT_TYPE=application/x-git-upload-pack-request \
160 QUERY_STRING=/repo.git/git-upload-pack \
161 PATH_TRANSLATED="$PWD"/.git/git-upload-pack \
162 GIT_HTTP_EXPORT_ALL=TRUE \
163 REQUEST_METHOD=POST \
164 CONTENT_LENGTH="$NOT_FIT_IN_SSIZE" \
165 git http-backend </dev/null >/dev/null 2>err &&
166 test_grep "fatal:.*CONTENT_LENGTH" err
167 '
168
169 test_expect_success 'empty CONTENT_LENGTH' '
170 env \
171 QUERY_STRING="service=git-receive-pack" \
172 PATH_TRANSLATED="$PWD"/.git/info/refs \
173 GIT_HTTP_EXPORT_ALL=TRUE \
174 REQUEST_METHOD=GET \
175 CONTENT_LENGTH="" \
176 git http-backend <empty_body >act.out.$test_count 2>act.err.$test_count &&
177 verify_http_result "200 OK"
178 '
179
180 test_done