| 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='verify safe.directory checks while running as root' |
| 4 | |
| 5 | . ./test-lib.sh |
| 6 | . "$TEST_DIRECTORY"/lib-sudo.sh |
| 7 | |
| 8 | if [ "$GIT_TEST_ALLOW_SUDO" != "YES" ] |
| 9 | then |
| 10 | skip_all="You must set env var GIT_TEST_ALLOW_SUDO=YES in order to run this test" |
| 11 | test_done |
| 12 | fi |
| 13 | |
| 14 | if ! test_have_prereq NOT_ROOT |
| 15 | then |
| 16 | skip_all="These tests do not support running as root" |
| 17 | test_done |
| 18 | fi |
| 19 | |
| 20 | test_lazy_prereq SUDO ' |
| 21 | sudo -n id -u >u && |
| 22 | id -u root >r && |
| 23 | test_cmp u r && |
| 24 | command -v git >u && |
| 25 | sudo command -v git >r && |
| 26 | test_cmp u r |
| 27 | ' |
| 28 | |
| 29 | if ! test_have_prereq SUDO |
| 30 | then |
| 31 | skip_all="Your sudo/system configuration is either too strict or unsupported" |
| 32 | test_done |
| 33 | fi |
| 34 | |
| 35 | test_expect_success SUDO 'setup' ' |
| 36 | sudo rm -rf root && |
| 37 | mkdir -p root/r && |
| 38 | ( |
| 39 | cd root/r && |
| 40 | git init |
| 41 | ) |
| 42 | ' |
| 43 | |
| 44 | test_expect_success SUDO 'sudo git status as original owner' ' |
| 45 | ( |
| 46 | cd root/r && |
| 47 | git status && |
| 48 | sudo git status |
| 49 | ) |
| 50 | ' |
| 51 | |
| 52 | test_expect_success SUDO 'setup root owned repository' ' |
| 53 | sudo mkdir -p root/p && |
| 54 | sudo git init root/p |
| 55 | ' |
| 56 | |
| 57 | test_expect_success 'cannot access if owned by root' ' |
| 58 | ( |
| 59 | cd root/p && |
| 60 | test_must_fail git status |
| 61 | ) |
| 62 | ' |
| 63 | |
| 64 | test_expect_success 'can access if addressed explicitly' ' |
| 65 | ( |
| 66 | cd root/p && |
| 67 | GIT_DIR=.git GIT_WORK_TREE=. git status |
| 68 | ) |
| 69 | ' |
| 70 | |
| 71 | test_expect_success SUDO 'can access with sudo if root' ' |
| 72 | ( |
| 73 | cd root/p && |
| 74 | sudo git status |
| 75 | ) |
| 76 | ' |
| 77 | |
| 78 | test_expect_success SUDO 'can access with sudo if root by removing SUDO_UID' ' |
| 79 | ( |
| 80 | cd root/p && |
| 81 | run_with_sudo <<-END |
| 82 | unset SUDO_UID && |
| 83 | git status |
| 84 | END |
| 85 | ) |
| 86 | ' |
| 87 | |
| 88 | # this MUST be always the last test |
| 89 | test_expect_success SUDO 'cleanup' ' |
| 90 | sudo rm -rf root |
| 91 | ' |
| 92 | |
| 93 | test_done |