diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/shared/tests.h | 170 | ||||
-rw-r--r-- | src/test/test-hashmap.c | 2 | ||||
-rw-r--r-- | src/test/test-hmac.c | 2 | ||||
-rw-r--r-- | src/test/test-mountpoint-util.c | 2 | ||||
-rw-r--r-- | src/test/test-os-util.c | 51 | ||||
-rw-r--r-- | src/test/test-path-util.c | 8 | ||||
-rw-r--r-- | src/test/test-pretty-print.c | 2 | ||||
-rw-r--r-- | src/test/test-recovery-key.c | 2 | ||||
-rw-r--r-- | src/test/test-user-util.c | 4 |
9 files changed, 168 insertions, 75 deletions
diff --git a/src/shared/tests.h b/src/shared/tests.h index 644417f7c5..85f9463f9b 100644 --- a/src/shared/tests.h +++ b/src/shared/tests.h @@ -211,50 +211,142 @@ static inline int run_test_table(void) { } \ }) +#define ASSERT_TRUE(expr) \ + ({ \ + if (!(expr)) { \ + log_error("%s:%i: Assertion failed: expected \"%s\" to be true", \ + PROJECT_FILE, __LINE__, #expr); \ + abort(); \ + } \ + }) + +#define ASSERT_FALSE(expr) \ + ({ \ + if ((expr)) { \ + log_error("%s:%i: Assertion failed: expected \"%s\" to be false", \ + PROJECT_FILE, __LINE__, #expr); \ + abort(); \ + } \ + }) + +#define ASSERT_NULL(expr) \ + ({ \ + if ((expr) != NULL) { \ + log_error("%s:%i: Assertion failed: expected \"%s\" to be NULL", \ + PROJECT_FILE, __LINE__, #expr); \ + abort(); \ + } \ + }) + +#define ASSERT_NOT_NULL(expr) \ + ({ \ + if ((expr) == NULL) { \ + log_error("%s:%i: Assertion failed: expected \"%s\" to be not NULL", \ + PROJECT_FILE, __LINE__, #expr); \ + abort(); \ + } \ + }) + +#define ASSERT_STREQ(expr1, expr2) \ + ({ \ + const char* _expr1 = (expr1); \ + const char* _expr2 = (expr2); \ + if (strcmp(_expr1, _expr2) != 0) { \ + log_error("%s:%i: Assertion failed: expected \"%s == %s\", but \"%s != %s\"", \ + PROJECT_FILE, __LINE__, #expr1, #expr2, _expr1, _expr2); \ + abort(); \ + } \ + }) + /* DECIMAL_STR_FMT() uses _Generic which cannot be used in string concatenation so we have to format the * input into strings first and then format those into the final assertion message. */ -#define ASSERT_EQ(expr1, expr2) \ - ({ \ - typeof(expr1) _expr1 = (expr1); \ - typeof(expr2) _expr2 = (expr2); \ - if (_expr1 != _expr2) { \ - char _sexpr1[DECIMAL_STR_MAX(typeof(expr1))]; \ - char _sexpr2[DECIMAL_STR_MAX(typeof(expr2))]; \ - xsprintf(_sexpr1, DECIMAL_STR_FMT(_expr1), _expr1); \ - xsprintf(_sexpr2, DECIMAL_STR_FMT(_expr2), _expr2); \ - log_error("%s:%i: Assertion failed: expected \"%s == %s\", but \"%s != %s\"", \ - PROJECT_FILE, __LINE__, #expr1, #expr2, _sexpr1, _sexpr2); \ - abort(); \ - } \ +#define ASSERT_EQ(expr1, expr2) \ + ({ \ + typeof(expr1) _expr1 = (expr1); \ + typeof(expr2) _expr2 = (expr2); \ + if (_expr1 != _expr2) { \ + char _sexpr1[DECIMAL_STR_MAX(typeof(expr1))]; \ + char _sexpr2[DECIMAL_STR_MAX(typeof(expr2))]; \ + xsprintf(_sexpr1, DECIMAL_STR_FMT(_expr1), _expr1); \ + xsprintf(_sexpr2, DECIMAL_STR_FMT(_expr2), _expr2); \ + log_error("%s:%i: Assertion failed: expected \"%s == %s\", but \"%s != %s\"", \ + PROJECT_FILE, __LINE__, #expr1, #expr2, _sexpr1, _sexpr2); \ + abort(); \ + } \ + }) + +#define ASSERT_GE(expr1, expr2) \ + ({ \ + typeof(expr1) _expr1 = (expr1); \ + typeof(expr2) _expr2 = (expr2); \ + if (_expr1 < _expr2) { \ + char _sexpr1[DECIMAL_STR_MAX(typeof(expr1))]; \ + char _sexpr2[DECIMAL_STR_MAX(typeof(expr2))]; \ + xsprintf(_sexpr1, DECIMAL_STR_FMT(_expr1), _expr1); \ + xsprintf(_sexpr2, DECIMAL_STR_FMT(_expr2), _expr2); \ + log_error("%s:%i: Assertion failed: expected \"%s >= %s\", but \"%s < %s\"", \ + PROJECT_FILE, __LINE__, #expr1, #expr2, _sexpr1, _sexpr2); \ + abort(); \ + } \ + }) + +#define ASSERT_LE(expr1, expr2) \ + ({ \ + typeof(expr1) _expr1 = (expr1); \ + typeof(expr2) _expr2 = (expr2); \ + if (_expr1 > _expr2) { \ + char _sexpr1[DECIMAL_STR_MAX(typeof(expr1))]; \ + char _sexpr2[DECIMAL_STR_MAX(typeof(expr2))]; \ + xsprintf(_sexpr1, DECIMAL_STR_FMT(_expr1), _expr1); \ + xsprintf(_sexpr2, DECIMAL_STR_FMT(_expr2), _expr2); \ + log_error("%s:%i: Assertion failed: expected \"%s <= %s\", but \"%s > %s\"", \ + PROJECT_FILE, __LINE__, #expr1, #expr2, _sexpr1, _sexpr2); \ + abort(); \ + } \ + }) + +#define ASSERT_NE(expr1, expr2) \ + ({ \ + typeof(expr1) _expr1 = (expr1); \ + typeof(expr2) _expr2 = (expr2); \ + if (_expr1 == _expr2) { \ + char _sexpr1[DECIMAL_STR_MAX(typeof(expr1))]; \ + char _sexpr2[DECIMAL_STR_MAX(typeof(expr2))]; \ + xsprintf(_sexpr1, DECIMAL_STR_FMT(_expr1), _expr1); \ + xsprintf(_sexpr2, DECIMAL_STR_FMT(_expr2), _expr2); \ + log_error("%s:%i: Assertion failed: expected \"%s != %s\", but \"%s == %s\"", \ + PROJECT_FILE, __LINE__, #expr1, #expr2, _sexpr1, _sexpr2); \ + abort(); \ + } \ }) -#define ASSERT_GE(expr1, expr2) \ - ({ \ - typeof(expr1) _expr1 = (expr1); \ - typeof(expr2) _expr2 = (expr2); \ - if (_expr1 < _expr2) { \ - char _sexpr1[DECIMAL_STR_MAX(typeof(expr1))]; \ - char _sexpr2[DECIMAL_STR_MAX(typeof(expr2))]; \ - xsprintf(_sexpr1, DECIMAL_STR_FMT(_expr1), _expr1); \ - xsprintf(_sexpr2, DECIMAL_STR_FMT(_expr2), _expr2); \ - log_error("%s:%i: Assertion failed: expected \"%s >= %s\", but \"%s < %s\"", \ - PROJECT_FILE, __LINE__, #expr1, #expr2, _sexpr1, _sexpr2); \ - abort(); \ - } \ +#define ASSERT_GT(expr1, expr2) \ + ({ \ + typeof(expr1) _expr1 = (expr1); \ + typeof(expr2) _expr2 = (expr2); \ + if (!(_expr1 > _expr2)) { \ + char _sexpr1[DECIMAL_STR_MAX(typeof(expr1))]; \ + char _sexpr2[DECIMAL_STR_MAX(typeof(expr2))]; \ + xsprintf(_sexpr1, DECIMAL_STR_FMT(_expr1), _expr1); \ + xsprintf(_sexpr2, DECIMAL_STR_FMT(_expr2), _expr2); \ + log_error("%s:%i: Assertion failed: expected \"%s > %s\", but \"%s <= %s\"", \ + PROJECT_FILE, __LINE__, #expr1, #expr2, _sexpr1, _sexpr2); \ + abort(); \ + } \ }) -#define ASSERT_LE(expr1, expr2) \ - ({ \ - typeof(expr1) _expr1 = (expr1); \ - typeof(expr2) _expr2 = (expr2); \ - if (_expr1 > _expr2) { \ - char _sexpr1[DECIMAL_STR_MAX(typeof(expr1))]; \ - char _sexpr2[DECIMAL_STR_MAX(typeof(expr2))]; \ - xsprintf(_sexpr1, DECIMAL_STR_FMT(_expr1), _expr1); \ - xsprintf(_sexpr2, DECIMAL_STR_FMT(_expr2), _expr2); \ - log_error("%s:%i: Assertion failed: expected \"%s <= %s\", but \"%s > %s\"", \ - PROJECT_FILE, __LINE__, #expr1, #expr2, _sexpr1, _sexpr2); \ - abort(); \ - } \ +#define ASSERT_LT(expr1, expr2) \ + ({ \ + typeof(expr1) _expr1 = (expr1); \ + typeof(expr2) _expr2 = (expr2); \ + if (!(_expr1 < _expr2)) { \ + char _sexpr1[DECIMAL_STR_MAX(typeof(expr1))]; \ + char _sexpr2[DECIMAL_STR_MAX(typeof(expr2))]; \ + xsprintf(_sexpr1, DECIMAL_STR_FMT(_expr1), _expr1); \ + xsprintf(_sexpr2, DECIMAL_STR_FMT(_expr2), _expr2); \ + log_error("%s:%i: Assertion failed: expected \"%s < %s\", but \"%s >= %s\"", \ + PROJECT_FILE, __LINE__, #expr1, #expr2, _sexpr1, _sexpr2); \ + abort(); \ + } \ }) diff --git a/src/test/test-hashmap.c b/src/test/test-hashmap.c index 5daa0e64f6..cf4fca27e3 100644 --- a/src/test/test-hashmap.c +++ b/src/test/test-hashmap.c @@ -42,7 +42,7 @@ TEST(trivial_compare_func) { } TEST(string_compare_func) { - assert_se(string_compare_func("fred", "wilma") != 0); + ASSERT_NE(string_compare_func("fred", "wilma"), 0); assert_se(string_compare_func("fred", "fred") == 0); } diff --git a/src/test/test-hmac.c b/src/test/test-hmac.c index 1b788b191c..18c81adbcd 100644 --- a/src/test/test-hmac.c +++ b/src/test/test-hmac.c @@ -19,7 +19,7 @@ TEST(hmac) { "", result); hex_result = hexmem(result, sizeof(result)); - assert_se(streq_ptr(hex_result, "cadd5e42114351181f3abff477641d88efb57d2b5641a1e5c6d623363a6d3bad")); + ASSERT_STREQ(hex_result, "cadd5e42114351181f3abff477641d88efb57d2b5641a1e5c6d623363a6d3bad"); hex_result = mfree(hex_result); hmac_sha256_by_string("waldo", diff --git a/src/test/test-mountpoint-util.c b/src/test/test-mountpoint-util.c index 6d784a9ae8..affcaccc00 100644 --- a/src/test/test-mountpoint-util.c +++ b/src/test/test-mountpoint-util.c @@ -152,7 +152,7 @@ TEST(path_is_mount_point) { */ /* file mountpoints */ - assert_se(mkdtemp(tmp_dir) != NULL); + ASSERT_NOT_NULL(mkdtemp(tmp_dir)); file1 = path_join(tmp_dir, "file1"); assert_se(file1); file2 = path_join(tmp_dir, "file2"); diff --git a/src/test/test-os-util.c b/src/test/test-os-util.c index 84e55e1754..e51ab9d290 100644 --- a/src/test/test-os-util.c +++ b/src/test/test-os-util.c @@ -14,47 +14,47 @@ #include "tmpfile-util.h" TEST(path_is_os_tree) { - assert_se(path_is_os_tree("/") > 0); - assert_se(path_is_os_tree("/etc") == 0); + ASSERT_GT(path_is_os_tree("/"), 0); + ASSERT_EQ(path_is_os_tree("/etc"), 0); assert_se(path_is_os_tree("/idontexist") == -ENOENT); } TEST(parse_os_release) { /* Let's assume that we're running in a valid system, so os-release is available */ _cleanup_free_ char *id = NULL, *id2 = NULL, *name = NULL, *foobar = NULL; - assert_se(parse_os_release(NULL, "ID", &id) == 0); + ASSERT_EQ(parse_os_release(NULL, "ID", &id), 0); log_info("ID: %s", id); - assert_se(setenv("SYSTEMD_OS_RELEASE", "/dev/null", 1) == 0); - assert_se(parse_os_release(NULL, "ID", &id2) == 0); + ASSERT_EQ(setenv("SYSTEMD_OS_RELEASE", "/dev/null", 1), 0); + ASSERT_EQ(parse_os_release(NULL, "ID", &id2), 0); log_info("ID: %s", strnull(id2)); _cleanup_(unlink_tempfilep) char tmpfile[] = "/tmp/test-os-util.XXXXXX"; - assert_se(write_tmpfile(tmpfile, + ASSERT_EQ(write_tmpfile(tmpfile, "ID=the-id \n" - "NAME=the-name") == 0); + "NAME=the-name"), 0); - assert_se(setenv("SYSTEMD_OS_RELEASE", tmpfile, 1) == 0); - assert_se(parse_os_release(NULL, "ID", &id, "NAME", &name) == 0); + ASSERT_EQ(setenv("SYSTEMD_OS_RELEASE", tmpfile, 1), 0); + ASSERT_EQ(parse_os_release(NULL, "ID", &id, "NAME", &name), 0); log_info("ID: %s NAME: %s", id, name); assert_se(streq(id, "the-id")); assert_se(streq(name, "the-name")); _cleanup_(unlink_tempfilep) char tmpfile2[] = "/tmp/test-os-util.XXXXXX"; - assert_se(write_tmpfile(tmpfile2, + ASSERT_EQ(write_tmpfile(tmpfile2, "ID=\"ignored\" \n" "ID=\"the-id\" \n" - "NAME='the-name'") == 0); + "NAME='the-name'"), 0); - assert_se(setenv("SYSTEMD_OS_RELEASE", tmpfile2, 1) == 0); - assert_se(parse_os_release(NULL, "ID", &id, "NAME", &name) == 0); + ASSERT_EQ(setenv("SYSTEMD_OS_RELEASE", tmpfile2, 1), 0); + ASSERT_EQ(parse_os_release(NULL, "ID", &id, "NAME", &name), 0); log_info("ID: %s NAME: %s", id, name); assert_se(streq(id, "the-id")); assert_se(streq(name, "the-name")); - assert_se(parse_os_release(NULL, "FOOBAR", &foobar) == 0); + ASSERT_EQ(parse_os_release(NULL, "FOOBAR", &foobar), 0); log_info("FOOBAR: %s", strnull(foobar)); - assert_se(foobar == NULL); + ASSERT_NULL(foobar); assert_se(unsetenv("SYSTEMD_OS_RELEASE") == 0); } @@ -70,6 +70,7 @@ TEST(parse_extension_release) { assert_se(a = path_join(tempdir, "/usr/lib/extension-release.d/extension-release.test")); assert_se(mkdir_parents(a, 0777) >= 0); + ASSERT_GE(mkdir_parents(a, 0777), 0); r = write_string_file(a, "ID=the-id \n VERSION_ID=the-version-id", WRITE_STRING_FILE_CREATE); if (r < 0) @@ -87,42 +88,42 @@ TEST(parse_extension_release) { if (r < 0) log_error_errno(r, "Failed to write file: %m"); - assert_se(parse_extension_release(tempdir, IMAGE_CONFEXT, "tester", false, "ID", &id, "VERSION_ID", &version_id) == 0); + ASSERT_EQ(parse_extension_release(tempdir, IMAGE_CONFEXT, "tester", false, "ID", &id, "VERSION_ID", &version_id), 0); log_info("ID: %s VERSION_ID: %s", id, version_id); assert_se(streq(id, "the-id")); assert_se(streq(version_id, "the-version-id")); assert_se(parse_extension_release(tempdir, IMAGE_CONFEXT, "tester", false, "FOOBAR", &foobar) == 0); log_info("FOOBAR: %s", strnull(foobar)); - assert_se(foobar == NULL); + ASSERT_NULL(foobar); assert_se(parse_extension_release(tempdir, IMAGE_SYSEXT, "test", false, "FOOBAR", &foobar) == 0); log_info("FOOBAR: %s", strnull(foobar)); - assert_se(foobar == NULL); + ASSERT_NULL(foobar); } TEST(load_os_release_pairs) { _cleanup_(unlink_tempfilep) char tmpfile[] = "/tmp/test-os-util.XXXXXX"; - assert_se(write_tmpfile(tmpfile, + ASSERT_EQ(write_tmpfile(tmpfile, "ID=\"ignored\" \n" "ID=\"the-id\" \n" - "NAME='the-name'") == 0); + "NAME='the-name'"), 0); - assert_se(setenv("SYSTEMD_OS_RELEASE", tmpfile, 1) == 0); + ASSERT_EQ(setenv("SYSTEMD_OS_RELEASE", tmpfile, 1), 0); _cleanup_strv_free_ char **pairs = NULL; - assert_se(load_os_release_pairs(NULL, &pairs) == 0); + ASSERT_EQ(load_os_release_pairs(NULL, &pairs), 0); assert_se(strv_equal(pairs, STRV_MAKE("ID", "the-id", "NAME", "the-name"))); - assert_se(unsetenv("SYSTEMD_OS_RELEASE") == 0); + ASSERT_EQ(unsetenv("SYSTEMD_OS_RELEASE"), 0); } TEST(os_release_support_ended) { int r; - assert_se(os_release_support_ended("1999-01-01", false, NULL) == true); - assert_se(os_release_support_ended("2037-12-31", false, NULL) == false); + ASSERT_TRUE(os_release_support_ended("1999-01-01", false, NULL)); + ASSERT_FALSE(os_release_support_ended("2037-12-31", false, NULL)); assert_se(os_release_support_ended("-1-1-1", true, NULL) == -EINVAL); r = os_release_support_ended(NULL, false, NULL); diff --git a/src/test/test-path-util.c b/src/test/test-path-util.c index ca11bf3a29..ceff01967a 100644 --- a/src/test/test-path-util.c +++ b/src/test/test-path-util.c @@ -506,24 +506,24 @@ TEST(prefixes) { log_error("---%s---", s); assert_se(streq(s, values[i++])); } - assert_se(values[i] == NULL); + ASSERT_NULL(values[i]); i = 1; PATH_FOREACH_PREFIX(s, "/a/b/c/d") { log_error("---%s---", s); assert_se(streq(s, values[i++])); } - assert_se(values[i] == NULL); + ASSERT_NULL(values[i]); i = 0; PATH_FOREACH_PREFIX_MORE(s, "////a////b////c///d///////") assert_se(streq(s, values[i++])); - assert_se(values[i] == NULL); + ASSERT_NULL(values[i]); i = 1; PATH_FOREACH_PREFIX(s, "////a////b////c///d///////") assert_se(streq(s, values[i++])); - assert_se(values[i] == NULL); + ASSERT_NULL(values[i]); PATH_FOREACH_PREFIX(s, "////") assert_not_reached(); diff --git a/src/test/test-pretty-print.c b/src/test/test-pretty-print.c index 52b2bc861e..9ab52c6012 100644 --- a/src/test/test-pretty-print.c +++ b/src/test/test-pretty-print.c @@ -22,7 +22,7 @@ static void test_draw_cylon_one(unsigned pos) { memset(buf, 0xff, sizeof(buf)); draw_cylon(buf, sizeof(buf), CYLON_WIDTH, pos); - assert_se(strlen(buf) < sizeof(buf)); + ASSERT_LE(strlen(buf), sizeof(buf)); } TEST(draw_cylon) { diff --git a/src/test/test-recovery-key.c b/src/test/test-recovery-key.c index 909ef4ccf2..2e901dc9bc 100644 --- a/src/test/test-recovery-key.c +++ b/src/test/test-recovery-key.c @@ -17,7 +17,7 @@ TEST(make_recovery_key) { /* Check for successful recovery-key creation */ r = make_recovery_key(&recovery_key); assert_se(r == 0); - assert_se(recovery_key != NULL); + ASSERT_NOT_NULL(recovery_key); /* Check that length of formatted key is 72 with 64 modhex characters */ length = strlen(recovery_key); diff --git a/src/test/test-user-util.c b/src/test/test-user-util.c index db76cde0a2..f115a47773 100644 --- a/src/test/test-user-util.c +++ b/src/test/test-user-util.c @@ -137,8 +137,8 @@ TEST(parse_uid) { } TEST(uid_ptr) { - assert_se(UID_TO_PTR(0) != NULL); - assert_se(UID_TO_PTR(1000) != NULL); + ASSERT_NOT_NULL(UID_TO_PTR(0)); + ASSERT_NOT_NULL(UID_TO_PTR(1000)); assert_se(PTR_TO_UID(UID_TO_PTR(0)) == 0); assert_se(PTR_TO_UID(UID_TO_PTR(1000)) == 1000); |