]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: add test for sha256
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 2 Jun 2022 05:12:20 +0000 (14:12 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 2 Jun 2022 05:14:22 +0000 (14:14 +0900)
src/test/meson.build
src/test/test-sha256.c [new file with mode: 0644]

index d9304d538b0848b485410172b7f41309c7b32cd8..6a467ea5672d37ee07d12f4dd9a81a39a2463aae 100644 (file)
@@ -668,6 +668,8 @@ tests += [
          [], [], [], 'ENABLE_NSCD', 'manual'],
 
         [files('test-hmac.c')],
+
+        [files('test-sha256.c')],
 ]
 
 ############################################################
diff --git a/src/test/test-sha256.c b/src/test/test-sha256.c
new file mode 100644 (file)
index 0000000..f168e4c
--- /dev/null
@@ -0,0 +1,50 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "hexdecoct.h"
+#include "sha256.h"
+#include "string-util.h"
+#include "tests.h"
+
+static void sha256_process_string(const char *key, struct sha256_ctx *ctx) {
+        sha256_process_bytes(key, strlen(key), ctx);
+}
+
+static void test_sha256_one(const char *key, const char *expect) {
+        uint8_t result[SHA256_DIGEST_SIZE + 3];
+        _cleanup_free_ char *str = NULL;
+        struct sha256_ctx ctx;
+
+        log_debug("\"%s\" → %s", key, expect);
+
+        assert_se(str = new(char, strlen(key) + 4));
+
+        /* This tests unaligned buffers. */
+
+        for (size_t i = 0; i < 4; i++) {
+                strcpy(str + i, key);
+
+                for (size_t j = 0; j < 4; j++) {
+                        _cleanup_free_ char *hex_result = NULL;
+
+                        sha256_init_ctx(&ctx);
+                        sha256_process_string(str + i, &ctx);
+                        sha256_finish_ctx(&ctx, result + j);
+
+                        hex_result = hexmem(result + j, SHA256_DIGEST_SIZE);
+                        assert_se(streq_ptr(hex_result, expect));
+                }
+        }
+}
+
+TEST(sha256) {
+        /* Results compared with output of 'echo -n "<input>" | sha256sum -' */
+
+        test_sha256_one("abcdefghijklmnopqrstuvwxyz",
+                        "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73");
+        test_sha256_one("ほげほげあっちょんぶりけ",
+                        "ce7225683653be3b74861c5a4323b6baf3c3ceb361413ca99e3a5b52c04411bd");
+        test_sha256_one("0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789",
+                        "9cfe7faff7054298ca87557e15a10262de8d3eee77827417fbdfea1c41b9ec23");
+}
+
+DEFINE_TEST_MAIN(LOG_INFO);