From c10fbc85c59d4b453d11843e5b3a09deaeaf786d Mon Sep 17 00:00:00 2001 From: Dominik Ermel Date: Thu, 7 Nov 2024 17:47:22 +0100 Subject: [PATCH 1/2] tests: flash_map: Reduce flash wear in test_flash_area_get_sectors Remove needless writes/read and erase in flash_area_get_sectors test scenario, by replacing it with comparison with layout directly obtained from device. Signed-off-by: Dominik Ermel --- tests/subsys/storage/flash_map/src/main.c | 62 +++++------------------ 1 file changed, 14 insertions(+), 48 deletions(-) diff --git a/tests/subsys/storage/flash_map/src/main.c b/tests/subsys/storage/flash_map/src/main.c index 555f973d35b96..a4c6a3572ccf5 100644 --- a/tests/subsys/storage/flash_map/src/main.c +++ b/tests/subsys/storage/flash_map/src/main.c @@ -15,6 +15,7 @@ #define SLOT1_PARTITION_ID FIXED_PARTITION_ID(SLOT1_PARTITION) #define SLOT1_PARTITION_DEV FIXED_PARTITION_DEVICE(SLOT1_PARTITION) #define SLOT1_PARTITION_NODE DT_NODELABEL(SLOT1_PARTITION) +#define SLOT1_PARTITION_OFFSET FIXED_PARTITION_OFFSET(SLOT1_PARTITION) extern int flash_map_entries; struct flash_sector fs_sectors[1024]; @@ -41,8 +42,6 @@ ZTEST(flash_map, test_flash_area_get_sectors) int i; int rc; off_t off; - uint8_t wd[512]; - uint8_t rd[512]; const struct device *flash_dev; const struct device *flash_dev_a = SLOT1_PARTITION_DEV; @@ -55,61 +54,28 @@ ZTEST(flash_map, test_flash_area_get_sectors) /* Device obtained by label should match the one from fa object */ zassert_equal(flash_dev, flash_dev_a, "Device for slot1_partition do not match"); - rc = flash_erase(flash_dev, fa->fa_off, fa->fa_size); - zassert_true(rc == 0, "flash area erase fail"); - - (void)memset(wd, 0xa5, sizeof(wd)); - sec_cnt = ARRAY_SIZE(fs_sectors); rc = flash_area_get_sectors(SLOT1_PARTITION_ID, &sec_cnt, fs_sectors); zassert_true(rc == 0, "flash_area_get_sectors failed"); - /* write stuff to beginning of every sector */ off = 0; - for (i = 0; i < sec_cnt; i++) { - rc = flash_area_write(fa, off, wd, sizeof(wd)); - zassert_true(rc == 0, "flash_area_write() fail"); - - /* read it back via hal_flash_Read() */ - rc = flash_read(flash_dev, fa->fa_off + off, rd, sizeof(rd)); - zassert_true(rc == 0, "hal_flash_read() fail"); - - rc = memcmp(wd, rd, sizeof(wd)); - zassert_true(rc == 0, "read data != write data"); - - /* write stuff to end of area */ - rc = flash_write(flash_dev, fa->fa_off + off + - fs_sectors[i].fs_size - sizeof(wd), - wd, sizeof(wd)); - zassert_true(rc == 0, "hal_flash_write() fail"); - - /* and read it back */ - (void)memset(rd, 0, sizeof(rd)); - rc = flash_area_read(fa, off + fs_sectors[i].fs_size - - sizeof(rd), - rd, sizeof(rd)); - zassert_true(rc == 0, "hal_flash_read() fail"); - - rc = memcmp(wd, rd, sizeof(rd)); - zassert_true(rc == 0, "read data != write data"); + /* For each reported sector, check if it corresponds to real page on device */ + for (i = 0; i < sec_cnt; ++i) { + struct flash_pages_info fpi; + + zassert_ok(flash_get_page_info_by_offs(flash_dev, + SLOT1_PARTITION_OFFSET + off, + &fpi)); + /* Offset of page taken directly from device corresponds to offset + * within flash area + */ + zassert_equal(fpi.start_offset, + fs_sectors[i].fs_off + SLOT1_PARTITION_OFFSET); + zassert_equal(fpi.size, fs_sectors[i].fs_size); off += fs_sectors[i].fs_size; } - /* erase it */ - rc = flash_area_erase(fa, 0, fa->fa_size); - zassert_true(rc == 0, "read data != write data"); - - /* should read back ff all throughout*/ - (void)memset(wd, 0xff, sizeof(wd)); - for (off = 0; off < fa->fa_size; off += sizeof(rd)) { - rc = flash_area_read(fa, off, rd, sizeof(rd)); - zassert_true(rc == 0, "hal_flash_read() fail"); - - rc = memcmp(wd, rd, sizeof(rd)); - zassert_true(rc == 0, "area not erased"); - } - flash_area_close(fa); } From 6f28511318762a66bd1545d8659018b41a2690df Mon Sep 17 00:00:00 2001 From: Dominik Ermel Date: Thu, 7 Nov 2024 18:05:55 +0100 Subject: [PATCH 2/2] tests: flash_map: Move SHA test to separate test scenario The commit moves SHA calculation to separate test scenario, so that it can be run separately from other flash map tests. This reduces flash wear by not running basic flash map tests each time different SHA backend is tested. Signed-off-by: Dominik Ermel --- tests/subsys/storage/flash_map/CMakeLists.txt | 7 +- tests/subsys/storage/flash_map/src/main.c | 119 --------------- tests/subsys/storage/flash_map/src/main_sha.c | 139 ++++++++++++++++++ tests/subsys/storage/flash_map/testcase.yaml | 4 +- 4 files changed, 147 insertions(+), 122 deletions(-) create mode 100644 tests/subsys/storage/flash_map/src/main_sha.c diff --git a/tests/subsys/storage/flash_map/CMakeLists.txt b/tests/subsys/storage/flash_map/CMakeLists.txt index 832036757645c..8c0287e6f171b 100644 --- a/tests/subsys/storage/flash_map/CMakeLists.txt +++ b/tests/subsys/storage/flash_map/CMakeLists.txt @@ -4,5 +4,10 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(flash_map) -FILE(GLOB app_sources src/*.c) +if(NOT CONFIG_FLASH_AREA_CHECK_INTEGRITY_PSA AND NOT CONFIG_FLASH_AREA_CHECK_INTEGRITY_MBEDTLS) + FILE(GLOB app_sources src/main.c) +else() + FILE(GLOB app_sources src/main_sha.c) +endif() + target_sources(app PRIVATE ${app_sources}) diff --git a/tests/subsys/storage/flash_map/src/main.c b/tests/subsys/storage/flash_map/src/main.c index a4c6a3572ccf5..2ff7724f42c95 100644 --- a/tests/subsys/storage/flash_map/src/main.c +++ b/tests/subsys/storage/flash_map/src/main.c @@ -79,125 +79,6 @@ ZTEST(flash_map, test_flash_area_get_sectors) flash_area_close(fa); } -ZTEST(flash_map, test_flash_area_check_int_sha256) -{ - /* for i in {1..16}; do echo $'0123456789abcdef\nfedcba98765432' >> tst.sha; done - * hexdump tst.sha - */ - uint8_t tst_vec[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, - 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, - 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, - 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, - 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, - 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, - 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, - 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, - 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, - 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, - 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, - 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, - 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, - 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, - 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, - 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, - 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, - 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, - 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, - 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, - 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, - 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, - 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, - 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, - 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, - 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, - 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, - 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, - 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, - 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, - 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, - 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, - 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, - 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, - 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, - 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, - 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, - 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, - 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, - 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, - 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, - 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, - 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, - 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, - 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, - 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, - 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, - 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, - 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, - 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, - 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, - 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, - 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, - 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, - 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, - 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, - 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, - 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, - 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, - 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, - 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, - 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, - 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, - 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a }; - /* sha256sum tst.sha */ - uint8_t tst_sha[] = { 0xae, 0xed, 0x7d, 0x59, 0x53, 0xbd, 0xb7, 0x28, - 0x3e, 0x59, 0xc2, 0x65, 0x59, 0x62, 0xe3, 0x7e, - 0xfa, 0x97, 0xbd, 0x76, 0xf6, 0xac, 0xc3, 0x92, - 0x59, 0x48, 0x4e, 0xc0, 0xaf, 0xa8, 0x49, 0x65 }; - - const struct flash_area *fa; - struct flash_area_check fac = { NULL, 0, -1, NULL, 0 }; - uint8_t buffer[16]; - int rc; - - rc = flash_area_open(SLOT1_PARTITION_ID, &fa); - zassert_true(rc == 0, "flash_area_open() fail, error %d\n", rc); - rc = flash_area_erase(fa, 0, fa->fa_size); - zassert_true(rc == 0, "Flash erase failure (%d), error %d\n", rc); - rc = flash_area_write(fa, 0, tst_vec, sizeof(tst_vec)); - zassert_true(rc == 0, "Flash img write, error %d\n", rc); - - rc = flash_area_check_int_sha256(NULL, NULL); - zassert_true(rc == -EINVAL, "Flash area check int 256 params 1, 2\n"); - rc = flash_area_check_int_sha256(NULL, &fac); - zassert_true(rc == -EINVAL, "Flash area check int 256 params 2\n"); - rc = flash_area_check_int_sha256(fa, NULL); - zassert_true(rc == -EINVAL, "Flash area check int 256 params 1\n"); - - rc = flash_area_check_int_sha256(fa, &fac); - zassert_true(rc == -EINVAL, "Flash area check int 256 fac match\n"); - fac.match = tst_sha; - rc = flash_area_check_int_sha256(fa, &fac); - zassert_true(rc == -EINVAL, "Flash area check int 256 fac clen\n"); - fac.clen = sizeof(tst_vec); - rc = flash_area_check_int_sha256(fa, &fac); - zassert_true(rc == -EINVAL, "Flash area check int 256 fac off\n"); - fac.off = 0; - rc = flash_area_check_int_sha256(fa, &fac); - zassert_true(rc == -EINVAL, "Flash area check int 256 fac rbuf\n"); - fac.rbuf = buffer; - rc = flash_area_check_int_sha256(fa, &fac); - zassert_true(rc == -EINVAL, "Flash area check int 256 fac rblen\n"); - fac.rblen = sizeof(buffer); - - rc = flash_area_check_int_sha256(fa, &fac); - zassert_true(rc == 0, "Flash area check int 256 OK, error %d\n", rc); - tst_sha[0] = 0x00; - rc = flash_area_check_int_sha256(fa, &fac); - zassert_false(rc == 0, "Flash area check int 256 wrong sha\n"); - - flash_area_close(fa); -} - ZTEST(flash_map, test_flash_area_erased_val) { const struct flash_parameters *param; diff --git a/tests/subsys/storage/flash_map/src/main_sha.c b/tests/subsys/storage/flash_map/src/main_sha.c new file mode 100644 index 0000000000000..01a4c7b8394c7 --- /dev/null +++ b/tests/subsys/storage/flash_map/src/main_sha.c @@ -0,0 +1,139 @@ +/* + * Copyright (c) 2017-2024 Nordic Semiconductor ASA + * Copyright (c) 2015 Runtime Inc + * Copyright (c) 2020 Gerson Fernando Budke + * Copyright 2024 NXP + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +#define SLOT1_PARTITION slot1_partition +#define SLOT1_PARTITION_ID FIXED_PARTITION_ID(SLOT1_PARTITION) +#define SLOT1_PARTITION_DEV FIXED_PARTITION_DEVICE(SLOT1_PARTITION) +#define SLOT1_PARTITION_NODE DT_NODELABEL(SLOT1_PARTITION) +#define SLOT1_PARTITION_OFFSET FIXED_PARTITION_OFFSET(SLOT1_PARTITION) + +ZTEST(flash_map_sha, test_flash_area_check_int_sha256) +{ + /* for i in {1..16}; do echo $'0123456789abcdef\nfedcba98765432' >> tst.sha; done + * hexdump tst.sha + */ + uint8_t tst_vec[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, + 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, + 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, + 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, + 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, + 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, + 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, + 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, + 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, + 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, + 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, + 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, + 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, + 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, + 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, + 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, + 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, + 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, + 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, + 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, + 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, + 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, + 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, + 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, + 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, + 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, + 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, + 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, + 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, + 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, + 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, + 0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39, + 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a }; + /* sha256sum tst.sha */ + uint8_t tst_sha[] = { 0xae, 0xed, 0x7d, 0x59, 0x53, 0xbd, 0xb7, 0x28, + 0x3e, 0x59, 0xc2, 0x65, 0x59, 0x62, 0xe3, 0x7e, + 0xfa, 0x97, 0xbd, 0x76, 0xf6, 0xac, 0xc3, 0x92, + 0x59, 0x48, 0x4e, 0xc0, 0xaf, 0xa8, 0x49, 0x65 }; + + const struct flash_area *fa; + struct flash_area_check fac = { NULL, 0, -1, NULL, 0 }; + uint8_t buffer[16]; + int rc; + + rc = flash_area_open(SLOT1_PARTITION_ID, &fa); + zassert_true(rc == 0, "flash_area_open() fail, error %d\n", rc); + rc = flash_area_erase(fa, 0, fa->fa_size); + zassert_true(rc == 0, "Flash erase failure (%d), error %d\n", rc); + rc = flash_area_write(fa, 0, tst_vec, sizeof(tst_vec)); + zassert_true(rc == 0, "Flash img write, error %d\n", rc); + + rc = flash_area_check_int_sha256(NULL, NULL); + zassert_true(rc == -EINVAL, "Flash area check int 256 params 1, 2\n"); + rc = flash_area_check_int_sha256(NULL, &fac); + zassert_true(rc == -EINVAL, "Flash area check int 256 params 2\n"); + rc = flash_area_check_int_sha256(fa, NULL); + zassert_true(rc == -EINVAL, "Flash area check int 256 params 1\n"); + + rc = flash_area_check_int_sha256(fa, &fac); + zassert_true(rc == -EINVAL, "Flash area check int 256 fac match\n"); + fac.match = tst_sha; + rc = flash_area_check_int_sha256(fa, &fac); + zassert_true(rc == -EINVAL, "Flash area check int 256 fac clen\n"); + fac.clen = sizeof(tst_vec); + rc = flash_area_check_int_sha256(fa, &fac); + zassert_true(rc == -EINVAL, "Flash area check int 256 fac off\n"); + fac.off = 0; + rc = flash_area_check_int_sha256(fa, &fac); + zassert_true(rc == -EINVAL, "Flash area check int 256 fac rbuf\n"); + fac.rbuf = buffer; + rc = flash_area_check_int_sha256(fa, &fac); + zassert_true(rc == -EINVAL, "Flash area check int 256 fac rblen\n"); + fac.rblen = sizeof(buffer); + + rc = flash_area_check_int_sha256(fa, &fac); + zassert_true(rc == 0, "Flash area check int 256 OK, error %d\n", rc); + tst_sha[0] = 0x00; + rc = flash_area_check_int_sha256(fa, &fac); + zassert_false(rc == 0, "Flash area check int 256 wrong sha\n"); + + flash_area_close(fa); +} + +ZTEST_SUITE(flash_map_sha, NULL, NULL, NULL, NULL, NULL); diff --git a/tests/subsys/storage/flash_map/testcase.yaml b/tests/subsys/storage/flash_map/testcase.yaml index 9256dad8bf597..c1b65b0fdf90b 100644 --- a/tests/subsys/storage/flash_map/testcase.yaml +++ b/tests/subsys/storage/flash_map/testcase.yaml @@ -22,7 +22,7 @@ tests: integration_platforms: - nrf52840dk/nrf52840 tags: flash_map - storage.flash_map.mbedtls: + storage.flash_map_sha.mbedtls: extra_args: EXTRA_CONF_FILE=overlay-mbedtls.conf platform_allow: - nrf51dk/nrf51822 @@ -33,7 +33,7 @@ tests: tags: flash_map integration_platforms: - native_sim - storage.flash_map.psa: + storage.flash_map_sha.psa: extra_args: EXTRA_CONF_FILE=overlay-psa.conf platform_allow: - nrf51dk/nrf51822