t / t5560-http-backend.shon commit Smart-http: check if repository is OK to export before serving it (8b2bd7c)
   1#!/bin/sh
   2
   3test_description='test git-http-backend'
   4. ./test-lib.sh
   5
   6if test -n "$NO_CURL"; then
   7        say 'skipping test, git built without http support'
   8        test_done
   9fi
  10
  11LIB_HTTPD_PORT=${LIB_HTTPD_PORT-'5560'}
  12. "$TEST_DIRECTORY"/lib-httpd.sh
  13start_httpd
  14
  15find_file() {
  16        cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
  17        find $1 -type f |
  18        sed -e 1q
  19}
  20
  21config() {
  22        git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" config $1 $2
  23}
  24
  25GET() {
  26        curl --include "$HTTPD_URL/$SMART/repo.git/$1" >out 2>/dev/null &&
  27        tr '\015' Q <out |
  28        sed '
  29                s/Q$//
  30                1q
  31        ' >act &&
  32        echo "HTTP/1.1 $2" >exp &&
  33        test_cmp exp act
  34}
  35
  36POST() {
  37        curl --include --data "$2" \
  38        --header "Content-Type: application/x-$1-request" \
  39        "$HTTPD_URL/smart/repo.git/$1" >out 2>/dev/null &&
  40        tr '\015' Q <out |
  41        sed '
  42                s/Q$//
  43                1q
  44        ' >act &&
  45        echo "HTTP/1.1 $3" >exp &&
  46        test_cmp exp act
  47}
  48
  49log_div() {
  50        echo >>"$HTTPD_ROOT_PATH"/access.log
  51        echo "###  $1" >>"$HTTPD_ROOT_PATH"/access.log
  52        echo "###" >>"$HTTPD_ROOT_PATH"/access.log
  53}
  54
  55test_expect_success 'setup repository' '
  56        echo content >file &&
  57        git add file &&
  58        git commit -m one &&
  59
  60        mkdir "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
  61        (cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
  62         git --bare init &&
  63         : >objects/info/alternates &&
  64         : >objects/info/http-alternates
  65        ) &&
  66        git remote add public "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
  67        git push public master:master &&
  68
  69        (cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
  70         git repack -a -d
  71        ) &&
  72
  73        echo other >file &&
  74        git add file &&
  75        git commit -m two &&
  76        git push public master:master &&
  77
  78        LOOSE_URL=$(find_file objects/??) &&
  79        PACK_URL=$(find_file objects/pack/*.pack) &&
  80        IDX_URL=$(find_file objects/pack/*.idx)
  81'
  82
  83get_static_files() {
  84        GET HEAD "$1" &&
  85        GET info/refs "$1" &&
  86        GET objects/info/packs "$1" &&
  87        GET objects/info/alternates "$1" &&
  88        GET objects/info/http-alternates "$1" &&
  89        GET $LOOSE_URL "$1" &&
  90        GET $PACK_URL "$1" &&
  91        GET $IDX_URL "$1"
  92}
  93
  94SMART=smart
  95test_expect_success 'direct refs/heads/master not found' '
  96        log_div "refs/heads/master"
  97        GET refs/heads/master "404 Not Found"
  98'
  99test_expect_success 'static file is ok' '
 100        log_div "getanyfile default"
 101        get_static_files "200 OK"
 102'
 103SMART=smart_noexport
 104test_expect_success 'no export by default' '
 105        log_div "no git-daemon-export-ok"
 106        get_static_files "404 Not Found"
 107'
 108test_expect_success 'export if git-daemon-export-ok' '
 109        log_div "git-daemon-export-ok"
 110        (cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
 111         touch git-daemon-export-ok
 112        ) &&
 113        get_static_files "200 OK"
 114'
 115SMART=smart
 116test_expect_success 'static file if http.getanyfile true is ok' '
 117        log_div "getanyfile true"
 118        config http.getanyfile true &&
 119        get_static_files "200 OK"
 120'
 121test_expect_success 'static file if http.getanyfile false fails' '
 122        log_div "getanyfile false"
 123        config http.getanyfile false &&
 124        get_static_files "403 Forbidden"
 125'
 126
 127test_expect_success 'http.uploadpack default enabled' '
 128        log_div "uploadpack default"
 129        GET info/refs?service=git-upload-pack "200 OK"  &&
 130        POST git-upload-pack 0000 "200 OK"
 131'
 132test_expect_success 'http.uploadpack true' '
 133        log_div "uploadpack true"
 134        config http.uploadpack true &&
 135        GET info/refs?service=git-upload-pack "200 OK" &&
 136        POST git-upload-pack 0000 "200 OK"
 137'
 138test_expect_success 'http.uploadpack false' '
 139        log_div "uploadpack false"
 140        config http.uploadpack false &&
 141        GET info/refs?service=git-upload-pack "403 Forbidden" &&
 142        POST git-upload-pack 0000 "403 Forbidden"
 143'
 144
 145test_expect_success 'http.receivepack default disabled' '
 146        log_div "receivepack default"
 147        GET info/refs?service=git-receive-pack "403 Forbidden"  &&
 148        POST git-receive-pack 0000 "403 Forbidden"
 149'
 150test_expect_success 'http.receivepack true' '
 151        log_div "receivepack true"
 152        config http.receivepack true &&
 153        GET info/refs?service=git-receive-pack "200 OK" &&
 154        POST git-receive-pack 0000 "200 OK"
 155'
 156test_expect_success 'http.receivepack false' '
 157        log_div "receivepack false"
 158        config http.receivepack false &&
 159        GET info/refs?service=git-receive-pack "403 Forbidden" &&
 160        POST git-receive-pack 0000 "403 Forbidden"
 161'
 162run_backend() {
 163        REQUEST_METHOD=GET \
 164        GIT_PROJECT_ROOT="$HTTPD_DOCUMENT_ROOT_PATH" \
 165        PATH_INFO="$2" \
 166        git http-backend >act.out 2>act.err
 167}
 168
 169path_info() {
 170        if test $1 = 0; then
 171                run_backend "$2"
 172        else
 173                test_must_fail run_backend "$2" &&
 174                echo "fatal: '$2': aliased" >exp.err &&
 175                test_cmp exp.err act.err
 176        fi
 177}
 178
 179test_expect_success 'http-backend blocks bad PATH_INFO' '
 180        config http.getanyfile true &&
 181
 182        run_backend 0 /repo.git/HEAD &&
 183
 184        run_backend 1 /repo.git/../HEAD &&
 185        run_backend 1 /../etc/passwd &&
 186        run_backend 1 ../etc/passwd &&
 187        run_backend 1 /etc//passwd &&
 188        run_backend 1 /etc/./passwd &&
 189        run_backend 1 /etc/.../passwd &&
 190        run_backend 1 //domain/data.txt
 191'
 192
 193cat >exp <<EOF
 194
 195###  refs/heads/master
 196###
 197GET  /smart/repo.git/refs/heads/master HTTP/1.1 404 -
 198
 199###  getanyfile default
 200###
 201GET  /smart/repo.git/HEAD HTTP/1.1 200
 202GET  /smart/repo.git/info/refs HTTP/1.1 200
 203GET  /smart/repo.git/objects/info/packs HTTP/1.1 200
 204GET  /smart/repo.git/objects/info/alternates HTTP/1.1 200 -
 205GET  /smart/repo.git/objects/info/http-alternates HTTP/1.1 200 -
 206GET  /smart/repo.git/$LOOSE_URL HTTP/1.1 200
 207GET  /smart/repo.git/$PACK_URL HTTP/1.1 200
 208GET  /smart/repo.git/$IDX_URL HTTP/1.1 200
 209
 210###  no git-daemon-export-ok
 211###
 212GET  /smart_noexport/repo.git/HEAD HTTP/1.1 404 -
 213GET  /smart_noexport/repo.git/info/refs HTTP/1.1 404 -
 214GET  /smart_noexport/repo.git/objects/info/packs HTTP/1.1 404 -
 215GET  /smart_noexport/repo.git/objects/info/alternates HTTP/1.1 404 -
 216GET  /smart_noexport/repo.git/objects/info/http-alternates HTTP/1.1 404 -
 217GET  /smart_noexport/repo.git/$LOOSE_URL HTTP/1.1 404 -
 218GET  /smart_noexport/repo.git/$PACK_URL HTTP/1.1 404 -
 219GET  /smart_noexport/repo.git/$IDX_URL HTTP/1.1 404 -
 220
 221###  git-daemon-export-ok
 222###
 223GET  /smart_noexport/repo.git/HEAD HTTP/1.1 200
 224GET  /smart_noexport/repo.git/info/refs HTTP/1.1 200
 225GET  /smart_noexport/repo.git/objects/info/packs HTTP/1.1 200
 226GET  /smart_noexport/repo.git/objects/info/alternates HTTP/1.1 200 -
 227GET  /smart_noexport/repo.git/objects/info/http-alternates HTTP/1.1 200 -
 228GET  /smart_noexport/repo.git/$LOOSE_URL HTTP/1.1 200
 229GET  /smart_noexport/repo.git/$PACK_URL HTTP/1.1 200
 230GET  /smart_noexport/repo.git/$IDX_URL HTTP/1.1 200
 231
 232###  getanyfile true
 233###
 234GET  /smart/repo.git/HEAD HTTP/1.1 200
 235GET  /smart/repo.git/info/refs HTTP/1.1 200
 236GET  /smart/repo.git/objects/info/packs HTTP/1.1 200
 237GET  /smart/repo.git/objects/info/alternates HTTP/1.1 200 -
 238GET  /smart/repo.git/objects/info/http-alternates HTTP/1.1 200 -
 239GET  /smart/repo.git/$LOOSE_URL HTTP/1.1 200
 240GET  /smart/repo.git/$PACK_URL HTTP/1.1 200
 241GET  /smart/repo.git/$IDX_URL HTTP/1.1 200
 242
 243###  getanyfile false
 244###
 245GET  /smart/repo.git/HEAD HTTP/1.1 403 -
 246GET  /smart/repo.git/info/refs HTTP/1.1 403 -
 247GET  /smart/repo.git/objects/info/packs HTTP/1.1 403 -
 248GET  /smart/repo.git/objects/info/alternates HTTP/1.1 403 -
 249GET  /smart/repo.git/objects/info/http-alternates HTTP/1.1 403 -
 250GET  /smart/repo.git/$LOOSE_URL HTTP/1.1 403 -
 251GET  /smart/repo.git/$PACK_URL HTTP/1.1 403 -
 252GET  /smart/repo.git/$IDX_URL HTTP/1.1 403 -
 253
 254###  uploadpack default
 255###
 256GET  /smart/repo.git/info/refs?service=git-upload-pack HTTP/1.1 200
 257POST /smart/repo.git/git-upload-pack HTTP/1.1 200 -
 258
 259###  uploadpack true
 260###
 261GET  /smart/repo.git/info/refs?service=git-upload-pack HTTP/1.1 200
 262POST /smart/repo.git/git-upload-pack HTTP/1.1 200 -
 263
 264###  uploadpack false
 265###
 266GET  /smart/repo.git/info/refs?service=git-upload-pack HTTP/1.1 403 -
 267POST /smart/repo.git/git-upload-pack HTTP/1.1 403 -
 268
 269###  receivepack default
 270###
 271GET  /smart/repo.git/info/refs?service=git-receive-pack HTTP/1.1 403 -
 272POST /smart/repo.git/git-receive-pack HTTP/1.1 403 -
 273
 274###  receivepack true
 275###
 276GET  /smart/repo.git/info/refs?service=git-receive-pack HTTP/1.1 200
 277POST /smart/repo.git/git-receive-pack HTTP/1.1 200 -
 278
 279###  receivepack false
 280###
 281GET  /smart/repo.git/info/refs?service=git-receive-pack HTTP/1.1 403 -
 282POST /smart/repo.git/git-receive-pack HTTP/1.1 403 -
 283EOF
 284test_expect_success 'server request log matches test results' '
 285        sed -e "
 286                s/^.* \"//
 287                s/\"//
 288                s/ [1-9][0-9]*\$//
 289                s/^GET /GET  /
 290        " >act <"$HTTPD_ROOT_PATH"/access.log &&
 291        test_cmp exp act
 292'
 293
 294stop_httpd
 295test_done