Skip to content

Commit 4c1a95f

Browse files
committed
test: move tests about cap_list_cap() from test-cap-list to test-capability
1 parent db4bd5b commit 4c1a95f

File tree

2 files changed

+39
-36
lines changed

2 files changed

+39
-36
lines changed

src/test/test-cap-list.c

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include "alloc-util.h"
2424
#include "cap-list.h"
2525
#include "capability-util.h"
26-
#include "fileio.h"
2726
#include "parse-util.h"
2827
#include "string-util.h"
2928
#include "util.h"
@@ -71,39 +70,6 @@ static void test_cap_list(void) {
7170
}
7271
}
7372

74-
/* verify cap_last_cap() against /proc/sys/kernel/cap_last_cap */
75-
static void test_last_cap_file(void) {
76-
_cleanup_free_ char *content = NULL;
77-
unsigned long val = 0;
78-
int r;
79-
80-
r = read_one_line_file("/proc/sys/kernel/cap_last_cap", &content);
81-
assert_se(r >= 0);
82-
83-
r = safe_atolu(content, &val);
84-
assert_se(r >= 0);
85-
assert_se(val != 0);
86-
assert_se(val == cap_last_cap());
87-
}
88-
89-
/* verify cap_last_cap() against syscall probing */
90-
static void test_last_cap_probe(void) {
91-
unsigned long p = (unsigned long)CAP_LAST_CAP;
92-
93-
if (prctl(PR_CAPBSET_READ, p) < 0) {
94-
for (p--; p > 0; p --)
95-
if (prctl(PR_CAPBSET_READ, p) >= 0)
96-
break;
97-
} else {
98-
for (;; p++)
99-
if (prctl(PR_CAPBSET_READ, p+1) < 0)
100-
break;
101-
}
102-
103-
assert_se(p != 0);
104-
assert_se(p == cap_last_cap());
105-
}
106-
10773
static void test_capability_set_to_string_alloc(void) {
10874
_cleanup_free_ char *t1 = NULL, *t2 = NULL, *t3 = NULL;
10975

@@ -119,8 +85,6 @@ static void test_capability_set_to_string_alloc(void) {
11985

12086
int main(int argc, char *argv[]) {
12187
test_cap_list();
122-
test_last_cap_file();
123-
test_last_cap_probe();
12488
test_capability_set_to_string_alloc();
12589

12690
return 0;

src/test/test-capability.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@
2626
#include <sys/wait.h>
2727
#include <unistd.h>
2828

29+
#include "alloc-util.h"
2930
#include "capability-util.h"
3031
#include "fd-util.h"
32+
#include "fileio.h"
3133
#include "macro.h"
34+
#include "parse-util.h"
3235
#include "util.h"
3336

3437
static uid_t test_uid = -1;
@@ -37,6 +40,39 @@ static gid_t test_gid = -1;
3740
/* We keep CAP_DAC_OVERRIDE to avoid errors with gcov when doing test coverage */
3841
static uint64_t test_flags = 1ULL << CAP_DAC_OVERRIDE;
3942

43+
/* verify cap_last_cap() against /proc/sys/kernel/cap_last_cap */
44+
static void test_last_cap_file(void) {
45+
_cleanup_free_ char *content = NULL;
46+
unsigned long val = 0;
47+
int r;
48+
49+
r = read_one_line_file("/proc/sys/kernel/cap_last_cap", &content);
50+
assert_se(r >= 0);
51+
52+
r = safe_atolu(content, &val);
53+
assert_se(r >= 0);
54+
assert_se(val != 0);
55+
assert_se(val == cap_last_cap());
56+
}
57+
58+
/* verify cap_last_cap() against syscall probing */
59+
static void test_last_cap_probe(void) {
60+
unsigned long p = (unsigned long)CAP_LAST_CAP;
61+
62+
if (prctl(PR_CAPBSET_READ, p) < 0) {
63+
for (p--; p > 0; p --)
64+
if (prctl(PR_CAPBSET_READ, p) >= 0)
65+
break;
66+
} else {
67+
for (;; p++)
68+
if (prctl(PR_CAPBSET_READ, p+1) < 0)
69+
break;
70+
}
71+
72+
assert_se(p != 0);
73+
assert_se(p == cap_last_cap());
74+
}
75+
4076
static void fork_test(void (*test_func)(void)) {
4177
pid_t pid = 0;
4278

@@ -203,6 +239,9 @@ int main(int argc, char *argv[]) {
203239
int r;
204240
bool run_ambient;
205241

242+
test_last_cap_file();
243+
test_last_cap_probe();
244+
206245
log_parse_environment();
207246
log_open();
208247

0 commit comments

Comments
 (0)