]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-sha256.c
ASSERT_STREQ for simple cases
[thirdparty/systemd.git] / src / test / test-sha256.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "hexdecoct.h"
4 #include "sha256.h"
5 #include "string-util.h"
6 #include "tests.h"
7
8 static void sha256_process_string(const char *key, struct sha256_ctx *ctx) {
9 sha256_process_bytes(key, strlen(key), ctx);
10 }
11
12 static void test_sha256_one(const char *key, const char *expect) {
13 uint8_t result[SHA256_DIGEST_SIZE + 3];
14 _cleanup_free_ char *str = NULL;
15 struct sha256_ctx ctx;
16
17 log_debug("\"%s\" → %s", key, expect);
18
19 assert_se(str = new(char, strlen(key) + 4));
20
21 /* This tests unaligned buffers. */
22
23 for (size_t i = 0; i < 4; i++) {
24 strcpy(str + i, key);
25
26 for (size_t j = 0; j < 4; j++) {
27 _cleanup_free_ char *hex_result = NULL;
28
29 sha256_init_ctx(&ctx);
30 sha256_process_string(str + i, &ctx);
31 sha256_finish_ctx(&ctx, result + j);
32
33 hex_result = hexmem(result + j, SHA256_DIGEST_SIZE);
34 ASSERT_STREQ(hex_result, expect);
35 }
36 }
37 }
38
39 TEST(sha256) {
40 /* Results compared with output of 'echo -n "<input>" | sha256sum -' */
41
42 test_sha256_one("abcdefghijklmnopqrstuvwxyz",
43 "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73");
44 test_sha256_one("ほげほげあっちょんぶりけ",
45 "ce7225683653be3b74861c5a4323b6baf3c3ceb361413ca99e3a5b52c04411bd");
46 test_sha256_one("0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789",
47 "9cfe7faff7054298ca87557e15a10262de8d3eee77827417fbdfea1c41b9ec23");
48 }
49
50 DEFINE_TEST_MAIN(LOG_INFO);