t / t5551-http-fetch-smart.shon commit t/lib-httpd: avoid occasional failures when checking access.log (e8b3b2e)
   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        check_access_log exp
 102'
 103
 104test_expect_success 'follow redirects (301)' '
 105        git clone $HTTPD_URL/smart-redir-perm/repo.git --quiet repo-p
 106'
 107
 108test_expect_success 'follow redirects (302)' '
 109        git clone $HTTPD_URL/smart-redir-temp/repo.git --quiet repo-t
 110'
 111
 112test_expect_success 'redirects re-root further requests' '
 113        git clone $HTTPD_URL/smart-redir-limited/repo.git repo-redir-limited
 114'
 115
 116test_expect_success 're-rooting dies on insane schemes' '
 117        test_must_fail git clone $HTTPD_URL/insane-redir/repo.git insane
 118'
 119
 120test_expect_success 'clone from password-protected repository' '
 121        echo two >expect &&
 122        set_askpass user@host pass@host &&
 123        git clone --bare "$HTTPD_URL/auth/smart/repo.git" smart-auth &&
 124        expect_askpass both user@host &&
 125        git --git-dir=smart-auth log -1 --format=%s >actual &&
 126        test_cmp expect actual
 127'
 128
 129test_expect_success 'clone from auth-only-for-push repository' '
 130        echo two >expect &&
 131        set_askpass wrong &&
 132        git clone --bare "$HTTPD_URL/auth-push/smart/repo.git" smart-noauth &&
 133        expect_askpass none &&
 134        git --git-dir=smart-noauth log -1 --format=%s >actual &&
 135        test_cmp expect actual
 136'
 137
 138test_expect_success 'clone from auth-only-for-objects repository' '
 139        echo two >expect &&
 140        set_askpass user@host pass@host &&
 141        git clone --bare "$HTTPD_URL/auth-fetch/smart/repo.git" half-auth &&
 142        expect_askpass both user@host &&
 143        git --git-dir=half-auth log -1 --format=%s >actual &&
 144        test_cmp expect actual
 145'
 146
 147test_expect_success 'no-op half-auth fetch does not require a password' '
 148        set_askpass wrong &&
 149        git --git-dir=half-auth fetch &&
 150        expect_askpass none
 151'
 152
 153test_expect_success 'redirects send auth to new location' '
 154        set_askpass user@host pass@host &&
 155        git -c credential.useHttpPath=true \
 156          clone $HTTPD_URL/smart-redir-auth/repo.git repo-redir-auth &&
 157        expect_askpass both user@host auth/smart/repo.git
 158'
 159
 160test_expect_success 'disable dumb http on server' '
 161        git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
 162                config http.getanyfile false
 163'
 164
 165test_expect_success 'GIT_SMART_HTTP can disable smart http' '
 166        (GIT_SMART_HTTP=0 &&
 167         export GIT_SMART_HTTP &&
 168         cd clone &&
 169         test_must_fail git fetch)
 170'
 171
 172test_expect_success 'invalid Content-Type rejected' '
 173        test_must_fail git clone $HTTPD_URL/broken_smart/repo.git 2>actual &&
 174        grep "not valid:" actual
 175'
 176
 177test_expect_success 'create namespaced refs' '
 178        test_commit namespaced &&
 179        git push public HEAD:refs/namespaces/ns/refs/heads/master &&
 180        git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
 181                symbolic-ref refs/namespaces/ns/HEAD refs/namespaces/ns/refs/heads/master
 182'
 183
 184test_expect_success 'smart clone respects namespace' '
 185        git clone "$HTTPD_URL/smart_namespace/repo.git" ns-smart &&
 186        echo namespaced >expect &&
 187        git --git-dir=ns-smart/.git log -1 --format=%s >actual &&
 188        test_cmp expect actual
 189'
 190
 191test_expect_success 'dumb clone via http-backend respects namespace' '
 192        git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
 193                config http.getanyfile true &&
 194        GIT_SMART_HTTP=0 git clone \
 195                "$HTTPD_URL/smart_namespace/repo.git" ns-dumb &&
 196        echo namespaced >expect &&
 197        git --git-dir=ns-dumb/.git log -1 --format=%s >actual &&
 198        test_cmp expect actual
 199'
 200
 201cat >cookies.txt <<EOF
 202127.0.0.1       FALSE   /smart_cookies/ FALSE   0       othername       othervalue
 203EOF
 204cat >expect_cookies.txt <<EOF
 205
 206127.0.0.1       FALSE   /smart_cookies/ FALSE   0       othername       othervalue
 207127.0.0.1       FALSE   /smart_cookies/repo.git/info/   FALSE   0       name    value
 208EOF
 209test_expect_success 'cookies stored in http.cookiefile when http.savecookies set' '
 210        git config http.cookiefile cookies.txt &&
 211        git config http.savecookies true &&
 212        git ls-remote $HTTPD_URL/smart_cookies/repo.git master &&
 213        tail -3 cookies.txt >cookies_tail.txt &&
 214        test_cmp expect_cookies.txt cookies_tail.txt
 215'
 216
 217test_expect_success 'transfer.hiderefs works over smart-http' '
 218        test_commit hidden &&
 219        test_commit visible &&
 220        git push public HEAD^:refs/heads/a HEAD:refs/heads/b &&
 221        git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
 222                config transfer.hiderefs refs/heads/a &&
 223        git clone --bare "$HTTPD_URL/smart/repo.git" hidden.git &&
 224        test_must_fail git -C hidden.git rev-parse --verify a &&
 225        git -C hidden.git rev-parse --verify b
 226'
 227
 228# create an arbitrary number of tags, numbered from tag-$1 to tag-$2
 229create_tags () {
 230        rm -f marks &&
 231        for i in $(test_seq "$1" "$2")
 232        do
 233                # don't use here-doc, because it requires a process
 234                # per loop iteration
 235                echo "commit refs/heads/too-many-refs-$1" &&
 236                echo "mark :$i" &&
 237                echo "committer git <git@example.com> $i +0000" &&
 238                echo "data 0" &&
 239                echo "M 644 inline bla.txt" &&
 240                echo "data 4" &&
 241                echo "bla" &&
 242                # make every commit dangling by always
 243                # rewinding the branch after each commit
 244                echo "reset refs/heads/too-many-refs-$1" &&
 245                echo "from :$1"
 246        done | git fast-import --export-marks=marks &&
 247
 248        # now assign tags to all the dangling commits we created above
 249        tag=$(perl -e "print \"bla\" x 30") &&
 250        sed -e "s|^:\([^ ]*\) \(.*\)$|\2 refs/tags/$tag-\1|" <marks >>packed-refs
 251}
 252
 253test_expect_success 'create 2,000 tags in the repo' '
 254        (
 255                cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
 256                create_tags 1 2000
 257        )
 258'
 259
 260test_expect_success CMDLINE_LIMIT \
 261        'clone the 2,000 tag repo to check OS command line overflow' '
 262        run_with_limited_cmdline git clone $HTTPD_URL/smart/repo.git too-many-refs &&
 263        (
 264                cd too-many-refs &&
 265                git for-each-ref refs/tags >actual &&
 266                test_line_count = 2000 actual
 267        )
 268'
 269
 270test_expect_success 'large fetch-pack requests can be split across POSTs' '
 271        GIT_TRACE_CURL=true git -c http.postbuffer=65536 \
 272                clone --bare "$HTTPD_URL/smart/repo.git" split.git 2>err &&
 273        grep "^=> Send header: POST" err >posts &&
 274        test_line_count = 2 posts
 275'
 276
 277test_expect_success 'test allowreachablesha1inwant' '
 278        test_when_finished "rm -rf test_reachable.git" &&
 279        server="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
 280        master_sha=$(git -C "$server" rev-parse refs/heads/master) &&
 281        git -C "$server" config uploadpack.allowreachablesha1inwant 1 &&
 282
 283        git init --bare test_reachable.git &&
 284        git -C test_reachable.git remote add origin "$HTTPD_URL/smart/repo.git" &&
 285        git -C test_reachable.git fetch origin "$master_sha"
 286'
 287
 288test_expect_success 'test allowreachablesha1inwant with unreachable' '
 289        test_when_finished "rm -rf test_reachable.git; git reset --hard $(git rev-parse HEAD)" &&
 290
 291        #create unreachable sha
 292        echo content >file2 &&
 293        git add file2 &&
 294        git commit -m two &&
 295        git push public HEAD:refs/heads/doomed &&
 296        git push public :refs/heads/doomed &&
 297
 298        server="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
 299        master_sha=$(git -C "$server" rev-parse refs/heads/master) &&
 300        git -C "$server" config uploadpack.allowreachablesha1inwant 1 &&
 301
 302        git init --bare test_reachable.git &&
 303        git -C test_reachable.git remote add origin "$HTTPD_URL/smart/repo.git" &&
 304        test_must_fail git -C test_reachable.git fetch origin "$(git rev-parse HEAD)"
 305'
 306
 307test_expect_success 'test allowanysha1inwant with unreachable' '
 308        test_when_finished "rm -rf test_reachable.git; git reset --hard $(git rev-parse HEAD)" &&
 309
 310        #create unreachable sha
 311        echo content >file2 &&
 312        git add file2 &&
 313        git commit -m two &&
 314        git push public HEAD:refs/heads/doomed &&
 315        git push public :refs/heads/doomed &&
 316
 317        server="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
 318        master_sha=$(git -C "$server" rev-parse refs/heads/master) &&
 319        git -C "$server" config uploadpack.allowreachablesha1inwant 1 &&
 320
 321        git init --bare test_reachable.git &&
 322        git -C test_reachable.git remote add origin "$HTTPD_URL/smart/repo.git" &&
 323        test_must_fail git -C test_reachable.git fetch origin "$(git rev-parse HEAD)" &&
 324
 325        git -C "$server" config uploadpack.allowanysha1inwant 1 &&
 326        git -C test_reachable.git fetch origin "$(git rev-parse HEAD)"
 327'
 328
 329test_expect_success EXPENSIVE 'http can handle enormous ref negotiation' '
 330        (
 331                cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
 332                create_tags 2001 50000
 333        ) &&
 334        git -C too-many-refs fetch -q --tags &&
 335        (
 336                cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
 337                create_tags 50001 100000
 338        ) &&
 339        git -C too-many-refs fetch -q --tags &&
 340        git -C too-many-refs for-each-ref refs/tags >tags &&
 341        test_line_count = 100000 tags
 342'
 343
 344test_expect_success 'custom http headers' '
 345        test_must_fail git -c http.extraheader="x-magic-two: cadabra" \
 346                fetch "$HTTPD_URL/smart_headers/repo.git" &&
 347        git -c http.extraheader="x-magic-one: abra" \
 348            -c http.extraheader="x-magic-two: cadabra" \
 349            fetch "$HTTPD_URL/smart_headers/repo.git" &&
 350        git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
 351        git config -f .gitmodules submodule.sub.path sub &&
 352        git config -f .gitmodules submodule.sub.url \
 353                "$HTTPD_URL/smart_headers/repo.git" &&
 354        git submodule init sub &&
 355        test_must_fail git submodule update sub &&
 356        git -c http.extraheader="x-magic-one: abra" \
 357            -c http.extraheader="x-magic-two: cadabra" \
 358                submodule update sub
 359'
 360
 361stop_httpd
 362test_done