]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Add reciprocal test for password hashing and checking
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 8 Sep 2020 13:28:21 +0000 (15:28 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 15 Sep 2020 09:52:30 +0000 (11:52 +0200)
src/test/test-libcrypt-util.c

index 94bd4172eab1bcfbb239c51409b8f5c18680e92e..5c4958ed724b4200593d15bd59c0e1b235a35767 100644 (file)
@@ -14,7 +14,7 @@ static void test_hash_password_full(void) {
         log_info("sizeof(struct crypt_data): %zu bytes", sizeof(struct crypt_data));
 
         for (unsigned c = 0; c < 2; c++)
-                FOREACH_STRING(i, "abc123", "password", "s3cr3t") {
+                FOREACH_STRING(i, "abc123", "h⸿sło") {
                         _cleanup_free_ char *hashed;
 
                         if (c == 0)
@@ -23,6 +23,21 @@ static void test_hash_password_full(void) {
                                 assert_se(hash_password_full(i, NULL, NULL, &hashed) == 0);
                         log_debug("\"%s\" → \"%s\"", i, hashed);
                         log_info("crypt_r[a] buffer size: %i bytes", cd_size);
+
+                        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("$y$j9T$dlCXwkX0GC5L6B8Gf.4PN/$VCyEH",
+                                                               hashed,
+                                                               "$y$j9T$SAayASazWZIQeJd9AS02m/$"),
+                                                     "") == false);
                 }
 }