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