Skip to content

Commit e441dee

Browse files
committed
tests: Add tests with KMU key revocation
Added additional tests for KMU key revocation. Signed-off-by: Lukasz Fundakowski <lukasz.fundakowski@nordicsemi.no>
1 parent 3bc9a3d commit e441dee

File tree

2 files changed

+66
-19
lines changed

2 files changed

+66
-19
lines changed

tests/subsys/kmu/hello_for_kmu/testcase.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ tests:
2929
tags:
3030
- pytest
3131
- kmu
32+
timeout: 300
3233
harness: pytest
3334
harness_config:
3435
pytest_root:
@@ -47,7 +48,7 @@ tests:
4748
tags:
4849
- pytest
4950
- kmu
50-
timeout: 180
51+
timeout: 600
5152
harness: pytest
5253
harness_config:
5354
pytest_root:
@@ -71,12 +72,14 @@ tests:
7172
tags:
7273
- pytest
7374
- kmu
74-
timeout: 180
75+
timeout: 600
7576
harness: pytest
7677
harness_config:
7778
pytest_root:
7879
- "../pytest/test_kmu_key_revocation.py::test_if_kmu_keys_revocation_with_two_slots"
7980
- "../pytest/test_kmu_key_revocation.py::test_if_kmu_keys_revocation_with_two_slots_third_key_valid"
81+
- "../pytest/test_kmu_key_revocation.py::test_boot_failure_when_kmu_key_is_missing"
82+
- "../pytest/test_kmu_key_revocation.py::test_dut_does_not_boot_when_flashed_with_image_signed_with_wrong_key"
8083
extra_args:
8184
- SB_CONFIG_BM_BOOTLOADER_MCUBOOT_GENERATE_DEFAULT_KMU_KEYFILE=n
8285
- mcuboot_CONFIG_BOOT_KEYS_REVOCATION=y

tests/subsys/kmu/pytest/test_kmu_key_revocation.py

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#
33
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
44

5+
import logging
56
from collections.abc import Callable
67
from pathlib import Path
78

@@ -36,6 +37,8 @@
3637
"*Unable to find bootable image*",
3738
]
3839

40+
logger = logging.getLogger(__name__)
41+
3942

