|
| 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); |
0 commit comments