mirror of
https://github.com/systemd/systemd.git
synced 2024-12-26 03:22:00 +03:00
Merge pull request #7490 from yuwata/test-basic
add more tests for basic functions
This commit is contained in:
commit
5c4912ea61
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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));
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -18,6 +18,8 @@
|
||||
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
||||
***/
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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,57 +70,72 @@ 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;
|
||||
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);
|
||||
|
||||
r = read_one_line_file("/proc/sys/kernel/cap_last_cap", &content);
|
||||
assert_se(r >= 0);
|
||||
assert_se(capability_set_to_string_alloc(c, &t1) == 0);
|
||||
assert_se(streq(t1, t));
|
||||
|
||||
r = safe_atolu(content, &val);
|
||||
assert_se(r >= 0);
|
||||
assert_se(val != 0);
|
||||
assert_se(val == cap_last_cap());
|
||||
assert_se(capability_set_from_string(t1, &c1) == 0);
|
||||
assert_se(c1 == c_masked);
|
||||
|
||||
free(t1);
|
||||
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);
|
||||
}
|
||||
|
||||
/* verify cap_last_cap() against syscall probing */
|
||||
static void test_last_cap_probe(void) {
|
||||
unsigned long p = (unsigned long)CAP_LAST_CAP;
|
||||
static void test_capability_set(void) {
|
||||
uint64_t c;
|
||||
|
||||
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(capability_set_from_string(NULL, &c) == 0);
|
||||
assert_se(c == 0);
|
||||
|
||||
assert_se(p != 0);
|
||||
assert_se(p == cap_last_cap());
|
||||
}
|
||||
assert_se(capability_set_from_string("", &c) == 0);
|
||||
assert_se(c == 0);
|
||||
|
||||
static void test_capability_set_to_string_alloc(void) {
|
||||
_cleanup_free_ char *t1 = NULL, *t2 = NULL, *t3 = NULL;
|
||||
assert_se(capability_set_from_string("0", &c) == 0);
|
||||
assert_se(c == UINT64_C(1));
|
||||
|
||||
assert_se(capability_set_to_string_alloc(0u, &t1) == 0);
|
||||
assert_se(streq(t1, ""));
|
||||
assert_se(capability_set_from_string("1", &c) == 0);
|
||||
assert_se(c == UINT64_C(1) << 1);
|
||||
|
||||
assert_se(capability_set_to_string_alloc(1u<<CAP_DAC_OVERRIDE, &t2) == 0);
|
||||
assert_se(streq(t2, "cap_dac_override"));
|
||||
assert_se(capability_set_from_string("0 1 2 3", &c) == 0);
|
||||
assert_se(c == (UINT64_C(1) << 4) - 1);
|
||||
|
||||
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);
|
||||
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"));
|
||||
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[]) {
|
||||
test_cap_list();
|
||||
test_last_cap_file();
|
||||
test_last_cap_probe();
|
||||
test_capability_set_to_string_alloc();
|
||||
test_capability_set();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -26,9 +26,12 @@
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#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();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user