t5551: move setup code inside test_expect blocks
[git.git] / t / t5562-http-backend-content-length.sh
blobf94d01f69e5cb9565540411de2c09df92262b9c3
1 #!/bin/sh
3 test_description='test git-http-backend respects CONTENT_LENGTH'
4 . ./test-lib.sh
6 test_lazy_prereq GZIP 'gzip --version'
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
12 then
13 return 1
16 if ! grep "Status" act.out >act
17 then
18 printf "Status: 200 OK\r\n" >act
20 printf "Status: $1\r\n" >exp &&
21 test_cmp exp act
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 "$TEST_DIRECTORY"/t5562/invoke-with-content-length.pl \
35 "$request_body" git http-backend >act.out 2>act.err
38 ssize_b100dots() {
39 # hardcoded ((size_t) SSIZE_MAX) + 1
40 case "$(build_option sizeof-size_t)" in
41 8) echo 9223372036854775808;;
42 4) echo 2147483648;;
43 *) die "Unexpected ssize_t size: $(build_option sizeof-size_t)";;
44 esac
47 test_expect_success 'setup' '
48 HTTP_CONTENT_ENCODING="identity" &&
49 export HTTP_CONTENT_ENCODING &&
50 git config http.receivepack true &&
51 test_commit c0 &&
52 test_commit c1 &&
53 hash_head=$(git rev-parse HEAD) &&
54 hash_prev=$(git rev-parse HEAD~1) &&
55 printf "want %s" "$hash_head" | packetize >fetch_body &&
56 printf 0000 >>fetch_body &&
57 printf "have %s" "$hash_prev" | packetize >>fetch_body &&
58 printf done | packetize >>fetch_body &&
59 test_copy_bytes 10 <fetch_body >fetch_body.trunc &&
60 hash_next=$(git commit-tree -p HEAD -m next HEAD^{tree}) &&
61 printf "%s %s refs/heads/newbranch\\0report-status\\n" "$_z40" "$hash_next" | packetize >push_body &&
62 printf 0000 >>push_body &&
63 echo "$hash_next" | git pack-objects --stdout >>push_body &&
64 test_copy_bytes 10 <push_body >push_body.trunc &&
65 : >empty_body
68 test_expect_success GZIP 'setup, compression related' '
69 gzip -c fetch_body >fetch_body.gz &&
70 test_copy_bytes 10 <fetch_body.gz >fetch_body.gz.trunc &&
71 gzip -c push_body >push_body.gz &&
72 test_copy_bytes 10 <push_body.gz >push_body.gz.trunc
75 test_expect_success 'fetch plain' '
76 test_http_env upload fetch_body &&
77 verify_http_result "200 OK"
80 test_expect_success 'fetch plain truncated' '
81 test_http_env upload fetch_body.trunc &&
82 ! verify_http_result "200 OK"
85 test_expect_success 'fetch plain empty' '
86 test_http_env upload empty_body &&
87 ! verify_http_result "200 OK"
90 test_expect_success GZIP 'fetch gzipped' '
91 test_env HTTP_CONTENT_ENCODING="gzip" test_http_env upload fetch_body.gz &&
92 verify_http_result "200 OK"
95 test_expect_success GZIP 'fetch gzipped truncated' '
96 test_env HTTP_CONTENT_ENCODING="gzip" test_http_env upload fetch_body.gz.trunc &&
97 ! verify_http_result "200 OK"
100 test_expect_success GZIP 'fetch gzipped empty' '
101 test_env HTTP_CONTENT_ENCODING="gzip" test_http_env upload empty_body &&
102 ! verify_http_result "200 OK"
105 test_expect_success GZIP 'push plain' '
106 test_when_finished "git branch -D newbranch" &&
107 test_http_env receive push_body &&
108 verify_http_result "200 OK" &&
109 git rev-parse newbranch >act.head &&
110 echo "$hash_next" >exp.head &&
111 test_cmp act.head exp.head
114 test_expect_success 'push plain truncated' '
115 test_http_env receive push_body.trunc &&
116 ! verify_http_result "200 OK"
119 test_expect_success 'push plain empty' '
120 test_http_env receive empty_body &&
121 ! verify_http_result "200 OK"
124 test_expect_success GZIP 'push gzipped' '
125 test_when_finished "git branch -D newbranch" &&
126 test_env HTTP_CONTENT_ENCODING="gzip" test_http_env receive push_body.gz &&
127 verify_http_result "200 OK" &&
128 git rev-parse newbranch >act.head &&
129 echo "$hash_next" >exp.head &&
130 test_cmp act.head exp.head
133 test_expect_success GZIP 'push gzipped truncated' '
134 test_env HTTP_CONTENT_ENCODING="gzip" test_http_env receive push_body.gz.trunc &&
135 ! verify_http_result "200 OK"
138 test_expect_success GZIP 'push gzipped empty' '
139 test_env HTTP_CONTENT_ENCODING="gzip" test_http_env receive empty_body &&
140 ! verify_http_result "200 OK"
143 test_expect_success 'CONTENT_LENGTH overflow ssite_t' '
144 NOT_FIT_IN_SSIZE=$(ssize_b100dots) &&
145 env \
146 CONTENT_TYPE=application/x-git-upload-pack-request \
147 QUERY_STRING=/repo.git/git-upload-pack \
148 PATH_TRANSLATED="$PWD"/.git/git-upload-pack \
149 GIT_HTTP_EXPORT_ALL=TRUE \
150 REQUEST_METHOD=POST \
151 CONTENT_LENGTH="$NOT_FIT_IN_SSIZE" \
152 git http-backend </dev/zero >/dev/null 2>err &&
153 grep "fatal:.*CONTENT_LENGTH" err
156 test_expect_success 'empty CONTENT_LENGTH' '
157 env \
158 QUERY_STRING=/repo.git/HEAD \
159 PATH_TRANSLATED="$PWD"/.git/HEAD \
160 GIT_HTTP_EXPORT_ALL=TRUE \
161 REQUEST_METHOD=GET \
162 CONTENT_LENGTH="" \
163 git http-backend <empty_body >act.out 2>act.err &&
164 verify_http_result "200 OK"
167 test_done