From 6016b4b0ba6a5d47533b47f7f944116425594f41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 8 Sep 2020 15:28:21 +0200 Subject: [PATCH] Add reciprocal test for password hashing and checking --- src/test/test-libcrypt-util.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/test/test-libcrypt-util.c b/src/test/test-libcrypt-util.c index 94bd4172ea..5c4958ed72 100644 --- a/src/test/test-libcrypt-util.c +++ b/src/test/test-libcrypt-util.c @@ -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); } }