diff options
author | Michael Biebl <biebl@debian.org> | 2017-12-14 23:22:02 +0100 |
---|---|---|
committer | Michael Biebl <biebl@debian.org> | 2017-12-14 23:22:02 +0100 |
commit | 52ad194e0b816b8273dd8d0fea3e6d467f6ca34e (patch) | |
tree | 1a3b3117e015f200ca0ce23f5ad27be6d0a7b0fb /src/test/test-cap-list.c | |
parent | f5e65279187b6aa0c0c5a00b14dca9eab441ffb2 (diff) | |
download | systemd-52ad194e0b816b8273dd8d0fea3e6d467f6ca34e.tar.gz |
New upstream version 236
Diffstat (limited to 'src/test/test-cap-list.c')
-rw-r--r-- | src/test/test-cap-list.c | 101 |
1 files changed, 58 insertions, 43 deletions
diff --git a/src/test/test-cap-list.c b/src/test/test-cap-list.c index c1af277f34..935567cc23 100644 --- a/src/test/test-cap-list.c +++ b/src/test/test-cap-list.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd. @@ -22,7 +23,6 @@ #include "alloc-util.h" #include "cap-list.h" #include "capability-util.h" -#include "fileio.h" #include "parse-util.h" #include "string-util.h" #include "util.h" @@ -70,57 +70,72 @@ static void test_cap_list(void) { } } -/* verify cap_last_cap() against /proc/sys/kernel/cap_last_cap */ -static void test_last_cap_file(void) { - _cleanup_free_ char *content = NULL; - unsigned long val = 0; - int r; +static void test_capability_set_one(uint64_t c, const char *t) { + _cleanup_free_ char *t1 = NULL; + uint64_t c1, c_masked = c & ((UINT64_C(1) << capability_list_length()) - 1); - r = read_one_line_file("/proc/sys/kernel/cap_last_cap", &content); - assert_se(r >= 0); + assert_se(capability_set_to_string_alloc(c, &t1) == 0); + assert_se(streq(t1, t)); - r = safe_atolu(content, &val); - assert_se(r >= 0); - assert_se(val != 0); - assert_se(val == cap_last_cap()); -} - -/* verify cap_last_cap() against syscall probing */ -static void test_last_cap_probe(void) { - unsigned long p = (unsigned long)CAP_LAST_CAP; - - if (prctl(PR_CAPBSET_READ, p) < 0) { - for (p--; p > 0; p --) - if (prctl(PR_CAPBSET_READ, p) >= 0) - break; - } else { - for (;; p++) - if (prctl(PR_CAPBSET_READ, p+1) < 0) - break; - } + assert_se(capability_set_from_string(t1, &c1) == 0); + assert_se(c1 == c_masked); - assert_se(p != 0); - assert_se(p == cap_last_cap()); + free(t1); + assert_se(t1 = strjoin("'cap_chown cap_dac_override' \"cap_setgid cap_setuid\"", t, + " hogehoge foobar 12345 3.14 -3 ", t)); + assert_se(capability_set_from_string(t1, &c1) == 0); + assert_se(c1 == c_masked); } -static void test_capability_set_to_string_alloc(void) { - _cleanup_free_ char *t1 = NULL, *t2 = NULL, *t3 = NULL; - - assert_se(capability_set_to_string_alloc(0u, &t1) == 0); - assert_se(streq(t1, "")); - - assert_se(capability_set_to_string_alloc(1u<<CAP_DAC_OVERRIDE, &t2) == 0); - assert_se(streq(t2, "cap_dac_override")); - - assert_se(capability_set_to_string_alloc(UINT64_C(1)<<CAP_CHOWN | UINT64_C(1)<<CAP_DAC_OVERRIDE | UINT64_C(1)<<CAP_DAC_READ_SEARCH | UINT64_C(1)<<CAP_FOWNER | UINT64_C(1)<<CAP_SETGID | UINT64_C(1)<<CAP_SETUID | UINT64_C(1)<<CAP_SYS_PTRACE | UINT64_C(1)<<CAP_SYS_ADMIN | UINT64_C(1)<<CAP_AUDIT_CONTROL | UINT64_C(1)<<CAP_MAC_OVERRIDE | UINT64_C(1)<<CAP_SYSLOG, &t3) == 0); - assert_se(streq(t3, "cap_chown cap_dac_override cap_dac_read_search cap_fowner cap_setgid cap_setuid cap_sys_ptrace cap_sys_admin cap_audit_control cap_mac_override cap_syslog")); +static void test_capability_set(void) { + uint64_t c; + + assert_se(capability_set_from_string(NULL, &c) == 0); + assert_se(c == 0); + + assert_se(capability_set_from_string("", &c) == 0); + assert_se(c == 0); + + assert_se(capability_set_from_string("0", &c) == 0); + assert_se(c == UINT64_C(1)); + + assert_se(capability_set_from_string("1", &c) == 0); + assert_se(c == UINT64_C(1) << 1); + + assert_se(capability_set_from_string("0 1 2 3", &c) == 0); + assert_se(c == (UINT64_C(1) << 4) - 1); + + test_capability_set_one(0, ""); + test_capability_set_one( + UINT64_C(1) << CAP_DAC_OVERRIDE, + "cap_dac_override"); + test_capability_set_one( + UINT64_C(1) << CAP_DAC_OVERRIDE | + UINT64_C(1) << capability_list_length(), + "cap_dac_override"); + test_capability_set_one( + UINT64_C(1) << capability_list_length(), ""); + test_capability_set_one( + UINT64_C(1) << CAP_CHOWN | + UINT64_C(1) << CAP_DAC_OVERRIDE | + UINT64_C(1) << CAP_DAC_READ_SEARCH | + UINT64_C(1) << CAP_FOWNER | + UINT64_C(1) << CAP_SETGID | + UINT64_C(1) << CAP_SETUID | + UINT64_C(1) << CAP_SYS_PTRACE | + UINT64_C(1) << CAP_SYS_ADMIN | + UINT64_C(1) << CAP_AUDIT_CONTROL | + UINT64_C(1) << CAP_MAC_OVERRIDE | + UINT64_C(1) << CAP_SYSLOG | + UINT64_C(1) << (capability_list_length() + 1), + "cap_chown cap_dac_override cap_dac_read_search cap_fowner " + "cap_setgid cap_setuid cap_sys_ptrace cap_sys_admin " + "cap_audit_control cap_mac_override cap_syslog"); } int main(int argc, char *argv[]) { test_cap_list(); - test_last_cap_file(); - test_last_cap_probe(); - test_capability_set_to_string_alloc(); + test_capability_set(); return 0; } |