From d40c54fe56db52857890ebbf9d8c9ffe82b8b66c Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 28 Nov 2017 18:11:58 +0900 Subject: [PATCH 1/8] alloc-util: coding style fix --- src/basic/alloc-util.c | 2 +- src/basic/alloc-util.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/basic/alloc-util.c b/src/basic/alloc-util.c index 2584cb07fc..cdde4f2859 100644 --- a/src/basic/alloc-util.c +++ b/src/basic/alloc-util.c @@ -38,7 +38,7 @@ void* memdup(const void *p, size_t l) { return ret; } -void* memdup_suffix0(const void*p, size_t l) { +void* memdup_suffix0(const void *p, size_t l) { void *ret; assert(l == 0 || p); diff --git a/src/basic/alloc-util.h b/src/basic/alloc-util.h index 38d1546b55..02dee37d36 100644 --- a/src/basic/alloc-util.h +++ b/src/basic/alloc-util.h @@ -55,7 +55,7 @@ static inline void *mfree(void *memory) { }) void* memdup(const void *p, size_t l) _alloc_(2); -void* memdup_suffix0(const void*p, size_t l) _alloc_(2); +void* memdup_suffix0(const void *p, size_t l) _alloc_(2); static inline void freep(void *p) { free(*(void**) p); From a4e6e06bc795cfd8b96ac8da4ea3b97f9a3ea4a0 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 28 Nov 2017 18:13:05 +0900 Subject: [PATCH 2/8] tests: add tests for empty string --- src/test/test-af-list.c | 1 + src/test/test-arphrd-list.c | 1 + 2 files changed, 2 insertions(+) diff --git a/src/test/test-af-list.c b/src/test/test-af-list.c index 6934549287..2d42dc2c54 100644 --- a/src/test/test-af-list.c +++ b/src/test/test-af-list.c @@ -46,6 +46,7 @@ int main(int argc, const char *argv[]) { assert_se(af_to_name(af_max()) == NULL); assert_se(af_to_name(-1) == NULL); assert_se(af_from_name("huddlduddl") == AF_UNSPEC); + assert_se(af_from_name("") == AF_UNSPEC); return 0; } diff --git a/src/test/test-arphrd-list.c b/src/test/test-arphrd-list.c index 2949a8aae0..e503582820 100644 --- a/src/test/test-arphrd-list.c +++ b/src/test/test-arphrd-list.c @@ -46,6 +46,7 @@ int main(int argc, const char *argv[]) { assert_se(arphrd_to_name(arphrd_max()) == NULL); assert_se(arphrd_to_name(0) == NULL); assert_se(arphrd_from_name("huddlduddl") == 0); + assert_se(arphrd_from_name("") == 0); return 0; } From f30788ee2343bba41686626249d0f049ac0edb12 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 28 Nov 2017 20:33:58 +0900 Subject: [PATCH 3/8] test-alloc-util: add tests for memdup_suffix0() and greedy_realloc() --- src/test/test-alloc-util.c | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/test/test-alloc-util.c b/src/test/test-alloc-util.c index 7e779ac561..ed598e106c 100644 --- a/src/test/test-alloc-util.c +++ b/src/test/test-alloc-util.c @@ -18,6 +18,8 @@ along with systemd; If not, see . ***/ +#include + #include "alloc-util.h" #include "macro.h" #include "util.h" @@ -35,22 +37,42 @@ static void test_alloca(void) { assert_se(!memcmp(t, zero, 997)); } -static void test_memdup_multiply(void) { +static void test_memdup_multiply_and_greedy_realloc(void) { int org[] = {1, 2, 3}; - int *dup; - - dup = (int*)memdup_multiply(org, sizeof(int), 3); + _cleanup_free_ int *dup; + int *p; + size_t i, allocated = 3; + dup = (int*) memdup_suffix0_multiply(org, sizeof(int), 3); assert_se(dup); assert_se(dup[0] == 1); assert_se(dup[1] == 2); assert_se(dup[2] == 3); + assert_se(*(uint8_t*) (dup + 3) == (uint8_t) 0); free(dup); + + dup = (int*) memdup_multiply(org, sizeof(int), 3); + assert_se(dup); + assert_se(dup[0] == 1); + assert_se(dup[1] == 2); + assert_se(dup[2] == 3); + + p = dup; + assert_se(greedy_realloc0((void**) &dup, &allocated, 2, sizeof(int)) == p); + + p = (int *) greedy_realloc0((void**) &dup, &allocated, 10, sizeof(int)); + assert_se(p == dup); + assert_se(allocated >= 10); + assert_se(p[0] == 1); + assert_se(p[1] == 2); + assert_se(p[2] == 3); + for (i = 3; i < allocated; i++) + assert_se(p[i] == 0); } int main(int argc, char *argv[]) { test_alloca(); - test_memdup_multiply(); + test_memdup_multiply_and_greedy_realloc(); return 0; } From ade0eb21909d019112e552cce31beb9966381b48 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 28 Nov 2017 18:14:55 +0900 Subject: [PATCH 4/8] test-architecture: add more tests --- src/test/test-architecture.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/test/test-architecture.c b/src/test/test-architecture.c index e81799539d..586d54b140 100644 --- a/src/test/test-architecture.c +++ b/src/test/test-architecture.c @@ -25,6 +25,14 @@ int main(int argc, char *argv[]) { int a, v; + const char *p; + + assert_se(architecture_from_string("") < 0); + assert_se(architecture_from_string(NULL) < 0); + assert_se(architecture_from_string("hoge") < 0); + assert_se(architecture_to_string(-1) == NULL); + assert_se(architecture_from_string(architecture_to_string(0)) == 0); + assert_se(architecture_from_string(architecture_to_string(1)) == 1); v = detect_virtualization(); if (IN_SET(v, -EPERM, -EACCES)) @@ -40,12 +48,18 @@ int main(int argc, char *argv[]) { a = uname_architecture(); assert_se(a >= 0); - log_info("uname architecture=%s", architecture_to_string(a)); + p = architecture_to_string(a); + assert_se(p); + log_info("uname architecture=%s", p); + assert_se(architecture_from_string(p) == a); a = native_architecture(); assert_se(a >= 0); - log_info("native architecture=%s", architecture_to_string(a)); + p = architecture_to_string(a); + assert_se(p); + log_info("native architecture=%s", p); + assert_se(architecture_from_string(p) == a); log_info("primary library architecture=" LIB_ARCH_TUPLE); From 6b86b9e65d4adbbdc04f573ab08334bf99d27583 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 28 Nov 2017 18:19:04 +0900 Subject: [PATCH 5/8] test-bitmap: add tests for bitmap_copy() --- src/test/test-bitmap.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/test/test-bitmap.c b/src/test/test-bitmap.c index 6bc88a4566..fa38056126 100644 --- a/src/test/test-bitmap.c +++ b/src/test/test-bitmap.c @@ -97,8 +97,19 @@ int main(int argc, const char *argv[]) { assert_se(i == (unsigned) -1); + b2 = bitmap_copy(b); + assert_se(b2); + assert_se(bitmap_equal(b, b2) == true); + assert_se(bitmap_equal(b, b) == true); + assert_se(bitmap_equal(b, NULL) == false); + assert_se(bitmap_equal(NULL, b) == false); + assert_se(bitmap_equal(NULL, NULL) == true); + bitmap_clear(b); assert_se(bitmap_isclear(b) == true); + assert_se(bitmap_equal(b, b2) == false); + bitmap_free(b2); + b2 = NULL; assert_se(bitmap_set(b, (unsigned) -1) == -ERANGE); From db4bd5bd625320071fa27e1accba53ab0d0ff948 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 28 Nov 2017 22:06:34 +0900 Subject: [PATCH 6/8] cap-list: check range of numeric value --- src/basic/cap-list.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/basic/cap-list.c b/src/basic/cap-list.c index 52b2ce800f..c4557666ef 100644 --- a/src/basic/cap-list.c +++ b/src/basic/cap-list.c @@ -54,8 +54,12 @@ int capability_from_name(const char *name) { /* Try to parse numeric capability */ r = safe_atoi(name, &i); - if (r >= 0 && i >= 0) - return i; + if (r >= 0) { + if (i >= 0 && i < (int) ELEMENTSOF(capability_names)) + return i; + else + return -EINVAL; + } /* Try to parse string capability */ sc = lookup_capability(name, strlen(name)); From 4c1a95fd84206ee96c8a0d855780d2d99ff020bc Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 28 Nov 2017 22:14:54 +0900 Subject: [PATCH 7/8] test: move tests about cap_list_cap() from test-cap-list to test-capability --- src/test/test-cap-list.c | 36 ----------------------------------- src/test/test-capability.c | 39 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 36 deletions(-) diff --git a/src/test/test-cap-list.c b/src/test/test-cap-list.c index 9c82e618f5..f6978cc588 100644 --- a/src/test/test-cap-list.c +++ b/src/test/test-cap-list.c @@ -23,7 +23,6 @@ #include "alloc-util.h" #include "cap-list.h" #include "capability-util.h" -#include "fileio.h" #include "parse-util.h" #include "string-util.h" #include "util.h" @@ -71,39 +70,6 @@ static void test_cap_list(void) { } } -/* verify cap_last_cap() against /proc/sys/kernel/cap_last_cap */ -static void test_last_cap_file(void) { - _cleanup_free_ char *content = NULL; - unsigned long val = 0; - int r; - - r = read_one_line_file("/proc/sys/kernel/cap_last_cap", &content); - assert_se(r >= 0); - - r = safe_atolu(content, &val); - assert_se(r >= 0); - assert_se(val != 0); - assert_se(val == cap_last_cap()); -} - -/* verify cap_last_cap() against syscall probing */ -static void test_last_cap_probe(void) { - unsigned long p = (unsigned long)CAP_LAST_CAP; - - if (prctl(PR_CAPBSET_READ, p) < 0) { - for (p--; p > 0; p --) - if (prctl(PR_CAPBSET_READ, p) >= 0) - break; - } else { - for (;; p++) - if (prctl(PR_CAPBSET_READ, p+1) < 0) - break; - } - - assert_se(p != 0); - assert_se(p == cap_last_cap()); -} - static void test_capability_set_to_string_alloc(void) { _cleanup_free_ char *t1 = NULL, *t2 = NULL, *t3 = NULL; @@ -119,8 +85,6 @@ static void test_capability_set_to_string_alloc(void) { int main(int argc, char *argv[]) { test_cap_list(); - test_last_cap_file(); - test_last_cap_probe(); test_capability_set_to_string_alloc(); return 0; diff --git a/src/test/test-capability.c b/src/test/test-capability.c index 367614708f..6c41e1ca15 100644 --- a/src/test/test-capability.c +++ b/src/test/test-capability.c @@ -26,9 +26,12 @@ #include #include +#include "alloc-util.h" #include "capability-util.h" #include "fd-util.h" +#include "fileio.h" #include "macro.h" +#include "parse-util.h" #include "util.h" static uid_t test_uid = -1; @@ -37,6 +40,39 @@ static gid_t test_gid = -1; /* We keep CAP_DAC_OVERRIDE to avoid errors with gcov when doing test coverage */ static uint64_t test_flags = 1ULL << CAP_DAC_OVERRIDE; +/* verify cap_last_cap() against /proc/sys/kernel/cap_last_cap */ +static void test_last_cap_file(void) { + _cleanup_free_ char *content = NULL; + unsigned long val = 0; + int r; + + r = read_one_line_file("/proc/sys/kernel/cap_last_cap", &content); + assert_se(r >= 0); + + r = safe_atolu(content, &val); + assert_se(r >= 0); + assert_se(val != 0); + assert_se(val == cap_last_cap()); +} + +/* verify cap_last_cap() against syscall probing */ +static void test_last_cap_probe(void) { + unsigned long p = (unsigned long)CAP_LAST_CAP; + + if (prctl(PR_CAPBSET_READ, p) < 0) { + for (p--; p > 0; p --) + if (prctl(PR_CAPBSET_READ, p) >= 0) + break; + } else { + for (;; p++) + if (prctl(PR_CAPBSET_READ, p+1) < 0) + break; + } + + assert_se(p != 0); + assert_se(p == cap_last_cap()); +} + static void fork_test(void (*test_func)(void)) { pid_t pid = 0; @@ -203,6 +239,9 @@ int main(int argc, char *argv[]) { int r; bool run_ambient; + test_last_cap_file(); + test_last_cap_probe(); + log_parse_environment(); log_open(); From 23cc81e7c221c6e78e54f8008cf089bbfbe15055 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 28 Nov 2017 22:17:00 +0900 Subject: [PATCH 8/8] test-cap-list: add more tests about capability_set_{from,to}_string() --- src/test/test-cap-list.c | 68 ++++++++++++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 9 deletions(-) diff --git a/src/test/test-cap-list.c b/src/test/test-cap-list.c index f6978cc588..935567cc23 100644 --- a/src/test/test-cap-list.c +++ b/src/test/test-cap-list.c @@ -70,22 +70,72 @@ static void test_cap_list(void) { } } -static void test_capability_set_to_string_alloc(void) { - _cleanup_free_ char *t1 = NULL, *t2 = NULL, *t3 = NULL; +static void test_capability_set_one(uint64_t c, const char *t) { + _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(streq(t1, "")); + assert_se(capability_set_to_string_alloc(c, &t1) == 0); + assert_se(streq(t1, t)); - assert_se(capability_set_to_string_alloc(1u<