feat: OTA-Trailer bauen und prüfen

This commit is contained in:
Michael Sauer
2026-09-12 01:16:04 +02:00
parent c96e0442c0
commit e3be3e84c1
5 changed files with 199 additions and 0 deletions
+95
View File
@@ -0,0 +1,95 @@
#include <string.h>
#include "unity.h"
#include "util/nl_fwup_trailer.h"
void setUp(void) {}
void tearDown(void) {}
static uint8_t HASH[32];
static uint8_t tail[NL_TRAILER_SIZE];
static void fill_hash(void)
{
for (int i = 0; i < 32; i++)
HASH[i] = (uint8_t)(i * 3 + 1);
}
static void test_build_layout(void)
{
fill_hash();
TEST_ASSERT_TRUE(nl_trailer_build(tail, "e27_rgb", "1.2.3", HASH));
TEST_ASSERT_EQUAL_MEMORY("NINL", tail, 4);
TEST_ASSERT_EQUAL_UINT8(NL_TRAILER_VERSION, tail[4]);
TEST_ASSERT_EQUAL_STRING("e27_rgb", (const char *)&tail[5]);
TEST_ASSERT_EQUAL_STRING("1.2.3", (const char *)&tail[21]);
TEST_ASSERT_EQUAL_UINT8_ARRAY(HASH, &tail[37], 32);
for (uint32_t i = 69; i < NL_TRAILER_SIZE; i++)
TEST_ASSERT_EQUAL_HEX8(0xFF, tail[i]);
}
static void test_build_rejects_too_long_strings(void)
{
fill_hash();
TEST_ASSERT_FALSE(nl_trailer_build(tail, "0123456789abcdef", "1.0", HASH)); /* 16 chars, no room for NUL */
TEST_ASSERT_FALSE(nl_trailer_build(tail, "evk", "0123456789abcdef", HASH));
TEST_ASSERT_TRUE(nl_trailer_build(tail, "012345678901234", "012345678901234", HASH)); /* 15 chars fit */
}
static void test_verify_accepts_matching(void)
{
fill_hash();
nl_trailer_build(tail, "evk", "1.0.0", HASH);
TEST_ASSERT_EQUAL(NL_TRAILER_OK, nl_trailer_verify(tail, NL_TRAILER_SIZE, "evk", HASH));
}
static void test_verify_rejects(void)
{
char version[NL_TRAILER_VERSION_LEN];
uint8_t other[32];
fill_hash();
memcpy(other, HASH, 32);
other[5] ^= 0x01;
nl_trailer_build(tail, "evk", "1.0.0", HASH);
TEST_ASSERT_EQUAL(NL_TRAILER_ERR_SIZE, nl_trailer_verify(tail, 256, "evk", HASH));
TEST_ASSERT_EQUAL(NL_TRAILER_ERR_DEVICE, nl_trailer_verify(tail, NL_TRAILER_SIZE, "gu10_cw", HASH));
TEST_ASSERT_EQUAL(NL_TRAILER_ERR_HASH, nl_trailer_verify(tail, NL_TRAILER_SIZE, "evk", other));
nl_trailer_version(tail, version);
TEST_ASSERT_EQUAL_STRING("1.0.0", version);
tail[4] = 2;
TEST_ASSERT_EQUAL(NL_TRAILER_ERR_VERSION, nl_trailer_verify(tail, NL_TRAILER_SIZE, "evk", HASH));
tail[0] = 'X';
TEST_ASSERT_EQUAL(NL_TRAILER_ERR_MAGIC, nl_trailer_verify(tail, NL_TRAILER_SIZE, "evk", HASH));
}
static void test_verify_needs_terminated_device_string(void)
{
fill_hash();
nl_trailer_build(tail, "evk", "1.0.0", HASH);
memset(&tail[5], 'a', NL_TRAILER_DEVICE_LEN); /* no NUL inside the field */
TEST_ASSERT_EQUAL(NL_TRAILER_ERR_DEVICE, nl_trailer_verify(tail, NL_TRAILER_SIZE, "aaaaaaaaaaaaaaaa", HASH));
}
static void test_error_names(void)
{
TEST_ASSERT_EQUAL_STRING("trailer", nl_trailer_err_name(NL_TRAILER_ERR_MAGIC));
TEST_ASSERT_EQUAL_STRING("trailer", nl_trailer_err_name(NL_TRAILER_ERR_SIZE));
TEST_ASSERT_EQUAL_STRING("trailer", nl_trailer_err_name(NL_TRAILER_ERR_VERSION));
TEST_ASSERT_EQUAL_STRING("device_mismatch", nl_trailer_err_name(NL_TRAILER_ERR_DEVICE));
TEST_ASSERT_EQUAL_STRING("hash", nl_trailer_err_name(NL_TRAILER_ERR_HASH));
TEST_ASSERT_NULL(nl_trailer_err_name(NL_TRAILER_OK));
}
int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_build_layout);
RUN_TEST(test_build_rejects_too_long_strings);
RUN_TEST(test_verify_accepts_matching);
RUN_TEST(test_verify_rejects);
RUN_TEST(test_verify_needs_terminated_device_string);
RUN_TEST(test_error_names);
return UNITY_END();
}