]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test-libcrypt-util: use DEFINE_TEST_MAIN() and ASSERT_XYZ()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 17 Aug 2025 15:13:27 +0000 (00:13 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 2 Jan 2026 03:54:01 +0000 (12:54 +0900)
Also, tests for make_salt() in test-user-util.c are moved to
test-libcrypt-util.c.

src/test/test-libcrypt-util.c
src/test/test-user-util.c

index f4968d91deff69b16b84bbf5319e12a70410afb2..9f171a2415a29fa786bcd4726a1b07c22ebde37b 100644 (file)
@@ -6,9 +6,7 @@
 #include "strv.h"
 #include "tests.h"
 
-static void test_crypt_preferred_method(void) {
-        log_info("/* %s */", __func__);
-
+TEST(crypt_preferred_method) {
         log_info("crypt_preferred_method: %s",
 #if HAVE_CRYPT_PREFERRED_METHOD
                  crypt_preferred_method()
@@ -18,24 +16,26 @@ static void test_crypt_preferred_method(void) {
         );
 }
 
-static void test_make_salt(void) {
-        log_info("/* %s */", __func__);
+TEST(make_salt) {
+        _cleanup_strv_free_ char **l = NULL;
 
         for (int i = 0; i < 10; i++) {
                 _cleanup_free_ char *t;
 
-                assert_se(make_salt(&t) == 0);
+                ASSERT_OK(make_salt(&t));
                 log_info("%s", t);
+
+                ASSERT_FALSE(strv_contains(l, t));
+                ASSERT_OK(strv_consume(&l, TAKE_PTR(t)));
         }
 }
 
-static int test_hash_password(void) {
-        log_info("/* %s */", __func__);
+TEST(hash_password) {
+#if defined(__powerpc__) && !defined(XCRYPT_VERSION_MAJOR)
+        return log_tests_skipped("crypt_r() causes a buffer overflow on ppc64el, see https://github.com/systemd/systemd/pull/16981#issuecomment-691203787");
+#endif
 
         /* As a warm-up exercise, check if we can hash passwords. */
-
-        bool have_sane_hash = false;
-
         FOREACH_STRING(hash,
                        "ew3bU1.hoKk4o",
                        "$1$gc5rWpTB$wK1aul1PyBn9AX1z93stk1",
@@ -43,72 +43,49 @@ static int test_hash_password(void) {
                        "$5$lGhDrcrao9zb5oIK$05KlOVG3ocknx/ThreqXE/gk.XzFFBMTksc4t2CPDUD",
                        "$6$c7wB/3GiRk0VHf7e$zXJ7hN0aLZapE.iO4mn/oHu6.prsXTUG/5k1AxpgR85ELolyAcaIGRgzfwJs3isTChMDBjnthZyaMCfCNxo9I.",
                        "$y$j9T$$9cKOWsAm4m97WiYk61lPPibZpy3oaGPIbsL4koRe/XD") {
-                int b;
 
-                b = test_password_one(hash, "ppp");
-                log_info("%s: %s", hash, yes_no(b));
-#if defined(XCRYPT_VERSION_MAJOR)
-                /* xcrypt is supposed to always implement all methods. */
-                assert_se(b);
+#ifndef __GLIBC__
+                /* musl does not support yescrypt. */
+                if (hash[1] == 'y') {
+                        ASSERT_OK_ZERO(test_password_one(hash, "ppp"));
+                        continue;
+                }
+#elif !defined(XCRYPT_VERSION_MAJOR)
+                ASSERT_OK(test_password_one(hash, "ppp"));
+                continue;
 #endif
-
-                if (b && IN_SET(hash[1], '6', 'y'))
-                        have_sane_hash = true;
+                ASSERT_OK_POSITIVE(test_password_one(hash, "ppp"));
         }
 
-        return have_sane_hash;
-}
-
-static void test_hash_password_full(void) {
-        log_info("/* %s */", __func__);
-
-        log_info("sizeof(struct crypt_data): %zu bytes", sizeof(struct crypt_data));
-
         FOREACH_STRING(i, "abc123", "h⸿sło") {
                 _cleanup_free_ char *hashed;
 
-                assert_se(hash_password(i, &hashed) == 0);
+                ASSERT_OK(hash_password(i, &hashed));
                 log_debug("\"%s\" → \"%s\"", i, hashed);
 
-                assert_se(test_password_one(hashed, i) == true);
-                assert_se(test_password_one(i, hashed) <= 0); /* We get an error for non-utf8 */
-                assert_se(test_password_one(hashed, "foobar") == false);
-                assert_se(test_password_many(STRV_MAKE(hashed), i) == true);
-                assert_se(test_password_many(STRV_MAKE(hashed), "foobar") == false);
-                assert_se(test_password_many(STRV_MAKE(hashed, hashed, hashed), "foobar") == false);
-                assert_se(test_password_many(STRV_MAKE("$y$j9T$dlCXwkX0GC5L6B8Gf.4PN/$VCyEH",
-                                                       hashed,
-                                                       "$y$j9T$SAayASazWZIQeJd9AS02m/$"),
-                                             i) == true);
-                assert_se(test_password_many(STRV_MAKE("$W$j9T$dlCXwkX0GC5L6B8Gf.4PN/$VCyEH", /* no such method exists... */
-                                                       hashed,
-                                                       "$y$j9T$SAayASazWZIQeJd9AS02m/$"),
-                                             i) == true);
-                assert_se(test_password_many(STRV_MAKE("$y$j9T$dlCXwkX0GC5L6B8Gf.4PN/$VCyEH",
-                                                       hashed,
-                                                       "$y$j9T$SAayASazWZIQeJd9AS02m/$"),
-                                             "") == false);
-                assert_se(test_password_many(STRV_MAKE("$W$j9T$dlCXwkX0GC5L6B8Gf.4PN/$VCyEH", /* no such method exists... */
-                                                       hashed,
-                                                       "$y$j9T$SAayASazWZIQeJd9AS02m/$"),
-                                             "") == false);
-        }
+                ASSERT_OK_POSITIVE(test_password_one(hashed, i));
+                ASSERT_LE(test_password_one(i, hashed), 0); /* We get an error for non-utf8 */
+                ASSERT_OK_ZERO(test_password_one(hashed, "foobar"));
+                ASSERT_OK_POSITIVE(test_password_many(STRV_MAKE(hashed), i));
+                ASSERT_OK_ZERO(test_password_many(STRV_MAKE(hashed), "foobar"));
+                ASSERT_OK_ZERO(test_password_many(STRV_MAKE(hashed, hashed, hashed), "foobar"));
+                ASSERT_OK_POSITIVE(test_password_many(STRV_MAKE("$y$j9T$dlCXwkX0GC5L6B8Gf.4PN/$VCyEH",
+                                                                hashed,
+                                                                "$y$j9T$SAayASazWZIQeJd9AS02m/$"),
+                                                      i));
+                ASSERT_OK_POSITIVE(test_password_many(STRV_MAKE("$W$j9T$dlCXwkX0GC5L6B8Gf.4PN/$VCyEH", /* no such method exists... */
+                                                                hashed,
+                                                                "$y$j9T$SAayASazWZIQeJd9AS02m/$"),
+                                                      i));
+                ASSERT_OK_ZERO(test_password_many(STRV_MAKE("$y$j9T$dlCXwkX0GC5L6B8Gf.4PN/$VCyEH",
+                                                            hashed,
+                                                            "$y$j9T$SAayASazWZIQeJd9AS02m/$"),
+                                                  ""));
+                ASSERT_OK_ZERO(test_password_many(STRV_MAKE("$W$j9T$dlCXwkX0GC5L6B8Gf.4PN/$VCyEH", /* no such method exists... */
+                                                            hashed,
+                                                            "$y$j9T$SAayASazWZIQeJd9AS02m/$"),
+                                                  ""));
+                }
 }
 
-int main(int argc, char *argv[]) {
-        test_setup_logging(LOG_DEBUG);
-
-#if defined(__powerpc__) && !defined(XCRYPT_VERSION_MAJOR)
-        return log_tests_skipped("crypt_r() causes a buffer overflow on ppc64el, see https://github.com/systemd/systemd/pull/16981#issuecomment-691203787");
-#endif
-
-        test_crypt_preferred_method();
-        test_make_salt();
-
-        if (!test_hash_password())
-                return log_tests_skipped("crypt doesn't support yescrypt or sha512crypt");
-
-        test_hash_password_full();
-
-        return 0;
-}
+DEFINE_TEST_MAIN(LOG_DEBUG);
index d822df61fb135c8b003f431bfda348f6f3b3e1f3..47bd01f9d0f80099952529d72536ac2097ca2a9b 100644 (file)
@@ -4,7 +4,6 @@
 
 #include "alloc-util.h"
 #include "format-util.h"
-#include "libcrypt-util.h"
 #include "log.h"
 #include "memory-util.h"
 #include "path-util.h"
@@ -342,18 +341,6 @@ TEST(get_group_creds) {
         test_get_group_creds_one("65534", NOBODY_GROUP_NAME, GID_NOBODY);
 }
 
-TEST(make_salt) {
-        _cleanup_free_ char *s, *t;
-
-        ASSERT_OK(make_salt(&s));
-        log_info("got %s", s);
-
-        ASSERT_OK(make_salt(&t));
-        log_info("got %s", t);
-
-        ASSERT_NOT_STREQ(s, t);
-}
-
 TEST(in_gid) {
         ASSERT_OK(in_gid(getgid()));
         ASSERT_OK(in_gid(getegid()));