t / t5551-http-fetch-smart.shon commit t/helper: add an empty test-tool program (efd71f8)
   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
  26cat >exp <<EOF
  27> GET /smart/repo.git/info/refs?service=git-upload-pack HTTP/1.1
  28> Accept: */*
  29> Accept-Encoding: gzip
  30> Pragma: no-cache
  31< HTTP/1.1 200 OK
  32< Pragma: no-cache
  33< Cache-Control: no-cache, max-age=0, must-revalidate
  34< Content-Type: application/x-git-upload-pack-advertisement
  35> POST /smart/repo.git/git-upload-pack HTTP/1.1
  36> Accept-Encoding: gzip
  37> Content-Type: application/x-git-upload-pack-request
  38> Accept: application/x-git-upload-pack-result
  39> Content-Length: xxx
  40< HTTP/1.1 200 OK
  41< Pragma: no-cache
  42< Cache-Control: no-cache, max-age=0, must-revalidate
  43< Content-Type: application/x-git-upload-pack-result
  44EOF
  45test_expect_success 'clone http repository' '
  46        GIT_TRACE_CURL=true git clone --quiet $HTTPD_URL/smart/repo.git clone 2>err &&
  47        test_cmp file clone/file &&
  48        tr '\''\015'\'' Q <err |
  49        sed -e "
  50                s/Q\$//
  51                /^[*] /d
  52                /^== Info:/d
  53                /^=> Send header, /d
  54                /^=> Send header:$/d
  55                /^<= Recv header, /d
  56                /^<= Recv header:$/d
  57                s/=> Send header: //
  58                s/= Recv header://
  59                /^<= Recv data/d
  60                /^=> Send data/d
  61                /^$/d
  62                /^< $/d
  63
  64                /^[^><]/{
  65                        s/^/> /
  66                }
  67
  68                /^> User-Agent: /d
  69                /^> Host: /d
  70                /^> POST /,$ {
  71                        /^> Accept: [*]\\/[*]/d
  72                }
  73                s/^> Content-Length: .*/> Content-Length: xxx/
  74                /^> 00..want /d
  75                /^> 00.*done/d
  76
  77                /^< Server: /d
  78                /^< Expires: /d
  79                /^< Date: /d
  80                /^< Content-Length: /d
  81                /^< Transfer-Encoding: /d
  82        " >act &&
  83        test_cmp exp act
  84'
  85
  86test_expect_success 'fetch changes via http' '
  87        echo content >>file &&
  88        git commit -a -m two &&
  89        git push public &&
  90        (cd clone && git pull) &&
  91        test_cmp file clone/file
  92'
  93
  94cat >exp <<EOF
  95GET  /smart/repo.git/info/refs?service=git-upload-pack HTTP/1.1 200
  96POST /smart/repo.git/git-upload-pack HTTP/1.1 200
  97GET  /smart/repo.git/info/refs?service=git-upload-pack HTTP/1.1 200
  98POST /smart/repo.git/git-upload-pack HTTP/1.1 200
  99EOF
 100test_expect_success 'used upload-pack service' '
 101        sed -e "
 102                s/^.* \"//
 103                s/\"//
 104                s/ [1-9][0-9]*\$//
 105                s/^GET /GET  /
 106        " >act <"$HTTPD_ROOT_PATH"/access.log &&
 107        test_cmp exp act
 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
 207cat >cookies.txt <<EOF
 208127.0.0.1       FALSE   /smart_cookies/ FALSE   0       othername       othervalue
 209EOF
 210cat >expect_cookies.txt <<EOF
 211
 212127.0.0.1       FALSE   /smart_cookies/ FALSE   0       othername       othervalue
 213127.0.0.1       FALSE   /smart_cookies/repo.git/info/   FALSE   0       name    value
 214EOF
 215test_expect_success 'cookies stored in http.cookiefile when http.savecookies set' '
 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 >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 'GIT_REDACT_COOKIES redacts cookies' '
 368        rm -rf clone &&
 369        echo "Set-Cookie: Foo=1" >cookies &&
 370        echo "Set-Cookie: Bar=2" >>cookies &&
 371        GIT_TRACE_CURL=true GIT_REDACT_COOKIES=Bar,Baz \
 372                git -c "http.cookieFile=$(pwd)/cookies" clone \
 373                $HTTPD_URL/smart/repo.git clone 2>err &&
 374        grep "Cookie:.*Foo=1" err &&
 375        grep "Cookie:.*Bar=<redacted>" err &&
 376        ! grep "Cookie:.*Bar=2" err
 377'
 378
 379test_expect_success 'GIT_REDACT_COOKIES handles empty values' '
 380        rm -rf clone &&
 381        echo "Set-Cookie: Foo=" >cookies &&
 382        GIT_TRACE_CURL=true GIT_REDACT_COOKIES=Foo \
 383                git -c "http.cookieFile=$(pwd)/cookies" clone \
 384                $HTTPD_URL/smart/repo.git clone 2>err &&
 385        grep "Cookie:.*Foo=<redacted>" err
 386'
 387
 388test_expect_success 'GIT_TRACE_CURL_NO_DATA prevents data from being traced' '
 389        rm -rf clone &&
 390        GIT_TRACE_CURL=true \
 391                git clone $HTTPD_URL/smart/repo.git clone 2>err &&
 392        grep "=> Send data" err &&
 393
 394        rm -rf clone &&
 395        GIT_TRACE_CURL=true GIT_TRACE_CURL_NO_DATA=1 \
 396                git clone $HTTPD_URL/smart/repo.git clone 2>err &&
 397        ! grep "=> Send data" err
 398'
 399
 400stop_httpd
 401test_done