test: Host-Testumgebung mit Unity, SHA256 und CRC
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
project(ninlight_host_tests C)
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||
|
||||
include(FetchContent)
|
||||
FetchContent_Declare(unity
|
||||
GIT_REPOSITORY https://github.com/ThrowTheSwitch/Unity.git
|
||||
GIT_TAG v2.6.0)
|
||||
FetchContent_MakeAvailable(unity)
|
||||
|
||||
enable_testing()
|
||||
set(NL_SRC ${CMAKE_CURRENT_SOURCE_DIR}/../src)
|
||||
|
||||
function(nl_test name)
|
||||
add_executable(${name} ${ARGN})
|
||||
target_include_directories(${name} PRIVATE ${NL_SRC} ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
target_compile_options(${name} PRIVATE -Wall -Wextra -Werror)
|
||||
target_compile_definitions(${name} PRIVATE NINLIGHT_HOST_TEST=1)
|
||||
target_link_libraries(${name} PRIVATE unity m)
|
||||
add_test(NAME ${name} COMMAND ${name})
|
||||
endfunction()
|
||||
|
||||
nl_test(test_sha256 test_sha256.c ${NL_SRC}/util/nl_sha256.c)
|
||||
nl_test(test_crc test_crc.c ${NL_SRC}/util/nl_crc.c)
|
||||
@@ -0,0 +1,36 @@
|
||||
#include "unity.h"
|
||||
#include "util/nl_crc.h"
|
||||
|
||||
void setUp(void) {}
|
||||
void tearDown(void) {}
|
||||
|
||||
static void test_crc32_check_value(void)
|
||||
{
|
||||
TEST_ASSERT_EQUAL_HEX32(0xCBF43926u, nl_crc32(0, "123456789", 9));
|
||||
}
|
||||
|
||||
static void test_crc32_empty_is_zero(void)
|
||||
{
|
||||
TEST_ASSERT_EQUAL_HEX32(0u, nl_crc32(0, "", 0));
|
||||
}
|
||||
|
||||
static void test_crc32_chaining(void)
|
||||
{
|
||||
uint32_t c = nl_crc32(0, "1234", 4);
|
||||
TEST_ASSERT_EQUAL_HEX32(0xCBF43926u, nl_crc32(c, "56789", 5));
|
||||
}
|
||||
|
||||
static void test_crc8_check_value(void)
|
||||
{
|
||||
TEST_ASSERT_EQUAL_HEX8(0xF4, nl_crc8("123456789", 9));
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
UNITY_BEGIN();
|
||||
RUN_TEST(test_crc32_check_value);
|
||||
RUN_TEST(test_crc32_empty_is_zero);
|
||||
RUN_TEST(test_crc32_chaining);
|
||||
RUN_TEST(test_crc8_check_value);
|
||||
return UNITY_END();
|
||||
}
|
||||
@@ -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