t / t5550-http-fetch.shon commit Merge branch 'pg/maint-apply-remove-unused-variable' into maint-1.7.11 (10a32fa)
   1#!/bin/sh
   2
   3test_description='test dumb fetching over http via static file'
   4. ./test-lib.sh
   5
   6if test -n "$NO_CURL"; then
   7        skip_all='skipping test, git built without http support'
   8        test_done
   9fi
  10
  11LIB_HTTPD_PORT=${LIB_HTTPD_PORT-'5550'}
  12. "$TEST_DIRECTORY"/lib-httpd.sh
  13start_httpd
  14
  15test_expect_success 'setup repository' '
  16        echo content1 >file &&
  17        git add file &&
  18        git commit -m one
  19        echo content2 >file &&
  20        git add file &&
  21        git commit -m two
  22'
  23
  24test_expect_success 'create http-accessible bare repository with loose objects' '
  25        cp -a .git "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
  26        (cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
  27         git config core.bare true &&
  28         mkdir -p hooks &&
  29         echo "exec git update-server-info" >hooks/post-update &&
  30         chmod +x hooks/post-update &&
  31         hooks/post-update
  32        ) &&
  33        git remote add public "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
  34        git push public master:master
  35'
  36
  37test_expect_success 'clone http repository' '
  38        git clone $HTTPD_URL/dumb/repo.git clone-tmpl &&
  39        cp -R clone-tmpl clone &&
  40        test_cmp file clone/file
  41'
  42
  43test_expect_success 'create password-protected repository' '
  44        mkdir "$HTTPD_DOCUMENT_ROOT_PATH/auth/" &&
  45        cp -Rf "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
  46               "$HTTPD_DOCUMENT_ROOT_PATH/auth/repo.git"
  47'
  48
  49test_expect_success 'setup askpass helpers' '
  50        cat >askpass <<-EOF &&
  51        #!/bin/sh
  52        echo >>"$PWD/askpass-query" "askpass: \$*" &&
  53        cat "$PWD/askpass-response"
  54        EOF
  55        chmod +x askpass &&
  56        GIT_ASKPASS="$PWD/askpass" &&
  57        export GIT_ASKPASS
  58'
  59
  60expect_askpass() {
  61        dest=$HTTPD_DEST
  62        {
  63                case "$1" in
  64                none)
  65                        ;;
  66                pass)
  67                        echo "askpass: Password for 'http://$2@$dest': "
  68                        ;;
  69                both)
  70                        echo "askpass: Username for 'http://$dest': "
  71                        echo "askpass: Password for 'http://$2@$dest': "
  72                        ;;
  73                *)
  74                        false
  75                        ;;
  76                esac
  77        } >askpass-expect &&
  78        test_cmp askpass-expect askpass-query
  79}
  80
  81test_expect_success 'cloning password-protected repository can fail' '
  82        >askpass-query &&
  83        echo wrong >askpass-response &&
  84        test_must_fail git clone "$HTTPD_URL/auth/repo.git" clone-auth-fail &&
  85        expect_askpass both wrong
  86'
  87
  88test_expect_success 'http auth can use user/pass in URL' '
  89        >askpass-query &&
  90        echo wrong >askpass-response &&
  91        git clone "$HTTPD_URL_USER_PASS/auth/repo.git" clone-auth-none &&
  92        expect_askpass none
  93'
  94
  95test_expect_success 'http auth can use just user in URL' '
  96        >askpass-query &&
  97        echo user@host >askpass-response &&
  98        git clone "$HTTPD_URL_USER/auth/repo.git" clone-auth-pass &&
  99        expect_askpass pass user@host
 100'
 101
 102test_expect_success 'http auth can request both user and pass' '
 103        >askpass-query &&
 104        echo user@host >askpass-response &&
 105        git clone "$HTTPD_URL/auth/repo.git" clone-auth-both &&
 106        expect_askpass both user@host
 107'
 108
 109test_expect_success 'http auth respects credential helper config' '
 110        test_config_global credential.helper "!f() {
 111                cat >/dev/null
 112                echo username=user@host
 113                echo password=user@host
 114        }; f" &&
 115        >askpass-query &&
 116        echo wrong >askpass-response &&
 117        git clone "$HTTPD_URL/auth/repo.git" clone-auth-helper &&
 118        expect_askpass none
 119'
 120
 121test_expect_success 'http auth can get username from config' '
 122        test_config_global "credential.$HTTPD_URL.username" user@host &&
 123        >askpass-query &&
 124        echo user@host >askpass-response &&
 125        git clone "$HTTPD_URL/auth/repo.git" clone-auth-user &&
 126        expect_askpass pass user@host
 127'
 128
 129test_expect_success 'configured username does not override URL' '
 130        test_config_global "credential.$HTTPD_URL.username" wrong &&
 131        >askpass-query &&
 132        echo user@host >askpass-response &&
 133        git clone "$HTTPD_URL_USER/auth/repo.git" clone-auth-user2 &&
 134        expect_askpass pass user@host
 135'
 136
 137test_expect_success 'fetch changes via http' '
 138        echo content >>file &&
 139        git commit -a -m two &&
 140        git push public &&
 141        (cd clone && git pull) &&
 142        test_cmp file clone/file
 143'
 144
 145test_expect_success 'fetch changes via manual http-fetch' '
 146        cp -R clone-tmpl clone2 &&
 147
 148        HEAD=$(git rev-parse --verify HEAD) &&
 149        (cd clone2 &&
 150         git http-fetch -a -w heads/master-new $HEAD $(git config remote.origin.url) &&
 151         git checkout master-new &&
 152         test $HEAD = $(git rev-parse --verify HEAD)) &&
 153        test_cmp file clone2/file
 154'
 155
 156test_expect_success 'http remote detects correct HEAD' '
 157        git push public master:other &&
 158        (cd clone &&
 159         git remote set-head origin -d &&
 160         git remote set-head origin -a &&
 161         git symbolic-ref refs/remotes/origin/HEAD > output &&
 162         echo refs/remotes/origin/master > expect &&
 163         test_cmp expect output
 164        )
 165'
 166
 167test_expect_success 'fetch packed objects' '
 168        cp -R "$HTTPD_DOCUMENT_ROOT_PATH"/repo.git "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git &&
 169        (cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git &&
 170         git --bare repack -a -d
 171        ) &&
 172        git clone $HTTPD_URL/dumb/repo_pack.git
 173'
 174
 175test_expect_success 'fetch notices corrupt pack' '
 176        cp -R "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad1.git &&
 177        (cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad1.git &&
 178         p=`ls objects/pack/pack-*.pack` &&
 179         chmod u+w $p &&
 180         printf %0256d 0 | dd of=$p bs=256 count=1 seek=1 conv=notrunc
 181        ) &&
 182        mkdir repo_bad1.git &&
 183        (cd repo_bad1.git &&
 184         git --bare init &&
 185         test_must_fail git --bare fetch $HTTPD_URL/dumb/repo_bad1.git &&
 186         test 0 = `ls objects/pack/pack-*.pack | wc -l`
 187        )
 188'
 189
 190test_expect_success 'fetch notices corrupt idx' '
 191        cp -R "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad2.git &&
 192        (cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad2.git &&
 193         p=`ls objects/pack/pack-*.idx` &&
 194         chmod u+w $p &&
 195         printf %0256d 0 | dd of=$p bs=256 count=1 seek=1 conv=notrunc
 196        ) &&
 197        mkdir repo_bad2.git &&
 198        (cd repo_bad2.git &&
 199         git --bare init &&
 200         test_must_fail git --bare fetch $HTTPD_URL/dumb/repo_bad2.git &&
 201         test 0 = `ls objects/pack | wc -l`
 202        )
 203'
 204
 205test_expect_success 'did not use upload-pack service' '
 206        grep '/git-upload-pack' <"$HTTPD_ROOT_PATH"/access.log >act
 207        : >exp
 208        test_cmp exp act
 209'
 210
 211stop_httpd
 212test_done