Skip to content

Commit 3ea2951

Browse files
petejohanson-adinashif
authored andcommitted
tests: subsys: secure_storage: Erase flash before write once test
Where possible (i.e. not running in NS), erase the flash before testing the write once secure storage API. Signed-off-by: Pete Johanson <pete.johanson@analog.com>
1 parent 6795a79 commit 3ea2951

File tree

1 file changed

+29
-2
lines changed
  • tests/subsys/secure_storage/psa/its/src

1 file changed

+29
-2
lines changed

tests/subsys/secure_storage/psa/its/src/main.c

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
11
/* Copyright (c) 2024 Nordic Semiconductor
22
* SPDX-License-Identifier: Apache-2.0
33
*/
4+
#include <zephyr/types.h>
45
#include <zephyr/ztest.h>
6+
#include <zephyr/drivers/flash.h>
7+
#include <zephyr/storage/flash_map.h>
58
#include <psa/internal_trusted_storage.h>
69

710
/* The flash must be erased after this test suite is run for the write-once entry test to pass. */
11+
#if !defined(CONFIG_BUILD_WITH_TFM) && defined(CONFIG_FLASH_PAGE_LAYOUT)
12+
static int erase_flash(void)
13+
{
14+
const struct device *const fdev = DEVICE_DT_GET(DT_CHOSEN(zephyr_flash_controller));
15+
int rc;
16+
17+
rc = flash_flatten(fdev, FIXED_PARTITION_OFFSET(storage_partition),
18+
FIXED_PARTITION_SIZE(storage_partition));
19+
if (rc < 0) {
20+
TC_PRINT("Failed to flatten the storage partition (%d) !", rc);
21+
return rc;
22+
}
23+
24+
return 0;
25+
}
26+
27+
/* Low priority to ensure we run after any flash drivers are initialized */
28+
SYS_INIT(erase_flash, POST_KERNEL, 100);
29+
30+
#endif /* !CONFIG_BUILD_WITH_TFM && CONFIG_FLASH_PAGE_LAYOUT */
31+
832
ZTEST_SUITE(secure_storage_psa_its, NULL, NULL, NULL, NULL, NULL);
933

1034
#ifdef CONFIG_SECURE_STORAGE
@@ -119,8 +143,11 @@ ZTEST(secure_storage_psa_its, test_write_once_flag)
119143
struct psa_storage_info_t info;
120144

121145
ret = psa_its_set(uid, sizeof(data), data, PSA_STORAGE_FLAG_WRITE_ONCE);
122-
zassert_equal(ret, PSA_SUCCESS, "%s%d", (ret == PSA_ERROR_NOT_PERMITTED) ?
123-
"Has the flash been erased since this test ran? " : "", ret);
146+
zassert_equal(ret, PSA_SUCCESS, "%s%d",
147+
(ret == PSA_ERROR_NOT_PERMITTED)
148+
? "Has the flash been erased since this test ran? "
149+
: "",
150+
ret);
124151

125152
ret = psa_its_get_info(uid, &info);
126153
zassert_equal(ret, PSA_SUCCESS);

0 commit comments

Comments
 (0)