1#!/bin/sh
2# Ensure cp --preserves copies capabilities
3
4# Copyright (C) 2010-2023 Free Software Foundation, Inc.
5
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.
10
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.
15
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/>.
18
19. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20print_ver_ cp
21require_root_
22working_umask_or_skip_
23
24
25grep '^#define HAVE_CAP 1' $CONFIG_HEADER > /dev/null \
26  || skip_ "configured without libcap support"
27
28(setcap --help) 2>&1 |grep 'usage: setcap' > /dev/null \
29  || skip_ "setcap utility not found"
30(getcap --help) 2>&1 |grep 'usage: getcap' > /dev/null \
31  || skip_ "getcap utility not found"
32
33
34touch file || framework_failure_
35chown $NON_ROOT_USERNAME file || framework_failure_
36
37setcap 'cap_net_bind_service=ep' file ||
38  skip_ "setcap doesn't work"
39getcap file | grep cap_net_bind_service >/dev/null ||
40  skip_ "getcap doesn't work"
41
42cp --preserve=xattr file copy1 || fail=1
43
44# Before coreutils 8.5 the capabilities would not be preserved,
45# as the owner was set _after_ copying xattrs, thus clearing any capabilities.
46cp --preserve=all   file copy2 || fail=1
47
48for file in copy1 copy2; do
49  getcap $file | grep cap_net_bind_service >/dev/null || fail=1
50done
51
52Exit $fail
53