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