]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
tests: fix capability testing
authorThomas Weißschuh <thomas@t-8ch.de>
Tue, 12 Sep 2023 08:34:37 +0000 (10:34 +0200)
committerThomas Weißschuh <thomas@t-8ch.de>
Tue, 12 Sep 2023 18:00:59 +0000 (20:00 +0200)
The old capability testing logic would incorrectly interpret the following
output from getpcaps as cap_wake_alarm being supported:

$ getpcaps 0
0: cap_wake_alarm=i

Instead of realying on brittle command output parsing at a test helper
to detect capabilities.

Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
meson.build
tests/commands.sh
tests/functions.sh
tests/helpers/Makemodule.am
tests/helpers/test_cap.c [new file with mode: 0644]
tests/ts/lsfd/mkfds-timerfd-alarm

index 8e3d6bb1d93dfc42afc4e1514bbd5b2a9fdeb630..76137715d100170f5265e05d6d27b635fd550574 100644 (file)
@@ -3300,6 +3300,14 @@ endif
 
 ############################################################
 
+exe = executable(
+  'test_cap',
+  'tests/helpers/test_cap.c',
+  include_directories : includes,
+  dependencies : [lib_cap_ng],
+  build_by_default: program_tests)
+exes += exe
+
 exe = executable(
   'test_mbsencode',
   'tests/helpers/test_mbsencode.c',
index 7dbe45a3e883862b457c38a6e0bfd74c30e16256..17b1132797b67285f34a3affa8de87ae7bd3d3b9 100644 (file)
@@ -4,6 +4,7 @@ TS_TESTUSER=${TS_TESTUSER:-"nobody"}
 # helpers
 TS_HELPER_BYTESWAP="${ts_helpersdir}test_byteswap"
 TS_HELPER_CPUSET="${ts_helpersdir}test_cpuset"
+TS_HELPER_CAP="${ts_helpersdir}test_cap"
 TS_HELPER_DMESG="${ts_helpersdir}test_dmesg"
 TS_HELPER_ENOSYS="${ts_helpersdir}test_enosys"
 TS_HELPER_ISLOCAL="${ts_helpersdir}test_islocal"
index 8a5dcb12acb71da0b219d8774ec98ae21b459fdd..5fe5ba07fd90c59dae993e73b5fe7de24fec26c2 100644 (file)
@@ -168,22 +168,11 @@ function ts_skip_nonroot {
 #      ts_skip_capability cap_wake_alarm
 #
 function ts_skip_capability {
-       local self=$$
-       local cap=$1
+       ts_check_prog "$TS_HELPER_CAP"
 
-       # On Fedora, libcap package provides getpcaps command.
-       ts_check_prog "getpcaps"
-
-       local caps=$(getpcaps "$self" 2>&1)
-       if [[ "$caps" == "${self}: =ep" || "$caps" == 'Capabilities for `'"${self}': =ep" ]]; then
-               return 0
-       fi
-
-       if [[ "$caps" =~ .*${cap}.* ]]; then
-               return 0
+       if ! "$TS_HELPER_CAP" "$1"; then
+               ts_skip "no capability: $1"
        fi
-
-       ts_skip "no capability: ${cap}"
 }
 
 function ts_skip_qemu_user {
index 6f0075f8388457979a8fb525ac85ed35ed62e681..a01828cba1591ea7b2c673d98bc75aba88b53d63 100644 (file)
@@ -39,3 +39,9 @@ test_mkfds_LDADD = $(LDADD) $(MQ_LIBS)
 check_PROGRAMS += test_enosys
 test_enosys_SOURCES = tests/helpers/test_enosys.c
 endif
+
+if HAVE_CAP_NG
+check_PROGRAMS += test_cap
+test_cap_SOURCES = tests/helpers/test_cap.c
+test_cap_LDADD = -lcap-ng
+endif
diff --git a/tests/helpers/test_cap.c b/tests/helpers/test_cap.c
new file mode 100644 (file)
index 0000000..658e86a
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * SPDX-License-Identifier: MIT
+ *
+ * Copyright (C) 2023 Thomas Weißschuh <thomas@t-8ch.de>
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+
+#include <cap-ng.h>
+
+int main(int argc, char **argv)
+{
+       int cap, r;
+
+       if (argc != 2)
+               return EXIT_FAILURE;
+
+       cap = capng_name_to_capability(argv[1]);
+       if (cap < 0) {
+               fprintf(stderr, "capng_name_to_capability(%s) failed\n", argv[1]);
+               return EXIT_FAILURE;
+       }
+
+       r = capng_get_caps_process();
+       if (r) {
+               fprintf(stderr, "capng_get_caps_process() failed\n");
+               return EXIT_FAILURE;
+       }
+
+       r = capng_have_capability(CAPNG_EFFECTIVE, cap);
+       return r ? EXIT_SUCCESS : EXIT_FAILURE;
+}
index bd40d559d4203de08d33168610907d7835a87e03..c7e6650c8b951ee7831d04b41d023638804e9138 100755 (executable)
@@ -20,7 +20,7 @@ TS_DESC="timerfd associating alarm"
 . "$TS_TOPDIR"/functions.sh
 ts_init "$*"
 
-ts_skip_capability cap_wake_alarm
+ts_skip_capability WAKE_ALARM
 
 ts_check_test_command "$TS_CMD_LSFD"
 ts_check_test_command "$TS_HELPER_MKFDS"