]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: Add tests for SHA1 34722/head
authoranonymix007 <48598263+anonymix007@users.noreply.github.com>
Fri, 11 Oct 2024 13:30:43 +0000 (16:30 +0300)
committeranonymix007 <48598263+anonymix007@users.noreply.github.com>
Fri, 11 Oct 2024 20:10:23 +0000 (23:10 +0300)
src/test/meson.build
src/test/test-sha1.c [new file with mode: 0644]

index b43bd5d8228e0d86f2ae2912c6b015f859f2bc8f..b8699a016bfc239e8664a0b5b07d99ef5910ff9e 100644 (file)
@@ -158,6 +158,7 @@ simple_tests += files(
         'test-secure-bits.c',
         'test-serialize.c',
         'test-set.c',
+        'test-sha1.c',
         'test-sha256.c',
         'test-sigbus.c',
         'test-signal-util.c',
diff --git a/src/test/test-sha1.c b/src/test/test-sha1.c
new file mode 100644 (file)
index 0000000..bddc7ac
--- /dev/null
@@ -0,0 +1,58 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "hexdecoct.h"
+#include "sha1-fundamental.h"
+#include "string-util.h"
+#include "tests.h"
+
+static void sha1_process_string(const char *key, struct sha1_ctx *ctx) {
+        sha1_process_bytes(key, strlen(key), ctx);
+}
+
+static void test_sha1_one(const char *key, const char *expect) {
+        uint8_t result[SHA1_DIGEST_SIZE + 3];
+        _cleanup_free_ char *str = NULL;
+        struct sha1_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;
+
+                        sha1_init_ctx(&ctx);
+                        sha1_process_string(str + i, &ctx);
+                        sha1_finish_ctx(&ctx, result + j);
+
+                        hex_result = hexmem(result + j, SHA1_DIGEST_SIZE);
+                        ASSERT_STREQ(hex_result, expect);
+                }
+        }
+}
+
+/* From https://datatracker.ietf.org/doc/html/rfc3174#section-7.3 */
+#define TEST1   "abc"
+#define RESULT1 "a9993e364706816aba3e25717850c26c9cd0d89d"
+#define TEST2   "abcdbcdecdefdefgefghfghighijhi" "jkijkljklmklmnlmnomnopnopq"
+#define RESULT2 "84983e441c3bd26ebaae4aa1f95129e5e54670f1"
+#define TEST3   "a"
+#define RESULT3 "86f7e437faa5a7fce15d1ddcb9eaeaea377667b8"
+#define TEST4   "01234567012345670123456701234567" "01234567012345670123456701234567"
+#define RESULT4 "e0c094e867ef46c350ef54a7f59dd60bed92ae83"
+
+TEST(sha1) {
+        /* Results compared with output of 'echo -n "<input>" | sha1sum -' */
+
+        test_sha1_one(TEST1, RESULT1);
+        test_sha1_one(TEST2, RESULT2);
+        test_sha1_one(TEST3, RESULT3);
+        test_sha1_one(TEST4, RESULT4);
+}
+
+DEFINE_TEST_MAIN(LOG_INFO);