Skip to content

Commit 6f28511

Browse files
committed
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 <[email protected]>
1 parent c10fbc8 commit 6f28511

File tree

4 files changed

+147
-122
lines changed

4 files changed

+147
-122
lines changed

tests/subsys/storage/flash_map/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,10 @@ cmake_minimum_required(VERSION 3.20.0)
44
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
55
project(flash_map)
66

7-
FILE(GLOB app_sources src/*.c)
7+
if(NOT CONFIG_FLASH_AREA_CHECK_INTEGRITY_PSA AND NOT CONFIG_FLASH_AREA_CHECK_INTEGRITY_MBEDTLS)
8+
FILE(GLOB app_sources src/main.c)
9+
else()
10+
FILE(GLOB app_sources src/main_sha.c)
11+
endif()
12+
813
target_sources(app PRIVATE ${app_sources})

tests/subsys/storage/flash_map/src/main.c

Lines changed: 0 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -79,125 +79,6 @@ ZTEST(flash_map, test_flash_area_get_sectors)
7979
flash_area_close(fa);
8080
}
8181

82-
ZTEST(flash_map, test_flash_area_check_int_sha256)
83-
{
84-
/* for i in {1..16}; do echo $'0123456789abcdef\nfedcba98765432' >> tst.sha; done
85-
* hexdump tst.sha
86-
*/
87-
uint8_t tst_vec[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
88-
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
89-
0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39,
90-
0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a,
91-
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
92-
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
93-
0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39,
94-
0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a,
95-
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
96-
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
97-
0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39,
98-
0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a,
99-
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
100-
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
101-
0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39,
102-
0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a,
103-
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
104-
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
105-
0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39,
106-
0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a,
107-
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
108-
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
109-
0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39,
110-
0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a,
111-
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
112-
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
113-
0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39,
114-
0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a,
115-
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
116-
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
117-
0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39,
118-
0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a,
119-
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
120-
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
121-
0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39,
122-
0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a,
123-
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
124-
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
125-
0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39,
126-
0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a,
127-
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
128-
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
129-
0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39,
130-
0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a,
131-
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
132-
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
133-
0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39,
134-
0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a,
135-
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
136-
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
137-
0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39,
138-
0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a,
139-
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
140-
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
141-
0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39,
142-
0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a,
143-
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
144-
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
145-
0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39,
146-
0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a,
147-
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
148-
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
149-
0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39,
150-
0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a };
151-
/* sha256sum tst.sha */
152-
uint8_t tst_sha[] = { 0xae, 0xed, 0x7d, 0x59, 0x53, 0xbd, 0xb7, 0x28,
153-
0x3e, 0x59, 0xc2, 0x65, 0x59, 0x62, 0xe3, 0x7e,
154-
0xfa, 0x97, 0xbd, 0x76, 0xf6, 0xac, 0xc3, 0x92,
155-
0x59, 0x48, 0x4e, 0xc0, 0xaf, 0xa8, 0x49, 0x65 };
156-
157-
const struct flash_area *fa;
158-
struct flash_area_check fac = { NULL, 0, -1, NULL, 0 };
159-
uint8_t buffer[16];
160-
int rc;
161-
162-
rc = flash_area_open(SLOT1_PARTITION_ID, &fa);
163-
zassert_true(rc == 0, "flash_area_open() fail, error %d\n", rc);
164-
rc = flash_area_erase(fa, 0, fa->fa_size);
165-
zassert_true(rc == 0, "Flash erase failure (%d), error %d\n", rc);
166-
rc = flash_area_write(fa, 0, tst_vec, sizeof(tst_vec));
167-
zassert_true(rc == 0, "Flash img write, error %d\n", rc);
168-
169-
rc = flash_area_check_int_sha256(NULL, NULL);
170-
zassert_true(rc == -EINVAL, "Flash area check int 256 params 1, 2\n");
171-
rc = flash_area_check_int_sha256(NULL, &fac);
172-
zassert_true(rc == -EINVAL, "Flash area check int 256 params 2\n");
173-
rc = flash_area_check_int_sha256(fa, NULL);
174-
zassert_true(rc == -EINVAL, "Flash area check int 256 params 1\n");
175-
176-
rc = flash_area_check_int_sha256(fa, &fac);
177-
zassert_true(rc == -EINVAL, "Flash area check int 256 fac match\n");
178-
fac.match = tst_sha;
179-
rc = flash_area_check_int_sha256(fa, &fac);
180-
zassert_true(rc == -EINVAL, "Flash area check int 256 fac clen\n");
181-
fac.clen = sizeof(tst_vec);
182-
rc = flash_area_check_int_sha256(fa, &fac);
183-
zassert_true(rc == -EINVAL, "Flash area check int 256 fac off\n");
184-
fac.off = 0;
185-
rc = flash_area_check_int_sha256(fa, &fac);
186-
zassert_true(rc == -EINVAL, "Flash area check int 256 fac rbuf\n");
187-
fac.rbuf = buffer;
188-
rc = flash_area_check_int_sha256(fa, &fac);
189-
zassert_true(rc == -EINVAL, "Flash area check int 256 fac rblen\n");
190-
fac.rblen = sizeof(buffer);
191-
192-
rc = flash_area_check_int_sha256(fa, &fac);
193-
zassert_true(rc == 0, "Flash area check int 256 OK, error %d\n", rc);
194-
tst_sha[0] = 0x00;
195-
rc = flash_area_check_int_sha256(fa, &fac);
196-
zassert_false(rc == 0, "Flash area check int 256 wrong sha\n");
197-
198-
flash_area_close(fa);
199-
}
200-
20182
ZTEST(flash_map, test_flash_area_erased_val)
20283
{
20384
const struct flash_parameters *param;
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Copyright (c) 2017-2024 Nordic Semiconductor ASA
3+
* Copyright (c) 2015 Runtime Inc
4+
* Copyright (c) 2020 Gerson Fernando Budke <[email protected]>
5+
* Copyright 2024 NXP
6+
*
7+
* SPDX-License-Identifier: Apache-2.0
8+
*/
9+
10+
#include <zephyr/ztest.h>
11+
#include <zephyr/drivers/flash.h>
12+
#include <zephyr/storage/flash_map.h>
13+
14+
#define SLOT1_PARTITION slot1_partition
15+
#define SLOT1_PARTITION_ID FIXED_PARTITION_ID(SLOT1_PARTITION)
16+
#define SLOT1_PARTITION_DEV FIXED_PARTITION_DEVICE(SLOT1_PARTITION)
17+
#define SLOT1_PARTITION_NODE DT_NODELABEL(SLOT1_PARTITION)
18+
#define SLOT1_PARTITION_OFFSET FIXED_PARTITION_OFFSET(SLOT1_PARTITION)
19+
20+
ZTEST(flash_map_sha, test_flash_area_check_int_sha256)
21+
{
22+
/* for i in {1..16}; do echo $'0123456789abcdef\nfedcba98765432' >> tst.sha; done
23+
* hexdump tst.sha
24+
*/
25+
uint8_t tst_vec[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
26+
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
27+
0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39,
28+
0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a,
29+
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
30+
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
31+
0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39,
32+
0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a,
33+
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
34+
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
35+
0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39,
36+
0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a,
37+
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
38+
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
39+
0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39,
40+
0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a,
41+
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
42+
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
43+
0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39,
44+
0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a,
45+
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
46+
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
47+
0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39,
48+
0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a,
49+
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
50+
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
51+
0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39,
52+
0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a,
53+
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
54+
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
55+
0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39,
56+
0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a,
57+
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
58+
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
59+
0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39,
60+
0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a,
61+
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
62+
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
63+
0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39,
64+
0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a,
65+
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
66+
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
67+
0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39,
68+
0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a,
69+
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
70+
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
71+
0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39,
72+
0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a,
73+
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
74+
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
75+
0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39,
76+
0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a,
77+
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
78+
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
79+
0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39,
80+
0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a,
81+
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
82+
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
83+
0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39,
84+
0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a,
85+
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
86+
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
87+
0x0a, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x39,
88+
0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x0a };
89+
/* sha256sum tst.sha */
90+
uint8_t tst_sha[] = { 0xae, 0xed, 0x7d, 0x59, 0x53, 0xbd, 0xb7, 0x28,
91+
0x3e, 0x59, 0xc2, 0x65, 0x59, 0x62, 0xe3, 0x7e,
92+
0xfa, 0x97, 0xbd, 0x76, 0xf6, 0xac, 0xc3, 0x92,
93+
0x59, 0x48, 0x4e, 0xc0, 0xaf, 0xa8, 0x49, 0x65 };
94+
95+
const struct flash_area *fa;
96+
struct flash_area_check fac = { NULL, 0, -1, NULL, 0 };
97+
uint8_t buffer[16];
98+
int rc;
99+
100+
rc = flash_area_open(SLOT1_PARTITION_ID, &fa);
101+
zassert_true(rc == 0, "flash_area_open() fail, error %d\n", rc);
102+
rc = flash_area_erase(fa, 0, fa->fa_size);
103+
zassert_true(rc == 0, "Flash erase failure (%d), error %d\n", rc);
104+
rc = flash_area_write(fa, 0, tst_vec, sizeof(tst_vec));
105+
zassert_true(rc == 0, "Flash img write, error %d\n", rc);
106+
107+
rc = flash_area_check_int_sha256(NULL, NULL);
108+
zassert_true(rc == -EINVAL, "Flash area check int 256 params 1, 2\n");
109+
rc = flash_area_check_int_sha256(NULL, &fac);
110+
zassert_true(rc == -EINVAL, "Flash area check int 256 params 2\n");
111+
rc = flash_area_check_int_sha256(fa, NULL);
112+
zassert_true(rc == -EINVAL, "Flash area check int 256 params 1\n");
113+
114+
rc = flash_area_check_int_sha256(fa, &fac);
115+
zassert_true(rc == -EINVAL, "Flash area check int 256 fac match\n");
116+
fac.match = tst_sha;
117+
rc = flash_area_check_int_sha256(fa, &fac);
118+
zassert_true(rc == -EINVAL, "Flash area check int 256 fac clen\n");
119+
fac.clen = sizeof(tst_vec);
120+
rc = flash_area_check_int_sha256(fa, &fac);
121+
zassert_true(rc == -EINVAL, "Flash area check int 256 fac off\n");
122+
fac.off = 0;
123+
rc = flash_area_check_int_sha256(fa, &fac);
124+
zassert_true(rc == -EINVAL, "Flash area check int 256 fac rbuf\n");
125+
fac.rbuf = buffer;
126+
rc = flash_area_check_int_sha256(fa, &fac);
127+
zassert_true(rc == -EINVAL, "Flash area check int 256 fac rblen\n");
128+
fac.rblen = sizeof(buffer);
129+
130+
rc = flash_area_check_int_sha256(fa, &fac);
131+
zassert_true(rc == 0, "Flash area check int 256 OK, error %d\n", rc);
132+
tst_sha[0] = 0x00;
133+
rc = flash_area_check_int_sha256(fa, &fac);
134+
zassert_false(rc == 0, "Flash area check int 256 wrong sha\n");
135+
136+
flash_area_close(fa);
137+
}
138+
139+
ZTEST_SUITE(flash_map_sha, NULL, NULL, NULL, NULL, NULL);

tests/subsys/storage/flash_map/testcase.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ tests:
2222
integration_platforms:
2323
- nrf52840dk/nrf52840
2424
tags: flash_map
25-
storage.flash_map.mbedtls:
25+
storage.flash_map_sha.mbedtls:
2626
extra_args: EXTRA_CONF_FILE=overlay-mbedtls.conf
2727
platform_allow:
2828
- nrf51dk/nrf51822
@@ -33,7 +33,7 @@ tests:
3333
tags: flash_map
3434
integration_platforms:
3535
- native_sim
36-
storage.flash_map.psa:
36+
storage.flash_map_sha.psa:
3737
extra_args: EXTRA_CONF_FILE=overlay-psa.conf
3838
platform_allow:
3939
- nrf51dk/nrf51822

0 commit comments

Comments
 (0)