test: Host-Testumgebung mit Unity, SHA256 und CRC
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "unity.h"
|
||||
#include "util/nl_sha256.h"
|
||||
|
||||
void setUp(void) {}
|
||||
void tearDown(void) {}
|
||||
|
||||
static void digest_hex(const void *msg, size_t len, char out[65])
|
||||
{
|
||||
uint8_t d[32];
|
||||
nl_sha256_ctx c;
|
||||
nl_sha256_init(&c);
|
||||
nl_sha256_update(&c, msg, len);
|
||||
nl_sha256_final(&c, d);
|
||||
for (int i = 0; i < 32; i++)
|
||||
sprintf(out + 2 * i, "%02x", d[i]);
|
||||
}
|
||||
|
||||
static void test_empty(void)
|
||||
{
|
||||
char h[65];
|
||||
digest_hex("", 0, h);
|
||||
TEST_ASSERT_EQUAL_STRING("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", h);
|
||||
}
|
||||
|
||||
static void test_abc(void)
|
||||
{
|
||||
char h[65];
|
||||
digest_hex("abc", 3, h);
|
||||
TEST_ASSERT_EQUAL_STRING("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", h);
|
||||
}
|
||||
|
||||
static void test_two_blocks(void)
|
||||
{
|
||||
const char *m = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
|
||||
char h[65];
|
||||
digest_hex(m, strlen(m), h);
|
||||
TEST_ASSERT_EQUAL_STRING("248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1", h);
|
||||
}
|
||||
|
||||
static void test_million_a(void)
|
||||
{
|
||||
uint8_t d[32];
|
||||
char h[65];
|
||||
nl_sha256_ctx c;
|
||||
nl_sha256_init(&c);
|
||||
for (int i = 0; i < 1000000; i++)
|
||||
nl_sha256_update(&c, "a", 1);
|
||||
nl_sha256_final(&c, d);
|
||||
for (int i = 0; i < 32; i++)
|
||||
sprintf(h + 2 * i, "%02x", d[i]);
|
||||
TEST_ASSERT_EQUAL_STRING("cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0", h);
|
||||
}
|
||||
|
||||
static void test_streaming_equals_oneshot(void)
|
||||
{
|
||||
uint8_t buf[1000], a[32], b[32];
|
||||
nl_sha256_ctx c;
|
||||
for (int i = 0; i < 1000; i++)
|
||||
buf[i] = (uint8_t)(i * 7);
|
||||
nl_sha256_init(&c);
|
||||
nl_sha256_update(&c, buf, sizeof buf);
|
||||
nl_sha256_final(&c, a);
|
||||
nl_sha256_init(&c);
|
||||
for (size_t off = 0; off < sizeof buf; off += 37) {
|
||||
size_t n = sizeof buf - off < 37 ? sizeof buf - off : 37;
|
||||
nl_sha256_update(&c, buf + off, n);
|
||||
}
|
||||
nl_sha256_final(&c, b);
|
||||
TEST_ASSERT_EQUAL_UINT8_ARRAY(a, b, 32);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
UNITY_BEGIN();
|
||||
RUN_TEST(test_empty);
|
||||
RUN_TEST(test_abc);
|
||||
RUN_TEST(test_two_blocks);
|
||||
RUN_TEST(test_million_a);
|
||||
RUN_TEST(test_streaming_equals_oneshot);
|
||||
return UNITY_END();
|
||||
}
|
||||
Reference in New Issue
Block a user