feat: OTA-Trailer bauen und prüfen
This commit is contained in:
@@ -48,3 +48,4 @@ nl_test(test_api test_api.c fake_api_ctx.c ${NL_SRC}/net/nl_api.c ${NL_SRC}/boar
|
||||
${NL_SRC}/light/nl_boot_state.c ${NL_SRC}/config/nl_settings.c ${NL_SRC}/util/nl_crc.c fake_flash.c)
|
||||
target_link_libraries(test_api PRIVATE nl_cjson)
|
||||
nl_test(test_wifi_sm test_wifi_sm.c ${NL_SRC}/net/nl_wifi_sm.c)
|
||||
nl_test(test_fwup_trailer test_fwup_trailer.c ${NL_SRC}/util/nl_fwup_trailer.c)
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
Reference in New Issue
Block a user