t / t5570-git-daemon.shon commit t/t3600-rm.sh: use the $( ... ) construct for command substitution (e3ab3bc)
   1#!/bin/sh
   2
   3test_description='test fetching over git protocol'
   4. ./test-lib.sh
   5
   6. "$TEST_DIRECTORY"/lib-git-daemon.sh
   7start_git_daemon
   8
   9test_expect_success 'setup repository' '
  10        git config push.default matching &&
  11        echo content >file &&
  12        git add file &&
  13        git commit -m one
  14'
  15
  16test_expect_success 'create git-accessible bare repository' '
  17        mkdir "$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git" &&
  18        (cd "$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git" &&
  19         git --bare init &&
  20         : >git-daemon-export-ok
  21        ) &&
  22        git remote add public "$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git" &&
  23        git push public master:master
  24'
  25
  26test_expect_success 'clone git repository' '
  27        git clone "$GIT_DAEMON_URL/repo.git" clone &&
  28        test_cmp file clone/file
  29'
  30
  31test_expect_success 'fetch changes via git protocol' '
  32        echo content >>file &&
  33        git commit -a -m two &&
  34        git push public &&
  35        (cd clone && git pull) &&
  36        test_cmp file clone/file
  37'
  38
  39test_expect_success 'remote detects correct HEAD' '
  40        git push public master:other &&
  41        (cd clone &&
  42         git remote set-head -d origin &&
  43         git remote set-head -a origin &&
  44         git symbolic-ref refs/remotes/origin/HEAD > output &&
  45         echo refs/remotes/origin/master > expect &&
  46         test_cmp expect output
  47        )
  48'
  49
  50test_expect_success 'prepare pack objects' '
  51        cp -R "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo.git "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_pack.git &&
  52        (cd "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_pack.git &&
  53         git --bare repack -a -d
  54        )
  55'
  56
  57test_expect_success 'fetch notices corrupt pack' '
  58        cp -R "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_pack.git "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_bad1.git &&
  59        (cd "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_bad1.git &&
  60         p=`ls objects/pack/pack-*.pack` &&
  61         chmod u+w $p &&
  62         printf %0256d 0 | dd of=$p bs=256 count=1 seek=1 conv=notrunc
  63        ) &&
  64        mkdir repo_bad1.git &&
  65        (cd repo_bad1.git &&
  66         git --bare init &&
  67         test_must_fail git --bare fetch "$GIT_DAEMON_URL/repo_bad1.git" &&
  68         test 0 = `ls objects/pack/pack-*.pack | wc -l`
  69        )
  70'
  71
  72test_expect_success 'fetch notices corrupt idx' '
  73        cp -R "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_pack.git "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_bad2.git &&
  74        (cd "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_bad2.git &&
  75         p=`ls objects/pack/pack-*.idx` &&
  76         chmod u+w $p &&
  77         printf %0256d 0 | dd of=$p bs=256 count=1 seek=1 conv=notrunc
  78        ) &&
  79        mkdir repo_bad2.git &&
  80        (cd repo_bad2.git &&
  81         git --bare init &&
  82         test_must_fail git --bare fetch "$GIT_DAEMON_URL/repo_bad2.git" &&
  83         test 0 = `ls objects/pack | wc -l`
  84        )
  85'
  86
  87test_remote_error()
  88{
  89        do_export=YesPlease
  90        while test $# -gt 0
  91        do
  92                case $1 in
  93                -x)
  94                        shift
  95                        chmod -x "$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git"
  96                        ;;
  97                -n)
  98                        shift
  99                        do_export=
 100                        ;;
 101                *)
 102                        break
 103                esac
 104        done
 105
 106        msg=$1
 107        shift
 108        cmd=$1
 109        shift
 110        repo=$1
 111        shift || error "invalid number of arguments"
 112
 113        if test -x "$GIT_DAEMON_DOCUMENT_ROOT_PATH/$repo"
 114        then
 115                if test -n "$do_export"
 116                then
 117                        : >"$GIT_DAEMON_DOCUMENT_ROOT_PATH/$repo/git-daemon-export-ok"
 118                else
 119                        rm -f "$GIT_DAEMON_DOCUMENT_ROOT_PATH/$repo/git-daemon-export-ok"
 120                fi
 121        fi
 122
 123        test_must_fail git "$cmd" "$GIT_DAEMON_URL/$repo" "$@" 2>output &&
 124        test_i18ngrep "fatal: remote error: $msg: /$repo" output &&
 125        ret=$?
 126        chmod +x "$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git"
 127        (exit $ret)
 128}
 129
 130msg="access denied or repository not exported"
 131test_expect_success 'clone non-existent' "test_remote_error    '$msg' clone nowhere.git    "
 132test_expect_success 'push disabled'      "test_remote_error    '$msg' push  repo.git master"
 133test_expect_success 'read access denied' "test_remote_error -x '$msg' fetch repo.git       "
 134test_expect_success 'not exported'       "test_remote_error -n '$msg' fetch repo.git       "
 135
 136stop_git_daemon
 137start_git_daemon --informative-errors
 138
 139test_expect_success 'clone non-existent' "test_remote_error    'no such repository'      clone nowhere.git    "
 140test_expect_success 'push disabled'      "test_remote_error    'service not enabled'     push  repo.git master"
 141test_expect_success 'read access denied' "test_remote_error -x 'no such repository'      fetch repo.git       "
 142test_expect_success 'not exported'       "test_remote_error -n 'repository not exported' fetch repo.git       "
 143
 144stop_git_daemon
 145start_git_daemon --interpolated-path="$GIT_DAEMON_DOCUMENT_ROOT_PATH/%H%D"
 146
 147test_expect_success 'access repo via interpolated hostname' '
 148        repo="$GIT_DAEMON_DOCUMENT_ROOT_PATH/localhost/interp.git" &&
 149        git init --bare "$repo" &&
 150        git push "$repo" HEAD &&
 151        >"$repo"/git-daemon-export-ok &&
 152        rm -rf tmp.git &&
 153        GIT_OVERRIDE_VIRTUAL_HOST=localhost \
 154                git clone --bare "$GIT_DAEMON_URL/interp.git" tmp.git &&
 155        rm -rf tmp.git &&
 156        GIT_OVERRIDE_VIRTUAL_HOST=LOCALHOST \
 157                git clone --bare "$GIT_DAEMON_URL/interp.git" tmp.git
 158'
 159
 160test_expect_success 'hostname cannot break out of directory' '
 161        rm -rf tmp.git &&
 162        repo="$GIT_DAEMON_DOCUMENT_ROOT_PATH/../escape.git" &&
 163        git init --bare "$repo" &&
 164        git push "$repo" HEAD &&
 165        >"$repo"/git-daemon-export-ok &&
 166        test_must_fail \
 167                env GIT_OVERRIDE_VIRTUAL_HOST=.. \
 168                git clone --bare "$GIT_DAEMON_URL/escape.git" tmp.git
 169'
 170
 171stop_git_daemon
 172test_done