1#!/bin/sh
2# Ensure "ls --color" properly colors name of file with capability.
3
4# Copyright (C) 2008-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_ ls printf
21require_root_
22
23grep '^#define HAVE_CAP 1' $CONFIG_HEADER > /dev/null \
24  || skip_ "configured without libcap support"
25
26(setcap --help) 2>&1 |grep 'usage: setcap' > /dev/null \
27  || skip_ "setcap utility not found"
28
29# Don't let a different umask perturb the results.
30umask 22
31
32# We create 2 files of the same name as
33# before coreutils 8.1 only the name rather than
34# the full path was used to read the capabilities
35# thus giving false positives and negatives.
36mkdir test test/dir
37cd test
38touch cap_pos dir/cap_pos dir/cap_neg
39for file in cap_pos dir/cap_neg; do
40  setcap 'cap_net_bind_service=ep' $file ||
41    skip_ "setcap doesn't work"
42done
43
44code='30;41'
45# Note we explicitly disable "executable" coloring
46# so that capability coloring is not dependent on it,
47# as was the case before coreutils 8.1
48for ex in '' ex=:; do
49  LS_COLORS="di=:${ex}ca=$code" \
50    ls --color=always cap_pos dir > out || fail=1
51
52  env printf "\
53\e[0m\e[${code}mcap_pos\e[0m
54
55dir:
56\e[${code}mcap_neg\e[0m
57cap_pos
58" > out_ok || framework_failure_
59
60  compare out out_ok || fail=1
61done
62
63Exit $fail
64