f7a84f30035009f945d6739b2075c06e3ca15c61
   1#!/bin/sh
   2
   3test_description='test smart fetching over http via http-backend'
   4. ./test-lib.sh
   5. "$TEST_DIRECTORY"/lib-httpd.sh
   6start_httpd
   7
   8test_expect_success 'setup repository' '
   9        git config push.default matching &&
  10        echo content >file &&
  11        git add file &&
  12        git commit -m one
  13'
  14
  15test_expect_success 'create http-accessible bare repository' '
  16        mkdir "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
  17        (cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
  18         git --bare init
  19        ) &&
  20        git remote add public "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
  21        git push public master:master
  22'
  23
  24setup_askpass_helper
  25
  26test_expect_success 'clone http repository' '
  27        cat >exp <<-\EOF &&
  28        > GET /smart/repo.git/info/refs?service=git-upload-pack HTTP/1.1
  29        > Accept: */*
  30        > Accept-Encoding: ENCODINGS
  31        > Pragma: no-cache
  32        < HTTP/1.1 200 OK
  33        < Pragma: no-cache
  34        < Cache-Control: no-cache, max-age=0, must-revalidate
  35        < Content-Type: application/x-git-upload-pack-advertisement
  36        > POST /smart/repo.git/git-upload-pack HTTP/1.1
  37        > Accept-Encoding: ENCODINGS
  38        > Content-Type: application/x-git-upload-pack-request
  39        > Accept: application/x-git-upload-pack-result
  40        > Content-Length: xxx
  41        < HTTP/1.1 200 OK
  42        < Pragma: no-cache
  43        < Cache-Control: no-cache, max-age=0, must-revalidate
  44        < Content-Type: application/x-git-upload-pack-result
  45        EOF
  46        GIT_TRACE_CURL=true GIT_TEST_PROTOCOL_VERSION= \
  47                git clone --quiet $HTTPD_URL/smart/repo.git clone 2>err &&
  48        test_cmp file clone/file &&
  49        tr '\''\015'\'' Q <err |
  50        sed -e "
  51                s/Q\$//
  52                /^[*] /d
  53                /^== Info:/d
  54                /^=> Send header, /d
  55                /^=> Send header:$/d
  56                /^<= Recv header, /d
  57                /^<= Recv header:$/d
  58                s/=> Send header: //
  59                s/= Recv header://
  60                /^<= Recv data/d
  61                /^=> Send data/d
  62                /^$/d
  63                /^< $/d
  64
  65                /^[^><]/{
  66                        s/^/> /
  67                }
  68
  69                /^> User-Agent: /d
  70                /^> Host: /d
  71                /^> POST /,$ {
  72                        /^> Accept: [*]\\/[*]/d
  73                }
  74                s/^> Content-Length: .*/> Content-Length: xxx/
  75                /^> 00..want /d
  76                /^> 00.*done/d
  77
  78                /^< Server: /d
  79                /^< Expires: /d
  80                /^< Date: /d
  81                /^< Content-Length: /d
  82                /^< Transfer-Encoding: /d
  83        " >actual &&
  84        sed -e "s/^> Accept-Encoding: .*/> Accept-Encoding: ENCODINGS/" \
  85                        actual >actual.smudged &&
  86        test_cmp exp actual.smudged &&
  87
  88        grep "Accept-Encoding:.*gzip" actual >actual.gzip &&
  89        test_line_count = 2 actual.gzip
  90'
  91
  92test_expect_success 'fetch changes via http' '
  93        echo content >>file &&
  94        git commit -a -m two &&
  95        git push public &&
  96        (cd clone && git pull) &&
  97        test_cmp file clone/file
  98'
  99
 100test_expect_success 'used upload-pack service' '
 101        cat >exp <<-\EOF &&
 102        GET  /smart/repo.git/info/refs?service=git-upload-pack HTTP/1.1 200
 103        POST /smart/repo.git/git-upload-pack HTTP/1.1 200
 104        GET  /smart/repo.git/info/refs?service=git-upload-pack HTTP/1.1 200
 105        POST /smart/repo.git/git-upload-pack HTTP/1.1 200
 106        EOF
 107        check_access_log exp
 108'
 109
 110test_expect_success 'follow redirects (301)' '
 111        git clone $HTTPD_URL/smart-redir-perm/repo.git --quiet repo-p
 112'
 113
 114test_expect_success 'follow redirects (302)' '
 115        git clone $HTTPD_URL/smart-redir-temp/repo.git --quiet repo-t
 116'
 117
 118test_expect_success 'redirects re-root further requests' '
 119        git clone $HTTPD_URL/smart-redir-limited/repo.git repo-redir-limited
 120'
 121
 122test_expect_success 're-rooting dies on insane schemes' '
 123        test_must_fail git clone $HTTPD_URL/insane-redir/repo.git insane
 124'
 125
 126test_expect_success 'clone from password-protected repository' '
 127        echo two >expect &&
 128        set_askpass user@host pass@host &&
 129        git clone --bare "$HTTPD_URL/auth/smart/repo.git" smart-auth &&
 130        expect_askpass both user@host &&
 131        git --git-dir=smart-auth log -1 --format=%s >actual &&
 132        test_cmp expect actual
 133'
 134
 135test_expect_success 'clone from auth-only-for-push repository' '
 136        echo two >expect &&
 137        set_askpass wrong &&
 138        git clone --bare "$HTTPD_URL/auth-push/smart/repo.git" smart-noauth &&
 139        expect_askpass none &&
 140        git --git-dir=smart-noauth log -1 --format=%s >actual &&
 141        test_cmp expect actual
 142'
 143
 144test_expect_success 'clone from auth-only-for-objects repository' '
 145        echo two >expect &&
 146        set_askpass user@host pass@host &&
 147        git clone --bare "$HTTPD_URL/auth-fetch/smart/repo.git" half-auth &&
 148        expect_askpass both user@host &&
 149        git --git-dir=half-auth log -1 --format=%s >actual &&
 150        test_cmp expect actual
 151'
 152
 153test_expect_success 'no-op half-auth fetch does not require a password' '
 154        set_askpass wrong &&
 155        git --git-dir=half-auth fetch &&
 156        expect_askpass none
 157'
 158
 159test_expect_success 'redirects send auth to new location' '
 160        set_askpass user@host pass@host &&
 161        git -c credential.useHttpPath=true \
 162          clone $HTTPD_URL/smart-redir-auth/repo.git repo-redir-auth &&
 163        expect_askpass both user@host auth/smart/repo.git
 164'
 165
 166test_expect_success 'disable dumb http on server' '
 167        git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
 168                config http.getanyfile false
 169'
 170
 171test_expect_success 'GIT_SMART_HTTP can disable smart http' '
 172        (GIT_SMART_HTTP=0 &&
 173         export GIT_SMART_HTTP &&
 174         cd clone &&
 175         test_must_fail git fetch)
 176'
 177
 178test_expect_success 'invalid Content-Type rejected' '
 179        test_must_fail git clone $HTTPD_URL/broken_smart/repo.git 2>actual &&
 180        grep "not valid:" actual
 181'
 182
 183test_expect_success 'create namespaced refs' '
 184        test_commit namespaced &&
 185        git push public HEAD:refs/namespaces/ns/refs/heads/master &&
 186        git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
 187                symbolic-ref refs/namespaces/ns/HEAD refs/namespaces/ns/refs/heads/master
 188'
 189
 190test_expect_success 'smart clone respects namespace' '
 191        git clone "$HTTPD_URL/smart_namespace/repo.git" ns-smart &&
 192        echo namespaced >expect &&
 193        git --git-dir=ns-smart/.git log -1 --format=%s >actual &&
 194        test_cmp expect actual
 195'
 196
 197test_expect_success 'dumb clone via http-backend respects namespace' '
 198        git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
 199                config http.getanyfile true &&
 200        GIT_SMART_HTTP=0 git clone \
 201                "$HTTPD_URL/smart_namespace/repo.git" ns-dumb &&
 202        echo namespaced >expect &&
 203        git --git-dir=ns-dumb/.git log -1 --format=%s >actual &&
 204        test_cmp expect actual
 205'
 206
 207test_expect_success 'cookies stored in http.cookiefile when http.savecookies set' '
 208        cat >cookies.txt <<-\EOF &&
 209        127.0.0.1       FALSE   /smart_cookies/ FALSE   0       othername       othervalue
 210        EOF
 211        sort >expect_cookies.txt <<-\EOF &&
 212
 213        127.0.0.1       FALSE   /smart_cookies/ FALSE   0       othername       othervalue
 214        127.0.0.1       FALSE   /smart_cookies/repo.git/info/   FALSE   0       name    value
 215        EOF
 216        git config http.cookiefile cookies.txt &&
 217        git config http.savecookies true &&
 218        git ls-remote $HTTPD_URL/smart_cookies/repo.git master &&
 219        tail -3 cookies.txt | sort >cookies_tail.txt &&
 220        test_cmp expect_cookies.txt cookies_tail.txt
 221'
 222
 223test_expect_success 'transfer.hiderefs works over smart-http' '
 224        test_commit hidden &&
 225        test_commit visible &&
 226        git push public HEAD^:refs/heads/a HEAD:refs/heads/b &&
 227        git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
 228                config transfer.hiderefs refs/heads/a &&
 229        git clone --bare "$HTTPD_URL/smart/repo.git" hidden.git &&
 230        test_must_fail git -C hidden.git rev-parse --verify a &&
 231        git -C hidden.git rev-parse --verify b
 232'
 233
 234# create an arbitrary number of tags, numbered from tag-$1 to tag-$2
 235create_tags () {
 236        rm -f marks &&
 237        for i in $(test_seq "$1" "$2")
 238        do
 239                # don't use here-doc, because it requires a process
 240                # per loop iteration
 241                echo "commit refs/heads/too-many-refs-$1" &&
 242                echo "mark :$i" &&
 243                echo "committer git <git@example.com> $i +0000" &&
 244                echo "data 0" &&
 245                echo "M 644 inline bla.txt" &&
 246                echo "data 4" &&
 247                echo "bla" &&
 248                # make every commit dangling by always
 249                # rewinding the branch after each commit
 250                echo "reset refs/heads/too-many-refs-$1" &&
 251                echo "from :$1"
 252        done | git fast-import --export-marks=marks &&
 253
 254        # now assign tags to all the dangling commits we created above
 255        tag=$(perl -e "print \"bla\" x 30") &&
 256        sed -e "s|^:\([^ ]*\) \(.*\)$|\2 refs/tags/$tag-\1|" <marks >>packed-refs
 257}
 258
 259test_expect_success 'create 2,000 tags in the repo' '
 260        (
 261                cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
 262                create_tags 1 2000
 263        )
 264'
 265
 266test_expect_success CMDLINE_LIMIT \
 267        'clone the 2,000 tag repo to check OS command line overflow' '
 268        run_with_limited_cmdline git clone $HTTPD_URL/smart/repo.git too-many-refs &&
 269        (
 270                cd too-many-refs &&
 271                git for-each-ref refs/tags >actual &&
 272                test_line_count = 2000 actual
 273        )
 274'
 275
 276test_expect_success 'large fetch-pack requests can be split across POSTs' '
 277        GIT_TRACE_CURL=true git -c http.postbuffer=65536 \
 278                clone --bare "$HTTPD_URL/smart/repo.git" split.git 2>err &&
 279        grep "^=> Send header: POST" err >posts &&
 280        test_line_count = 2 posts
 281'
 282
 283test_expect_success 'test allowreachablesha1inwant' '
 284        test_when_finished "rm -rf test_reachable.git" &&
 285        server="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
 286        master_sha=$(git -C "$server" rev-parse refs/heads/master) &&
 287        git -C "$server" config uploadpack.allowreachablesha1inwant 1 &&
 288
 289        git init --bare test_reachable.git &&
 290        git -C test_reachable.git remote add origin "$HTTPD_URL/smart/repo.git" &&
 291        git -C test_reachable.git fetch origin "$master_sha"
 292'
 293
 294test_expect_success 'test allowreachablesha1inwant with unreachable' '
 295        test_when_finished "rm -rf test_reachable.git; git reset --hard $(git rev-parse HEAD)" &&
 296
 297        #create unreachable sha
 298        echo content >file2 &&
 299        git add file2 &&
 300        git commit -m two &&
 301        git push public HEAD:refs/heads/doomed &&
 302        git push public :refs/heads/doomed &&
 303
 304        server="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
 305        master_sha=$(git -C "$server" rev-parse refs/heads/master) &&
 306        git -C "$server" config uploadpack.allowreachablesha1inwant 1 &&
 307
 308        git init --bare test_reachable.git &&
 309        git -C test_reachable.git remote add origin "$HTTPD_URL/smart/repo.git" &&
 310        test_must_fail git -C test_reachable.git fetch origin "$(git rev-parse HEAD)"
 311'
 312
 313test_expect_success 'test allowanysha1inwant with unreachable' '
 314        test_when_finished "rm -rf test_reachable.git; git reset --hard $(git rev-parse HEAD)" &&
 315
 316        #create unreachable sha
 317        echo content >file2 &&
 318        git add file2 &&
 319        git commit -m two &&
 320        git push public HEAD:refs/heads/doomed &&
 321        git push public :refs/heads/doomed &&
 322
 323        server="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
 324        master_sha=$(git -C "$server" rev-parse refs/heads/master) &&
 325        git -C "$server" config uploadpack.allowreachablesha1inwant 1 &&
 326
 327        git init --bare test_reachable.git &&
 328        git -C test_reachable.git remote add origin "$HTTPD_URL/smart/repo.git" &&
 329        test_must_fail git -C test_reachable.git fetch origin "$(git rev-parse HEAD)" &&
 330
 331        git -C "$server" config uploadpack.allowanysha1inwant 1 &&
 332        git -C test_reachable.git fetch origin "$(git rev-parse HEAD)"
 333'
 334
 335test_expect_success EXPENSIVE 'http can handle enormous ref negotiation' '
 336        (
 337                cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
 338                create_tags 2001 50000
 339        ) &&
 340        git -C too-many-refs fetch -q --tags &&
 341        (
 342                cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
 343                create_tags 50001 100000
 344        ) &&
 345        git -C too-many-refs fetch -q --tags &&
 346        git -C too-many-refs for-each-ref refs/tags >tags &&
 347        test_line_count = 100000 tags
 348'
 349
 350test_expect_success 'custom http headers' '
 351        test_must_fail git -c http.extraheader="x-magic-two: cadabra" \
 352                fetch "$HTTPD_URL/smart_headers/repo.git" &&
 353        git -c http.extraheader="x-magic-one: abra" \
 354            -c http.extraheader="x-magic-two: cadabra" \
 355            fetch "$HTTPD_URL/smart_headers/repo.git" &&
 356        git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
 357        git config -f .gitmodules submodule.sub.path sub &&
 358        git config -f .gitmodules submodule.sub.url \
 359                "$HTTPD_URL/smart_headers/repo.git" &&
 360        git submodule init sub &&
 361        test_must_fail git submodule update sub &&
 362        git -c http.extraheader="x-magic-one: abra" \
 363            -c http.extraheader="x-magic-two: cadabra" \
 364                submodule update sub
 365'
 366
 367test_expect_success 'using fetch command in remote-curl updates refs' '
 368        SERVER="$HTTPD_DOCUMENT_ROOT_PATH/twobranch" &&
 369        rm -rf "$SERVER" client &&
 370
 371        git init "$SERVER" &&
 372        test_commit -C "$SERVER" foo &&
 373        git -C "$SERVER" update-ref refs/heads/anotherbranch foo &&
 374
 375        git clone $HTTPD_URL/smart/twobranch client &&
 376
 377        test_commit -C "$SERVER" bar &&
 378        git -C client -c protocol.version=0 fetch &&
 379
 380        git -C "$SERVER" rev-parse master >expect &&
 381        git -C client rev-parse origin/master >actual &&
 382        test_cmp expect actual
 383'
 384
 385test_expect_success 'fetch by SHA-1 without tag following' '
 386        SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
 387        rm -rf "$SERVER" client &&
 388
 389        git init "$SERVER" &&
 390        test_commit -C "$SERVER" foo &&
 391
 392        git clone $HTTPD_URL/smart/server client &&
 393
 394        test_commit -C "$SERVER" bar &&
 395        git -C "$SERVER" rev-parse bar >bar_hash &&
 396        git -C client -c protocol.version=0 fetch \
 397                --no-tags origin $(cat bar_hash)
 398'
 399
 400test_expect_success 'GIT_REDACT_COOKIES redacts cookies' '
 401        rm -rf clone &&
 402        echo "Set-Cookie: Foo=1" >cookies &&
 403        echo "Set-Cookie: Bar=2" >>cookies &&
 404        GIT_TRACE_CURL=true GIT_REDACT_COOKIES=Bar,Baz \
 405                git -c "http.cookieFile=$(pwd)/cookies" clone \
 406                $HTTPD_URL/smart/repo.git clone 2>err &&
 407        grep "Cookie:.*Foo=1" err &&
 408        grep "Cookie:.*Bar=<redacted>" err &&
 409        ! grep "Cookie:.*Bar=2" err
 410'
 411
 412test_expect_success 'GIT_REDACT_COOKIES handles empty values' '
 413        rm -rf clone &&
 414        echo "Set-Cookie: Foo=" >cookies &&
 415        GIT_TRACE_CURL=true GIT_REDACT_COOKIES=Foo \
 416                git -c "http.cookieFile=$(pwd)/cookies" clone \
 417                $HTTPD_URL/smart/repo.git clone 2>err &&
 418        grep "Cookie:.*Foo=<redacted>" err
 419'
 420
 421test_expect_success 'GIT_TRACE_CURL_NO_DATA prevents data from being traced' '
 422        rm -rf clone &&
 423        GIT_TRACE_CURL=true \
 424                git clone $HTTPD_URL/smart/repo.git clone 2>err &&
 425        grep "=> Send data" err &&
 426
 427        rm -rf clone &&
 428        GIT_TRACE_CURL=true GIT_TRACE_CURL_NO_DATA=1 \
 429                git clone $HTTPD_URL/smart/repo.git clone 2>err &&
 430        ! grep "=> Send data" err
 431'
 432
 433stop_httpd
 434test_done