From a40b728e1172cc07a09e12dd56089ab37c8c5924 Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Tue, 23 Nov 2021 13:40:27 +0100 Subject: [PATCH 1/4] test: Slightly rework DEFINE_TEST_MAIN macros - A lot of tests want a different log level - Provides saved_argc/saved_argv to tests - Separate intro/outro is more flexible --- src/shared/tests.h | 21 ++++++++++----------- src/test/test-macro.c | 2 +- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/shared/tests.h b/src/shared/tests.h index f333ebd842..872b9b2d6c 100644 --- a/src/shared/tests.h +++ b/src/shared/tests.h @@ -6,6 +6,7 @@ #include "sd-daemon.h" #include "macro.h" +#include "util.h" static inline bool manager_errno_skip_test(int r) { return IN_SET(abs(r), @@ -77,16 +78,14 @@ static inline void run_test_table(void) { } } -#define DEFINE_TEST_MAIN \ - int main(int argc, char *argv[]) { \ - test_setup_logging(LOG_INFO); \ - run_test_table(); \ - return EXIT_SUCCESS; \ +#define DEFINE_CUSTOM_TEST_MAIN(log_level, intro, outro) \ + int main(int argc, char *argv[]) { \ + test_setup_logging(log_level); \ + save_argc_argv(argc, argv); \ + intro; \ + run_test_table(); \ + outro; \ + return EXIT_SUCCESS; \ } -#define DEFINE_CUSTOM_TEST_MAIN(impl) \ - int main(int argc, char *argv[]) { \ - test_setup_logging(LOG_INFO); \ - run_test_table(); \ - return impl(); \ - } +#define DEFINE_TEST_MAIN(log_level) DEFINE_CUSTOM_TEST_MAIN(log_level, , ) diff --git a/src/test/test-macro.c b/src/test/test-macro.c index 09cb4bf7bc..d33d22c18c 100644 --- a/src/test/test-macro.c +++ b/src/test/test-macro.c @@ -387,4 +387,4 @@ TEST(flags) { assert_se(f == F2); } -DEFINE_TEST_MAIN; +DEFINE_TEST_MAIN(LOG_INFO); From 4d5ad9d951299c905453ac8e3769dc82b0a09fb4 Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Wed, 24 Nov 2021 18:45:16 +0100 Subject: [PATCH 2/4] test-time-util: Properly restore TZ variable The test fails to call tzset() after unsetting TZ variable, which could break other tests. Also, let's be nicer and actually restore the TZ to its original value. --- src/test/test-time-util.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/test/test-time-util.c b/src/test/test-time-util.c index 53f8114501..0992a31659 100644 --- a/src/test/test-time-util.c +++ b/src/test/test-time-util.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include "env-util.h" #include "random-util.h" #include "serialize.h" #include "string-util.h" @@ -570,6 +571,8 @@ static void test_usec_shift_clock(void) { static void test_in_utc_timezone(void) { log_info("/* %s */", __func__); + const char *tz = getenv("TZ"); + assert_se(setenv("TZ", ":UTC", 1) >= 0); assert_se(in_utc_timezone()); assert_se(streq(tzname[0], "UTC")); @@ -582,7 +585,8 @@ static void test_in_utc_timezone(void) { assert_se(streq(tzname[0], "CET")); assert_se(streq(tzname[1], "CEST")); - assert_se(unsetenv("TZ") == 0); + assert_se(set_unset_env("TZ", tz, true) == 0); + tzset(); } static void test_map_clock_usec(void) { From 4f7452a8eb7a946efea927fae017d4d661097833 Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Wed, 24 Nov 2021 12:00:02 +0100 Subject: [PATCH 3/4] test: Use TEST macro This converts to TEST macro where it is trivial. Some additional notable changes: - simplify HAVE_LIBIDN #ifdef in test-dns-domain.c - use saved_argc/saved_argv in test-copy.c, test-path-util.c, test-tmpfiles.c and test-unit-file.c --- src/test/meson.build | 3 +- src/test/test-af-list.c | 12 +- src/test/test-alloc-util.c | 28 +-- src/test/test-arphrd-util.c | 8 +- src/test/test-ask-password-api.c | 9 +- src/test/test-blockdev-util.c | 8 +- src/test/test-bus-util.c | 12 +- src/test/test-calendarspec.c | 22 ++- src/test/test-cap-list.c | 19 +- src/test/test-cgroup-cpu.c | 10 +- src/test/test-cgroup.c | 16 +- src/test/test-clock.c | 12 +- src/test/test-condition.c | 63 ++---- src/test/test-conf-files.c | 25 +-- src/test/test-conf-parser.c | 55 ++---- src/test/test-copy.c | 48 ++--- src/test/test-coredump-util.c | 17 +- src/test/test-cpu-set-util.c | 26 +-- src/test/test-data-fd-util.c | 13 +- src/test/test-device-nodes.c | 9 +- src/test/test-dns-domain.c | 140 +++---------- src/test/test-ellipsize.c | 12 +- src/test/test-emergency-action.c | 12 +- src/test/test-env-file.c | 26 +-- src/test/test-env-util.c | 103 +++------- src/test/test-escape.c | 46 ++--- src/test/test-ether-addr-util.c | 21 +- src/test/test-exec-util.c | 40 ++-- src/test/test-exit-status.c | 22 +-- src/test/test-extract-word.c | 24 +-- src/test/test-fd-util.c | 43 ++-- src/test/test-fdset.c | 33 ++-- src/test/test-fileio.c | 132 ++++--------- src/test/test-format-util.c | 9 +- src/test/test-fs-util.c | 84 ++------ src/test/test-fstab-util.c | 18 +- src/test/test-gcrypt-util.c | 9 +- src/test/test-glob-util.c | 26 +-- src/test/test-gpt.c | 13 +- src/test/test-hash-funcs.c | 10 +- src/test/test-hexdecoct.c | 57 ++---- src/test/test-hmac.c | 12 +- src/test/test-hostname-setup.c | 13 +- src/test/test-hostname-util.c | 27 +-- src/test/test-id128.c | 20 +- src/test/test-import-util.c | 14 +- src/test/test-in-addr-prefix-util.c | 8 +- src/test/test-in-addr-util.c | 52 +---- src/test/test-install-file.c | 12 +- src/test/test-io-util.c | 9 +- src/test/test-ip-protocol-list.c | 31 +-- src/test/test-journal-importer.c | 13 +- src/test/test-json.c | 133 ++++++------- src/test/test-libmount.c | 9 +- src/test/test-local-addresses.c | 8 +- src/test/test-locale-util.c | 30 +-- src/test/test-modhex.c | 8 +- src/test/test-mount-util.c | 32 +-- src/test/test-net-naming-scheme.c | 15 +- src/test/test-ordered-set.c | 26 +-- src/test/test-os-util.c | 10 +- src/test/test-parse-argument.c | 20 +- src/test/test-parse-socket-bind-item.c | 8 +- src/test/test-parse-util.c | 63 ++---- src/test/test-path-lookup.c | 31 ++- src/test/test-path-util.c | 158 ++++----------- src/test/test-percent-util.c | 28 +-- src/test/test-pretty-print.c | 18 +- src/test/test-prioq.c | 13 +- src/test/test-psi-util.c | 8 +- src/test/test-random-util.c | 38 ++-- src/test/test-ratelimit.c | 9 +- src/test/test-replace-var.c | 11 +- src/test/test-rm-rf.c | 12 +- src/test/test-sd-path.c | 15 +- src/test/test-seccomp.c | 95 ++------- src/test/test-set-disable-mempool.c | 8 +- src/test/test-set.c | 44 ++--- src/test/test-signal-util.c | 21 +- src/test/test-siphash24.c | 43 ++-- src/test/test-socket-netlink.c | 57 ++---- src/test/test-socket-util.c | 68 ++----- src/test/test-specifier.c | 27 +-- src/test/test-stat-util.c | 53 +---- src/test/test-static-destruct.c | 8 +- src/test/test-strbuf.c | 9 +- src/test/test-string-util.c | 184 ++++------------- src/test/test-strip-tab-ansi.c | 7 +- src/test/test-strv.c | 264 +++++++------------------ src/test/test-strxcpyx.c | 25 +-- src/test/test-sysctl-util.c | 15 +- src/test/test-terminal-util.c | 35 +--- src/test/test-time-util.c | 116 +++-------- src/test/test-tmpfile-util.c | 17 +- src/test/test-tmpfiles.c | 10 +- src/test/test-tpm2.c | 37 ++-- src/test/test-uid-alloc-range.c | 31 +-- src/test/test-unaligned.c | 14 +- src/test/test-unit-file.c | 25 +-- src/test/test-user-util.c | 117 ++++------- src/test/test-utf8.c | 78 ++------ src/test/test-util.c | 57 ++---- src/test/test-verbs.c | 12 +- src/test/test-web-util.c | 9 +- src/test/test-xattr-util.c | 17 +- 105 files changed, 1035 insertions(+), 2677 deletions(-) diff --git a/src/test/meson.build b/src/test/meson.build index 275047b23f..de0b8d7fd2 100644 --- a/src/test/meson.build +++ b/src/test/meson.build @@ -389,8 +389,7 @@ tests += [ test_hashmap_ordered_c], [], [], [], '', 'timeout=180'], - [['src/test/test-set.c'], - [libbasic]], + [['src/test/test-set.c']], [['src/test/test-ordered-set.c']], diff --git a/src/test/test-af-list.c b/src/test/test-af-list.c index 672dc68be9..644bc9e27b 100644 --- a/src/test/test-af-list.c +++ b/src/test/test-af-list.c @@ -4,6 +4,7 @@ #include "macro.h" #include "string-util.h" +#include "tests.h" #include "util.h" _unused_ @@ -13,11 +14,8 @@ static const struct af_name* lookup_af(register const char *str, register GPERF_ #include "af-list.h" #include "af-to-name.h" -int main(int argc, const char *argv[]) { - - unsigned i; - - for (i = 0; i < ELEMENTSOF(af_names); i++) { +TEST(af_list) { + for (unsigned i = 0; i < ELEMENTSOF(af_names); i++) { if (af_names[i]) { assert_se(streq(af_to_name(i), af_names[i])); assert_se(af_from_name(af_names[i]) == (int) i); @@ -28,6 +26,6 @@ int main(int argc, const char *argv[]) { assert_se(af_to_name(-1) == NULL); assert_se(af_from_name("huddlduddl") == -EINVAL); assert_se(af_from_name("") == -EINVAL); - - return 0; } + +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-alloc-util.c b/src/test/test-alloc-util.c index fb69009d97..05c3195d6e 100644 --- a/src/test/test-alloc-util.c +++ b/src/test/test-alloc-util.c @@ -9,7 +9,7 @@ #include "random-util.h" #include "tests.h" -static void test_alloca(void) { +TEST(alloca) { static const uint8_t zero[997] = { }; char *t; @@ -22,7 +22,7 @@ static void test_alloca(void) { assert_se(!memcmp(t, zero, 997)); } -static void test_GREEDY_REALLOC(void) { +TEST(GREEDY_REALLOC) { _cleanup_free_ int *a = NULL, *b = NULL; size_t i, j; @@ -55,7 +55,7 @@ static void test_GREEDY_REALLOC(void) { assert_se(b[j] == (int) j); } -static void test_memdup_multiply_and_greedy_realloc(void) { +TEST(memdup_multiply_and_greedy_realloc) { static const int org[] = { 1, 2, 3 }; _cleanup_free_ int *dup; size_t i; @@ -90,7 +90,7 @@ static void test_memdup_multiply_and_greedy_realloc(void) { assert_se(p[i] == 0); } -static void test_bool_assign(void) { +TEST(bool_assign) { bool b, c, *cp = &c, d, e, f, g, h; b = 123; @@ -125,7 +125,7 @@ static void cleanup3(void *a) { assert_se(++cleanup_counter == *(int*) a); } -static void test_cleanup_order(void) { +TEST(cleanup_order) { _cleanup_(cleanup1) int x1 = 4, x2 = 3; _cleanup_(cleanup3) int z = 2; _cleanup_(cleanup2) int y = 1; @@ -135,7 +135,7 @@ static void test_cleanup_order(void) { log_debug("z: %p", &z); } -static void test_auto_erase_memory(void) { +TEST(auto_erase_memory) { _cleanup_(erase_and_freep) uint8_t *p1, *p2; /* print address of p2, else e.g. clang-11 will optimize it out */ @@ -165,7 +165,7 @@ static void test_auto_erase_memory(void) { assert_se(__builtin_object_size(f, 0) >= sizeof(*f) * n); \ } while(false) -static void test_malloc_size_safe(void) { +TEST(malloc_size_safe) { _cleanup_free_ uint32_t *f = NULL; size_t n = 4711; @@ -191,16 +191,4 @@ static void test_malloc_size_safe(void) { } } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_DEBUG); - - test_alloca(); - test_GREEDY_REALLOC(); - test_memdup_multiply_and_greedy_realloc(); - test_bool_assign(); - test_cleanup_order(); - test_auto_erase_memory(); - test_malloc_size_safe(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-arphrd-util.c b/src/test/test-arphrd-util.c index f12388dcce..d8dd4649af 100644 --- a/src/test/test-arphrd-util.c +++ b/src/test/test-arphrd-util.c @@ -6,9 +6,7 @@ #include "string-util.h" #include "tests.h" -int main(int argc, const char *argv[]) { - test_setup_logging(LOG_INFO); - +TEST(arphrd) { for (int i = 0; i <= ARPHRD_VOID + 1; i++) { const char *name; @@ -23,6 +21,6 @@ int main(int argc, const char *argv[]) { assert_se(arphrd_to_name(ARPHRD_VOID + 1) == NULL); assert_se(arphrd_from_name("huddlduddl") == -EINVAL); assert_se(arphrd_from_name("") == -EINVAL); - - return 0; } + +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-ask-password-api.c b/src/test/test-ask-password-api.c index 06158acf12..b24159e2aa 100644 --- a/src/test/test-ask-password-api.c +++ b/src/test/test-ask-password-api.c @@ -4,7 +4,7 @@ #include "strv.h" #include "tests.h" -static void test_ask_password(void) { +TEST(ask_password) { int r; _cleanup_strv_free_ char **ret = NULL; @@ -18,9 +18,4 @@ static void test_ask_password(void) { } } -int main(int argc, char **argv) { - test_setup_logging(LOG_DEBUG); - - test_ask_password(); - return EXIT_SUCCESS; -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-blockdev-util.c b/src/test/test-blockdev-util.c index ab5169c43a..d6ac6da299 100644 --- a/src/test/test-blockdev-util.c +++ b/src/test/test-blockdev-util.c @@ -22,7 +22,7 @@ static void test_path_is_encrypted_one(const char *p, int expect) { assert_se(expect < 0 || ((r > 0) == (expect > 0))); } -static void test_path_is_encrypted(void) { +TEST(path_is_encrypted) { int booted = sd_booted(); /* If this is run in build environments such as koji, /dev might be a * reguar fs. Don't assume too much if not running under systemd. */ @@ -36,8 +36,4 @@ static void test_path_is_encrypted(void) { test_path_is_encrypted_one("/dev", booted > 0 ? false : -1); } -int main(int argc, char **argv) { - test_setup_logging(LOG_INFO); - - test_path_is_encrypted(); -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-bus-util.c b/src/test/test-bus-util.c index 5aa1bfac0e..2f52bca59a 100644 --- a/src/test/test-bus-util.c +++ b/src/test/test-bus-util.c @@ -14,15 +14,13 @@ static void destroy_callback(void *userdata) { (*n_called) ++; } -static void test_destroy_callback(void) { +TEST(destroy_callback) { _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; sd_bus_slot *slot = NULL; sd_bus_destroy_t t; int r, n_called = 0; - log_info("/* %s */", __func__); - r = bus_open_system_watch_bind_with_description(&bus, "test-bus"); if (r < 0) { log_error_errno(r, "Failed to connect to bus: %m"); @@ -46,10 +44,4 @@ static void test_destroy_callback(void) { assert_se(n_called == 1); } -int main(int argc, char **argv) { - test_setup_logging(LOG_DEBUG); - - test_destroy_callback(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-calendarspec.c b/src/test/test-calendarspec.c index 750b9ea2ca..e1862f4eb8 100644 --- a/src/test/test-calendarspec.c +++ b/src/test/test-calendarspec.c @@ -5,6 +5,7 @@ #include "env-util.h" #include "errno-util.h" #include "string-util.h" +#include "tests.h" static void _test_one(int line, const char *input, const char *output) { CalendarSpec *c; @@ -67,7 +68,7 @@ static void _test_next(int line, const char *input, const char *new_tz, usec_t a } #define test_next(input, new_tz, after, expect) _test_next(__LINE__, input,new_tz,after,expect) -static void test_timestamp(void) { +TEST(timestamp) { char buf[FORMAT_TIMESTAMP_MAX]; _cleanup_free_ char *t = NULL; CalendarSpec *c; @@ -88,7 +89,7 @@ static void test_timestamp(void) { assert_se(y == x); } -static void test_hourly_bug_4031(void) { +TEST(hourly_bug_4031) { CalendarSpec *c; usec_t n, u, w; int r; @@ -111,9 +112,7 @@ static void test_hourly_bug_4031(void) { calendar_spec_free(c); } -int main(int argc, char* argv[]) { - CalendarSpec *c; - +TEST(calendar_spec_one) { test_one("Sat,Thu,Mon-Wed,Sat-Sun", "Mon..Thu,Sat,Sun *-*-* 00:00:00"); test_one("Sat,Thu,Mon..Wed,Sat..Sun", "Mon..Thu,Sat,Sun *-*-* 00:00:00"); test_one("Mon,Sun 12-*-* 2,1:23", "Mon,Sun 2012-*-* 01,02:23:00"); @@ -180,7 +179,9 @@ int main(int argc, char* argv[]) { test_one("@0 UTC", "1970-01-01 00:00:00 UTC"); test_one("*:05..05", "*-*-* *:05:00"); test_one("*:05..10/6", "*-*-* *:05:00"); +} +TEST(calendar_spec_next) { test_next("2016-03-27 03:17:00", "", 12345, 1459048620000000); test_next("2016-03-27 03:17:00", "CET", 12345, 1459041420000000); test_next("2016-03-27 03:17:00", "EET", 12345, -1); @@ -214,6 +215,10 @@ int main(int argc, char* argv[]) { /* Check that we don't start looping if mktime() moves us backwards */ test_next("Sun *-*-* 01:00:00 Europe/Dublin", "", 1616412478000000, 1617494400000000); test_next("Sun *-*-* 01:00:00 Europe/Dublin", "IST", 1616412478000000, 1617494400000000); +} + +TEST(calendar_spec_from_string) { + CalendarSpec *c; assert_se(calendar_spec_from_string("test", &c) < 0); assert_se(calendar_spec_from_string(" utc", &c) < 0); @@ -240,9 +245,6 @@ int main(int argc, char* argv[]) { assert_se(calendar_spec_from_string("00:00:2300", &c) < 0); assert_se(calendar_spec_from_string("00:00:18446744073709551615", &c) < 0); assert_se(calendar_spec_from_string("@88588582097858858", &c) == -ERANGE); - - test_timestamp(); - test_hourly_bug_4031(); - - return 0; } + +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-cap-list.c b/src/test/test-cap-list.c index c4b40f34fa..38f7420324 100644 --- a/src/test/test-cap-list.c +++ b/src/test/test-cap-list.c @@ -8,10 +8,11 @@ #include "capability-util.h" #include "parse-util.h" #include "string-util.h" +#include "tests.h" #include "util.h" /* verify the capability parser */ -static void test_cap_list(void) { +TEST(cap_list) { assert_se(!capability_to_name(-1)); assert_se(!capability_to_name(capability_list_length())); @@ -70,7 +71,7 @@ static void test_capability_set_one(uint64_t c, const char *t) { assert_se(c1 == c_masked); } -static void test_capability_set_from_string(void) { +TEST(capability_set_from_string) { uint64_t c; assert_se(capability_set_from_string(NULL, &c) == 0); @@ -89,7 +90,7 @@ static void test_capability_set_from_string(void) { assert_se(c == (UINT64_C(1) << 4) - 1); } -static void test_capability_set_to_string(uint64_t invalid_cap_set) { +static void test_capability_set_to_string_invalid(uint64_t invalid_cap_set) { uint64_t c; test_capability_set_one(invalid_cap_set, ""); @@ -114,15 +115,13 @@ static void test_capability_set_to_string(uint64_t invalid_cap_set) { "cap_audit_control cap_mac_override cap_syslog")); } -int main(int argc, char *argv[]) { - test_cap_list(); - test_capability_set_from_string(); - test_capability_set_to_string(0); +TEST(capability_set_to_string) { + test_capability_set_to_string_invalid(0); /* once the kernel supports 63 caps, there are no 'invalid' numbers * for us to test with */ if (cap_last_cap() < 63) - test_capability_set_to_string(all_capabilities() + 1); - - return 0; + test_capability_set_to_string_invalid(all_capabilities() + 1); } + +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-cgroup-cpu.c b/src/test/test-cgroup-cpu.c index be73be83f8..fcf84d379c 100644 --- a/src/test/test-cgroup-cpu.c +++ b/src/test/test-cgroup-cpu.c @@ -2,10 +2,9 @@ #include "cgroup.h" #include "log.h" +#include "tests.h" -static void test_cgroup_cpu_adjust_period(void) { - log_info("/* %s */", __func__); - +TEST(group_cpu_adjust_period) { /* Period 1ms, quota 40% -> Period 2.5ms */ assert_se(2500 == cgroup_cpu_adjust_period(USEC_PER_MSEC, 400 * USEC_PER_MSEC, USEC_PER_MSEC, USEC_PER_SEC)); /* Period 10ms, quota 10% -> keep. */ @@ -32,7 +31,4 @@ static void test_cgroup_cpu_adjust_period(void) { assert_se(2500 == cgroup_cpu_adjust_period(0, 400 * USEC_PER_MSEC, USEC_PER_MSEC, USEC_PER_SEC)); } -int main(int argc, char *argv[]) { - test_cgroup_cpu_adjust_period(); - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-cgroup.c b/src/test/test-cgroup.c index 3025a9a986..7341e5b022 100644 --- a/src/test/test-cgroup.c +++ b/src/test/test-cgroup.c @@ -10,11 +10,9 @@ #include "string-util.h" #include "tests.h" -static void test_cg_split_spec(void) { +TEST(cg_split_spec) { char *c, *p; - log_info("/* %s */", __func__); - assert_se(cg_split_spec("foobar:/", &c, &p) == 0); assert_se(streq(c, "foobar")); assert_se(streq(p, "/")); @@ -42,8 +40,7 @@ static void test_cg_split_spec(void) { c = mfree(c); } -static void test_cg_create(void) { - log_info("/* %s */", __func__); +TEST(cg_create) { int r; r = cg_unified_cached(false); @@ -128,11 +125,4 @@ static void test_cg_create(void) { assert_se(cg_rmdir(SYSTEMD_CGROUP_CONTROLLER, test_a) == 0); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_DEBUG); - - test_cg_split_spec(); - test_cg_create(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-clock.c b/src/test/test-clock.c index 714935cb81..123831a96d 100644 --- a/src/test/test-clock.c +++ b/src/test/test-clock.c @@ -12,9 +12,10 @@ #include "fs-util.h" #include "log.h" #include "macro.h" +#include "tests.h" #include "tmpfile-util.h" -static void test_clock_is_localtime(void) { +TEST(clock_is_localtime) { _cleanup_(unlink_tempfilep) char adjtime[] = "/tmp/test-adjtime.XXXXXX"; _cleanup_fclose_ FILE* f = NULL; @@ -56,7 +57,7 @@ static void test_clock_is_localtime(void) { } /* Test with the real /etc/adjtime */ -static void test_clock_is_localtime_system(void) { +TEST(clock_is_localtime_system) { int r; r = clock_is_localtime(NULL); @@ -70,9 +71,4 @@ static void test_clock_is_localtime_system(void) { assert_se(r == 0 || ERRNO_IS_PRIVILEGE(r)); } -int main(int argc, char *argv[]) { - test_clock_is_localtime(); - test_clock_is_localtime_system(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-condition.c b/src/test/test-condition.c index f7fa4ef2b1..fff697d5a4 100644 --- a/src/test/test-condition.c +++ b/src/test/test-condition.c @@ -36,7 +36,7 @@ #include "user-util.h" #include "virt.h" -static void test_condition_test_path(void) { +TEST(condition_test_path) { Condition *condition; condition = condition_new(CONDITION_PATH_EXISTS, "/bin/sh", false, false); @@ -125,7 +125,7 @@ static void test_condition_test_path(void) { condition_free(condition); } -static void test_condition_test_control_group_hierarchy(void) { +TEST(condition_test_control_group_hierarchy) { Condition *condition; int r; @@ -147,7 +147,7 @@ static void test_condition_test_control_group_hierarchy(void) { condition_free(condition); } -static void test_condition_test_control_group_controller(void) { +TEST(condition_test_control_group_controller) { Condition *condition; CGroupMask system_mask; _cleanup_free_ char *controller_name = NULL; @@ -216,7 +216,7 @@ static void test_condition_test_control_group_controller(void) { condition_free(condition); } -static void test_condition_test_ac_power(void) { +TEST(condition_test_ac_power) { Condition *condition; condition = condition_new(CONDITION_AC_POWER, "true", false, false); @@ -235,7 +235,7 @@ static void test_condition_test_ac_power(void) { condition_free(condition); } -static void test_condition_test_host(void) { +TEST(condition_test_host) { _cleanup_free_ char *hostname = NULL; Condition *condition; sd_id128_t id; @@ -271,7 +271,7 @@ static void test_condition_test_host(void) { } } -static void test_condition_test_architecture(void) { +TEST(condition_test_architecture) { Condition *condition; const char *sa; int a; @@ -298,7 +298,7 @@ static void test_condition_test_architecture(void) { condition_free(condition); } -static void test_condition_test_kernel_command_line(void) { +TEST(condition_test_kernel_command_line) { Condition *condition; int r; @@ -316,7 +316,7 @@ static void test_condition_test_kernel_command_line(void) { condition_free(condition); } -static void test_condition_test_kernel_version(void) { +TEST(condition_test_kernel_version) { Condition *condition; struct utsname u; const char *v; @@ -459,7 +459,7 @@ static void test_condition_test_kernel_version(void) { } #if defined(__i386__) || defined(__x86_64__) -static void test_condition_test_cpufeature(void) { +TEST(condition_test_cpufeature) { Condition *condition; condition = condition_new(CONDITION_CPU_FEATURE, "fpu", false, false); @@ -479,7 +479,7 @@ static void test_condition_test_cpufeature(void) { } #endif -static void test_condition_test_security(void) { +TEST(condition_test_security) { Condition *condition; condition = condition_new(CONDITION_SECURITY, "garbage oifdsjfoidsjoj", false, false); @@ -523,7 +523,7 @@ static void test_condition_test_security(void) { condition_free(condition); } -static void print_securities(void) { +TEST(print_securities) { log_info("------ enabled security technologies ------"); log_info("SELinux: %s", yes_no(mac_selinux_use())); log_info("AppArmor: %s", yes_no(mac_apparmor_use())); @@ -535,7 +535,7 @@ static void print_securities(void) { log_info("-------------------------------------------"); } -static void test_condition_test_virtualization(void) { +TEST(condition_test_virtualization) { Condition *condition; const char *virt; int r; @@ -594,7 +594,7 @@ static void test_condition_test_virtualization(void) { } } -static void test_condition_test_user(void) { +TEST(condition_test_user) { Condition *condition; char* uid; char* username; @@ -663,7 +663,7 @@ static void test_condition_test_user(void) { condition_free(condition); } -static void test_condition_test_group(void) { +TEST(condition_test_group) { Condition *condition; char* gid; char* groupname; @@ -753,7 +753,7 @@ static void test_condition_test_cpus_one(const char *s, bool result) { condition_free(condition); } -static void test_condition_test_cpus(void) { +TEST(condition_test_cpus) { _cleanup_free_ char *t = NULL; int cpus; @@ -814,7 +814,7 @@ static void test_condition_test_memory_one(const char *s, bool result) { condition_free(condition); } -static void test_condition_test_memory(void) { +TEST(condition_test_memory) { _cleanup_free_ char *t = NULL; uint64_t memory; @@ -874,7 +874,7 @@ static void test_condition_test_environment_one(const char *s, bool result) { condition_free(condition); } -static void test_condition_test_environment(void) { +TEST(condition_test_environment) { assert_se(setenv("EXISTINGENVVAR", "foo", false) >= 0); test_condition_test_environment_one("MISSINGENVVAR", false); @@ -887,7 +887,7 @@ static void test_condition_test_environment(void) { test_condition_test_environment_one("EXISTINGENVVAR=", false); } -static void test_condition_test_os_release(void) { +TEST(condition_test_os_release) { _cleanup_strv_free_ char **os_release_pairs = NULL; _cleanup_free_ char *version_id = NULL; const char *key_value_pair; @@ -1031,29 +1031,4 @@ static void test_condition_test_os_release(void) { condition_free(condition); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_DEBUG); - - test_condition_test_path(); - test_condition_test_ac_power(); - test_condition_test_host(); - test_condition_test_architecture(); - test_condition_test_kernel_command_line(); - test_condition_test_kernel_version(); - test_condition_test_security(); - print_securities(); - test_condition_test_virtualization(); - test_condition_test_user(); - test_condition_test_group(); - test_condition_test_control_group_hierarchy(); - test_condition_test_control_group_controller(); - test_condition_test_cpus(); - test_condition_test_memory(); - test_condition_test_environment(); -#if defined(__i386__) || defined(__x86_64__) - test_condition_test_cpufeature(); -#endif - test_condition_test_os_release(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-conf-files.c b/src/test/test-conf-files.c index ea9898e104..da8f3a97f2 100644 --- a/src/test/test-conf-files.c +++ b/src/test/test-conf-files.c @@ -38,7 +38,7 @@ static void setup_test_dir(char *tmp_dir, const char *files, ...) { va_end(ap); } -static void test_conf_files_list(bool use_root) { +static void test_conf_files_list_one(bool use_root) { char tmp_dir[] = "/tmp/test-conf-files-XXXXXX"; _cleanup_strv_free_ char **found_files = NULL, **found_files2 = NULL; const char *root_dir, *search, *expect_a, *expect_b, *expect_c, *mask; @@ -89,7 +89,12 @@ static void test_conf_files_list(bool use_root) { assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0); } -static void test_conf_files_insert(const char *root) { +TEST(conf_files_list) { + test_conf_files_list_one(false); + test_conf_files_list_one(true); +} + +static void test_conf_files_insert_one(const char *root) { _cleanup_strv_free_ char **s = NULL; log_info("/* %s root=%s */", __func__, strempty(root)); @@ -143,14 +148,10 @@ static void test_conf_files_insert(const char *root) { assert_se(strv_equal(s, STRV_MAKE(bar2, foo1, whatever, zzz3))); } -int main(int argc, char **argv) { - test_setup_logging(LOG_DEBUG); - - test_conf_files_list(false); - test_conf_files_list(true); - test_conf_files_insert(NULL); - test_conf_files_insert("/root"); - test_conf_files_insert("/root/"); - - return 0; +TEST(conf_files_insert) { + test_conf_files_insert_one(NULL); + test_conf_files_insert_one("/root"); + test_conf_files_insert_one("/root/"); } + +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-conf-parser.c b/src/test/test-conf-parser.c index 5ecc3b2d37..e61932ab16 100644 --- a/src/test/test-conf-parser.c +++ b/src/test/test-conf-parser.c @@ -7,6 +7,7 @@ #include "macro.h" #include "string-util.h" #include "strv.h" +#include "tests.h" #include "tmpfile-util.h" #include "util.h" @@ -87,7 +88,7 @@ static void test_config_parse_nsec_one(const char *rvalue, nsec_t expected) { assert_se(expected == v); } -static void test_config_parse_path(void) { +TEST(config_parse_path) { test_config_parse_path_one("/path", "/path"); test_config_parse_path_one("/path//////////", "/path"); test_config_parse_path_one("///path/foo///bar////bar//", "/path/foo/bar/bar"); @@ -98,21 +99,21 @@ static void test_config_parse_path(void) { test_config_parse_path_one("/path/\xc3\x7f", NULL); } -static void test_config_parse_log_level(void) { +TEST(config_parse_log_level) { test_config_parse_log_level_one("debug", LOG_DEBUG); test_config_parse_log_level_one("info", LOG_INFO); test_config_parse_log_level_one("garbage", 0); } -static void test_config_parse_log_facility(void) { +TEST(config_parse_log_facility) { test_config_parse_log_facility_one("mail", LOG_MAIL); test_config_parse_log_facility_one("user", LOG_USER); test_config_parse_log_facility_one("garbage", 0); } -static void test_config_parse_iec_size(void) { +TEST(config_parse_iec_size) { test_config_parse_iec_size_one("1024", 1024); test_config_parse_iec_size_one("2K", 2048); test_config_parse_iec_size_one("10M", 10 * 1024 * 1024); @@ -125,7 +126,7 @@ static void test_config_parse_iec_size(void) { test_config_parse_iec_size_one("garbage", 0); } -static void test_config_parse_si_uint64(void) { +TEST(config_parse_si_uint64) { test_config_parse_si_uint64_one("1024", 1024); test_config_parse_si_uint64_one("2K", 2000); test_config_parse_si_uint64_one("10M", 10 * 1000 * 1000); @@ -138,7 +139,7 @@ static void test_config_parse_si_uint64(void) { test_config_parse_si_uint64_one("garbage", 0); } -static void test_config_parse_int(void) { +TEST(config_parse_int) { test_config_parse_int_one("1024", 1024); test_config_parse_int_one("-1024", -1024); test_config_parse_int_one("0", 0); @@ -149,7 +150,7 @@ static void test_config_parse_int(void) { test_config_parse_int_one("garbage", -1); } -static void test_config_parse_unsigned(void) { +TEST(config_parse_unsigned) { test_config_parse_unsigned_one("10241024", 10241024); test_config_parse_unsigned_one("1024", 1024); test_config_parse_unsigned_one("0", 0); @@ -160,7 +161,7 @@ static void test_config_parse_unsigned(void) { test_config_parse_unsigned_one("1000garbage", 0); } -static void test_config_parse_strv(void) { +TEST(config_parse_strv) { test_config_parse_strv_one("", STRV_MAKE_EMPTY); test_config_parse_strv_one("foo", STRV_MAKE("foo")); test_config_parse_strv_one("foo bar foo", STRV_MAKE("foo", "bar", "foo")); @@ -169,7 +170,7 @@ static void test_config_parse_strv(void) { test_config_parse_strv_one("\xc3\x7f", STRV_MAKE("\xc3\x7f")); } -static void test_config_parse_mode(void) { +TEST(config_parse_mode) { test_config_parse_mode_one("777", 0777); test_config_parse_mode_one("644", 0644); @@ -180,7 +181,7 @@ static void test_config_parse_mode(void) { test_config_parse_mode_one("777 garbage", 0); } -static void test_config_parse_sec(void) { +TEST(config_parse_sec) { test_config_parse_sec_one("1", 1 * USEC_PER_SEC); test_config_parse_sec_one("1s", 1 * USEC_PER_SEC); test_config_parse_sec_one("100ms", 100 * USEC_PER_MSEC); @@ -191,7 +192,7 @@ static void test_config_parse_sec(void) { test_config_parse_sec_one("garbage", 0); } -static void test_config_parse_nsec(void) { +TEST(config_parse_nsec) { test_config_parse_nsec_one("1", 1); test_config_parse_nsec_one("1s", 1 * NSEC_PER_SEC); test_config_parse_nsec_one("100ms", 100 * NSEC_PER_MSEC); @@ -202,7 +203,7 @@ static void test_config_parse_nsec(void) { test_config_parse_nsec_one("garbage", 0); } -static void test_config_parse_iec_uint64(void) { +TEST(config_parse_iec_uint64) { uint64_t offset = 0; assert_se(config_parse_iec_uint64(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4M", &offset, NULL) == 0); assert_se(offset == 4 * 1024 * 1024); @@ -310,7 +311,7 @@ static const char* const config_file[] = { "setting1=3\n", }; -static void test_config_parse(unsigned i, const char *s) { +static void test_config_parse_one(unsigned i, const char *s) { _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-conf-parser.XXXXXX"; _cleanup_fclose_ FILE *f = NULL; _cleanup_free_ char *setting1 = NULL; @@ -385,27 +386,9 @@ static void test_config_parse(unsigned i, const char *s) { } } -int main(int argc, char **argv) { - unsigned i; - - log_parse_environment(); - log_open(); - - test_config_parse_path(); - test_config_parse_log_level(); - test_config_parse_log_facility(); - test_config_parse_iec_size(); - test_config_parse_si_uint64(); - test_config_parse_int(); - test_config_parse_unsigned(); - test_config_parse_strv(); - test_config_parse_mode(); - test_config_parse_sec(); - test_config_parse_nsec(); - test_config_parse_iec_uint64(); - - for (i = 0; i < ELEMENTSOF(config_file); i++) - test_config_parse(i, config_file[i]); - - return 0; +TEST(config_parse) { + for (unsigned i = 0; i < ELEMENTSOF(config_file); i++) + test_config_parse_one(i, config_file[i]); } + +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-copy.c b/src/test/test-copy.c index 0e4b194e2d..c7ed054207 100644 --- a/src/test/test-copy.c +++ b/src/test/test-copy.c @@ -23,15 +23,13 @@ #include "util.h" #include "xattr-util.h" -static void test_copy_file(void) { +TEST(copy_file) { _cleanup_free_ char *buf = NULL; char fn[] = "/tmp/test-copy_file.XXXXXX"; char fn_copy[] = "/tmp/test-copy_file.XXXXXX"; size_t sz = 0; int fd; - log_info("%s", __func__); - fd = mkostemp_safe(fn); assert_se(fd >= 0); close(fd); @@ -52,15 +50,13 @@ static void test_copy_file(void) { unlink(fn_copy); } -static void test_copy_file_fd(void) { +TEST(copy_file_fd) { char in_fn[] = "/tmp/test-copy-file-fd-XXXXXX"; char out_fn[] = "/tmp/test-copy-file-fd-XXXXXX"; _cleanup_close_ int in_fd = -1, out_fd = -1; const char *text = "boohoo\nfoo\n\tbar\n"; char buf[64] = {}; - log_info("%s", __func__); - in_fd = mkostemp_safe(in_fn); assert_se(in_fd >= 0); out_fd = mkostemp_safe(out_fn); @@ -78,7 +74,7 @@ static void test_copy_file_fd(void) { unlink(out_fn); } -static void test_copy_tree(void) { +TEST(copy_tree) { char original_dir[] = "/tmp/test-copy_tree/"; char copy_dir[] = "/tmp/test-copy_tree-copy/"; char **files = STRV_MAKE("file", "dir1/file", "dir1/dir2/file", "dir1/dir2/dir3/dir4/dir5/file"); @@ -92,8 +88,6 @@ static void test_copy_tree(void) { int xattr_worked = -1; /* xattr support is optional in temporary directories, hence use it if we can, * but don't fail if we can't */ - log_info("%s", __func__); - (void) rm_rf(copy_dir, REMOVE_ROOT|REMOVE_PHYSICAL); (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL); @@ -194,7 +188,7 @@ static void test_copy_tree(void) { (void) rm_rf(original_dir, REMOVE_ROOT|REMOVE_PHYSICAL); } -static void test_copy_bytes(void) { +TEST(copy_bytes) { _cleanup_close_pair_ int pipefd[2] = {-1, -1}; _cleanup_close_ int infd = -1; int r, r2; @@ -230,7 +224,7 @@ static void test_copy_bytes(void) { assert_se(r == -EBADF); } -static void test_copy_bytes_regular_file(const char *src, bool try_reflink, uint64_t max_bytes) { +static void test_copy_bytes_regular_file_one(const char *src, bool try_reflink, uint64_t max_bytes) { char fn2[] = "/tmp/test-copy-file-XXXXXX"; char fn3[] = "/tmp/test-copy-file-XXXXXX"; _cleanup_close_ int fd = -1, fd2 = -1, fd3 = -1; @@ -286,7 +280,16 @@ static void test_copy_bytes_regular_file(const char *src, bool try_reflink, uint unlink(fn3); } -static void test_copy_atomic(void) { +TEST(copy_bytes_regular_file) { + test_copy_bytes_regular_file_one(saved_argv[0], false, UINT64_MAX); + test_copy_bytes_regular_file_one(saved_argv[0], true, UINT64_MAX); + test_copy_bytes_regular_file_one(saved_argv[0], false, 1000); /* smaller than copy buffer size */ + test_copy_bytes_regular_file_one(saved_argv[0], true, 1000); + test_copy_bytes_regular_file_one(saved_argv[0], false, 32000); /* larger than copy buffer size */ + test_copy_bytes_regular_file_one(saved_argv[0], true, 32000); +} + +TEST(copy_atomic) { _cleanup_(rm_rf_physical_and_freep) char *p = NULL; const char *q; int r; @@ -304,7 +307,7 @@ static void test_copy_atomic(void) { assert_se(copy_file_atomic("/etc/fstab", q, 0644, 0, 0, COPY_REPLACE) >= 0); } -static void test_copy_proc(void) { +TEST(copy_proc) { _cleanup_(rm_rf_physical_and_freep) char *p = NULL; _cleanup_free_ char *f = NULL, *a = NULL, *b = NULL; @@ -320,21 +323,4 @@ static void test_copy_proc(void) { assert_se(!isempty(a)); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_DEBUG); - - test_copy_file(); - test_copy_file_fd(); - test_copy_tree(); - test_copy_bytes(); - test_copy_bytes_regular_file(argv[0], false, UINT64_MAX); - test_copy_bytes_regular_file(argv[0], true, UINT64_MAX); - test_copy_bytes_regular_file(argv[0], false, 1000); /* smaller than copy buffer size */ - test_copy_bytes_regular_file(argv[0], true, 1000); - test_copy_bytes_regular_file(argv[0], false, 32000); /* larger than copy buffer size */ - test_copy_bytes_regular_file(argv[0], true, 32000); - test_copy_atomic(); - test_copy_proc(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-coredump-util.c b/src/test/test-coredump-util.c index f9a44b2c65..a669da7746 100644 --- a/src/test/test-coredump-util.c +++ b/src/test/test-coredump-util.c @@ -5,9 +5,7 @@ #include "macro.h" #include "tests.h" -static void test_coredump_filter_to_from_string(void) { - log_info("/* %s */", __func__); - +TEST(coredump_filter_to_from_string) { for (CoredumpFilter i = 0; i < _COREDUMP_FILTER_MAX; i++) { const char *n; @@ -21,9 +19,7 @@ static void test_coredump_filter_to_from_string(void) { } } -static void test_coredump_filter_mask_from_string(void) { - log_info("/* %s */", __func__); - +TEST(coredump_filter_mask_from_string) { uint64_t f; assert_se(coredump_filter_mask_from_string("default", &f) == 0); assert_se(f == COREDUMP_FILTER_MASK_DEFAULT); @@ -68,11 +64,4 @@ static void test_coredump_filter_mask_from_string(void) { 1 << COREDUMP_FILTER_SHARED_DAX))); } -int main(int argc, char **argv) { - test_setup_logging(LOG_INFO); - - test_coredump_filter_to_from_string(); - test_coredump_filter_mask_from_string(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-cpu-set-util.c b/src/test/test-cpu-set-util.c index c07b939506..a0660f579e 100644 --- a/src/test/test-cpu-set-util.c +++ b/src/test/test-cpu-set-util.c @@ -3,15 +3,14 @@ #include "alloc-util.h" #include "cpu-set-util.h" #include "string-util.h" +#include "tests.h" #include "macro.h" -static void test_parse_cpu_set(void) { +TEST(parse_cpu_set) { CPUSet c = {}; _cleanup_free_ char *str = NULL; int cpu; - log_info("/* %s */", __func__); - /* Single value */ assert_se(parse_cpu_set_full("0", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0); assert_se(c.set); @@ -210,12 +209,10 @@ static void test_parse_cpu_set(void) { cpu_set_reset(&c); } -static void test_parse_cpu_set_extend(void) { +TEST(parse_cpu_set_extend) { CPUSet c = {}; _cleanup_free_ char *s1 = NULL, *s2 = NULL; - log_info("/* %s */", __func__); - assert_se(parse_cpu_set_extend("1 3", &c, true, NULL, "fake", 1, "CPUAffinity") == 1); assert_se(CPU_COUNT_S(c.allocated, c.set) == 2); assert_se(s1 = cpu_set_to_string(&c)); @@ -232,12 +229,10 @@ static void test_parse_cpu_set_extend(void) { log_info("cpu_set_to_string: (null)"); } -static void test_cpu_set_to_from_dbus(void) { +TEST(cpu_set_to_from_dbus) { _cleanup_(cpu_set_reset) CPUSet c = {}, c2 = {}; _cleanup_free_ char *s = NULL; - log_info("/* %s */", __func__); - assert_se(parse_cpu_set_extend("1 3 8 100-200", &c, true, NULL, "fake", 1, "CPUAffinity") == 1); assert_se(s = cpu_set_to_string(&c)); log_info("cpu_set_to_string: %s", s); @@ -264,7 +259,7 @@ static void test_cpu_set_to_from_dbus(void) { assert_se(memcmp(c.set, c2.set, c.allocated) == 0); } -static void test_cpus_in_affinity_mask(void) { +TEST(cpus_in_affinity_mask) { int r; r = cpus_in_affinity_mask(); @@ -272,7 +267,7 @@ static void test_cpus_in_affinity_mask(void) { log_info("cpus_in_affinity_mask: %d", r); } -int main(int argc, char *argv[]) { +TEST(print_cpu_alloc_size) { log_info("CPU_ALLOC_SIZE(1) = %zu", CPU_ALLOC_SIZE(1)); log_info("CPU_ALLOC_SIZE(9) = %zu", CPU_ALLOC_SIZE(9)); log_info("CPU_ALLOC_SIZE(64) = %zu", CPU_ALLOC_SIZE(64)); @@ -280,11 +275,6 @@ int main(int argc, char *argv[]) { log_info("CPU_ALLOC_SIZE(1024) = %zu", CPU_ALLOC_SIZE(1024)); log_info("CPU_ALLOC_SIZE(1025) = %zu", CPU_ALLOC_SIZE(1025)); log_info("CPU_ALLOC_SIZE(8191) = %zu", CPU_ALLOC_SIZE(8191)); - - test_parse_cpu_set(); - test_parse_cpu_set_extend(); - test_cpus_in_affinity_mask(); - test_cpu_set_to_from_dbus(); - - return 0; } + +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-data-fd-util.c b/src/test/test-data-fd-util.c index 8a506ae75e..432ba14825 100644 --- a/src/test/test-data-fd-util.c +++ b/src/test/test-data-fd-util.c @@ -47,7 +47,7 @@ static void test_acquire_data_fd_one(unsigned flags) { fd = safe_close(fd); } -static void test_acquire_data_fd(void) { +TEST(acquire_data_fd) { test_acquire_data_fd_one(0); test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL); test_acquire_data_fd_one(ACQUIRE_NO_MEMFD); @@ -79,7 +79,7 @@ static void assert_equal_fd(int fd1, int fd2) { } } -static void test_copy_data_fd(void) { +TEST(copy_data_fd) { _cleanup_close_ int fd1 = -1, fd2 = -1; _cleanup_(close_pairp) int sfd[2] = { -1, -1 }; _cleanup_(sigkill_waitp) pid_t pid = -1; @@ -145,11 +145,4 @@ static void test_copy_data_fd(void) { assert_se(read(fd2, &j, sizeof(j)) == 0); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_DEBUG); - - test_acquire_data_fd(); - test_copy_data_fd(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-device-nodes.c b/src/test/test-device-nodes.c index 7ba05b53e9..36fa2ce8ee 100644 --- a/src/test/test-device-nodes.c +++ b/src/test/test-device-nodes.c @@ -6,6 +6,7 @@ #include "alloc-util.h" #include "device-nodes.h" #include "string-util.h" +#include "tests.h" /* helpers for test_encode_devnode_name */ static char *do_encode_string(const char *in) { @@ -24,7 +25,7 @@ static bool expect_encoded_as(const char *in, const char *expected) { return streq(encoded, expected); } -static void test_encode_devnode_name(void) { +TEST(encode_devnode_name) { assert_se(expect_encoded_as("systemd sucks", "systemd\\x20sucks")); assert_se(expect_encoded_as("pinkiepie", "pinkiepie")); assert_se(expect_encoded_as("valíd\\ųtf8", "valíd\\x5cųtf8")); @@ -34,8 +35,4 @@ static void test_encode_devnode_name(void) { assert_se(expect_encoded_as("QEMU ", "QEMU\\x20\\x20\\x20\\x20")); } -int main(int argc, char *argv[]) { - test_encode_devnode_name(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-dns-domain.c b/src/test/test-dns-domain.c index 2df2380de4..a6e0c8501e 100644 --- a/src/test/test-dns-domain.c +++ b/src/test/test-dns-domain.c @@ -32,9 +32,7 @@ static void test_dns_label_unescape_one(const char *what, const char *expect, si assert_se(streq(buffer, expect)); } -static void test_dns_label_unescape(void) { - log_info("/* %s */", __func__); - +TEST(dns_label_unescape) { test_dns_label_unescape_one("hallo", "hallo", 6, 5, 5); test_dns_label_unescape_one("hallo", "hallo", 4, -ENOBUFS, -ENOBUFS); test_dns_label_unescape_one("", "", 10, 0, 0); @@ -74,7 +72,7 @@ static void test_dns_name_to_wire_format_one(const char *what, const char *expec assert_se(!memcmp(buffer, expect, r)); } -static void test_dns_name_to_wire_format(void) { +TEST(dns_name_to_wire_format) { static const char out0[] = { 0 }; static const char out1[] = { 3, 'f', 'o', 'o', 0 }; static const char out2[] = { 5, 'h', 'a', 'l', 'l', 'o', 3, 'f', 'o', 'o', 3, 'b', 'a', 'r', 0 }; @@ -106,8 +104,6 @@ static void test_dns_name_to_wire_format(void) { 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', 3, 'a', '1', '2', 0 }; - log_info("/* %s */", __func__); - test_dns_name_to_wire_format_one("", out0, sizeof(out0), sizeof(out0)); test_dns_name_to_wire_format_one("foo", out1, sizeof(out1), sizeof(out1)); @@ -143,9 +139,7 @@ static void test_dns_label_unescape_suffix_one(const char *what, const char *exp assert_se(streq(buffer, expect2)); } -static void test_dns_label_unescape_suffix(void) { - log_info("/* %s */", __func__); - +TEST(dns_label_unescape_suffix) { test_dns_label_unescape_suffix_one("hallo", "hallo", "", 6, 5, 0); test_dns_label_unescape_suffix_one("hallo", "hallo", "", 4, -ENOBUFS, -ENOBUFS); test_dns_label_unescape_suffix_one("", "", "", 10, 0, 0); @@ -182,9 +176,7 @@ static void test_dns_label_escape_one(const char *what, size_t l, const char *ex assert_se(streq_ptr(expect, t)); } -static void test_dns_label_escape(void) { - log_info("/* %s */", __func__); - +TEST(dns_label_escape) { test_dns_label_escape_one("", 0, NULL, -EINVAL); test_dns_label_escape_one("hallo", 5, "hallo", 5); test_dns_label_escape_one("hallo", 6, "hallo\\000", 9); @@ -204,7 +196,7 @@ static void test_dns_name_normalize_one(const char *what, const char *expect, in assert_se(streq_ptr(expect, t)); } -static void test_dns_name_normalize(void) { +TEST(dns_name_normalize) { test_dns_name_normalize_one("", ".", 0); test_dns_name_normalize_one("f", "f", 0); test_dns_name_normalize_one("f.waldi", "f.waldi", 0); @@ -226,7 +218,7 @@ static void test_dns_name_equal_one(const char *a, const char *b, int ret) { assert_se(r == ret); } -static void test_dns_name_equal(void) { +TEST(dns_name_equal) { test_dns_name_equal_one("", "", true); test_dns_name_equal_one("x", "x", true); test_dns_name_equal_one("x", "x.", true); @@ -254,7 +246,7 @@ static void test_dns_name_between_one(const char *a, const char *b, const char * assert_se(r == ret); } -static void test_dns_name_between(void) { +TEST(dns_name_between) { /* see https://tools.ietf.org/html/rfc4034#section-6.1 Note that we use "\033.z.example" in stead of "\001.z.example" as we consider the latter invalid */ @@ -279,7 +271,7 @@ static void test_dns_name_endswith_one(const char *a, const char *b, int ret) { assert_se(dns_name_endswith(a, b) == ret); } -static void test_dns_name_endswith(void) { +TEST(dns_name_endswith) { test_dns_name_endswith_one("", "", true); test_dns_name_endswith_one("", "xxx", false); test_dns_name_endswith_one("xxx", "", true); @@ -301,7 +293,7 @@ static void test_dns_name_startswith_one(const char *a, const char *b, int ret) assert_se(dns_name_startswith(a, b) == ret); } -static void test_dns_name_startswith(void) { +TEST(dns_name_startswith) { test_dns_name_startswith_one("", "", true); test_dns_name_startswith_one("", "xxx", false); test_dns_name_startswith_one("xxx", "", true); @@ -316,7 +308,7 @@ static void test_dns_name_startswith(void) { test_dns_name_startswith_one("x.y", "X", true); } -static void test_dns_name_is_root(void) { +TEST(dns_name_is_root) { assert_se(dns_name_is_root("")); assert_se(dns_name_is_root(".")); assert_se(!dns_name_is_root("xxx")); @@ -324,7 +316,7 @@ static void test_dns_name_is_root(void) { assert_se(!dns_name_is_root("..")); } -static void test_dns_name_is_single_label(void) { +TEST(dns_name_is_single_label) { assert_se(!dns_name_is_single_label("")); assert_se(!dns_name_is_single_label(".")); assert_se(!dns_name_is_single_label("..")); @@ -346,7 +338,7 @@ static void test_dns_name_reverse_one(const char *address, const char *name) { assert_se(in_addr_equal(familya, &a, &b)); } -static void test_dns_name_reverse(void) { +TEST(dns_name_reverse) { test_dns_name_reverse_one("47.11.8.15", "15.8.11.47.in-addr.arpa"); test_dns_name_reverse_one("fe80::47", "7.4.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f.ip6.arpa"); test_dns_name_reverse_one("127.0.0.1", "1.0.0.127.in-addr.arpa"); @@ -360,7 +352,7 @@ static void test_dns_name_concat_one(const char *a, const char *b, int r, const assert_se(streq_ptr(p, result)); } -static void test_dns_name_concat(void) { +TEST(dns_name_concat) { test_dns_name_concat_one("", "", 0, "."); test_dns_name_concat_one(".", "", 0, "."); test_dns_name_concat_one("", ".", 0, "."); @@ -382,9 +374,7 @@ static void test_dns_name_is_valid_one(const char *s, int ret, int ret_ldh) { assert_se(dns_name_is_valid_ldh(s) == ret_ldh); } -static void test_dns_name_is_valid(void) { - log_info("/* %s */", __func__); - +TEST(dns_name_is_valid) { test_dns_name_is_valid_one("foo", 1, 1); test_dns_name_is_valid_one("foo.", 1, 1); test_dns_name_is_valid_one("foo..", 0, 0); @@ -434,9 +424,7 @@ static void test_dns_name_is_valid(void) { test_dns_name_is_valid_one("a123456789a123456789a123456789a123456789a123456789a123456789a12", 1, 1); } -static void test_dns_service_name_is_valid(void) { - log_info("/* %s */", __func__); - +TEST(dns_service_name_is_valid) { assert_se(dns_service_name_is_valid("Lennart's Compüter")); assert_se(dns_service_name_is_valid("piff.paff")); @@ -447,9 +435,7 @@ static void test_dns_service_name_is_valid(void) { assert_se(!dns_service_name_is_valid("this is an overly long string that is certainly longer than 63 characters")); } -static void test_dns_srv_type_is_valid(void) { - log_info("/* %s */", __func__); - +TEST(dns_srv_type_is_valid) { assert_se(dns_srv_type_is_valid("_http._tcp")); assert_se(dns_srv_type_is_valid("_foo-bar._tcp")); assert_se(dns_srv_type_is_valid("_w._udp")); @@ -471,9 +457,7 @@ static void test_dns_srv_type_is_valid(void) { assert_se(!dns_srv_type_is_valid("_piep._foo._udp")); } -static void test_dnssd_srv_type_is_valid(void) { - log_info("/* %s */", __func__); - +TEST(dnssd_srv_type_is_valid) { assert_se(dnssd_srv_type_is_valid("_http._tcp")); assert_se(dnssd_srv_type_is_valid("_foo-bar._tcp")); assert_se(dnssd_srv_type_is_valid("_w._udp")); @@ -513,9 +497,7 @@ static void test_dns_service_join_one(const char *a, const char *b, const char * assert_se(dns_name_equal(c, z) > 0); } -static void test_dns_service_join(void) { - log_info("/* %s */", __func__); - +TEST(dns_service_join) { test_dns_service_join_one("", "", "", -EINVAL, NULL); test_dns_service_join_one("", "_http._tcp", "", -EINVAL, NULL); test_dns_service_join_one("", "_http._tcp", "foo", -EINVAL, NULL); @@ -550,9 +532,7 @@ static void test_dns_service_split_one(const char *joined, const char *a, const assert_se(!x && dns_name_equal(z, joined) > 0); } -static void test_dns_service_split(void) { - log_info("/* %s */", __func__); - +TEST(dns_service_split) { test_dns_service_split_one("", NULL, NULL, ".", 0); test_dns_service_split_one("foo", NULL, NULL, "foo", 0); test_dns_service_split_one("foo.bar", NULL, NULL, "foo.bar", 0); @@ -571,9 +551,7 @@ static void test_dns_name_change_suffix_one(const char *name, const char *old_su assert_se(streq_ptr(s, result)); } -static void test_dns_name_change_suffix(void) { - log_info("/* %s */", __func__); - +TEST(dns_name_change_suffix) { test_dns_name_change_suffix_one("foo.bar", "bar", "waldo", 1, "foo.waldo"); test_dns_name_change_suffix_one("foo.bar.waldi.quux", "foo.bar.waldi.quux", "piff.paff", 1, "piff.paff"); test_dns_name_change_suffix_one("foo.bar.waldi.quux", "bar.waldi.quux", "piff.paff", 1, "foo.piff.paff"); @@ -594,9 +572,7 @@ static void test_dns_name_suffix_one(const char *name, unsigned n_labels, const assert_se(streq_ptr(p, result)); } -static void test_dns_name_suffix(void) { - log_info("/* %s */", __func__); - +TEST(dns_name_suffix) { test_dns_name_suffix_one("foo.bar", 2, "foo.bar", 0); test_dns_name_suffix_one("foo.bar", 1, "bar", 1); test_dns_name_suffix_one("foo.bar", 0, "", 2); @@ -619,9 +595,7 @@ static void test_dns_name_count_labels_one(const char *name, int n) { assert_se(dns_name_count_labels(name) == n); } -static void test_dns_name_count_labels(void) { - log_info("/* %s */", __func__); - +TEST(dns_name_count_labels) { test_dns_name_count_labels_one("foo.bar.quux.", 3); test_dns_name_count_labels_one("foo.bar.quux", 3); test_dns_name_count_labels_one("foo.bar.", 2); @@ -639,9 +613,7 @@ static void test_dns_name_equal_skip_one(const char *a, unsigned n_labels, const assert_se(dns_name_equal_skip(a, n_labels, b) == ret); } -static void test_dns_name_equal_skip(void) { - log_info("/* %s */", __func__); - +TEST(dns_name_equal_skip) { test_dns_name_equal_skip_one("foo", 0, "bar", 0); test_dns_name_equal_skip_one("foo", 0, "foo", 1); test_dns_name_equal_skip_one("foo", 1, "foo", 0); @@ -668,9 +640,7 @@ static void test_dns_name_equal_skip(void) { test_dns_name_equal_skip_one("", 2, "foo", 0); } -static void test_dns_name_compare_func(void) { - log_info("/* %s */", __func__); - +TEST(dns_name_compare_func) { assert_se(dns_name_compare_func("", "") == 0); assert_se(dns_name_compare_func("", ".") == 0); assert_se(dns_name_compare_func(".", "") == 0); @@ -692,9 +662,7 @@ static void test_dns_name_common_suffix_one(const char *a, const char *b, const assert_se(streq(c, result)); } -static void test_dns_name_common_suffix(void) { - log_info("/* %s */", __func__); - +TEST(dns_name_common_suffix) { test_dns_name_common_suffix_one("", "", ""); test_dns_name_common_suffix_one("foo", "", ""); test_dns_name_common_suffix_one("", "foo", ""); @@ -723,13 +691,8 @@ static void test_dns_name_apply_idna_one(const char *s, int expected, const char assert_se(dns_name_equal(buf, result) == 1); } -static void test_dns_name_apply_idna(void) { -#if HAVE_LIBIDN2 || HAVE_LIBIDN - const int ret = 1; -#else - const int ret = 0; -#endif - log_info("/* %s */", __func__); +TEST(dns_name_apply_idna) { + const int ret = HAVE_LIBIDN2 | HAVE_LIBIDN; /* IDNA2008 forbids names with hyphens in third and fourth positions * (https://tools.ietf.org/html/rfc5891#section-4.2.3.1). @@ -739,11 +702,7 @@ static void test_dns_name_apply_idna(void) { * labels. If registrars follow IDNA2008 we'll just be performing a * useless lookup. */ -#if HAVE_LIBIDN - const int ret2 = 1; -#else - const int ret2 = 0; -#endif + const int ret2 = HAVE_LIBIDN; test_dns_name_apply_idna_one("", ret, ""); test_dns_name_apply_idna_one("foo", ret, "foo"); @@ -766,9 +725,7 @@ static void test_dns_name_apply_idna(void) { ret2 ? "r3---sn-ab5l6ne7.googlevideo.com" : ""); } -static void test_dns_name_is_valid_or_address(void) { - log_info("/* %s */", __func__); - +TEST(dns_name_is_valid_or_address) { assert_se(dns_name_is_valid_or_address(NULL) == 0); assert_se(dns_name_is_valid_or_address("") == 0); assert_se(dns_name_is_valid_or_address("foobar") > 0); @@ -780,9 +737,7 @@ static void test_dns_name_is_valid_or_address(void) { assert_se(dns_name_is_valid_or_address("::1") > 0); } -static void test_dns_name_dot_suffixed(void) { - log_info("/* %s */", __func__); - +TEST(dns_name_dot_suffixed) { assert_se(dns_name_dot_suffixed("") == 0); assert_se(dns_name_dot_suffixed(".") > 0); assert_se(dns_name_dot_suffixed("foo") == 0); @@ -794,37 +749,4 @@ static void test_dns_name_dot_suffixed(void) { assert_se(dns_name_dot_suffixed("foo.bar\\.\\.\\.\\.") == 0); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_DEBUG); - - test_dns_label_unescape(); - test_dns_label_unescape_suffix(); - test_dns_label_escape(); - test_dns_name_normalize(); - test_dns_name_equal(); - test_dns_name_endswith(); - test_dns_name_startswith(); - test_dns_name_between(); - test_dns_name_is_root(); - test_dns_name_is_single_label(); - test_dns_name_reverse(); - test_dns_name_concat(); - test_dns_name_is_valid(); - test_dns_name_to_wire_format(); - test_dns_service_name_is_valid(); - test_dns_srv_type_is_valid(); - test_dnssd_srv_type_is_valid(); - test_dns_service_join(); - test_dns_service_split(); - test_dns_name_change_suffix(); - test_dns_name_suffix(); - test_dns_name_count_labels(); - test_dns_name_equal_skip(); - test_dns_name_compare_func(); - test_dns_name_common_suffix(); - test_dns_name_apply_idna(); - test_dns_name_is_valid_or_address(); - test_dns_name_dot_suffixed(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-ellipsize.c b/src/test/test-ellipsize.c index a25c0b57bd..b840355f5e 100644 --- a/src/test/test-ellipsize.c +++ b/src/test/test-ellipsize.c @@ -7,6 +7,7 @@ #include "string-util.h" #include "strv.h" #include "terminal-util.h" +#include "tests.h" #include "util.h" #include "utf8.h" @@ -62,7 +63,7 @@ static void test_ellipsize_mem_one(const char *s, size_t old_length, size_t new_ } } -static void test_ellipsize_mem(void) { +TEST(ellipsize_mem) { const char *s; ssize_t l, k; @@ -108,7 +109,7 @@ static void test_ellipsize_one(const char *p) { puts(t); } -static void test_ellipsize(void) { +TEST(ellipsize) { test_ellipsize_one(DIGITS LETTERS DIGITS LETTERS); test_ellipsize_one("한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어"); test_ellipsize_one("-日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国"); @@ -119,9 +120,4 @@ static void test_ellipsize(void) { test_ellipsize_one("shórt"); } -int main(int argc, char *argv[]) { - test_ellipsize_mem(); - test_ellipsize(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-emergency-action.c b/src/test/test-emergency-action.c index 88214aa7a6..36dd1e42e6 100644 --- a/src/test/test-emergency-action.c +++ b/src/test/test-emergency-action.c @@ -3,11 +3,9 @@ #include "emergency-action.h" #include "tests.h" -static void test_parse_emergency_action(void) { +TEST(parse_emergency_action) { EmergencyAction x; - log_info("/* %s */", __func__); - assert_se(parse_emergency_action("none", false, &x) == 0); assert_se(x == EMERGENCY_ACTION_NONE); assert_se(parse_emergency_action("reboot", false, &x) == -EOPNOTSUPP); @@ -42,10 +40,4 @@ static void test_parse_emergency_action(void) { assert_se(x == EMERGENCY_ACTION_EXIT_FORCE); } -int main(int argc, char **argv) { - test_setup_logging(LOG_INFO); - - test_parse_emergency_action(); - - return EXIT_SUCCESS; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-env-file.c b/src/test/test-env-file.c index a3acde17ba..6cc2455c1f 100644 --- a/src/test/test-env-file.c +++ b/src/test/test-env-file.c @@ -42,7 +42,7 @@ "a=\n" \ "b=" -static void test_load_env_file_1(void) { +TEST(load_env_file_1) { _cleanup_strv_free_ char **data = NULL; int r; @@ -64,7 +64,7 @@ static void test_load_env_file_1(void) { assert_se(data[6] == NULL); } -static void test_load_env_file_2(void) { +TEST(load_env_file_2) { _cleanup_strv_free_ char **data = NULL; int r; @@ -81,7 +81,7 @@ static void test_load_env_file_2(void) { assert_se(data[1] == NULL); } -static void test_load_env_file_3(void) { +TEST(load_env_file_3) { _cleanup_strv_free_ char **data = NULL; int r; @@ -97,7 +97,7 @@ static void test_load_env_file_3(void) { assert_se(data == NULL); } -static void test_load_env_file_4(void) { +TEST(load_env_file_4) { _cleanup_strv_free_ char **data = NULL; _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-load-env-file.XXXXXX"; _cleanup_close_ int fd; @@ -115,7 +115,7 @@ static void test_load_env_file_4(void) { assert_se(data[3] == NULL); } -static void test_load_env_file_5(void) { +TEST(load_env_file_5) { _cleanup_strv_free_ char **data = NULL; int r; @@ -133,7 +133,7 @@ static void test_load_env_file_5(void) { assert_se(data[2] == NULL); } -static void test_write_and_load_env_file(void) { +TEST(write_and_load_env_file) { const char *v; /* Make sure that our writer, parser and the shell agree on what our env var files mean */ @@ -173,16 +173,4 @@ static void test_write_and_load_env_file(void) { } } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_INFO); - - test_load_env_file_1(); - test_load_env_file_2(); - test_load_env_file_3(); - test_load_env_file_4(); - test_load_env_file_5(); - - test_write_and_load_env_file(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-env-util.c b/src/test/test-env-util.c index fb5dfd7461..19523aa0d9 100644 --- a/src/test/test-env-util.c +++ b/src/test/test-env-util.c @@ -12,9 +12,7 @@ #include "tests.h" #include "util.h" -static void test_strv_env_delete(void) { - log_info("/* %s */", __func__); - +TEST(strv_env_delete) { _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL, **d = NULL; a = strv_new("FOO=BAR", "WALDO=WALDO", "WALDO=", "PIEP", "SCHLUMPF=SMURF"); @@ -34,9 +32,7 @@ static void test_strv_env_delete(void) { assert_se(strv_length(d) == 2); } -static void test_strv_env_get(void) { - log_info("/* %s */", __func__); - +TEST(strv_env_get) { char **l = STRV_MAKE("ONE_OR_TWO=1", "THREE=3", "ONE_OR_TWO=2", "FOUR=4"); assert_se(streq(strv_env_get(l, "ONE_OR_TWO"), "2")); @@ -44,9 +40,7 @@ static void test_strv_env_get(void) { assert_se(streq(strv_env_get(l, "FOUR"), "4")); } -static void test_strv_env_pairs_get(void) { - log_info("/* %s */", __func__); - +TEST(strv_env_pairs_get) { char **l = STRV_MAKE("ONE_OR_TWO", "1", "THREE", "3", "ONE_OR_TWO", "2", "FOUR", "4", "FIVE", "5", "SIX", "FIVE", "SEVEN", "7"); assert_se(streq(strv_env_pairs_get(l, "ONE_OR_TWO"), "2")); @@ -55,9 +49,7 @@ static void test_strv_env_pairs_get(void) { assert_se(streq(strv_env_pairs_get(l, "FIVE"), "5")); } -static void test_strv_env_unset(void) { - log_info("/* %s */", __func__); - +TEST(strv_env_unset) { _cleanup_strv_free_ char **l = NULL; l = strv_new("PIEP", "SCHLUMPF=SMURFF", "NANANANA=YES"); @@ -70,9 +62,7 @@ static void test_strv_env_unset(void) { assert_se(strv_length(l) == 2); } -static void test_strv_env_merge(void) { - log_info("/* %s */", __func__); - +TEST(strv_env_merge) { char **a = STRV_MAKE("FOO=BAR", "WALDO=WALDO", "WALDO=", "PIEP", "SCHLUMPF=SMURF", "EQ==="); char **b = STRV_MAKE("FOO=KKK", "FOO=", "PIEP=", "SCHLUMPF=SMURFF", "NANANANA=YES"); @@ -97,9 +87,7 @@ static void test_strv_env_merge(void) { assert_se(strv_length(r) == 6); } -static void test_strv_env_replace_strdup(void) { - log_info("/* %s */", __func__); - +TEST(strv_env_replace_strdup) { _cleanup_strv_free_ char **a = NULL; assert_se(strv_env_replace_strdup(&a, "a=a") == 1); @@ -113,9 +101,7 @@ static void test_strv_env_replace_strdup(void) { assert_se(streq(a[1], "b=b")); } -static void test_strv_env_replace_strdup_passthrough(void) { - log_info("/* %s */", __func__); - +TEST(strv_env_replace_strdup_passthrough) { _cleanup_strv_free_ char **a = NULL; assert_se(putenv((char*) "a=a") == 0); @@ -134,9 +120,7 @@ static void test_strv_env_replace_strdup_passthrough(void) { assert_se(streq(a[2], "c=")); } -static void test_strv_env_assign(void) { - log_info("/* %s */", __func__); - +TEST(strv_env_assign) { _cleanup_strv_free_ char **a = NULL; assert_se(strv_env_assign(&a, "a", "a") == 1); @@ -150,9 +134,7 @@ static void test_strv_env_assign(void) { assert_se(streq(a[0], "a=A")); } -static void test_env_strv_get_n(void) { - log_info("/* %s */", __func__); - +TEST(env_strv_get_n) { const char *_env[] = { "FOO=NO NO NO", "FOO=BAR BAR", @@ -182,7 +164,7 @@ static void test_env_strv_get_n(void) { getenv("PATH"))); } -static void test_replace_env(bool braceless) { +static void test_replace_env1(bool braceless) { log_info("/* %s(braceless=%s) */", __func__, yes_no(braceless)); const char *env[] = { @@ -242,9 +224,14 @@ static void test_replace_env2(bool extended) { assert_se(streq(y, extended ? "FOO=foobetweenbar" : "FOO=foobetween${BAR:-baz}")); } -static void test_replace_env_argv(void) { - log_info("/* %s */", __func__); +TEST(replace_env) { + test_replace_env1(false); + test_replace_env1(true); + test_replace_env2(false); + test_replace_env2(true); +} +TEST(replace_env_argv) { const char *env[] = { "FOO=BAR BAR", "BAR=waldo", @@ -294,9 +281,7 @@ static void test_replace_env_argv(void) { assert_se(strv_length(r) == 17); } -static void test_env_clean(void) { - log_info("/* %s */", __func__); - +TEST(env_clean) { _cleanup_strv_free_ char **e = strv_new("FOOBAR=WALDO", "FOOBAR=WALDO", "FOOBAR", @@ -332,9 +317,7 @@ static void test_env_clean(void) { assert_se(e[8] == NULL); } -static void test_env_name_is_valid(void) { - log_info("/* %s */", __func__); - +TEST(env_name_is_valid) { assert_se(env_name_is_valid("test")); assert_se(!env_name_is_valid(NULL)); @@ -346,9 +329,7 @@ static void test_env_name_is_valid(void) { assert_se(!env_name_is_valid("#¤%&?_only_numbers_letters_and_underscore_allowed")); } -static void test_env_value_is_valid(void) { - log_info("/* %s */", __func__); - +TEST(env_value_is_valid) { assert_se(env_value_is_valid("")); assert_se(env_value_is_valid("głąb kapuściany")); assert_se(env_value_is_valid("printf \"\\x1b]0;\\x07\"")); @@ -361,9 +342,7 @@ static void test_env_value_is_valid(void) { * We currently disallow that. */ } -static void test_env_assignment_is_valid(void) { - log_info("/* %s */", __func__); - +TEST(env_assignment_is_valid) { assert_se(env_assignment_is_valid("a=")); assert_se(env_assignment_is_valid("b=głąb kapuściany")); assert_se(env_assignment_is_valid("c=\\007\\009\\011")); @@ -383,9 +362,7 @@ static void test_env_assignment_is_valid(void) { assert_se(!env_assignment_is_valid("głąb=printf \"\x1b]0;\x07\"")); } -static void test_putenv_dup(void) { - log_info("/* %s */", __func__); - +TEST(putenv_dup) { assert_se(putenv_dup("A=a1", true) == 0); assert_se(streq_ptr(getenv("A"), "a1")); assert_se(putenv_dup("A=a1", true) == 0); @@ -396,13 +373,11 @@ static void test_putenv_dup(void) { assert_se(streq_ptr(getenv("A"), "a2")); } -static void test_setenv_systemd_exec_pid(void) { +TEST(setenv_systemd_exec_pid) { _cleanup_free_ char *saved = NULL; const char *e; pid_t p; - log_info("/* %s */", __func__); - e = getenv("SYSTEMD_EXEC_PID"); if (e) assert_se(saved = strdup(e)); @@ -431,11 +406,9 @@ static void test_setenv_systemd_exec_pid(void) { assert_se(set_unset_env("SYSTEMD_EXEC_PID", saved, 1) >= 0); } -static void test_unsetenv_erase(void) { +TEST(unsetenv_erase) { int r; - log_info("/* %s */", __func__); - r = safe_fork("(sd-unsetenverase)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL); if (r == 0) { _cleanup_strv_free_ char **l = NULL; @@ -477,30 +450,4 @@ static void test_unsetenv_erase(void) { assert_se(r > 0); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_DEBUG); - - test_strv_env_delete(); - test_strv_env_get(); - test_strv_env_pairs_get(); - test_strv_env_unset(); - test_strv_env_merge(); - test_strv_env_replace_strdup(); - test_strv_env_replace_strdup_passthrough(); - test_strv_env_assign(); - test_env_strv_get_n(); - test_replace_env(false); - test_replace_env(true); - test_replace_env2(false); - test_replace_env2(true); - test_replace_env_argv(); - test_env_clean(); - test_env_name_is_valid(); - test_env_value_is_valid(); - test_env_assignment_is_valid(); - test_putenv_dup(); - test_setenv_systemd_exec_pid(); - test_unsetenv_erase(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-escape.c b/src/test/test-escape.c index 75aa86bf69..214190e134 100644 --- a/src/test/test-escape.c +++ b/src/test/test-escape.c @@ -5,25 +5,21 @@ #include "macro.h" #include "tests.h" -static void test_cescape(void) { +TEST(cescape) { _cleanup_free_ char *t; - log_info("/* %s */", __func__); - assert_se(t = cescape("abc\\\"\b\f\n\r\t\v\a\003\177\234\313")); assert_se(streq(t, "abc\\\\\\\"\\b\\f\\n\\r\\t\\v\\a\\003\\177\\234\\313")); } -static void test_xescape(void) { +TEST(xescape) { _cleanup_free_ char *t; - log_info("/* %s */", __func__); - assert_se(t = xescape("abc\\\"\b\f\n\r\t\v\a\003\177\234\313", "")); assert_se(streq(t, "abc\\x5c\"\\x08\\x0c\\x0a\\x0d\\x09\\x0b\\x07\\x03\\x7f\\x9c\\xcb")); } -static void test_xescape_full(bool eight_bits) { +static void test_xescape_full_one(bool eight_bits) { const char* escaped = !eight_bits ? "a\\x62c\\x5c\"\\x08\\x0c\\x0a\\x0d\\x09\\x0b\\x07\\x03\\x7f\\x9c\\xcb" : "a\\x62c\\x5c\"\\x08\\x0c\\x0a\\x0d\\x09\\x0b\\x07\\x03\177\234\313"; @@ -63,10 +59,13 @@ static void test_xescape_full(bool eight_bits) { } } -static void test_cunescape(void) { - _cleanup_free_ char *unescaped; +TEST(test_xescape_full) { + test_xescape_full_one(false); + test_xescape_full_one(true); +} - log_info("/* %s */", __func__); +TEST(cunescape) { + _cleanup_free_ char *unescaped; assert_se(cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00", 0, &unescaped) < 0); assert_se(cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00", UNESCAPE_RELAX, &unescaped) >= 0); @@ -140,9 +139,7 @@ static void test_shell_escape_one(const char *s, const char *bad, const char *ex assert_se(streq_ptr(r, expected)); } -static void test_shell_escape(void) { - log_info("/* %s */", __func__); - +TEST(shell_escape) { test_shell_escape_one("", "", ""); test_shell_escape_one("\\", "", "\\\\"); test_shell_escape_one("foobar", "", "foobar"); @@ -159,9 +156,7 @@ static void test_shell_maybe_quote_one(const char *s, ShellEscapeFlags flags, co assert_se(streq(ret, expected)); } -static void test_shell_maybe_quote(void) { - log_info("/* %s */", __func__); - +TEST(shell_maybe_quote) { test_shell_maybe_quote_one("", 0, ""); test_shell_maybe_quote_one("", SHELL_ESCAPE_EMPTY, "\"\""); test_shell_maybe_quote_one("", SHELL_ESCAPE_POSIX, ""); @@ -211,9 +206,7 @@ static void test_quote_command_line_one(char **argv, const char *expected) { assert_se(streq(s, expected)); } -static void test_quote_command_line(void) { - log_info("/* %s */", __func__); - +TEST(quote_command_line) { test_quote_command_line_one(STRV_MAKE("true", "true"), "true true"); test_quote_command_line_one(STRV_MAKE("true", "with a space"), @@ -226,17 +219,4 @@ static void test_quote_command_line(void) { "true \"\\$dollar\""); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_DEBUG); - - test_cescape(); - test_xescape(); - test_xescape_full(false); - test_xescape_full(true); - test_cunescape(); - test_shell_escape(); - test_shell_maybe_quote(); - test_quote_command_line(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-ether-addr-util.c b/src/test/test-ether-addr-util.c index e708f019d7..d680f80a70 100644 --- a/src/test/test-ether-addr-util.c +++ b/src/test/test-ether-addr-util.c @@ -4,11 +4,9 @@ #include "string-util.h" #include "tests.h" -static void test_ether_addr_helpers(void) { +TEST(ether_addr_helpers) { struct ether_addr a; - log_info("/* %s */", __func__); - a = ETHER_ADDR_NULL; assert_se(ether_addr_is_null(&a)); assert_se(!ether_addr_is_broadcast(&a)); @@ -36,9 +34,7 @@ static void test_ether_addr_helpers(void) { #define INFINIBAD_ADDR_1 ((const struct hw_addr_data){ .length = 20, .infiniband = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20} }) -static void test_HW_ADDR_TO_STRING(void) { - log_info("/* %s */", __func__); - +TEST(HW_ADDR_TO_STRING) { const char *s = HW_ADDR_TO_STR(&(const struct hw_addr_data){6}); log_info("null: %s", s); @@ -80,9 +76,7 @@ static void test_parse_hw_addr_full_one(const char *in, size_t expected_len, con } } -static void test_parse_hw_addr(void) { - log_info("/* %s */", __func__); - +TEST(parse_hw_addr) { /* IPv4 */ test_parse_hw_addr_full_one("10.0.0.1", 0, "0a:00:00:01"); test_parse_hw_addr_full_one("10.0.0.1", 4, "0a:00:00:01"); @@ -165,11 +159,4 @@ static void test_parse_hw_addr(void) { test_parse_hw_addr_full_one("aa bb", SIZE_MAX, NULL); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_INFO); - - test_ether_addr_helpers(); - test_HW_ADDR_TO_STRING(); - test_parse_hw_addr(); - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-exec-util.c b/src/test/test-exec-util.c index ae740218b2..94ef6e8bbf 100644 --- a/src/test/test-exec-util.c +++ b/src/test/test-exec-util.c @@ -53,7 +53,7 @@ static const gather_stdout_callback_t ignore_stdout[] = { ignore_stdout_func3, }; -static void test_execute_directory(bool gather_stdout) { +static void test_execute_directory_one(bool gather_stdout) { char template_lo[] = "/tmp/test-exec-util.lo.XXXXXXX"; char template_hi[] = "/tmp/test-exec-util.hi.XXXXXXX"; const char * dirs[] = {template_hi, template_lo, NULL}; @@ -135,7 +135,12 @@ static void test_execute_directory(bool gather_stdout) { (void) rm_rf(template_hi, REMOVE_ROOT|REMOVE_PHYSICAL); } -static void test_execution_order(void) { +TEST(execute_directory) { + test_execute_directory_one(true); + test_execute_directory_one(false); +} + +TEST(execution_order) { char template_lo[] = "/tmp/test-exec-util-lo.XXXXXXX"; char template_hi[] = "/tmp/test-exec-util-hi.XXXXXXX"; const char *dirs[] = {template_hi, template_lo, NULL}; @@ -237,7 +242,7 @@ const gather_stdout_callback_t gather_stdout[] = { gather_stdout_three, }; -static void test_stdout_gathering(void) { +TEST(stdout_gathering) { char template[] = "/tmp/test-exec-util.XXXXXXX"; const char *dirs[] = {template, NULL}; const char *name, *name2, *name3; @@ -250,8 +255,6 @@ static void test_stdout_gathering(void) { assert_se(mkdtemp(template)); - log_info("/* %s */", __func__); - /* write files */ name = strjoina(template, "/10-foo"); name2 = strjoina(template, "/20-bar"); @@ -282,7 +285,7 @@ static void test_stdout_gathering(void) { assert_se(streq(output, "a\nb\nc\nd\n")); } -static void test_environment_gathering(void) { +TEST(environment_gathering) { char template[] = "/tmp/test-exec-util.XXXXXXX", **p; const char *dirs[] = {template, NULL}; const char *name, *name2, *name3, *old; @@ -295,8 +298,6 @@ static void test_environment_gathering(void) { assert_se(mkdtemp(template)); - log_info("/* %s */", __func__); - /* write files */ name = strjoina(template, "/10-foo"); name2 = strjoina(template, "/20-bar"); @@ -375,7 +376,7 @@ static void test_environment_gathering(void) { assert_se(set_unset_env("PATH", old, true) == 0); } -static void test_error_catching(void) { +TEST(error_catching) { char template[] = "/tmp/test-exec-util.XXXXXXX"; const char *dirs[] = {template, NULL}; const char *name, *name2, *name3; @@ -383,8 +384,6 @@ static void test_error_catching(void) { assert_se(mkdtemp(template)); - log_info("/* %s */", __func__); - /* write files */ name = strjoina(template, "/10-foo"); name2 = strjoina(template, "/20-bar"); @@ -413,7 +412,7 @@ static void test_error_catching(void) { assert_se(r == 42); } -static void test_exec_command_flags_from_strv(void) { +TEST(exec_command_flags_from_strv) { ExecCommandFlags flags = 0; char **valid_strv = STRV_MAKE("no-env-expand", "no-setuid", "ignore-failure"); char **invalid_strv = STRV_MAKE("no-env-expand", "no-setuid", "nonexistent-option", "ignore-failure"); @@ -433,7 +432,7 @@ static void test_exec_command_flags_from_strv(void) { assert_se(r == -EINVAL); } -static void test_exec_command_flags_to_strv(void) { +TEST(exec_command_flags_to_strv) { _cleanup_strv_free_ char **opts = NULL, **empty_opts = NULL, **invalid_opts = NULL; ExecCommandFlags flags = 0; int r; @@ -457,17 +456,4 @@ static void test_exec_command_flags_to_strv(void) { assert_se(r == -EINVAL); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_DEBUG); - - test_execute_directory(true); - test_execute_directory(false); - test_execution_order(); - test_stdout_gathering(); - test_environment_gathering(); - test_error_catching(); - test_exec_command_flags_from_strv(); - test_exec_command_flags_to_strv(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-exit-status.c b/src/test/test-exit-status.c index 4dc197301d..86d39767d4 100644 --- a/src/test/test-exit-status.c +++ b/src/test/test-exit-status.c @@ -4,9 +4,7 @@ #include "string-util.h" #include "tests.h" -static void test_exit_status_to_string(void) { - log_info("/* %s */", __func__); - +TEST(exit_status_to_string) { for (int i = -1; i <= 256; i++) { const char *s, *class; @@ -21,9 +19,7 @@ static void test_exit_status_to_string(void) { } } -static void test_exit_status_from_string(void) { - log_info("/* %s */", __func__); - +TEST(exit_status_from_string) { assert_se(exit_status_from_string("11") == 11); assert_se(exit_status_from_string("-1") == -ERANGE); assert_se(exit_status_from_string("256") == -ERANGE); @@ -32,21 +28,11 @@ static void test_exit_status_from_string(void) { assert_se(exit_status_from_string("FAILURE") == 1); } -static void test_exit_status_NUMA_POLICY(void) { - log_info("/* %s */", __func__); - +TEST(exit_status_NUMA_POLICY) { assert_se(streq(exit_status_to_string(EXIT_NUMA_POLICY, EXIT_STATUS_FULL), "NUMA_POLICY")); assert_se(streq(exit_status_to_string(EXIT_NUMA_POLICY, EXIT_STATUS_SYSTEMD), "NUMA_POLICY")); assert_se(!exit_status_to_string(EXIT_NUMA_POLICY, EXIT_STATUS_BSD)); assert_se(!exit_status_to_string(EXIT_NUMA_POLICY, EXIT_STATUS_LSB)); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_DEBUG); - - test_exit_status_to_string(); - test_exit_status_from_string(); - test_exit_status_NUMA_POLICY(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-extract-word.c b/src/test/test-extract-word.c index 8cf0b63a4c..6e12fbe2c6 100644 --- a/src/test/test-extract-word.c +++ b/src/test/test-extract-word.c @@ -6,13 +6,12 @@ #include "extract-word.h" #include "log.h" #include "string-util.h" +#include "tests.h" -static void test_extract_first_word(void) { +TEST(extract_first_word) { const char *p, *original; char *t; - log_info("/* %s */", __func__); - p = original = "foobar waldo"; assert_se(extract_first_word(&p, &t, NULL, 0) > 0); assert_se(streq(t, "foobar")); @@ -536,12 +535,10 @@ static void test_extract_first_word(void) { free(t); } -static void test_extract_first_word_and_warn(void) { +TEST(extract_first_word_and_warn) { const char *p, *original; char *t; - log_info("/* %s */", __func__); - p = original = "foobar waldo"; assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) > 0); assert_se(streq(t, "foobar")); @@ -682,12 +679,10 @@ static void test_extract_first_word_and_warn(void) { assert_se(isempty(p)); } -static void test_extract_many_words(void) { +TEST(extract_many_words) { const char *p, *original; char *a, *b, *c, *d, *e, *f; - log_info("/* %s */", __func__); - p = original = "foobar waldi piep"; assert_se(extract_many_words(&p, NULL, 0, &a, &b, &c, NULL) == 3); assert_se(isempty(p)); @@ -765,13 +760,4 @@ static void test_extract_many_words(void) { free(a); } -int main(int argc, char *argv[]) { - log_parse_environment(); - log_open(); - - test_extract_first_word(); - test_extract_first_word_and_warn(); - test_extract_many_words(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-fd-util.c b/src/test/test-fd-util.c index 8e8dd023af..167fecab25 100644 --- a/src/test/test-fd-util.c +++ b/src/test/test-fd-util.c @@ -22,7 +22,7 @@ #include "tests.h" #include "tmpfile-util.h" -static void test_close_many(void) { +TEST(close_many) { int fds[3]; char name0[] = "/tmp/test-close-many.XXXXXX"; char name1[] = "/tmp/test-close-many.XXXXXX"; @@ -45,7 +45,7 @@ static void test_close_many(void) { unlink(name2); } -static void test_close_nointr(void) { +TEST(close_nointr) { char name[] = "/tmp/test-test-close_nointr.XXXXXX"; int fd; @@ -57,7 +57,7 @@ static void test_close_nointr(void) { unlink(name); } -static void test_same_fd(void) { +TEST(same_fd) { _cleanup_close_pair_ int p[2] = { -1, -1 }; _cleanup_close_ int a = -1, b = -1, c = -1; @@ -91,7 +91,7 @@ static void test_same_fd(void) { assert_se(same_fd(b, a) == 0); } -static void test_open_serialization_fd(void) { +TEST(open_serialization_fd) { _cleanup_close_ int fd = -1; fd = open_serialization_fd("test"); @@ -100,7 +100,7 @@ static void test_open_serialization_fd(void) { assert_se(write(fd, "test\n", 5) == 5); } -static void test_fd_move_above_stdio(void) { +TEST(fd_move_above_stdio) { int original_stdin, new_fd; original_stdin = fcntl(0, F_DUPFD, 3); @@ -118,7 +118,7 @@ static void test_fd_move_above_stdio(void) { assert_se(close_nointr(new_fd) != EBADF); } -static void test_rearrange_stdio(void) { +TEST(rearrange_stdio) { pid_t pid; int r; @@ -184,7 +184,7 @@ static void test_rearrange_stdio(void) { } } -static void test_read_nr_open(void) { +TEST(read_nr_open) { log_info("nr-open: %i", read_nr_open()); } @@ -318,7 +318,7 @@ static int seccomp_prohibit_close_range(void) { #endif } -static void test_close_all_fds(void) { +TEST(close_all_fds) { int r; /* Runs the test four times. Once as is. Once with close_range() syscall blocked via seccomp, once @@ -384,7 +384,7 @@ static void test_close_all_fds(void) { assert_se(r >= 0); } -static void test_format_proc_fd_path(void) { +TEST(format_proc_fd_path) { assert_se(streq_ptr(FORMAT_PROC_FD_PATH(0), "/proc/self/fd/0")); assert_se(streq_ptr(FORMAT_PROC_FD_PATH(1), "/proc/self/fd/1")); assert_se(streq_ptr(FORMAT_PROC_FD_PATH(2), "/proc/self/fd/2")); @@ -392,7 +392,7 @@ static void test_format_proc_fd_path(void) { assert_se(streq_ptr(FORMAT_PROC_FD_PATH(2147483647), "/proc/self/fd/2147483647")); } -static void test_fd_reopen(void) { +TEST(fd_reopen) { _cleanup_close_ int fd1 = -1, fd2 = -1; struct stat st1, st2; int fl; @@ -487,12 +487,10 @@ static void test_fd_reopen(void) { fd1 = -1; } -static void test_take_fd(void) { +TEST(take_fd) { _cleanup_close_ int fd1 = -1, fd2 = -1; int array[2] = { -1, -1 }, i = 0; - log_info("/* %s */", __func__); - assert_se(fd1 == -1); assert_se(fd2 == -1); @@ -528,21 +526,4 @@ static void test_take_fd(void) { assert_se(array[1] == -1); } -int main(int argc, char *argv[]) { - - test_setup_logging(LOG_DEBUG); - - test_close_many(); - test_close_nointr(); - test_same_fd(); - test_open_serialization_fd(); - test_fd_move_above_stdio(); - test_rearrange_stdio(); - test_read_nr_open(); - test_close_all_fds(); - test_format_proc_fd_path(); - test_fd_reopen(); - test_take_fd(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-fdset.c b/src/test/test-fdset.c index c77d7ffe83..5d63eeee37 100644 --- a/src/test/test-fdset.c +++ b/src/test/test-fdset.c @@ -6,10 +6,11 @@ #include "fd-util.h" #include "fdset.h" #include "macro.h" +#include "tests.h" #include "tmpfile-util.h" #include "util.h" -static void test_fdset_new_fill(void) { +TEST(fdset_new_fill) { int fd = -1; _cleanup_fdset_free_ FDSet *fdset = NULL; char name[] = "/tmp/test-fdset_new_fill.XXXXXX"; @@ -22,7 +23,7 @@ static void test_fdset_new_fill(void) { unlink(name); } -static void test_fdset_put_dup(void) { +TEST(fdset_put_dup) { _cleanup_close_ int fd = -1; int copyfd = -1; _cleanup_fdset_free_ FDSet *fdset = NULL; @@ -41,7 +42,7 @@ static void test_fdset_put_dup(void) { unlink(name); } -static void test_fdset_cloexec(void) { +TEST(fdset_cloexec) { int fd = -1; _cleanup_fdset_free_ FDSet *fdset = NULL; int flags = -1; @@ -67,7 +68,7 @@ static void test_fdset_cloexec(void) { unlink(name); } -static void test_fdset_close_others(void) { +TEST(fdset_close_others) { int fd = -1; int copyfd = -1; _cleanup_fdset_free_ FDSet *fdset = NULL; @@ -91,7 +92,7 @@ static void test_fdset_close_others(void) { unlink(name); } -static void test_fdset_remove(void) { +TEST(fdset_remove) { _cleanup_close_ int fd = -1; FDSet *fdset = NULL; char name[] = "/tmp/test-fdset_remove.XXXXXX"; @@ -111,7 +112,7 @@ static void test_fdset_remove(void) { unlink(name); } -static void test_fdset_iterate(void) { +TEST(fdset_iterate) { int fd = -1; FDSet *fdset = NULL; char name[] = "/tmp/test-fdset_iterate.XXXXXX"; @@ -138,7 +139,7 @@ static void test_fdset_iterate(void) { unlink(name); } -static void test_fdset_isempty(void) { +TEST(fdset_isempty) { int fd; _cleanup_fdset_free_ FDSet *fdset = NULL; char name[] = "/tmp/test-fdset_isempty.XXXXXX"; @@ -156,7 +157,7 @@ static void test_fdset_isempty(void) { unlink(name); } -static void test_fdset_steal_first(void) { +TEST(fdset_steal_first) { int fd; _cleanup_fdset_free_ FDSet *fdset = NULL; char name[] = "/tmp/test-fdset_steal_first.XXXXXX"; @@ -176,7 +177,7 @@ static void test_fdset_steal_first(void) { unlink(name); } -static void test_fdset_new_array(void) { +TEST(fdset_new_array) { int fds[] = {10, 11, 12, 13}; _cleanup_fdset_free_ FDSet *fdset = NULL; @@ -188,16 +189,4 @@ static void test_fdset_new_array(void) { assert_se(fdset_contains(fdset, 13)); } -int main(int argc, char *argv[]) { - test_fdset_new_fill(); - test_fdset_put_dup(); - test_fdset_cloexec(); - test_fdset_close_others(); - test_fdset_remove(); - test_fdset_iterate(); - test_fdset_isempty(); - test_fdset_steal_first(); - test_fdset_new_array(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-fileio.c b/src/test/test-fileio.c index 5c266c0c9a..087d76f760 100644 --- a/src/test/test-fileio.c +++ b/src/test/test-fileio.c @@ -26,7 +26,7 @@ #include "tmpfile-util.h" #include "util.h" -static void test_parse_env_file(void) { +TEST(parse_env_file) { _cleanup_(unlink_tempfilep) char t[] = "/tmp/test-fileio-in-XXXXXX", p[] = "/tmp/test-fileio-out-XXXXXX"; @@ -166,7 +166,7 @@ static void test_one_shell_var(const char *file, const char *variable, const cha assert_se(streq(from_shell, value)); } -static void test_parse_multiline_env_file(void) { +TEST(parse_multiline_env_file) { _cleanup_(unlink_tempfilep) char t[] = "/tmp/test-fileio-in-XXXXXX", p[] = "/tmp/test-fileio-out-XXXXXX"; @@ -218,7 +218,7 @@ static void test_parse_multiline_env_file(void) { assert_se(r >= 0); } -static void test_merge_env_file(void) { +TEST(merge_env_file) { _cleanup_(unlink_tempfilep) char t[] = "/tmp/test-fileio-XXXXXX"; _cleanup_fclose_ FILE *f = NULL; _cleanup_strv_free_ char **a = NULL; @@ -282,7 +282,7 @@ static void test_merge_env_file(void) { assert_se(a[10] == NULL); } -static void test_merge_env_file_invalid(void) { +TEST(merge_env_file_invalid) { _cleanup_(unlink_tempfilep) char t[] = "/tmp/test-fileio-XXXXXX"; _cleanup_fclose_ FILE *f = NULL; _cleanup_strv_free_ char **a = NULL; @@ -316,14 +316,12 @@ static void test_merge_env_file_invalid(void) { assert_se(strv_isempty(a)); } -static void test_executable_is_script(void) { +TEST(executable_is_script) { _cleanup_(unlink_tempfilep) char t[] = "/tmp/test-fileio-XXXXXX"; _cleanup_fclose_ FILE *f = NULL; char *command; int r; - log_info("/* %s */", __func__); - assert_se(fmkostemp_safe(t, "w", &f) == 0); fputs("#! /bin/script -a -b \ngoo goo", f); fflush(f); @@ -344,13 +342,11 @@ static void test_executable_is_script(void) { } } -static void test_status_field(void) { +TEST(status_field) { _cleanup_free_ char *t = NULL, *p = NULL, *s = NULL, *z = NULL; unsigned long long total = 0, buffers = 0; int r; - log_info("/* %s */", __func__); - assert_se(get_proc_field("/proc/self/status", "Threads", WHITESPACE, &t) == 0); puts(t); assert_se(streq(t, "1")); @@ -381,9 +377,7 @@ static void test_status_field(void) { } } -static void test_capeff(void) { - log_info("/* %s */", __func__); - +TEST(capeff) { for (int pid = 0; pid < 2; pid++) { _cleanup_free_ char *capeff = NULL; int r, p; @@ -401,14 +395,12 @@ static void test_capeff(void) { } } -static void test_write_string_stream(void) { +TEST(write_string_stream) { _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-write_string_stream-XXXXXX"; _cleanup_fclose_ FILE *f = NULL; int fd; char buf[64]; - log_info("/* %s */", __func__); - fd = mkostemp_safe(fn); assert_se(fd >= 0); @@ -438,13 +430,11 @@ static void test_write_string_stream(void) { assert_se(streq(buf, "boohoo")); } -static void test_write_string_file(void) { +TEST(write_string_file) { _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-write_string_file-XXXXXX"; char buf[64] = {}; _cleanup_close_ int fd; - log_info("/* %s */", __func__); - fd = mkostemp_safe(fn); assert_se(fd >= 0); @@ -454,13 +444,11 @@ static void test_write_string_file(void) { assert_se(streq(buf, "boohoo\n")); } -static void test_write_string_file_no_create(void) { +TEST(write_string_file_no_create) { _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-write_string_file_no_create-XXXXXX"; _cleanup_close_ int fd; char buf[64] = {}; - log_info("/* %s */", __func__); - fd = mkostemp_safe(fn); assert_se(fd >= 0); @@ -471,12 +459,10 @@ static void test_write_string_file_no_create(void) { assert_se(streq(buf, "boohoo\n")); } -static void test_write_string_file_verify(void) { +TEST(write_string_file_verify) { _cleanup_free_ char *buf = NULL, *buf2 = NULL; int r; - log_info("/* %s */", __func__); - r = read_one_line_file("/proc/version", &buf); if (ERRNO_IS_PRIVILEGE(r)) return; @@ -496,15 +482,13 @@ static void test_write_string_file_verify(void) { assert_se(write_string_file("/proc/version", buf2, WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_AVOID_NEWLINE) == 0); } -static void test_load_env_file_pairs(void) { +TEST(load_env_file_pairs) { _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-load_env_file_pairs-XXXXXX"; int fd, r; _cleanup_fclose_ FILE *f = NULL; _cleanup_strv_free_ char **l = NULL; char **k, **v; - log_info("/* %s */", __func__); - fd = mkostemp_safe(fn); assert_se(fd >= 0); @@ -539,7 +523,7 @@ static void test_load_env_file_pairs(void) { } } -static void test_search_and_fopen(void) { +TEST(search_and_fopen) { static const char* const dirs[] = { "/tmp/foo/bar", "/tmp", @@ -552,8 +536,6 @@ static void test_search_and_fopen(void) { const char *e; int r; - log_info("/* %s */", __func__); - fd = mkostemp_safe(name); assert_se(fd >= 0); fd = safe_close(fd); @@ -590,13 +572,11 @@ static void test_search_and_fopen(void) { assert_se(r == -ENOENT); } -static void test_search_and_fopen_nulstr(void) { +TEST(search_and_fopen_nulstr) { static const char dirs[] = "/tmp/foo/bar\0" "/tmp\0"; - log_info("/* %s */", __func__); - _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-search_and_fopen.XXXXXX"; _cleanup_fclose_ FILE *f = NULL; _cleanup_free_ char *p = NULL; @@ -633,15 +613,13 @@ static void test_search_and_fopen_nulstr(void) { assert_se(r == -ENOENT); } -static void test_writing_tmpfile(void) { +TEST(writing_tmpfile) { _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-systemd_writing_tmpfile.XXXXXX"; _cleanup_free_ char *contents = NULL; size_t size; _cleanup_close_ int fd = -1; int r; - log_info("/* %s */", __func__); - struct iovec iov[] = { IOVEC_MAKE_STRING("abc\n"), IOVEC_MAKE_STRING(ALPHANUMERICAL "\n"), @@ -660,11 +638,9 @@ static void test_writing_tmpfile(void) { assert_se(streq(contents, "abc\n" ALPHANUMERICAL "\n")); } -static void test_tempfn(void) { +TEST(tempfn) { char *ret = NULL, *p; - log_info("/* %s */", __func__); - assert_se(tempfn_xxxxxx("/foo/bar/waldo", NULL, &ret) >= 0); assert_se(streq_ptr(ret, "/foo/bar/.#waldoXXXXXX")); free(ret); @@ -703,7 +679,7 @@ static const char chars[] = DISABLE_WARNING_TYPE_LIMITS; -static void test_fgetc(void) { +TEST(fgetc) { _cleanup_fclose_ FILE *f = NULL; char c; @@ -797,22 +773,18 @@ static void test_read_line_one_file(FILE *f) { assert_se(read_line(f, 1024, &line) == 0 && streq(line, "")); } -static void test_read_line(void) { +TEST(read_line1) { _cleanup_fclose_ FILE *f = NULL; - log_info("/* %s */", __func__); - assert_se(f = fmemopen_unlocked((void*) buffer, sizeof(buffer), "re")); test_read_line_one_file(f); } -static void test_read_line2(void) { +TEST(read_line2) { _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fileio.XXXXXX"; int fd; _cleanup_fclose_ FILE *f = NULL; - log_info("/* %s */", __func__); - fd = mkostemp_safe(name); assert_se(fd >= 0); assert_se((size_t) write(fd, buffer, sizeof(buffer)) == sizeof(buffer)); @@ -823,13 +795,11 @@ static void test_read_line2(void) { test_read_line_one_file(f); } -static void test_read_line3(void) { +TEST(read_line3) { _cleanup_fclose_ FILE *f = NULL; _cleanup_free_ char *line = NULL; int r; - log_info("/* %s */", __func__); - f = fopen("/proc/uptime", "re"); if (!f && IN_SET(errno, ENOENT, EPERM)) return; @@ -844,7 +814,7 @@ static void test_read_line3(void) { assert_se(read_line(f, LINE_MAX, NULL) == 0); } -static void test_read_line4(void) { +TEST(read_line4) { static const struct { size_t length; const char *string; @@ -878,7 +848,7 @@ static void test_read_line4(void) { } } -static void test_read_nul_string(void) { +TEST(read_nul_string) { static const char test[] = "string nr. 1\0" "string nr. 2\n\0" "\377empty string follows\0" @@ -889,8 +859,6 @@ static void test_read_nul_string(void) { _cleanup_fclose_ FILE *f = NULL; _cleanup_free_ char *s = NULL; - log_info("/* %s */", __func__); - assert_se(f = fmemopen_unlocked((void*) test, sizeof(test)-1, "r")); assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 13 && streq_ptr(s, "string nr. 1")); @@ -914,7 +882,7 @@ static void test_read_nul_string(void) { assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 0 && streq_ptr(s, "")); } -static void test_read_full_file_socket(void) { +TEST(read_full_file_socket) { _cleanup_(rm_rf_physical_and_freep) char *z = NULL; _cleanup_close_ int listener = -1; _cleanup_free_ char *data = NULL, *clientname = NULL; @@ -924,8 +892,6 @@ static void test_read_full_file_socket(void) { pid_t pid; int r; - log_info("/* %s */", __func__); - listener = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0); assert_se(listener >= 0); @@ -973,15 +939,13 @@ static void test_read_full_file_socket(void) { #undef TEST_STR } -static void test_read_full_file_offset_size(void) { +TEST(read_full_file_offset_size) { _cleanup_fclose_ FILE *f = NULL; _cleanup_(unlink_and_freep) char *fn = NULL; _cleanup_free_ char *rbuf = NULL; size_t rbuf_size; uint8_t buf[4711]; - log_info("/* %s */", __func__); - random_bytes(buf, sizeof(buf)); assert_se(tempfn_random_child(NULL, NULL, &fn) >= 0); @@ -1038,7 +1002,7 @@ static void test_read_full_file_offset_size(void) { rbuf = mfree(rbuf); } -static void test_read_virtual_file(size_t max_size) { +static void test_read_virtual_file_one(size_t max_size) { const char *filename; int r; @@ -1069,40 +1033,14 @@ static void test_read_virtual_file(size_t max_size) { } } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_DEBUG); - - test_parse_env_file(); - test_parse_multiline_env_file(); - test_merge_env_file(); - test_merge_env_file_invalid(); - test_executable_is_script(); - test_status_field(); - test_capeff(); - test_write_string_stream(); - test_write_string_file(); - test_write_string_file_no_create(); - test_write_string_file_verify(); - test_load_env_file_pairs(); - test_search_and_fopen(); - test_search_and_fopen_nulstr(); - test_writing_tmpfile(); - test_tempfn(); - test_fgetc(); - test_read_line(); - test_read_line2(); - test_read_line3(); - test_read_line4(); - test_read_nul_string(); - test_read_full_file_socket(); - test_read_full_file_offset_size(); - test_read_virtual_file(0); - test_read_virtual_file(1); - test_read_virtual_file(2); - test_read_virtual_file(20); - test_read_virtual_file(4096); - test_read_virtual_file(4097); - test_read_virtual_file(SIZE_MAX); - - return 0; +TEST(test_read_virtual_file) { + test_read_virtual_file_one(0); + test_read_virtual_file_one(1); + test_read_virtual_file_one(2); + test_read_virtual_file_one(20); + test_read_virtual_file_one(4096); + test_read_virtual_file_one(4097); + test_read_virtual_file_one(SIZE_MAX); } + +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-format-util.c b/src/test/test-format-util.c index 306e0788d0..3e640a84b3 100644 --- a/src/test/test-format-util.c +++ b/src/test/test-format-util.c @@ -3,6 +3,7 @@ #include "format-util.h" #include "macro.h" #include "string-util.h" +#include "tests.h" /* Do some basic checks on STRLEN() and DECIMAL_STR_MAX() */ assert_cc(STRLEN("xxx") == 3); @@ -25,7 +26,7 @@ static void test_format_bytes_one(uint64_t val, bool trailing_B, const char *iec assert_se(streq_ptr(format_bytes_full(buf, sizeof buf, val, trailing_B ? FORMAT_BYTES_TRAILING_B : 0), si_without_p)); } -static void test_format_bytes(void) { +TEST(format_bytes) { test_format_bytes_one(900, true, "900B", "900B", "900B", "900B"); test_format_bytes_one(900, false, "900", "900", "900", "900"); test_format_bytes_one(1023, true, "1023B", "1023B", "1.0K", "1K"); @@ -43,8 +44,4 @@ static void test_format_bytes(void) { test_format_bytes_one(UINT64_MAX, false, NULL, NULL, NULL, NULL); } -int main(void) { - test_format_bytes(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-fs-util.c b/src/test/test-fs-util.c index 0a36d676af..0e0d91d04e 100644 --- a/src/test/test-fs-util.c +++ b/src/test/test-fs-util.c @@ -27,15 +27,13 @@ static const char *arg_test_dir = NULL; -static void test_chase_symlinks(void) { +TEST(chase_symlinks) { _cleanup_free_ char *result = NULL; char *temp; const char *top, *p, *pslash, *q, *qslash; struct stat st; int r, pfd; - log_info("/* %s */", __func__); - temp = strjoina(arg_test_dir ?: "/tmp", "/test-chase.XXXXXX"); assert_se(mkdtemp(temp)); @@ -390,12 +388,10 @@ static void test_chase_symlinks(void) { assert_se(rm_rf(temp, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0); } -static void test_unlink_noerrno(void) { +TEST(unlink_noerrno) { char *name; int fd; - log_info("/* %s */", __func__); - name = strjoina(arg_test_dir ?: "/tmp", "/test-close_nointr.XXXXXX"); fd = mkostemp_safe(name); assert_se(fd >= 0); @@ -411,12 +407,10 @@ static void test_unlink_noerrno(void) { } } -static void test_readlink_and_make_absolute(void) { +TEST(readlink_and_make_absolute) { const char *tempdir, *name, *name2, *name_alias; _cleanup_free_ char *r1 = NULL, *r2 = NULL, *pwd = NULL; - log_info("/* %s */", __func__); - tempdir = strjoina(arg_test_dir ?: "/tmp", "/test-readlink_and_make_absolute"); name = strjoina(tempdir, "/original"); name2 = "test-readlink_and_make_absolute/original"; @@ -447,7 +441,7 @@ static void test_readlink_and_make_absolute(void) { assert_se(rm_rf(tempdir, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0); } -static void test_get_files_in_directory(void) { +TEST(get_files_in_directory) { _cleanup_strv_free_ char **l = NULL, **t = NULL; assert_se(get_files_in_directory(arg_test_dir ?: "/tmp", &l) >= 0); @@ -455,12 +449,10 @@ static void test_get_files_in_directory(void) { assert_se(get_files_in_directory(".", NULL) >= 0); } -static void test_var_tmp(void) { +TEST(var_tmp) { _cleanup_free_ char *tmpdir_backup = NULL, *temp_backup = NULL, *tmp_backup = NULL; const char *tmp_dir = NULL, *t; - log_info("/* %s */", __func__); - t = getenv("TMPDIR"); if (t) { tmpdir_backup = strdup(t); @@ -514,9 +506,7 @@ static void test_var_tmp(void) { } } -static void test_dot_or_dot_dot(void) { - log_info("/* %s */", __func__); - +TEST(dot_or_dot_dot) { assert_se(!dot_or_dot_dot(NULL)); assert_se(!dot_or_dot_dot("")); assert_se(!dot_or_dot_dot("xxx")); @@ -526,13 +516,11 @@ static void test_dot_or_dot_dot(void) { assert_se(!dot_or_dot_dot("..foo")); } -static void test_access_fd(void) { +TEST(access_fd) { _cleanup_(rmdir_and_freep) char *p = NULL; _cleanup_close_ int fd = -1; const char *a; - log_info("/* %s */", __func__); - a = strjoina(arg_test_dir ?: "/tmp", "/access-fd.XXXXXX"); assert_se(mkdtemp_malloc(a, &p) >= 0); @@ -556,7 +544,7 @@ static void test_access_fd(void) { } } -static void test_touch_file(void) { +TEST(touch_file) { uid_t test_uid, test_gid; _cleanup_(rm_rf_physical_and_freep) char *p = NULL; struct stat st; @@ -564,8 +552,6 @@ static void test_touch_file(void) { usec_t test_mtime; int r; - log_info("/* %s */", __func__); - test_uid = geteuid() == 0 ? 65534 : getuid(); test_gid = geteuid() == 0 ? 65534 : getgid(); @@ -656,13 +642,11 @@ static void test_touch_file(void) { assert_se(timespec_load(&st.st_mtim) == test_mtime); } -static void test_unlinkat_deallocate(void) { +TEST(unlinkat_deallocate) { _cleanup_free_ char *p = NULL; _cleanup_close_ int fd = -1; struct stat st; - log_info("/* %s */", __func__); - assert_se(tempfn_random_child(arg_test_dir, "unlink-deallocation", &p) >= 0); fd = open(p, O_WRONLY|O_CLOEXEC|O_CREAT|O_EXCL, 0600); @@ -684,18 +668,16 @@ static void test_unlinkat_deallocate(void) { assert_se(st.st_nlink == 0); } -static void test_fsync_directory_of_file(void) { +TEST(fsync_directory_of_file) { _cleanup_close_ int fd = -1; - log_info("/* %s */", __func__); - fd = open_tmpfile_unlinkable(arg_test_dir, O_RDWR); assert_se(fd >= 0); assert_se(fsync_directory_of_file(fd) >= 0); } -static void test_rename_noreplace(void) { +TEST(rename_noreplace) { static const char* const table[] = { "/reg", "/dir", @@ -709,8 +691,6 @@ static void test_rename_noreplace(void) { const char *j = NULL; char **a, **b; - log_info("/* %s */", __func__); - if (arg_test_dir) j = strjoina(arg_test_dir, "/testXXXXXX"); assert_se(mkdtemp_malloc(j, &z) >= 0); @@ -763,7 +743,7 @@ static void test_rename_noreplace(void) { } } -static void test_chmod_and_chown(void) { +TEST(chmod_and_chown) { _cleanup_(rm_rf_physical_and_freep) char *d = NULL; struct stat st; const char *p; @@ -771,8 +751,6 @@ static void test_chmod_and_chown(void) { if (geteuid() != 0) return; - log_info("/* %s */", __func__); - BLOCK_WITH_UMASK(0000); assert_se(mkdtemp_malloc(NULL, &d) >= 0); @@ -816,7 +794,7 @@ static void create_binary_file(const char *p, const void *data, size_t l) { assert_se(write(fd, data, l) == (ssize_t) l); } -static void test_conservative_rename(void) { +TEST(conservative_rename) { _cleanup_(unlink_and_freep) char *p = NULL; _cleanup_free_ char *q = NULL; size_t l = 16*1024 + random_u64() % (32 * 1024); /* some randomly sized buffer 16k…48k */ @@ -899,11 +877,9 @@ static void test_rmdir_parents_one( } } -static void test_rmdir_parents(void) { +TEST(rmdir_parents) { char *temp; - log_info("/* %s */", __func__); - temp = strjoina(arg_test_dir ?: "/tmp", "/test-rmdir.XXXXXX"); assert_se(mkdtemp(temp)); @@ -931,9 +907,7 @@ static void test_parse_cifs_service_one(const char *f, const char *h, const char assert_se(streq_ptr(c, d)); } -static void test_parse_cifs_service(void) { - log_info("/* %s */", __func__); - +TEST(parse_cifs_service) { test_parse_cifs_service_one("//foo/bar/baz", "foo", "bar", "baz", 0); test_parse_cifs_service_one("\\\\foo\\bar\\baz", "foo", "bar", "baz", 0); test_parse_cifs_service_one("//foo/bar", "foo", "bar", NULL, 0); @@ -952,10 +926,9 @@ static void test_parse_cifs_service(void) { test_parse_cifs_service_one("//./a", NULL, NULL, NULL, -EINVAL); } -static void test_open_mkdir_at(void) { +TEST(open_mkdir_at) { _cleanup_close_ int fd = -1, subdir_fd = -1, subsubdir_fd = -1; _cleanup_(rm_rf_physical_and_freep) char *t = NULL; - log_info("/* %s */", __func__); assert_se(open_mkdir_at(AT_FDCWD, "/proc", O_EXCL|O_CLOEXEC, 0) == -EEXIST); @@ -995,27 +968,4 @@ static void test_open_mkdir_at(void) { assert_se(subsubdir_fd >= 0); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_INFO); - - arg_test_dir = argv[1]; - - test_chase_symlinks(); - test_unlink_noerrno(); - test_readlink_and_make_absolute(); - test_get_files_in_directory(); - test_var_tmp(); - test_dot_or_dot_dot(); - test_access_fd(); - test_touch_file(); - test_unlinkat_deallocate(); - test_fsync_directory_of_file(); - test_rename_noreplace(); - test_chmod_and_chown(); - test_conservative_rename(); - test_rmdir_parents(); - test_parse_cifs_service(); - test_open_mkdir_at(); - - return 0; -} +DEFINE_CUSTOM_TEST_MAIN(LOG_INFO, arg_test_dir = argv[1], /* no outro */); diff --git a/src/test/test-fstab-util.c b/src/test/test-fstab-util.c index d2f2018526..2ce4e18643 100644 --- a/src/test/test-fstab-util.c +++ b/src/test/test-fstab-util.c @@ -7,6 +7,7 @@ #include "log.h" #include "string-util.h" #include "strv.h" +#include "tests.h" /* int fstab_filter_options( @@ -62,7 +63,7 @@ static void do_fstab_filter_options(const char *opts, assert_se(streq_ptr(name, name_expected)); } -static void test_fstab_filter_options(void) { +TEST(fstab_filter_options) { do_fstab_filter_options("opt=0", "opt\0x-opt\0", 1, 1, "opt", "0", "0", ""); do_fstab_filter_options("opt=0", "x-opt\0opt\0", 1, 1, "opt", "0", "0", ""); do_fstab_filter_options("opt", "opt\0x-opt\0", 1, 0, "opt", NULL, "", ""); @@ -127,7 +128,7 @@ static void test_fstab_filter_options(void) { do_fstab_filter_options("opt1=\\\\,opt2=\\xff", "opt2\0", 1, 1, "opt2", "\\xff", "\\xff", "opt1=\\"); } -static void test_fstab_find_pri(void) { +TEST(fstab_find_pri) { int pri = -1; assert_se(fstab_find_pri("pri", &pri) == 0); @@ -146,7 +147,7 @@ static void test_fstab_find_pri(void) { assert_se(pri == 13); } -static void test_fstab_yes_no_option(void) { +TEST(fstab_yes_no_option) { assert_se(fstab_test_yes_no_option("nofail,fail,nofail", "nofail\0fail\0") == true); assert_se(fstab_test_yes_no_option("nofail,nofail,fail", "nofail\0fail\0") == false); assert_se(fstab_test_yes_no_option("abc,cde,afail", "nofail\0fail\0") == false); @@ -154,7 +155,7 @@ static void test_fstab_yes_no_option(void) { assert_se(fstab_test_yes_no_option("nofail,nofail=0,fail=0", "nofail\0fail\0") == false); } -static void test_fstab_node_to_udev_node(void) { +TEST(fstab_node_to_udev_node) { char *n; n = fstab_node_to_udev_node("LABEL=applé/jack"); @@ -188,11 +189,4 @@ static void test_fstab_node_to_udev_node(void) { free(n); } -int main(void) { - test_fstab_filter_options(); - test_fstab_find_pri(); - test_fstab_yes_no_option(); - test_fstab_node_to_udev_node(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-gcrypt-util.c b/src/test/test-gcrypt-util.c index 2af040b63a..8eb63cd385 100644 --- a/src/test/test-gcrypt-util.c +++ b/src/test/test-gcrypt-util.c @@ -4,8 +4,9 @@ #include "gcrypt-util.h" #include "macro.h" #include "string-util.h" +#include "tests.h" -static void test_string_hashsum(void) { +TEST(string_hashsum) { _cleanup_free_ char *out1 = NULL, *out2 = NULL, *out3 = NULL, *out4 = NULL; assert_se(string_hashsum("asdf", 4, GCRY_MD_SHA224, &out1) == 0); @@ -25,8 +26,4 @@ static void test_string_hashsum(void) { assert_se(streq(out4, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")); } -int main(int argc, char **argv) { - test_string_hashsum(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-glob-util.c b/src/test/test-glob-util.c index 50b4924679..ec8b74f48f 100644 --- a/src/test/test-glob-util.c +++ b/src/test/test-glob-util.c @@ -10,11 +10,10 @@ #include "glob-util.h" #include "macro.h" #include "rm-rf.h" +#include "tests.h" #include "tmpfile-util.h" -static void test_glob_exists(void) { - log_info("/* %s */", __func__); - +TEST(glob_exists) { char name[] = "/tmp/test-glob_exists.XXXXXX"; int fd = -1; int r; @@ -36,7 +35,7 @@ static void closedir_wrapper(void* v) { (void) closedir(v); } -static void test_glob_no_dot(void) { +TEST(glob_no_dot) { char template[] = "/tmp/test-glob-util.XXXXXXX"; const char *fn; @@ -50,8 +49,6 @@ static void test_glob_no_dot(void) { int r; - log_info("/* %s */", __func__); - assert_se(mkdtemp(template)); fn = strjoina(template, "/*"); @@ -65,15 +62,13 @@ static void test_glob_no_dot(void) { (void) rm_rf(template, REMOVE_ROOT|REMOVE_PHYSICAL); } -static void test_safe_glob(void) { +TEST(safe_glob) { char template[] = "/tmp/test-glob-util.XXXXXXX"; const char *fn, *fn2, *fname; _cleanup_globfree_ glob_t g = {}; int r; - log_info("/* %s */", __func__); - assert_se(mkdtemp(template)); fn = strjoina(template, "/*"); @@ -106,9 +101,7 @@ static void test_glob_non_glob_prefix_one(const char *path, const char *expected assert_se(streq(t, expected)); } -static void test_glob_non_glob(void) { - log_info("/* %s */", __func__); - +TEST(glob_non_glob) { test_glob_non_glob_prefix_one("/tmp/.X11-*", "/tmp/"); test_glob_non_glob_prefix_one("/tmp/*", "/tmp/"); test_glob_non_glob_prefix_one("/tmp*", "/"); @@ -120,11 +113,4 @@ static void test_glob_non_glob(void) { assert_se(glob_non_glob_prefix("?", &x) == -ENOENT); } -int main(void) { - test_glob_exists(); - test_glob_no_dot(); - test_safe_glob(); - test_glob_non_glob(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-gpt.c b/src/test/test-gpt.c index 7ee044ba50..30a93a83ec 100644 --- a/src/test/test-gpt.c +++ b/src/test/test-gpt.c @@ -10,12 +10,10 @@ #include "tests.h" #include "util.h" -static void test_gpt_types_against_architectures(void) { +TEST(gpt_types_against_architectures) { const char *prefix; int r; - log_info("/* %s */", __func__); - /* Dumps a table indicating for which architectures we know we have matching GPT partition * types. Also validates whether we can properly categorize the entries. */ @@ -51,11 +49,4 @@ static void test_gpt_types_against_architectures(void) { } } -int main(int argc, char *argv[]) { - - test_setup_logging(LOG_INFO); - - test_gpt_types_against_architectures(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-hash-funcs.c b/src/test/test-hash-funcs.c index db68e3a1e7..f5166c1fad 100644 --- a/src/test/test-hash-funcs.c +++ b/src/test/test-hash-funcs.c @@ -4,12 +4,10 @@ #include "hash-funcs.h" #include "set.h" -static void test_path_hash_set(void) { +TEST(path_hash_set) { /* The goal is to make sure that non-simplified path are hashed as expected, * and that we don't need to simplify them beforehand. */ - log_info("/* %s */", __func__); - /* No freeing of keys, we operate on static strings here… */ _cleanup_set_free_ Set *set = NULL; @@ -76,8 +74,4 @@ static void test_path_hash_set(void) { assert_se(!set_contains(set, "/////../bar/./")); } -int main(int argc, char **argv) { - test_setup_logging(LOG_INFO); - - test_path_hash_set(); -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-hexdecoct.c b/src/test/test-hexdecoct.c index b7188d84fc..cc9a7cb838 100644 --- a/src/test/test-hexdecoct.c +++ b/src/test/test-hexdecoct.c @@ -7,26 +7,27 @@ #include "macro.h" #include "random-util.h" #include "string-util.h" +#include "tests.h" -static void test_hexchar(void) { +TEST(hexchar) { assert_se(hexchar(0xa) == 'a'); assert_se(hexchar(0x0) == '0'); } -static void test_unhexchar(void) { +TEST(unhexchar) { assert_se(unhexchar('a') == 0xA); assert_se(unhexchar('A') == 0xA); assert_se(unhexchar('0') == 0x0); } -static void test_base32hexchar(void) { +TEST(base32hexchar) { assert_se(base32hexchar(0) == '0'); assert_se(base32hexchar(9) == '9'); assert_se(base32hexchar(10) == 'A'); assert_se(base32hexchar(31) == 'V'); } -static void test_unbase32hexchar(void) { +TEST(unbase32hexchar) { assert_se(unbase32hexchar('0') == 0); assert_se(unbase32hexchar('9') == 9); assert_se(unbase32hexchar('A') == 10); @@ -34,13 +35,13 @@ static void test_unbase32hexchar(void) { assert_se(unbase32hexchar('=') == -EINVAL); } -static void test_base64char(void) { +TEST(base64char) { assert_se(base64char(0) == 'A'); assert_se(base64char(26) == 'a'); assert_se(base64char(63) == '/'); } -static void test_unbase64char(void) { +TEST(unbase64char) { assert_se(unbase64char('A') == 0); assert_se(unbase64char('Z') == 25); assert_se(unbase64char('a') == 26); @@ -52,22 +53,22 @@ static void test_unbase64char(void) { assert_se(unbase64char('=') == -EINVAL); } -static void test_octchar(void) { +TEST(octchar) { assert_se(octchar(00) == '0'); assert_se(octchar(07) == '7'); } -static void test_unoctchar(void) { +TEST(unoctchar) { assert_se(unoctchar('0') == 00); assert_se(unoctchar('7') == 07); } -static void test_decchar(void) { +TEST(decchar) { assert_se(decchar(0) == '0'); assert_se(decchar(9) == '9'); } -static void test_undecchar(void) { +TEST(undecchar) { assert_se(undecchar('0') == 0); assert_se(undecchar('9') == 9); } @@ -90,7 +91,7 @@ static void test_unhexmem_one(const char *s, size_t l, int retval) { } } -static void test_unhexmem(void) { +TEST(unhexmem) { const char *hex = "efa2149213"; const char *hex_space = " e f a\n 2\r 14\n\r\t9\t2 \n1\r3 \r\r\t"; const char *hex_invalid = "efa214921o"; @@ -109,7 +110,7 @@ static void test_unhexmem(void) { } /* https://tools.ietf.org/html/rfc4648#section-10 */ -static void test_base32hexmem(void) { +TEST(base32hexmem) { char *b32; b32 = base32hexmem("", STRLEN(""), true); @@ -196,7 +197,7 @@ static void test_unbase32hexmem_one(const char *hex, bool padding, int retval, c } } -static void test_unbase32hexmem(void) { +TEST(unbase32hexmem) { test_unbase32hexmem_one("", true, 0, ""); test_unbase32hexmem_one("CO======", true, 0, "f"); @@ -244,7 +245,7 @@ static void test_unbase32hexmem(void) { } /* https://tools.ietf.org/html/rfc4648#section-10 */ -static void test_base64mem(void) { +TEST(base64mem) { char *b64; assert_se(base64mem("", STRLEN(""), &b64) == 0); @@ -276,7 +277,7 @@ static void test_base64mem(void) { free(b64); } -static void test_base64mem_linebreak(void) { +TEST(base64mem_linebreak) { uint8_t data[4096]; for (size_t i = 0; i < 20; i++) { @@ -320,7 +321,7 @@ static void test_unbase64mem_one(const char *input, const char *output, int ret) } } -static void test_unbase64mem(void) { +TEST(unbase64mem) { test_unbase64mem_one("", "", 0); test_unbase64mem_one("Zg==", "f", 0); @@ -346,7 +347,7 @@ static void test_unbase64mem(void) { test_unbase64mem_one(" Z m 8 = q u u x ", NULL, -ENAMETOOLONG); } -static void test_hexdump(void) { +TEST(hexdump) { uint8_t data[146]; unsigned i; @@ -365,24 +366,4 @@ static void test_hexdump(void) { hexdump(stdout, data, sizeof(data)); } -int main(int argc, char *argv[]) { - test_hexchar(); - test_unhexchar(); - test_base32hexchar(); - test_unbase32hexchar(); - test_base64char(); - test_unbase64char(); - test_octchar(); - test_unoctchar(); - test_decchar(); - test_undecchar(); - test_unhexmem(); - test_base32hexmem(); - test_unbase32hexmem(); - test_base64mem(); - test_base64mem_linebreak(); - test_unbase64mem(); - test_hexdump(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-hmac.c b/src/test/test-hmac.c index 5c73eca110..523f09495c 100644 --- a/src/test/test-hmac.c +++ b/src/test/test-hmac.c @@ -9,12 +9,10 @@ static void hmac_sha256_by_string(const char *key, const char *value, uint8_t re hmac_sha256(key, strlen(key), value, strlen(value), res); } -static void test_hmac(void) { +TEST(hmac) { uint8_t result[SHA256_DIGEST_SIZE]; char *hex_result = NULL; - log_info("/* %s */", __func__); - /* Results compared with output of 'echo -n "" | openssl dgst -sha256 -hmac ""' */ hmac_sha256_by_string("waldo", @@ -67,10 +65,4 @@ static void test_hmac(void) { hex_result = mfree(hex_result); } -int main(int argc, char **argv) { - test_setup_logging(LOG_INFO); - - test_hmac(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-hostname-setup.c b/src/test/test-hostname-setup.c index dcd61b295f..241b197f47 100644 --- a/src/test/test-hostname-setup.c +++ b/src/test/test-hostname-setup.c @@ -9,7 +9,7 @@ #include "tests.h" #include "tmpfile-util.h" -static void test_read_etc_hostname(void) { +TEST(read_etc_hostname) { char path[] = "/tmp/hostname.XXXXXX"; char *hostname; int fd; @@ -58,15 +58,8 @@ static void test_read_etc_hostname(void) { unlink(path); } -static void test_hostname_setup(void) { +TEST(hostname_setup) { hostname_setup(false); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_DEBUG); - - test_read_etc_hostname(); - test_hostname_setup(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-hostname-util.c b/src/test/test-hostname-util.c index 2ac6622624..77e9a1992f 100644 --- a/src/test/test-hostname-util.c +++ b/src/test/test-hostname-util.c @@ -9,9 +9,7 @@ #include "tests.h" #include "tmpfile-util.h" -static void test_hostname_is_valid(void) { - log_info("/* %s */", __func__); - +TEST(hostname_is_valid) { assert_se(hostname_is_valid("foobar", 0)); assert_se(hostname_is_valid("foobar.com", 0)); assert_se(!hostname_is_valid("foobar.com.", 0)); @@ -48,11 +46,9 @@ static void test_hostname_is_valid(void) { assert_se(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", VALID_HOSTNAME_TRAILING_DOT)); } -static void test_hostname_cleanup(void) { +TEST(hostname_cleanup) { char *s; - log_info("/* %s */", __func__); - s = strdupa_safe("foobar"); assert_se(streq(hostname_cleanup(s), "foobar")); s = strdupa_safe("foobar.com"); @@ -95,11 +91,9 @@ static void test_hostname_cleanup(void) { assert_se(streq(hostname_cleanup(s), "xxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")); } -static void test_hostname_malloc(void) { +TEST(hostname_malloc) { _cleanup_free_ char *h = NULL, *l = NULL; - log_info("/* %s */", __func__); - assert_se(h = gethostname_malloc()); log_info("hostname_malloc: \"%s\"", h); @@ -107,9 +101,7 @@ static void test_hostname_malloc(void) { log_info("hostname_short_malloc: \"%s\"", l); } -static void test_default_hostname(void) { - log_info("/* %s */", __func__); - +TEST(default_hostname) { if (!hostname_is_valid(FALLBACK_HOSTNAME, 0)) { log_error("Configured fallback hostname \"%s\" is not valid.", FALLBACK_HOSTNAME); exit(EXIT_FAILURE); @@ -121,13 +113,4 @@ static void test_default_hostname(void) { assert_se(hostname_is_valid(n, 0)); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_DEBUG); - - test_hostname_is_valid(); - test_hostname_cleanup(); - test_hostname_malloc(); - test_default_hostname(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-id128.c b/src/test/test-id128.c index 79dc7de159..33c4f1a5ea 100644 --- a/src/test/test-id128.c +++ b/src/test/test-id128.c @@ -19,14 +19,12 @@ #define STR_WALDI "0102030405060708090a0b0c0d0e0f10" #define UUID_WALDI "01020304-0506-0708-090a-0b0c0d0e0f10" -static void test_id128(void) { +TEST(id128) { sd_id128_t id, id2; char t[SD_ID128_STRING_MAX], q[ID128_UUID_STRING_MAX]; _cleanup_free_ char *b = NULL; _cleanup_close_ int fd = -1; - log_info("/* %s */", __func__); - assert_se(sd_id128_randomize(&id) == 0); printf("random: %s\n", sd_id128_to_string(id, t)); @@ -154,12 +152,10 @@ static void test_id128(void) { assert_se(!sd_id128_equal(id, id2)); } -static void test_sd_id128_get_invocation(void) { +TEST(sd_id128_get_invocation) { sd_id128_t id; int r; - log_info("/* %s */", __func__); - /* Query the invocation ID */ r = sd_id128_get_invocation(&id); if (r < 0) @@ -168,7 +164,7 @@ static void test_sd_id128_get_invocation(void) { log_info("Invocation ID: " SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(id)); } -static void benchmark_sd_id128_get_machine_app_specific(void) { +TEST(benchmark_sd_id128_get_machine_app_specific) { unsigned iterations = slow_tests_enabled() ? 1000000 : 1000; usec_t t, q; @@ -189,12 +185,4 @@ static void benchmark_sd_id128_get_machine_app_specific(void) { log_info("%lf µs each\n", (double) q / iterations); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_INFO); - - test_id128(); - test_sd_id128_get_invocation(); - benchmark_sd_id128_get_machine_app_specific(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-import-util.c b/src/test/test-import-util.c index 92ff0f5654..7930fe508d 100644 --- a/src/test/test-import-util.c +++ b/src/test/test-import-util.c @@ -13,7 +13,7 @@ static void test_import_url_last_component_one(const char *input, const char *ou assert_se(streq_ptr(output, s)); } -static void test_import_url_last_component(void) { +TEST(import_url_last_component) { test_import_url_last_component_one("https://foobar/waldo/quux", "quux", 0); test_import_url_last_component_one("https://foobar/waldo/quux/", "quux", 0); test_import_url_last_component_one("https://foobar/waldo/", "waldo", 0); @@ -40,7 +40,7 @@ static void test_import_url_change_suffix_one(const char *input, size_t n, const assert_se(streq_ptr(output, s)); } -static void test_import_url_change_suffix(void) { +TEST(import_url_change_suffix) { test_import_url_change_suffix_one("https://foobar/waldo/quux", 1, "wuff", "https://foobar/waldo/wuff", 0); test_import_url_change_suffix_one("https://foobar/waldo/quux/", 1, "wuff", "https://foobar/waldo/wuff", 0); test_import_url_change_suffix_one("https://foobar/waldo/quux///?mief", 1, "wuff", "https://foobar/waldo/wuff", 0); @@ -61,12 +61,4 @@ static void test_import_url_change_suffix(void) { test_import_url_change_suffix_one("x:y/z/", 2, "wuff", "x:y/wuff", 0); } -int main(int argc, char *argv[]) { - - test_setup_logging(LOG_INFO); - - test_import_url_last_component(); - test_import_url_change_suffix(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-in-addr-prefix-util.c b/src/test/test-in-addr-prefix-util.c index 5db22302bf..6503353465 100644 --- a/src/test/test-in-addr-prefix-util.c +++ b/src/test/test-in-addr-prefix-util.c @@ -82,13 +82,11 @@ static void test_in_addr_prefixes_reduce(Set *prefixes) { assert_se(in_addr_prefixes_is_any(prefixes)); } -int main(int argc, char *argv[]) { +TEST(in_addr_prefixes) { _cleanup_set_free_ Set *prefixes = NULL; - test_setup_logging(LOG_DEBUG); - test_config_parse_in_addr_prefixes(&prefixes); test_in_addr_prefixes_reduce(prefixes); - - return 0; } + +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-in-addr-util.c b/src/test/test-in-addr-util.c index b7171be1a4..636967c240 100644 --- a/src/test/test-in-addr-util.c +++ b/src/test/test-in-addr-util.c @@ -57,9 +57,7 @@ static void test_in_addr_prefix_from_string_one( } } -static void test_in_addr_prefix_from_string(void) { - log_info("/* %s */", __func__); - +TEST(in_addr_prefix_from_string) { test_in_addr_prefix_from_string_one("", AF_INET, -EINVAL, NULL, 0, -EINVAL, 0, -EINVAL, 0); test_in_addr_prefix_from_string_one("/", AF_INET, -EINVAL, NULL, 0, -EINVAL, 0, -EINVAL, 0); test_in_addr_prefix_from_string_one("/8", AF_INET, -EINVAL, NULL, 0, -EINVAL, 0, -EINVAL, 0); @@ -116,9 +114,7 @@ static void test_in_addr_prefix_to_string_unoptimized(int family, const char *p) assert_se(in_addr_equal(family, &u1, &u2) > 0); } -static void test_in_addr_prefix_to_string(void) { - log_info("/* %s */", __func__); - +TEST(in_addr_prefix_to_string) { test_in_addr_prefix_to_string_valid(AF_INET, "0.0.0.0/32"); test_in_addr_prefix_to_string_valid(AF_INET, "1.2.3.4/0"); test_in_addr_prefix_to_string_valid(AF_INET, "1.2.3.4/24"); @@ -137,12 +133,10 @@ static void test_in_addr_prefix_to_string(void) { test_in_addr_prefix_to_string_unoptimized(AF_INET6, "fd00:1111::0000:2222:3333:4444:0001/64"); } -static void test_in_addr_random_prefix(void) { +TEST(in_addr_random_prefix) { _cleanup_free_ char *str = NULL; union in_addr_union a; - log_info("/* %s */", __func__); - assert_se(in_addr_from_string(AF_INET, "192.168.10.1", &a) >= 0); assert_se(in_addr_random_prefix(AF_INET, &a, 31, 32) >= 0); @@ -183,11 +177,9 @@ static void test_in_addr_random_prefix(void) { str = mfree(str); } -static void test_in_addr_is_null(void) { +TEST(in_addr_is_null) { union in_addr_union i = {}; - log_info("/* %s */", __func__); - assert_se(in_addr_is_null(AF_INET, &i) == true); assert_se(in_addr_is_null(AF_INET6, &i) == true); @@ -207,9 +199,7 @@ static void test_in_addr_prefix_intersect_one(unsigned f, const char *a, unsigne assert_se(in_addr_prefix_intersect(f, &ua, apl, &ub, bpl) == result); } -static void test_in_addr_prefix_intersect(void) { - log_info("/* %s */", __func__); - +TEST(in_addr_prefix_intersect) { test_in_addr_prefix_intersect_one(AF_INET, "255.255.255.255", 32, "255.255.255.254", 32, 0); test_in_addr_prefix_intersect_one(AF_INET, "255.255.255.255", 0, "255.255.255.255", 32, 1); test_in_addr_prefix_intersect_one(AF_INET, "0.0.0.0", 0, "47.11.8.15", 32, 1); @@ -251,9 +241,7 @@ static void test_in_addr_prefix_next_one(unsigned f, const char *before, unsigne } } -static void test_in_addr_prefix_next(void) { - log_info("/* %s */", __func__); - +TEST(in_addr_prefix_next) { test_in_addr_prefix_next_one(AF_INET, "192.168.0.0", 24, "192.168.1.0"); test_in_addr_prefix_next_one(AF_INET, "192.168.0.0", 16, "192.169.0.0"); test_in_addr_prefix_next_one(AF_INET, "192.168.0.0", 20, "192.168.16.0"); @@ -291,9 +279,7 @@ static void test_in_addr_prefix_nth_one(unsigned f, const char *before, unsigned } } -static void test_in_addr_prefix_nth(void) { - log_info("/* %s */", __func__); - +TEST(in_addr_prefix_nth) { test_in_addr_prefix_nth_one(AF_INET, "192.168.0.0", 24, 0, "192.168.0.0"); test_in_addr_prefix_nth_one(AF_INET, "192.168.0.123", 24, 0, "192.168.0.0"); test_in_addr_prefix_nth_one(AF_INET, "192.168.0.123", 24, 1, "192.168.1.0"); @@ -346,9 +332,7 @@ static void test_in_addr_prefix_range_one( } } -static void test_in_addr_prefix_range(void) { - log_info("/* %s */", __func__); - +TEST(in_addr_prefix_range) { test_in_addr_prefix_range_one(AF_INET, "192.168.123.123", 24, "192.168.123.0", "192.168.124.0"); test_in_addr_prefix_range_one(AF_INET, "192.168.123.123", 16, "192.168.0.0", "192.169.0.0"); @@ -371,9 +355,7 @@ static void test_in_addr_to_string_one(int f, const char *addr) { assert_se(streq(addr, r)); } -static void test_in_addr_to_string(void) { - log_info("/* %s */", __func__); - +TEST(in_addr_to_string) { test_in_addr_to_string_one(AF_INET, "192.168.0.1"); test_in_addr_to_string_one(AF_INET, "10.11.12.13"); test_in_addr_to_string_one(AF_INET6, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); @@ -381,18 +363,4 @@ static void test_in_addr_to_string(void) { test_in_addr_to_string_one(AF_INET6, "fe80::"); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_DEBUG); - - test_in_addr_prefix_from_string(); - test_in_addr_random_prefix(); - test_in_addr_prefix_to_string(); - test_in_addr_is_null(); - test_in_addr_prefix_intersect(); - test_in_addr_prefix_next(); - test_in_addr_prefix_nth(); - test_in_addr_prefix_range(); - test_in_addr_to_string(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-install-file.c b/src/test/test-install-file.c index 55d77ac236..7112775507 100644 --- a/src/test/test-install-file.c +++ b/src/test/test-install-file.c @@ -8,13 +8,11 @@ #include "tmpfile-util.h" #include "umask-util.h" -static void test_install_file(void) { +TEST(install_file) { _cleanup_(rm_rf_physical_and_freep) char *p = NULL; _cleanup_free_ char *a = NULL, *b = NULL, *c = NULL; struct stat stat1, stat2; - log_info("/* %s */", __func__); - assert_se(mkdtemp_malloc(NULL, &p) >= 0); assert_se(a = path_join(p, "foo")); assert_se(b = path_join(p, "bar")); @@ -63,10 +61,4 @@ static void test_install_file(void) { assert_se(install_file(AT_FDCWD, b, AT_FDCWD, a, INSTALL_FSYNC_FULL|INSTALL_REPLACE) == 0); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_INFO); - - test_install_file(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-io-util.c b/src/test/test-io-util.c index 104c022cb4..38ca3d858c 100644 --- a/src/test/test-io-util.c +++ b/src/test/test-io-util.c @@ -8,6 +8,7 @@ #include "fd-util.h" #include "io-util.h" #include "macro.h" +#include "tests.h" static void test_sparse_write_one(int fd, const char *buffer, size_t n) { char check[n]; @@ -25,7 +26,7 @@ static void test_sparse_write_one(int fd, const char *buffer, size_t n) { assert_se(memcmp(buffer, check, n) == 0); } -static void test_sparse_write(void) { +TEST(sparse_write) { const char test_a[] = "test"; const char test_b[] = "\0\0\0\0test\0\0\0\0"; const char test_c[] = "\0\0test\0\0\0\0"; @@ -45,8 +46,4 @@ static void test_sparse_write(void) { test_sparse_write_one(fd, test_e, sizeof(test_e)); } -int main(void) { - test_sparse_write(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-ip-protocol-list.c b/src/test/test-ip-protocol-list.c index 15bbbdeb72..018441d497 100644 --- a/src/test/test-ip-protocol-list.c +++ b/src/test/test-ip-protocol-list.c @@ -6,6 +6,7 @@ #include "ip-protocol-list.h" #include "stdio-util.h" #include "string-util.h" +#include "tests.h" static void test_int(int i) { char str[DECIMAL_STR_MAX(int)]; @@ -35,30 +36,34 @@ static void test_str_fail(const char *s) { assert_se(parse_ip_protocol(s) == -EINVAL); } -static void test_parse_ip_protocol(const char *s, int expected) { +static void test_parse_ip_protocol_one(const char *s, int expected) { assert_se(parse_ip_protocol(s) == expected); } -int main(int argc, const char *argv[]) { +TEST(integer) { test_int(IPPROTO_TCP); test_int(IPPROTO_DCCP); test_int_fail(-1); test_int_fail(1024 * 1024); +} +TEST(string) { test_str("sctp"); test_str("udp"); test_str_fail("hoge"); test_str_fail("-1"); test_str_fail("1000000000"); - - test_parse_ip_protocol("sctp", IPPROTO_SCTP); - test_parse_ip_protocol("ScTp", IPPROTO_SCTP); - test_parse_ip_protocol("ip", IPPROTO_IP); - test_parse_ip_protocol("", IPPROTO_IP); - test_parse_ip_protocol("1", 1); - test_parse_ip_protocol("0", 0); - test_parse_ip_protocol("-10", -EINVAL); - test_parse_ip_protocol("100000000", -EINVAL); - - return 0; } + +TEST(parse_ip_protocol) { + test_parse_ip_protocol_one("sctp", IPPROTO_SCTP); + test_parse_ip_protocol_one("ScTp", IPPROTO_SCTP); + test_parse_ip_protocol_one("ip", IPPROTO_IP); + test_parse_ip_protocol_one("", IPPROTO_IP); + test_parse_ip_protocol_one("1", 1); + test_parse_ip_protocol_one("0", 0); + test_parse_ip_protocol_one("-10", -EINVAL); + test_parse_ip_protocol_one("100000000", -EINVAL); +} + +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-journal-importer.c b/src/test/test-journal-importer.c index da266d92ac..ec9e49e9e4 100644 --- a/src/test/test-journal-importer.c +++ b/src/test/test-journal-importer.c @@ -20,7 +20,7 @@ static void assert_iovec_entry(const struct iovec *iovec, const char* content) { "COREDUMP_PROC_CGROUP=1:name=systemd:/\n" \ "0::/user.slice/user-1002.slice/user@1002.service/gnome-terminal-server.service\n" -static void test_basic_parsing(void) { +TEST(basic_parsing) { _cleanup_(journal_importer_cleanup) JournalImporter imp = JOURNAL_IMPORTER_INIT(-1); _cleanup_free_ char *journal_data_path = NULL; int r; @@ -51,7 +51,7 @@ static void test_basic_parsing(void) { assert_se(journal_importer_eof(&imp)); } -static void test_bad_input(void) { +TEST(bad_input) { _cleanup_(journal_importer_cleanup) JournalImporter imp = JOURNAL_IMPORTER_INIT(-1); _cleanup_free_ char *journal_data_path = NULL; int r; @@ -68,11 +68,4 @@ static void test_bad_input(void) { assert_se(journal_importer_eof(&imp)); } -int main(int argc, char **argv) { - test_setup_logging(LOG_DEBUG); - - test_basic_parsing(); - test_bad_input(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-json.c b/src/test/test-json.c index 926b246e17..b385edc269 100644 --- a/src/test/test-json.c +++ b/src/test/test-json.c @@ -14,7 +14,7 @@ #include "tests.h" #include "util.h" -static void test_tokenizer(const char *data, ...) { +static void test_tokenizer_one(const char *data, ...) { unsigned line = 0, column = 0; void *state = NULL; va_list ap; @@ -78,7 +78,7 @@ static void test_tokenizer(const char *data, ...) { typedef void (*Test)(JsonVariant *); -static void test_variant(const char *data, Test test) { +static void test_variant_one(const char *data, Test test) { _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *w = NULL; _cleanup_free_ char *s = NULL; int r; @@ -262,9 +262,7 @@ static void test_zeroes(JsonVariant *v) { } } -static void test_build(void) { - log_info("/* %s */", __func__); - +TEST(build) { _cleanup_(json_variant_unrefp) JsonVariant *a = NULL, *b = NULL; _cleanup_free_ char *s = NULL, *t = NULL; @@ -348,7 +346,7 @@ static void test_build(void) { assert_se(json_variant_equal(a, b)); } -static void test_source(void) { +TEST(source) { static const char data[] = "\n" "\n" @@ -365,8 +363,6 @@ static void test_source(void) { "false, 7.5, {} ]\n" "}\n"; - log_info("/* %s */", __func__); - _cleanup_fclose_ FILE *f = NULL; _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; @@ -387,9 +383,7 @@ static void test_source(void) { printf("--- pretty end ---\n"); } -static void test_depth(void) { - log_info("/* %s */", __func__); - +TEST(depth) { _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; int r; @@ -427,9 +421,7 @@ static void test_depth(void) { fputs("\n", stdout); } -static void test_normalize(void) { - log_info("/* %s */", __func__); - +TEST(normalize) { _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *w = NULL; _cleanup_free_ char *t = NULL; @@ -473,9 +465,7 @@ static void test_normalize(void) { t = mfree(t); } -static void test_bisect(void) { - log_info("/* %s */", __func__); - +TEST(bisect) { _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; /* Tests the bisection logic in json_variant_by_key() */ @@ -540,12 +530,10 @@ static void test_float_match(JsonVariant *v) { json_variant_integer(json_variant_by_index(v, 8)) == -10); } -static void test_float(void) { +TEST(float) { _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *w = NULL; _cleanup_free_ char *text = NULL; - log_info("/* %s */", __func__); - assert_se(json_build(&v, JSON_BUILD_ARRAY( JSON_BUILD_REAL(DBL_MIN), JSON_BUILD_REAL(DBL_MAX), @@ -576,11 +564,9 @@ static void test_equal_text(JsonVariant *v, const char *text) { assert_se(json_variant_equal(v, w) || (!v && json_variant_is_null(w))); } -static void test_set_field(void) { +TEST(set_field) { _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; - log_info("/* %s */", __func__); - test_equal_text(v, "null"); assert_se(json_variant_set_field(&v, "foo", NULL) >= 0); test_equal_text(v, "{\"foo\" : null}"); @@ -592,60 +578,51 @@ static void test_set_field(void) { test_equal_text(v, "{\"foo\" : \"quux2\", \"bar\" : null}"); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_DEBUG); +TEST(tokenizer) { + test_tokenizer_one("x", -EINVAL); + test_tokenizer_one("", JSON_TOKEN_END); + test_tokenizer_one(" ", JSON_TOKEN_END); + test_tokenizer_one("0", JSON_TOKEN_UNSIGNED, (uint64_t) 0, JSON_TOKEN_END); + test_tokenizer_one("-0", JSON_TOKEN_INTEGER, (int64_t) 0, JSON_TOKEN_END); + test_tokenizer_one("1234", JSON_TOKEN_UNSIGNED, (uint64_t) 1234, JSON_TOKEN_END); + test_tokenizer_one("-1234", JSON_TOKEN_INTEGER, (int64_t) -1234, JSON_TOKEN_END); + test_tokenizer_one("18446744073709551615", JSON_TOKEN_UNSIGNED, (uint64_t) UINT64_MAX, JSON_TOKEN_END); + test_tokenizer_one("-9223372036854775808", JSON_TOKEN_INTEGER, (int64_t) INT64_MIN, JSON_TOKEN_END); + test_tokenizer_one("18446744073709551616", JSON_TOKEN_REAL, (double) 18446744073709551616.0L, JSON_TOKEN_END); + test_tokenizer_one("-9223372036854775809", JSON_TOKEN_REAL, (double) -9223372036854775809.0L, JSON_TOKEN_END); + test_tokenizer_one("-1234", JSON_TOKEN_INTEGER, (int64_t) -1234, JSON_TOKEN_END); + test_tokenizer_one("3.141", JSON_TOKEN_REAL, (double) 3.141, JSON_TOKEN_END); + test_tokenizer_one("0.0", JSON_TOKEN_REAL, (double) 0.0, JSON_TOKEN_END); + test_tokenizer_one("7e3", JSON_TOKEN_REAL, (double) 7e3, JSON_TOKEN_END); + test_tokenizer_one("-7e-3", JSON_TOKEN_REAL, (double) -7e-3, JSON_TOKEN_END); + test_tokenizer_one("true", JSON_TOKEN_BOOLEAN, true, JSON_TOKEN_END); + test_tokenizer_one("false", JSON_TOKEN_BOOLEAN, false, JSON_TOKEN_END); + test_tokenizer_one("null", JSON_TOKEN_NULL, JSON_TOKEN_END); + test_tokenizer_one("{}", JSON_TOKEN_OBJECT_OPEN, JSON_TOKEN_OBJECT_CLOSE, JSON_TOKEN_END); + test_tokenizer_one("\t {\n} \n", JSON_TOKEN_OBJECT_OPEN, JSON_TOKEN_OBJECT_CLOSE, JSON_TOKEN_END); + test_tokenizer_one("[]", JSON_TOKEN_ARRAY_OPEN, JSON_TOKEN_ARRAY_CLOSE, JSON_TOKEN_END); + test_tokenizer_one("\t [] \n\n", JSON_TOKEN_ARRAY_OPEN, JSON_TOKEN_ARRAY_CLOSE, JSON_TOKEN_END); + test_tokenizer_one("\"\"", JSON_TOKEN_STRING, "", JSON_TOKEN_END); + test_tokenizer_one("\"foo\"", JSON_TOKEN_STRING, "foo", JSON_TOKEN_END); + test_tokenizer_one("\"foo\\nfoo\"", JSON_TOKEN_STRING, "foo\nfoo", JSON_TOKEN_END); + test_tokenizer_one("{\"foo\" : \"bar\"}", JSON_TOKEN_OBJECT_OPEN, JSON_TOKEN_STRING, "foo", JSON_TOKEN_COLON, JSON_TOKEN_STRING, "bar", JSON_TOKEN_OBJECT_CLOSE, JSON_TOKEN_END); + test_tokenizer_one("{\"foo\" : [true, false]}", JSON_TOKEN_OBJECT_OPEN, JSON_TOKEN_STRING, "foo", JSON_TOKEN_COLON, JSON_TOKEN_ARRAY_OPEN, JSON_TOKEN_BOOLEAN, true, JSON_TOKEN_COMMA, JSON_TOKEN_BOOLEAN, false, JSON_TOKEN_ARRAY_CLOSE, JSON_TOKEN_OBJECT_CLOSE, JSON_TOKEN_END); + test_tokenizer_one("\"\xef\xbf\xbd\"", JSON_TOKEN_STRING, "\xef\xbf\xbd", JSON_TOKEN_END); + test_tokenizer_one("\"\\ufffd\"", JSON_TOKEN_STRING, "\xef\xbf\xbd", JSON_TOKEN_END); + test_tokenizer_one("\"\\uf\"", -EINVAL); + test_tokenizer_one("\"\\ud800a\"", -EINVAL); + test_tokenizer_one("\"\\udc00\\udc00\"", -EINVAL); + test_tokenizer_one("\"\\ud801\\udc37\"", JSON_TOKEN_STRING, "\xf0\x90\x90\xb7", JSON_TOKEN_END); - test_tokenizer("x", -EINVAL); - test_tokenizer("", JSON_TOKEN_END); - test_tokenizer(" ", JSON_TOKEN_END); - test_tokenizer("0", JSON_TOKEN_UNSIGNED, (uint64_t) 0, JSON_TOKEN_END); - test_tokenizer("-0", JSON_TOKEN_INTEGER, (int64_t) 0, JSON_TOKEN_END); - test_tokenizer("1234", JSON_TOKEN_UNSIGNED, (uint64_t) 1234, JSON_TOKEN_END); - test_tokenizer("-1234", JSON_TOKEN_INTEGER, (int64_t) -1234, JSON_TOKEN_END); - test_tokenizer("18446744073709551615", JSON_TOKEN_UNSIGNED, (uint64_t) UINT64_MAX, JSON_TOKEN_END); - test_tokenizer("-9223372036854775808", JSON_TOKEN_INTEGER, (int64_t) INT64_MIN, JSON_TOKEN_END); - test_tokenizer("18446744073709551616", JSON_TOKEN_REAL, (double) 18446744073709551616.0L, JSON_TOKEN_END); - test_tokenizer("-9223372036854775809", JSON_TOKEN_REAL, (double) -9223372036854775809.0L, JSON_TOKEN_END); - test_tokenizer("-1234", JSON_TOKEN_INTEGER, (int64_t) -1234, JSON_TOKEN_END); - test_tokenizer("3.141", JSON_TOKEN_REAL, (double) 3.141, JSON_TOKEN_END); - test_tokenizer("0.0", JSON_TOKEN_REAL, (double) 0.0, JSON_TOKEN_END); - test_tokenizer("7e3", JSON_TOKEN_REAL, (double) 7e3, JSON_TOKEN_END); - test_tokenizer("-7e-3", JSON_TOKEN_REAL, (double) -7e-3, JSON_TOKEN_END); - test_tokenizer("true", JSON_TOKEN_BOOLEAN, true, JSON_TOKEN_END); - test_tokenizer("false", JSON_TOKEN_BOOLEAN, false, JSON_TOKEN_END); - test_tokenizer("null", JSON_TOKEN_NULL, JSON_TOKEN_END); - test_tokenizer("{}", JSON_TOKEN_OBJECT_OPEN, JSON_TOKEN_OBJECT_CLOSE, JSON_TOKEN_END); - test_tokenizer("\t {\n} \n", JSON_TOKEN_OBJECT_OPEN, JSON_TOKEN_OBJECT_CLOSE, JSON_TOKEN_END); - test_tokenizer("[]", JSON_TOKEN_ARRAY_OPEN, JSON_TOKEN_ARRAY_CLOSE, JSON_TOKEN_END); - test_tokenizer("\t [] \n\n", JSON_TOKEN_ARRAY_OPEN, JSON_TOKEN_ARRAY_CLOSE, JSON_TOKEN_END); - test_tokenizer("\"\"", JSON_TOKEN_STRING, "", JSON_TOKEN_END); - test_tokenizer("\"foo\"", JSON_TOKEN_STRING, "foo", JSON_TOKEN_END); - test_tokenizer("\"foo\\nfoo\"", JSON_TOKEN_STRING, "foo\nfoo", JSON_TOKEN_END); - test_tokenizer("{\"foo\" : \"bar\"}", JSON_TOKEN_OBJECT_OPEN, JSON_TOKEN_STRING, "foo", JSON_TOKEN_COLON, JSON_TOKEN_STRING, "bar", JSON_TOKEN_OBJECT_CLOSE, JSON_TOKEN_END); - test_tokenizer("{\"foo\" : [true, false]}", JSON_TOKEN_OBJECT_OPEN, JSON_TOKEN_STRING, "foo", JSON_TOKEN_COLON, JSON_TOKEN_ARRAY_OPEN, JSON_TOKEN_BOOLEAN, true, JSON_TOKEN_COMMA, JSON_TOKEN_BOOLEAN, false, JSON_TOKEN_ARRAY_CLOSE, JSON_TOKEN_OBJECT_CLOSE, JSON_TOKEN_END); - test_tokenizer("\"\xef\xbf\xbd\"", JSON_TOKEN_STRING, "\xef\xbf\xbd", JSON_TOKEN_END); - test_tokenizer("\"\\ufffd\"", JSON_TOKEN_STRING, "\xef\xbf\xbd", JSON_TOKEN_END); - test_tokenizer("\"\\uf\"", -EINVAL); - test_tokenizer("\"\\ud800a\"", -EINVAL); - test_tokenizer("\"\\udc00\\udc00\"", -EINVAL); - test_tokenizer("\"\\ud801\\udc37\"", JSON_TOKEN_STRING, "\xf0\x90\x90\xb7", JSON_TOKEN_END); - - test_tokenizer("[1, 2, -3]", JSON_TOKEN_ARRAY_OPEN, JSON_TOKEN_UNSIGNED, (uint64_t) 1, JSON_TOKEN_COMMA, JSON_TOKEN_UNSIGNED, (uint64_t) 2, JSON_TOKEN_COMMA, JSON_TOKEN_INTEGER, (int64_t) -3, JSON_TOKEN_ARRAY_CLOSE, JSON_TOKEN_END); - - test_variant("{\"k\": \"v\", \"foo\": [1, 2, 3], \"bar\": {\"zap\": null}}", test_1); - test_variant("{\"mutant\": [1, null, \"1\", {\"1\": [1, \"1\"]}], \"thisisaverylongproperty\": 1.27}", test_2); - test_variant("{\"foo\" : \"\\u0935\\u093f\\u0935\\u0947\\u0915\\u0916\\u094d\\u092f\\u093e\\u0924\\u093f\\u0930\\u0935\\u093f\\u092a\\u094d\\u0932\\u0935\\u093e\\u0020\\u0939\\u093e\\u0928\\u094b\\u092a\\u093e\\u092f\\u0903\\u0964\"}", NULL); - - test_variant("[ 0, -0, 0.0, -0.0, 0.000, -0.000, 0e0, -0e0, 0e+0, -0e-0, 0e-0, -0e000, 0e+000 ]", test_zeroes); - - test_build(); - test_source(); - test_depth(); - - test_normalize(); - test_bisect(); - test_float(); - test_set_field(); - - return 0; + test_tokenizer_one("[1, 2, -3]", JSON_TOKEN_ARRAY_OPEN, JSON_TOKEN_UNSIGNED, (uint64_t) 1, JSON_TOKEN_COMMA, JSON_TOKEN_UNSIGNED, (uint64_t) 2, JSON_TOKEN_COMMA, JSON_TOKEN_INTEGER, (int64_t) -3, JSON_TOKEN_ARRAY_CLOSE, JSON_TOKEN_END); } + +TEST(variant) { + test_variant_one("{\"k\": \"v\", \"foo\": [1, 2, 3], \"bar\": {\"zap\": null}}", test_1); + test_variant_one("{\"mutant\": [1, null, \"1\", {\"1\": [1, \"1\"]}], \"thisisaverylongproperty\": 1.27}", test_2); + test_variant_one("{\"foo\" : \"\\u0935\\u093f\\u0935\\u0947\\u0915\\u0916\\u094d\\u092f\\u093e\\u0924\\u093f\\u0930\\u0935\\u093f\\u092a\\u094d\\u0932\\u0935\\u093e\\u0020\\u0939\\u093e\\u0928\\u094b\\u092a\\u093e\\u092f\\u0903\\u0964\"}", NULL); + + test_variant_one("[ 0, -0, 0.0, -0.0, 0.000, -0.000, 0e0, -0e0, 0e+0, -0e-0, 0e-0, -0e000, 0e+000 ]", test_zeroes); +} + +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-libmount.c b/src/test/test-libmount.c index bd2381f38e..b3e1a327ab 100644 --- a/src/test/test-libmount.c +++ b/src/test/test-libmount.c @@ -66,7 +66,7 @@ static void test_libmount_unescaping_one( assert_se(mnt_table_next_fs(table, iter, &fs) == 1); } -static void test_libmount_unescaping(void) { +TEST(libmount_unescaping) { test_libmount_unescaping_one( "escaped space + utf8", "729 38 0:59 / /tmp/„zupa\\040zębowa” rw,relatime shared:395 - tmpfs die\\040Brühe rw,seclabel", @@ -107,9 +107,4 @@ static void test_libmount_unescaping(void) { ); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_DEBUG); - - test_libmount_unescaping(); - return 0; -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-local-addresses.c b/src/test/test-local-addresses.c index 1063aeb7e1..732d47a3f9 100644 --- a/src/test/test-local-addresses.c +++ b/src/test/test-local-addresses.c @@ -19,12 +19,10 @@ static void print_local_addresses(struct local_address *a, unsigned n) { } } -int main(int argc, char *argv[]) { +TEST(local_addresses) { struct local_address *a = NULL; int n, n_ipv4, n_ipv6; - test_setup_logging(LOG_DEBUG); - n = local_addresses(NULL, 0, AF_INET, &a); assert_se(n >= 0); log_debug("/* Local Addresses(ifindex:0, AF_INET) */"); @@ -78,6 +76,6 @@ int main(int argc, char *argv[]) { log_debug("/* Local Outbounds */"); print_local_addresses(a, (unsigned) n); free(a); - - return 0; } + +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-locale-util.c b/src/test/test-locale-util.c index 17a8c2b985..55f86f7c2f 100644 --- a/src/test/test-locale-util.c +++ b/src/test/test-locale-util.c @@ -5,9 +5,10 @@ #include "locale-util.h" #include "macro.h" #include "strv.h" +#include "tests.h" #include "util.h" -static void test_get_locales(void) { +TEST(get_locales) { _cleanup_strv_free_ char **locales = NULL; char **p; int r; @@ -22,9 +23,7 @@ static void test_get_locales(void) { } } -static void test_locale_is_valid(void) { - log_info("/* %s */", __func__); - +TEST(locale_is_valid) { assert_se(locale_is_valid("en_EN.utf8")); assert_se(locale_is_valid("fr_FR.utf8")); assert_se(locale_is_valid("fr_FR@euro")); @@ -37,9 +36,7 @@ static void test_locale_is_valid(void) { assert_se(!locale_is_valid("\x01gar\x02 bage\x03")); } -static void test_locale_is_installed(void) { - log_info("/* %s */", __func__); - +TEST(locale_is_installed) { /* Always available */ assert_se(locale_is_installed("POSIX") > 0); assert_se(locale_is_installed("C") > 0); @@ -59,13 +56,11 @@ static void test_locale_is_installed(void) { assert_se(locale_is_installed("zz_ZZ") == 0); } -static void test_keymaps(void) { +TEST(keymaps) { _cleanup_strv_free_ char **kmaps = NULL; char **p; int r; - log_info("/* %s */", __func__); - assert_se(!keymap_is_valid("")); assert_se(!keymap_is_valid("/usr/bin/foo")); assert_se(!keymap_is_valid("\x01gar\x02 bage\x03")); @@ -89,11 +84,9 @@ static void test_keymaps(void) { } #define dump_glyph(x) log_info(STRINGIFY(x) ": %s", special_glyph(x)) -static void dump_special_glyphs(void) { +TEST(dump_special_glyphs) { assert_cc(SPECIAL_GLYPH_SPARKLES + 1 == _SPECIAL_GLYPH_MAX); - log_info("/* %s */", __func__); - log_info("is_locale_utf8: %s", yes_no(is_locale_utf8())); dump_glyph(SPECIAL_GLYPH_TREE_VERTICAL); @@ -126,13 +119,4 @@ static void dump_special_glyphs(void) { dump_glyph(SPECIAL_GLYPH_SPARKLES); } -int main(int argc, char *argv[]) { - test_get_locales(); - test_locale_is_valid(); - test_locale_is_installed(); - test_keymaps(); - - dump_special_glyphs(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-modhex.c b/src/test/test-modhex.c index 314be99879..6725732be8 100644 --- a/src/test/test-modhex.c +++ b/src/test/test-modhex.c @@ -3,6 +3,7 @@ #include "recovery-key.h" #include "alloc-util.h" #include "string-util.h" +#include "tests.h" static void test_normalize_recovery_key(const char *t, const char *expected) { _cleanup_free_ char *z = NULL; @@ -16,8 +17,7 @@ static void test_normalize_recovery_key(const char *t, const char *expected) { (r == -EINVAL && z == NULL)); } -int main(int argc, char *arv[]) { - +TEST(normalize_recovery_key_all) { test_normalize_recovery_key("iefgcelh-biduvkjv-cjvuncnk-vlfchdid-jhtuhhde-urkllkeg-ilkjgbrt-hjkbgktj", "iefgcelh-biduvkjv-cjvuncnk-vlfchdid-jhtuhhde-urkllkeg-ilkjgbrt-hjkbgktj"); @@ -46,6 +46,6 @@ int main(int argc, char *arv[]) { test_normalize_recovery_key("iefgcelhebiduvkjv-cjvuncnk-vlfchdid-jhtuhhde-urkllkeg-ilkjgbrt-hjkbgktj", NULL); test_normalize_recovery_key("", NULL); - - return 0; } + +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-mount-util.c b/src/test/test-mount-util.c index fdf2a249ed..74d352268e 100644 --- a/src/test/test-mount-util.c +++ b/src/test/test-mount-util.c @@ -20,12 +20,10 @@ #include "tests.h" #include "tmpfile-util.h" -static void test_mount_option_mangle(void) { +TEST(mount_option_mangle) { char *opts = NULL; unsigned long f; - log_info("/* %s */", __func__); - assert_se(mount_option_mangle(NULL, MS_RDONLY|MS_NOSUID, &f, &opts) == 0); assert_se(f == (MS_RDONLY|MS_NOSUID)); assert_se(opts == NULL); @@ -91,9 +89,7 @@ static void test_mount_flags_to_string_one(unsigned long flags, const char *expe assert_se(streq(x, expected)); } -static void test_mount_flags_to_string(void) { - log_info("/* %s */", __func__); - +TEST(mount_flags_to_string) { test_mount_flags_to_string_one(0, "0"); test_mount_flags_to_string_one(MS_RDONLY, "MS_RDONLY"); test_mount_flags_to_string_one(MS_NOSUID, "MS_NOSUID"); @@ -129,13 +125,11 @@ static void test_mount_flags_to_string(void) { "MS_I_VERSION|MS_STRICTATIME|MS_LAZYTIME|fc000200"); } -static void test_bind_remount_recursive(void) { +TEST(bind_remount_recursive) { _cleanup_(rm_rf_physical_and_freep) char *tmp = NULL; _cleanup_free_ char *subdir = NULL; const char *p; - log_info("/* %s */", __func__); - if (geteuid() != 0 || have_effective_cap(CAP_SYS_ADMIN) <= 0) { (void) log_tests_skipped("not running privileged"); return; @@ -186,11 +180,9 @@ static void test_bind_remount_recursive(void) { } } -static void test_bind_remount_one(void) { +TEST(bind_remount_one) { pid_t pid; - log_info("/* %s */", __func__); - if (geteuid() != 0 || have_effective_cap(CAP_SYS_ADMIN) <= 0) { (void) log_tests_skipped("not running privileged"); return; @@ -220,13 +212,11 @@ static void test_bind_remount_one(void) { assert_se(wait_for_terminate_and_check("test-remount-one", pid, WAIT_LOG) == EXIT_SUCCESS); } -static void test_make_mount_point_inode(void) { +TEST(make_mount_point_inode) { _cleanup_(rm_rf_physical_and_freep) char *d = NULL; const char *src_file, *src_dir, *dst_file, *dst_dir; struct stat st; - log_info("/* %s */", __func__); - assert_se(mkdtemp_malloc(NULL, &d) >= 0); src_file = strjoina(d, "/src/file"); @@ -266,14 +256,4 @@ static void test_make_mount_point_inode(void) { assert_se(!(S_IXOTH & st.st_mode)); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_DEBUG); - - test_mount_option_mangle(); - test_mount_flags_to_string(); - test_bind_remount_recursive(); - test_bind_remount_one(); - test_make_mount_point_inode(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-net-naming-scheme.c b/src/test/test-net-naming-scheme.c index 693b2f6604..0766170757 100644 --- a/src/test/test-net-naming-scheme.c +++ b/src/test/test-net-naming-scheme.c @@ -4,17 +4,13 @@ #include "string-util.h" #include "tests.h" -static void test_default_net_naming_scheme(void) { - log_info("/* %s */", __func__); - +TEST(default_net_naming_scheme) { const NamingScheme *n; assert_se(n = naming_scheme_from_name(DEFAULT_NET_NAMING_SCHEME)); log_info("default → %s", n->name); } -static void test_naming_scheme_conversions(void) { - log_info("/* %s */", __func__); - +TEST(naming_scheme_conversions) { const NamingScheme *n; assert_se(n = naming_scheme_from_name("latest")); log_info("latest → %s", n->name); @@ -23,9 +19,4 @@ static void test_naming_scheme_conversions(void) { assert_se(streq(n->name, "v238")); } -int main(int argc, char **argv) { - test_setup_logging(LOG_INFO); - - test_default_net_naming_scheme(); - test_naming_scheme_conversions(); -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-ordered-set.c b/src/test/test-ordered-set.c index 8d3c5b0d8c..c055411b79 100644 --- a/src/test/test-ordered-set.c +++ b/src/test/test-ordered-set.c @@ -5,10 +5,9 @@ #include "ordered-set.h" #include "string-util.h" #include "strv.h" +#include "tests.h" -static void test_set_steal_first(void) { - log_info("/* %s */", __func__); - +TEST(set_steal_first) { _cleanup_ordered_set_free_ OrderedSet *m = NULL; int seen[3] = {}; char *val; @@ -41,12 +40,10 @@ static void item_seen(Item *item) { DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(item_hash_ops, void, trivial_hash_func, trivial_compare_func, Item, item_seen); -static void test_set_free_with_hash_ops(void) { +TEST(set_free_with_hash_ops) { OrderedSet *m; struct Item items[4] = {}; - log_info("/* %s */", __func__); - assert_se(m = ordered_set_new(&item_hash_ops)); for (size_t i = 0; i < ELEMENTSOF(items) - 1; i++) @@ -63,12 +60,10 @@ static void test_set_free_with_hash_ops(void) { assert_se(items[3].seen == 0); } -static void test_set_put(void) { +TEST(set_put) { _cleanup_ordered_set_free_ OrderedSet *m = NULL; _cleanup_free_ char **t = NULL, *str = NULL; - log_info("/* %s */", __func__); - m = ordered_set_new(&string_hash_ops); assert_se(m); @@ -94,12 +89,10 @@ static void test_set_put(void) { ordered_set_print(stdout, "FOO=", m); } -static void test_set_put_string_set(void) { +TEST(set_put_string_set) { _cleanup_ordered_set_free_ OrderedSet *m = NULL, *q = NULL; _cleanup_free_ char **final = NULL; /* "just free" because the strings are in the set */ - log_info("/* %s */", __func__); - assert_se(ordered_set_put_strdup(&m, "1") == 1); assert_se(ordered_set_put_strdup(&m, "22") == 1); assert_se(ordered_set_put_strdup(&m, "333") == 1); @@ -116,11 +109,4 @@ static void test_set_put_string_set(void) { ordered_set_print(stdout, "BAR=", m); } -int main(int argc, const char *argv[]) { - test_set_steal_first(); - test_set_free_with_hash_ops(); - test_set_put(); - test_set_put_string_set(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-os-util.c b/src/test/test-os-util.c index ef63026945..eb5466259a 100644 --- a/src/test/test-os-util.c +++ b/src/test/test-os-util.c @@ -6,16 +6,10 @@ #include "os-util.h" #include "tests.h" -static void test_path_is_os_tree(void) { +TEST(path_is_os_tree) { assert_se(path_is_os_tree("/") > 0); assert_se(path_is_os_tree("/etc") == 0); assert_se(path_is_os_tree("/idontexist") == -ENOENT); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_DEBUG); - - test_path_is_os_tree(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-parse-argument.c b/src/test/test-parse-argument.c index 5e9139af2c..cf3d54288a 100644 --- a/src/test/test-parse-argument.c +++ b/src/test/test-parse-argument.c @@ -6,9 +6,7 @@ #include "stdio-util.h" #include "tests.h" -static void test_parse_json_argument(void) { - log_info("/* %s */", __func__); - +TEST(parse_json_argument) { JsonFormatFlags flags = JSON_FORMAT_PRETTY; assert_se(parse_json_argument("help", &flags) == 0); @@ -18,9 +16,7 @@ static void test_parse_json_argument(void) { assert_se(flags == JSON_FORMAT_OFF); } -static void test_parse_path_argument(void) { - log_info("/* %s */", __func__); - +TEST(parse_path_argument) { _cleanup_free_ char *path = NULL; assert_se(parse_path_argument("help", false, &path) == 0); @@ -33,9 +29,7 @@ static void test_parse_path_argument(void) { assert_se(path == NULL); } -static void test_parse_signal_argument(void) { - log_info("/* %s */", __func__); - +TEST(parse_signal_argument) { int signal = -1; assert_se(parse_signal_argument("help", &signal) == 0); @@ -56,10 +50,4 @@ static void test_parse_signal_argument(void) { assert_se(signal == SIGABRT); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_INFO); - - test_parse_json_argument(); - test_parse_path_argument(); - test_parse_signal_argument(); -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-parse-socket-bind-item.c b/src/test/test-parse-socket-bind-item.c index 6d52582bcd..6bca27265c 100644 --- a/src/test/test-parse-socket-bind-item.c +++ b/src/test/test-parse-socket-bind-item.c @@ -6,6 +6,7 @@ #include "macro.h" #include "parse-socket-bind-item.h" +#include "tests.h" static void test_valid_item( const char *str, @@ -34,7 +35,7 @@ static void test_invalid_item(const char *str) { log_info("%s: \"%s\" ok", __func__, str); } -int main(int argc, char *argv[]) { +TEST(valid_items) { test_valid_item("any", AF_UNSPEC, 0, 0, 0); test_valid_item("ipv4", AF_INET, 0, 0, 0); test_valid_item("ipv6", AF_INET6, 0, 0, 0); @@ -60,7 +61,9 @@ int main(int argc, char *argv[]) { test_valid_item("ipv6:tcp:6666", AF_INET6, IPPROTO_TCP, 1, 6666); test_valid_item("ipv6:udp:6666-6667", AF_INET6, IPPROTO_UDP, 2, 6666); test_valid_item("ipv6:tcp:any", AF_INET6, IPPROTO_TCP, 0, 0); +} +TEST(invalid_items) { test_invalid_item(""); test_invalid_item(":"); test_invalid_item("::"); @@ -87,5 +90,6 @@ int main(int argc, char *argv[]) { test_invalid_item("ipv6:tcp:6666 zupa"); test_invalid_item("ipv6:tcp:6666: zupa"); test_invalid_item("ipv6:tcp:6666\n zupa"); - return 0; } + +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-parse-util.c b/src/test/test-parse-util.c index b6a593d0db..27f83f98ab 100644 --- a/src/test/test-parse-util.c +++ b/src/test/test-parse-util.c @@ -10,8 +10,9 @@ #include "log.h" #include "parse-util.h" #include "string-util.h" +#include "tests.h" -static void test_parse_boolean(void) { +TEST(parse_boolean) { assert_se(parse_boolean("1") == 1); assert_se(parse_boolean("y") == 1); assert_se(parse_boolean("Y") == 1); @@ -37,7 +38,7 @@ static void test_parse_boolean(void) { assert_se(parse_boolean("full") < 0); } -static void test_parse_pid(void) { +TEST(parse_pid) { int r; pid_t pid; @@ -71,7 +72,7 @@ static void test_parse_pid(void) { assert_se(r == -EINVAL); } -static void test_parse_mode(void) { +TEST(parse_mode) { mode_t m; assert_se(parse_mode("-1", &m) < 0); @@ -93,7 +94,7 @@ static void test_parse_mode(void) { assert_se(parse_mode(" 1", &m) >= 0 && m == 1); } -static void test_parse_size(void) { +TEST(parse_size) { uint64_t bytes; assert_se(parse_size("", 1024, &bytes) == -EINVAL); @@ -163,7 +164,7 @@ static void test_parse_size(void) { assert_se(parse_size("-10B 20K", 1024, &bytes) == -ERANGE); } -static void test_parse_range(void) { +TEST(parse_range) { unsigned lower, upper; /* Successful cases */ @@ -346,7 +347,7 @@ static void test_parse_range(void) { assert_se(upper == 9999); } -static void test_safe_atolli(void) { +TEST(safe_atolli) { int r; long long l; @@ -397,7 +398,7 @@ static void test_safe_atolli(void) { assert_se(r == -EINVAL); } -static void test_safe_atou16(void) { +TEST(safe_atou16) { int r; uint16_t l; @@ -431,7 +432,7 @@ static void test_safe_atou16(void) { assert_se(r == -EINVAL); } -static void test_safe_atoi16(void) { +TEST(safe_atoi16) { int r; int16_t l; @@ -478,7 +479,7 @@ static void test_safe_atoi16(void) { assert_se(r == -EINVAL); } -static void test_safe_atoux16(void) { +TEST(safe_atoux16) { int r; uint16_t l; @@ -523,7 +524,7 @@ static void test_safe_atoux16(void) { assert_se(r == -EINVAL); } -static void test_safe_atou64(void) { +TEST(safe_atou64) { int r; uint64_t l; @@ -565,7 +566,7 @@ static void test_safe_atou64(void) { assert_se(r == -EINVAL); } -static void test_safe_atoi64(void) { +TEST(safe_atoi64) { int r; int64_t l; @@ -612,7 +613,7 @@ static void test_safe_atoi64(void) { assert_se(r == -EINVAL); } -static void test_safe_atoux64(void) { +TEST(safe_atoux64) { int r; uint64_t l; @@ -657,7 +658,7 @@ static void test_safe_atoux64(void) { assert_se(r == -EINVAL); } -static void test_safe_atod(void) { +TEST(safe_atod) { int r; double d; char *e; @@ -714,7 +715,7 @@ static void test_safe_atod(void) { assert_se(r == -EINVAL); } -static void test_parse_nice(void) { +TEST(parse_nice) { int n; assert_se(parse_nice("0", &n) >= 0 && n == 0); @@ -741,7 +742,7 @@ static void test_parse_nice(void) { assert_se(parse_nice("+20", &n) == -ERANGE); } -static void test_parse_dev(void) { +TEST(parse_dev) { dev_t dev; assert_se(parse_dev("", &dev) == -EINVAL); @@ -758,7 +759,7 @@ static void test_parse_dev(void) { assert_se(parse_dev("0:0", &dev) >= 0 && major(dev) == 0 && minor(dev) == 0); } -static void test_parse_errno(void) { +TEST(parse_errno) { assert_se(parse_errno("EILSEQ") == EILSEQ); assert_se(parse_errno("EINVAL") == EINVAL); assert_se(parse_errno("0") == 0); @@ -779,7 +780,7 @@ static void test_parse_errno(void) { assert_se(parse_errno("EINVALaaa") == -EINVAL); } -static void test_parse_mtu(void) { +TEST(parse_mtu) { uint32_t mtu = 0; assert_se(parse_mtu(AF_UNSPEC, "1500", &mtu) >= 0 && mtu == 1500); @@ -800,7 +801,7 @@ static void test_parse_mtu(void) { assert_se(parse_mtu(AF_UNSPEC, "", &mtu) == -EINVAL); } -static void test_parse_loadavg_fixed_point(void) { +TEST(parse_loadavg_fixed_point) { loadavg_t fp; assert_se(parse_loadavg_fixed_point("1.23", &fp) == 0); @@ -836,28 +837,4 @@ static void test_parse_loadavg_fixed_point(void) { assert_se(parse_loadavg_fixed_point("", &fp) == -EINVAL); } -int main(int argc, char *argv[]) { - log_parse_environment(); - log_open(); - - test_parse_boolean(); - test_parse_pid(); - test_parse_mode(); - test_parse_size(); - test_parse_range(); - test_safe_atolli(); - test_safe_atou16(); - test_safe_atoi16(); - test_safe_atoux16(); - test_safe_atou64(); - test_safe_atoi64(); - test_safe_atoux64(); - test_safe_atod(); - test_parse_nice(); - test_parse_dev(); - test_parse_errno(); - test_parse_mtu(); - test_parse_loadavg_fixed_point(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-path-lookup.c b/src/test/test-path-lookup.c index f0b716a4c5..a19a33c64b 100644 --- a/src/test/test-path-lookup.c +++ b/src/test/test-path-lookup.c @@ -10,7 +10,7 @@ #include "strv.h" #include "tests.h" -static void test_paths(UnitFileScope scope) { +static void test_paths_one(UnitFileScope scope) { char template[] = "/tmp/test-path-lookup.XXXXXXX"; _cleanup_(lookup_paths_free) LookupPaths lp_without_env = {}; @@ -35,7 +35,13 @@ static void test_paths(UnitFileScope scope) { assert_se(rm_rf(template, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0); } -static void test_user_and_global_paths(void) { +TEST(paths) { + test_paths_one(UNIT_FILE_SYSTEM); + test_paths_one(UNIT_FILE_USER); + test_paths_one(UNIT_FILE_GLOBAL); +} + +TEST(user_and_global_paths) { _cleanup_(lookup_paths_free) LookupPaths lp_global = {}, lp_user = {}; char **u, **g, **p; unsigned k = 0; @@ -53,7 +59,6 @@ static void test_user_and_global_paths(void) { * that they also exist in the user search path. Skip any * entries in user search path which don't exist in the global * one, but not vice versa. */ - log_info("/* %s */", __func__); STRV_FOREACH(p, g) { while (u[k] && !streq(*p, u[k])) { log_info("+ %s", u[k]); @@ -67,7 +72,7 @@ static void test_user_and_global_paths(void) { log_info("+ %s", *p); } -static void test_generator_binary_paths(UnitFileScope scope) { +static void test_generator_binary_paths_one(UnitFileScope scope) { char template[] = "/tmp/test-path-lookup.XXXXXXX"; _cleanup_strv_free_ char **gp_without_env = NULL; @@ -117,17 +122,9 @@ static void test_generator_binary_paths(UnitFileScope scope) { assert_se(strv_equal(env_gp_with_env, STRV_MAKE(systemd_env_generator_path))); } -int main(int argc, char **argv) { - test_setup_logging(LOG_DEBUG); - - test_paths(UNIT_FILE_SYSTEM); - test_paths(UNIT_FILE_USER); - test_paths(UNIT_FILE_GLOBAL); - - test_user_and_global_paths(); - - test_generator_binary_paths(UNIT_FILE_SYSTEM); - test_generator_binary_paths(UNIT_FILE_USER); - - return EXIT_SUCCESS; +TEST(generator_binary_paths) { + test_generator_binary_paths_one(UNIT_FILE_SYSTEM); + test_generator_binary_paths_one(UNIT_FILE_USER); } + +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-path-util.c b/src/test/test-path-util.c index b6ebe7f5fc..b9c4ef4126 100644 --- a/src/test/test-path-util.c +++ b/src/test/test-path-util.c @@ -18,14 +18,12 @@ #include "tmpfile-util.h" #include "util.h" -static void test_print_paths(void) { +TEST(print_paths) { log_info("DEFAULT_PATH=%s", DEFAULT_PATH); log_info("DEFAULT_USER_PATH=%s", DEFAULT_USER_PATH); } -static void test_path(void) { - log_info("/* %s */", __func__); - +TEST(path) { assert_se(path_is_absolute("/")); assert_se(!path_is_absolute("./")); @@ -65,12 +63,10 @@ static void test_path_simplify_one(const char *in, const char *out) { assert_se(streq(p, out)); } -static void test_path_simplify(void) { +TEST(path_simplify) { _cleanup_free_ char *hoge = NULL, *hoge_out = NULL; char foo[NAME_MAX * 2]; - log_info("/* %s */", __func__); - test_path_simplify_one("", ""); test_path_simplify_one("aaa/bbb////ccc", "aaa/bbb/ccc"); test_path_simplify_one("//aaa/.////ccc", "/aaa/ccc"); @@ -127,9 +123,7 @@ static void test_path_compare_one(const char *a, const char *b, int expected) { assert_se(path_equal(b, a) == (expected == 0)); } -static void test_path_compare(void) { - log_info("/* %s */", __func__); - +TEST(path_compare) { test_path_compare_one("/goo", "/goo", 0); test_path_compare_one("/goo", "/goo", 0); test_path_compare_one("//goo", "/goo", 0); @@ -157,11 +151,9 @@ static void test_path_compare(void) { test_path_compare_one("/foo/a/b", "/foo/aaa", -1); } -static void test_path_equal_root(void) { +TEST(path_equal_root) { /* Nail down the details of how path_equal("/", ...) works. */ - log_info("/* %s */", __func__); - assert_se(path_equal("/", "/")); assert_se(path_equal("/", "//")); @@ -201,14 +193,12 @@ static void test_path_equal_root(void) { assert_se(!path_equal_or_files_same("/", "/.../", AT_SYMLINK_NOFOLLOW)); } -static void test_find_executable_full(void) { +TEST(find_executable_full) { char *p; char* test_file_name; _cleanup_close_ int fd = -1; char fn[] = "/tmp/test-XXXXXX"; - log_info("/* %s */", __func__); - assert_se(find_executable_full("sh", NULL, NULL, true, &p, NULL) == 0); puts(p); assert_se(streq(basename(p), "sh")); @@ -253,17 +243,15 @@ static void test_find_executable_full(void) { assert_se(find_executable_full(test_file_name, NULL, STRV_MAKE("/doesnotexist", "/tmp", "/bin"), false, &p, NULL) == -ENOENT); } -static void test_find_executable(const char *self) { +TEST(find_executable) { char *p; - log_info("/* %s */", __func__); - assert_se(find_executable("/bin/sh", &p) == 0); puts(p); assert_se(path_equal(p, "/bin/sh")); free(p); - assert_se(find_executable(self, &p) == 0); + assert_se(find_executable(saved_argv[0], &p) == 0); puts(p); assert_se(endswith(p, "/test-path-util")); assert_se(path_is_absolute(p)); @@ -315,9 +303,7 @@ static void test_find_executable_exec_one(const char *path) { assert_se(wait_for_terminate_and_check(t, pid, WAIT_LOG) == 0); } -static void test_find_executable_exec(void) { - log_info("/* %s */", __func__); - +TEST(find_executable_exec) { test_find_executable_exec_one("touch"); test_find_executable_exec_one("/bin/touch"); @@ -326,7 +312,7 @@ static void test_find_executable_exec(void) { test_find_executable_exec_one(script); } -static void test_prefixes(void) { +TEST(prefixes) { static const char* const values[] = { "/a/b/c/d", "/a/b/c", @@ -339,8 +325,6 @@ static void test_prefixes(void) { char s[PATH_MAX]; bool b; - log_info("/* %s */", __func__); - i = 0; PATH_FOREACH_PREFIX_MORE(s, "/a/b/c/d") { log_error("---%s---", s); @@ -387,9 +371,7 @@ static void test_prefixes(void) { } } -static void test_path_join(void) { - log_info("/* %s */", __func__); - +TEST(path_join) { #define test_join(expected, ...) { \ _cleanup_free_ char *z = NULL; \ z = path_join(__VA_ARGS__); \ @@ -433,11 +415,9 @@ static void test_path_join(void) { test_join("//foo////bar////baz//", "//foo/", "///bar/", "///baz//"); } -static void test_path_extend(void) { +TEST(path_extend) { _cleanup_free_ char *p = NULL; - log_info("/* %s */", __func__); - assert_se(path_extend(&p, "foo", "bar", "baz") == p); assert_se(streq(p, "foo/bar/baz")); @@ -460,9 +440,7 @@ static void test_path_extend(void) { assert_se(streq(p, "/foo")); } -static void test_fsck_exists(void) { - log_info("/* %s */", __func__); - +TEST(fsck_exists) { /* Ensure we use a sane default for PATH. */ assert_se(unsetenv("PATH") == 0); @@ -484,9 +462,7 @@ static void test_path_make_relative_one(const char *from, const char *to, const assert_se(streq_ptr(z, expected)); } -static void test_make_relative(void) { - log_info("/* %s */", __func__); - +TEST(path_make_relative) { test_path_make_relative_one("some/relative/path", "/some/path", NULL); test_path_make_relative_one("/some/path", "some/relative/path", NULL); test_path_make_relative_one("/some/dotdot/../path", "/some/path", NULL); @@ -501,7 +477,7 @@ static void test_make_relative(void) { test_path_make_relative_one("//extra.//.//./.slashes//./won't////fo.ol///anybody//", "/././/extra././/.slashes////ar.e/.just/././.fine///", "../../../ar.e/.just/.fine"); } -static void test_strv_resolve(void) { +TEST(path_strv_resolve) { char tmp_dir[] = "/tmp/test-path-util-XXXXXX"; _cleanup_strv_free_ char **search_dirs = NULL; _cleanup_strv_free_ char **absolute_dirs = NULL; @@ -543,9 +519,7 @@ static void test_path_startswith_one(const char *path, const char *prefix, const } } -static void test_path_startswith(void) { - log_info("/* %s */", __func__); - +TEST(path_startswith) { test_path_startswith_one("/foo/bar/barfoo/", "/foo", "/foo/", "bar/barfoo/"); test_path_startswith_one("/foo/bar/barfoo/", "/foo/", "/foo/", "bar/barfoo/"); test_path_startswith_one("/foo/bar/barfoo/", "/", "/", "foo/bar/barfoo/"); @@ -579,9 +553,7 @@ static void test_prefix_root_one(const char *r, const char *p, const char *expec assert_se(path_equal_ptr(t, expected)); } -static void test_prefix_root(void) { - log_info("/* %s */", __func__); - +TEST(prefix_root) { test_prefix_root_one("/", "/foo", "/foo"); test_prefix_root_one(NULL, "/foo", "/foo"); test_prefix_root_one("", "/foo", "/foo"); @@ -600,11 +572,9 @@ static void test_prefix_root(void) { test_prefix_root_one("/foo///", "//bar", "/foo/bar"); } -static void test_file_in_same_dir(void) { +TEST(file_in_same_dir) { char *t; - log_info("/* %s */", __func__); - t = file_in_same_dir("/", "a"); assert_se(streq(t, "/a")); free(t); @@ -659,12 +629,10 @@ static void test_path_find_first_component_one( } } -static void test_path_find_first_component(void) { +TEST(path_find_first_component) { _cleanup_free_ char *hoge = NULL; char foo[NAME_MAX * 2]; - log_info("/* %s */", __func__); - test_path_find_first_component_one(NULL, false, NULL, 0); test_path_find_first_component_one("", false, NULL, 0); test_path_find_first_component_one("/", false, NULL, 0); @@ -740,12 +708,10 @@ static void test_path_find_last_component_one( } } -static void test_path_find_last_component(void) { +TEST(path_find_last_component) { _cleanup_free_ char *hoge = NULL; char foo[NAME_MAX * 2]; - log_info("/* %s */", __func__); - test_path_find_last_component_one(NULL, false, NULL, 0); test_path_find_last_component_one("", false, NULL, 0); test_path_find_last_component_one("/", false, NULL, 0); @@ -789,7 +755,7 @@ static void test_path_find_last_component(void) { test_path_find_last_component_one(hoge, true, STRV_MAKE("c", "b", "a"), -EINVAL); } -static void test_last_path_component(void) { +TEST(last_path_component) { assert_se(last_path_component(NULL) == NULL); assert_se(streq(last_path_component("a/b/c"), "c")); assert_se(streq(last_path_component("a/b/c/"), "c/")); @@ -822,9 +788,7 @@ static void test_path_extract_filename_one(const char *input, const char *output assert_se(r == ret); } -static void test_path_extract_filename(void) { - log_info("/* %s */", __func__); - +TEST(path_extract_filename) { test_path_extract_filename_one(NULL, NULL, -EINVAL); test_path_extract_filename_one("a/b/c", "c", 0); test_path_extract_filename_one("a/b/c/", "c", O_DIRECTORY); @@ -882,9 +846,7 @@ static void test_path_extract_directory_one(const char *input, const char *outpu } } -static void test_path_extract_directory(void) { - log_info("/* %s */", __func__); - +TEST(path_extract_directory) { test_path_extract_directory_one(NULL, NULL, -EINVAL); test_path_extract_directory_one("a/b/c", "a/b", 0); test_path_extract_directory_one("a/b/c/", "a/b", 0); @@ -915,11 +877,9 @@ static void test_path_extract_directory(void) { test_path_extract_directory_one("../", NULL, -EINVAL); } -static void test_filename_is_valid(void) { +TEST(filename_is_valid) { char foo[NAME_MAX+2]; - log_info("/* %s */", __func__); - assert_se(!filename_is_valid("")); assert_se(!filename_is_valid("/bar/foo")); assert_se(!filename_is_valid("/")); @@ -950,12 +910,10 @@ static void test_path_is_valid_and_safe_one(const char *p, bool ret) { assert_se(path_is_safe(p) == ret); } -static void test_path_is_valid_and_safe(void) { +TEST(path_is_valid_and_safe) { char foo[PATH_MAX+2]; const char *c; - log_info("/* %s */", __func__); - test_path_is_valid_and_safe_one("", false); test_path_is_valid_and_safe_one("/bar/foo", true); test_path_is_valid_and_safe_one("/bar/foo/", true); @@ -982,9 +940,7 @@ static void test_path_is_valid_and_safe(void) { test_path_is_valid_and_safe_one("o.o", true); } -static void test_hidden_or_backup_file(void) { - log_info("/* %s */", __func__); - +TEST(hidden_or_backup_file) { assert_se(hidden_or_backup_file(".hidden")); assert_se(hidden_or_backup_file("..hidden")); assert_se(!hidden_or_backup_file("hidden.")); @@ -1005,7 +961,7 @@ static void test_hidden_or_backup_file(void) { assert_se(!hidden_or_backup_file("test.dpkg-old.foo")); } -static void test_systemd_installation_has_version(const char *path) { +TEST(systemd_installation_has_version) { int r; const unsigned versions[] = {0, 231, PROJECT_VERSION, 999}; unsigned i; @@ -1013,16 +969,14 @@ static void test_systemd_installation_has_version(const char *path) { log_info("/* %s */", __func__); for (i = 0; i < ELEMENTSOF(versions); i++) { - r = systemd_installation_has_version(path, versions[i]); + r = systemd_installation_has_version(saved_argv[1], versions[i]); assert_se(r >= 0); log_info("%s has systemd >= %u: %s", - path ?: "Current installation", versions[i], yes_no(r)); + saved_argv[1] ?: "Current installation", versions[i], yes_no(r)); } } -static void test_skip_dev_prefix(void) { - log_info("/* %s */", __func__); - +TEST(skip_dev_prefix) { assert_se(streq(skip_dev_prefix("/"), "/")); assert_se(streq(skip_dev_prefix("/dev"), "")); assert_se(streq(skip_dev_prefix("/dev/"), "")); @@ -1036,9 +990,7 @@ static void test_skip_dev_prefix(void) { assert_se(streq(skip_dev_prefix("foo"), "foo")); } -static void test_empty_or_root(void) { - log_info("/* %s */", __func__); - +TEST(empty_or_root) { assert_se(empty_or_root(NULL)); assert_se(empty_or_root("")); assert_se(empty_or_root("/")); @@ -1051,9 +1003,7 @@ static void test_empty_or_root(void) { assert_se(!empty_or_root("//yy//")); } -static void test_path_startswith_set(void) { - log_info("/* %s */", __func__); - +TEST(path_startswith_set) { assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/bar", "/zzz"), "")); assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo/", "/zzz"), "bar")); assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo/bar", "/foo/quux", "/foo", "/zzz"), "bar")); @@ -1073,9 +1023,7 @@ static void test_path_startswith_set(void) { assert_se(streq_ptr(PATH_STARTSWITH_SET("/foo2/bar", "/foo/quux", "", "/zzz"), NULL)); } -static void test_path_startswith_strv(void) { - log_info("/* %s */", __func__); - +TEST(path_startswith_strv) { assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo/bar", "/zzz")), "")); assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo/", "/zzz")), "bar")); assert_se(streq_ptr(path_startswith_strv("/foo/bar", STRV_MAKE("/foo/quux", "/foo", "/zzz")), "bar")); @@ -1095,9 +1043,7 @@ static void test_path_startswith_strv(void) { assert_se(streq_ptr(path_startswith_strv("/foo2/bar", STRV_MAKE("/foo/quux", "", "/zzz")), NULL)); } -int main(int argc, char **argv) { - test_setup_logging(LOG_DEBUG); - +TEST(print_MAX) { log_info("PATH_MAX=%zu\n" "FILENAME_MAX=%zu\n" "NAME_MAX=%zu", @@ -1106,38 +1052,6 @@ int main(int argc, char **argv) { (size_t) NAME_MAX); assert_cc(FILENAME_MAX == PATH_MAX); - - test_print_paths(); - test_path(); - test_path_simplify(); - test_path_compare(); - test_path_equal_root(); - test_find_executable_full(); - test_find_executable(argv[0]); - test_find_executable_exec(); - test_prefixes(); - test_path_join(); - test_path_extend(); - test_fsck_exists(); - test_make_relative(); - test_strv_resolve(); - test_path_startswith(); - test_prefix_root(); - test_file_in_same_dir(); - test_path_find_first_component(); - test_path_find_last_component(); - test_last_path_component(); - test_path_extract_filename(); - test_path_extract_directory(); - test_filename_is_valid(); - test_path_is_valid_and_safe(); - test_hidden_or_backup_file(); - test_skip_dev_prefix(); - test_empty_or_root(); - test_path_startswith_set(); - test_path_startswith_strv(); - - test_systemd_installation_has_version(argv[1]); /* NULL is OK */ - - return 0; } + +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-percent-util.c b/src/test/test-percent-util.c index b8801438a7..7e8e11b49c 100644 --- a/src/test/test-percent-util.c +++ b/src/test/test-percent-util.c @@ -4,7 +4,7 @@ #include "tests.h" #include "time-util.h" -static void test_parse_percent(void) { +TEST(parse_percent) { assert_se(parse_percent("") == -EINVAL); assert_se(parse_percent("foo") == -EINVAL); assert_se(parse_percent("0") == -EINVAL); @@ -24,12 +24,12 @@ static void test_parse_percent(void) { assert_se(parse_percent("3.2%") == -EINVAL); } -static void test_parse_percent_unbounded(void) { +TEST(parse_percent_unbounded) { assert_se(parse_percent_unbounded("101%") == 101); assert_se(parse_percent_unbounded("400%") == 400); } -static void test_parse_permille(void) { +TEST(parse_permille) { assert_se(parse_permille("") == -EINVAL); assert_se(parse_permille("foo") == -EINVAL); assert_se(parse_permille("0") == -EINVAL); @@ -67,7 +67,7 @@ static void test_parse_permille(void) { assert_se(parse_permille("0.1%") == 1); } -static void test_parse_permille_unbounded(void) { +TEST(parse_permille_unbounded) { assert_se(parse_permille_unbounded("1001‰") == 1001); assert_se(parse_permille_unbounded("4000‰") == 4000); assert_se(parse_permille_unbounded("2147483647‰") == 2147483647); @@ -83,7 +83,7 @@ static void test_parse_permille_unbounded(void) { assert_se(parse_permille_unbounded("429496729.6%") == -ERANGE); } -static void test_parse_permyriad(void) { +TEST(parse_permyriad) { assert_se(parse_permyriad("") == -EINVAL); assert_se(parse_permyriad("foo") == -EINVAL); assert_se(parse_permyriad("0") == -EINVAL); @@ -128,7 +128,7 @@ static void test_parse_permyriad(void) { assert_se(parse_permyriad("3.212%") == -EINVAL); } -static void test_parse_permyriad_unbounded(void) { +TEST(parse_permyriad_unbounded) { assert_se(parse_permyriad_unbounded("1001‱") == 1001); assert_se(parse_permyriad_unbounded("4000‱") == 4000); assert_se(parse_permyriad_unbounded("2147483647‱") == 2147483647); @@ -151,7 +151,7 @@ static void test_parse_permyriad_unbounded(void) { assert_se(parse_permyriad_unbounded("42949672.96%") == -ERANGE); } -static void test_scale(void) { +TEST(scale) { /* Check some fixed values */ assert_se(UINT32_SCALE_FROM_PERCENT(0) == 0); assert_se(UINT32_SCALE_FROM_PERCENT(50) == UINT32_MAX/2+1); @@ -196,16 +196,4 @@ static void test_scale(void) { } } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_DEBUG); - - test_parse_percent(); - test_parse_percent_unbounded(); - test_parse_permille(); - test_parse_permille_unbounded(); - test_parse_permyriad(); - test_parse_permyriad_unbounded(); - test_scale(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-pretty-print.c b/src/test/test-pretty-print.c index 17d21ed109..22bb4e5239 100644 --- a/src/test/test-pretty-print.c +++ b/src/test/test-pretty-print.c @@ -11,7 +11,7 @@ #include "strv.h" #include "tests.h" -static void test_terminal_urlify(void) { +TEST(terminal_urlify) { _cleanup_free_ char *formatted = NULL; assert_se(terminal_urlify("https://www.freedesktop.org/wiki/Software/systemd/", "systemd homepage", &formatted) >= 0); @@ -23,7 +23,7 @@ static void test_terminal_urlify(void) { printf("Or click on %s to have a look at it!\n", formatted); } -static void test_cat_files(void) { +TEST(cat_files) { assert_se(cat_files("/no/such/file", NULL, 0) == -ENOENT); assert_se(cat_files("/no/such/file", NULL, CAT_FLAGS_MAIN_FILE_OPTIONAL) == 0); @@ -31,7 +31,7 @@ static void test_cat_files(void) { assert_se(cat_files("/etc/fstab", STRV_MAKE("/etc/fstab", "/etc/fstab"), 0) == 0); } -static void test_red_green_cross_check_mark(void) { +TEST(red_green_cross_check_mark) { bool b = false; printf("yea: <%s>\n", GREEN_CHECK_MARK()); @@ -44,14 +44,8 @@ static void test_red_green_cross_check_mark(void) { COLOR_MARK_BOOL(!!!b)); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_INFO); - - test_terminal_urlify(); - test_cat_files(); - test_red_green_cross_check_mark(); - +TEST(print_separator) { print_separator(); - - return 0; } + +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-prioq.c b/src/test/test-prioq.c index de902ca427..540863c2ff 100644 --- a/src/test/test-prioq.c +++ b/src/test/test-prioq.c @@ -7,6 +7,7 @@ #include "set.h" #include "siphash24.h" #include "sort-util.h" +#include "tests.h" #define SET_SIZE 1024*4 @@ -14,7 +15,7 @@ static int unsigned_compare(const unsigned *a, const unsigned *b) { return CMP(*a, *b); } -static void test_unsigned(void) { +TEST(unsigned) { _cleanup_(prioq_freep) Prioq *q = NULL; unsigned buffer[SET_SIZE], i, u, n; @@ -58,7 +59,7 @@ static void test_hash(const struct test *x, struct siphash *state) { DEFINE_PRIVATE_HASH_OPS(test_hash_ops, struct test, test_hash, test_compare); -static void test_struct(void) { +TEST(struct) { _cleanup_(prioq_freep) Prioq *q = NULL; _cleanup_set_free_ Set *s = NULL; unsigned previous = 0, i; @@ -119,10 +120,4 @@ static void test_struct(void) { assert_se(set_isempty(s)); } -int main(int argc, char* argv[]) { - - test_unsigned(); - test_struct(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-psi-util.c b/src/test/test-psi-util.c index d8d09118a6..ed465b807e 100644 --- a/src/test/test-psi-util.c +++ b/src/test/test-psi-util.c @@ -7,7 +7,7 @@ #include "psi-util.h" #include "tests.h" -static void test_read_mem_pressure(void) { +TEST(read_mem_pressure) { _cleanup_(unlink_tempfilep) char path[] = "/tmp/pressurereadtestXXXXXX"; ResourcePressure rp; @@ -72,8 +72,4 @@ static void test_read_mem_pressure(void) { assert_se(rp.total == 58464525); } -int main(void) { - test_setup_logging(LOG_DEBUG); - test_read_mem_pressure(); - return 0; -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-random-util.c b/src/test/test-random-util.c index 42d7a746da..2b09a4513a 100644 --- a/src/test/test-random-util.c +++ b/src/test/test-random-util.c @@ -9,10 +9,10 @@ #include "terminal-util.h" #include "tests.h" -static void test_genuine_random_bytes(RandomFlags flags) { +static void test_genuine_random_bytes_one(RandomFlags flags) { uint8_t buf[16] = {}; - log_info("/* %s */", __func__); + log_info("/* %s(%d) */", __func__, flags); for (size_t i = 1; i < sizeof buf; i++) { assert_se(genuine_random_bytes(buf, i, flags) == 0); @@ -23,10 +23,16 @@ static void test_genuine_random_bytes(RandomFlags flags) { } } -static void test_pseudo_random_bytes(void) { - uint8_t buf[16] = {}; +TEST(genuine_random_bytes) { + test_genuine_random_bytes_one(RANDOM_EXTEND_WITH_PSEUDO); + test_genuine_random_bytes_one(0); + test_genuine_random_bytes_one(RANDOM_BLOCK); + test_genuine_random_bytes_one(RANDOM_ALLOW_RDRAND); + test_genuine_random_bytes_one(RANDOM_ALLOW_INSECURE); +} - log_info("/* %s */", __func__); +TEST(pseudo_random_bytes) { + uint8_t buf[16] = {}; for (size_t i = 1; i < sizeof buf; i++) { pseudo_random_bytes(buf, i); @@ -37,11 +43,9 @@ static void test_pseudo_random_bytes(void) { } } -static void test_rdrand(void) { +TEST(rdrand) { int r; - log_info("/* %s */", __func__); - for (unsigned i = 0; i < 10; i++) { unsigned long x = 0; @@ -93,23 +97,9 @@ static void test_random_u64_range_one(unsigned mod) { } } -static void test_random_u64_range(void) { +TEST(random_u64_range) { for (unsigned mod = 1; mod < 29; mod++) test_random_u64_range_one(mod); } -int main(int argc, char **argv) { - test_setup_logging(LOG_DEBUG); - - test_genuine_random_bytes(RANDOM_EXTEND_WITH_PSEUDO); - test_genuine_random_bytes(0); - test_genuine_random_bytes(RANDOM_BLOCK); - test_genuine_random_bytes(RANDOM_ALLOW_RDRAND); - test_genuine_random_bytes(RANDOM_ALLOW_INSECURE); - - test_pseudo_random_bytes(); - test_rdrand(); - test_random_u64_range(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-ratelimit.c b/src/test/test-ratelimit.c index af60572e05..4797812107 100644 --- a/src/test/test-ratelimit.c +++ b/src/test/test-ratelimit.c @@ -4,9 +4,10 @@ #include "macro.h" #include "ratelimit.h" +#include "tests.h" #include "time-util.h" -static void test_ratelimit_below(void) { +TEST(ratelimit_below) { int i; RateLimit ratelimit = { 1 * USEC_PER_SEC, 10 }; @@ -22,8 +23,4 @@ static void test_ratelimit_below(void) { assert_se(ratelimit_below(&ratelimit)); } -int main(int argc, char *argv[]) { - test_ratelimit_below(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-replace-var.c b/src/test/test-replace-var.c index 4d699b92f0..f861b27c12 100644 --- a/src/test/test-replace-var.c +++ b/src/test/test-replace-var.c @@ -5,23 +5,28 @@ #include "macro.h" #include "replace-var.h" #include "string-util.h" +#include "tests.h" static char *lookup(const char *variable, void *userdata) { return strjoin("<<<", variable, ">>>"); } -int main(int argc, char *argv[]) { +TEST(replace_var) { char *r; assert_se(r = replace_var("@@@foobar@xyz@HALLO@foobar@test@@testtest@TEST@...@@@", lookup, NULL)); puts(r); assert_se(streq(r, "@@@foobar@xyz<<>>foobar@test@@testtest<<>>...@@@")); free(r); +} + +TEST(strreplace) { + char *r; assert_se(r = strreplace("XYZFFFFXYZFFFFXYZ", "XYZ", "ABC")); puts(r); assert_se(streq(r, "ABCFFFFABCFFFFABC")); free(r); - - return 0; } + +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-rm-rf.c b/src/test/test-rm-rf.c index 38aa100e0b..1eb9a49077 100644 --- a/src/test/test-rm-rf.c +++ b/src/test/test-rm-rf.c @@ -38,11 +38,9 @@ static void test_rm_rf_chmod_inner(void) { assert_se(access(d, F_OK) < 0 && errno == ENOENT); } -static void test_rm_rf_chmod(void) { +TEST(rm_rf_chmod) { int r; - log_info("/* %s */", __func__); - if (getuid() == 0) { /* This test only works unpriv (as only then the access mask for the owning user matters), * hence drop privs here */ @@ -65,10 +63,4 @@ static void test_rm_rf_chmod(void) { test_rm_rf_chmod_inner(); } -int main(int argc, char **argv) { - test_setup_logging(LOG_DEBUG); - - test_rm_rf_chmod(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-sd-path.c b/src/test/test-sd-path.c index 75436ab1ce..10a8a4a63f 100644 --- a/src/test/test-sd-path.c +++ b/src/test/test-sd-path.c @@ -7,9 +7,7 @@ #include "strv.h" #include "tests.h" -static void test_sd_path_lookup(void) { - log_info("/* %s */", __func__); - +TEST(sd_path_lookup) { for (uint64_t i = 0; i < _SD_PATH_MAX; i++) { _cleanup_free_ char *t = NULL, *s = NULL; int r; @@ -31,9 +29,7 @@ static void test_sd_path_lookup(void) { assert_se(sd_path_lookup(_SD_PATH_MAX, NULL, &tt) == -EOPNOTSUPP); } -static void test_sd_path_lookup_strv(void) { - log_info("/* %s */", __func__); - +TEST(sd_path_lookup_strv) { for (uint64_t i = 0; i < _SD_PATH_MAX; i++) { _cleanup_strv_free_ char **t = NULL, **s = NULL; char **item; @@ -61,9 +57,4 @@ static void test_sd_path_lookup_strv(void) { assert_se(sd_path_lookup(_SD_PATH_MAX, NULL, &tt) == -EOPNOTSUPP); } -int main(void) { - test_setup_logging(LOG_DEBUG); - - test_sd_path_lookup(); - test_sd_path_lookup_strv(); -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-seccomp.c b/src/test/test-seccomp.c index dc3088d4b0..7ccfeadbb8 100644 --- a/src/test/test-seccomp.c +++ b/src/test/test-seccomp.c @@ -46,7 +46,7 @@ static bool have_seccomp_privs(void) { return geteuid() == 0 && have_effective_cap(CAP_SYS_ADMIN) > 0; /* If we are root but CAP_SYS_ADMIN we can't do caps (unless we also do NNP) */ } -static void test_parse_syscall_and_errno(void) { +TEST(parse_syscall_and_errno) { _cleanup_free_ char *n = NULL; int e; @@ -104,12 +104,10 @@ static void test_parse_syscall_and_errno(void) { assert_se(parse_syscall_and_errno("hoge:", &n, &e) == -EINVAL); } -static void test_seccomp_arch_to_string(void) { +TEST(seccomp_arch_to_string) { uint32_t a, b; const char *name; - log_info("/* %s */", __func__); - a = seccomp_arch_native(); assert_se(a > 0); name = seccomp_arch_to_string(a); @@ -118,11 +116,9 @@ static void test_seccomp_arch_to_string(void) { assert_se(a == b); } -static void test_architecture_table(void) { +TEST(architecture_table) { const char *n, *n2; - log_info("/* %s */", __func__); - NULSTR_FOREACH(n, "native\0" "x86\0" @@ -153,9 +149,7 @@ static void test_architecture_table(void) { } } -static void test_syscall_filter_set_find(void) { - log_info("/* %s */", __func__); - +TEST(syscall_filter_set_find) { assert_se(!syscall_filter_set_find(NULL)); assert_se(!syscall_filter_set_find("")); assert_se(!syscall_filter_set_find("quux")); @@ -166,9 +160,7 @@ static void test_syscall_filter_set_find(void) { assert_se(syscall_filter_set_find("@raw-io") == syscall_filter_sets + SYSCALL_FILTER_SET_RAW_IO); } -static void test_filter_sets(void) { - log_info("/* %s */", __func__); - +TEST(filter_sets) { if (!is_seccomp_available()) { log_notice("Seccomp not available, skipping %s", __func__); return; @@ -230,9 +222,7 @@ static void test_filter_sets(void) { } } -static void test_filter_sets_ordered(void) { - log_info("/* %s */", __func__); - +TEST(filter_sets_ordered) { /* Ensure "@default" always remains at the beginning of the list */ assert_se(SYSCALL_FILTER_SET_DEFAULT == 0); assert_se(streq(syscall_filter_sets[0].name, "@default")); @@ -265,7 +255,7 @@ static void test_filter_sets_ordered(void) { } } -static void test_restrict_namespace(void) { +TEST(restrict_namespace) { char *s = NULL; unsigned long ul; pid_t pid; @@ -275,8 +265,6 @@ static void test_restrict_namespace(void) { return; } - log_info("/* %s */", __func__); - assert_se(namespace_flags_to_string(0, &s) == 0 && isempty(s)); s = mfree(s); assert_se(namespace_flags_to_string(CLONE_NEWNS, &s) == 0 && streq(s, "mnt")); @@ -368,12 +356,10 @@ static void test_restrict_namespace(void) { assert_se(wait_for_terminate_and_check("nsseccomp", pid, WAIT_LOG) == EXIT_SUCCESS); } -static void test_protect_sysctl(void) { +TEST(protect_sysctl) { pid_t pid; _cleanup_free_ char *seccomp = NULL; - log_info("/* %s */", __func__); - if (!is_seccomp_available()) { log_notice("Seccomp not available, skipping %s", __func__); return; @@ -422,11 +408,9 @@ static void test_protect_sysctl(void) { assert_se(wait_for_terminate_and_check("sysctlseccomp", pid, WAIT_LOG) == EXIT_SUCCESS); } -static void test_protect_syslog(void) { +TEST(protect_syslog) { pid_t pid; - log_info("/* %s */", __func__); - if (!is_seccomp_available()) { log_notice("Seccomp not available, skipping %s", __func__); return; @@ -464,11 +448,9 @@ static void test_protect_syslog(void) { assert_se(wait_for_terminate_and_check("syslogseccomp", pid, WAIT_LOG) == EXIT_SUCCESS); } -static void test_restrict_address_families(void) { +TEST(restrict_address_families) { pid_t pid; - log_info("/* %s */", __func__); - if (!is_seccomp_available()) { log_notice("Seccomp not available, skipping %s", __func__); return; @@ -553,11 +535,9 @@ static void test_restrict_address_families(void) { assert_se(wait_for_terminate_and_check("socketseccomp", pid, WAIT_LOG) == EXIT_SUCCESS); } -static void test_restrict_realtime(void) { +TEST(restrict_realtime) { pid_t pid; - log_info("/* %s */", __func__); - if (!is_seccomp_available()) { log_notice("Seccomp not available, skipping %s", __func__); return; @@ -600,11 +580,9 @@ static void test_restrict_realtime(void) { assert_se(wait_for_terminate_and_check("realtimeseccomp", pid, WAIT_LOG) == EXIT_SUCCESS); } -static void test_memory_deny_write_execute_mmap(void) { +TEST(memory_deny_write_execute_mmap) { pid_t pid; - log_info("/* %s */", __func__); - if (!is_seccomp_available()) { log_notice("Seccomp not available, skipping %s", __func__); return; @@ -660,13 +638,11 @@ static void test_memory_deny_write_execute_mmap(void) { assert_se(wait_for_terminate_and_check("memoryseccomp-mmap", pid, WAIT_LOG) == EXIT_SUCCESS); } -static void test_memory_deny_write_execute_shmat(void) { +TEST(memory_deny_write_execute_shmat) { int shmid; pid_t pid; uint32_t arch; - log_info("/* %s */", __func__); - SECCOMP_FOREACH_LOCAL_ARCH(arch) { log_debug("arch %s: SCMP_SYS(mmap) = %d", seccomp_arch_to_string(arch), SCMP_SYS(mmap)); log_debug("arch %s: SCMP_SYS(mmap2) = %d", seccomp_arch_to_string(arch), SCMP_SYS(mmap2)); @@ -735,11 +711,9 @@ static void test_memory_deny_write_execute_shmat(void) { assert_se(wait_for_terminate_and_check("memoryseccomp-shmat", pid, WAIT_LOG) == EXIT_SUCCESS); } -static void test_restrict_archs(void) { +TEST(restrict_archs) { pid_t pid; - log_info("/* %s */", __func__); - if (!is_seccomp_available()) { log_notice("Seccomp not available, skipping %s", __func__); return; @@ -775,11 +749,9 @@ static void test_restrict_archs(void) { assert_se(wait_for_terminate_and_check("archseccomp", pid, WAIT_LOG) == EXIT_SUCCESS); } -static void test_load_syscall_filter_set_raw(void) { +TEST(load_syscall_filter_set_raw) { pid_t pid; - log_info("/* %s */", __func__); - if (!is_seccomp_available()) { log_notice("Seccomp not available, skipping %s", __func__); return; @@ -890,11 +862,9 @@ static void test_load_syscall_filter_set_raw(void) { assert_se(wait_for_terminate_and_check("syscallrawseccomp", pid, WAIT_LOG) == EXIT_SUCCESS); } -static void test_native_syscalls_filtered(void) { +TEST(native_syscalls_filtered) { pid_t pid; - log_info("/* %s */", __func__); - if (!is_seccomp_available()) { log_notice("Seccomp not available, skipping %s", __func__); return; @@ -950,12 +920,10 @@ static void test_native_syscalls_filtered(void) { assert_se(wait_for_terminate_and_check("nativeseccomp", pid, WAIT_LOG) == EXIT_SUCCESS); } -static void test_lock_personality(void) { +TEST(lock_personality) { unsigned long current; pid_t pid; - log_info("/* %s */", __func__); - if (!is_seccomp_available()) { log_notice("Seccomp not available, skipping %s", __func__); return; @@ -1015,11 +983,9 @@ static int real_open(const char *path, int flags, mode_t mode) { #endif } -static void test_restrict_suid_sgid(void) { +TEST(restrict_suid_sgid) { pid_t pid; - log_info("/* %s */", __func__); - if (!is_seccomp_available()) { log_notice("Seccomp not available, skipping %s", __func__); return; @@ -1213,27 +1179,4 @@ static void test_restrict_suid_sgid(void) { assert_se(wait_for_terminate_and_check("suidsgidseccomp", pid, WAIT_LOG) == EXIT_SUCCESS); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_DEBUG); - - test_parse_syscall_and_errno(); - test_seccomp_arch_to_string(); - test_architecture_table(); - test_syscall_filter_set_find(); - test_filter_sets(); - test_filter_sets_ordered(); - test_restrict_namespace(); - test_protect_sysctl(); - test_protect_syslog(); - test_restrict_address_families(); - test_restrict_realtime(); - test_memory_deny_write_execute_mmap(); - test_memory_deny_write_execute_shmat(); - test_restrict_archs(); - test_load_syscall_filter_set_raw(); - test_native_syscalls_filtered(); - test_lock_personality(); - test_restrict_suid_sgid(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-set-disable-mempool.c b/src/test/test-set-disable-mempool.c index ae36fac102..f02e433d7b 100644 --- a/src/test/test-set-disable-mempool.c +++ b/src/test/test-set-disable-mempool.c @@ -41,13 +41,11 @@ static void test_one(const char *val) { assert_se(!s); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_DEBUG); - +TEST(disable_mempool) { test_one("0"); /* The value $SYSTEMD_MEMPOOL= is cached. So the following * test should also succeed. */ test_one("1"); - - return 0; } + +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-set.c b/src/test/test-set.c index e63137192c..4dd98ef4f8 100644 --- a/src/test/test-set.c +++ b/src/test/test-set.c @@ -3,10 +3,11 @@ #include "random-util.h" #include "set.h" #include "strv.h" +#include "tests.h" const bool mempool_use_allowed = VALGRIND; -static void test_set_steal_first(void) { +TEST(set_steal_first) { _cleanup_set_free_ Set *m = NULL; int seen[3] = {}; char *val; @@ -33,7 +34,7 @@ static void item_seen(Item *item) { item->seen++; } -static void test_set_free_with_destructor(void) { +TEST(set_free_with_destructor) { Set *m; struct Item items[4] = {}; unsigned i; @@ -51,7 +52,7 @@ static void test_set_free_with_destructor(void) { DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(item_hash_ops, void, trivial_hash_func, trivial_compare_func, Item, item_seen); -static void test_set_free_with_hash_ops(void) { +TEST(set_free_with_hash_ops) { Set *m; struct Item items[4] = {}; unsigned i; @@ -67,7 +68,7 @@ static void test_set_free_with_hash_ops(void) { assert_se(items[3].seen == 0); } -static void test_set_put(void) { +TEST(set_put) { _cleanup_set_free_ Set *m = NULL; m = set_new(&string_hash_ops); @@ -89,7 +90,7 @@ static void test_set_put(void) { assert_se(strv_length(t) == 3); } -static void test_set_put_strdup(void) { +TEST(set_put_strdup) { _cleanup_set_free_ Set *m = NULL; assert_se(set_put_strdup(&m, "aaa") == 1); @@ -100,7 +101,7 @@ static void test_set_put_strdup(void) { assert_se(set_size(m) == 2); } -static void test_set_put_strdupv(void) { +TEST(set_put_strdupv) { _cleanup_set_free_ Set *m = NULL; assert_se(set_put_strdupv(&m, STRV_MAKE("aaa", "aaa", "bbb", "bbb", "aaa")) == 2); @@ -108,7 +109,7 @@ static void test_set_put_strdupv(void) { assert_se(set_size(m) == 3); } -static void test_set_ensure_allocated(void) { +TEST(set_ensure_allocated) { _cleanup_set_free_ Set *m = NULL; assert_se(set_ensure_allocated(&m, &string_hash_ops) == 1); @@ -117,12 +118,10 @@ static void test_set_ensure_allocated(void) { assert_se(set_size(m) == 0); } -static void test_set_copy(void) { +TEST(set_copy) { Set *s, *copy; char *key1, *key2, *key3, *key4; - log_info("/* %s */", __func__); - key1 = strdup("key1"); assert_se(key1); key2 = strdup("key2"); @@ -149,7 +148,7 @@ static void test_set_copy(void) { set_free_free(copy); } -static void test_set_ensure_put(void) { +TEST(set_ensure_put) { _cleanup_set_free_ Set *m = NULL; assert_se(set_ensure_put(&m, &string_hash_ops, "a") == 1); @@ -161,7 +160,7 @@ static void test_set_ensure_put(void) { assert_se(set_size(m) == 2); } -static void test_set_ensure_consume(void) { +TEST(set_ensure_consume) { _cleanup_set_free_ Set *m = NULL; char *s, *t; @@ -183,7 +182,7 @@ static void test_set_ensure_consume(void) { assert_se(set_size(m) == 2); } -static void test_set_strjoin(void) { +TEST(set_strjoin) { _cleanup_set_free_ Set *m = NULL; _cleanup_free_ char *joined = NULL; @@ -260,7 +259,7 @@ static void test_set_strjoin(void) { assert_se(STR_IN_SET(joined, "xxxaaaxxxbbbxxx", "xxxbbbxxxaaaxxx")); } -static void test_set_equal(void) { +TEST(set_equal) { _cleanup_set_free_ Set *a = NULL, *b = NULL; void *p; int r; @@ -331,19 +330,4 @@ static void test_set_equal(void) { assert_se(set_equal(b, a)); } -int main(int argc, const char *argv[]) { - test_set_steal_first(); - test_set_free_with_destructor(); - test_set_free_with_hash_ops(); - test_set_put(); - test_set_put_strdup(); - test_set_put_strdupv(); - test_set_ensure_allocated(); - test_set_ensure_put(); - test_set_ensure_consume(); - test_set_strjoin(); - test_set_equal(); - test_set_copy(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-signal-util.c b/src/test/test-signal-util.c index 1e08b7a062..335066a374 100644 --- a/src/test/test-signal-util.c +++ b/src/test/test-signal-util.c @@ -7,11 +7,12 @@ #include "signal-util.h" #include "stdio-util.h" #include "string-util.h" +#include "tests.h" #include "process-util.h" #define info(sig) log_info(#sig " = " STRINGIFY(sig) " = %d", sig) -static void test_rt_signals(void) { +TEST(rt_signals) { info(SIGRTMIN); info(SIGRTMAX); @@ -48,7 +49,7 @@ static void test_signal_from_string_number(const char *s, int val) { assert_se(signal_from_string(p) == -EINVAL); } -static void test_signal_from_string(void) { +TEST(signal_from_string) { char buf[STRLEN("RTMIN+") + DECIMAL_STR_MAX(int) + 1]; test_signal_to_string_one(SIGHUP); @@ -104,7 +105,7 @@ static void test_signal_from_string(void) { test_signal_from_string_number("-2", -ERANGE); } -static void test_block_signals(void) { +TEST(block_signals) { assert_se(signal_is_blocked(SIGUSR1) == 0); assert_se(signal_is_blocked(SIGALRM) == 0); assert_se(signal_is_blocked(SIGVTALRM) == 0); @@ -122,7 +123,7 @@ static void test_block_signals(void) { assert_se(signal_is_blocked(SIGVTALRM) == 0); } -static void test_ignore_signals(void) { +TEST(ignore_signals) { assert_se(ignore_signals(SIGINT) >= 0); assert_se(kill(getpid_cached(), SIGINT) >= 0); assert_se(ignore_signals(SIGUSR1, SIGUSR2, SIGTERM, SIGPIPE) >= 0); @@ -133,7 +134,7 @@ static void test_ignore_signals(void) { assert_se(default_signals(SIGINT, SIGUSR1, SIGUSR2, SIGTERM, SIGPIPE) >= 0); } -static void test_pop_pending_signal(void) { +TEST(pop_pending_signal) { assert_se(signal_is_blocked(SIGUSR1) == 0); assert_se(signal_is_blocked(SIGUSR2) == 0); @@ -171,12 +172,4 @@ static void test_pop_pending_signal(void) { assert_se(pop_pending_signal(SIGUSR2) == 0); } -int main(int argc, char *argv[]) { - test_rt_signals(); - test_signal_from_string(); - test_block_signals(); - test_ignore_signals(); - test_pop_pending_signal(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-siphash24.c b/src/test/test-siphash24.c index 4a1672fb95..de91eb28fc 100644 --- a/src/test/test-siphash24.c +++ b/src/test/test-siphash24.c @@ -2,10 +2,11 @@ #include "memory-util.h" #include "siphash24.h" +#include "tests.h" #define ITERATIONS 10000000ULL -static void do_test(const uint8_t *in, size_t len, const uint8_t *key) { +static void test_alignment_one(const uint8_t *in, size_t len, const uint8_t *key) { struct siphash state = {}; uint64_t out; unsigned i, j; @@ -45,7 +46,25 @@ static void do_test(const uint8_t *in, size_t len, const uint8_t *key) { } } -static void test_short_hashes(void) { +TEST(alignment) { + const uint8_t in[15] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e }; + const uint8_t key[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}; + uint8_t in_buf[20]; + + /* Test with same input but different alignments. */ + memcpy(in_buf, in, sizeof(in)); + test_alignment_one(in_buf, sizeof(in), key); + memcpy(in_buf + 1, in, sizeof(in)); + test_alignment_one(in_buf + 1, sizeof(in), key); + memcpy(in_buf + 2, in, sizeof(in)); + test_alignment_one(in_buf + 2, sizeof(in), key); + memcpy(in_buf + 4, in, sizeof(in)); + test_alignment_one(in_buf + 4, sizeof(in), key); +} + +TEST(short_hashes) { const uint8_t one[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }; const uint8_t key[16] = { 0x22, 0x24, 0x41, 0x22, 0x55, 0x77, 0x88, 0x07, @@ -86,22 +105,4 @@ static void test_short_hashes(void) { } /* see https://131002.net/siphash/siphash.pdf, Appendix A */ -int main(int argc, char *argv[]) { - const uint8_t in[15] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e }; - const uint8_t key[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}; - uint8_t in_buf[20]; - - /* Test with same input but different alignments. */ - memcpy(in_buf, in, sizeof(in)); - do_test(in_buf, sizeof(in), key); - memcpy(in_buf + 1, in, sizeof(in)); - do_test(in_buf + 1, sizeof(in), key); - memcpy(in_buf + 2, in, sizeof(in)); - do_test(in_buf + 2, sizeof(in), key); - memcpy(in_buf + 4, in, sizeof(in)); - do_test(in_buf + 4, sizeof(in), key); - - test_short_hashes(); -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-socket-netlink.c b/src/test/test-socket-netlink.c index c7d039af55..6dbd50f4a1 100644 --- a/src/test/test-socket-netlink.c +++ b/src/test/test-socket-netlink.c @@ -33,9 +33,7 @@ static void test_socket_address_parse_one(const char *in, int ret, int family, c } } -static void test_socket_address_parse(void) { - log_info("/* %s */", __func__); - +TEST(socket_address_parse) { test_socket_address_parse_one("junk", -EINVAL, 0, NULL); test_socket_address_parse_one("192.168.1.1", -EINVAL, 0, NULL); test_socket_address_parse_one(".168.1.1", -EINVAL, 0, NULL); @@ -100,11 +98,9 @@ static void test_socket_address_parse(void) { test_socket_address_parse_one("vsock:2", -EINVAL, 0, NULL); } -static void test_socket_address_parse_netlink(void) { +TEST(socket_address_parse_netlink) { SocketAddress a; - log_info("/* %s */", __func__); - assert_se(socket_address_parse_netlink(&a, "junk") < 0); assert_se(socket_address_parse_netlink(&a, "") < 0); @@ -142,11 +138,9 @@ static void test_socket_address_parse_netlink(void) { assert_se(socket_address_parse_netlink(&a, "\xff") < 0); } -static void test_socket_address_equal(void) { +TEST(socket_address_equal) { SocketAddress a, b; - log_info("/* %s */", __func__); - assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0); assert_se(socket_address_parse(&b, "192.168.1.1:888") >= 0); assert_se(!socket_address_equal(&a, &b)); @@ -192,11 +186,9 @@ static void test_socket_address_equal(void) { assert_se(!socket_address_equal(&a, &b)); } -static void test_socket_address_get_path(void) { +TEST(socket_address_get_path) { SocketAddress a; - log_info("/* %s */", __func__); - assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0); assert_se(!socket_address_get_path(&a)); @@ -213,11 +205,9 @@ static void test_socket_address_get_path(void) { assert_se(!socket_address_get_path(&a)); } -static void test_socket_address_is(void) { +TEST(socket_address_is) { SocketAddress a; - log_info("/* %s */", __func__); - assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0); assert_se( socket_address_is(&a, "192.168.1.1:8888", 0 /* unspecified yet */)); assert_se(!socket_address_is(&a, "route", 0)); @@ -228,11 +218,9 @@ static void test_socket_address_is(void) { assert_se( socket_address_is(&a, "192.168.1.1:8888", SOCK_STREAM)); } -static void test_socket_address_is_netlink(void) { +TEST(socket_address_is_netlink) { SocketAddress a; - log_info("/* %s */", __func__); - assert_se(socket_address_parse_netlink(&a, "route 10") >= 0); assert_se( socket_address_is_netlink(&a, "route 10")); assert_se(!socket_address_is_netlink(&a, "192.168.1.1:8888")); @@ -255,9 +243,7 @@ static void test_in_addr_ifindex_to_string_one(int f, const char *a, int ifindex assert_se(ifindex2 == ifindex || ifindex2 == 0); } -static void test_in_addr_ifindex_to_string(void) { - log_info("/* %s */", __func__); - +TEST(in_addr_ifindex_to_string) { test_in_addr_ifindex_to_string_one(AF_INET, "192.168.0.1", 7, "192.168.0.1"); test_in_addr_ifindex_to_string_one(AF_INET, "10.11.12.13", 9, "10.11.12.13"); test_in_addr_ifindex_to_string_one(AF_INET6, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 10, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); @@ -269,11 +255,10 @@ static void test_in_addr_ifindex_to_string(void) { test_in_addr_ifindex_to_string_one(AF_INET6, "fe80::16", LOOPBACK_IFINDEX, "fe80::16%1"); } -static void test_in_addr_ifindex_from_string_auto(void) { +TEST(in_addr_ifindex_from_string_auto) { int family, ifindex; union in_addr_union ua; - log_info("/* %s */", __func__); /* Most in_addr_ifindex_from_string_auto() invocations have already been tested above, but let's test some more */ assert_se(in_addr_ifindex_from_string_auto("fe80::17", &family, &ua, &ifindex) >= 0); @@ -300,9 +285,7 @@ static void test_in_addr_ifindex_name_from_string_auto_one(const char *a, const assert_se(streq_ptr(server_name, expected)); } -static void test_in_addr_ifindex_name_from_string_auto(void) { - log_info("/* %s */", __func__); - +TEST(in_addr_ifindex_name_from_string_auto) { test_in_addr_ifindex_name_from_string_auto_one("192.168.0.1", NULL); test_in_addr_ifindex_name_from_string_auto_one("192.168.0.1#test.com", "test.com"); test_in_addr_ifindex_name_from_string_auto_one("fe80::18%19", NULL); @@ -366,9 +349,7 @@ static void test_in_addr_port_ifindex_name_from_string_auto_one(const char *str, } } -static void test_in_addr_port_ifindex_name_from_string_auto(void) { - log_info("/* %s */", __func__); - +TEST(in_addr_port_ifindex_name_from_string_auto) { test_in_addr_port_ifindex_name_from_string_auto_one("192.168.0.1", AF_INET, 0, 0, NULL, NULL); test_in_addr_port_ifindex_name_from_string_auto_one("192.168.0.1#test.com", AF_INET, 0, 0, "test.com", NULL); test_in_addr_port_ifindex_name_from_string_auto_one("192.168.0.1:53", AF_INET, 53, 0, NULL, NULL); @@ -388,20 +369,4 @@ static void test_in_addr_port_ifindex_name_from_string_auto(void) { test_in_addr_port_ifindex_name_from_string_auto_one("[fe80::18]:53%lo#hoge.com", AF_INET6, 53, 1, "hoge.com", "[fe80::18]:53%1#hoge.com"); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_DEBUG); - - test_socket_address_parse(); - test_socket_address_parse_netlink(); - test_socket_address_equal(); - test_socket_address_get_path(); - test_socket_address_is(); - test_socket_address_is_netlink(); - - test_in_addr_ifindex_to_string(); - test_in_addr_ifindex_from_string_auto(); - test_in_addr_ifindex_name_from_string_auto(); - test_in_addr_port_ifindex_name_from_string_auto(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-socket-util.c b/src/test/test-socket-util.c index f9b22c7e11..9ee651a5fa 100644 --- a/src/test/test-socket-util.c +++ b/src/test/test-socket-util.c @@ -23,9 +23,7 @@ assert_cc(SUN_PATH_LEN == 108); -static void test_ifname_valid(void) { - log_info("/* %s */", __func__); - +TEST(ifname_valid) { assert_se( ifname_valid("foo")); assert_se( ifname_valid("eth0")); @@ -69,9 +67,7 @@ static void test_socket_print_unix_one(const char *in, size_t len_in, const char assert_se(streq(out, expected)); } -static void test_socket_print_unix(void) { - log_info("/* %s */", __func__); - +TEST(socket_print_unix) { /* Some additional tests for abstract addresses which we don't parse */ test_socket_print_unix_one("\0\0\0\0", 4, "@\\000\\000\\000"); @@ -86,7 +82,7 @@ static void test_socket_print_unix(void) { test_socket_print_unix_one("\0\a\b\n\255", 6, "@\\a\\b\\n\\255\\000"); } -static void test_sockaddr_equal(void) { +TEST(sockaddr_equal) { union sockaddr_union a = { .in.sin_family = AF_INET, .in.sin_port = 0, @@ -113,8 +109,6 @@ static void test_sockaddr_equal(void) { .vm.svm_cid = VMADDR_CID_ANY, }; - log_info("/* %s */", __func__); - assert_se(sockaddr_equal(&a, &a)); assert_se(sockaddr_equal(&a, &b)); assert_se(sockaddr_equal(&d, &d)); @@ -124,9 +118,7 @@ static void test_sockaddr_equal(void) { assert_se(!sockaddr_equal(&a, &e)); } -static void test_sockaddr_un_len(void) { - log_info("/* %s */", __func__); - +TEST(sockaddr_un_len) { static const struct sockaddr_un fs = { .sun_family = AF_UNIX, .sun_path = "/foo/bar/waldo", @@ -141,12 +133,10 @@ static void test_sockaddr_un_len(void) { assert_se(SOCKADDR_UN_LEN(abstract) == offsetof(struct sockaddr_un, sun_path) + 1 + strlen(abstract.sun_path + 1)); } -static void test_in_addr_is_multicast(void) { +TEST(in_addr_is_multicast) { union in_addr_union a, b; int f; - log_info("/* %s */", __func__); - assert_se(in_addr_from_string_auto("192.168.3.11", &f, &a) >= 0); assert_se(in_addr_is_multicast(f, &a) == 0); @@ -160,11 +150,9 @@ static void test_in_addr_is_multicast(void) { assert_se(in_addr_is_multicast(f, &b) == 0); } -static void test_getpeercred_getpeergroups(void) { +TEST(getpeercred_getpeergroups) { int r; - log_info("/* %s */", __func__); - r = safe_fork("(getpeercred)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL); assert_se(r >= 0); @@ -228,13 +216,11 @@ static void test_getpeercred_getpeergroups(void) { } } -static void test_passfd_read(void) { +TEST(passfd_read) { static const char file_contents[] = "test contents for passfd"; _cleanup_close_pair_ int pair[2] = { -1, -1 }; int r; - log_info("/* %s */", __func__); - assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0); r = safe_fork("(passfd_read)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL); @@ -276,14 +262,12 @@ static void test_passfd_read(void) { assert_se(streq(buf, file_contents)); } -static void test_passfd_contents_read(void) { +TEST(passfd_contents_read) { _cleanup_close_pair_ int pair[2] = { -1, -1 }; static const char file_contents[] = "test contents in the file"; static const char wire_contents[] = "test contents on the wire"; int r; - log_info("/* %s */", __func__); - assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0); r = safe_fork("(passfd_contents_read)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL); @@ -330,13 +314,11 @@ static void test_passfd_contents_read(void) { assert_se(streq(buf, file_contents)); } -static void test_receive_nopassfd(void) { +TEST(receive_nopassfd) { _cleanup_close_pair_ int pair[2] = { -1, -1 }; static const char wire_contents[] = "no fd passed here"; int r; - log_info("/* %s */", __func__); - assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0); r = safe_fork("(receive_nopassfd)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL); @@ -369,12 +351,10 @@ static void test_receive_nopassfd(void) { assert_se(fd == -1); } -static void test_send_nodata_nofd(void) { +TEST(send_nodata_nofd) { _cleanup_close_pair_ int pair[2] = { -1, -1 }; int r; - log_info("/* %s */", __func__); - assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0); r = safe_fork("(send_nodata_nofd)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL); @@ -404,12 +384,10 @@ static void test_send_nodata_nofd(void) { assert_se(fd == -999); } -static void test_send_emptydata(void) { +TEST(send_emptydata) { _cleanup_close_pair_ int pair[2] = { -1, -1 }; int r; - log_info("/* %s */", __func__); - assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) >= 0); r = safe_fork("(send_emptydata)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL); @@ -443,7 +421,7 @@ static void test_send_emptydata(void) { assert_se(fd == -999); } -static void test_flush_accept(void) { +TEST(flush_accept) { _cleanup_close_ int listen_stream = -1, listen_dgram = -1, listen_seqpacket = 1, connect_stream = -1, connect_dgram = -1, connect_seqpacket = -1; static const union sockaddr_union sa = { .un.sun_family = AF_UNIX }; union sockaddr_union lsa; @@ -504,27 +482,9 @@ static void test_flush_accept(void) { assert_se(flush_accept(listen_seqpacket) >= 0); } -static void test_ipv6_enabled(void) { +TEST(ipv6_enabled) { log_info("IPv6 supported: %s", yes_no(socket_ipv6_is_supported())); log_info("IPv6 enabled: %s", yes_no(socket_ipv6_is_enabled())); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_DEBUG); - - test_ifname_valid(); - test_socket_print_unix(); - test_sockaddr_equal(); - test_sockaddr_un_len(); - test_in_addr_is_multicast(); - test_getpeercred_getpeergroups(); - test_passfd_read(); - test_passfd_contents_read(); - test_receive_nopassfd(); - test_send_nodata_nofd(); - test_send_emptydata(); - test_flush_accept(); - test_ipv6_enabled(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-specifier.c b/src/test/test-specifier.c index 853943a996..40957eeb59 100644 --- a/src/test/test-specifier.c +++ b/src/test/test-specifier.c @@ -15,9 +15,7 @@ static void test_specifier_escape_one(const char *a, const char *b) { assert_se(streq_ptr(x, b)); } -static void test_specifier_escape(void) { - log_info("/* %s */", __func__); - +TEST(specifier_escape) { test_specifier_escape_one(NULL, NULL); test_specifier_escape_one("", ""); test_specifier_escape_one("%", "%%"); @@ -33,9 +31,7 @@ static void test_specifier_escape_strv_one(char **a, char **b) { assert_se(strv_equal(x, b)); } -static void test_specifier_escape_strv(void) { - log_info("/* %s */", __func__); - +TEST(specifier_escape_strv) { test_specifier_escape_strv_one(NULL, NULL); test_specifier_escape_strv_one(STRV_MAKE(NULL), STRV_MAKE(NULL)); test_specifier_escape_strv_one(STRV_MAKE(""), STRV_MAKE("")); @@ -56,7 +52,7 @@ static const Specifier specifier_table[] = { {} }; -static void test_specifier_printf(void) { +TEST(specifier_printf) { static const Specifier table[] = { { 'X', specifier_string, (char*) "AAAA" }, { 'Y', specifier_string, (char*) "BBBB" }, @@ -67,8 +63,6 @@ static void test_specifier_printf(void) { _cleanup_free_ char *w = NULL; int r; - log_info("/* %s */", __func__); - r = specifier_printf("xxx a=%X b=%Y yyy", SIZE_MAX, table, NULL, NULL, &w); assert_se(r >= 0); assert_se(w); @@ -88,9 +82,7 @@ static void test_specifier_printf(void) { puts(w); } -static void test_specifiers(void) { - log_info("/* %s */", __func__); - +TEST(specifiers) { for (const Specifier *s = specifier_table; s->specifier; s++) { char spec[3]; _cleanup_free_ char *resolved = NULL; @@ -103,13 +95,4 @@ static void test_specifiers(void) { } } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_DEBUG); - - test_specifier_escape(); - test_specifier_escape_strv(); - test_specifier_printf(); - test_specifiers(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-stat-util.c b/src/test/test-stat-util.c index da90a98823..0f7b3ca3ce 100644 --- a/src/test/test-stat-util.c +++ b/src/test/test-stat-util.c @@ -18,13 +18,11 @@ #include "tests.h" #include "tmpfile-util.h" -static void test_files_same(void) { +TEST(files_same) { _cleanup_close_ int fd = -1; char name[] = "/tmp/test-files_same.XXXXXX"; char name_alias[] = "/tmp/test-files_same.alias"; - log_info("/* %s */", __func__); - fd = mkostemp_safe(name); assert_se(fd >= 0); assert_se(symlink(name, name_alias) >= 0); @@ -38,13 +36,11 @@ static void test_files_same(void) { unlink(name_alias); } -static void test_is_symlink(void) { +TEST(is_symlink) { char name[] = "/tmp/test-is_symlink.XXXXXX"; char name_link[] = "/tmp/test-is_symlink.link"; _cleanup_close_ int fd = -1; - log_info("/* %s */", __func__); - fd = mkostemp_safe(name); assert_se(fd >= 0); assert_se(symlink(name, name_link) >= 0); @@ -57,9 +53,7 @@ static void test_is_symlink(void) { unlink(name_link); } -static void test_path_is_fs_type(void) { - log_info("/* %s */", __func__); - +TEST(path_is_fs_type) { /* run might not be a mount point in build chroots */ if (path_is_mount_point("/run", NULL, AT_SYMLINK_FOLLOW) > 0) { assert_se(path_is_fs_type("/run", TMPFS_MAGIC) > 0); @@ -72,12 +66,10 @@ static void test_path_is_fs_type(void) { assert_se(path_is_fs_type("/i-dont-exist", BTRFS_SUPER_MAGIC) == -ENOENT); } -static void test_path_is_temporary_fs(void) { +TEST(path_is_temporary_fs) { const char *s; int r; - log_info("/* %s */", __func__); - FOREACH_STRING(s, "/", "/run", "/sys", "/sys/", "/proc", "/i-dont-exist", "/var", "/var/lib") { r = path_is_temporary_fs(s); @@ -92,12 +84,10 @@ static void test_path_is_temporary_fs(void) { assert_se(path_is_temporary_fs("/i-dont-exist") == -ENOENT); } -static void test_path_is_read_only_fs(void) { +TEST(path_is_read_only_fs) { const char *s; int r; - log_info("/* %s */", __func__); - FOREACH_STRING(s, "/", "/run", "/sys", "/sys/", "/proc", "/i-dont-exist", "/var", "/var/lib") { r = path_is_read_only_fs(s); @@ -112,11 +102,9 @@ static void test_path_is_read_only_fs(void) { assert_se(path_is_read_only_fs("/i-dont-exist") == -ENOENT); } -static void test_fd_is_ns(void) { +TEST(fd_is_ns) { _cleanup_close_ int fd = -1; - log_info("/* %s */", __func__); - assert_se(fd_is_ns(STDIN_FILENO, CLONE_NEWNET) == 0); assert_se(fd_is_ns(STDERR_FILENO, CLONE_NEWNET) == 0); assert_se(fd_is_ns(STDOUT_FILENO, CLONE_NEWNET) == 0); @@ -139,9 +127,7 @@ static void test_fd_is_ns(void) { assert_se(IN_SET(fd_is_ns(fd, CLONE_NEWNET), 1, -EUCLEAN)); } -static void test_device_major_minor_valid(void) { - log_info("/* %s */", __func__); - +TEST(device_major_minor_valid) { /* on glibc dev_t is 64bit, even though in the kernel it is only 32bit */ assert_cc(sizeof(dev_t) == sizeof(uint64_t)); @@ -209,9 +195,7 @@ static void test_device_path_make_canonical_one(const char *path) { assert_se((st.st_mode & S_IFMT) == (mode & S_IFMT)); } -static void test_device_path_make_canonical(void) { - log_info("/* %s */", __func__); - +TEST(device_path_make_canonical) { test_device_path_make_canonical_one("/dev/null"); test_device_path_make_canonical_one("/dev/zero"); test_device_path_make_canonical_one("/dev/full"); @@ -225,12 +209,10 @@ static void test_device_path_make_canonical(void) { } } -static void test_dir_is_empty(void) { +TEST(dir_is_empty) { _cleanup_(rm_rf_physical_and_freep) char *empty_dir = NULL; _cleanup_free_ char *j = NULL, *jj = NULL; - log_info("/* %s */", __func__); - assert_se(dir_is_empty_at(AT_FDCWD, "/proc") == 0); assert_se(dir_is_empty_at(AT_FDCWD, "/icertainlydontexistdoi") == -ENOENT); @@ -254,19 +236,4 @@ static void test_dir_is_empty(void) { assert_se(dir_is_empty_at(AT_FDCWD, empty_dir) > 0); } -int main(int argc, char *argv[]) { - log_show_color(true); - test_setup_logging(LOG_INFO); - - test_files_same(); - test_is_symlink(); - test_path_is_fs_type(); - test_path_is_temporary_fs(); - test_path_is_read_only_fs(); - test_fd_is_ns(); - test_device_major_minor_valid(); - test_device_path_make_canonical(); - test_dir_is_empty(); - - return 0; -} +DEFINE_CUSTOM_TEST_MAIN(LOG_INFO, log_show_color(true), /* no outro */); diff --git a/src/test/test-static-destruct.c b/src/test/test-static-destruct.c index 904a9bbf5d..cb518ea362 100644 --- a/src/test/test-static-destruct.c +++ b/src/test/test-static-destruct.c @@ -21,14 +21,14 @@ STATIC_DESTRUCTOR_REGISTER(baz, test_destroy); STATIC_DESTRUCTOR_REGISTER(baz, test_destroy); STATIC_DESTRUCTOR_REGISTER(memory, freep); -int main(int argc, char *argv[]) { - test_setup_logging(LOG_INFO); - +TEST(static_destruct) { assert_se(memory = strdup("hallo")); assert_se(foo == 0 && bar == 0 && baz == 0); static_destruct(); assert_se(foo == 1 && bar == 2 && baz == 3); - return EXIT_SUCCESS; + assert_se(memory == NULL); } + +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-strbuf.c b/src/test/test-strbuf.c index a7c2f83c73..9233c63f90 100644 --- a/src/test/test-strbuf.c +++ b/src/test/test-strbuf.c @@ -5,13 +5,14 @@ #include "strbuf.h" #include "string-util.h" #include "strv.h" +#include "tests.h" #include "util.h" static ssize_t add_string(struct strbuf *sb, const char *s) { return strbuf_add_string(sb, s, strlen(s)); } -static void test_strbuf(void) { +TEST(strbuf) { _cleanup_(strbuf_freep) struct strbuf *sb; _cleanup_strv_free_ char **l; ssize_t a, b, c, d, e, f, g, h; @@ -69,8 +70,4 @@ static void test_strbuf(void) { assert_se(sb->root == NULL); } -int main(int argc, const char *argv[]) { - test_strbuf(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-string-util.c b/src/test/test-string-util.c index 8e2ccc06f4..57cd03e4e2 100644 --- a/src/test/test-string-util.c +++ b/src/test/test-string-util.c @@ -9,9 +9,7 @@ #include "utf8.h" #include "util.h" -static void test_string_erase(void) { - log_info("/* %s */", __func__); - +TEST(string_erase) { char *x; x = strdupa_safe(""); assert_se(streq(string_erase(x), "")); @@ -42,9 +40,7 @@ static void test_free_and_strndup_one(char **t, const char *src, size_t l, const assert_se(r == change); /* check that change occurs only when necessary */ } -static void test_free_and_strndup(void) { - log_info("/* %s */", __func__); - +TEST(free_and_strndup) { static const struct test_case { const char *src; size_t len; @@ -91,9 +87,7 @@ static void test_free_and_strndup(void) { } } -static void test_ascii_strcasecmp_n(void) { - log_info("/* %s */", __func__); - +TEST(ascii_strcasecmp_n) { assert_se(ascii_strcasecmp_n("", "", 0) == 0); assert_se(ascii_strcasecmp_n("", "", 1) == 0); assert_se(ascii_strcasecmp_n("", "a", 1) < 0); @@ -119,9 +113,7 @@ static void test_ascii_strcasecmp_n(void) { assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxXxxxx", 9) > 0); } -static void test_ascii_strcasecmp_nn(void) { - log_info("/* %s */", __func__); - +TEST(ascii_strcasecmp_nn) { assert_se(ascii_strcasecmp_nn("", 0, "", 0) == 0); assert_se(ascii_strcasecmp_nn("", 0, "", 1) < 0); assert_se(ascii_strcasecmp_nn("", 1, "", 0) > 0); @@ -138,11 +130,9 @@ static void test_ascii_strcasecmp_nn(void) { assert_se(ascii_strcasecmp_nn("BBbb", 4, "aaaa", 4) > 0); } -static void test_cellescape(void) { +TEST(cellescape) { char buf[40]; - log_info("/* %s */", __func__); - assert_se(streq(cellescape(buf, 1, ""), "")); assert_se(streq(cellescape(buf, 1, "1"), "")); assert_se(streq(cellescape(buf, 1, "12"), "")); @@ -221,25 +211,19 @@ static void test_cellescape(void) { assert_se(streq(cellescape(buf, sizeof buf, "1\020x"), "1\\020x")); } -static void test_streq_ptr(void) { - log_info("/* %s */", __func__); - +TEST(streq_ptr) { assert_se(streq_ptr(NULL, NULL)); assert_se(!streq_ptr("abc", "cdef")); } -static void test_strstrip(void) { - log_info("/* %s */", __func__); - +TEST(strstrip) { char *ret, input[] = " hello, waldo. "; ret = strstrip(input); assert_se(streq(ret, "hello, waldo.")); } -static void test_strextend(void) { - log_info("/* %s */", __func__); - +TEST(strextend) { _cleanup_free_ char *str = NULL; assert_se(strextend(&str, NULL)); @@ -250,9 +234,7 @@ static void test_strextend(void) { assert_se(streq_ptr(str, "0123456789")); } -static void test_strextend_with_separator(void) { - log_info("/* %s */", __func__); - +TEST(strextend_with_separator) { _cleanup_free_ char *str = NULL; assert_se(strextend_with_separator(&str, NULL, NULL)); @@ -275,9 +257,7 @@ static void test_strextend_with_separator(void) { assert_se(streq_ptr(str, "start,,1,234;more;5;678")); } -static void test_strrep(void) { - log_info("/* %s */", __func__); - +TEST(strrep) { _cleanup_free_ char *one, *three, *zero; one = strrep("waldo", 1); three = strrep("waldo", 3); @@ -288,7 +268,7 @@ static void test_strrep(void) { assert_se(streq(zero, "")); } -static void test_string_has_cc(void) { +TEST(string_has_cc) { assert_se(string_has_cc("abc\1", NULL)); assert_se(string_has_cc("abc\x7f", NULL)); assert_se(string_has_cc("abc\x7f", NULL)); @@ -302,16 +282,12 @@ static void test_string_has_cc(void) { assert_se(!string_has_cc("a\ab\tc", "\t\a")); } -static void test_ascii_strlower(void) { - log_info("/* %s */", __func__); - +TEST(ascii_strlower) { char a[] = "AabBcC Jk Ii Od LKJJJ kkd LK"; assert_se(streq(ascii_strlower(a), "aabbcc jk ii od lkjjj kkd lk")); } -static void test_strshorten(void) { - log_info("/* %s */", __func__); - +TEST(strshorten) { char s[] = "foobar"; assert_se(strlen(strshorten(s, 6)) == 6); @@ -320,9 +296,7 @@ static void test_strshorten(void) { assert_se(strlen(strshorten(s, 0)) == 0); } -static void test_strjoina(void) { - log_info("/* %s */", __func__); - +TEST(strjoina) { char *actual; actual = strjoina("", "foo", "bar"); @@ -353,7 +327,7 @@ static void test_strjoina(void) { assert_se(streq(actual, "/sys/fs/cgroup/dn")); } -static void test_strjoin(void) { +TEST(strjoin) { char *actual; actual = strjoin("", "foo", "bar"); @@ -385,9 +359,7 @@ static void test_strjoin(void) { mfree(actual); } -static void test_strcmp_ptr(void) { - log_info("/* %s */", __func__); - +TEST(strcmp_ptr) { assert_se(strcmp_ptr(NULL, NULL) == 0); assert_se(strcmp_ptr("", NULL) > 0); assert_se(strcmp_ptr("foo", NULL) > 0); @@ -399,9 +371,7 @@ static void test_strcmp_ptr(void) { assert_se(strcmp_ptr("", "") == 0); } -static void test_foreach_word(void) { - log_info("/* %s */", __func__); - +TEST(foreach_word) { const char *test = "test abc d\te f "; const char * const expected[] = { "test", @@ -450,9 +420,7 @@ static void check(const char *test, char** expected, bool trailing) { assert_se(expected[i] == NULL); } -static void test_foreach_word_quoted(void) { - log_info("/* %s */", __func__); - +TEST(foreach_word_quoted) { check("test a b c 'd' e '' '' hhh '' '' \"a b c\"", STRV_MAKE("test", "a", @@ -477,9 +445,7 @@ static void test_foreach_word_quoted(void) { true); } -static void test_endswith(void) { - log_info("/* %s */", __func__); - +TEST(endswith) { assert_se(endswith("foobar", "bar")); assert_se(endswith("foobar", "")); assert_se(endswith("foobar", "foobar")); @@ -489,9 +455,7 @@ static void test_endswith(void) { assert_se(!endswith("foobar", "foobarfoofoo")); } -static void test_endswith_no_case(void) { - log_info("/* %s */", __func__); - +TEST(endswith_no_case) { assert_se(endswith_no_case("fooBAR", "bar")); assert_se(endswith_no_case("foobar", "")); assert_se(endswith_no_case("foobar", "FOOBAR")); @@ -501,9 +465,7 @@ static void test_endswith_no_case(void) { assert_se(!endswith_no_case("foobar", "FOOBARFOOFOO")); } -static void test_delete_chars(void) { - log_info("/* %s */", __func__); - +TEST(delete_chars) { char *s, input[] = " hello, waldo. abc"; s = delete_chars(input, WHITESPACE); @@ -511,9 +473,7 @@ static void test_delete_chars(void) { assert_se(s == input); } -static void test_delete_trailing_chars(void) { - log_info("/* %s */", __func__); - +TEST(delete_trailing_chars) { char *s, input1[] = " \n \r k \n \r ", input2[] = "kkkkthiskkkiskkkaktestkkk", @@ -536,9 +496,7 @@ static void test_delete_trailing_chars(void) { assert_se(s == input3); } -static void test_delete_trailing_slashes(void) { - log_info("/* %s */", __func__); - +TEST(delete_trailing_slashes) { char s1[] = "foobar//", s2[] = "foobar/", s3[] = "foobar", @@ -551,9 +509,7 @@ static void test_delete_trailing_slashes(void) { assert_se(streq(delete_trailing_chars(s4, "/"), "")); } -static void test_skip_leading_chars(void) { - log_info("/* %s */", __func__); - +TEST(skip_leading_chars) { char input1[] = " \n \r k \n \r ", input2[] = "kkkkthiskkkiskkkaktestkkk", input3[] = "abcdef"; @@ -565,16 +521,12 @@ static void test_skip_leading_chars(void) { assert_se(streq(skip_leading_chars(input3, "bcaef"), "def")); } -static void test_in_charset(void) { - log_info("/* %s */", __func__); - +TEST(in_charset) { assert_se(in_charset("dddaaabbbcccc", "abcd")); assert_se(!in_charset("dddaaabbbcccc", "abc f")); } -static void test_split_pair(void) { - log_info("/* %s */", __func__); - +TEST(split_pair) { _cleanup_free_ char *a = NULL, *b = NULL; assert_se(split_pair("", "", &a, &b) == -EINVAL); @@ -596,9 +548,7 @@ static void test_split_pair(void) { assert_se(streq(b, "=")); } -static void test_first_word(void) { - log_info("/* %s */", __func__); - +TEST(first_word) { assert_se(first_word("Hello", "")); assert_se(first_word("Hello", "Hello")); assert_se(first_word("Hello world", "Hello")); @@ -612,17 +562,13 @@ static void test_first_word(void) { assert_se(!first_word("Hellooo", "Hello")); } -static void test_strlen_ptr(void) { - log_info("/* %s */", __func__); - +TEST(strlen_ptr) { assert_se(strlen_ptr("foo") == 3); assert_se(strlen_ptr("") == 0); assert_se(strlen_ptr(NULL) == 0); } -static void test_memory_startswith(void) { - log_info("/* %s */", __func__); - +TEST(memory_startswith) { assert_se(streq(memory_startswith("", 0, ""), "")); assert_se(streq(memory_startswith("", 1, ""), "")); assert_se(streq(memory_startswith("x", 2, ""), "x")); @@ -634,9 +580,7 @@ static void test_memory_startswith(void) { assert_se(!memory_startswith("xxx", 4, "xxxx")); } -static void test_memory_startswith_no_case(void) { - log_info("/* %s */", __func__); - +TEST(memory_startswith_no_case) { assert_se(streq(memory_startswith_no_case("", 0, ""), "")); assert_se(streq(memory_startswith_no_case("", 1, ""), "")); assert_se(streq(memory_startswith_no_case("x", 2, ""), "x")); @@ -668,9 +612,7 @@ static void test_string_truncate_lines_one(const char *input, size_t n_lines, co assert_se(!!k == truncation); } -static void test_string_truncate_lines(void) { - log_info("/* %s */", __func__); - +TEST(string_truncate_lines) { test_string_truncate_lines_one("", 0, "", false); test_string_truncate_lines_one("", 1, "", false); test_string_truncate_lines_one("", 2, "", false); @@ -741,9 +683,7 @@ static void test_string_extract_lines_one(const char *input, size_t i, const cha assert_se(!!k == more); } -static void test_string_extract_line(void) { - log_info("/* %s */", __func__); - +TEST(string_extract_line) { test_string_extract_lines_one("", 0, "", false); test_string_extract_lines_one("", 1, "", false); test_string_extract_lines_one("", 2, "", false); @@ -810,9 +750,7 @@ static void test_string_extract_line(void) { test_string_extract_lines_one("\n\n\nx\n", 3, "x", false); } -static void test_string_contains_word_strv(void) { - log_info("/* %s */", __func__); - +TEST(string_contains_word_strv) { const char *w; assert_se(string_contains_word_strv("a b cc", NULL, STRV_MAKE("a", "b"), NULL)); @@ -839,9 +777,7 @@ static void test_string_contains_word_strv(void) { assert_se(streq(w, "")); } -static void test_string_contains_word(void) { - log_info("/* %s */", __func__); - +TEST(string_contains_word) { assert_se( string_contains_word("a b cc", NULL, "a")); assert_se( string_contains_word("a b cc", NULL, "b")); assert_se(!string_contains_word("a b cc", NULL, "c")); @@ -901,7 +837,7 @@ static void test_strverscmp_improved_one(const char *newer, const char *older) { assert_se(strverscmp_improved(older, older) == 0); } -static void test_strverscmp_improved(void) { +TEST(strverscmp_improved) { static const char * const versions[] = { "", "~1", @@ -970,7 +906,7 @@ static void test_strverscmp_improved(void) { assert_se(strverscmp_improved("123_aa2-67.89", "123aa+2-67.89") == 0); } -static void test_strextendf(void) { +TEST(strextendf) { _cleanup_free_ char *p = NULL; assert_se(strextendf(&p, "<%i>", 77) >= 0); @@ -1000,9 +936,7 @@ static void test_strextendf(void) { assert_se(streq(p, "<77>,<99>,< 88>,<00001234>")); } -static void test_streq_skip_trailing_chars(void) { - log_info("/* %s */", __func__); - +TEST(streq_skip_trailing_chars) { /* NULL is WHITESPACE by default*/ assert_se(streq_skip_trailing_chars("foo bar", "foo bar", NULL)); assert_se(streq_skip_trailing_chars("foo", "foo", NULL)); @@ -1027,46 +961,4 @@ static void test_streq_skip_trailing_chars(void) { assert_se(!streq_skip_trailing_chars("", "f", NULL)); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_DEBUG); - - test_string_erase(); - test_free_and_strndup(); - test_ascii_strcasecmp_n(); - test_ascii_strcasecmp_nn(); - test_cellescape(); - test_streq_ptr(); - test_strstrip(); - test_strextend(); - test_strextend_with_separator(); - test_strrep(); - test_string_has_cc(); - test_ascii_strlower(); - test_strshorten(); - test_strjoina(); - test_strjoin(); - test_strcmp_ptr(); - test_foreach_word(); - test_foreach_word_quoted(); - test_endswith(); - test_endswith_no_case(); - test_delete_chars(); - test_delete_trailing_chars(); - test_delete_trailing_slashes(); - test_skip_leading_chars(); - test_in_charset(); - test_split_pair(); - test_first_word(); - test_strlen_ptr(); - test_memory_startswith(); - test_memory_startswith_no_case(); - test_string_truncate_lines(); - test_string_extract_line(); - test_string_contains_word_strv(); - test_string_contains_word(); - test_strverscmp_improved(); - test_strextendf(); - test_streq_skip_trailing_chars(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-strip-tab-ansi.c b/src/test/test-strip-tab-ansi.c index be54b0d4d6..5152cf2cf8 100644 --- a/src/test/test-strip-tab-ansi.c +++ b/src/test/test-strip-tab-ansi.c @@ -6,9 +6,10 @@ #include "pretty-print.h" #include "string-util.h" #include "terminal-util.h" +#include "tests.h" #include "util.h" -int main(int argc, char *argv[]) { +TEST(strip_tab_ansi) { _cleanup_free_ char *urlified = NULL, *q = NULL, *qq = NULL; char *p, *z; @@ -67,6 +68,6 @@ int main(int argc, char *argv[]) { assert_se(strip_tab_ansi(&q, NULL, NULL)); assert_se(streq(q, qq)); } - - return 0; } + +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-strv.c b/src/test/test-strv.c index 34f438cf9c..a025f34408 100644 --- a/src/test/test-strv.c +++ b/src/test/test-strv.c @@ -5,19 +5,16 @@ #include "nulstr-util.h" #include "string-util.h" #include "strv.h" +#include "tests.h" -static void test_str_in_set(void) { - log_info("/* %s */", __func__); - +TEST(str_in_set) { assert_se(STR_IN_SET("x", "x", "y", "z")); assert_se(!STR_IN_SET("X", "x", "y", "z")); assert_se(!STR_IN_SET("", "x", "y", "z")); assert_se(STR_IN_SET("x", "w", "x")); } -static void test_strptr_in_set(void) { - log_info("/* %s */", __func__); - +TEST(strptr_in_set) { assert_se(STRPTR_IN_SET("x", "x", "y", "z")); assert_se(!STRPTR_IN_SET("X", "x", "y", "z")); assert_se(!STRPTR_IN_SET("", "x", "y", "z")); @@ -29,9 +26,7 @@ static void test_strptr_in_set(void) { assert_se(!STRPTR_IN_SET(NULL, NULL)); } -static void test_startswith_set(void) { - log_info("/* %s */", __func__); - +TEST(startswith_set) { assert_se(!STARTSWITH_SET("foo", "bar", "baz", "waldo")); assert_se(!STARTSWITH_SET("foo", "bar")); @@ -99,16 +94,12 @@ static const char* const input_table_retain_escape[] = { NULL, }; -static void test_strv_find(void) { - log_info("/* %s */", __func__); - +TEST(strv_find) { assert_se(strv_find((char **)input_table_multiple, "three")); assert_se(!strv_find((char **)input_table_multiple, "four")); } -static void test_strv_find_prefix(void) { - log_info("/* %s */", __func__); - +TEST(strv_find_prefix) { assert_se(strv_find_prefix((char **)input_table_multiple, "o")); assert_se(strv_find_prefix((char **)input_table_multiple, "one")); assert_se(strv_find_prefix((char **)input_table_multiple, "")); @@ -116,11 +107,9 @@ static void test_strv_find_prefix(void) { assert_se(!strv_find_prefix((char **)input_table_multiple, "onee")); } -static void test_strv_find_startswith(void) { +TEST(strv_find_startswith) { char *r; - log_info("/* %s */", __func__); - r = strv_find_startswith((char **)input_table_multiple, "o"); assert_se(r && streq(r, "ne")); @@ -134,9 +123,7 @@ static void test_strv_find_startswith(void) { assert_se(!strv_find_startswith((char **)input_table_multiple, "onee")); } -static void test_strv_join(void) { - log_info("/* %s */", __func__); - +TEST(strv_join) { _cleanup_free_ char *p = strv_join((char **)input_table_multiple, ", "); assert_se(p); assert_se(streq(p, "one, two, three")); @@ -170,9 +157,7 @@ static void test_strv_join(void) { assert_se(streq(w, "")); } -static void test_strv_join_full(void) { - log_info("/* %s */", __func__); - +TEST(strv_join_full) { _cleanup_free_ char *p = strv_join_full((char **)input_table_multiple, ", ", "foo", false); assert_se(p); assert_se(streq(p, "fooone, footwo, foothree")); @@ -215,7 +200,7 @@ static void test_strv_join_full(void) { assert_se(streq(y, "foo")); } -static void test_strv_unquote(const char *quoted, char **list) { +static void test_strv_unquote_one(const char *quoted, char **list) { _cleanup_strv_free_ char **s; _cleanup_free_ char *j; unsigned i = 0; @@ -237,7 +222,25 @@ static void test_strv_unquote(const char *quoted, char **list) { assert_se(list[i] == NULL); } -static void test_invalid_unquote(const char *quoted) { +TEST(strv_unquote) { + test_strv_unquote_one(" foo=bar \"waldo\" zzz ", STRV_MAKE("foo=bar", "waldo", "zzz")); + test_strv_unquote_one("", STRV_MAKE_EMPTY); + test_strv_unquote_one(" ", STRV_MAKE_EMPTY); + test_strv_unquote_one(" ", STRV_MAKE_EMPTY); + test_strv_unquote_one(" x", STRV_MAKE("x")); + test_strv_unquote_one("x ", STRV_MAKE("x")); + test_strv_unquote_one(" x ", STRV_MAKE("x")); + test_strv_unquote_one(" \"x\" ", STRV_MAKE("x")); + test_strv_unquote_one(" 'x' ", STRV_MAKE("x")); + test_strv_unquote_one(" 'x\"' ", STRV_MAKE("x\"")); + test_strv_unquote_one(" \"x'\" ", STRV_MAKE("x'")); + test_strv_unquote_one("a '--b=c \"d e\"'", STRV_MAKE("a", "--b=c \"d e\"")); + + /* trailing backslashes */ + test_strv_unquote_one(" x\\\\", STRV_MAKE("x\\")); +} + +static void test_invalid_unquote_one(const char *quoted) { char **s = NULL; int r; @@ -248,12 +251,20 @@ static void test_invalid_unquote(const char *quoted) { assert_se(r == -EINVAL); } -static void test_strv_split(void) { +TEST(invalid_unquote) { + test_invalid_unquote_one(" x\\"); + test_invalid_unquote_one("a --b='c \"d e\"''"); + test_invalid_unquote_one("a --b='c \"d e\" '\""); + test_invalid_unquote_one("a --b='c \"d e\"garbage"); + test_invalid_unquote_one("'"); + test_invalid_unquote_one("\""); + test_invalid_unquote_one("'x'y'g"); +} + +TEST(strv_split) { _cleanup_(strv_free_erasep) char **l = NULL; const char str[] = "one,two,three"; - log_info("/* %s */", __func__); - l = strv_split(str, ","); assert_se(l); assert_se(strv_equal(l, (char**) input_table_multiple)); @@ -309,11 +320,9 @@ static void test_strv_split(void) { assert_se(strv_equal(l, STRV_MAKE("\\"))); } -static void test_strv_split_empty(void) { +TEST(strv_split_empty) { _cleanup_strv_free_ char **l = NULL; - log_info("/* %s */", __func__); - l = strv_split("", WHITESPACE); assert_se(l); assert_se(strv_isempty(l)); @@ -373,13 +382,11 @@ static void test_strv_split_empty(void) { assert_se(strv_isempty(l)); } -static void test_strv_split_full(void) { +TEST(strv_split_full) { _cleanup_strv_free_ char **l = NULL; const char *str = ":foo\\:bar::waldo:"; int r; - log_info("/* %s */", __func__); - r = strv_split_full(&l, str, ":", EXTRACT_DONT_COALESCE_SEPARATORS); assert_se(r == (int) strv_length(l)); assert_se(streq_ptr(l[0], "")); @@ -390,14 +397,12 @@ static void test_strv_split_full(void) { assert_se(streq_ptr(l[5], NULL)); } -static void test_strv_split_and_extend_full(void) { +TEST(strv_split_and_extend_full) { _cleanup_strv_free_ char **l = NULL; const char *str1 = ":foo\\:bar:"; const char *str2 = "waldo::::::baz"; int r; - log_info("/* %s */", __func__); - r = strv_split_and_extend(&l, "", ":", false); assert_se(r == (int) strv_length(l)); r = strv_split_and_extend_full(&l, str1, ":", false, EXTRACT_DONT_COALESCE_SEPARATORS); @@ -412,14 +417,12 @@ static void test_strv_split_and_extend_full(void) { assert_se(streq_ptr(l[5], NULL)); } -static void test_strv_split_colon_pairs(void) { +TEST(strv_split_colon_pairs) { _cleanup_strv_free_ char **l = NULL; const char *str = "one:two three four:five six seven:eight\\:nine ten\\:eleven\\\\", *str_inval="one:two three:four:five"; int r; - log_info("/* %s */", __func__); - r = strv_split_colon_pairs(&l, str); assert_se(r == (int) strv_length(l)); assert_se(r == 12); @@ -441,14 +444,12 @@ static void test_strv_split_colon_pairs(void) { assert_se(r == -EINVAL); } -static void test_strv_split_newlines(void) { +TEST(strv_split_newlines) { unsigned i = 0; char **s; _cleanup_strv_free_ char **l = NULL; const char str[] = "one\ntwo\nthree"; - log_info("/* %s */", __func__); - l = strv_split_newlines(str); assert_se(l); @@ -456,7 +457,7 @@ static void test_strv_split_newlines(void) { assert_se(streq(*s, input_table_multiple[i++])); } -static void test_strv_split_newlines_full(void) { +TEST(strv_split_newlines_full) { const char str[] = "ID_VENDOR=QEMU\n" "ID_VENDOR_ENC=QEMU\\x20\\x20\\x20\\x20\n" @@ -464,8 +465,6 @@ static void test_strv_split_newlines_full(void) { "\n\n\n"; _cleanup_strv_free_ char **l = NULL; - log_info("/* %s */", __func__); - assert_se(strv_split_newlines_full(&l, str, 0) == 3); assert_se(strv_equal(l, (char**) input_table_unescape)); @@ -475,12 +474,10 @@ static void test_strv_split_newlines_full(void) { assert_se(strv_equal(l, (char**) input_table_retain_escape)); } -static void test_strv_split_nulstr(void) { +TEST(strv_split_nulstr) { _cleanup_strv_free_ char **l = NULL; const char nulstr[] = "str0\0str1\0str2\0str3\0"; - log_info("/* %s */", __func__); - l = strv_split_nulstr (nulstr); assert_se(l); @@ -490,12 +487,10 @@ static void test_strv_split_nulstr(void) { assert_se(streq(l[3], "str3")); } -static void test_strv_parse_nulstr(void) { +TEST(strv_parse_nulstr) { _cleanup_strv_free_ char **l = NULL; const char nulstr[] = "hoge\0hoge2\0hoge3\0\0hoge5\0\0xxx"; - log_info("/* %s */", __func__); - l = strv_parse_nulstr(nulstr, sizeof(nulstr)-1); assert_se(l); puts("Parse nulstr:"); @@ -510,7 +505,7 @@ static void test_strv_parse_nulstr(void) { assert_se(streq(l[6], "xxx")); } -static void test_strv_overlap(void) { +TEST(strv_overlap) { const char * const input_table[] = { "one", "two", @@ -528,13 +523,11 @@ static void test_strv_overlap(void) { NULL }; - log_info("/* %s */", __func__); - assert_se(strv_overlap((char **)input_table, (char**)input_table_overlap)); assert_se(!strv_overlap((char **)input_table, (char**)input_table_unique)); } -static void test_strv_sort(void) { +TEST(strv_sort) { const char* input_table[] = { "durian", "apple", @@ -544,8 +537,6 @@ static void test_strv_sort(void) { NULL }; - log_info("/* %s */", __func__); - strv_sort((char **)input_table); assert_se(streq(input_table[0], "CAPITAL LETTERS FIRST")); @@ -555,11 +546,9 @@ static void test_strv_sort(void) { assert_se(streq(input_table[4], "durian")); } -static void test_strv_extend_strv_concat(void) { +TEST(strv_extend_strv_concat) { _cleanup_strv_free_ char **a = NULL, **b = NULL; - log_info("/* %s */", __func__); - a = strv_new("without", "suffix"); b = strv_new("with", "suffix"); assert_se(a); @@ -573,11 +562,9 @@ static void test_strv_extend_strv_concat(void) { assert_se(streq(a[3], "suffix_suffix")); } -static void test_strv_extend_strv(void) { +TEST(strv_extend_strv) { _cleanup_strv_free_ char **a = NULL, **b = NULL, **n = NULL; - log_info("/* %s */", __func__); - a = strv_new("abc", "def", "ghi"); b = strv_new("jkl", "mno", "abc", "pqr"); assert_se(a); @@ -601,11 +588,9 @@ static void test_strv_extend_strv(void) { assert_se(strv_length(n) == 4); } -static void test_strv_extend(void) { +TEST(strv_extend) { _cleanup_strv_free_ char **a = NULL, **b = NULL; - log_info("/* %s */", __func__); - a = strv_new("test", "test1"); assert_se(a); assert_se(strv_extend(&a, "test2") >= 0); @@ -617,11 +602,9 @@ static void test_strv_extend(void) { assert_se(streq(b[0], "test3")); } -static void test_strv_extendf(void) { +TEST(strv_extendf) { _cleanup_strv_free_ char **a = NULL, **b = NULL; - log_info("/* %s */", __func__); - a = strv_new("test", "test1"); assert_se(a); assert_se(strv_extendf(&a, "test2 %s %d %s", "foo", 128, "bar") >= 0); @@ -633,13 +616,11 @@ static void test_strv_extendf(void) { assert_se(streq(b[0], "test3 bar foo 128")); } -static void test_strv_foreach(void) { +TEST(strv_foreach) { _cleanup_strv_free_ char **a; unsigned i = 0; char **check; - log_info("/* %s */", __func__); - a = strv_new("one", "two", "three"); assert_se(a); @@ -647,13 +628,11 @@ static void test_strv_foreach(void) { assert_se(streq(*check, input_table_multiple[i++])); } -static void test_strv_foreach_backwards(void) { +TEST(strv_foreach_backwards) { _cleanup_strv_free_ char **a; unsigned i = 2; char **check; - log_info("/* %s */", __func__); - a = strv_new("one", "two", "three"); assert_se(a); @@ -668,12 +647,10 @@ static void test_strv_foreach_backwards(void) { assert_not_reached(); } -static void test_strv_foreach_pair(void) { +TEST(strv_foreach_pair) { _cleanup_strv_free_ char **a = NULL; char **x, **y; - log_info("/* %s */", __func__); - a = strv_new("pair_one", "pair_one", "pair_two", "pair_two", "pair_three", "pair_three"); @@ -697,19 +674,15 @@ static void test_strv_from_stdarg_alloca_one(char **l, const char *first, ...) { } } -static void test_strv_from_stdarg_alloca(void) { - log_info("/* %s */", __func__); - +TEST(strv_from_stdarg_alloca) { test_strv_from_stdarg_alloca_one(STRV_MAKE("foo", "bar"), "foo", "bar", NULL); test_strv_from_stdarg_alloca_one(STRV_MAKE("foo"), "foo", NULL); test_strv_from_stdarg_alloca_one(STRV_MAKE_EMPTY, NULL); } -static void test_strv_insert(void) { +TEST(strv_insert) { _cleanup_strv_free_ char **a = NULL; - log_info("/* %s */", __func__); - assert_se(strv_insert(&a, 0, strdup("first")) == 0); assert_se(streq(a[0], "first")); assert_se(!a[1]); @@ -737,11 +710,9 @@ static void test_strv_insert(void) { assert_se(!a[4]); } -static void test_strv_push_prepend(void) { +TEST(strv_push_prepend) { _cleanup_strv_free_ char **a = NULL; - log_info("/* %s */", __func__); - assert_se(a = strv_new("foo", "bar", "three")); assert_se(strv_push_prepend(&a, strdup("first")) >= 0); @@ -760,12 +731,10 @@ static void test_strv_push_prepend(void) { assert_se(!a[5]); } -static void test_strv_push(void) { +TEST(strv_push) { _cleanup_strv_free_ char **a = NULL; char *i, *j; - log_info("/* %s */", __func__); - assert_se(i = strdup("foo")); assert_se(strv_push(&a, i) >= 0); @@ -779,14 +748,12 @@ static void test_strv_push(void) { assert_se(streq_ptr(a[3], NULL)); } -static void test_strv_compare(void) { +TEST(strv_compare) { _cleanup_strv_free_ char **a = NULL; _cleanup_strv_free_ char **b = NULL; _cleanup_strv_free_ char **c = NULL; _cleanup_strv_free_ char **d = NULL; - log_info("/* %s */", __func__); - a = strv_new("one", "two", "three"); assert_se(a); b = strv_new("one", "two", "three"); @@ -808,11 +775,9 @@ static void test_strv_compare(void) { assert_se(strv_compare(b, NULL) == 1); } -static void test_strv_is_uniq(void) { +TEST(strv_is_uniq) { _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL, **d = NULL; - log_info("/* %s */", __func__); - a = strv_new(NULL); assert_se(a); assert_se(strv_is_uniq(a)); @@ -830,11 +795,9 @@ static void test_strv_is_uniq(void) { assert_se(!strv_is_uniq(d)); } -static void test_strv_reverse(void) { +TEST(strv_reverse) { _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL, **d = NULL; - log_info("/* %s */", __func__); - a = strv_new(NULL); assert_se(a); @@ -863,11 +826,9 @@ static void test_strv_reverse(void) { assert_se(streq_ptr(d[3], NULL)); } -static void test_strv_shell_escape(void) { +TEST(strv_shell_escape) { _cleanup_strv_free_ char **v = NULL; - log_info("/* %s */", __func__); - v = strv_new("foo:bar", "bar,baz", "wal\\do"); assert_se(v); assert_se(strv_shell_escape(v, ",:")); @@ -882,9 +843,7 @@ static void test_strv_skip_one(char **a, size_t n, char **b) { assert_se(strv_equal(a, b)); } -static void test_strv_skip(void) { - log_info("/* %s */", __func__); - +TEST(strv_skip) { test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 0, STRV_MAKE("foo", "bar", "baz")); test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 1, STRV_MAKE("bar", "baz")); test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 2, STRV_MAKE("baz")); @@ -901,11 +860,9 @@ static void test_strv_skip(void) { test_strv_skip_one(STRV_MAKE(NULL), 55, STRV_MAKE(NULL)); } -static void test_strv_extend_n(void) { +TEST(strv_extend_n) { _cleanup_strv_free_ char **v = NULL; - log_info("/* %s */", __func__); - v = strv_new("foo", "bar"); assert_se(v); @@ -952,9 +909,7 @@ static void test_strv_make_nulstr_one(char **l) { assert_se(i == strv_length(l)); } -static void test_strv_make_nulstr(void) { - log_info("/* %s */", __func__); - +TEST(strv_make_nulstr) { test_strv_make_nulstr_one(NULL); test_strv_make_nulstr_one(STRV_MAKE(NULL)); test_strv_make_nulstr_one(STRV_MAKE("foo")); @@ -962,11 +917,9 @@ static void test_strv_make_nulstr(void) { test_strv_make_nulstr_one(STRV_MAKE("foo", "bar", "quuux")); } -static void test_strv_free_free(void) { +TEST(strv_free_free) { char ***t; - log_info("/* %s */", __func__); - assert_se(t = new(char**, 3)); assert_se(t[0] = strv_new("a", "b")); assert_se(t[1] = strv_new("c", "d", "e")); @@ -975,7 +928,7 @@ static void test_strv_free_free(void) { t = strv_free_free(t); } -static void test_foreach_string(void) { +TEST(foreach_string) { const char * const t[] = { "foo", "bar", @@ -985,8 +938,6 @@ static void test_foreach_string(void) { const char *x; unsigned i = 0; - log_info("/* %s */", __func__); - FOREACH_STRING(x, "foo", "bar", "waldo") assert_se(streq_ptr(t[i++], x)); @@ -996,12 +947,10 @@ static void test_foreach_string(void) { assert_se(streq(x, "zzz")); } -static void test_strv_fnmatch(void) { +TEST(strv_fnmatch) { _cleanup_strv_free_ char **v = NULL; size_t pos; - log_info("/* %s */", __func__); - assert_se(!strv_fnmatch(STRV_MAKE_EMPTY, "a")); v = strv_new("xxx", "*\\*", "yyy"); @@ -1010,73 +959,4 @@ static void test_strv_fnmatch(void) { assert_se(pos == 1); } -int main(int argc, char *argv[]) { - test_str_in_set(); - test_strptr_in_set(); - test_startswith_set(); - test_strv_foreach(); - test_strv_foreach_backwards(); - test_strv_foreach_pair(); - test_strv_find(); - test_strv_find_prefix(); - test_strv_find_startswith(); - test_strv_join(); - test_strv_join_full(); - - test_strv_unquote(" foo=bar \"waldo\" zzz ", STRV_MAKE("foo=bar", "waldo", "zzz")); - test_strv_unquote("", STRV_MAKE_EMPTY); - test_strv_unquote(" ", STRV_MAKE_EMPTY); - test_strv_unquote(" ", STRV_MAKE_EMPTY); - test_strv_unquote(" x", STRV_MAKE("x")); - test_strv_unquote("x ", STRV_MAKE("x")); - test_strv_unquote(" x ", STRV_MAKE("x")); - test_strv_unquote(" \"x\" ", STRV_MAKE("x")); - test_strv_unquote(" 'x' ", STRV_MAKE("x")); - test_strv_unquote(" 'x\"' ", STRV_MAKE("x\"")); - test_strv_unquote(" \"x'\" ", STRV_MAKE("x'")); - test_strv_unquote("a '--b=c \"d e\"'", STRV_MAKE("a", "--b=c \"d e\"")); - - /* trailing backslashes */ - test_strv_unquote(" x\\\\", STRV_MAKE("x\\")); - test_invalid_unquote(" x\\"); - - test_invalid_unquote("a --b='c \"d e\"''"); - test_invalid_unquote("a --b='c \"d e\" '\""); - test_invalid_unquote("a --b='c \"d e\"garbage"); - test_invalid_unquote("'"); - test_invalid_unquote("\""); - test_invalid_unquote("'x'y'g"); - - test_strv_split(); - test_strv_split_empty(); - test_strv_split_full(); - test_strv_split_and_extend_full(); - test_strv_split_colon_pairs(); - test_strv_split_newlines(); - test_strv_split_newlines_full(); - test_strv_split_nulstr(); - test_strv_parse_nulstr(); - test_strv_overlap(); - test_strv_sort(); - test_strv_extend_strv(); - test_strv_extend_strv_concat(); - test_strv_extend(); - test_strv_extendf(); - test_strv_from_stdarg_alloca(); - test_strv_insert(); - test_strv_push_prepend(); - test_strv_push(); - test_strv_compare(); - test_strv_is_uniq(); - test_strv_reverse(); - test_strv_shell_escape(); - test_strv_skip(); - test_strv_extend_n(); - test_strv_make_nulstr(); - test_strv_free_free(); - - test_foreach_string(); - test_strv_fnmatch(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-strxcpyx.c b/src/test/test-strxcpyx.c index c68fe3c064..4e2118979f 100644 --- a/src/test/test-strxcpyx.c +++ b/src/test/test-strxcpyx.c @@ -4,9 +4,10 @@ #include "string-util.h" #include "strxcpyx.h" +#include "tests.h" #include "util.h" -static void test_strpcpy(void) { +TEST(strpcpy) { char target[25]; char *s = target; size_t space_left; @@ -23,7 +24,7 @@ static void test_strpcpy(void) { assert_se(space_left == 0); } -static void test_strpcpyf(void) { +TEST(strpcpyf) { char target[25]; char *s = target; size_t space_left; @@ -43,7 +44,7 @@ static void test_strpcpyf(void) { assert_se(target[12] == '2'); } -static void test_strpcpyl(void) { +TEST(strpcpyl) { char target[25]; char *s = target; size_t space_left; @@ -56,7 +57,7 @@ static void test_strpcpyl(void) { assert_se(space_left == 1); } -static void test_strscpy(void) { +TEST(strscpy) { char target[25]; size_t space_left; @@ -67,7 +68,7 @@ static void test_strscpy(void) { assert_se(space_left == 20); } -static void test_strscpyl(void) { +TEST(strscpyl) { char target[25]; size_t space_left; @@ -78,7 +79,7 @@ static void test_strscpyl(void) { assert_se(space_left == 10); } -static void test_sd_event_code_migration(void) { +TEST(sd_event_code_migration) { char b[100 * DECIMAL_STR_MAX(unsigned) + 1]; char c[100 * DECIMAL_STR_MAX(unsigned) + 1], *p; unsigned i; @@ -99,14 +100,4 @@ static void test_sd_event_code_migration(void) { assert_se(streq(b, c)); } -int main(int argc, char *argv[]) { - test_strpcpy(); - test_strpcpyf(); - test_strpcpyl(); - test_strscpy(); - test_strscpyl(); - - test_sd_event_code_migration(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-sysctl-util.c b/src/test/test-sysctl-util.c index 17ec04e732..6464a7965b 100644 --- a/src/test/test-sysctl-util.c +++ b/src/test/test-sysctl-util.c @@ -24,9 +24,7 @@ static const char* const cases[] = { NULL, }; -static void test_sysctl_normalize(void) { - log_info("/* %s */", __func__); - +TEST(sysctl_normalize) { const char **s, **expected; STRV_FOREACH_PAIR(s, expected, (const char**) cases) { _cleanup_free_ char *t; @@ -39,7 +37,7 @@ static void test_sysctl_normalize(void) { } } -static void test_sysctl_read(void) { +TEST(sysctl_read) { _cleanup_free_ char *s = NULL, *h = NULL; sd_id128_t a, b; int r; @@ -72,11 +70,4 @@ static void test_sysctl_read(void) { assert_se(r >= 0 || ERRNO_IS_PRIVILEGE(r) || r == -EROFS); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_INFO); - - test_sysctl_normalize(); - test_sysctl_read(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-terminal-util.c b/src/test/test-terminal-util.c index e793a74b13..292e5348bc 100644 --- a/src/test/test-terminal-util.c +++ b/src/test/test-terminal-util.c @@ -22,9 +22,7 @@ "in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat " \ "non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." -static void test_default_term_for_tty(void) { - log_info("/* %s */", __func__); - +TEST(default_term_for_tty) { puts(default_term_for_tty("/dev/tty23")); puts(default_term_for_tty("/dev/ttyS23")); puts(default_term_for_tty("/dev/tty0")); @@ -39,14 +37,12 @@ static void test_default_term_for_tty(void) { puts(default_term_for_tty("console")); } -static void test_read_one_char(void) { +TEST(read_one_char) { _cleanup_fclose_ FILE *file = NULL; char r; bool need_nl; char name[] = "/tmp/test-read_one_char.XXXXXX"; - log_info("/* %s */", __func__); - assert_se(fmkostemp_safe(name, "r+", &file) == 0); assert_se(fputs("c\n", file) >= 0); @@ -69,12 +65,10 @@ static void test_read_one_char(void) { assert_se(unlink(name) >= 0); } -static void test_getttyname_malloc(void) { +TEST(getttyname_malloc) { _cleanup_free_ char *ttyname = NULL; _cleanup_close_ int master = -1; - log_info("/* %s */", __func__); - assert_se((master = posix_openpt(O_RDWR|O_NOCTTY)) >= 0); assert_se(getttyname_malloc(master, &ttyname) >= 0); log_info("ttyname = %s", ttyname); @@ -130,16 +124,12 @@ static const Color colors[] = { { "highlight-grey-underline", ansi_highlight_grey_underline }, }; -static void test_colors(void) { - log_info("/* %s */", __func__); - +TEST(colors) { for (size_t i = 0; i < ELEMENTSOF(colors); i++) printf("<%s%s%s>\n", colors[i].func(), colors[i].name, ansi_normal()); } -static void test_text(void) { - log_info("/* %s */", __func__); - +TEST(text) { for (size_t i = 0; !streq(colors[i].name, "underline"); i++) { bool blwh = strstr(colors[i].name, "black") || strstr(colors[i].name, "white"); @@ -154,7 +144,7 @@ static void test_text(void) { } } -static void test_get_ctty(void) { +TEST(get_ctty) { _cleanup_free_ char *ctty = NULL; struct stat st; dev_t devnr; @@ -177,15 +167,4 @@ static void test_get_ctty(void) { log_notice("Not invoked with stdin == ctty, cutting get_ctty() test short"); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_INFO); - - test_default_term_for_tty(); - test_read_one_char(); - test_getttyname_malloc(); - test_colors(); - test_text(); - test_get_ctty(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-time-util.c b/src/test/test-time-util.c index 0992a31659..4d0131827e 100644 --- a/src/test/test-time-util.c +++ b/src/test/test-time-util.c @@ -8,11 +8,9 @@ #include "tests.h" #include "time-util.h" -static void test_parse_sec(void) { +TEST(parse_sec) { usec_t u; - log_info("/* %s */", __func__); - assert_se(parse_sec("5s", &u) >= 0); assert_se(u == 5 * USEC_PER_SEC); assert_se(parse_sec("5s500ms", &u) >= 0); @@ -72,11 +70,9 @@ static void test_parse_sec(void) { assert_se(parse_sec("1234..", &u) < 0); } -static void test_parse_sec_fix_0(void) { +TEST(parse_sec_fix_0) { usec_t u; - log_info("/* %s */", __func__); - assert_se(parse_sec_fix_0("5s", &u) >= 0); assert_se(u == 5 * USEC_PER_SEC); assert_se(parse_sec_fix_0("0s", &u) >= 0); @@ -87,11 +83,9 @@ static void test_parse_sec_fix_0(void) { assert_se(u == USEC_INFINITY); } -static void test_parse_sec_def_infinity(void) { +TEST(parse_sec_def_infinity) { usec_t u; - log_info("/* %s */", __func__); - assert_se(parse_sec_def_infinity("5s", &u) >= 0); assert_se(u == 5 * USEC_PER_SEC); assert_se(parse_sec_def_infinity("", &u) >= 0); @@ -107,11 +101,9 @@ static void test_parse_sec_def_infinity(void) { assert_se(parse_sec_def_infinity("-5s", &u) < 0); } -static void test_parse_time(void) { +TEST(parse_time) { usec_t u; - log_info("/* %s */", __func__); - assert_se(parse_time("5", &u, 1) >= 0); assert_se(u == 5); @@ -134,11 +126,9 @@ static void test_parse_time(void) { assert_se(parse_time("1.1111111111111y", &u, 1) >= 0); } -static void test_parse_nsec(void) { +TEST(parse_nsec) { nsec_t u; - log_info("/* %s */", __func__); - assert_se(parse_nsec("5s", &u) >= 0); assert_se(u == 5 * NSEC_PER_SEC); assert_se(parse_nsec("5s500ms", &u) >= 0); @@ -217,7 +207,7 @@ static void test_format_timespan_one(usec_t x, usec_t accuracy) { assert_se(x / accuracy == y / accuracy); } -static void test_format_timespan(usec_t accuracy) { +static void test_format_timespan_accuracy(usec_t accuracy) { log_info("/* %s accuracy="USEC_FMT" */", __func__, accuracy); test_format_timespan_one(0, accuracy); @@ -244,9 +234,13 @@ static void test_format_timespan(usec_t accuracy) { test_format_timespan_one(USEC_INFINITY, accuracy); } -static void test_verify_timezone(void) { - log_info("/* %s */", __func__); +TEST(format_timespan) { + test_format_timespan_accuracy(1); + test_format_timespan_accuracy(USEC_PER_MSEC); + test_format_timespan_accuracy(USEC_PER_SEC); +} +TEST(verify_timezone) { assert_se(verify_timezone("Europe/Berlin", LOG_DEBUG) == 0); assert_se(verify_timezone("Australia/Sydney", LOG_DEBUG) == 0); assert_se(verify_timezone("Europe/Do not exist", LOG_DEBUG) == -EINVAL); @@ -255,21 +249,17 @@ static void test_verify_timezone(void) { assert_se(verify_timezone("DoNotExist/", LOG_DEBUG) == -EINVAL); } -static void test_timezone_is_valid(void) { - log_info("/* %s */", __func__); - +TEST(timezone_is_valid) { assert_se(timezone_is_valid("Europe/Berlin", LOG_ERR)); assert_se(timezone_is_valid("Australia/Sydney", LOG_ERR)); assert_se(!timezone_is_valid("Europe/Do not exist", LOG_ERR)); } -static void test_get_timezones(void) { +TEST(get_timezones) { _cleanup_strv_free_ char **zones = NULL; int r; char **zone; - log_info("/* %s */", __func__); - r = get_timezones(&zones); assert_se(r == 0); @@ -280,9 +270,7 @@ static void test_get_timezones(void) { } } -static void test_usec_add(void) { - log_info("/* %s */", __func__); - +TEST(usec_add) { assert_se(usec_add(0, 0) == 0); assert_se(usec_add(1, 4) == 5); assert_se(usec_add(USEC_INFINITY, 5) == USEC_INFINITY); @@ -293,9 +281,7 @@ static void test_usec_add(void) { assert_se(usec_add(USEC_INFINITY, 2) == USEC_INFINITY); } -static void test_usec_sub_unsigned(void) { - log_info("/* %s */", __func__); - +TEST(usec_sub_unsigned) { assert_se(usec_sub_unsigned(0, 0) == 0); assert_se(usec_sub_unsigned(0, 2) == 0); assert_se(usec_sub_unsigned(0, USEC_INFINITY) == 0); @@ -316,9 +302,7 @@ static void test_usec_sub_unsigned(void) { assert_se(usec_sub_unsigned(USEC_INFINITY, USEC_INFINITY) == USEC_INFINITY); } -static void test_usec_sub_signed(void) { - log_info("/* %s */", __func__); - +TEST(usec_sub_signed) { assert_se(usec_sub_signed(0, 0) == 0); assert_se(usec_sub_signed(4, 1) == 3); assert_se(usec_sub_signed(4, 4) == 0); @@ -329,9 +313,7 @@ static void test_usec_sub_signed(void) { assert_se(usec_sub_signed(USEC_INFINITY, 5) == USEC_INFINITY); } -static void test_format_timestamp(void) { - log_info("/* %s */", __func__); - +TEST(format_timestamp) { for (unsigned i = 0; i < 100; i++) { char buf[MAX(FORMAT_TIMESTAMP_MAX, FORMAT_TIMESPAN_MAX)]; usec_t x, y; @@ -370,9 +352,7 @@ static void test_format_timestamp(void) { } } -static void test_FORMAT_TIMESTAMP(void) { - log_info("/* %s */", __func__); - +TEST(FORMAT_TIMESTAMP) { for (unsigned i = 0; i < 100; i++) { _cleanup_free_ char *buf; usec_t x, y; @@ -389,9 +369,7 @@ static void test_FORMAT_TIMESTAMP(void) { } } -static void test_format_timestamp_relative(void) { - log_info("/* %s */", __func__); - +TEST(format_timestamp_relative) { char buf[MAX(FORMAT_TIMESTAMP_MAX, FORMAT_TIMESPAN_MAX)]; usec_t x; @@ -471,9 +449,7 @@ static void test_format_timestamp_utc_one(usec_t val, const char *result) { assert_se(streq_ptr(t, result)); } -static void test_format_timestamp_utc(void) { - log_info("/* %s */", __func__); - +TEST(format_timestamp_utc) { test_format_timestamp_utc_one(0, NULL); test_format_timestamp_utc_one(1, "Thu 1970-01-01 00:00:00 UTC"); test_format_timestamp_utc_one(USEC_PER_SEC, "Thu 1970-01-01 00:00:01 UTC"); @@ -489,12 +465,10 @@ static void test_format_timestamp_utc(void) { test_format_timestamp_utc_one(USEC_INFINITY, NULL); } -static void test_deserialize_dual_timestamp(void) { +TEST(deserialize_dual_timestamp) { int r; dual_timestamp t; - log_info("/* %s */", __func__); - r = deserialize_dual_timestamp("1234 5678", &t); assert_se(r == 0); assert_se(t.realtime == 1234); @@ -539,11 +513,9 @@ static void assert_similar(usec_t a, usec_t b) { assert_se(d < 10*USEC_PER_SEC); } -static void test_usec_shift_clock(void) { +TEST(usec_shift_clock) { usec_t rt, mn, bt; - log_info("/* %s */", __func__); - rt = now(CLOCK_REALTIME); mn = now(CLOCK_MONOTONIC); bt = now(clock_boottime_or_monotonic()); @@ -568,9 +540,7 @@ static void test_usec_shift_clock(void) { } } -static void test_in_utc_timezone(void) { - log_info("/* %s */", __func__); - +TEST(in_utc_timezone) { const char *tz = getenv("TZ"); assert_se(setenv("TZ", ":UTC", 1) >= 0); @@ -589,13 +559,10 @@ static void test_in_utc_timezone(void) { tzset(); } -static void test_map_clock_usec(void) { +TEST(map_clock_usec) { usec_t nowr, x, y, z; - log_info("/* %s */", __func__); - nowr = now(CLOCK_REALTIME); - - x = nowr; /* right now */ + x = nowr = now(CLOCK_REALTIME); /* right now */ y = map_clock_usec(x, CLOCK_REALTIME, CLOCK_MONOTONIC); z = map_clock_usec(y, CLOCK_MONOTONIC, CLOCK_REALTIME); /* Converting forth and back will introduce inaccuracies, since we cannot query both clocks atomically, but it should be small. Even on the slowest CI smaller than 1h */ @@ -621,9 +588,7 @@ static void test_map_clock_usec(void) { } } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_INFO); - +static void setup_test(void) { log_info("realtime=" USEC_FMT "\n" "monotonic=" USEC_FMT "\n" "boottime=" USEC_FMT "\n", @@ -631,29 +596,6 @@ int main(int argc, char *argv[]) { now(CLOCK_MONOTONIC), now(clock_boottime_or_monotonic())); - test_parse_sec(); - test_parse_sec_fix_0(); - test_parse_sec_def_infinity(); - test_parse_time(); - test_parse_nsec(); - test_format_timespan(1); - test_format_timespan(USEC_PER_MSEC); - test_format_timespan(USEC_PER_SEC); - test_verify_timezone(); - test_timezone_is_valid(); - test_get_timezones(); - test_usec_add(); - test_usec_sub_signed(); - test_usec_sub_unsigned(); - test_format_timestamp(); - test_FORMAT_TIMESTAMP(); - test_format_timestamp_relative(); - test_format_timestamp_utc(); - test_deserialize_dual_timestamp(); - test_usec_shift_clock(); - test_in_utc_timezone(); - test_map_clock_usec(); - /* Ensure time_t is signed */ assert_cc((time_t) -1 < (time_t) 1); @@ -661,6 +603,6 @@ int main(int argc, char *argv[]) { uintmax_t x = TIME_T_MAX; x++; assert_se((time_t) x < 0); - - return 0; } + +DEFINE_CUSTOM_TEST_MAIN(LOG_INFO, setup_test(), /* no outro */); diff --git a/src/test/test-tmpfile-util.c b/src/test/test-tmpfile-util.c index 39d524e2b6..5e5f60bdc8 100644 --- a/src/test/test-tmpfile-util.c +++ b/src/test/test-tmpfile-util.c @@ -25,9 +25,7 @@ static void test_tempfn_random_one(const char *p, const char *extra, const char assert_se(ret == r); } -static void test_tempfn_random(void) { - log_info("/* %s */", __func__); - +TEST(tempfn_random) { test_tempfn_random_one("", NULL, NULL, -EINVAL); test_tempfn_random_one(".", NULL, NULL, -EADDRNOTAVAIL); test_tempfn_random_one("..", NULL, NULL, -EINVAL); @@ -69,9 +67,7 @@ static void test_tempfn_xxxxxx_one(const char *p, const char *extra, const char assert_se(ret == r); } -static void test_tempfn_xxxxxx(void) { - log_info("/* %s */", __func__); - +TEST(tempfn_xxxxxx) { test_tempfn_xxxxxx_one("", NULL, NULL, -EINVAL); test_tempfn_xxxxxx_one(".", NULL, NULL, -EADDRNOTAVAIL); test_tempfn_xxxxxx_one("..", NULL, NULL, -EINVAL); @@ -96,11 +92,4 @@ static void test_tempfn_xxxxxx(void) { test_tempfn_xxxxxx_one("../foo/", "bar", "../.#barfoo", 0); } -int main(int argc, char **argv) { - test_setup_logging(LOG_DEBUG); - - test_tempfn_random(); - test_tempfn_xxxxxx(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-tmpfiles.c b/src/test/test-tmpfiles.c index 4c3389af8c..99243eb77a 100644 --- a/src/test/test-tmpfiles.c +++ b/src/test/test-tmpfiles.c @@ -17,14 +17,12 @@ #include "tmpfile-util.h" #include "util.h" -int main(int argc, char** argv) { +TEST(tmpfiles) { _cleanup_free_ char *cmd = NULL, *cmd2 = NULL, *ans = NULL, *ans2 = NULL, *d = NULL, *tmp = NULL, *line = NULL; _cleanup_close_ int fd = -1, fd2 = -1; - const char *p = argv[1] ?: "/tmp"; + const char *p = saved_argv[1] ?: "/tmp"; char *pattern; - test_setup_logging(LOG_DEBUG); - pattern = strjoina(p, "/systemd-test-XXXXXX"); fd = open_tmpfile_unlinkable(p, O_RDWR|O_CLOEXEC); @@ -61,6 +59,6 @@ int main(int argc, char** argv) { assert_se(read_one_line_file(d, &line) >= 0); assert_se(streq(line, "foobar")); assert_se(unlink(d) >= 0); - - return 0; } + +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-tpm2.c b/src/test/test-tpm2.c index a1fd0aa03b..c5f3d41da9 100644 --- a/src/test/test-tpm2.c +++ b/src/test/test-tpm2.c @@ -3,7 +3,7 @@ #include "tpm2-util.h" #include "tests.h" -static void test_tpm2_parse_pcrs(const char *s, uint32_t mask, int ret) { +static void test_tpm2_parse_pcrs_one(const char *s, uint32_t mask, int ret) { uint32_t m; assert_se(tpm2_parse_pcrs(s, &m) == ret); @@ -12,23 +12,20 @@ static void test_tpm2_parse_pcrs(const char *s, uint32_t mask, int ret) { assert_se(m == mask); } -int main(int argc, char *argv[]) { - - test_setup_logging(LOG_DEBUG); - - test_tpm2_parse_pcrs("", 0, 0); - test_tpm2_parse_pcrs("0", 1, 0); - test_tpm2_parse_pcrs("1", 2, 0); - test_tpm2_parse_pcrs("0,1", 3, 0); - test_tpm2_parse_pcrs("0+1", 3, 0); - test_tpm2_parse_pcrs("0-1", 0, -EINVAL); - test_tpm2_parse_pcrs("0,1,2", 7, 0); - test_tpm2_parse_pcrs("0+1+2", 7, 0); - test_tpm2_parse_pcrs("0+1,2", 7, 0); - test_tpm2_parse_pcrs("0,1+2", 7, 0); - test_tpm2_parse_pcrs("0,2", 5, 0); - test_tpm2_parse_pcrs("0+2", 5, 0); - test_tpm2_parse_pcrs("foo", 0, -EINVAL); - - return 0; +TEST(tpm2_parse_pcrs) { + test_tpm2_parse_pcrs_one("", 0, 0); + test_tpm2_parse_pcrs_one("0", 1, 0); + test_tpm2_parse_pcrs_one("1", 2, 0); + test_tpm2_parse_pcrs_one("0,1", 3, 0); + test_tpm2_parse_pcrs_one("0+1", 3, 0); + test_tpm2_parse_pcrs_one("0-1", 0, -EINVAL); + test_tpm2_parse_pcrs_one("0,1,2", 7, 0); + test_tpm2_parse_pcrs_one("0+1+2", 7, 0); + test_tpm2_parse_pcrs_one("0+1,2", 7, 0); + test_tpm2_parse_pcrs_one("0,1+2", 7, 0); + test_tpm2_parse_pcrs_one("0,2", 5, 0); + test_tpm2_parse_pcrs_one("0+2", 5, 0); + test_tpm2_parse_pcrs_one("foo", 0, -EINVAL); } + +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-uid-alloc-range.c b/src/test/test-uid-alloc-range.c index 8fa0b165d4..ac08b74a03 100644 --- a/src/test/test-uid-alloc-range.c +++ b/src/test/test-uid-alloc-range.c @@ -11,7 +11,7 @@ #include "tmpfile-util.h" #include "uid-alloc-range.h" -static void test_read_login_defs(const char *path) { +static void test_read_login_defs_one(const char *path) { log_info("/* %s(\"%s\") */", __func__, path ?: ""); _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-user-record.XXXXXX"; @@ -52,9 +52,13 @@ static void test_read_login_defs(const char *path) { } } -static void test_acquire_ugid_allocation_range(void) { - log_info("/* %s */", __func__); +TEST(read_login_defs) { + test_read_login_defs_one("/dev/null"); + test_read_login_defs_one("/etc/login.defs"); + test_read_login_defs_one(NULL); +} +TEST(acquire_ugid_allocation_range) { const UGIDAllocationRange *defs; assert_se(defs = acquire_ugid_allocation_range()); @@ -64,9 +68,7 @@ static void test_acquire_ugid_allocation_range(void) { log_info("system_gid_max="GID_FMT, defs->system_gid_max); } -static void test_uid_is_system(void) { - log_info("/* %s */", __func__); - +TEST(uid_is_system) { uid_t uid = 0; log_info("uid_is_system("UID_FMT") = %s", uid, yes_no(uid_is_system(uid))); @@ -77,9 +79,7 @@ static void test_uid_is_system(void) { log_info("uid_is_system("UID_FMT") = %s", uid, yes_no(uid_is_system(uid))); } -static void test_gid_is_system(void) { - log_info("/* %s */", __func__); - +TEST(gid_is_system) { gid_t gid = 0; log_info("gid_is_system("GID_FMT") = %s", gid, yes_no(gid_is_system(gid))); @@ -90,15 +90,4 @@ static void test_gid_is_system(void) { log_info("gid_is_system("GID_FMT") = %s", gid, yes_no(gid_is_system(gid))); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_DEBUG); - - test_read_login_defs("/dev/null"); - test_read_login_defs("/etc/login.defs"); - test_read_login_defs(NULL); - test_acquire_ugid_allocation_range(); - test_uid_is_system(); - test_gid_is_system(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-unaligned.c b/src/test/test-unaligned.c index b4d380b529..728c193339 100644 --- a/src/test/test-unaligned.c +++ b/src/test/test-unaligned.c @@ -2,6 +2,7 @@ #include "memory-util.h" #include "sparse-endian.h" +#include "tests.h" #include "unaligned.h" static uint8_t data[] = { @@ -9,7 +10,7 @@ static uint8_t data[] = { 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, }; -static void test_be(void) { +TEST(be) { uint8_t scratch[16]; assert_se(unaligned_read_be16(&data[0]) == 0x0001); @@ -75,7 +76,7 @@ static void test_be(void) { assert_se(memcmp(&scratch[7], &data[7], sizeof(uint64_t)) == 0); } -static void test_le(void) { +TEST(le) { uint8_t scratch[16]; assert_se(unaligned_read_le16(&data[0]) == 0x0100); @@ -142,7 +143,7 @@ static void test_le(void) { assert_se(memcmp(&scratch[7], &data[7], sizeof(uint64_t)) == 0); } -static void test_ne(void) { +TEST(ne) { uint16_t x = 4711; uint32_t y = 123456; uint64_t z = 9876543210; @@ -164,9 +165,4 @@ static void test_ne(void) { assert_se(z == 3); } -int main(int argc, const char *argv[]) { - test_be(); - test_le(); - test_ne(); - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-unit-file.c b/src/test/test-unit-file.c index 7900c1f460..0f8c25c218 100644 --- a/src/test/test-unit-file.c +++ b/src/test/test-unit-file.c @@ -7,9 +7,7 @@ #include "tests.h" #include "unit-file.h" -static void test_unit_validate_alias_symlink_and_warn(void) { - log_info("/* %s */", __func__); - +TEST(unit_validate_alias_symlink_and_warn) { assert_se(unit_validate_alias_symlink_and_warn("/path/a.service", "/other/b.service") == 0); assert_se(unit_validate_alias_symlink_and_warn("/path/a.service", "/other/b.socket") == -EXDEV); assert_se(unit_validate_alias_symlink_and_warn("/path/a.service", "/other/b.foobar") == -EXDEV); @@ -26,15 +24,17 @@ static void test_unit_validate_alias_symlink_and_warn(void) { assert_se(unit_validate_alias_symlink_and_warn("/path/a.slice", "/other/b.slice") == -EINVAL); } -static void test_unit_file_build_name_map(char **ids) { +TEST(unit_file_build_name_map) { _cleanup_(lookup_paths_free) LookupPaths lp = {}; _cleanup_hashmap_free_ Hashmap *unit_ids = NULL; _cleanup_hashmap_free_ Hashmap *unit_names = NULL; const char *k, *dst; - char **v; + char **v, **ids; usec_t mtime = 0; int r; + ids = strv_skip(saved_argv, 1); + assert_se(lookup_paths_init(&lp, UNIT_FILE_SYSTEM, 0, NULL) >= 0); assert_se(unit_file_build_name_map(&lp, &mtime, &unit_ids, &unit_names, NULL) == 1); @@ -86,9 +86,7 @@ static void test_unit_file_build_name_map(char **ids) { } } -static void test_runlevel_to_target(void) { - log_info("/* %s */", __func__); - +TEST(runlevel_to_target) { in_initrd_force(false); assert_se(streq_ptr(runlevel_to_target(NULL), NULL)); assert_se(streq_ptr(runlevel_to_target("unknown-runlevel"), NULL)); @@ -104,13 +102,4 @@ static void test_runlevel_to_target(void) { assert_se(streq_ptr(runlevel_to_target("rd.rescue"), SPECIAL_RESCUE_TARGET)); } -int main(int argc, char **argv) { - log_show_color(true); - test_setup_logging(LOG_DEBUG); - - test_unit_validate_alias_symlink_and_warn(); - test_unit_file_build_name_map(strv_skip(argv, 1)); - test_runlevel_to_target(); - - return 0; -} +DEFINE_CUSTOM_TEST_MAIN(LOG_DEBUG, log_show_color(true), /* no outro */); diff --git a/src/test/test-user-util.c b/src/test/test-user-util.c index af9537a3ab..907de54eaa 100644 --- a/src/test/test-user-util.c +++ b/src/test/test-user-util.c @@ -8,6 +8,7 @@ #include "memory-util.h" #include "path-util.h" #include "string-util.h" +#include "tests.h" #include "user-util.h" static void test_uid_to_name_one(uid_t uid, const char *name) { @@ -23,6 +24,13 @@ static void test_uid_to_name_one(uid_t uid, const char *name) { assert_se(streq_ptr(t, name)); } +TEST(uid_to_name) { + test_uid_to_name_one(0, "root"); + test_uid_to_name_one(UID_NOBODY, NOBODY_USER_NAME); + test_uid_to_name_one(0xFFFF, "65535"); + test_uid_to_name_one(0xFFFFFFFF, "4294967295"); +} + static void test_gid_to_name_one(gid_t gid, const char *name) { _cleanup_free_ char *t = NULL; @@ -36,12 +44,18 @@ static void test_gid_to_name_one(gid_t gid, const char *name) { assert_se(streq_ptr(t, name)); } -static void test_parse_uid(void) { +TEST(gid_to_name) { + test_gid_to_name_one(0, "root"); + test_gid_to_name_one(GID_NOBODY, NOBODY_GROUP_NAME); + test_gid_to_name_one(TTY_GID, "tty"); + test_gid_to_name_one(0xFFFF, "65535"); + test_gid_to_name_one(0xFFFFFFFF, "4294967295"); +} + +TEST(parse_uid) { int r; uid_t uid; - log_info("/* %s */", __func__); - r = parse_uid("0", &uid); assert_se(r == 0); assert_se(uid == 0); @@ -123,9 +137,7 @@ static void test_parse_uid(void) { assert_se(uid == 100); } -static void test_uid_ptr(void) { - log_info("/* %s */", __func__); - +TEST(uid_ptr) { assert_se(UID_TO_PTR(0) != NULL); assert_se(UID_TO_PTR(1000) != NULL); @@ -133,9 +145,7 @@ static void test_uid_ptr(void) { assert_se(PTR_TO_UID(UID_TO_PTR(1000)) == 1000); } -static void test_valid_user_group_name_relaxed(void) { - log_info("/* %s */", __func__); - +TEST(valid_user_group_name_relaxed) { assert_se(!valid_user_group_name(NULL, VALID_USER_RELAX)); assert_se(!valid_user_group_name("", VALID_USER_RELAX)); assert_se(!valid_user_group_name("1", VALID_USER_RELAX)); @@ -174,9 +184,7 @@ static void test_valid_user_group_name_relaxed(void) { assert_se(valid_user_group_name("Dāvis", VALID_USER_RELAX)); } -static void test_valid_user_group_name(void) { - log_info("/* %s */", __func__); - +TEST(valid_user_group_name) { assert_se(!valid_user_group_name(NULL, 0)); assert_se(!valid_user_group_name("", 0)); assert_se(!valid_user_group_name("1", 0)); @@ -216,9 +224,7 @@ static void test_valid_user_group_name(void) { assert_se(!valid_user_group_name("Dāvis", 0)); } -static void test_valid_user_group_name_or_numeric_relaxed(void) { - log_info("/* %s */", __func__); - +TEST(valid_user_group_name_or_numeric_relaxed) { assert_se(!valid_user_group_name(NULL, VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX)); assert_se(!valid_user_group_name("", VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX)); assert_se(valid_user_group_name("0", VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX)); @@ -254,9 +260,7 @@ static void test_valid_user_group_name_or_numeric_relaxed(void) { assert_se(valid_user_group_name("Dāvis", VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX)); } -static void test_valid_user_group_name_or_numeric(void) { - log_info("/* %s */", __func__); - +TEST(valid_user_group_name_or_numeric) { assert_se(!valid_user_group_name(NULL, VALID_USER_ALLOW_NUMERIC)); assert_se(!valid_user_group_name("", VALID_USER_ALLOW_NUMERIC)); assert_se(valid_user_group_name("0", VALID_USER_ALLOW_NUMERIC)); @@ -292,9 +296,7 @@ static void test_valid_user_group_name_or_numeric(void) { assert_se(!valid_user_group_name("Dāvis", VALID_USER_ALLOW_NUMERIC)); } -static void test_valid_gecos(void) { - log_info("/* %s */", __func__); - +TEST(valid_gecos) { assert_se(!valid_gecos(NULL)); assert_se(valid_gecos("")); assert_se(valid_gecos("test")); @@ -303,9 +305,7 @@ static void test_valid_gecos(void) { assert_se(!valid_gecos("In:valid")); } -static void test_valid_home(void) { - log_info("/* %s */", __func__); - +TEST(valid_home) { assert_se(!valid_home(NULL)); assert_se(!valid_home("")); assert_se(!valid_home(".")); @@ -346,6 +346,13 @@ static void test_get_user_creds_one(const char *id, const char *name, uid_t uid, assert_se(path_equal(rshell, shell)); } +TEST(get_user_creds) { + test_get_user_creds_one("root", "root", 0, 0, "/root", "/bin/sh"); + test_get_user_creds_one("0", "root", 0, 0, "/root", "/bin/sh"); + test_get_user_creds_one(NOBODY_USER_NAME, NOBODY_USER_NAME, UID_NOBODY, GID_NOBODY, "/", NOLOGIN); + test_get_user_creds_one("65534", NOBODY_USER_NAME, UID_NOBODY, GID_NOBODY, "/", NOLOGIN); +} + static void test_get_group_creds_one(const char *id, const char *name, gid_t gid) { gid_t rgid = GID_INVALID; int r; @@ -363,9 +370,14 @@ static void test_get_group_creds_one(const char *id, const char *name, gid_t gid assert_se(rgid == gid); } -static void test_make_salt(void) { - log_info("/* %s */", __func__); +TEST(get_group_creds) { + test_get_group_creds_one("root", "root", 0); + test_get_group_creds_one("0", "root", 0); + test_get_group_creds_one(NOBODY_GROUP_NAME, NOBODY_GROUP_NAME, GID_NOBODY); + test_get_group_creds_one("65534", NOBODY_GROUP_NAME, GID_NOBODY); +} +TEST(make_salt) { _cleanup_free_ char *s, *t; assert_se(make_salt(&s) == 0); @@ -377,14 +389,14 @@ static void test_make_salt(void) { assert_se(!streq(s, t)); } -static void test_in_gid(void) { +TEST(in_gid) { assert_se(in_gid(getgid()) >= 0); assert_se(in_gid(getegid()) >= 0); assert_se(in_gid(GID_INVALID) < 0); assert_se(in_gid(TTY_GID) == 0); /* The TTY gid is for owning ttys, it would be really really weird if we were in it. */ } -static void test_gid_lists_ops(void) { +TEST(gid_lists_ops) { static const gid_t l1[] = { 5, 10, 15, 20, 25}; static const gid_t l2[] = { 1, 2, 3, 15, 20, 25}; static const gid_t l3[] = { 5, 10, 15, 20, 25, 26, 27}; @@ -421,11 +433,9 @@ static void test_gid_lists_ops(void) { assert_se(gids); } -static void test_parse_uid_range(void) { +TEST(parse_uid_range) { uid_t a = 4711, b = 4711; - log_info("/* %s */", __func__); - assert_se(parse_uid_range("", &a, &b) == -EINVAL && a == 4711 && b == 4711); assert_se(parse_uid_range(" ", &a, &b) == -EINVAL && a == 4711 && b == 4711); assert_se(parse_uid_range("x", &a, &b) == -EINVAL && a == 4711 && b == 4711); @@ -462,7 +472,7 @@ static void test_mangle_gecos_one(const char *input, const char *expected) { assert_se(valid_gecos(p)); } -static void test_mangle_gecos(void) { +TEST(mangle_gecos) { test_mangle_gecos_one("", ""); test_mangle_gecos_one("root", "root"); test_mangle_gecos_one("wuff\nwuff", "wuff wuff"); @@ -473,45 +483,4 @@ static void test_mangle_gecos(void) { test_mangle_gecos_one("\xe2\x28\xa1", " ( "); } -int main(int argc, char *argv[]) { - test_uid_to_name_one(0, "root"); - test_uid_to_name_one(UID_NOBODY, NOBODY_USER_NAME); - test_uid_to_name_one(0xFFFF, "65535"); - test_uid_to_name_one(0xFFFFFFFF, "4294967295"); - - test_gid_to_name_one(0, "root"); - test_gid_to_name_one(GID_NOBODY, NOBODY_GROUP_NAME); - test_gid_to_name_one(TTY_GID, "tty"); - test_gid_to_name_one(0xFFFF, "65535"); - test_gid_to_name_one(0xFFFFFFFF, "4294967295"); - - test_get_user_creds_one("root", "root", 0, 0, "/root", "/bin/sh"); - test_get_user_creds_one("0", "root", 0, 0, "/root", "/bin/sh"); - test_get_user_creds_one(NOBODY_USER_NAME, NOBODY_USER_NAME, UID_NOBODY, GID_NOBODY, "/", NOLOGIN); - test_get_user_creds_one("65534", NOBODY_USER_NAME, UID_NOBODY, GID_NOBODY, "/", NOLOGIN); - - test_get_group_creds_one("root", "root", 0); - test_get_group_creds_one("0", "root", 0); - test_get_group_creds_one(NOBODY_GROUP_NAME, NOBODY_GROUP_NAME, GID_NOBODY); - test_get_group_creds_one("65534", NOBODY_GROUP_NAME, GID_NOBODY); - - test_parse_uid(); - test_uid_ptr(); - - test_valid_user_group_name_relaxed(); - test_valid_user_group_name(); - test_valid_user_group_name_or_numeric_relaxed(); - test_valid_user_group_name_or_numeric(); - test_valid_gecos(); - test_mangle_gecos(); - test_valid_home(); - - test_make_salt(); - - test_in_gid(); - test_gid_lists_ops(); - - test_parse_uid_range(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-utf8.c b/src/test/test-utf8.c index 763d17ce9e..a21fcd6fd2 100644 --- a/src/test/test-utf8.c +++ b/src/test/test-utf8.c @@ -7,9 +7,7 @@ #include "utf8.h" #include "util.h" -static void test_utf8_is_printable(void) { - log_info("/* %s */", __func__); - +TEST(utf8_is_printable) { assert_se(utf8_is_printable("ascii is valid\tunicode", 22)); assert_se(utf8_is_printable("\342\204\242", 3)); assert_se(!utf8_is_printable("\341\204", 2)); @@ -19,9 +17,7 @@ static void test_utf8_is_printable(void) { assert_se(utf8_is_printable("\t", 1)); } -static void test_utf8_n_is_valid(void) { - log_info("/* %s */", __func__); - +TEST(utf8_n_is_valid) { assert_se( utf8_is_valid_n("ascii is valid unicode", 21)); assert_se( utf8_is_valid_n("ascii is valid unicode", 22)); assert_se(!utf8_is_valid_n("ascii is valid unicode", 23)); @@ -38,25 +34,19 @@ static void test_utf8_n_is_valid(void) { assert_se(!utf8_is_valid_n("", 5)); } -static void test_utf8_is_valid(void) { - log_info("/* %s */", __func__); - +TEST(utf8_is_valid) { assert_se(utf8_is_valid("ascii is valid unicode")); assert_se(utf8_is_valid("\342\204\242")); assert_se(!utf8_is_valid("\341\204")); } -static void test_ascii_is_valid(void) { - log_info("/* %s */", __func__); - +TEST(ascii_is_valid) { assert_se( ascii_is_valid("alsdjf\t\vbarr\nba z")); assert_se(!ascii_is_valid("\342\204\242")); assert_se(!ascii_is_valid("\341\204")); } -static void test_ascii_is_valid_n(void) { - log_info("/* %s */", __func__); - +TEST(ascii_is_valid_n) { assert_se( ascii_is_valid_n("alsdjf\t\vbarr\nba z", 17)); assert_se( ascii_is_valid_n("alsdjf\t\vbarr\nba z", 16)); assert_se(!ascii_is_valid_n("alsdjf\t\vbarr\nba z", 18)); @@ -76,9 +66,7 @@ static void test_utf8_to_ascii_one(const char *s, int r_expected, const char *ex assert_se(streq_ptr(ans, expected)); } -static void test_utf8_to_ascii(void) { - log_info("/* %s */", __func__); - +TEST(utf8_to_ascii) { test_utf8_to_ascii_one("asdf", 0, "asdf"); test_utf8_to_ascii_one("dąb", 0, "d*b"); test_utf8_to_ascii_one("żęśłą óźń", 0, "***** ***"); @@ -93,9 +81,7 @@ static void test_utf8_to_ascii(void) { test_utf8_to_ascii_one("…👊🔪💐…", 0, "*****"); } -static void test_utf8_encoded_valid_unichar(void) { - log_info("/* %s */", __func__); - +TEST(utf8_encoded_valid_unichar) { assert_se(utf8_encoded_valid_unichar("\342\204\242", 1) == -EINVAL); /* truncated */ assert_se(utf8_encoded_valid_unichar("\342\204\242", 2) == -EINVAL); /* truncated */ assert_se(utf8_encoded_valid_unichar("\342\204\242", 3) == 3); @@ -113,11 +99,9 @@ static void test_utf8_encoded_valid_unichar(void) { assert_se(utf8_encoded_valid_unichar("\341\204\341\204", 5) == -EINVAL); } -static void test_utf8_escape_invalid(void) { +TEST(utf8_escape_invalid) { _cleanup_free_ char *p1, *p2, *p3; - log_info("/* %s */", __func__); - p1 = utf8_escape_invalid("goo goo goo"); log_debug("\"%s\"", p1); assert_se(utf8_is_valid(p1)); @@ -131,11 +115,9 @@ static void test_utf8_escape_invalid(void) { assert_se(utf8_is_valid(p3)); } -static void test_utf8_escape_non_printable(void) { +TEST(utf8_escape_non_printable) { _cleanup_free_ char *p1, *p2, *p3, *p4, *p5, *p6; - log_info("/* %s */", __func__); - p1 = utf8_escape_non_printable("goo goo goo"); log_debug("\"%s\"", p1); assert_se(utf8_is_valid(p1)); @@ -161,9 +143,7 @@ static void test_utf8_escape_non_printable(void) { assert_se(utf8_is_valid(p6)); } -static void test_utf8_escape_non_printable_full(void) { - log_info("/* %s */", __func__); - +TEST(utf8_escape_non_printable_full) { const char *s; FOREACH_STRING(s, "goo goo goo", /* ASCII */ @@ -189,14 +169,12 @@ static void test_utf8_escape_non_printable_full(void) { } } -static void test_utf16_to_utf8(void) { +TEST(utf16_to_utf8) { const char16_t utf16[] = { htole16('a'), htole16(0xd800), htole16('b'), htole16(0xdc00), htole16('c'), htole16(0xd801), htole16(0xdc37) }; static const char utf8[] = { 'a', 'b', 'c', 0xf0, 0x90, 0x90, 0xb7 }; _cleanup_free_ char16_t *b = NULL; _cleanup_free_ char *a = NULL; - log_info("/* %s */", __func__); - /* Convert UTF-16 to UTF-8, filtering embedded bad chars */ a = utf16_to_utf8(utf16, sizeof(utf16)); assert_se(a); @@ -213,9 +191,7 @@ static void test_utf16_to_utf8(void) { assert_se(memcmp(a, utf8, sizeof(utf8)) == 0); } -static void test_utf8_n_codepoints(void) { - log_info("/* %s */", __func__); - +TEST(utf8_n_codepoints) { assert_se(utf8_n_codepoints("abc") == 3); assert_se(utf8_n_codepoints("zażółcić gęślą jaźń") == 19); assert_se(utf8_n_codepoints("串") == 1); @@ -224,9 +200,7 @@ static void test_utf8_n_codepoints(void) { assert_se(utf8_n_codepoints("\xF1") == SIZE_MAX); } -static void test_utf8_console_width(void) { - log_info("/* %s */", __func__); - +TEST(utf8_console_width) { assert_se(utf8_console_width("abc") == 3); assert_se(utf8_console_width("zażółcić gęślą jaźń") == 19); assert_se(utf8_console_width("串") == 2); @@ -235,11 +209,9 @@ static void test_utf8_console_width(void) { assert_se(utf8_console_width("\xF1") == SIZE_MAX); } -static void test_utf8_to_utf16(void) { +TEST(utf8_to_utf16) { const char *p; - log_info("/* %s */", __func__); - FOREACH_STRING(p, "abc", "zażółcić gęślą jaźń", @@ -259,24 +231,4 @@ static void test_utf8_to_utf16(void) { } } -int main(int argc, char *argv[]) { - log_show_color(true); - test_setup_logging(LOG_INFO); - - test_utf8_n_is_valid(); - test_utf8_is_valid(); - test_utf8_is_printable(); - test_ascii_is_valid(); - test_ascii_is_valid_n(); - test_utf8_to_ascii(); - test_utf8_encoded_valid_unichar(); - test_utf8_escape_invalid(); - test_utf8_escape_non_printable(); - test_utf8_escape_non_printable_full(); - test_utf16_to_utf8(); - test_utf8_n_codepoints(); - test_utf8_console_width(); - test_utf8_to_utf16(); - - return 0; -} +DEFINE_CUSTOM_TEST_MAIN(LOG_INFO, log_show_color(true), /* no outro */); diff --git a/src/test/test-util.c b/src/test/test-util.c index 8a9ac9058a..413469a26f 100644 --- a/src/test/test-util.c +++ b/src/test/test-util.c @@ -17,9 +17,7 @@ #include "tests.h" #include "util.h" -static void test_u64log2(void) { - log_info("/* %s */", __func__); - +TEST(u64log2) { assert_se(u64log2(0) == 0); assert_se(u64log2(8) == 3); assert_se(u64log2(9) == 3); @@ -29,9 +27,7 @@ static void test_u64log2(void) { assert_se(u64log2(1024*1024+5) == 20); } -static void test_protect_errno(void) { - log_info("/* %s */", __func__); - +TEST(protect_errno) { errno = 12; { PROTECT_ERRNO; @@ -46,9 +42,7 @@ static void test_unprotect_errno_inner_function(void) { errno = 2222; } -static void test_unprotect_errno(void) { - log_info("/* %s */", __func__); - +TEST(unprotect_errno) { errno = 4711; PROTECT_ERRNO; @@ -64,9 +58,7 @@ static void test_unprotect_errno(void) { assert_se(errno == 4711); } -static void test_log2i(void) { - log_info("/* %s */", __func__); - +TEST(log2i) { assert_se(log2i(1) == 0); assert_se(log2i(2) == 1); assert_se(log2i(3) == 1); @@ -77,25 +69,21 @@ static void test_log2i(void) { assert_se(log2i(INT_MAX) == sizeof(int)*8-2); } -static void test_eqzero(void) { +TEST(eqzero) { const uint32_t zeros[] = {0, 0, 0}; const uint32_t ones[] = {1, 1}; const uint32_t mixed[] = {0, 1, 0, 0, 0}; const uint8_t longer[] = {[55] = 255}; - log_info("/* %s */", __func__); - assert_se(eqzero(zeros)); assert_se(!eqzero(ones)); assert_se(!eqzero(mixed)); assert_se(!eqzero(longer)); } -static void test_raw_clone(void) { +TEST(raw_clone) { pid_t parent, pid, pid2; - log_info("/* %s */", __func__); - parent = getpid(); log_info("before clone: getpid()→"PID_FMT, parent); assert_se(raw_getpid() == parent); @@ -122,11 +110,9 @@ static void test_raw_clone(void) { assert_se(errno == EINVAL || ERRNO_IS_PRIVILEGE(errno)); /* Certain container environments prohibit namespaces to us, don't fail in that case */ } -static void test_physical_memory(void) { +TEST(physical_memory) { uint64_t p; - log_info("/* %s */", __func__); - p = physical_memory(); assert_se(p > 0); assert_se(p < UINT64_MAX); @@ -135,11 +121,9 @@ static void test_physical_memory(void) { log_info("Memory: %s (%" PRIu64 ")", FORMAT_BYTES(p), p); } -static void test_physical_memory_scale(void) { +TEST(physical_memory_scale) { uint64_t p; - log_info("/* %s */", __func__); - p = physical_memory(); assert_se(physical_memory_scale(0, 100) == 0); @@ -171,11 +155,9 @@ static void test_physical_memory_scale(void) { assert_se(physical_memory_scale(UINT64_MAX/4, UINT64_MAX) == UINT64_MAX); } -static void test_system_tasks_max(void) { +TEST(system_tasks_max) { uint64_t t; - log_info("/* %s */", __func__); - t = system_tasks_max(); assert_se(t > 0); assert_se(t < UINT64_MAX); @@ -183,11 +165,9 @@ static void test_system_tasks_max(void) { log_info("Max tasks: %" PRIu64, t); } -static void test_system_tasks_max_scale(void) { +TEST(system_tasks_max_scale) { uint64_t t; - log_info("/* %s */", __func__); - t = system_tasks_max(); assert_se(system_tasks_max_scale(0, 100) == 0); @@ -212,19 +192,4 @@ static void test_system_tasks_max_scale(void) { assert_se(system_tasks_max_scale(UINT64_MAX/4, UINT64_MAX) == UINT64_MAX); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_INFO); - - test_u64log2(); - test_protect_errno(); - test_unprotect_errno(); - test_log2i(); - test_eqzero(); - test_raw_clone(); - test_physical_memory(); - test_physical_memory_scale(); - test_system_tasks_max(); - test_system_tasks_max_scale(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-verbs.c b/src/test/test-verbs.c index b7a0cbf527..6e30794fa3 100644 --- a/src/test/test-verbs.c +++ b/src/test/test-verbs.c @@ -4,6 +4,7 @@ #include "macro.h" #include "strv.h" +#include "tests.h" #include "verbs.h" static int noop_dispatcher(int argc, char *argv[], void *userdata) { @@ -14,7 +15,7 @@ static int noop_dispatcher(int argc, char *argv[], void *userdata) { optind = 0; \ assert_se(dispatch_verb(strv_length(argv), argv, verbs, NULL) == expected); -static void test_verbs(void) { +TEST(verbs) { static const Verb verbs[] = { { "help", VERB_ANY, VERB_ANY, 0, noop_dispatcher }, { "list-images", VERB_ANY, 1, 0, noop_dispatcher }, @@ -46,7 +47,7 @@ static void test_verbs(void) { test_dispatch_one(STRV_MAKE_EMPTY, verbs, 0); } -static void test_verbs_no_default(void) { +TEST(verbs_no_default) { static const Verb verbs[] = { { "help", VERB_ANY, VERB_ANY, 0, noop_dispatcher }, {}, @@ -55,9 +56,4 @@ static void test_verbs_no_default(void) { test_dispatch_one(STRV_MAKE(NULL), verbs, -EINVAL); } -int main(int argc, char *argv[]) { - test_verbs(); - test_verbs_no_default(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-web-util.c b/src/test/test-web-util.c index 853ea9c291..f17e5ec988 100644 --- a/src/test/test-web-util.c +++ b/src/test/test-web-util.c @@ -1,9 +1,10 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "macro.h" +#include "tests.h" #include "web-util.h" -static void test_is_valid_documentation_url(void) { +TEST(is_valid_documentation_url) { assert_se(documentation_url_is_valid("http://www.freedesktop.org/wiki/Software/systemd")); assert_se(documentation_url_is_valid("https://www.kernel.org/doc/Documentation/binfmt_misc.txt")); /* dead */ assert_se(documentation_url_is_valid("https://www.kernel.org/doc/Documentation/admin-guide/binfmt-misc.rst")); @@ -17,8 +18,4 @@ static void test_is_valid_documentation_url(void) { assert_se(!documentation_url_is_valid("")); } -int main(int argc, char *argv[]) { - test_is_valid_documentation_url(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-xattr-util.c b/src/test/test-xattr-util.c index b30a639ff3..2ddb4b4b84 100644 --- a/src/test/test-xattr-util.c +++ b/src/test/test-xattr-util.c @@ -15,15 +15,13 @@ #include "tmpfile-util.h" #include "xattr-util.h" -static void test_getxattr_at_malloc(void) { +TEST(getxattr_at_malloc) { char t[] = "/var/tmp/xattrtestXXXXXX"; _cleanup_free_ char *value = NULL; _cleanup_close_ int fd = -1; const char *x; int r; - log_info("/* %s */", __func__); - assert_se(mkdtemp(t)); x = strjoina(t, "/test"); assert_se(touch(x) >= 0); @@ -61,14 +59,12 @@ cleanup: assert_se(rmdir(t) >= 0); } -static void test_getcrtime(void) { +TEST(getcrtime) { _cleanup_close_ int fd = -1; const char *vt; usec_t usec, k; int r; - log_info("/* %s */", __func__); - assert_se(var_tmp_dir(&vt) >= 0); fd = open_tmpfile_unlinkable(vt, O_RDWR); @@ -90,11 +86,4 @@ static void test_getcrtime(void) { } } -int main(void) { - test_setup_logging(LOG_DEBUG); - - test_getxattr_at_malloc(); - test_getcrtime(); - - return 0; -} +DEFINE_TEST_MAIN(LOG_DEBUG); From c462e63eea7c781e00a3ce83055967dbbd67bf96 Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Wed, 24 Nov 2021 12:11:17 +0100 Subject: [PATCH 4/4] test: Use TEST macro in more cases This converts to TEST macro in less trivial cases. This is mostly due to having an intro or outro before/after the actual tests. Some notable changes: - add a "test" to make sure the hashmap and ordered_hashmap tests from different compilation units are actually run in test-hashmap.c - make root arg a global var in test-install-root.c - slightly rework an EFI specific test in test-proc-cmdline.c - usage of saved_argv/saved_argc in test-process-util.c - splitting test-rlimit-util.c into several tests - moving the hwdb open check into intro in test-sd-hwdb.c - condense several "tests" into one in test-udev-util.c --- src/test/test-cgroup-setup.c | 40 +++---- src/test/test-chown-rec.c | 19 ++-- src/test/test-format-table.c | 42 +++----- src/test/test-hashmap-plain.c | 170 +++++++---------------------- src/test/test-hashmap.c | 56 +++------- src/test/test-install-root.c | 59 ++++------ src/test/test-mountpoint-util.c | 61 +++++------ src/test/test-proc-cmdline.c | 67 +++++------- src/test/test-process-util.c | 103 ++++++------------ src/test/test-rlimit-util.c | 91 ++++++++-------- src/test/test-sd-hwdb.c | 38 +++---- src/test/test-serialize.c | 36 +++---- src/test/test-udev-util.c | 185 +++++--------------------------- 13 files changed, 299 insertions(+), 668 deletions(-) diff --git a/src/test/test-cgroup-setup.c b/src/test/test-cgroup-setup.c index 37ef66b0fd..018992f96d 100644 --- a/src/test/test-cgroup-setup.c +++ b/src/test/test-cgroup-setup.c @@ -11,7 +11,7 @@ #include "tests.h" #include "version.h" -static void test_is_wanted_print(bool header) { +static void test_is_wanted_print_one(bool header) { _cleanup_free_ char *cmdline = NULL; log_info("-- %s --", __func__); @@ -28,46 +28,46 @@ static void test_is_wanted_print(bool header) { log_info(" "); } -static void test_is_wanted(void) { +TEST(is_wanted_print) { + test_is_wanted_print_one(true); + test_is_wanted_print_one(false); /* run twice to test caching */ +} + +TEST(is_wanted) { assert_se(setenv("SYSTEMD_PROC_CMDLINE", "systemd.unified_cgroup_hierarchy", 1) >= 0); - test_is_wanted_print(false); + test_is_wanted_print_one(false); assert_se(setenv("SYSTEMD_PROC_CMDLINE", "systemd.unified_cgroup_hierarchy=0", 1) >= 0); - test_is_wanted_print(false); + test_is_wanted_print_one(false); assert_se(setenv("SYSTEMD_PROC_CMDLINE", "systemd.unified_cgroup_hierarchy=0 " "systemd.legacy_systemd_cgroup_controller", 1) >= 0); - test_is_wanted_print(false); + test_is_wanted_print_one(false); assert_se(setenv("SYSTEMD_PROC_CMDLINE", "systemd.unified_cgroup_hierarchy=0 " "systemd.legacy_systemd_cgroup_controller=0", 1) >= 0); - test_is_wanted_print(false); + test_is_wanted_print_one(false); /* cgroup_no_v1=all implies unified cgroup hierarchy, unless otherwise * explicitly specified. */ assert_se(setenv("SYSTEMD_PROC_CMDLINE", "cgroup_no_v1=all", 1) >= 0); - test_is_wanted_print(false); + test_is_wanted_print_one(false); assert_se(setenv("SYSTEMD_PROC_CMDLINE", "cgroup_no_v1=all " "systemd.unified_cgroup_hierarchy=0", 1) >= 0); - test_is_wanted_print(false); + test_is_wanted_print_one(false); } -int main(void) { - test_setup_logging(LOG_DEBUG); - - if (access("/proc/cmdline", R_OK) < 0 && ERRNO_IS_PRIVILEGE(errno)) - return log_tests_skipped("can't read /proc/cmdline"); - - test_is_wanted_print(true); - test_is_wanted_print(false); /* run twice to test caching */ - test_is_wanted(); - - return 0; -} +DEFINE_CUSTOM_TEST_MAIN( + LOG_DEBUG, + ({ + if (access("/proc/cmdline", R_OK) < 0 && ERRNO_IS_PRIVILEGE(errno)) + return log_tests_skipped("can't read /proc/cmdline"); + }), + /* no outro */); diff --git a/src/test/test-chown-rec.c b/src/test/test-chown-rec.c index 66c6fd97d1..53d44566d5 100644 --- a/src/test/test-chown-rec.c +++ b/src/test/test-chown-rec.c @@ -40,7 +40,7 @@ static bool has_xattr(const char *p) { return true; } -static void test_chown_recursive(void) { +TEST(chown_recursive) { _cleanup_(rm_rf_physical_and_freep) char *t = NULL; struct stat st; const char *p; @@ -149,13 +149,10 @@ static void test_chown_recursive(void) { assert_se(!has_xattr(p)); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_DEBUG); - - if (geteuid() != 0) - return log_tests_skipped("not running as root"); - - test_chown_recursive(); - - return EXIT_SUCCESS; -} +DEFINE_CUSTOM_TEST_MAIN( + LOG_DEBUG, + ({ + if (geteuid() != 0) + return log_tests_skipped("not running as root"); + }), + /* no outro */); diff --git a/src/test/test-format-table.c b/src/test/test-format-table.c index 7a4c98eba6..a3b29ca337 100644 --- a/src/test/test-format-table.c +++ b/src/test/test-format-table.c @@ -6,14 +6,13 @@ #include "format-table.h" #include "string-util.h" #include "strv.h" +#include "tests.h" #include "time-util.h" -static void test_issue_9549(void) { +TEST(issue_9549) { _cleanup_(table_unrefp) Table *table = NULL; _cleanup_free_ char *formatted = NULL; - log_info("/* %s */", __func__); - assert_se(table = table_new("name", "type", "ro", "usage", "created", "modified")); assert_se(table_set_align_percent(table, TABLE_HEADER_CELL(3), 100) >= 0); assert_se(table_add_many(table, @@ -34,12 +33,10 @@ static void test_issue_9549(void) { )); } -static void test_multiline(void) { +TEST(multiline) { _cleanup_(table_unrefp) Table *table = NULL; _cleanup_free_ char *formatted = NULL; - log_info("/* %s */", __func__); - assert_se(table = table_new("foo", "bar")); assert_se(table_set_align_percent(table, TABLE_HEADER_CELL(1), 100) >= 0); @@ -148,12 +145,10 @@ static void test_multiline(void) { formatted = mfree(formatted); } -static void test_strv(void) { +TEST(strv) { _cleanup_(table_unrefp) Table *table = NULL; _cleanup_free_ char *formatted = NULL; - log_info("/* %s */", __func__); - assert_se(table = table_new("foo", "bar")); assert_se(table_set_align_percent(table, TABLE_HEADER_CELL(1), 100) >= 0); @@ -262,12 +257,10 @@ static void test_strv(void) { formatted = mfree(formatted); } -static void test_strv_wrapped(void) { +TEST(strv_wrapped) { _cleanup_(table_unrefp) Table *table = NULL; _cleanup_free_ char *formatted = NULL; - log_info("/* %s */", __func__); - assert_se(table = table_new("foo", "bar")); assert_se(table_set_align_percent(table, TABLE_HEADER_CELL(1), 100) >= 0); @@ -366,12 +359,10 @@ static void test_strv_wrapped(void) { formatted = mfree(formatted); } -static void test_json(void) { +TEST(json) { _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *w = NULL; _cleanup_(table_unrefp) Table *t = NULL; - log_info("/* %s */", __func__); - assert_se(t = table_new("foo bar", "quux", "piep miau")); assert_se(table_set_json_field_name(t, 2, "zzz") >= 0); @@ -401,13 +392,10 @@ static void test_json(void) { assert_se(json_variant_equal(v, w)); } -int main(int argc, char *argv[]) { +TEST(table) { _cleanup_(table_unrefp) Table *t = NULL; _cleanup_free_ char *formatted = NULL; - assert_se(setenv("SYSTEMD_COLORS", "0", 1) >= 0); - assert_se(setenv("COLUMNS", "40", 1) >= 0); - assert_se(t = table_new("one", "two", "three")); assert_se(table_set_align_percent(t, TABLE_HEADER_CELL(2), 100) >= 0); @@ -539,12 +527,12 @@ int main(int argc, char *argv[]) { " yes fäää yes fäää fäää\n" " yes xxx yes xxx xxx\n" "5min 5min \n")); - - test_issue_9549(); - test_multiline(); - test_strv(); - test_strv_wrapped(); - test_json(); - - return 0; } + +DEFINE_CUSTOM_TEST_MAIN( + LOG_INFO, + ({ + assert_se(setenv("SYSTEMD_COLORS", "0", 1) >= 0); + assert_se(setenv("COLUMNS", "40", 1) >= 0); + }), + /* no outro */); diff --git a/src/test/test-hashmap-plain.c b/src/test/test-hashmap-plain.c index 45a4755399..36a775012b 100644 --- a/src/test/test-hashmap-plain.c +++ b/src/test/test-hashmap-plain.c @@ -10,14 +10,10 @@ #include "time-util.h" #include "tests.h" -void test_hashmap_funcs(void); - -static void test_hashmap_replace(void) { +TEST(hashmap_replace) { Hashmap *m; char *val1, *val2, *val3, *val4, *val5, *r; - log_info("/* %s */", __func__); - m = hashmap_new(&string_hash_ops); val1 = strdup("val1"); @@ -52,12 +48,10 @@ static void test_hashmap_replace(void) { hashmap_free(m); } -static void test_hashmap_copy(void) { +TEST(hashmap_copy) { Hashmap *m, *copy; char *val1, *val2, *val3, *val4, *r; - log_info("/* %s */", __func__); - val1 = strdup("val1"); assert_se(val1); val2 = strdup("val2"); @@ -89,13 +83,11 @@ static void test_hashmap_copy(void) { hashmap_free(m); } -static void test_hashmap_get_strv(void) { +TEST(hashmap_get_strv) { Hashmap *m; char **strv; char *val1, *val2, *val3, *val4; - log_info("/* %s */", __func__); - val1 = strdup("val1"); assert_se(val1); val2 = strdup("val2"); @@ -128,12 +120,10 @@ static void test_hashmap_get_strv(void) { hashmap_free(m); } -static void test_hashmap_move_one(void) { +TEST(hashmap_move_one) { Hashmap *m, *n; char *val1, *val2, *val3, *val4, *r; - log_info("/* %s */", __func__); - val1 = strdup("val1"); assert_se(val1); val2 = strdup("val2"); @@ -169,12 +159,10 @@ static void test_hashmap_move_one(void) { hashmap_free_free(n); } -static void test_hashmap_move(void) { +TEST(hashmap_move) { Hashmap *m, *n; char *val1, *val2, *val3, *val4, *r; - log_info("/* %s */", __func__); - val1 = strdup("val1"); assert_se(val1); val2 = strdup("val2"); @@ -213,12 +201,10 @@ static void test_hashmap_move(void) { hashmap_free_free(n); } -static void test_hashmap_update(void) { +TEST(hashmap_update) { Hashmap *m; char *val1, *val2, *r; - log_info("/* %s */", __func__); - m = hashmap_new(&string_hash_ops); val1 = strdup("old_value"); assert_se(val1); @@ -242,15 +228,13 @@ static void test_hashmap_update(void) { hashmap_free(m); } -static void test_hashmap_put(void) { +TEST(hashmap_put) { Hashmap *m = NULL; int valid_hashmap_put; void *val1 = (void*) "val 1"; void *val2 = (void*) "val 2"; _cleanup_free_ char* key1 = NULL; - log_info("/* %s */", __func__); - assert_se(hashmap_ensure_allocated(&m, &string_hash_ops) == 1); assert_se(m); @@ -265,12 +249,10 @@ static void test_hashmap_put(void) { hashmap_free(m); } -static void test_hashmap_remove(void) { +TEST(hashmap_remove1) { _cleanup_hashmap_free_ Hashmap *m = NULL; char *r; - log_info("/* %s */", __func__); - r = hashmap_remove(NULL, "key 1"); assert_se(r == NULL); @@ -291,7 +273,7 @@ static void test_hashmap_remove(void) { assert_se(!hashmap_get(m, "key 1")); } -static void test_hashmap_remove2(void) { +TEST(hashmap_remove2) { _cleanup_hashmap_free_free_free_ Hashmap *m = NULL; char key1[] = "key 1"; char key2[] = "key 2"; @@ -299,8 +281,6 @@ static void test_hashmap_remove2(void) { char val2[] = "val 2"; void *r, *r2; - log_info("/* %s */", __func__); - r = hashmap_remove2(NULL, "key 1", &r2); assert_se(r == NULL); @@ -324,15 +304,13 @@ static void test_hashmap_remove2(void) { assert_se(!hashmap_get(m, key1)); } -static void test_hashmap_remove_value(void) { +TEST(hashmap_remove_value) { _cleanup_hashmap_free_ Hashmap *m = NULL; char *r; char val1[] = "val 1"; char val2[] = "val 2"; - log_info("/* %s */", __func__); - r = hashmap_remove_value(NULL, "key 1", val1); assert_se(r == NULL); @@ -360,13 +338,11 @@ static void test_hashmap_remove_value(void) { assert_se(!hashmap_get(m, "key 1")); } -static void test_hashmap_remove_and_put(void) { +TEST(hashmap_remove_and_put) { _cleanup_hashmap_free_ Hashmap *m = NULL; int valid; char *r; - log_info("/* %s */", __func__); - m = hashmap_new(&string_hash_ops); assert_se(m); @@ -392,7 +368,7 @@ static void test_hashmap_remove_and_put(void) { assert_se(valid == -EEXIST); } -static void test_hashmap_remove_and_replace(void) { +TEST(hashmap_remove_and_replace) { _cleanup_hashmap_free_ Hashmap *m = NULL; int valid; void *key1 = UINT_TO_PTR(1); @@ -401,8 +377,6 @@ static void test_hashmap_remove_and_replace(void) { void *r; int i, j; - log_info("/* %s */", __func__); - m = hashmap_new(&trivial_hash_ops); assert_se(m); @@ -450,12 +424,10 @@ static void test_hashmap_remove_and_replace(void) { } } -static void test_hashmap_ensure_allocated(void) { +TEST(hashmap_ensure_allocated) { _cleanup_hashmap_free_ Hashmap *m = NULL; int r; - log_info("/* %s */", __func__); - r = hashmap_ensure_allocated(&m, &string_hash_ops); assert_se(r == 1); @@ -467,7 +439,7 @@ static void test_hashmap_ensure_allocated(void) { assert_se(r == 0); } -static void test_hashmap_foreach_key(void) { +TEST(hashmap_foreach_key) { Hashmap *m; bool key_found[] = { false, false, false, false }; const char *s; @@ -478,8 +450,6 @@ static void test_hashmap_foreach_key(void) { "key 3\0" "key 4\0"; - log_info("/* %s */", __func__); - m = hashmap_new(&string_hash_ops); NULSTR_FOREACH(key, key_table) @@ -503,14 +473,12 @@ static void test_hashmap_foreach_key(void) { hashmap_free(m); } -static void test_hashmap_foreach(void) { +TEST(hashmap_foreach) { Hashmap *m; bool value_found[] = { false, false, false, false }; char *val1, *val2, *val3, *val4, *s; unsigned count; - log_info("/* %s */", __func__); - val1 = strdup("my val1"); assert_se(val1); val2 = strdup("my val2"); @@ -556,12 +524,10 @@ static void test_hashmap_foreach(void) { hashmap_free_free(m); } -static void test_hashmap_merge(void) { +TEST(hashmap_merge) { Hashmap *m, *n; char *val1, *val2, *val3, *val4, *r; - log_info("/* %s */", __func__); - val1 = strdup("my val1"); assert_se(val1); val2 = strdup("my val2"); @@ -591,12 +557,10 @@ static void test_hashmap_merge(void) { hashmap_free_free(m); } -static void test_hashmap_contains(void) { +TEST(hashmap_contains) { Hashmap *m; char *val1; - log_info("/* %s */", __func__); - val1 = strdup("my val"); assert_se(val1); @@ -613,12 +577,10 @@ static void test_hashmap_contains(void) { hashmap_free_free(m); } -static void test_hashmap_isempty(void) { +TEST(hashmap_isempty) { Hashmap *m; char *val1; - log_info("/* %s */", __func__); - val1 = strdup("my val"); assert_se(val1); @@ -632,12 +594,10 @@ static void test_hashmap_isempty(void) { hashmap_free_free(m); } -static void test_hashmap_size(void) { +TEST(hashmap_size) { Hashmap *m; char *val1, *val2, *val3, *val4; - log_info("/* %s */", __func__); - val1 = strdup("my val"); assert_se(val1); val2 = strdup("my val"); @@ -663,13 +623,11 @@ static void test_hashmap_size(void) { hashmap_free_free(m); } -static void test_hashmap_get(void) { +TEST(hashmap_get) { Hashmap *m; char *r; char *val; - log_info("/* %s */", __func__); - val = strdup("my val"); assert_se(val); @@ -690,15 +648,13 @@ static void test_hashmap_get(void) { hashmap_free_free(m); } -static void test_hashmap_get2(void) { +TEST(hashmap_get2) { Hashmap *m; char *r; char *val; char key_orig[] = "Key 1"; void *key_copy; - log_info("/* %s */", __func__); - val = strdup("my val"); assert_se(val); @@ -734,7 +690,7 @@ static const struct hash_ops crippled_hashmap_ops = { .compare = trivial_compare_func, }; -static void test_hashmap_many(void) { +TEST(hashmap_many) { Hashmap *h; unsigned i, j; void *v, *k; @@ -785,7 +741,7 @@ static void test_hashmap_many(void) { extern unsigned custom_counter; extern const struct hash_ops boring_hash_ops, custom_hash_ops; -static void test_hashmap_free(void) { +TEST(hashmap_free) { Hashmap *h; bool slow = slow_tests_enabled(); usec_t ts, n; @@ -835,13 +791,11 @@ static void item_seen(Item *item) { item->seen++; } -static void test_hashmap_free_with_destructor(void) { +TEST(hashmap_free_with_destructor) { Hashmap *m; struct Item items[4] = {}; unsigned i; - log_info("/* %s */", __func__); - assert_se(m = hashmap_new(NULL)); for (i = 0; i < ELEMENTSOF(items) - 1; i++) assert_se(hashmap_put(m, INT_TO_PTR(i), items + i) == 1); @@ -853,11 +807,9 @@ static void test_hashmap_free_with_destructor(void) { assert_se(items[3].seen == 0); } -static void test_hashmap_first(void) { +TEST(hashmap_first) { _cleanup_hashmap_free_ Hashmap *m = NULL; - log_info("/* %s */", __func__); - m = hashmap_new(&string_hash_ops); assert_se(m); @@ -872,11 +824,9 @@ static void test_hashmap_first(void) { #endif } -static void test_hashmap_first_key(void) { +TEST(hashmap_first_key) { _cleanup_hashmap_free_ Hashmap *m = NULL; - log_info("/* %s */", __func__); - m = hashmap_new(&string_hash_ops); assert_se(m); @@ -891,11 +841,9 @@ static void test_hashmap_first_key(void) { #endif } -static void test_hashmap_steal_first_key(void) { +TEST(hashmap_steal_first_key) { _cleanup_hashmap_free_ Hashmap *m = NULL; - log_info("/* %s */", __func__); - m = hashmap_new(&string_hash_ops); assert_se(m); @@ -906,13 +854,11 @@ static void test_hashmap_steal_first_key(void) { assert_se(hashmap_isempty(m)); } -static void test_hashmap_steal_first(void) { +TEST(hashmap_steal_first) { _cleanup_hashmap_free_ Hashmap *m = NULL; int seen[3] = {}; char *val; - log_info("/* %s */", __func__); - m = hashmap_new(&string_hash_ops); assert_se(m); @@ -928,11 +874,9 @@ static void test_hashmap_steal_first(void) { assert_se(hashmap_isempty(m)); } -static void test_hashmap_clear_free_free(void) { +TEST(hashmap_clear_free_free) { _cleanup_hashmap_free_ Hashmap *m = NULL; - log_info("/* %s */", __func__); - m = hashmap_new(&string_hash_ops); assert_se(m); @@ -954,11 +898,9 @@ static void test_hashmap_clear_free_free(void) { DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(test_hash_ops_key, char, string_hash_func, string_compare_func, free); DEFINE_PRIVATE_HASH_OPS_FULL(test_hash_ops_full, char, string_hash_func, string_compare_func, free, char, free); -static void test_hashmap_clear_free_with_destructor(void) { +TEST(hashmap_clear_free_with_destructor) { _cleanup_hashmap_free_ Hashmap *m = NULL; - log_info("/* %s */", __func__); - m = hashmap_new(&test_hash_ops_key); assert_se(m); @@ -981,11 +923,9 @@ static void test_hashmap_clear_free_with_destructor(void) { assert_se(hashmap_isempty(m)); } -static void test_hashmap_reserve(void) { +TEST(hashmap_reserve) { _cleanup_hashmap_free_ Hashmap *m = NULL; - log_info("/* %s */", __func__); - m = hashmap_new(&string_hash_ops); assert_se(hashmap_reserve(m, 1) == 0); @@ -1000,11 +940,9 @@ static void test_hashmap_reserve(void) { assert_se(hashmap_reserve(m, UINT_MAX - 1) == -ENOMEM); } -static void test_path_hashmap(void) { +TEST(path_hashmap) { _cleanup_hashmap_free_ Hashmap *h = NULL; - log_info("/* %s */", __func__); - assert_se(h = hashmap_new(&path_hash_ops)); assert_se(hashmap_put(h, "foo", INT_TO_PTR(1)) >= 0); @@ -1035,12 +973,10 @@ static void test_path_hashmap(void) { assert_se(hashmap_get(h, "foo././//ba.r////.quux///.//.") == INT_TO_PTR(9)); } -static void test_string_strv_hashmap(void) { +TEST(string_strv_hashmap) { _cleanup_hashmap_free_ Hashmap *m = NULL; char **s; - log_info("/* %s */", __func__); - assert_se(string_strv_hashmap_put(&m, "foo", "bar") == 1); assert_se(string_strv_hashmap_put(&m, "foo", "bar") == 0); assert_se(string_strv_hashmap_put(&m, "foo", "BAR") == 1); @@ -1062,40 +998,8 @@ static void test_string_strv_hashmap(void) { assert_se(strv_equal(s, STRV_MAKE("bar", "BAR"))); } -void test_hashmap_funcs(void) { - log_info("/************ %s ************/", __func__); - - test_hashmap_copy(); - test_hashmap_get_strv(); - test_hashmap_move_one(); - test_hashmap_move(); - test_hashmap_replace(); - test_hashmap_update(); - test_hashmap_put(); - test_hashmap_remove(); - test_hashmap_remove2(); - test_hashmap_remove_value(); - test_hashmap_remove_and_put(); - test_hashmap_remove_and_replace(); - test_hashmap_ensure_allocated(); - test_hashmap_foreach(); - test_hashmap_foreach_key(); - test_hashmap_contains(); - test_hashmap_merge(); - test_hashmap_isempty(); - test_hashmap_get(); - test_hashmap_get2(); - test_hashmap_size(); - test_hashmap_many(); - test_hashmap_free(); - test_hashmap_free_with_destructor(); - test_hashmap_first(); - test_hashmap_first_key(); - test_hashmap_steal_first_key(); - test_hashmap_steal_first(); - test_hashmap_clear_free_free(); - test_hashmap_clear_free_with_destructor(); - test_hashmap_reserve(); - test_path_hashmap(); - test_string_strv_hashmap(); +/* Signal to test-hashmap.c that tests from this compilation unit were run. */ +extern int n_extern_tests_run; +TEST(ensure_extern_hashmap_tests) { + n_extern_tests_run++; } diff --git a/src/test/test-hashmap.c b/src/test/test-hashmap.c index 20bc97ce58..cba0c33a8a 100644 --- a/src/test/test-hashmap.c +++ b/src/test/test-hashmap.c @@ -2,6 +2,7 @@ #include "hashmap.h" #include "string-util.h" +#include "tests.h" #include "util.h" unsigned custom_counter = 0; @@ -13,15 +14,10 @@ static void custom_destruct(void* p) { DEFINE_HASH_OPS_FULL(boring_hash_ops, char, string_hash_func, string_compare_func, free, char, free); DEFINE_HASH_OPS_FULL(custom_hash_ops, char, string_hash_func, string_compare_func, custom_destruct, char, custom_destruct); -void test_hashmap_funcs(void); -void test_ordered_hashmap_funcs(void); - -static void test_ordered_hashmap_next(void) { +TEST(ordered_hashmap_next) { _cleanup_ordered_hashmap_free_ OrderedHashmap *m = NULL; int i; - log_info("/* %s */", __func__); - assert_se(m = ordered_hashmap_new(NULL)); for (i = -2; i <= 2; i++) assert_se(ordered_hashmap_put(m, INT_TO_PTR(i), INT_TO_PTR(i+10)) == 1); @@ -32,7 +28,7 @@ static void test_ordered_hashmap_next(void) { assert_se(!ordered_hashmap_next(m, INT_TO_PTR(3))); } -static void test_uint64_compare_func(void) { +TEST(uint64_compare_func) { const uint64_t a = 0x100, b = 0x101; assert_se(uint64_compare_func(&a, &a) == 0); @@ -40,13 +36,13 @@ static void test_uint64_compare_func(void) { assert_se(uint64_compare_func(&b, &a) == 1); } -static void test_trivial_compare_func(void) { +TEST(trivial_compare_func) { assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('a')) == 0); assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('b')) == -1); assert_se(trivial_compare_func(INT_TO_PTR('b'), INT_TO_PTR('a')) == 1); } -static void test_string_compare_func(void) { +TEST(string_compare_func) { assert_se(string_compare_func("fred", "wilma") != 0); assert_se(string_compare_func("fred", "fred") == 0); } @@ -71,12 +67,10 @@ static void compare_cache(Hashmap *map, IteratedCache *cache) { assert_se(idx == num); } -static void test_iterated_cache(void) { +TEST(iterated_cache) { Hashmap *m; IteratedCache *c; - log_info("/* %s */", __func__); - assert_se(m = hashmap_new(NULL)); assert_se(c = hashmap_iterated_cache_new(m)); compare_cache(m, c); @@ -109,15 +103,13 @@ static void test_iterated_cache(void) { assert_se(iterated_cache_free(c) == NULL); } -static void test_hashmap_put_strdup(void) { +TEST(hashmap_put_strdup) { _cleanup_hashmap_free_ Hashmap *m = NULL; char *s; /* We don't have ordered_hashmap_put_strdup() yet. If it is added, * these tests should be moved to test-hashmap-plain.c. */ - log_info("/* %s */", __func__); - assert_se(hashmap_put_strdup(&m, "foo", "bar") == 1); assert_se(hashmap_put_strdup(&m, "foo", "bar") == 0); assert_se(hashmap_put_strdup(&m, "foo", "BAR") == -EEXIST); @@ -137,12 +129,10 @@ static void test_hashmap_put_strdup(void) { assert_se(streq(s, "bar")); } -static void test_hashmap_put_strdup_null(void) { +TEST(hashmap_put_strdup_null) { _cleanup_hashmap_free_ Hashmap *m = NULL; char *s; - log_info("/* %s */", __func__); - assert_se(hashmap_put_strdup(&m, "foo", "bar") == 1); assert_se(hashmap_put_strdup(&m, "foo", "bar") == 0); assert_se(hashmap_put_strdup(&m, "foo", NULL) == -EEXIST); @@ -161,26 +151,14 @@ static void test_hashmap_put_strdup_null(void) { assert_se(s == NULL); } -int main(int argc, const char *argv[]) { - /* This file tests in test-hashmap-plain.c, and tests in test-hashmap-ordered.c, which is generated - * from test-hashmap-plain.c. Hashmap tests should be added to test-hashmap-plain.c, and here only if - * they don't apply to ordered hashmaps. */ +/* This file tests in test-hashmap-plain.c, and tests in test-hashmap-ordered.c, which is generated + * from test-hashmap-plain.c. Hashmap tests should be added to test-hashmap-plain.c, and here only if + * they don't apply to ordered hashmaps. */ - log_parse_environment(); - log_open(); +/* This variable allows us to assert that the tests from different compilation units were actually run. */ +int n_extern_tests_run = 0; - test_hashmap_funcs(); - test_ordered_hashmap_funcs(); - - log_info("/************ non-shared tests ************/"); - - test_ordered_hashmap_next(); - test_uint64_compare_func(); - test_trivial_compare_func(); - test_string_compare_func(); - test_iterated_cache(); - test_hashmap_put_strdup(); - test_hashmap_put_strdup_null(); - - return 0; -} +DEFINE_CUSTOM_TEST_MAIN( + LOG_INFO, + assert_se(n_extern_tests_run == 0), + assert_se(n_extern_tests_run == 2)); /* Ensure hashmap and ordered_hashmap were tested. */ diff --git a/src/test/test-install-root.c b/src/test/test-install-root.c index d0bd6917a8..ba715e6d7e 100644 --- a/src/test/test-install-root.c +++ b/src/test/test-install-root.c @@ -12,14 +12,14 @@ #include "string-util.h" #include "tests.h" -static void test_basic_mask_and_enable(const char *root) { +static char root[] = "/tmp/rootXXXXXX"; + +TEST(basic_mask_and_enable) { const char *p; UnitFileState state; UnitFileChange *changes = NULL; size_t n_changes = 0; - test_setup_logging(LOG_DEBUG); - assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "a.service", NULL) == -ENOENT); assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "b.service", NULL) == -ENOENT); assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "c.service", NULL) == -ENOENT); @@ -196,7 +196,7 @@ static void test_basic_mask_and_enable(const char *root) { assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "f.service", &state) >= 0 && state == UNIT_FILE_ENABLED); } -static void test_linked_units(const char *root) { +TEST(linked_units) { const char *p, *q; UnitFileState state; UnitFileChange *changes = NULL; @@ -342,7 +342,7 @@ static void test_linked_units(const char *root) { changes = NULL; n_changes = 0; } -static void test_default(const char *root) { +TEST(default) { _cleanup_free_ char *def = NULL; UnitFileChange *changes = NULL; size_t n_changes = 0; @@ -378,7 +378,7 @@ static void test_default(const char *root) { assert_se(streq_ptr(def, "test-default-real.target")); } -static void test_add_dependency(const char *root) { +TEST(add_dependency) { UnitFileChange *changes = NULL; size_t n_changes = 0; const char *p; @@ -405,7 +405,7 @@ static void test_add_dependency(const char *root) { changes = NULL; n_changes = 0; } -static void test_template_enable(const char *root) { +TEST(template_enable) { UnitFileChange *changes = NULL; size_t n_changes = 0; UnitFileState state; @@ -519,7 +519,7 @@ static void test_template_enable(const char *root) { assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "template-symlink@quux.service", &state) >= 0 && state == UNIT_FILE_ENABLED); } -static void test_indirect(const char *root) { +TEST(indirect) { UnitFileChange *changes = NULL; size_t n_changes = 0; UnitFileState state; @@ -568,7 +568,7 @@ static void test_indirect(const char *root) { changes = NULL; n_changes = 0; } -static void test_preset_and_list(const char *root) { +TEST(preset_and_list) { UnitFileChange *changes = NULL; size_t n_changes = 0, i; const char *p, *q; @@ -675,14 +675,12 @@ static void test_preset_and_list(const char *root) { assert_se(got_yes && got_no); } -static void test_revert(const char *root) { +TEST(revert) { const char *p; UnitFileState state; UnitFileChange *changes = NULL; size_t n_changes = 0; - assert_se(root); - assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "xx.service", NULL) == -ENOENT); assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "yy.service", NULL) == -ENOENT); @@ -725,7 +723,7 @@ static void test_revert(const char *root) { changes = NULL; n_changes = 0; } -static void test_preset_order(const char *root) { +TEST(preset_order) { UnitFileChange *changes = NULL; size_t n_changes = 0; const char *p; @@ -772,7 +770,7 @@ static void test_preset_order(const char *root) { assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "prefix-2.service", &state) >= 0 && state == UNIT_FILE_DISABLED); } -static void test_static_instance(const char *root) { +TEST(static_instance) { UnitFileState state; const char *p; @@ -794,7 +792,7 @@ static void test_static_instance(const char *root) { assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "static-instance@foo.service", &state) >= 0 && state == UNIT_FILE_STATIC); } -static void test_with_dropin(const char *root) { +TEST(with_dropin) { const char *p; UnitFileState state; UnitFileChange *changes = NULL; @@ -923,7 +921,7 @@ static void test_with_dropin(const char *root) { assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "with-dropin-4b.service", &state) >= 0 && state == UNIT_FILE_ENABLED); } -static void test_with_dropin_template(const char *root) { +TEST(with_dropin_template) { const char *p; UnitFileState state; UnitFileChange *changes = NULL; @@ -1021,7 +1019,7 @@ static void test_with_dropin_template(const char *root) { assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "with-dropin-3@instance-2.service", &state) >= 0 && state == UNIT_FILE_ENABLED); } -static void test_preset_multiple_instances(const char *root) { +TEST(preset_multiple_instances) { UnitFileChange *changes = NULL; size_t n_changes = 0; const char *p; @@ -1104,7 +1102,7 @@ static void verify_one( assert_se(streq_ptr(alias2, updated_name)); } -static void test_verify_alias(void) { +TEST(verify_alias) { const UnitFileInstallInfo plain_service = { .name = (char*) "plain.service" }, bare_template = { .name = (char*) "template1@.service" }, @@ -1241,8 +1239,7 @@ static void test_verify_alias(void) { verify_one(&di_inst_template, "goo.target.conf/plain.service", -EXDEV, NULL); } -int main(int argc, char *argv[]) { - char root[] = "/tmp/rootXXXXXX"; +static void setup_root(void) { const char *p; assert_se(mkdtemp(root)); @@ -1267,24 +1264,6 @@ int main(int argc, char *argv[]) { p = strjoina(root, "/usr/lib/systemd/system/graphical.target"); assert_se(write_string_file(p, "# pretty much empty", WRITE_STRING_FILE_CREATE) >= 0); - - test_basic_mask_and_enable(root); - test_linked_units(root); - test_default(root); - test_add_dependency(root); - test_template_enable(root); - test_indirect(root); - test_preset_and_list(root); - test_preset_order(root); - test_preset_multiple_instances(root); - test_revert(root); - test_static_instance(root); - test_with_dropin(root); - test_with_dropin_template(root); - - assert_se(rm_rf(root, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0); - - test_verify_alias(); - - return 0; } + +DEFINE_CUSTOM_TEST_MAIN(LOG_INFO, setup_root(), assert_se(rm_rf(root, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0)); diff --git a/src/test/test-mountpoint-util.c b/src/test/test-mountpoint-util.c index 983e1842d6..d11edf502a 100644 --- a/src/test/test-mountpoint-util.c +++ b/src/test/test-mountpoint-util.c @@ -17,7 +17,7 @@ #include "tests.h" #include "tmpfile-util.h" -static void test_mount_propagation_flags(const char *name, int ret, unsigned long expected) { +static void test_mount_propagation_flags_one(const char *name, int ret, unsigned long expected) { long unsigned flags; log_info("/* %s(%s) */", __func__, name); @@ -37,7 +37,17 @@ static void test_mount_propagation_flags(const char *name, int ret, unsigned lon } } -static void test_mnt_id(void) { +TEST(mount_propagation_flags) { + test_mount_propagation_flags_one("shared", 0, MS_SHARED); + test_mount_propagation_flags_one("slave", 0, MS_SLAVE); + test_mount_propagation_flags_one("private", 0, MS_PRIVATE); + test_mount_propagation_flags_one(NULL, 0, 0); + test_mount_propagation_flags_one("", 0, 0); + test_mount_propagation_flags_one("xxxx", -EINVAL, 0); + test_mount_propagation_flags_one(" ", -EINVAL, 0); +} + +TEST(mnt_id) { _cleanup_fclose_ FILE *f = NULL; _cleanup_hashmap_free_free_ Hashmap *h = NULL; char *p; @@ -96,15 +106,13 @@ static void test_mnt_id(void) { } } -static void test_path_is_mount_point(void) { +TEST(path_is_mount_point) { int fd; char tmp_dir[] = "/tmp/test-path-is-mount-point-XXXXXX"; _cleanup_free_ char *file1 = NULL, *file2 = NULL, *link1 = NULL, *link2 = NULL; _cleanup_free_ char *dir1 = NULL, *dir1file = NULL, *dirlink1 = NULL, *dirlink1file = NULL; _cleanup_free_ char *dir2 = NULL, *dir2file = NULL; - log_info("/* %s */", __func__); - assert_se(path_is_mount_point("/", NULL, AT_SYMLINK_FOLLOW) > 0); assert_se(path_is_mount_point("/", NULL, 0) > 0); assert_se(path_is_mount_point("//", NULL, AT_SYMLINK_FOLLOW) > 0); @@ -257,11 +265,9 @@ static void test_path_is_mount_point(void) { assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0); } -static void test_fd_is_mount_point(void) { +TEST(fd_is_mount_point) { _cleanup_close_ int fd = -1; - log_info("/* %s */", __func__); - fd = open("/", O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY); assert_se(fd >= 0); @@ -288,30 +294,17 @@ static void test_fd_is_mount_point(void) { assert_se(IN_SET(fd_is_mount_point(fd, "root/", 0), -ENOENT, 0)); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_DEBUG); +DEFINE_CUSTOM_TEST_MAIN( + LOG_DEBUG, + ({ + /* let's move into our own mount namespace with all propagation from the host turned off, so + * that /proc/self/mountinfo is static and constant for the whole time our test runs. */ + if (unshare(CLONE_NEWNS) < 0) { + if (!ERRNO_IS_PRIVILEGE(errno)) + return log_error_errno(errno, "Failed to detach mount namespace: %m"); - /* let's move into our own mount namespace with all propagation from the host turned off, so that - * /proc/self/mountinfo is static and constant for the whole time our test runs. */ - if (unshare(CLONE_NEWNS) < 0) { - if (!ERRNO_IS_PRIVILEGE(errno)) - return log_error_errno(errno, "Failed to detach mount namespace: %m"); - - log_notice("Lacking privilege to create separate mount namespace, proceeding in originating mount namespace."); - } else - assert_se(mount(NULL, "/", NULL, MS_PRIVATE | MS_REC, NULL) >= 0); - - test_mount_propagation_flags("shared", 0, MS_SHARED); - test_mount_propagation_flags("slave", 0, MS_SLAVE); - test_mount_propagation_flags("private", 0, MS_PRIVATE); - test_mount_propagation_flags(NULL, 0, 0); - test_mount_propagation_flags("", 0, 0); - test_mount_propagation_flags("xxxx", -EINVAL, 0); - test_mount_propagation_flags(" ", -EINVAL, 0); - - test_mnt_id(); - test_path_is_mount_point(); - test_fd_is_mount_point(); - - return 0; -} + log_notice("Lacking privilege to create separate mount namespace, proceeding in originating mount namespace."); + } else + assert_se(mount(NULL, "/", NULL, MS_PRIVATE | MS_REC, NULL) >= 0); + }), + /* no outro */); diff --git a/src/test/test-proc-cmdline.c b/src/test/test-proc-cmdline.c index a72523bd02..1c8c9b80b7 100644 --- a/src/test/test-proc-cmdline.c +++ b/src/test/test-proc-cmdline.c @@ -21,15 +21,11 @@ static int parse_item(const char *key, const char *value, void *data) { return 0; } -static void test_proc_cmdline_parse(void) { - log_info("/* %s */", __func__); - +TEST(proc_cmdline_parse) { assert_se(proc_cmdline_parse(parse_item, &obj, PROC_CMDLINE_STRIP_RD_PREFIX) >= 0); } -static void test_proc_cmdline_override(void) { - log_info("/* %s */", __func__); - +TEST(proc_cmdline_override) { assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar=quux wuff-piep=tuet zumm some_arg_with_space='foo bar' and_one_more=\"zzz aaa\"") == 0); assert_se(putenv((char*) "SYSTEMD_EFI_OPTIONS=different") == 0); @@ -87,7 +83,7 @@ static int parse_item_given(const char *key, const char *value, void *data) { return 0; } -static void test_proc_cmdline_given(bool flip_initrd) { +static void test_proc_cmdline_given_one(bool flip_initrd) { log_info("/* %s (flip: %s) */", __func__, yes_no(flip_initrd)); if (flip_initrd) @@ -104,10 +100,15 @@ static void test_proc_cmdline_given(bool flip_initrd) { in_initrd_force(!in_initrd()); } -static void test_proc_cmdline_get_key(void) { +TEST(test_proc_cmdline_given) { + test_proc_cmdline_given_one(false); + /* Repeat the same thing, but now flip our ininitrdness */ + test_proc_cmdline_given_one(true); +} + +TEST(proc_cmdline_get_key) { _cleanup_free_ char *value = NULL; - log_info("/* %s */", __func__); assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar=quux wuff-piep=tuet zumm spaaace='ö ü ß' ticks=\"''\"\n\nkkk=uuu\n\n\n") == 0); assert_se(proc_cmdline_get_key("", 0, &value) == -EINVAL); @@ -150,10 +151,9 @@ static void test_proc_cmdline_get_key(void) { assert_se(proc_cmdline_get_key("kkk", 0, &value) > 0 && streq_ptr(value, "uuu")); } -static void test_proc_cmdline_get_bool(void) { +TEST(proc_cmdline_get_bool) { bool value = false; - log_info("/* %s */", __func__); assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar bar-waldo=1 x_y-z=0 quux=miep\nda=yes\nthe=1") == 0); assert_se(putenv((char*) "SYSTEMD_EFI_OPTIONS=") == 0); @@ -170,11 +170,15 @@ static void test_proc_cmdline_get_bool(void) { assert_se(proc_cmdline_get_bool("quux", &value) == -EINVAL && value == false); assert_se(proc_cmdline_get_bool("da", &value) > 0 && value == true); assert_se(proc_cmdline_get_bool("the", &value) > 0 && value == true); +} + +#if ENABLE_EFI +TEST(proc_cmdline_get_bool_efi) { + bool value = false; assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=") == 0); assert_se(putenv((char*) "SYSTEMD_EFI_OPTIONS=foo_bar bar-waldo=1 x_y-z=0 quux=miep\nda=yes\nthe=1") == 0); -#if ENABLE_EFI assert_se(proc_cmdline_get_bool("", &value) == -EINVAL); assert_se(proc_cmdline_get_bool("abc", &value) == 0 && value == false); assert_se(proc_cmdline_get_bool("foo_bar", &value) > 0 && value == true); @@ -188,13 +192,12 @@ static void test_proc_cmdline_get_bool(void) { assert_se(proc_cmdline_get_bool("quux", &value) == -EINVAL && value == false); assert_se(proc_cmdline_get_bool("da", &value) > 0 && value == true); assert_se(proc_cmdline_get_bool("the", &value) > 0 && value == true); -#endif } +#endif -static void test_proc_cmdline_get_key_many(void) { +TEST(proc_cmdline_get_key_many) { _cleanup_free_ char *value1 = NULL, *value2 = NULL, *value3 = NULL, *value4 = NULL, *value5 = NULL, *value6 = NULL, *value7 = NULL; - log_info("/* %s */", __func__); assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar=quux wuff-piep=tuet zumm SPACE='one two' doubleticks=\" aaa aaa \"\n\nzummm='\n'\n") == 0); assert_se(proc_cmdline_get_key_many(0, @@ -215,9 +218,7 @@ static void test_proc_cmdline_get_key_many(void) { assert_se(streq_ptr(value7, "\n")); } -static void test_proc_cmdline_key_streq(void) { - log_info("/* %s */", __func__); - +TEST(proc_cmdline_key_streq) { assert_se(proc_cmdline_key_streq("", "")); assert_se(proc_cmdline_key_streq("a", "a")); assert_se(!proc_cmdline_key_streq("", "a")); @@ -232,9 +233,7 @@ static void test_proc_cmdline_key_streq(void) { assert_se(!proc_cmdline_key_streq("x_y-z", "x-z_z")); } -static void test_proc_cmdline_key_startswith(void) { - log_info("/* %s */", __func__); - +TEST(proc_cmdline_key_startswith) { assert_se(proc_cmdline_key_startswith("", "")); assert_se(proc_cmdline_key_startswith("x", "")); assert_se(!proc_cmdline_key_startswith("", "x")); @@ -248,22 +247,10 @@ static void test_proc_cmdline_key_startswith(void) { assert_se(!proc_cmdline_key_startswith("foo-bar", "foo_xx")); } -int main(void) { - test_setup_logging(LOG_INFO); - - if (access("/proc/cmdline", R_OK) < 0 && ERRNO_IS_PRIVILEGE(errno)) - return log_tests_skipped("can't read /proc/cmdline"); - - test_proc_cmdline_parse(); - test_proc_cmdline_override(); - test_proc_cmdline_given(false); - /* Repeat the same thing, but now flip our ininitrdness */ - test_proc_cmdline_given(true); - test_proc_cmdline_key_streq(); - test_proc_cmdline_key_startswith(); - test_proc_cmdline_get_key(); - test_proc_cmdline_get_bool(); - test_proc_cmdline_get_key_many(); - - return 0; -} +DEFINE_CUSTOM_TEST_MAIN( + LOG_INFO, + ({ + if (access("/proc/cmdline", R_OK) < 0 && ERRNO_IS_PRIVILEGE(errno)) + return log_tests_skipped("can't read /proc/cmdline"); + }), + /* no outro */); diff --git a/src/test/test-process-util.c b/src/test/test-process-util.c index cc0f870e57..a180f3f9b4 100644 --- a/src/test/test-process-util.c +++ b/src/test/test-process-util.c @@ -37,7 +37,7 @@ #include "util.h" #include "virt.h" -static void test_get_process_comm(pid_t pid) { +static void test_get_process_comm_one(pid_t pid) { struct stat st; _cleanup_free_ char *a = NULL, *c = NULL, *d = NULL, *f = NULL, *i = NULL; _cleanup_free_ char *env = NULL; @@ -98,6 +98,18 @@ static void test_get_process_comm(pid_t pid) { log_info("PID"PID_FMT" $PATH: '%s'", pid, strna(i)); } +TEST(get_process_comm) { + if (saved_argc > 1) { + pid_t pid = 0; + + (void) parse_pid(saved_argv[1], &pid); + test_get_process_comm_one(pid); + } else { + TEST_REQ_RUNNING_SYSTEMD(test_get_process_comm_one(1)); + test_get_process_comm_one(getpid()); + } +} + static void test_get_process_cmdline_one(pid_t pid) { _cleanup_free_ char *c = NULL, *d = NULL, *e = NULL, *f = NULL, *g = NULL, *h = NULL; int r; @@ -121,12 +133,10 @@ static void test_get_process_cmdline_one(pid_t pid) { log_info(" %s", r >= 0 ? h : errno_to_name(r)); } -static void test_get_process_cmdline(void) { +TEST(get_process_cmdline) { _cleanup_closedir_ DIR *d = NULL; struct dirent *de; - log_info("/* %s */", __func__); - assert_se(d = opendir("/proc")); FOREACH_DIRENT(de, d, return) { @@ -155,11 +165,9 @@ static void test_get_process_comm_escape_one(const char *input, const char *outp assert_se(streq_ptr(n, output)); } -static void test_get_process_comm_escape(void) { +TEST(get_process_comm_escape) { _cleanup_free_ char *saved = NULL; - log_info("/* %s */", __func__); - assert_se(get_process_comm(0, &saved) >= 0); test_get_process_comm_escape_one("", ""); @@ -176,7 +184,7 @@ static void test_get_process_comm_escape(void) { assert_se(prctl(PR_SET_NAME, saved) >= 0); } -static void test_pid_is_unwaited(void) { +TEST(pid_is_unwaited) { pid_t pid; pid = fork(); @@ -193,11 +201,9 @@ static void test_pid_is_unwaited(void) { assert_se(!pid_is_unwaited(-1)); } -static void test_pid_is_alive(void) { +TEST(pid_is_alive) { pid_t pid; - log_info("/* %s */", __func__); - pid = fork(); assert_se(pid >= 0); if (pid == 0) { @@ -212,9 +218,7 @@ static void test_pid_is_alive(void) { assert_se(!pid_is_alive(-1)); } -static void test_personality(void) { - log_info("/* %s */", __func__); - +TEST(personality) { assert_se(personality_to_string(PER_LINUX)); assert_se(!personality_to_string(PERSONALITY_INVALID)); @@ -236,14 +240,12 @@ static void test_personality(void) { #endif } -static void test_get_process_cmdline_harder(void) { +TEST(get_process_cmdline_harder) { char path[] = "/tmp/test-cmdlineXXXXXX"; _cleanup_close_ int fd = -1; _cleanup_free_ char *line = NULL; pid_t pid; - log_info("/* %s */", __func__); - if (geteuid() != 0) { log_info("Skipping %s: not root", __func__); return; @@ -589,7 +591,7 @@ static void test_rename_process_one(const char *p, int ret) { assert_se(si.si_status == EXIT_SUCCESS); } -static void test_rename_process_multi(void) { +TEST(rename_process_multi) { pid_t pid; pid = fork(); @@ -616,21 +618,18 @@ static void test_rename_process_multi(void) { _exit(EXIT_SUCCESS); } -static void test_rename_process(void) { +TEST(rename_process) { test_rename_process_one(NULL, -EINVAL); test_rename_process_one("", -EINVAL); test_rename_process_one("foo", 1); /* should always fit */ test_rename_process_one("this is a really really long process name, followed by some more words", 0); /* unlikely to fit */ test_rename_process_one("1234567", 1); /* should always fit */ - test_rename_process_multi(); /* multiple invocations and dropped privileges */ } -static void test_getpid_cached(void) { +TEST(getpid_cached) { siginfo_t si; pid_t a, b, c, d, e, f, child; - log_info("/* %s */", __func__); - a = raw_getpid(); b = getpid_cached(); c = getpid(); @@ -661,7 +660,7 @@ static void test_getpid_cached(void) { assert_se(si.si_code == CLD_EXITED); } -static void test_getpid_measure(void) { +TEST(getpid_measure) { usec_t t, q; unsigned long long iterations = slow_tests_enabled() ? 1000000 : 1000; @@ -685,13 +684,11 @@ static void test_getpid_measure(void) { log_info("getpid_cached(): %lf µs each\n", (double) q / iterations); } -static void test_safe_fork(void) { +TEST(safe_fork) { siginfo_t status; pid_t pid; int r; - log_info("/* %s */", __func__); - BLOCK_SIGNALS(SIGCHLD); r = safe_fork("(test-child)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_NULL_STDIO|FORK_REOPEN_LOG, &pid); @@ -709,8 +706,7 @@ static void test_safe_fork(void) { assert_se(status.si_status == 88); } -static void test_pid_to_ptr(void) { - +TEST(pid_to_ptr) { assert_se(PTR_TO_PID(NULL) == 0); assert_se(PID_TO_PTR(0) == NULL); @@ -745,9 +741,7 @@ static void test_ioprio_class_from_to_string_one(const char *val, int expected, } } -static void test_ioprio_class_from_to_string(void) { - log_info("/* %s */", __func__); - +TEST(ioprio_class_from_to_string) { test_ioprio_class_from_to_string_one("none", IOPRIO_CLASS_NONE, IOPRIO_CLASS_BE); test_ioprio_class_from_to_string_one("realtime", IOPRIO_CLASS_RT, IOPRIO_CLASS_RT); test_ioprio_class_from_to_string_one("best-effort", IOPRIO_CLASS_BE, IOPRIO_CLASS_BE); @@ -760,11 +754,9 @@ static void test_ioprio_class_from_to_string(void) { test_ioprio_class_from_to_string_one("-1", -EINVAL, -EINVAL); } -static void test_setpriority_closest(void) { +TEST(setpriority_closest) { int r; - log_info("/* %s */", __func__); - r = safe_fork("(test-setprio)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_WAIT|FORK_LOG, NULL); assert_se(r >= 0); @@ -849,12 +841,10 @@ static void test_setpriority_closest(void) { } } -static void test_get_process_ppid(void) { +TEST(get_process_ppid) { uint64_t limit; int r; - log_info("/* %s */", __func__); - assert_se(get_process_ppid(1, NULL) == -EADDRNOTAVAIL); /* the process with the PID above the global limit definitely doesn't exist. Verify that */ @@ -888,7 +878,7 @@ static void test_get_process_ppid(void) { } } -static void test_set_oom_score_adjust(void) { +TEST(set_oom_score_adjust) { int a, b, r; assert_se(get_oom_score_adjust(&a) >= 0); @@ -906,37 +896,4 @@ static void test_set_oom_score_adjust(void) { assert_se(b == a); } -int main(int argc, char *argv[]) { - log_show_color(true); - test_setup_logging(LOG_INFO); - - save_argc_argv(argc, argv); - - if (argc > 1) { - pid_t pid = 0; - - (void) parse_pid(argv[1], &pid); - test_get_process_comm(pid); - } else { - TEST_REQ_RUNNING_SYSTEMD(test_get_process_comm(1)); - test_get_process_comm(getpid()); - } - - test_get_process_comm_escape(); - test_get_process_cmdline(); - test_pid_is_unwaited(); - test_pid_is_alive(); - test_personality(); - test_get_process_cmdline_harder(); - test_rename_process(); - test_getpid_cached(); - test_getpid_measure(); - test_safe_fork(); - test_pid_to_ptr(); - test_ioprio_class_from_to_string(); - test_setpriority_closest(); - test_get_process_ppid(); - test_set_oom_score_adjust(); - - return 0; -} +DEFINE_CUSTOM_TEST_MAIN(LOG_INFO, log_show_color(true), /* no outro */); diff --git a/src/test/test-rlimit-util.c b/src/test/test-rlimit-util.c index 2ebe81df41..86d0c04555 100644 --- a/src/test/test-rlimit-util.c +++ b/src/test/test-rlimit-util.c @@ -8,9 +8,10 @@ #include "missing_resource.h" #include "rlimit-util.h" #include "string-util.h" +#include "tests.h" #include "time-util.h" -static void test_rlimit_parse_format(int resource, const char *string, rlim_t soft, rlim_t hard, int ret, const char *formatted) { +static void test_rlimit_parse_format_one(int resource, const char *string, rlim_t soft, rlim_t hard, int ret, const char *formatted) { _cleanup_free_ char *f = NULL; struct rlimit rl = { .rlim_cur = 4711, @@ -34,37 +35,48 @@ static void test_rlimit_parse_format(int resource, const char *string, rlim_t so assert_se(memcmp(&rl, &rl2, sizeof(struct rlimit)) == 0); } -int main(int argc, char *argv[]) { - struct rlimit old, new, high; - struct rlimit err = { - .rlim_cur = 10, - .rlim_max = 5, - }; - int i; - - log_parse_environment(); - log_open(); - - assert_se(drop_capability(CAP_SYS_RESOURCE) == 0); - - assert_se(getrlimit(RLIMIT_NOFILE, &old) == 0); - new.rlim_cur = MIN(5U, old.rlim_max); - new.rlim_max = old.rlim_max; - assert_se(setrlimit(RLIMIT_NOFILE, &new) >= 0); +TEST(rlimit_parse_format) { + test_rlimit_parse_format_one(RLIMIT_NOFILE, "4:5", 4, 5, 0, "4:5"); + test_rlimit_parse_format_one(RLIMIT_NOFILE, "6", 6, 6, 0, "6"); + test_rlimit_parse_format_one(RLIMIT_NOFILE, "infinity", RLIM_INFINITY, RLIM_INFINITY, 0, "infinity"); + test_rlimit_parse_format_one(RLIMIT_NOFILE, "infinity:infinity", RLIM_INFINITY, RLIM_INFINITY, 0, "infinity"); + test_rlimit_parse_format_one(RLIMIT_NOFILE, "8:infinity", 8, RLIM_INFINITY, 0, "8:infinity"); + test_rlimit_parse_format_one(RLIMIT_CPU, "25min:13h", (25*USEC_PER_MINUTE) / USEC_PER_SEC, (13*USEC_PER_HOUR) / USEC_PER_SEC, 0, "1500:46800"); + test_rlimit_parse_format_one(RLIMIT_NOFILE, "", 0, 0, -EINVAL, NULL); + test_rlimit_parse_format_one(RLIMIT_NOFILE, "5:4", 0, 0, -EILSEQ, NULL); + test_rlimit_parse_format_one(RLIMIT_NOFILE, "5:4:3", 0, 0, -EINVAL, NULL); + test_rlimit_parse_format_one(RLIMIT_NICE, "20", 20, 20, 0, "20"); + test_rlimit_parse_format_one(RLIMIT_NICE, "40", 40, 40, 0, "40"); + test_rlimit_parse_format_one(RLIMIT_NICE, "41", 41, 41, -ERANGE, "41"); + test_rlimit_parse_format_one(RLIMIT_NICE, "0", 0, 0, 0, "0"); + test_rlimit_parse_format_one(RLIMIT_NICE, "-7", 27, 27, 0, "27"); + test_rlimit_parse_format_one(RLIMIT_NICE, "-20", 40, 40, 0, "40"); + test_rlimit_parse_format_one(RLIMIT_NICE, "-21", 41, 41, -ERANGE, "41"); + test_rlimit_parse_format_one(RLIMIT_NICE, "-0", 20, 20, 0, "20"); + test_rlimit_parse_format_one(RLIMIT_NICE, "+7", 13, 13, 0, "13"); + test_rlimit_parse_format_one(RLIMIT_NICE, "+19", 1, 1, 0, "1"); + test_rlimit_parse_format_one(RLIMIT_NICE, "+20", 0, 0, -ERANGE, "0"); + test_rlimit_parse_format_one(RLIMIT_NICE, "+0", 20, 20, 0, "20"); +} +TEST(rlimit_from_string) { assert_se(rlimit_from_string("NOFILE") == RLIMIT_NOFILE); assert_se(rlimit_from_string("LimitNOFILE") == -EINVAL); assert_se(rlimit_from_string("RLIMIT_NOFILE") == -EINVAL); assert_se(rlimit_from_string("xxxNOFILE") == -EINVAL); assert_se(rlimit_from_string("DefaultLimitNOFILE") == -EINVAL); +} +TEST(rlimit_from_string_harder) { assert_se(rlimit_from_string_harder("NOFILE") == RLIMIT_NOFILE); assert_se(rlimit_from_string_harder("LimitNOFILE") == RLIMIT_NOFILE); assert_se(rlimit_from_string_harder("RLIMIT_NOFILE") == RLIMIT_NOFILE); assert_se(rlimit_from_string_harder("xxxNOFILE") == -EINVAL); assert_se(rlimit_from_string_harder("DefaultLimitNOFILE") == -EINVAL); +} - for (i = 0; i < _RLIMIT_MAX; i++) { +TEST(rlimit_to_string_all) { + for (int i = 0; i < _RLIMIT_MAX; i++) { _cleanup_free_ char *prefixed = NULL; const char *p; @@ -85,6 +97,21 @@ int main(int argc, char *argv[]) { assert_se(rlimit_from_string(prefixed) < 0); assert_se(rlimit_from_string_harder(prefixed) == i); } +} + +TEST(setrlimit) { + struct rlimit old, new, high; + struct rlimit err = { + .rlim_cur = 10, + .rlim_max = 5, + }; + + assert_se(drop_capability(CAP_SYS_RESOURCE) == 0); + + assert_se(getrlimit(RLIMIT_NOFILE, &old) == 0); + new.rlim_cur = MIN(5U, old.rlim_max); + new.rlim_max = old.rlim_max; + assert_se(setrlimit(RLIMIT_NOFILE, &new) >= 0); assert_se(streq_ptr(rlimit_to_string(RLIMIT_NOFILE), "NOFILE")); assert_se(rlimit_to_string(-1) == NULL); @@ -107,28 +134,6 @@ int main(int argc, char *argv[]) { assert_se(getrlimit(RLIMIT_NOFILE, &new) == 0); assert_se(old.rlim_cur == new.rlim_cur); assert_se(old.rlim_max == new.rlim_max); - - test_rlimit_parse_format(RLIMIT_NOFILE, "4:5", 4, 5, 0, "4:5"); - test_rlimit_parse_format(RLIMIT_NOFILE, "6", 6, 6, 0, "6"); - test_rlimit_parse_format(RLIMIT_NOFILE, "infinity", RLIM_INFINITY, RLIM_INFINITY, 0, "infinity"); - test_rlimit_parse_format(RLIMIT_NOFILE, "infinity:infinity", RLIM_INFINITY, RLIM_INFINITY, 0, "infinity"); - test_rlimit_parse_format(RLIMIT_NOFILE, "8:infinity", 8, RLIM_INFINITY, 0, "8:infinity"); - test_rlimit_parse_format(RLIMIT_CPU, "25min:13h", (25*USEC_PER_MINUTE) / USEC_PER_SEC, (13*USEC_PER_HOUR) / USEC_PER_SEC, 0, "1500:46800"); - test_rlimit_parse_format(RLIMIT_NOFILE, "", 0, 0, -EINVAL, NULL); - test_rlimit_parse_format(RLIMIT_NOFILE, "5:4", 0, 0, -EILSEQ, NULL); - test_rlimit_parse_format(RLIMIT_NOFILE, "5:4:3", 0, 0, -EINVAL, NULL); - test_rlimit_parse_format(RLIMIT_NICE, "20", 20, 20, 0, "20"); - test_rlimit_parse_format(RLIMIT_NICE, "40", 40, 40, 0, "40"); - test_rlimit_parse_format(RLIMIT_NICE, "41", 41, 41, -ERANGE, "41"); - test_rlimit_parse_format(RLIMIT_NICE, "0", 0, 0, 0, "0"); - test_rlimit_parse_format(RLIMIT_NICE, "-7", 27, 27, 0, "27"); - test_rlimit_parse_format(RLIMIT_NICE, "-20", 40, 40, 0, "40"); - test_rlimit_parse_format(RLIMIT_NICE, "-21", 41, 41, -ERANGE, "41"); - test_rlimit_parse_format(RLIMIT_NICE, "-0", 20, 20, 0, "20"); - test_rlimit_parse_format(RLIMIT_NICE, "+7", 13, 13, 0, "13"); - test_rlimit_parse_format(RLIMIT_NICE, "+19", 1, 1, 0, "1"); - test_rlimit_parse_format(RLIMIT_NICE, "+20", 0, 0, -ERANGE, "0"); - test_rlimit_parse_format(RLIMIT_NICE, "+0", 20, 20, 0, "20"); - - return 0; } + +DEFINE_TEST_MAIN(LOG_INFO); diff --git a/src/test/test-sd-hwdb.c b/src/test/test-sd-hwdb.c index 7ed6907cbd..7961c17c4a 100644 --- a/src/test/test-sd-hwdb.c +++ b/src/test/test-sd-hwdb.c @@ -7,38 +7,28 @@ #include "errno.h" #include "tests.h" -static int test_failed_enumerate(void) { +TEST(failed_enumerate) { _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL; const char *key, *value; - int r; - log_info("/* %s */", __func__); - - r = sd_hwdb_new(&hwdb); - if (r == -ENOENT || ERRNO_IS_PRIVILEGE(r)) - return r; - assert_se(r == 0); + assert_se(sd_hwdb_new(&hwdb) == 0); assert_se(sd_hwdb_seek(hwdb, "no-such-modalias-should-exist") == 0); assert_se(sd_hwdb_enumerate(hwdb, &key, &value) == 0); assert_se(sd_hwdb_enumerate(hwdb, &key, NULL) == -EINVAL); assert_se(sd_hwdb_enumerate(hwdb, NULL, &value) == -EINVAL); - - return 0; } #define DELL_MODALIAS \ "evdev:atkbd:dmi:bvnXXX:bvrYYY:bdZZZ:svnDellXXX:pnYYY" -static void test_basic_enumerate(void) { +TEST(basic_enumerate) { _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL; const char *key, *value; size_t len1 = 0, len2 = 0; int r; - log_info("/* %s */", __func__); - assert_se(sd_hwdb_new(&hwdb) == 0); assert_se(sd_hwdb_seek(hwdb, DELL_MODALIAS) == 0); @@ -62,16 +52,12 @@ static void test_basic_enumerate(void) { assert_se(len1 == len2); } -int main(int argc, char *argv[]) { - int r; - - test_setup_logging(LOG_DEBUG); - - r = test_failed_enumerate(); - if (r < 0) - return log_tests_skipped_errno(r, "cannot open hwdb"); - - test_basic_enumerate(); - - return 0; -} +DEFINE_CUSTOM_TEST_MAIN( + LOG_DEBUG, + ({ + _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL; + int r = sd_hwdb_new(&hwdb); + if (r == -ENOENT || ERRNO_IS_PRIVILEGE(r)) + return log_tests_skipped_errno(r, "cannot open hwdb"); + }), + /* no outro */); diff --git a/src/test/test-serialize.c b/src/test/test-serialize.c index 7bd53a861d..fb04b3e7fa 100644 --- a/src/test/test-serialize.c +++ b/src/test/test-serialize.c @@ -12,7 +12,7 @@ char long_string[LONG_LINE_MAX+1]; -static void test_serialize_item(void) { +TEST(serialize_item) { _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-serialize.XXXXXX"; _cleanup_fclose_ FILE *f = NULL; @@ -37,7 +37,7 @@ static void test_serialize_item(void) { assert_se(streq(line3, "")); } -static void test_serialize_item_escaped(void) { +TEST(serialize_item_escaped) { _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-serialize.XXXXXX"; _cleanup_fclose_ FILE *f = NULL; @@ -62,7 +62,7 @@ static void test_serialize_item_escaped(void) { assert_se(streq(line3, "")); } -static void test_serialize_usec(void) { +TEST(serialize_usec) { _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-serialize.XXXXXX"; _cleanup_fclose_ FILE *f = NULL; @@ -89,7 +89,7 @@ static void test_serialize_usec(void) { assert_se(x == USEC_INFINITY-1); } -static void test_serialize_strv(void) { +TEST(serialize_strv) { _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-serialize.XXXXXX"; _cleanup_fclose_ FILE *f = NULL; @@ -133,11 +133,9 @@ static void test_serialize_strv(void) { assert_se(strv_equal(strv, strv2)); } -static void test_deserialize_environment(void) { +TEST(deserialize_environment) { _cleanup_strv_free_ char **env; - log_info("/* %s */", __func__); - assert_se(env = strv_new("A=1")); assert_se(deserialize_environment("B=2", &env) >= 0); @@ -149,7 +147,7 @@ static void test_deserialize_environment(void) { assert_se(deserialize_environment("bar\\_baz", &env) < 0); } -static void test_serialize_environment(void) { +TEST(serialize_environment) { _cleanup_strv_free_ char **env = NULL, **env2 = NULL; _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-env-util.XXXXXXX"; _cleanup_fclose_ FILE *f = NULL; @@ -191,18 +189,10 @@ static void test_serialize_environment(void) { assert_se(strv_equal(env, env2)); } -int main(int argc, char *argv[]) { - test_setup_logging(LOG_INFO); - - memset(long_string, 'x', sizeof(long_string)-1); - char_array_0(long_string); - - test_serialize_item(); - test_serialize_item_escaped(); - test_serialize_usec(); - test_serialize_strv(); - test_deserialize_environment(); - test_serialize_environment(); - - return EXIT_SUCCESS; -} +DEFINE_CUSTOM_TEST_MAIN( + LOG_INFO, + ({ + memset(long_string, 'x', sizeof(long_string)-1); + char_array_0(long_string); + }), + /* no outro */); diff --git a/src/test/test-udev-util.c b/src/test/test-udev-util.c index b87a90f766..d413997189 100644 --- a/src/test/test-udev-util.c +++ b/src/test/test-udev-util.c @@ -5,6 +5,7 @@ #include "macro.h" #include "string-util.h" +#include "tests.h" #include "udev-util.h" static void test_udev_rule_parse_value_one(const char *in, const char *expected_value, int expected_retval) { @@ -12,6 +13,8 @@ static void test_udev_rule_parse_value_one(const char *in, const char *expected_ char *value = UINT_TO_PTR(0x12345678U); char *endpos = UINT_TO_PTR(0x87654321U); + log_info("/* %s (%s, %s, %d) */", __func__, in, expected_value, expected_retval); + assert_se(str = strdup(in)); assert_se(udev_rule_parse_value(str, &value, &endpos) == expected_retval); if (expected_retval < 0) { @@ -24,161 +27,50 @@ static void test_udev_rule_parse_value_one(const char *in, const char *expected_ } } -static void test_parse_value(void) { +TEST(udev_rule_parse_value) { /* input: "valid operand" * parsed: valid operand * use the following command to help generate textual C strings: * python3 -c 'import json; print(json.dumps(input()))' */ - test_udev_rule_parse_value_one( - "\"valid operand\"", - "valid operand", - 0 - ); -} - -static void test_parse_value_with_backslashes(void) { + test_udev_rule_parse_value_one("\"valid operand\"", "valid operand", 0); /* input: "va'l\'id\"op\"erand" * parsed: va'l\'id"op"erand */ - test_udev_rule_parse_value_one( - "\"va'l\\'id\\\"op\\\"erand\"", - "va'l\\'id\"op\"erand", - 0 - ); -} - -static void test_parse_value_no_quotes(void) { - test_udev_rule_parse_value_one( - "no quotes", - 0, - -EINVAL - ); -} - -static void test_parse_value_noescape(void) { - test_udev_rule_parse_value_one( - "\"\\\\a\\b\\x\\y\"", - "\\\\a\\b\\x\\y", - 0 - ); -} - -static void test_parse_value_nul(void) { - test_udev_rule_parse_value_one( - "\"reject\0nul\"", - 0, - -EINVAL - ); -} - -static void test_parse_value_escape_nothing(void) { + test_udev_rule_parse_value_one("\"va'l\\'id\\\"op\\\"erand\"", "va'l\\'id\"op\"erand", 0); + test_udev_rule_parse_value_one("no quotes", 0, -EINVAL); + test_udev_rule_parse_value_one("\"\\\\a\\b\\x\\y\"", "\\\\a\\b\\x\\y", 0); + test_udev_rule_parse_value_one("\"reject\0nul\"", 0, -EINVAL); /* input: e"" */ - test_udev_rule_parse_value_one( - "e\"\"", - "", - 0 - ); -} - -static void test_parse_value_escape_nothing2(void) { + test_udev_rule_parse_value_one("e\"\"", "", 0); /* input: e"1234" */ - test_udev_rule_parse_value_one( - "e\"1234\"", - "1234", - 0 - ); -} - -static void test_parse_value_escape_double_quote(void) { + test_udev_rule_parse_value_one("e\"1234\"", "1234", 0); /* input: e"\"" */ - test_udev_rule_parse_value_one( - "e\"\\\"\"", - "\"", - 0 - ); -} - -static void test_parse_value_escape_backslash(void) { + test_udev_rule_parse_value_one("e\"\\\"\"", "\"", 0); /* input: e"\ */ - test_udev_rule_parse_value_one( - "e\"\\", - 0, - -EINVAL - ); + test_udev_rule_parse_value_one("e\"\\", 0, -EINVAL); /* input: e"\" */ - test_udev_rule_parse_value_one( - "e\"\\\"", - 0, - -EINVAL - ); + test_udev_rule_parse_value_one("e\"\\\"", 0, -EINVAL); /* input: e"\\" */ - test_udev_rule_parse_value_one( - "e\"\\\\\"", - "\\", - 0 - ); + test_udev_rule_parse_value_one("e\"\\\\\"", "\\", 0); /* input: e"\\\" */ - test_udev_rule_parse_value_one( - "e\"\\\\\\\"", - 0, - -EINVAL - ); + test_udev_rule_parse_value_one("e\"\\\\\\\"", 0, -EINVAL); /* input: e"\\\"" */ - test_udev_rule_parse_value_one( - "e\"\\\\\\\"\"", - "\\\"", - 0 - ); + test_udev_rule_parse_value_one("e\"\\\\\\\"\"", "\\\"", 0); /* input: e"\\\\" */ - test_udev_rule_parse_value_one( - "e\"\\\\\\\\\"", - "\\\\", - 0 - ); -} - -static void test_parse_value_newline(void) { + test_udev_rule_parse_value_one("e\"\\\\\\\\\"", "\\\\", 0); /* input: e"operand with newline\n" */ - test_udev_rule_parse_value_one( - "e\"operand with newline\\n\"", - "operand with newline\n", - 0 - ); -} - -static void test_parse_value_escaped(void) { + test_udev_rule_parse_value_one("e\"operand with newline\\n\"", "operand with newline\n", 0); /* input: e"single\rcharacter\t\aescape\bsequence" */ test_udev_rule_parse_value_one( - "e\"single\\rcharacter\\t\\aescape\\bsequence\"", - "single\rcharacter\t\aescape\bsequence", - 0 - ); -} - -static void test_parse_value_invalid_escape(void) { + "e\"single\\rcharacter\\t\\aescape\\bsequence\"", "single\rcharacter\t\aescape\bsequence", 0); /* input: e"reject\invalid escape sequence" */ - test_udev_rule_parse_value_one( - "e\"reject\\invalid escape sequence", - 0, - -EINVAL - ); -} - -static void test_parse_value_invalid_termination(void) { + test_udev_rule_parse_value_one("e\"reject\\invalid escape sequence", 0, -EINVAL); /* input: e"\ */ - test_udev_rule_parse_value_one( - "e\"\\", - 0, - -EINVAL - ); -} - -static void test_parse_value_unicode(void) { + test_udev_rule_parse_value_one("e\"\\", 0, -EINVAL); /* input: "s\u1d1c\u1d04\u029c \u1d1c\u0274\u026a\u1d04\u1d0f\u1d05\u1d07 \U0001d568\U0001d560\U0001d568" */ test_udev_rule_parse_value_one( "e\"s\\u1d1c\\u1d04\\u029c \\u1d1c\\u0274\\u026a\\u1d04\\u1d0f\\u1d05\\u1d07 \\U0001d568\\U0001d560\\U0001d568\"", "s\xe1\xb4\x9c\xe1\xb4\x84\xca\x9c \xe1\xb4\x9c\xc9\xb4\xc9\xaa\xe1\xb4\x84\xe1\xb4\x8f\xe1\xb4\x85\xe1\xb4\x87 \xf0\x9d\x95\xa8\xf0\x9d\x95\xa0\xf0\x9d\x95\xa8", - 0 - ); + 0); } static void test_udev_replace_whitespace_one_len(const char *str, size_t len, const char *expected) { @@ -196,9 +88,7 @@ static void test_udev_replace_whitespace_one(const char *str, const char *expect test_udev_replace_whitespace_one_len(str, strlen(str), expected); } -static void test_udev_replace_whitespace(void) { - log_info("/* %s */", __func__); - +TEST(udev_replace_whitespace) { test_udev_replace_whitespace_one("hogehoge", "hogehoge"); test_udev_replace_whitespace_one("hoge hoge", "hoge_hoge"); test_udev_replace_whitespace_one(" hoge hoge ", "hoge_hoge"); @@ -246,9 +136,7 @@ static void test_udev_resolve_subsys_kernel_one(const char *str, bool read_value assert_se(streq(result, expected)); } -static void test_udev_resolve_subsys_kernel(void) { - log_info("/* %s */", __func__); - +TEST(udev_resolve_subsys_kernel) { test_udev_resolve_subsys_kernel_one("hoge", false, -EINVAL, NULL); test_udev_resolve_subsys_kernel_one("[hoge", false, -EINVAL, NULL); test_udev_resolve_subsys_kernel_one("[hoge/foo", false, -EINVAL, NULL); @@ -267,25 +155,4 @@ static void test_udev_resolve_subsys_kernel(void) { test_udev_resolve_subsys_kernel_one("[net/lo]/address", true, 0, "00:00:00:00:00:00"); } -int main(int argc, char **argv) { - test_parse_value(); - test_parse_value_with_backslashes(); - test_parse_value_no_quotes(); - test_parse_value_nul(); - test_parse_value_noescape(); - - test_parse_value_escape_nothing(); - test_parse_value_escape_nothing2(); - test_parse_value_escape_double_quote(); - test_parse_value_escape_backslash(); - test_parse_value_newline(); - test_parse_value_escaped(); - test_parse_value_invalid_escape(); - test_parse_value_invalid_termination(); - test_parse_value_unicode(); - - test_udev_replace_whitespace(); - test_udev_resolve_subsys_kernel(); - - return EXIT_SUCCESS; -} +DEFINE_TEST_MAIN(LOG_INFO);