1 #include "libcap.h"
2
3 static cap_value_t top;
4
cf(cap_value_t x)5 static int cf(cap_value_t x) {
6 return top - x - 1;
7 }
8
test_cap_bits(void)9 static int test_cap_bits(void) {
10 static cap_value_t vs[] = {
11 5, 6, 11, 12, 15, 16, 17, 38, 41, 63, 64, __CAP_MAXBITS+3, 0, -1
12 };
13 int failed = 0;
14 cap_value_t i;
15 for (i = 0; vs[i] >= 0; i++) {
16 cap_value_t ans;
17
18 top = i;
19 _binary_search(ans, cf, 0, __CAP_MAXBITS, 0);
20 if (ans != top) {
21 if (top > __CAP_MAXBITS && ans == __CAP_MAXBITS) {
22 } else {
23 printf("test_cap_bits miscompared [%d] top=%d - got=%d\n",
24 i, top, ans);
25 failed = -1;
26 }
27 }
28 }
29 return failed;
30 }
31
test_cap_flags(void)32 static int test_cap_flags(void) {
33 cap_t c, d;
34 cap_flag_t f = CAP_INHERITABLE, t;
35 cap_value_t v;
36
37 c = cap_init();
38 if (c == NULL) {
39 printf("test_flags failed to allocate a set\n");
40 return -1;
41 }
42
43 for (v = 0; v < __CAP_MAXBITS; v += 3) {
44 if (cap_set_flag(c, CAP_INHERITABLE, 1, &v, CAP_SET)) {
45 printf("unable to set inheritable bit %d\n", v);
46 return -1;
47 }
48 }
49
50 d = cap_dup(c);
51 for (t = CAP_EFFECTIVE; t <= CAP_INHERITABLE; t++) {
52 if (cap_fill(c, t, f)) {
53 printf("cap_fill failed %d -> %d\n", f, t);
54 return -1;
55 }
56 if (cap_clear_flag(c, f)) {
57 printf("cap_fill unable to clear flag %d\n", f);
58 return -1;
59 }
60 f = t;
61 }
62 if (cap_compare(c, d)) {
63 printf("permuted cap_fill()ing failed to perform net no-op\n");
64 return -1;
65 }
66 cap_free(d);
67 cap_free(c);
68 return 0;
69 }
70
main(int argc,char ** argv)71 int main(int argc, char **argv) {
72 int result = 0;
73
74 result = test_cap_bits() | result;
75 result = test_cap_flags() | result;
76
77 if (result) {
78 printf("cap_test FAILED\n");
79 exit(1);
80 }
81 printf("cap_test PASS\n");
82 exit(0);
83 }
84