1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-26 03:22:00 +03:00

test-cap-list: add more tests about capability_set_{from,to}_string()

This commit is contained in:
Yu Watanabe 2017-11-28 22:17:00 +09:00
parent 4c1a95fd84
commit 23cc81e7c2

View File

@ -70,22 +70,72 @@ static void test_cap_list(void) {
} }
} }
static void test_capability_set_to_string_alloc(void) { static void test_capability_set_one(uint64_t c, const char *t) {
_cleanup_free_ char *t1 = NULL, *t2 = NULL, *t3 = NULL; _cleanup_free_ char *t1 = NULL;
uint64_t c1, c_masked = c & ((UINT64_C(1) << capability_list_length()) - 1);
assert_se(capability_set_to_string_alloc(0u, &t1) == 0); assert_se(capability_set_to_string_alloc(c, &t1) == 0);
assert_se(streq(t1, "")); assert_se(streq(t1, t));
assert_se(capability_set_to_string_alloc(1u<<CAP_DAC_OVERRIDE, &t2) == 0); assert_se(capability_set_from_string(t1, &c1) == 0);
assert_se(streq(t2, "cap_dac_override")); assert_se(c1 == c_masked);
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); free(t1);
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")); 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(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[]) { int main(int argc, char *argv[]) {
test_cap_list(); test_cap_list();
test_capability_set_to_string_alloc(); test_capability_set();
return 0; return 0;
} }