22#
33# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
44
5+ import logging
56from collections .abc import Callable
67from pathlib import Path
78
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