Skip to content

Commit 68ee02d

Browse files
committed
Improved unit test coverage
1 parent 61a0bf5 commit 68ee02d

File tree

3 files changed

+105
-3
lines changed

3 files changed

+105
-3
lines changed

tools/unit-tests/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ LDFLAGS+=-ftest-coverage
2626

2727

2828
TESTS:=unit-parser unit-extflash unit-aes128 unit-aes256 unit-chacha20 unit-pci \
29-
unit-mock-state unit-sectorflags unit-image unit-nvm unit-nvm-flagshome \
30-
unit-enc-nvm unit-enc-nvm-flagshome unit-delta unit-update-flash \
31-
unit-update-ram unit-pkcs11_store
29+
unit-mock-state unit-sectorflags unit-image unit-nvm unit-nvm-flagshome \
30+
unit-enc-nvm unit-enc-nvm-flagshome unit-delta unit-update-flash \
31+
unit-update-ram unit-pkcs11_store
3232

3333
all: $(TESTS)
3434

tools/unit-tests/unit-image.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,20 @@ START_TEST(test_open_image)
551551
ck_assert_ptr_eq(img.fw_base, (uint8_t *)WOLFBOOT_PARTITION_UPDATE_ADDRESS
552552
+ 256);
553553

554+
/* External helper should accept the same mapped header pointer */
555+
ret = wolfBoot_open_image_external(NULL, PART_UPDATE,
556+
(uint8_t *)WOLFBOOT_PARTITION_UPDATE_ADDRESS);
557+
ck_assert_int_eq(ret, -1);
558+
559+
memset(&img, 0, sizeof(img));
560+
hdr_cpy_done = 0;
561+
ret = wolfBoot_open_image_external(&img, PART_UPDATE,
562+
(uint8_t *)WOLFBOOT_PARTITION_UPDATE_ADDRESS);
563+
ck_assert_int_eq(ret, 0);
564+
ck_assert_uint_eq(img.hdr_ok, 1);
565+
ck_assert_ptr_eq(img.hdr, (void *)WOLFBOOT_PARTITION_UPDATE_ADDRESS);
566+
ck_assert_ptr_eq(img.fw_base, (uint8_t *)WOLFBOOT_PARTITION_UPDATE_ADDRESS
567+
+ 256);
554568
}
555569
END_TEST
556570

tools/unit-tests/unit-update-flash.c

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,29 @@
4848

4949
const char *argv0;
5050

51+
static void reset_mock_stats(void);
52+
static void prepare_flash(void);
53+
static void cleanup_flash(void);
54+
55+
START_TEST (test_boot_success_sets_state)
56+
{
57+
uint8_t state = 0;
58+
59+
reset_mock_stats();
60+
prepare_flash();
61+
hal_flash_unlock();
62+
wolfBoot_set_partition_state(PART_BOOT, IMG_STATE_TESTING);
63+
hal_flash_lock();
64+
65+
wolfBoot_success();
66+
67+
ck_assert_int_eq(wolfBoot_get_partition_state(PART_BOOT, &state), 0);
68+
ck_assert_uint_eq(state, IMG_STATE_SUCCESS);
69+
70+
cleanup_flash();
71+
}
72+
END_TEST
73+
5174
Suite *wolfboot_suite(void);
5275

5376
int wolfBoot_staged_ok = 0;
@@ -412,6 +435,62 @@ START_TEST (test_empty_boot_but_update_sha_corrupted_denied) {
412435
cleanup_flash();
413436
}
414437

