@@ -40,7 +40,8 @@ static int mock_verify_integrity_ret;
4040static int mock_integrity_saw_not_ext ;
4141static int mock_authenticity_saw_not_ext ;
4242static int mock_verify_authenticity_ret ;
43- static int mock_image_part_matches_ret ;
43+ static int mock_part_mismatch_part ;
44+ static int mock_last_read_part ;
4445
4546ChaCha chacha ;
4647
@@ -87,6 +88,8 @@ static void reset_mocks(void)
8788 mock_fail_payload_part = -1 ;
8889 mock_verify_integrity_ret = 0 ;
8990 mock_verify_authenticity_ret = 0 ;
91+ mock_part_mismatch_part = -2 ;
92+ mock_last_read_part = -1 ;
9093 mock_integrity_saw_not_ext = -1 ;
9194 mock_authenticity_saw_not_ext = -1 ;
9295 mock_flash_protect_called = 0 ;
@@ -159,6 +162,7 @@ int disk_part_read(int drv, int part, uint64_t off, uint64_t sz, uint8_t *buf)
159162 uint64_t max = IMAGE_HEADER_SIZE + TEST_PAYLOAD_SIZE ;
160163
161164 (void )drv ;
165+ mock_last_read_part = part ;
162166 image = (part == BOOT_PART_B ) ? part_b_image : part_a_image ;
163167 if ((mock_fail_payload_part == part ) && (off >= IMAGE_HEADER_SIZE ))
164168 return -1 ;
@@ -205,7 +209,11 @@ int wolfBoot_verify_authenticity(struct wolfBoot_image* img)
205209int wolfBoot_image_part_matches (struct wolfBoot_image * img )
206210{
207211 (void )img ;
208- return mock_image_part_matches_ret ;
212+ /* The header of the slot under test is the last one read. */
213+ if ((mock_part_mismatch_part == -1 ) ||
214+ (mock_last_read_part == mock_part_mismatch_part ))
215+ return -1 ;
216+ return 0 ;
209217}
210218
211219int wolfBoot_get_dts_size (void * dts_addr , uint32_t capacity )
@@ -443,6 +451,36 @@ START_TEST(test_update_disk_rejects_invalid_signature)
443451}
444452END_TEST
445453
454+ START_TEST (test_update_disk_skips_partition_mismatch )
455+ {
456+ reset_mocks ();
457+ build_image (part_a_image , 7 , 0xA1 );
458+ build_image (part_b_image , 7 , 0xB2 );
459+ mock_part_mismatch_part = BOOT_PART_A ;
460+
461+ wolfBoot_start ();
462+
463+ ck_assert_int_eq (wolfBoot_panicked , 0 );
464+ ck_assert_int_eq (mock_do_boot_called , 1 );
465+ ck_assert_int_eq (memcmp (load_buffer , part_b_image + IMAGE_HEADER_SIZE ,
466+ TEST_PAYLOAD_SIZE ), 0 );
467+ }
468+ END_TEST
469+
470+ START_TEST (test_update_disk_rejects_partition_mismatch )
471+ {
472+ reset_mocks ();
473+ build_image (part_a_image , 7 , 0xA1 );
474+ build_image (part_b_image , 7 , 0xB2 );
475+ mock_part_mismatch_part = -1 ; /* both slots */
476+
477+ wolfBoot_start ();
478+
479+ ck_assert_int_gt (wolfBoot_panicked , 0 );
480+ ck_assert_int_eq (mock_do_boot_called , 0 );
481+ }
482+ END_TEST
483+
446484Suite * wolfboot_suite (void )
447485{
448486 Suite * s = suite_create ("wolfBoot" );
@@ -459,6 +497,8 @@ Suite *wolfboot_suite(void)
459497 tcase_add_test (tc , test_update_disk_rejects_rollback_after_higher_image_failure );
460498 tcase_add_test (tc , test_update_disk_rejects_invalid_integrity );
461499 tcase_add_test (tc , test_update_disk_rejects_invalid_signature );
500+ tcase_add_test (tc , test_update_disk_skips_partition_mismatch );
501+ tcase_add_test (tc , test_update_disk_rejects_partition_mismatch );
462502 suite_add_tcase (s , tc );
463503
464504 return s ;
0 commit comments