]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-cap-list.c
util-lib: split string parsing related calls from util.[ch] into parse-util.[ch]
[thirdparty/systemd.git] / src / test / test-cap-list.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2014 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <sys/prctl.h>
23
24 #include "cap-list.h"
25 #include "capability.h"
26 #include "fileio.h"
27 #include "parse-util.h"
28 #include "util.h"
29
30 /* verify the capability parser */
31 static void test_cap_list(void) {
32 int i;
33
34 assert_se(!capability_to_name(-1));
35 assert_se(!capability_to_name(capability_list_length()));
36
37 for (i = 0; i < capability_list_length(); i++) {
38 const char *n;
39
40 assert_se(n = capability_to_name(i));
41 assert_se(capability_from_name(n) == i);
42 printf("%s = %i\n", n, i);
43 }
44
45 assert_se(capability_from_name("asdfbsd") == -EINVAL);
46 assert_se(capability_from_name("CAP_AUDIT_READ") == CAP_AUDIT_READ);
47 assert_se(capability_from_name("cap_audit_read") == CAP_AUDIT_READ);
48 assert_se(capability_from_name("cAp_aUdIt_rEAd") == CAP_AUDIT_READ);
49 assert_se(capability_from_name("0") == 0);
50 assert_se(capability_from_name("15") == 15);
51 assert_se(capability_from_name("-1") == -EINVAL);
52
53 for (i = 0; i < capability_list_length(); i++) {
54 _cleanup_cap_free_charp_ char *a = NULL;
55 const char *b;
56 unsigned u;
57
58 assert_se(a = cap_to_name(i));
59
60 /* quit the loop as soon as libcap starts returning
61 * numeric ids, formatted as strings */
62 if (safe_atou(a, &u) >= 0)
63 break;
64
65 assert_se(b = capability_to_name(i));
66
67 printf("%s vs. %s\n", a, b);
68
69 assert_se(strcasecmp(a, b) == 0);
70 }
71 }
72
73 /* verify cap_last_cap() against /proc/sys/kernel/cap_last_cap */
74 static void test_last_cap_file(void) {
75 _cleanup_free_ char *content = NULL;
76 unsigned long val = 0;
77 int r;
78
79 r = read_one_line_file("/proc/sys/kernel/cap_last_cap", &content);
80 assert_se(r >= 0);
81
82 r = safe_atolu(content, &val);
83 assert_se(r >= 0);
84 assert_se(val != 0);
85 assert_se(val == cap_last_cap());
86 }
87
88 /* verify cap_last_cap() against syscall probing */
89 static void test_last_cap_probe(void) {
90 unsigned long p = (unsigned long)CAP_LAST_CAP;
91
92 if (prctl(PR_CAPBSET_READ, p) < 0) {
93 for (p--; p > 0; p --)
94 if (prctl(PR_CAPBSET_READ, p) >= 0)
95 break;
96 } else {
97 for (;; p++)
98 if (prctl(PR_CAPBSET_READ, p+1) < 0)
99 break;
100 }
101
102 assert_se(p != 0);
103 assert_se(p == cap_last_cap());
104 }
105
106 int main(int argc, char *argv[]) {
107 test_cap_list();
108 test_last_cap_file();
109 test_last_cap_probe();
110
111 return 0;
112 }