Skip to content

Commit c2f9452

Browse files
committed
Add native_sim tests for persist_state and supervisor
1 parent e1949cf commit c2f9452

File tree

7 files changed

+188
-0
lines changed

7 files changed

+188
-0
lines changed

tests/persist_state/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
cmake_minimum_required(VERSION 3.20.0)
2+
set(APP_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
3+
set(KCONFIG_ROOT ${APP_ROOT}/Kconfig)
4+
set(BOARD_ROOT ${APP_ROOT})
5+
6+
set(DTC_OVERLAY_FILE ${APP_ROOT}/tests/common/native.overlay)
7+
8+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
9+
project(persist_state_tests)
10+
11+
target_sources(app PRIVATE
12+
${APP_ROOT}/src/app_crypto.c
13+
${APP_ROOT}/src/simple_aes.c
14+
${APP_ROOT}/src/persist_state.c
15+
src/main.c
16+
)
17+
18+
target_include_directories(app PRIVATE ${APP_ROOT}/src)

tests/persist_state/src/main.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <string.h>
2+
3+
#include <zephyr/ztest.h>
4+
5+
#include "app_crypto.h"
6+
#include "persist_state_priv.h"
7+
#include "persist_state_test.h"
8+
9+
static void *persist_state_suite_setup(void)
10+
{
11+
int rc = app_crypto_init();
12+
zassert_ok(rc, "AES helper init failed (%d)", rc);
13+
return NULL;
14+
}
15+
16+
ZTEST(persist_state_suite, test_encrypt_decrypt_round_trip)
17+
{
18+
struct persist_blob original;
19+
struct persist_blob decoded;
20+
struct persist_blob_encrypted storage = {0};
21+
size_t cipher_len = 0U;
22+
23+
persist_state_test_init_blob(&original, 3U, 12U, 2500U);
24+
25+
int rc = persist_state_test_encrypt_blob(&original, &storage, &cipher_len);
26+
zassert_ok(rc, "encrypt failed (%d)", rc);
27+
zassert_equal(cipher_len, sizeof(storage.data), "cipher length mismatch");
28+
29+
rc = persist_state_test_decrypt_blob(&storage, &decoded);
30+
zassert_ok(rc, "decrypt failed (%d)", rc);
31+
zassert_equal(decoded.consecutive_watchdog, original.consecutive_watchdog, NULL);
32+
zassert_equal(decoded.total_watchdog, original.total_watchdog, NULL);
33+
zassert_equal(decoded.watchdog_override_ms, original.watchdog_override_ms, NULL);
34+
}
35+
36+
ZTEST(persist_state_suite, test_plain_copy_helper)
37+
{
38+
struct persist_blob baseline;
39+
struct persist_blob copied;
40+
41+
persist_state_test_init_blob(&baseline, 9U, 42U, 0U);
42+
persist_state_test_copy_plain(&copied, &baseline);
43+
44+
zassert_equal(memcmp(&copied, &baseline, sizeof(baseline)), 0, "blob copy mismatch");
45+
}
46+
47+
ZTEST_SUITE(persist_state_suite, NULL, persist_state_suite_setup, NULL, NULL, NULL);

tests/persist_state/testcase.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
tests:
2+
zephyr_secure_supervisor.persist_state:
3+
platform_allow:
4+
- native_sim
5+
tags:
6+
- persist_state

tests/supervisor/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
cmake_minimum_required(VERSION 3.20.0)
2+
set(APP_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
3+
set(KCONFIG_ROOT ${APP_ROOT}/Kconfig)
4+
set(BOARD_ROOT ${APP_ROOT})
5+
6+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
7+
project(supervisor_tests)
8+
9+
target_sources(app PRIVATE
10+
${APP_ROOT}/src/supervisor.c
11+
src/main.c
12+
src/stubs.c
13+
)
14+
15+
target_include_directories(app PRIVATE ${APP_ROOT}/src)

tests/supervisor/src/main.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <zephyr/ztest.h>
2+
3+
#include "supervisor_test.h"
4+
5+
static void supervisor_fixture_reset(void)
6+
{
7+
supervisor_test_set_last_seen(0U, 0U);
8+
}
9+
10+
ZTEST(supervisor_suite, test_led_and_hb_fresh)
11+
{
12+
uint32_t now = 2000U;
13+
supervisor_test_set_last_seen(now - 100U, now - 150U);
14+
15+
struct supervisor_health_snapshot snapshot = supervisor_test_sample(true, now);
16+
zassert_true(snapshot.led_ok, "LED should be considered fresh");
17+
zassert_true(snapshot.hb_ok, "Heartbeat should be considered fresh");
18+
}
19+
20+
ZTEST(supervisor_suite, test_led_stale_when_monitored)
21+
{
22+
uint32_t now = 10000U;
23+
supervisor_test_set_last_seen(0U, now - 100U);
24+
25+
struct supervisor_health_snapshot snapshot = supervisor_test_sample(true, now);
26+
zassert_false(snapshot.led_ok, "LED should be stale when monitoring is enabled");
27+
zassert_true(snapshot.hb_ok, "Heartbeat remains fresh");
28+
}
29+
30+
ZTEST(supervisor_suite, test_led_ignored_when_not_monitored)
31+
{
32+
uint32_t now = 5000U;
33+
supervisor_test_set_last_seen(0U, now - 200U);
34+
35+
struct supervisor_health_snapshot snapshot = supervisor_test_sample(false, now);
36+
zassert_true(snapshot.led_ok, "LED should be ignored when not monitored");
37+
zassert_true(snapshot.hb_ok, "Heartbeat still fresh");
38+
}
39+
40+
ZTEST(supervisor_suite, test_heartbeat_stale)
41+
{
42+
uint32_t now = 9000U;
43+
supervisor_test_set_last_seen(now - 100U, 0U);
44+
45+
struct supervisor_health_snapshot snapshot = supervisor_test_sample(true, now);
46+
zassert_false(snapshot.hb_ok, "Heartbeat should be stale");
47+
zassert_true(snapshot.led_ok, "LED is still healthy");
48+
}
49+
50+
ZTEST_SUITE(supervisor_suite, NULL, NULL, supervisor_fixture_reset, supervisor_fixture_reset, NULL);

tests/supervisor/src/stubs.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <zephyr/sys/util.h>
2+
3+
#include "persist_state.h"
4+
#include "recovery.h"
5+
#include "watchdog_ctrl.h"
6+
7+
int watchdog_ctrl_init(uint32_t timeout_ms)
8+
{
9+
ARG_UNUSED(timeout_ms);
10+
return 0;
11+
}
12+
13+
int watchdog_ctrl_feed(void)
14+
{
15+
return 0;
16+
}
17+
18+
void watchdog_ctrl_set_enabled(bool enable)
19+
{
20+
ARG_UNUSED(enable);
21+
}
22+
23+
bool watchdog_ctrl_is_enabled(void)
24+
{
25+
return true;
26+
}
27+
28+
int watchdog_ctrl_retune(uint32_t timeout_ms)
29+
{
30+
ARG_UNUSED(timeout_ms);
31+
return 0;
32+
}
33+
34+
uint32_t watchdog_ctrl_get_timeout(void)
35+
{
36+
return 0U;
37+
}
38+
39+
void persist_state_clear_watchdog_counter(void)
40+
{
41+
}
42+
43+
void recovery_request(enum recovery_reason reason)
44+
{
45+
ARG_UNUSED(reason);
46+
}

tests/supervisor/testcase.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
tests:
2+
zephyr_secure_supervisor.supervisor:
3+
platform_allow:
4+
- native_sim
5+
tags:
6+
- supervisor

0 commit comments

Comments
 (0)