438+
START_TEST (test_swap_resume_noop)
439+
{
440+
reset_mock_stats();
441+
prepare_flash();
442+
ext_flash_unlock();
443+
wolfBoot_set_partition_state(PART_UPDATE, IMG_STATE_NEW);
444+
ext_flash_lock();
445+
ck_assert_int_eq(wolfBoot_swap_and_final_erase(1), -1);
446+
cleanup_flash();
447+
}
448+
END_TEST
449+
450+
START_TEST (test_diffbase_version_reads)
451+
{
452+
uint32_t word;
453+
uint32_t version = 0x01020304;
454+
uint32_t delta_base = 0x33445566;
455+
uint16_t img_type = HDR_IMG_TYPE_AUTH | HDR_IMG_TYPE_APP;
456+
457+
reset_mock_stats();
458+
prepare_flash();
459+
460+
ext_flash_unlock();
461+
ext_flash_write(WOLFBOOT_PARTITION_UPDATE_ADDRESS,
462+
(const uint8_t *)"WOLF", 4);
463+
ext_flash_write(WOLFBOOT_PARTITION_UPDATE_ADDRESS + 4,
464+
(const uint8_t *)&version, sizeof(version));
465+
466+
word = (4u << 16) | HDR_VERSION;
467+
ext_flash_write(WOLFBOOT_PARTITION_UPDATE_ADDRESS + 8,
468+
(const uint8_t *)&word, sizeof(word));
469+
ext_flash_write(WOLFBOOT_PARTITION_UPDATE_ADDRESS + 12,
470+
(const uint8_t *)&version, sizeof(version));
471+
472+
word = (2u << 16) | HDR_IMG_TYPE;
473+
ext_flash_write(WOLFBOOT_PARTITION_UPDATE_ADDRESS + 16,
474+
(const uint8_t *)&word, sizeof(word));
475+
ext_flash_write(WOLFBOOT_PARTITION_UPDATE_ADDRESS + 20,
476+
(const uint8_t *)&img_type, sizeof(img_type));
477+
478+
word = (4u << 16) | HDR_IMG_DELTA_BASE;
479+
ext_flash_write(WOLFBOOT_PARTITION_UPDATE_ADDRESS + 24,
480+
(const uint8_t *)&word, sizeof(word));
481+
ext_flash_write(WOLFBOOT_PARTITION_UPDATE_ADDRESS + 28,
482+
(const uint8_t *)&delta_base, sizeof(delta_base));
483+
ext_flash_lock();
484+
485+
ck_assert_uint_eq(wolfBoot_get_diffbase_version(PART_UPDATE), delta_base);
486+
ck_assert_uint_eq(wolfBoot_get_diffbase_version(PART_BOOT), 0);
487+
ck_assert_uint_eq(wolfBoot_get_image_version(PART_UPDATE), version);
488+
ck_assert_uint_eq(wolfBoot_get_image_type(PART_UPDATE), img_type);
489+
490+
cleanup_flash();
491+
}
492+
END_TEST
493+
415494

416495
Suite *wolfboot_suite(void)
417496
{
@@ -439,6 +518,9 @@ Suite *wolfboot_suite(void)
439518
TCase *emergency_rollback_failure_due_to_bad_update = tcase_create("Emergency rollback failure due to bad update");
440519
TCase *empty_boot_partition_update = tcase_create("Empty boot partition update");
441520
TCase *empty_boot_but_update_sha_corrupted_denied = tcase_create("Empty boot partition but update SHA corrupted");
521+
TCase *swap_resume = tcase_create("Swap resume noop");
522+
TCase *diffbase_version = tcase_create("Diffbase version lookup");
523+
TCase *boot_success = tcase_create("Boot success state");
442524

443525

444526

@@ -456,6 +538,9 @@ Suite *wolfboot_suite(void)
456538
tcase_add_test(emergency_rollback_failure_due_to_bad_update, test_emergency_rollback_failure_due_to_bad_update);
457539
tcase_add_test(empty_boot_partition_update, test_empty_boot_partition_update);
458540
tcase_add_test(empty_boot_but_update_sha_corrupted_denied, test_empty_boot_but_update_sha_corrupted_denied);
541+
tcase_add_test(swap_resume, test_swap_resume_noop);
542+
tcase_add_test(diffbase_version, test_diffbase_version_reads);
543+
tcase_add_test(boot_success, test_boot_success_sets_state);
459544

460545

461546

@@ -473,6 +558,9 @@ Suite *wolfboot_suite(void)
473558
suite_add_tcase(s, emergency_rollback_failure_due_to_bad_update);
474559
suite_add_tcase(s, empty_boot_partition_update);
475560
suite_add_tcase(s, empty_boot_but_update_sha_corrupted_denied);
561+
suite_add_tcase(s, swap_resume);
562+
suite_add_tcase(s, diffbase_version);
563+
suite_add_tcase(s, boot_success);
476564

477565

478566

0 commit comments

Comments
 (0)