]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Added more ASSERT macro and also make some test file to use them
authorUnique-Usman <usmanakinyemi202@gmail.com>
Wed, 20 Mar 2024 17:35:55 +0000 (23:05 +0530)
committerLuca Boccassi <luca.boccassi@gmail.com>
Fri, 22 Mar 2024 12:31:14 +0000 (12:31 +0000)
src/shared/tests.h
src/test/test-hashmap.c
src/test/test-hmac.c
src/test/test-mountpoint-util.c
src/test/test-os-util.c
src/test/test-path-util.c
src/test/test-pretty-print.c
src/test/test-recovery-key.c
src/test/test-user-util.c

index 644417f7c5974abdbabd4ab0a650d3707e6987e9..85f9463f9b5c5f1a2bcc69c4e6d8eabe4c2244a7 100644 (file)
@@ -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();                                                                                \
+                }                                                                                               \
         })
index 5daa0e64f6542d40a24882a5fadf39601de71402..cf4fca27e3f7aef5b55cf9658004d449eddff689 100644 (file)
@@ -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);
 }
 
index 1b788b191c0b76cf5c6106d095e38e29aa137d00..18c81adbcdbfbdb9df3a792be946561f11c10597 100644 (file)
@@ -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",
index 6d784a9ae83cc422d810a28ca5017e1376853346..affcaccc007fa8f961cb76f5808eb47565b792dd 100644 (file)
@@ -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");
index 84e55e175445bfc63cbb5b5354d7ef5f4484f0b5..e51ab9d29086bb28f7543e3f88e1bd683962cbeb 100644 (file)
 #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);
index ca11bf3a29c854073bc031f1e9dd9d3c9fde252e..ceff01967aa0a408d44e682fc02f62b0dac4e2bc 100644 (file)
@@ -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();
index 52b2bc861e8a8e8b6bf4a89e23acdf0709295621..9ab52c60129f168f9a4f6e5bdcee7dbf05fb63ef 100644 (file)
@@ -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) {
index 909ef4ccf265119f90d8b03e6ed4608cff13f7d5..2e901dc9bcfff46132f5288f3dd9a7715da789df 100644 (file)
@@ -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);
index db76cde0a263c4b301b6cff11d64f35d595b8b52..f115a477738e67f5b7712cb640be6e272a8b26c0 100644 (file)
@@ -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);