t / t5562-http-backend-content-length.shon commit cache.h: flip NO_THE_INDEX_COMPATIBILITY_MACROS switch (f8adbec)
   1#!/bin/sh
   2
   3test_description='test git-http-backend respects CONTENT_LENGTH'
   4. ./test-lib.sh
   5
   6test_lazy_prereq GZIP 'gzip --version'
   7
   8verify_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
  14        fi
  15
  16        if ! grep "Status" act.out >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
  24test_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 2>act.err
  37}
  38
  39ssize_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
  48test_expect_success 'setup' '
  49        HTTP_CONTENT_ENCODING="identity" &&
  50        export HTTP_CONTENT_ENCODING &&
  51        git config http.receivepack true &&
  52        test_commit c0 &&
  53        test_commit c1 &&
  54        hash_head=$(git rev-parse HEAD) &&
  55        hash_prev=$(git rev-parse HEAD~1) &&
  56        printf "want %s" "$hash_head" | packetize >fetch_body &&
  57        printf 0000 >>fetch_body &&
  58        printf "have %s" "$hash_prev" | packetize >>fetch_body &&
  59        printf done | packetize >>fetch_body &&
  60        test_copy_bytes 10 <fetch_body >fetch_body.trunc &&
  61        hash_next=$(git commit-tree -p HEAD -m next HEAD^{tree}) &&
  62        printf "%s %s refs/heads/newbranch\\0report-status\\n" "$_z40" "$hash_next" | packetize >push_body &&
  63        printf 0000 >>push_body &&
  64        echo "$hash_next" | git pack-objects --stdout >>push_body &&
  65        test_copy_bytes 10 <push_body >push_body.trunc &&
  66        : >empty_body
  67'
  68
  69test_expect_success GZIP 'setup, compression related' '
  70        gzip -c fetch_body >fetch_body.gz &&
  71        test_copy_bytes 10 <fetch_body.gz >fetch_body.gz.trunc &&
  72        gzip -c push_body >push_body.gz &&
  73        test_copy_bytes 10 <push_body.gz >push_body.gz.trunc
  74'
  75
  76test_expect_success 'fetch plain' '
  77        test_http_env upload fetch_body &&
  78        verify_http_result "200 OK"
  79'
  80
  81test_expect_success 'fetch plain truncated' '
  82        test_http_env upload fetch_body.trunc &&
  83        ! verify_http_result "200 OK"
  84'
  85
  86test_expect_success 'fetch plain empty' '
  87        test_http_env upload empty_body &&
  88        ! verify_http_result "200 OK"
  89'
  90
  91test_expect_success GZIP 'fetch gzipped' '
  92        test_env HTTP_CONTENT_ENCODING="gzip" test_http_env upload fetch_body.gz &&
  93        verify_http_result "200 OK"
  94'
  95
  96test_expect_success GZIP 'fetch gzipped truncated' '
  97        test_env HTTP_CONTENT_ENCODING="gzip" test_http_env upload fetch_body.gz.trunc &&
  98        ! verify_http_result "200 OK"
  99'
 100
 101test_expect_success GZIP 'fetch gzipped empty' '
 102        test_env HTTP_CONTENT_ENCODING="gzip" test_http_env upload empty_body &&
 103        ! verify_http_result "200 OK"
 104'
 105
 106test_expect_success GZIP 'push plain' '
 107        test_when_finished "git branch -D newbranch" &&
 108        test_http_env receive push_body &&
 109        verify_http_result "200 OK" &&
 110        git rev-parse newbranch >act.head &&
 111        echo "$hash_next" >exp.head &&
 112        test_cmp act.head exp.head
 113'
 114
 115test_expect_success 'push plain truncated' '
 116        test_http_env receive push_body.trunc &&
 117        ! verify_http_result "200 OK"
 118'
 119
 120test_expect_success 'push plain empty' '
 121        test_http_env receive empty_body &&
 122        ! verify_http_result "200 OK"
 123'
 124
 125test_expect_success GZIP 'push gzipped' '
 126        test_when_finished "git branch -D newbranch" &&
 127        test_env HTTP_CONTENT_ENCODING="gzip" test_http_env receive push_body.gz &&
 128        verify_http_result "200 OK" &&
 129        git rev-parse newbranch >act.head &&
 130        echo "$hash_next" >exp.head &&
 131        test_cmp act.head exp.head
 132'
 133
 134test_expect_success GZIP 'push gzipped truncated' '
 135        test_env HTTP_CONTENT_ENCODING="gzip" test_http_env receive push_body.gz.trunc &&
 136        ! verify_http_result "200 OK"
 137'
 138
 139test_expect_success GZIP 'push gzipped empty' '
 140        test_env HTTP_CONTENT_ENCODING="gzip" test_http_env receive empty_body &&
 141        ! verify_http_result "200 OK"
 142'
 143
 144test_expect_success 'CONTENT_LENGTH overflow ssite_t' '
 145        NOT_FIT_IN_SSIZE=$(ssize_b100dots) &&
 146        env \
 147                CONTENT_TYPE=application/x-git-upload-pack-request \
 148                QUERY_STRING=/repo.git/git-upload-pack \
 149                PATH_TRANSLATED="$PWD"/.git/git-upload-pack \
 150                GIT_HTTP_EXPORT_ALL=TRUE \
 151                REQUEST_METHOD=POST \
 152                CONTENT_LENGTH="$NOT_FIT_IN_SSIZE" \
 153                git http-backend </dev/zero >/dev/null 2>err &&
 154        grep "fatal:.*CONTENT_LENGTH" err
 155'
 156
 157test_expect_success 'empty CONTENT_LENGTH' '
 158        env \
 159                QUERY_STRING="service=git-receive-pack" \
 160                PATH_TRANSLATED="$PWD"/.git/info/refs \
 161                GIT_HTTP_EXPORT_ALL=TRUE \
 162                REQUEST_METHOD=GET \
 163                CONTENT_LENGTH="" \
 164                git http-backend <empty_body >act.out 2>act.err &&
 165        verify_http_result "200 OK"
 166'
 167
 168test_done