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 <dominik.ermel@nordicsemi.no>
This commit is contained in:
Dominik Ermel 2024-11-07 18:05:55 +01:00 committed by Anas Nashif
commit 06c0b764cc
4 changed files with 147 additions and 122 deletions

View file

@ -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})

View file

@ -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;

View file

@ -0,0 +1,139 @@
/*
* Copyright (c) 2017-2024 Nordic Semiconductor ASA
* Copyright (c) 2015 Runtime Inc
* Copyright (c) 2020 Gerson Fernando Budke <nandojve@gmail.com>
* Copyright 2024 NXP
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/ztest.h>
#include <zephyr/drivers/flash.h>
#include <zephyr/storage/flash_map.h>
#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);

View file

@ -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