chown: port test to macOS 12.6 nogroup user
[coreutils.git] / tests / chown / preserve-root.sh
blob9f28679fcce1de3602fa78269074ef35e0f19d6d
1 #!/bin/sh
2 # Verify that --preserve-root works.
4 # Copyright (C) 2006-2024 Free Software Foundation, Inc.
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <https://www.gnu.org/licenses/>.
19 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20 print_ver_ chown
21 skip_if_root_
23 id_g=$(id -g) && test -n "$id_g" || framework_failure_
24 case $(id -gn) in
25 nogroup) id_g= ;; # In macOS, users can be in nogroup but cannot chgrp to it.
26 esac
28 mkdir d && ln -s / d/slink-to-root
31 # Even if --preserve-root were to malfunction, allowing the chown or
32 # chgrp to traverse through "/", since we're running as non-root,
33 # they would be very unlikely to cause any changes.
34 chown -R --preserve-root 0 / > out 2>&1 && fail=1
35 chgrp -R --preserve-root 0 / >> out 2>&1 && fail=1
37 # Here, if --preserve-root were to malfunction, chmod could make changes,
38 # but only to files owned and unreadable by the user running this test,
39 # and then, only to make them readable by owner.
40 chmod -R --preserve-root u+r / >> out 2>&1 && fail=1
42 # With -RHh, --preserve-root should trigger nothing,
43 # since the symlink in question is not a command line argument.
44 # Contrary to the above commands, these two should succeed.
45 echo '==== test -RHh' >> out || framework_failure_
46 chown -RHh --preserve-root $(id -u) d >> out 2>&1 || fail=1
47 test -z "$id_g" || chgrp -RHh --preserve-root "$id_g" d >> out 2>&1 || fail=1
49 # These must fail.
50 echo '==== test -RLh' >> out || framework_failure_
51 chown -RLh --preserve-root $(id -u) d >> out 2>&1 && fail=1
52 test -n "$id_g" && chgrp -RLh --preserve-root "$id_g" d >> out 2>&1 && fail=1
55 cat <<\EOF &&
56 chown: it is dangerous to operate recursively on '/'
57 chown: use --no-preserve-root to override this failsafe
58 chgrp: it is dangerous to operate recursively on '/'
59 chgrp: use --no-preserve-root to override this failsafe
60 chmod: it is dangerous to operate recursively on '/'
61 chmod: use --no-preserve-root to override this failsafe
62 ==== test -RHh
63 ==== test -RLh
64 chown: it is dangerous to operate recursively on 'd/slink-to-root' (same as '/')
65 chown: use --no-preserve-root to override this failsafe
66 EOF
67 { test -z "$id_g" || cat <<\EOF; }
68 chgrp: it is dangerous to operate recursively on 'd/slink-to-root' (same as '/')
69 chgrp: use --no-preserve-root to override this failsafe
70 EOF
71 ) > exp || framework_failure_
73 compare exp out || fail=1
75 Exit $fail