4043
@pytest.mark.usefixtures("no_reset")
4144
@pytest.mark.parametrize(
@@ -170,14 +173,14 @@ def test_if_previous_key_is_revoked_when_flashing_new_image(
170173
lines = dut.readlines_until(regex="Hello World!", print_output=True, timeout=20)
171174
pytest.LineMatcher(lines).fnmatch_lines(LINES_FOR_CORRECT_BOOT)
172175

173-
# flash DUT with the second image
176+
logger.info("Flash DUT with the second image")
174177
west_flash(build_dir_2, dut.device_config.id)
175178
dut.clear_buffer()
176179
reset_board(dut.device_config.id)
177180
lines = dut.readlines_until(regex="Hello World!", print_output=True, timeout=20)
178181
pytest.LineMatcher(lines).fnmatch_lines(LINES_FOR_CORRECT_BOOT)
179182

180-
# flash again with the first image
183+
logger.info("Flash DUT with the first image")
181184
west_flash(build_dir_1, dut.device_config.id)
182185
dut.clear_buffer()
183186
reset_board(dut.device_config.id)
@@ -196,14 +199,14 @@ def test_if_revocation_of_last_remaining_key_is_not_allowed(
196199
):
197200
"""Prevent Revocation of Last Remaining Key.
198201
199-
- build images signed with key1, key2 and key3
200-
- provison Dut with 2 keys (key1 and key2)
201-
- flash device with the image signed with key1 and reset
202-
- flash device with the image signed with key2 and reset
203-
- flash device with the image signed with key3 and reset
204-
- verified that Dut does not boot due to 3rd image is not signed with valid key
205-
- flash device with image sidned with key2
206-
- verify that Dut boots correctly and `Hello World` is printed to UART console
202+
- Build images signed with key1, key2 and key3
203+
- Provison Dut with 2 keys (key1 and key2)
204+
- Flash device with the image signed with key1 and reset
205+
- Flash device with the image signed with key2 and reset
206+
- Flash device with the image signed with key3 and reset
207+
- Verified that Dut does not boot due to 3rd image is not signed with valid key
208+
- Flash device with image sidned with key2
209+
- Verify that Dut boots correctly and `Hello World` is printed to UART console
207210
"""
208211
sysbuild_config = Path(dut.device_config.build_dir) / "zephyr" / ".config"
209212
valid_key_file = config_reader(sysbuild_config).read(
@@ -244,7 +247,7 @@ def test_if_revocation_of_last_remaining_key_is_not_allowed(
244247
build_director.run()
245248
assert not build_director.excptions, "Building samples failed"
246249

247-
# provision with first two keys
250+
logger.info("Provision DUT with two keys")
248251
provision_keys_for_kmu(
249252
keys=list(keys.values())[:2], keyname="BL_PUBKEY", dev_id=dut.device_config.id
250253
)
@@ -254,15 +257,15 @@ def test_if_revocation_of_last_remaining_key_is_not_allowed(
254257
lines = dut.readlines_until(regex="Hello World!", print_output=True, timeout=20)
255258
pytest.LineMatcher(lines).fnmatch_lines(LINES_FOR_CORRECT_BOOT)
256259

257-
# flash DUT with the second image
258-
west_flash(build_dir_2, dut.device_config.id)
260+
logger.info("Flash DUT with the second image")
261+
west_flash(build_dir_2, dut.device_config.id, extra_args="--no-reset")
259262
dut.clear_buffer()
260263
reset_board(dut.device_config.id)
261264
lines = dut.readlines_until(regex="Hello World!", print_output=True, timeout=20)
262265
pytest.LineMatcher(lines).fnmatch_lines(LINES_FOR_CORRECT_BOOT)
263266

264-
# flash DUT with the third image
265-
west_flash(build_dir_3, dut.device_config.id)
267+
logger.info("Flash DUT with the third image")
268+
west_flash(build_dir_3, dut.device_config.id, extra_args="--no-reset")
266269
dut.clear_buffer()
267270
reset_board(dut.device_config.id)
268271
lines = dut.readlines_until(
@@ -271,12 +274,53 @@ def test_if_revocation_of_last_remaining_key_is_not_allowed(
271274
pytest.LineMatcher(lines).no_fnmatch_line("*Hello World!*")
272275
pytest.LineMatcher(lines).fnmatch_lines(LINES_FOR_REVOCED_KEYS)
273276

274-
# flash DUT with the second image
277+
logger.info("Flash DUT with the second image")
275278
west_flash(build_dir_2, dut.device_config.id)
276279
dut.clear_buffer()
277280
reset_board(dut.device_config.id)
278-
# Check if Dut boots correctly
281+
# Check if Dut boots correctly and `Hello World` is printed
279282
lines = dut.readlines_until(
280283
regex="Unable to find bootable image|Hello World!", print_output=True, timeout=20
281284
)
282285
pytest.LineMatcher(lines).fnmatch_lines(LINES_FOR_CORRECT_BOOT)
286+
287+
288+
@pytest.mark.usefixtures("no_reset")
289+
def test_boot_failure_when_kmu_key_is_missing(
290+
dut: DeviceAdapter, config_reader: Callable, nrf_bm_path: Path, request: pytest.FixtureRequest
291+
):
292+
"""Verify if DUT does not boot when the key was not provisioned.
293+
294+
- Flash DUT with signed image
295+
- Reset DUT without provisinonig any KMU keys
296+
- Verify if DUT does not boot
297+
"""
298+
dut.clear_buffer()
299+
reset_board(dut.device_config.id)
300+
# Check if Dut boots correctly
301+
lines = dut.readlines_until(
302+
regex="Unable to find bootable image|Hello World!", print_output=True, timeout=20
303+
)
304+
pytest.LineMatcher(lines).no_fnmatch_line("*Hello World!*")
305+
pytest.LineMatcher(lines).fnmatch_lines(LINES_FOR_REVOCED_KEYS)
306+
307+
308+
@pytest.mark.usefixtures("no_reset")
309+
def test_dut_does_not_boot_when_flashed_with_image_signed_with_wrong_key(
310+
dut: DeviceAdapter, nrf_bm_path: Path
311+
):
312+
"""Verify if DUT does not boot when flashed with an image signed with wrong key."""
313+
keys_dict = {
314+
1: nrf_bm_path / "tests/subsys/kmu/keys/ed25519-1.pem",
315+
2: nrf_bm_path / "tests/subsys/kmu/keys/ed25519-2.pem",
316+
3: nrf_bm_path / "tests/subsys/kmu/keys/ed25519-3.pem",
317+
}
318+
keys = [str(value) for value in keys_dict.values()]
319+
320+
provision_keys_for_kmu(keys=keys, keyname="BL_PUBKEY", dev_id=dut.device_config.id)
321+
reset_board(dut.device_config.id)
322+
lines = dut.readlines_until(
323+
regex="Unable to find bootable image|Hello World!", print_output=True, timeout=20
324+
)
325+
pytest.LineMatcher(lines).no_fnmatch_line("*Hello World!*")
326+
pytest.LineMatcher(lines).fnmatch_lines(LINES_FOR_KEY_VERIFICATION_FAIL)

0 commit comments

Comments
 (0)