chiark / gitweb /
fsckd: the error code is actually returned in 'fd'
[elogind.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 "util.h"
23 #include "fileio.h"
24 #include "cap-list.h"
25 #include "capability.h"
26 #include <sys/prctl.h>
27
28 /* verify the capability parser */
29 static void test_cap_list(void) {
30         int i;
31
32         assert_se(!capability_to_name(-1));
33         assert_se(!capability_to_name(capability_list_length()));
34
35         for (i = 0; i < capability_list_length(); i++) {
36                 const char *n;
37
38                 assert_se(n = capability_to_name(i));
39                 assert_se(capability_from_name(n) == i);
40                 printf("%s = %i\n", n, i);
41         }
42
43         assert_se(capability_from_name("asdfbsd") == -EINVAL);
44         assert_se(capability_from_name("CAP_AUDIT_READ") == CAP_AUDIT_READ);
45         assert_se(capability_from_name("cap_audit_read") == CAP_AUDIT_READ);
46         assert_se(capability_from_name("cAp_aUdIt_rEAd") == CAP_AUDIT_READ);
47         assert_se(capability_from_name("0") == 0);
48         assert_se(capability_from_name("15") == 15);
49         assert_se(capability_from_name("-1") == -EINVAL);
50
51         for (i = 0; i < capability_list_length(); i++) {
52                 _cleanup_cap_free_charp_ char *a = NULL;
53                 const char *b;
54                 unsigned u;
55
56                 assert_se(a = cap_to_name(i));
57
58                 /* quit the loop as soon as libcap starts returning
59                  * numeric ids, formatted as strings */
60                 if (safe_atou(a, &u) >= 0)
61                         break;
62
63                 assert_se(b = capability_to_name(i));
64
65                 printf("%s vs. %s\n", a, b);
66
67                 assert_se(strcasecmp(a, b) == 0);
68         }
69 }
70
71 /* verify cap_last_cap() against /proc/sys/kernel/cap_last_cap */
72 static void test_last_cap_file(void) {
73         _cleanup_free_ char *content = NULL;
74         unsigned long val = 0;
75         int r;
76
77         r = read_one_line_file("/proc/sys/kernel/cap_last_cap", &content);
78         assert_se(r >= 0);
79
80         r = safe_atolu(content, &val);
81         assert_se(r >= 0);
82         assert_se(val != 0);
83         assert_se(val == cap_last_cap());
84 }
85
86 /* verify cap_last_cap() against syscall probing */
87 static void test_last_cap_probe(void) {
88         unsigned long p = (unsigned long)CAP_LAST_CAP;
89
90         if (prctl(PR_CAPBSET_READ, p) < 0) {
91                 for (p--; p > 0; p --)
92                         if (prctl(PR_CAPBSET_READ, p) >= 0)
93                                 break;
94         } else {
95                 for (;; p++)
96                         if (prctl(PR_CAPBSET_READ, p+1) < 0)
97                                 break;
98         }
99
100         assert_se(p != 0);
101         assert_se(p == cap_last_cap());
102 }
103
104 int main(int argc, char *argv[]) {
105         test_cap_list();
106         test_last_cap_file();
107         test_last_cap_probe();
108
109         return 0;
110 }