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