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