From: Daan De Meyer Date: Wed, 29 Oct 2025 19:43:28 +0000 (+0100) Subject: test: migrate test-load-fragment to use ASSERT_* macros X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1a3b3c57c6ad140befb43c8d239d546d8925dd3b;p=thirdparty%2Fsystemd.git test: migrate test-load-fragment to use ASSERT_* macros Replace all assert_se() calls with appropriate ASSERT_* macros from tests.h: - assert_se(r >= 0) → ASSERT_OK(r) - assert_se(r == 0) → ASSERT_OK_ZERO(r) - assert_se(r == -ERR) → ASSERT_ERROR(r, ERR) - assert_se(expr == value) → ASSERT_EQ(expr, value) - assert_se(str1, str2) → ASSERT_STREQ(str1, str2) - assert_se(ptr) → ASSERT_NOT_NULL(ptr) - assert_se(!ptr) → ASSERT_NULL(ptr) - assert_se(expr) → ASSERT_TRUE(expr) - assert_se(!expr) → ASSERT_FALSE(expr) Also added unsigned suffixes (U) to numeric literals in comparisons with unsigned types (size_t, rlim_t) to eliminate signedness warnings. This code was written with the help of AI. --- diff --git a/src/fundamental/string-util-fundamental.h b/src/fundamental/string-util-fundamental.h index ff74a80a183..98fcc0881fc 100644 --- a/src/fundamental/string-util-fundamental.h +++ b/src/fundamental/string-util-fundamental.h @@ -55,6 +55,13 @@ static inline int strcmp_ptr(const sd_char *a, const sd_char *b) { return CMP(a, b); } +static inline int strncmp_ptr(const sd_char *a, const sd_char *b, size_t n) { + if (a && b) + return strncmp(a, b, n); + + return CMP(a, b); +} + static inline int strcasecmp_ptr(const sd_char *a, const sd_char *b) { if (a && b) return strcasecmp(a, b); @@ -66,6 +73,10 @@ static inline bool streq_ptr(const sd_char *a, const sd_char *b) { return strcmp_ptr(a, b) == 0; } +static inline bool strneq_ptr(const sd_char *a, const sd_char *b, size_t n) { + return strncmp_ptr(a, b, n) == 0; +} + static inline bool strcaseeq_ptr(const sd_char *a, const sd_char *b) { return strcasecmp_ptr(a, b) == 0; } diff --git a/src/shared/tests.h b/src/shared/tests.h index a408fbd087c..e4c7a296089 100644 --- a/src/shared/tests.h +++ b/src/shared/tests.h @@ -390,6 +390,19 @@ _noreturn_ void log_test_failed_internal(const char *file, int line, const char }) #endif +#ifdef __COVERITY__ +# define ASSERT_STRNEQ(expr1, expr2, n) __coverity_check__(strneq_ptr((expr1), (expr2), (n))) +#else +# define ASSERT_STRNEQ(expr1, expr2, n) \ + ({ \ + const char *_expr1 = (expr1), *_expr2 = (expr2); \ + size_t _n = (n); \ + if (!strneq_ptr(_expr1, _expr2, _n)) \ + log_test_failed("Expected \"%s == %s\", got \"%s != %s\" (first %zu characters)", \ + #expr1, #expr2, strnull(_expr1), strnull(_expr2), _n); \ + }) +#endif + #ifdef __COVERITY__ # define ASSERT_PTR_EQ(expr1, expr2) __coverity_check__((expr1) == (expr2)) #else diff --git a/src/test/test-load-fragment.c b/src/test/test-load-fragment.c index 4fd3b0a07d8..9ddc8a2eba2 100644 --- a/src/test/test-load-fragment.c +++ b/src/test/test-load-fragment.c @@ -66,7 +66,7 @@ static void check_execcommand(ExecCommand *c, bool ignore) { size_t n; - assert_se(c); + ASSERT_NOT_NULL(c); log_info("expect: \"%s\" [\"%s\" \"%s\" \"%s\"]", path, argv0 ?: path, strnull(argv1), strnull(argv2)); n = strv_length(c->argv); @@ -78,7 +78,7 @@ static void check_execcommand(ExecCommand *c, ASSERT_STREQ(c->argv[1], argv1); if (n > 1) ASSERT_STREQ(c->argv[2], argv2); - assert_se(!!(c->flags & EXEC_COMMAND_IGNORE_FAILURE) == ignore); + ASSERT_EQ(FLAGS_SET(c->flags, EXEC_COMMAND_IGNORE_FAILURE), ignore); } TEST(config_parse_exec) { @@ -106,93 +106,81 @@ TEST(config_parse_exec) { return; } - assert_se(r >= 0); - assert_se(manager_startup(m, NULL, NULL, NULL) >= 0); + ASSERT_OK(r); + ASSERT_OK(manager_startup(m, NULL, NULL, NULL)); - assert_se(u = unit_new(m, sizeof(Service))); + ASSERT_NOT_NULL(u = unit_new(m, sizeof(Service))); log_info("/* basic test */"); - r = config_parse_exec(NULL, "fake", 1, "section", 1, - "LValue", 0, "/RValue r1", - &c, u); - assert_se(r >= 0); + ASSERT_OK(config_parse_exec(NULL, "fake", 1, "section", 1, + "LValue", 0, "/RValue r1", + &c, u)); check_execcommand(c, "/RValue", "/RValue", "r1", NULL, false); - r = config_parse_exec(NULL, "fake", 2, "section", 1, - "LValue", 0, "/RValue///slashes r1///", - &c, u); - log_info("/* test slashes */"); - assert_se(r >= 0); + ASSERT_OK(config_parse_exec(NULL, "fake", 2, "section", 1, + "LValue", 0, "/RValue///slashes r1///", + &c, u)); c1 = c->command_next; check_execcommand(c1, "/RValue/slashes", "/RValue///slashes", "r1///", NULL, false); log_info("/* trailing slash */"); - r = config_parse_exec(NULL, "fake", 4, "section", 1, - "LValue", 0, "/RValue/ argv0 r1", - &c, u); - assert_se(r == -ENOEXEC); + ASSERT_ERROR(config_parse_exec(NULL, "fake", 4, "section", 1, + "LValue", 0, "/RValue/ argv0 r1", + &c, u), ENOEXEC); ASSERT_NULL(c1->command_next); log_info("/* honour_argv0 */"); - r = config_parse_exec(NULL, "fake", 3, "section", 1, - "LValue", 0, "@/RValue///slashes2 ///argv0 r1", - &c, u); - assert_se(r >= 0); + ASSERT_OK(config_parse_exec(NULL, "fake", 3, "section", 1, + "LValue", 0, "@/RValue///slashes2 ///argv0 r1", + &c, u)); c1 = c1->command_next; check_execcommand(c1, "/RValue/slashes2", "///argv0", "r1", NULL, false); log_info("/* honour_argv0, no args */"); - r = config_parse_exec(NULL, "fake", 3, "section", 1, - "LValue", 0, "@/RValue", - &c, u); - assert_se(r == -ENOEXEC); + ASSERT_ERROR(config_parse_exec(NULL, "fake", 3, "section", 1, + "LValue", 0, "@/RValue", + &c, u), ENOEXEC); ASSERT_NULL(c1->command_next); log_info("/* no command, whitespace only, reset */"); - r = config_parse_exec(NULL, "fake", 3, "section", 1, - "LValue", 0, "", - &c, u); - assert_se(r == 0); + ASSERT_OK_ZERO(config_parse_exec(NULL, "fake", 3, "section", 1, + "LValue", 0, "", + &c, u)); ASSERT_NULL(c); log_info("/* ignore && honour_argv0 */"); - r = config_parse_exec(NULL, "fake", 4, "section", 1, - "LValue", 0, "-@/RValue///slashes3 argv0a r1", - &c, u); - assert_se(r >= 0); + ASSERT_OK(config_parse_exec(NULL, "fake", 4, "section", 1, + "LValue", 0, "-@/RValue///slashes3 argv0a r1", + &c, u)); c1 = c; check_execcommand(c1, "/RValue/slashes3", "argv0a", "r1", NULL, true); log_info("/* ignore && honour_argv0 */"); - r = config_parse_exec(NULL, "fake", 4, "section", 1, - "LValue", 0, "@-/RValue///slashes4 argv0b r1", - &c, u); - assert_se(r >= 0); + ASSERT_OK(config_parse_exec(NULL, "fake", 4, "section", 1, + "LValue", 0, "@-/RValue///slashes4 argv0b r1", + &c, u)); c1 = c1->command_next; check_execcommand(c1, "/RValue/slashes4", "argv0b", "r1", NULL, true); log_info("/* ignore && ignore */"); - r = config_parse_exec(NULL, "fake", 4, "section", 1, - "LValue", 0, "--/RValue argv0 r1", - &c, u); - assert_se(r == 0); + ASSERT_OK_ZERO(config_parse_exec(NULL, "fake", 4, "section", 1, + "LValue", 0, "--/RValue argv0 r1", + &c, u)); ASSERT_NULL(c1->command_next); log_info("/* ignore && ignore (2) */"); - r = config_parse_exec(NULL, "fake", 4, "section", 1, - "LValue", 0, "-@-/RValue argv0 r1", - &c, u); - assert_se(r == 0); + ASSERT_OK_ZERO(config_parse_exec(NULL, "fake", 4, "section", 1, + "LValue", 0, "-@-/RValue argv0 r1", + &c, u)); ASSERT_NULL(c1->command_next); log_info("/* semicolon */"); - r = config_parse_exec(NULL, "fake", 5, "section", 1, - "LValue", 0, - "-@/RValue argv0 r1 ; " - "/goo/goo boo", - &c, u); - assert_se(r >= 0); + ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1, + "LValue", 0, + "-@/RValue argv0 r1 ; " + "/goo/goo boo", + &c, u)); c1 = c1->command_next; check_execcommand(c1, "/RValue", "argv0", "r1", NULL, true); @@ -200,151 +188,136 @@ TEST(config_parse_exec) { check_execcommand(c1, "/goo/goo", NULL, "boo", NULL, false); log_info("/* two semicolons in a row */"); - r = config_parse_exec(NULL, "fake", 5, "section", 1, - "LValue", 0, - "-@/RValue argv0 r1 ; ; " - "/goo/goo boo", - &c, u); - assert_se(r >= 0); + ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1, + "LValue", 0, + "-@/RValue argv0 r1 ; ; " + "/goo/goo boo", + &c, u)); c1 = c1->command_next; check_execcommand(c1, "/RValue", "argv0", "r1", NULL, true); c1 = c1->command_next; check_execcommand(c1, "/goo/goo", "/goo/goo", "boo", NULL, false); log_info("/* trailing semicolon */"); - r = config_parse_exec(NULL, "fake", 5, "section", 1, - "LValue", 0, - "-@/RValue argv0 r1 ; ", - &c, u); - assert_se(r >= 0); + ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1, + "LValue", 0, + "-@/RValue argv0 r1 ; ", + &c, u)); c1 = c1->command_next; check_execcommand(c1, "/RValue", "argv0", "r1", NULL, true); ASSERT_NULL(c1->command_next); log_info("/* trailing semicolon, no whitespace */"); - r = config_parse_exec(NULL, "fake", 5, "section", 1, - "LValue", 0, - "-@/RValue argv0 r1 ;", - &c, u); - assert_se(r >= 0); + ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1, + "LValue", 0, + "-@/RValue argv0 r1 ;", + &c, u)); c1 = c1->command_next; check_execcommand(c1, "/RValue", "argv0", "r1", NULL, true); ASSERT_NULL(c1->command_next); log_info("/* trailing semicolon in single quotes */"); - r = config_parse_exec(NULL, "fake", 5, "section", 1, - "LValue", 0, - "-@/RValue argv0 r1 ';'", - &c, u); - assert_se(r >= 0); + ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1, + "LValue", 0, + "-@/RValue argv0 r1 ';'", + &c, u)); c1 = c1->command_next; check_execcommand(c1, "/RValue", "argv0", "r1", ";", true); log_info("/* escaped semicolon */"); - r = config_parse_exec(NULL, "fake", 5, "section", 1, - "LValue", 0, - "/bin/find \\;", - &c, u); - assert_se(r >= 0); + ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1, + "LValue", 0, + "/bin/find \\;", + &c, u)); c1 = c1->command_next; check_execcommand(c1, "/bin/find", NULL, ";", NULL, false); log_info("/* escaped semicolon with following arg */"); - r = config_parse_exec(NULL, "fake", 5, "section", 1, - "LValue", 0, - "/sbin/find \\; /x", - &c, u); - assert_se(r >= 0); + ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1, + "LValue", 0, + "/sbin/find \\; /x", + &c, u)); c1 = c1->command_next; check_execcommand(c1, "/sbin/find", NULL, ";", "/x", false); log_info("/* escaped semicolon as part of an expression */"); - r = config_parse_exec(NULL, "fake", 5, "section", 1, - "LValue", 0, - "/sbin/find \\;x", - &c, u); - assert_se(r >= 0); + ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1, + "LValue", 0, + "/sbin/find \\;x", + &c, u)); c1 = c1->command_next; check_execcommand(c1, "/sbin/find", NULL, "\\;x", NULL, false); log_info("/* encoded semicolon */"); - r = config_parse_exec(NULL, "fake", 5, "section", 1, - "LValue", 0, - "/bin/find \\073", - &c, u); - assert_se(r >= 0); + ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1, + "LValue", 0, + "/bin/find \\073", + &c, u)); c1 = c1->command_next; check_execcommand(c1, "/bin/find", NULL, ";", NULL, false); log_info("/* quoted semicolon */"); - r = config_parse_exec(NULL, "fake", 5, "section", 1, - "LValue", 0, - "/bin/find \";\"", - &c, u); - assert_se(r >= 0); + ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1, + "LValue", 0, + "/bin/find \";\"", + &c, u)); c1 = c1->command_next; check_execcommand(c1, "/bin/find", NULL, ";", NULL, false); log_info("/* quoted semicolon with following arg */"); - r = config_parse_exec(NULL, "fake", 5, "section", 1, - "LValue", 0, - "/sbin/find \";\" /x", - &c, u); - assert_se(r >= 0); + ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1, + "LValue", 0, + "/sbin/find \";\" /x", + &c, u)); c1 = c1->command_next; check_execcommand(c1, "/sbin/find", NULL, ";", "/x", false); log_info("/* spaces in the filename */"); - r = config_parse_exec(NULL, "fake", 5, "section", 1, - "LValue", 0, - "\"/PATH WITH SPACES/daemon\" -1 -2", - &c, u); - assert_se(r >= 0); + ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1, + "LValue", 0, + "\"/PATH WITH SPACES/daemon\" -1 -2", + &c, u)); c1 = c1->command_next; check_execcommand(c1, "/PATH WITH SPACES/daemon", NULL, "-1", "-2", false); log_info("/* spaces in the filename, no args */"); - r = config_parse_exec(NULL, "fake", 5, "section", 1, - "LValue", 0, - "\"/PATH WITH SPACES/daemon -1 -2\"", - &c, u); - assert_se(r >= 0); + ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1, + "LValue", 0, + "\"/PATH WITH SPACES/daemon -1 -2\"", + &c, u)); c1 = c1->command_next; check_execcommand(c1, "/PATH WITH SPACES/daemon -1 -2", NULL, NULL, NULL, false); log_info("/* spaces in the filename, everything quoted */"); - r = config_parse_exec(NULL, "fake", 5, "section", 1, - "LValue", 0, - "\"/PATH WITH SPACES/daemon\" \"-1\" '-2'", - &c, u); - assert_se(r >= 0); + ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1, + "LValue", 0, + "\"/PATH WITH SPACES/daemon\" \"-1\" '-2'", + &c, u)); c1 = c1->command_next; check_execcommand(c1, "/PATH WITH SPACES/daemon", NULL, "-1", "-2", false); log_info("/* escaped spaces in the filename */"); - r = config_parse_exec(NULL, "fake", 5, "section", 1, - "LValue", 0, - "\"/PATH\\sWITH\\sSPACES/daemon\" '-1 -2'", - &c, u); - assert_se(r >= 0); + ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1, + "LValue", 0, + "\"/PATH\\sWITH\\sSPACES/daemon\" '-1 -2'", + &c, u)); c1 = c1->command_next; check_execcommand(c1, "/PATH WITH SPACES/daemon", NULL, "-1 -2", NULL, false); log_info("/* escaped spaces in the filename (2) */"); - r = config_parse_exec(NULL, "fake", 5, "section", 1, - "LValue", 0, - "\"/PATH\\x20WITH\\x20SPACES/daemon\" \"-1 -2\"", - &c, u); - assert_se(r >= 0); + ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1, + "LValue", 0, + "\"/PATH\\x20WITH\\x20SPACES/daemon\" \"-1 -2\"", + &c, u)); c1 = c1->command_next; check_execcommand(c1, "/PATH WITH SPACES/daemon", NULL, "-1 -2", NULL, false); @@ -355,64 +328,56 @@ TEST(config_parse_exec) { path[sizeof(path) - 2] = *ccc; log_info("/* invalid character: \\%c */", *ccc); - r = config_parse_exec(NULL, "fake", 4, "section", 1, - "LValue", 0, path, - &c, u); - assert_se(r == -ENOEXEC); + ASSERT_ERROR(config_parse_exec(NULL, "fake", 4, "section", 1, + "LValue", 0, path, + &c, u), ENOEXEC); ASSERT_NULL(c1->command_next); } log_info("/* valid character: \\s */"); - r = config_parse_exec(NULL, "fake", 4, "section", 1, - "LValue", 0, "/path\\s", - &c, u); - assert_se(r >= 0); + ASSERT_OK(config_parse_exec(NULL, "fake", 4, "section", 1, + "LValue", 0, "/path\\s", + &c, u)); c1 = c1->command_next; check_execcommand(c1, "/path ", NULL, NULL, NULL, false); log_info("/* quoted backslashes */"); - r = config_parse_exec(NULL, "fake", 5, "section", 1, - "LValue", 0, - "/bin/grep '\\w+\\K'", - &c, u); - assert_se(r >= 0); + ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1, + "LValue", 0, + "/bin/grep '\\w+\\K'", + &c, u)); c1 = c1->command_next; check_execcommand(c1, "/bin/grep", NULL, "\\w+\\K", NULL, false); log_info("/* trailing backslash: \\ */"); /* backslash is invalid */ - r = config_parse_exec(NULL, "fake", 4, "section", 1, - "LValue", 0, "/path\\", - &c, u); - assert_se(r == -ENOEXEC); + ASSERT_ERROR(config_parse_exec(NULL, "fake", 4, "section", 1, + "LValue", 0, "/path\\", + &c, u), ENOEXEC); ASSERT_NULL(c1->command_next); log_info("/* missing ending ' */"); - r = config_parse_exec(NULL, "fake", 4, "section", 1, - "LValue", 0, "/path 'foo", - &c, u); - assert_se(r == -ENOEXEC); + ASSERT_ERROR(config_parse_exec(NULL, "fake", 4, "section", 1, + "LValue", 0, "/path 'foo", + &c, u), ENOEXEC); ASSERT_NULL(c1->command_next); log_info("/* missing ending ' with trailing backslash */"); - r = config_parse_exec(NULL, "fake", 4, "section", 1, - "LValue", 0, "/path 'foo\\", - &c, u); - assert_se(r == -ENOEXEC); + ASSERT_ERROR(config_parse_exec(NULL, "fake", 4, "section", 1, + "LValue", 0, "/path 'foo\\", + &c, u), ENOEXEC); ASSERT_NULL(c1->command_next); log_info("/* invalid space between modifiers */"); - r = config_parse_exec(NULL, "fake", 4, "section", 1, - "LValue", 0, "- /path", - &c, u); - assert_se(r == 0); + ASSERT_OK_ZERO(config_parse_exec(NULL, "fake", 4, "section", 1, + "LValue", 0, "- /path", + &c, u)); ASSERT_NULL(c1->command_next); log_info("/* only modifiers, no path */"); - r = config_parse_exec(NULL, "fake", 4, "section", 1, - "LValue", 0, "-", - &c, u); - assert_se(r == 0); + ASSERT_OK_ZERO(config_parse_exec(NULL, "fake", 4, "section", 1, + "LValue", 0, "-", + &c, u)); ASSERT_NULL(c1->command_next); log_info("/* long arg */"); /* See issue #22957. */ @@ -422,19 +387,17 @@ TEST(config_parse_exec) { memset(y, 'x', sizeof(x) - STRLEN("/bin/echo ") - 1); x[sizeof(x) - 1] = '\0'; - r = config_parse_exec(NULL, "fake", 5, "section", 1, - "LValue", 0, x, - &c, u); - assert_se(r >= 0); + ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1, + "LValue", 0, x, + &c, u)); c1 = c1->command_next; check_execcommand(c1, "/bin/echo", NULL, y, NULL, false); log_info("/* empty argument, reset */"); - r = config_parse_exec(NULL, "fake", 4, "section", 1, - "LValue", 0, "", - &c, u); - assert_se(r == 0); + ASSERT_OK_ZERO(config_parse_exec(NULL, "fake", 4, "section", 1, + "LValue", 0, "", + &c, u)); ASSERT_NULL(c); exec_command_free_list(c); @@ -465,39 +428,36 @@ TEST(config_parse_log_extra_fields) { return; } - assert_se(r >= 0); - assert_se(manager_startup(m, NULL, NULL, NULL) >= 0); + ASSERT_OK(r); + ASSERT_OK(manager_startup(m, NULL, NULL, NULL)); - assert_se(u = unit_new(m, sizeof(Service))); + ASSERT_NOT_NULL(u = unit_new(m, sizeof(Service))); log_info("/* %s – basic test */", __func__); - r = config_parse_log_extra_fields(NULL, "fake", 1, "section", 1, - "LValue", 0, "FOO=BAR \"QOOF=quux ' ' \"", - &c, u); - assert_se(r >= 0); - assert_se(c.n_log_extra_fields == 2); - assert_se(strneq(c.log_extra_fields[0].iov_base, "FOO=BAR", c.log_extra_fields[0].iov_len)); - assert_se(strneq(c.log_extra_fields[1].iov_base, "QOOF=quux ' ' ", c.log_extra_fields[1].iov_len)); + ASSERT_OK(config_parse_log_extra_fields(NULL, "fake", 1, "section", 1, + "LValue", 0, "FOO=BAR \"QOOF=quux ' ' \"", + &c, u)); + ASSERT_EQ(c.n_log_extra_fields, 2U); + ASSERT_STRNEQ(c.log_extra_fields[0].iov_base, "FOO=BAR", c.log_extra_fields[0].iov_len); + ASSERT_STRNEQ(c.log_extra_fields[1].iov_base, "QOOF=quux ' ' ", c.log_extra_fields[1].iov_len); log_info("/* %s – add some */", __func__); - r = config_parse_log_extra_fields(NULL, "fake", 1, "section", 1, - "LValue", 0, "FOO2=BAR2 QOOF2=quux ' '", - &c, u); - assert_se(r >= 0); - assert_se(c.n_log_extra_fields == 4); - assert_se(strneq(c.log_extra_fields[0].iov_base, "FOO=BAR", c.log_extra_fields[0].iov_len)); - assert_se(strneq(c.log_extra_fields[1].iov_base, "QOOF=quux ' ' ", c.log_extra_fields[1].iov_len)); - assert_se(strneq(c.log_extra_fields[2].iov_base, "FOO2=BAR2", c.log_extra_fields[2].iov_len)); - assert_se(strneq(c.log_extra_fields[3].iov_base, "QOOF2=quux", c.log_extra_fields[3].iov_len)); + ASSERT_OK(config_parse_log_extra_fields(NULL, "fake", 1, "section", 1, + "LValue", 0, "FOO2=BAR2 QOOF2=quux ' '", + &c, u)); + ASSERT_EQ(c.n_log_extra_fields, 4U); + ASSERT_STRNEQ(c.log_extra_fields[0].iov_base, "FOO=BAR", c.log_extra_fields[0].iov_len); + ASSERT_STRNEQ(c.log_extra_fields[1].iov_base, "QOOF=quux ' ' ", c.log_extra_fields[1].iov_len); + ASSERT_STRNEQ(c.log_extra_fields[2].iov_base, "FOO2=BAR2", c.log_extra_fields[2].iov_len); + ASSERT_STRNEQ(c.log_extra_fields[3].iov_base, "QOOF2=quux", c.log_extra_fields[3].iov_len); exec_context_dump(&c, stdout, " --> "); log_info("/* %s – reset */", __func__); - r = config_parse_log_extra_fields(NULL, "fake", 1, "section", 1, - "LValue", 0, "", - &c, u); - assert_se(r >= 0); - assert_se(c.n_log_extra_fields == 0); + ASSERT_OK(config_parse_log_extra_fields(NULL, "fake", 1, "section", 1, + "LValue", 0, "", + &c, u)); + ASSERT_EQ(c.n_log_extra_fields, 0U); exec_context_free_log_extra_fields(&c); @@ -516,15 +476,19 @@ TEST(install_printf, .sd_booted = true) { _cleanup_free_ char *mid = NULL, *bid = NULL, *host = NULL, *gid = NULL, *group = NULL, *uid = NULL, *user = NULL; - if (sd_id128_get_machine(NULL) >= 0) - assert_se(specifier_machine_id('m', NULL, NULL, NULL, &mid) >= 0 && mid); - if (sd_booted() > 0) - assert_se(specifier_boot_id('b', NULL, NULL, NULL, &bid) >= 0 && bid); - assert_se(host = gethostname_malloc()); - assert_se(group = gid_to_name(getgid())); - assert_se(asprintf(&gid, UID_FMT, getgid()) >= 0); - assert_se(user = uid_to_name(getuid())); - assert_se(asprintf(&uid, UID_FMT, getuid()) >= 0); + if (sd_id128_get_machine(NULL) >= 0) { + ASSERT_OK(specifier_machine_id('m', NULL, NULL, NULL, &mid)); + ASSERT_NOT_NULL(mid); + } + if (sd_booted() > 0) { + ASSERT_OK(specifier_boot_id('b', NULL, NULL, NULL, &bid)); + ASSERT_NOT_NULL(bid); + } + ASSERT_NOT_NULL(host = gethostname_malloc()); + ASSERT_NOT_NULL(group = gid_to_name(getgid())); + ASSERT_OK(asprintf(&gid, UID_FMT, getgid())); + ASSERT_NOT_NULL(user = uid_to_name(getuid())); + ASSERT_OK(asprintf(&uid, UID_FMT, getuid())); #define expect(scope, src, pattern, result) \ do { \ @@ -532,14 +496,17 @@ TEST(install_printf, .sd_booted = true) { *d1 = ASSERT_PTR(strdup(i.name)), \ *d2 = ASSERT_PTR(strdup(i.path)); \ int r = install_name_printf(scope, &src, pattern, &t); \ - assert_se(result ? r >= 0 : r < 0); \ + if (result) \ + ASSERT_OK(r); \ + else \ + ASSERT_FAIL(r); \ memzero(i.name, strlen(i.name)); \ memzero(i.path, strlen(i.path)); \ if (result) { \ printf("%s\n", t); \ - ASSERT_STREQ(t, result); \ + ASSERT_STREQ(t, result); \ } else \ - assert_se(!t); \ + ASSERT_NULL(t); \ strcpy(i.name, d1); \ strcpy(i.path, d2); \ } while (false) @@ -612,157 +579,151 @@ TEST(config_parse_capability_set) { const char *rvalue, void *data, void *userdata) */ - int r; + uint64_t capability_bounding_set = 0; - r = config_parse_capability_set(NULL, "fake", 1, "section", 1, - "CapabilityBoundingSet", 0, "CAP_NET_RAW", - &capability_bounding_set, NULL); - assert_se(r >= 0); - assert_se(capability_bounding_set == make_cap(CAP_NET_RAW)); - - r = config_parse_capability_set(NULL, "fake", 1, "section", 1, - "CapabilityBoundingSet", 0, "CAP_NET_ADMIN", - &capability_bounding_set, NULL); - assert_se(r >= 0); - assert_se(capability_bounding_set == (make_cap(CAP_NET_RAW) | make_cap(CAP_NET_ADMIN))); - - r = config_parse_capability_set(NULL, "fake", 1, "section", 1, - "CapabilityBoundingSet", 0, "~CAP_NET_ADMIN", - &capability_bounding_set, NULL); - assert_se(r >= 0); - assert_se(capability_bounding_set == make_cap(CAP_NET_RAW)); - - r = config_parse_capability_set(NULL, "fake", 1, "section", 1, - "CapabilityBoundingSet", 0, "", - &capability_bounding_set, NULL); - assert_se(r >= 0); - assert_se(capability_bounding_set == UINT64_C(0)); - - r = config_parse_capability_set(NULL, "fake", 1, "section", 1, - "CapabilityBoundingSet", 0, "~", - &capability_bounding_set, NULL); - assert_se(r >= 0); - assert_se(cap_test_all(capability_bounding_set)); + ASSERT_OK(config_parse_capability_set(NULL, "fake", 1, "section", 1, + "CapabilityBoundingSet", 0, "CAP_NET_RAW", + &capability_bounding_set, NULL)); + ASSERT_EQ(capability_bounding_set, make_cap(CAP_NET_RAW)); + + ASSERT_OK(config_parse_capability_set(NULL, "fake", 1, "section", 1, + "CapabilityBoundingSet", 0, "CAP_NET_ADMIN", + &capability_bounding_set, NULL)); + ASSERT_EQ(capability_bounding_set, make_cap(CAP_NET_RAW) | make_cap(CAP_NET_ADMIN)); + + ASSERT_OK(config_parse_capability_set(NULL, "fake", 1, "section", 1, + "CapabilityBoundingSet", 0, "~CAP_NET_ADMIN", + &capability_bounding_set, NULL)); + ASSERT_EQ(capability_bounding_set, make_cap(CAP_NET_RAW)); + + ASSERT_OK(config_parse_capability_set(NULL, "fake", 1, "section", 1, + "CapabilityBoundingSet", 0, "", + &capability_bounding_set, NULL)); + ASSERT_EQ(capability_bounding_set, UINT64_C(0)); + + ASSERT_OK(config_parse_capability_set(NULL, "fake", 1, "section", 1, + "CapabilityBoundingSet", 0, "~", + &capability_bounding_set, NULL)); + ASSERT_TRUE(cap_test_all(capability_bounding_set)); capability_bounding_set = 0; - r = config_parse_capability_set(NULL, "fake", 1, "section", 1, - "CapabilityBoundingSet", 0, " 'CAP_NET_RAW' WAT_CAP??? CAP_NET_ADMIN CAP'_trailing_garbage", - &capability_bounding_set, NULL); - assert_se(r >= 0); - assert_se(capability_bounding_set == (make_cap(CAP_NET_RAW) | make_cap(CAP_NET_ADMIN))); + ASSERT_OK(config_parse_capability_set(NULL, "fake", 1, "section", 1, + "CapabilityBoundingSet", 0, " 'CAP_NET_RAW' WAT_CAP??? CAP_NET_ADMIN CAP'_trailing_garbage", + &capability_bounding_set, NULL)); + ASSERT_EQ(capability_bounding_set, make_cap(CAP_NET_RAW) | make_cap(CAP_NET_ADMIN)); } TEST(config_parse_rlimit) { struct rlimit * rl[_RLIMIT_MAX] = {}; - assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "55", rl, NULL) >= 0); - assert_se(rl[RLIMIT_NOFILE]); - assert_se(rl[RLIMIT_NOFILE]->rlim_cur == 55); - assert_se(rl[RLIMIT_NOFILE]->rlim_cur == rl[RLIMIT_NOFILE]->rlim_max); + ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "55", rl, NULL)); + ASSERT_NOT_NULL(rl[RLIMIT_NOFILE]); + ASSERT_EQ(rl[RLIMIT_NOFILE]->rlim_cur, 55U); + ASSERT_EQ(rl[RLIMIT_NOFILE]->rlim_cur, rl[RLIMIT_NOFILE]->rlim_max); - assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "55:66", rl, NULL) >= 0); - assert_se(rl[RLIMIT_NOFILE]); - assert_se(rl[RLIMIT_NOFILE]->rlim_cur == 55); - assert_se(rl[RLIMIT_NOFILE]->rlim_max == 66); + ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "55:66", rl, NULL)); + ASSERT_NOT_NULL(rl[RLIMIT_NOFILE]); + ASSERT_EQ(rl[RLIMIT_NOFILE]->rlim_cur, 55U); + ASSERT_EQ(rl[RLIMIT_NOFILE]->rlim_max, 66U); - assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "infinity", rl, NULL) >= 0); - assert_se(rl[RLIMIT_NOFILE]); - assert_se(rl[RLIMIT_NOFILE]->rlim_cur == RLIM_INFINITY); - assert_se(rl[RLIMIT_NOFILE]->rlim_cur == rl[RLIMIT_NOFILE]->rlim_max); + ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "infinity", rl, NULL)); + ASSERT_NOT_NULL(rl[RLIMIT_NOFILE]); + ASSERT_EQ(rl[RLIMIT_NOFILE]->rlim_cur, RLIM_INFINITY); + ASSERT_EQ(rl[RLIMIT_NOFILE]->rlim_cur, rl[RLIMIT_NOFILE]->rlim_max); - assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "infinity:infinity", rl, NULL) >= 0); - assert_se(rl[RLIMIT_NOFILE]); - assert_se(rl[RLIMIT_NOFILE]->rlim_cur == RLIM_INFINITY); - assert_se(rl[RLIMIT_NOFILE]->rlim_cur == rl[RLIMIT_NOFILE]->rlim_max); + ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "infinity:infinity", rl, NULL)); + ASSERT_NOT_NULL(rl[RLIMIT_NOFILE]); + ASSERT_EQ(rl[RLIMIT_NOFILE]->rlim_cur, RLIM_INFINITY); + ASSERT_EQ(rl[RLIMIT_NOFILE]->rlim_cur, rl[RLIMIT_NOFILE]->rlim_max); rl[RLIMIT_NOFILE]->rlim_cur = 10; rl[RLIMIT_NOFILE]->rlim_max = 20; /* Invalid values don't change rl */ - assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "10:20:30", rl, NULL) >= 0); - assert_se(rl[RLIMIT_NOFILE]); - assert_se(rl[RLIMIT_NOFILE]->rlim_cur == 10); - assert_se(rl[RLIMIT_NOFILE]->rlim_max == 20); - - assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "wat:wat", rl, NULL) >= 0); - assert_se(rl[RLIMIT_NOFILE]); - assert_se(rl[RLIMIT_NOFILE]->rlim_cur == 10); - assert_se(rl[RLIMIT_NOFILE]->rlim_max == 20); - - assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "66:wat", rl, NULL) >= 0); - assert_se(rl[RLIMIT_NOFILE]); - assert_se(rl[RLIMIT_NOFILE]->rlim_cur == 10); - assert_se(rl[RLIMIT_NOFILE]->rlim_max == 20); - - assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "200:100", rl, NULL) >= 0); - assert_se(rl[RLIMIT_NOFILE]); - assert_se(rl[RLIMIT_NOFILE]->rlim_cur == 10); - assert_se(rl[RLIMIT_NOFILE]->rlim_max == 20); + ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "10:20:30", rl, NULL)); + ASSERT_NOT_NULL(rl[RLIMIT_NOFILE]); + ASSERT_EQ(rl[RLIMIT_NOFILE]->rlim_cur, 10U); + ASSERT_EQ(rl[RLIMIT_NOFILE]->rlim_max, 20U); + + ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "wat:wat", rl, NULL)); + ASSERT_NOT_NULL(rl[RLIMIT_NOFILE]); + ASSERT_EQ(rl[RLIMIT_NOFILE]->rlim_cur, 10U); + ASSERT_EQ(rl[RLIMIT_NOFILE]->rlim_max, 20U); + + ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "66:wat", rl, NULL)); + ASSERT_NOT_NULL(rl[RLIMIT_NOFILE]); + ASSERT_EQ(rl[RLIMIT_NOFILE]->rlim_cur, 10U); + ASSERT_EQ(rl[RLIMIT_NOFILE]->rlim_max, 20U); + + ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "200:100", rl, NULL)); + ASSERT_NOT_NULL(rl[RLIMIT_NOFILE]); + ASSERT_EQ(rl[RLIMIT_NOFILE]->rlim_cur, 10U); + ASSERT_EQ(rl[RLIMIT_NOFILE]->rlim_max, 20U); rl[RLIMIT_NOFILE] = mfree(rl[RLIMIT_NOFILE]); - assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "56", rl, NULL) >= 0); - assert_se(rl[RLIMIT_CPU]); - assert_se(rl[RLIMIT_CPU]->rlim_cur == 56); - assert_se(rl[RLIMIT_CPU]->rlim_cur == rl[RLIMIT_CPU]->rlim_max); + ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "56", rl, NULL)); + ASSERT_NOT_NULL(rl[RLIMIT_CPU]); + ASSERT_EQ(rl[RLIMIT_CPU]->rlim_cur, 56U); + ASSERT_EQ(rl[RLIMIT_CPU]->rlim_cur, rl[RLIMIT_CPU]->rlim_max); - assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "57s", rl, NULL) >= 0); - assert_se(rl[RLIMIT_CPU]); - assert_se(rl[RLIMIT_CPU]->rlim_cur == 57); - assert_se(rl[RLIMIT_CPU]->rlim_cur == rl[RLIMIT_CPU]->rlim_max); + ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "57s", rl, NULL)); + ASSERT_NOT_NULL(rl[RLIMIT_CPU]); + ASSERT_EQ(rl[RLIMIT_CPU]->rlim_cur, 57U); + ASSERT_EQ(rl[RLIMIT_CPU]->rlim_cur, rl[RLIMIT_CPU]->rlim_max); - assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "40s:1m", rl, NULL) >= 0); - assert_se(rl[RLIMIT_CPU]); - assert_se(rl[RLIMIT_CPU]->rlim_cur == 40); - assert_se(rl[RLIMIT_CPU]->rlim_max == 60); + ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "40s:1m", rl, NULL)); + ASSERT_NOT_NULL(rl[RLIMIT_CPU]); + ASSERT_EQ(rl[RLIMIT_CPU]->rlim_cur, 40U); + ASSERT_EQ(rl[RLIMIT_CPU]->rlim_max, 60U); - assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "infinity", rl, NULL) >= 0); - assert_se(rl[RLIMIT_CPU]); - assert_se(rl[RLIMIT_CPU]->rlim_cur == RLIM_INFINITY); - assert_se(rl[RLIMIT_CPU]->rlim_cur == rl[RLIMIT_CPU]->rlim_max); + ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "infinity", rl, NULL)); + ASSERT_NOT_NULL(rl[RLIMIT_CPU]); + ASSERT_EQ(rl[RLIMIT_CPU]->rlim_cur, RLIM_INFINITY); + ASSERT_EQ(rl[RLIMIT_CPU]->rlim_cur, rl[RLIMIT_CPU]->rlim_max); - assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "1234ms", rl, NULL) >= 0); - assert_se(rl[RLIMIT_CPU]); - assert_se(rl[RLIMIT_CPU]->rlim_cur == 2); - assert_se(rl[RLIMIT_CPU]->rlim_cur == rl[RLIMIT_CPU]->rlim_max); + ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "1234ms", rl, NULL)); + ASSERT_NOT_NULL(rl[RLIMIT_CPU]); + ASSERT_EQ(rl[RLIMIT_CPU]->rlim_cur, 2U); + ASSERT_EQ(rl[RLIMIT_CPU]->rlim_cur, rl[RLIMIT_CPU]->rlim_max); rl[RLIMIT_CPU] = mfree(rl[RLIMIT_CPU]); - assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "58", rl, NULL) >= 0); - assert_se(rl[RLIMIT_RTTIME]); - assert_se(rl[RLIMIT_RTTIME]->rlim_cur == 58); - assert_se(rl[RLIMIT_RTTIME]->rlim_cur == rl[RLIMIT_RTTIME]->rlim_max); - - assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "58:60", rl, NULL) >= 0); - assert_se(rl[RLIMIT_RTTIME]); - assert_se(rl[RLIMIT_RTTIME]->rlim_cur == 58); - assert_se(rl[RLIMIT_RTTIME]->rlim_max == 60); - - assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "59s", rl, NULL) >= 0); - assert_se(rl[RLIMIT_RTTIME]); - assert_se(rl[RLIMIT_RTTIME]->rlim_cur == 59 * USEC_PER_SEC); - assert_se(rl[RLIMIT_RTTIME]->rlim_cur == rl[RLIMIT_RTTIME]->rlim_max); - - assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "59s:123s", rl, NULL) >= 0); - assert_se(rl[RLIMIT_RTTIME]); - assert_se(rl[RLIMIT_RTTIME]->rlim_cur == 59 * USEC_PER_SEC); - assert_se(rl[RLIMIT_RTTIME]->rlim_max == 123 * USEC_PER_SEC); - - assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "infinity", rl, NULL) >= 0); - assert_se(rl[RLIMIT_RTTIME]); - assert_se(rl[RLIMIT_RTTIME]->rlim_cur == RLIM_INFINITY); - assert_se(rl[RLIMIT_RTTIME]->rlim_cur == rl[RLIMIT_RTTIME]->rlim_max); - - assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "infinity:infinity", rl, NULL) >= 0); - assert_se(rl[RLIMIT_RTTIME]); - assert_se(rl[RLIMIT_RTTIME]->rlim_cur == RLIM_INFINITY); - assert_se(rl[RLIMIT_RTTIME]->rlim_cur == rl[RLIMIT_RTTIME]->rlim_max); - - assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "2345ms", rl, NULL) >= 0); - assert_se(rl[RLIMIT_RTTIME]); - assert_se(rl[RLIMIT_RTTIME]->rlim_cur == 2345 * USEC_PER_MSEC); - assert_se(rl[RLIMIT_RTTIME]->rlim_cur == rl[RLIMIT_RTTIME]->rlim_max); + ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "58", rl, NULL)); + ASSERT_NOT_NULL(rl[RLIMIT_RTTIME]); + ASSERT_EQ(rl[RLIMIT_RTTIME]->rlim_cur, 58U); + ASSERT_EQ(rl[RLIMIT_RTTIME]->rlim_cur, rl[RLIMIT_RTTIME]->rlim_max); + + ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "58:60", rl, NULL)); + ASSERT_NOT_NULL(rl[RLIMIT_RTTIME]); + ASSERT_EQ(rl[RLIMIT_RTTIME]->rlim_cur, 58U); + ASSERT_EQ(rl[RLIMIT_RTTIME]->rlim_max, 60U); + + ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "59s", rl, NULL)); + ASSERT_NOT_NULL(rl[RLIMIT_RTTIME]); + ASSERT_EQ(rl[RLIMIT_RTTIME]->rlim_cur, 59 * USEC_PER_SEC); + ASSERT_EQ(rl[RLIMIT_RTTIME]->rlim_cur, rl[RLIMIT_RTTIME]->rlim_max); + + ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "59s:123s", rl, NULL)); + ASSERT_NOT_NULL(rl[RLIMIT_RTTIME]); + ASSERT_EQ(rl[RLIMIT_RTTIME]->rlim_cur, 59 * USEC_PER_SEC); + ASSERT_EQ(rl[RLIMIT_RTTIME]->rlim_max, 123 * USEC_PER_SEC); + + ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "infinity", rl, NULL)); + ASSERT_NOT_NULL(rl[RLIMIT_RTTIME]); + ASSERT_EQ(rl[RLIMIT_RTTIME]->rlim_cur, RLIM_INFINITY); + ASSERT_EQ(rl[RLIMIT_RTTIME]->rlim_cur, rl[RLIMIT_RTTIME]->rlim_max); + + ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "infinity:infinity", rl, NULL)); + ASSERT_NOT_NULL(rl[RLIMIT_RTTIME]); + ASSERT_EQ(rl[RLIMIT_RTTIME]->rlim_cur, RLIM_INFINITY); + ASSERT_EQ(rl[RLIMIT_RTTIME]->rlim_cur, rl[RLIMIT_RTTIME]->rlim_max); + + ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "2345ms", rl, NULL)); + ASSERT_NOT_NULL(rl[RLIMIT_RTTIME]); + ASSERT_EQ(rl[RLIMIT_RTTIME]->rlim_cur, 2345 * USEC_PER_MSEC); + ASSERT_EQ(rl[RLIMIT_RTTIME]->rlim_cur, rl[RLIMIT_RTTIME]->rlim_max); rl[RLIMIT_RTTIME] = mfree(rl[RLIMIT_RTTIME]); } @@ -779,28 +740,25 @@ TEST(config_parse_pass_environ) { const char *rvalue, void *data, void *userdata) */ - int r; + _cleanup_strv_free_ char **passenv = NULL; - r = config_parse_pass_environ(NULL, "fake", 1, "section", 1, - "PassEnvironment", 0, "A B", - &passenv, NULL); - assert_se(r >= 0); - assert_se(strv_length(passenv) == 2); + ASSERT_OK(config_parse_pass_environ(NULL, "fake", 1, "section", 1, + "PassEnvironment", 0, "A B", + &passenv, NULL)); + ASSERT_EQ(strv_length(passenv), 2U); ASSERT_STREQ(passenv[0], "A"); ASSERT_STREQ(passenv[1], "B"); - r = config_parse_pass_environ(NULL, "fake", 1, "section", 1, - "PassEnvironment", 0, "", - &passenv, NULL); - assert_se(r >= 0); - assert_se(strv_isempty(passenv)); - - r = config_parse_pass_environ(NULL, "fake", 1, "section", 1, - "PassEnvironment", 0, "'invalid name' 'normal_name' A=1 'special_name$$' \\", - &passenv, NULL); - assert_se(r >= 0); - assert_se(strv_length(passenv) == 1); + ASSERT_OK(config_parse_pass_environ(NULL, "fake", 1, "section", 1, + "PassEnvironment", 0, "", + &passenv, NULL)); + ASSERT_TRUE(strv_isempty(passenv)); + + ASSERT_OK(config_parse_pass_environ(NULL, "fake", 1, "section", 1, + "PassEnvironment", 0, "'invalid name' 'normal_name' A=1 'special_name$$' \\", + &passenv, NULL)); + ASSERT_EQ(strv_length(passenv), 1U); ASSERT_STREQ(passenv[0], "normal_name"); } @@ -828,43 +786,38 @@ TEST(config_parse_unit_env_file) { return; } - assert_se(r >= 0); - assert_se(manager_startup(m, NULL, NULL, NULL) >= 0); - - assert_se(u = unit_new(m, sizeof(Service))); - assert_se(unit_add_name(u, "foobar.service") == 0); - - r = config_parse_unit_env_file(u->id, "fake", 1, "section", 1, - "EnvironmentFile", 0, "not-absolute", - &files, u); - assert_se(r == 0); - assert_se(strv_isempty(files)); - - r = config_parse_unit_env_file(u->id, "fake", 1, "section", 1, - "EnvironmentFile", 0, "/absolute1", - &files, u); - assert_se(r == 0); - assert_se(strv_length(files) == 1); - - r = config_parse_unit_env_file(u->id, "fake", 1, "section", 1, - "EnvironmentFile", 0, "/absolute2", - &files, u); - assert_se(r == 0); - assert_se(strv_length(files) == 2); + ASSERT_OK(r); + ASSERT_OK(manager_startup(m, NULL, NULL, NULL)); + + ASSERT_NOT_NULL(u = unit_new(m, sizeof(Service))); + ASSERT_OK_ZERO(unit_add_name(u, "foobar.service")); + + ASSERT_OK_ZERO(config_parse_unit_env_file(u->id, "fake", 1, "section", 1, + "EnvironmentFile", 0, "not-absolute", + &files, u)); + ASSERT_TRUE(strv_isempty(files)); + + ASSERT_OK_ZERO(config_parse_unit_env_file(u->id, "fake", 1, "section", 1, + "EnvironmentFile", 0, "/absolute1", + &files, u)); + ASSERT_EQ(strv_length(files), 1U); + + ASSERT_OK_ZERO(config_parse_unit_env_file(u->id, "fake", 1, "section", 1, + "EnvironmentFile", 0, "/absolute2", + &files, u)); + ASSERT_EQ(strv_length(files), 2U); ASSERT_STREQ(files[0], "/absolute1"); ASSERT_STREQ(files[1], "/absolute2"); - r = config_parse_unit_env_file(u->id, "fake", 1, "section", 1, - "EnvironmentFile", 0, "", - &files, u); - assert_se(r == 0); - assert_se(strv_isempty(files)); - - r = config_parse_unit_env_file(u->id, "fake", 1, "section", 1, - "EnvironmentFile", 0, "/path/%n.conf", - &files, u); - assert_se(r == 0); - assert_se(strv_length(files) == 1); + ASSERT_OK_ZERO(config_parse_unit_env_file(u->id, "fake", 1, "section", 1, + "EnvironmentFile", 0, "", + &files, u)); + ASSERT_TRUE(strv_isempty(files)); + + ASSERT_OK_ZERO(config_parse_unit_env_file(u->id, "fake", 1, "section", 1, + "EnvironmentFile", 0, "/path/%n.conf", + &files, u)); + ASSERT_EQ(strv_length(files), 1U); ASSERT_STREQ(files[0], "/path/foobar.service.conf"); } @@ -908,44 +861,42 @@ TEST(config_parse_memory_limit) { { "MemoryMax", "10", &c.memory_max, 10 }, { "MemoryMax", "infinity", &c.memory_max, CGROUP_LIMIT_MAX }, }; - int r; FOREACH_ELEMENT(test, limit_tests) { c.memory_min = CGROUP_LIMIT_DUMMY; c.memory_low = CGROUP_LIMIT_DUMMY; c.memory_high = CGROUP_LIMIT_DUMMY; c.memory_max = CGROUP_LIMIT_DUMMY; - r = config_parse_memory_limit(NULL, "fake", 1, "section", 1, - test->limit, 1, - test->value, &c, NULL); log_info("%s=%s\t%"PRIu64"==%"PRIu64, test->limit, test->value, *test->result, test->expected); - assert_se(r >= 0); - assert_se(*test->result == test->expected); + ASSERT_OK(config_parse_memory_limit(NULL, "fake", 1, "section", 1, + test->limit, 1, + test->value, &c, NULL)); + ASSERT_EQ(*test->result, test->expected); } } TEST(contains_instance_specifier_superset) { - assert_se(contains_instance_specifier_superset("foobar@a%i")); - assert_se(contains_instance_specifier_superset("foobar@%ia")); - assert_se(contains_instance_specifier_superset("foobar@%n")); - assert_se(contains_instance_specifier_superset("foobar@%n.service")); - assert_se(contains_instance_specifier_superset("foobar@%N")); - assert_se(contains_instance_specifier_superset("foobar@%N.service")); - assert_se(contains_instance_specifier_superset("foobar@baz.%N.service")); - assert_se(contains_instance_specifier_superset("@%N.service")); - assert_se(contains_instance_specifier_superset("@%N")); - assert_se(contains_instance_specifier_superset("@%a%N")); - - assert_se(!contains_instance_specifier_superset("foobar@%i.service")); - assert_se(!contains_instance_specifier_superset("foobar%ia.service")); - assert_se(!contains_instance_specifier_superset("foobar@%%n.service")); - assert_se(!contains_instance_specifier_superset("foobar@baz.service")); - assert_se(!contains_instance_specifier_superset("%N.service")); - assert_se(!contains_instance_specifier_superset("%N")); - assert_se(!contains_instance_specifier_superset("@%aN")); - assert_se(!contains_instance_specifier_superset("@%a%b")); + ASSERT_TRUE(contains_instance_specifier_superset("foobar@a%i")); + ASSERT_TRUE(contains_instance_specifier_superset("foobar@%ia")); + ASSERT_TRUE(contains_instance_specifier_superset("foobar@%n")); + ASSERT_TRUE(contains_instance_specifier_superset("foobar@%n.service")); + ASSERT_TRUE(contains_instance_specifier_superset("foobar@%N")); + ASSERT_TRUE(contains_instance_specifier_superset("foobar@%N.service")); + ASSERT_TRUE(contains_instance_specifier_superset("foobar@baz.%N.service")); + ASSERT_TRUE(contains_instance_specifier_superset("@%N.service")); + ASSERT_TRUE(contains_instance_specifier_superset("@%N")); + ASSERT_TRUE(contains_instance_specifier_superset("@%a%N")); + + ASSERT_FALSE(contains_instance_specifier_superset("foobar@%i.service")); + ASSERT_FALSE(contains_instance_specifier_superset("foobar%ia.service")); + ASSERT_FALSE(contains_instance_specifier_superset("foobar@%%n.service")); + ASSERT_FALSE(contains_instance_specifier_superset("foobar@baz.service")); + ASSERT_FALSE(contains_instance_specifier_superset("%N.service")); + ASSERT_FALSE(contains_instance_specifier_superset("%N")); + ASSERT_FALSE(contains_instance_specifier_superset("@%aN")); + ASSERT_FALSE(contains_instance_specifier_superset("@%a%b")); } TEST(unit_is_recursive_template_dependency) { @@ -959,36 +910,36 @@ TEST(unit_is_recursive_template_dependency) { return; } - assert_se(r >= 0); - assert_se(manager_startup(m, NULL, NULL, NULL) >= 0); + ASSERT_OK(r); + ASSERT_OK(manager_startup(m, NULL, NULL, NULL)); - assert_se(u = unit_new(m, sizeof(Service))); - assert_se(unit_add_name(u, "foobar@1.service") == 0); + ASSERT_NOT_NULL(u = unit_new(m, sizeof(Service))); + ASSERT_OK_ZERO(unit_add_name(u, "foobar@1.service")); u->fragment_path = strdup("/foobar@.service"); - assert_se(hashmap_put_strdup(&m->unit_id_map, "foobar@foobar@123.service", "/foobar@.service")); - assert_se(hashmap_put_strdup(&m->unit_id_map, "foobar@foobar@456.service", "/custom.service")); + ASSERT_OK(hashmap_put_strdup(&m->unit_id_map, "foobar@foobar@123.service", "/foobar@.service")); + ASSERT_OK(hashmap_put_strdup(&m->unit_id_map, "foobar@foobar@456.service", "/custom.service")); /* Test that %n, %N and any extension of %i specifiers in the instance are detected as recursive. */ - assert_se(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@123.service", "foobar@%N.service") == 1); - assert_se(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@123.service", "foobar@%n.service") == 1); - assert_se(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@123.service", "foobar@a%i.service") == 1); - assert_se(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@123.service", "foobar@%ia.service") == 1); - assert_se(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@123.service", "foobar@%x%n.service") == 1); + ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@123.service", "foobar@%N.service"), 1); + ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@123.service", "foobar@%n.service"), 1); + ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@123.service", "foobar@a%i.service"), 1); + ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@123.service", "foobar@%ia.service"), 1); + ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@123.service", "foobar@%x%n.service"), 1); /* Test that %i on its own is not detected as recursive. */ - assert_se(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@123.service", "foobar@%i.service") == 0); + ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@123.service", "foobar@%i.service"), 0); /* Test that a specifier other than %i, %n and %N is not detected as recursive. */ - assert_se(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@123.service", "foobar@%xn.service") == 0); + ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@123.service", "foobar@%xn.service"), 0); /* Test that an expanded specifier is not detected as recursive. */ - assert_se(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@123.service", "foobar@foobar@123.service") == 0); + ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@123.service", "foobar@foobar@123.service"), 0); /* Test that a dependency with a custom fragment path is not detected as recursive. */ - assert_se(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@456.service", "foobar@%n.service") == 0); + ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@456.service", "foobar@%n.service"), 0); /* Test that a dependency without a fragment path is not detected as recursive. */ - assert_se(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@789.service", "foobar@%n.service") == 0); + ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@789.service", "foobar@%n.service"), 0); /* Test that a dependency with a different prefix is not detected as recursive. */ - assert_se(unit_is_likely_recursive_template_dependency(u, "quux@foobar@123.service", "quux@%n.service") == 0); + ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "quux@foobar@123.service", "quux@%n.service"), 0); /* Test that a dependency of a different type is not detected as recursive. */ - assert_se(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@123.mount", "foobar@%n.mount") == 0); + ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@foobar@123.mount", "foobar@%n.mount"), 0); } #define TEST_PATTERN(_regex, _allowed_patterns_count, _denied_patterns_count) \ @@ -1024,18 +975,18 @@ TEST(config_parse_log_filter_patterns) { return (void) log_tests_skipped("PCRE2 support is not available"); FOREACH_ELEMENT(test, regex_tests) { - assert_se(config_parse_log_filter_patterns(NULL, "fake", 1, "section", 1, "LogFilterPatterns", 1, - test->regex, &c, NULL) >= 0); + ASSERT_OK(config_parse_log_filter_patterns(NULL, "fake", 1, "section", 1, "LogFilterPatterns", 1, + test->regex, &c, NULL)); - assert_se(set_size(c.log_filter_allowed_patterns) == test->allowed_patterns_count); - assert_se(set_size(c.log_filter_denied_patterns) == test->denied_patterns_count); + ASSERT_EQ(set_size(c.log_filter_allowed_patterns), test->allowed_patterns_count); + ASSERT_EQ(set_size(c.log_filter_denied_patterns), test->denied_patterns_count); /* Ensure `~` is properly removed */ const char *p; SET_FOREACH(p, c.log_filter_allowed_patterns) - assert_se(p && p[0] != '~'); + ASSERT_TRUE(p && p[0] != '~'); SET_FOREACH(p, c.log_filter_denied_patterns) - assert_se(p && p[0] != '~'); + ASSERT_TRUE(p && p[0] != '~'); } exec_context_done(&c); @@ -1053,43 +1004,40 @@ TEST(config_parse_open_file) { return; } - assert_se(r >= 0); - assert_se(manager_startup(m, NULL, NULL, NULL) >= 0); + ASSERT_OK(r); + ASSERT_OK(manager_startup(m, NULL, NULL, NULL)); - assert_se(u = unit_new(m, sizeof(Service))); - assert_se(unit_add_name(u, "foobar.service") == 0); + ASSERT_NOT_NULL(u = unit_new(m, sizeof(Service))); + ASSERT_OK_ZERO(unit_add_name(u, "foobar.service")); - r = config_parse_open_file(NULL, "fake", 1, "section", 1, - "OpenFile", 0, "/proc/1/ns/mnt:host-mount-namespace:read-only", - &of, u); - assert_se(r >= 0); - assert_se(of); + ASSERT_OK(config_parse_open_file(NULL, "fake", 1, "section", 1, + "OpenFile", 0, "/proc/1/ns/mnt:host-mount-namespace:read-only", + &of, u)); + ASSERT_NOT_NULL(of); ASSERT_STREQ(of->path, "/proc/1/ns/mnt"); ASSERT_STREQ(of->fdname, "host-mount-namespace"); - assert_se(of->flags == OPENFILE_READ_ONLY); + ASSERT_EQ(of->flags, OPENFILE_READ_ONLY); of = open_file_free(of); - r = config_parse_open_file(NULL, "fake", 1, "section", 1, - "OpenFile", 0, "/proc/1/ns/mnt::read-only", - &of, u); - assert_se(r >= 0); - assert_se(of); + ASSERT_OK(config_parse_open_file(NULL, "fake", 1, "section", 1, + "OpenFile", 0, "/proc/1/ns/mnt::read-only", + &of, u)); + ASSERT_NOT_NULL(of); ASSERT_STREQ(of->path, "/proc/1/ns/mnt"); ASSERT_STREQ(of->fdname, "mnt"); - assert_se(of->flags == OPENFILE_READ_ONLY); + ASSERT_EQ(of->flags, OPENFILE_READ_ONLY); - r = config_parse_open_file(NULL, "fake", 1, "section", 1, - "OpenFile", 0, "", - &of, u); - assert_se(r >= 0); - assert_se(!of); + ASSERT_OK(config_parse_open_file(NULL, "fake", 1, "section", 1, + "OpenFile", 0, "", + &of, u)); + ASSERT_NULL(of); } static int intro(void) { if (enter_cgroup_subroot(NULL) == -ENOMEDIUM) return log_tests_skipped("cgroupfs not available"); - assert_se(runtime_dir = setup_fake_runtime_dir()); + ASSERT_NOT_NULL(runtime_dir = setup_fake_runtime_dir()); return EXIT_SUCCESS; }