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