From 06c0b764cc3a83a8f95c75a98f95a3f16493e656 Mon Sep 17 00:00:00 2001 From: Dominik Ermel Date: Thu, 7 Nov 2024 18:05:55 +0100 Subject: [PATCH] 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 83203675764..8c0287e6f17 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 a4c6a3572cc..2ff7724f42c 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 00000000000..01a4c7b8394 --- /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 9256dad8bf5..c1b65b0fdf9 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