blob: ee941fe1cbb7477c4c4b3a719f16aafe6ee86896 [file] [log] [blame]
David Brownc8d62012021-10-27 15:03:48 -06001// Copyright (c) 2017-2021 Linaro LTD
David Browne2acfae2020-01-21 16:45:01 -07002// Copyright (c) 2017-2019 JUUL Labs
Roland Mikhel6945bb62023-04-11 15:57:49 +02003// Copyright (c) 2021-2023 Arm Limited
David Browne2acfae2020-01-21 16:45:01 -07004//
5// SPDX-License-Identifier: Apache-2.0
6
David Browndd2b1182017-11-02 15:39:21 -06007//! Core tests
8//!
9//! Run the existing testsuite as a Rust unit test.
10
David Brownc3898d62019-08-05 14:20:02 -060011use bootsim::{
12 DepTest, DepType, UpgradeInfo,
13 ImagesBuilder,
David Brownfe5ab1c2019-08-13 15:35:23 -060014 Images,
David Brownc3898d62019-08-05 14:20:02 -060015 NO_DEPS,
David Brown2ee5f7f2020-01-13 14:04:01 -070016 REV_DEPS,
David Brownc3898d62019-08-05 14:20:02 -060017 testlog,
Roland Mikhel6945bb62023-04-11 15:57:49 +020018 ImageManipulation
David Brownc3898d62019-08-05 14:20:02 -060019};
David Brownfe5ab1c2019-08-13 15:35:23 -060020use std::{
21 env,
22 sync::atomic::{AtomicUsize, Ordering},
23};
David Browndd2b1182017-11-02 15:39:21 -060024
David Brownc3898d62019-08-05 14:20:02 -060025/// A single test, after setting up logging and such. Within the $body,
26/// $arg will be bound to each device.
27macro_rules! test_shell {
28 ($name:ident, $arg: ident, $body:expr) => {
David Browna4167ef2017-11-06 14:30:05 -070029 #[test]
30 fn $name() {
31 testlog::setup();
David Brownc3898d62019-08-05 14:20:02 -060032 ImagesBuilder::each_device(|$arg| {
33 $body;
David Browna4167ef2017-11-06 14:30:05 -070034 });
David Browndd2b1182017-11-02 15:39:21 -060035 }
David Brownc3898d62019-08-05 14:20:02 -060036 }
37}
38
39/// A typical test calls a particular constructor, and runs a given test on
40/// that constructor.
41macro_rules! sim_test {
42 ($name:ident, $maker:ident($($margs:expr),*), $test:ident($($targs:expr),*)) => {
43 test_shell!($name, r, {
44 let image = r.$maker($($margs),*);
David Brownfe5ab1c2019-08-13 15:35:23 -060045 dump_image(&image, stringify!($name));
David Brownc3898d62019-08-05 14:20:02 -060046 assert!(!image.$test($($targs),*));
47 });
David Browna4167ef2017-11-06 14:30:05 -070048 };
David Browndd2b1182017-11-02 15:39:21 -060049}
David Browna4167ef2017-11-06 14:30:05 -070050
David Browneebf5022019-07-30 15:01:07 -060051sim_test!(bad_secondary_slot, make_bad_secondary_slot_image(), run_signfail_upgrade());
Fabio Utzig2c3be5c2020-07-09 19:54:45 -030052sim_test!(secondary_trailer_leftover, make_erased_secondary_image(), run_secondary_leftover_trailer());
Fabio Utzigd0157342020-10-02 15:22:11 -030053sim_test!(bootstrap, make_bootstrap_image(), run_bootstrap());
Andrzej Puzdrowski26d19d32022-08-10 18:11:53 +020054sim_test!(oversized_bootstrap, make_oversized_bootstrap_image(), run_oversized_bootstrap());
Roland Mikhel6945bb62023-04-11 15:57:49 +020055sim_test!(norevert_newimage, make_no_upgrade_image(&NO_DEPS, ImageManipulation::None), run_norevert_newimage());
David Brownc3898d62019-08-05 14:20:02 -060056sim_test!(basic_revert, make_image(&NO_DEPS, true), run_basic_revert());
57sim_test!(revert_with_fails, make_image(&NO_DEPS, false), run_revert_with_fails());
58sim_test!(perm_with_fails, make_image(&NO_DEPS, true), run_perm_with_fails());
59sim_test!(perm_with_random_fails, make_image(&NO_DEPS, true), run_perm_with_random_fails(5));
60sim_test!(norevert, make_image(&NO_DEPS, true), run_norevert());
Andrzej Puzdrowski9324d2b2022-08-11 15:16:56 +020061
62#[cfg(not(feature = "max-align-32"))]
63sim_test!(oversized_secondary_slot, make_oversized_secondary_slot_image(), run_oversizefail_upgrade());
64
David Brownc3898d62019-08-05 14:20:02 -060065sim_test!(status_write_fails_complete, make_image(&NO_DEPS, true), run_with_status_fails_complete());
66sim_test!(status_write_fails_with_reset, make_image(&NO_DEPS, true), run_with_status_fails_with_reset());
David Brown2ee5f7f2020-01-13 14:04:01 -070067sim_test!(downgrade_prevention, make_image(&REV_DEPS, true), run_nodowngrade());
David Brownc3898d62019-08-05 14:20:02 -060068
Roland Mikhel6945bb62023-04-11 15:57:49 +020069sim_test!(direct_xip_first, make_no_upgrade_image(&NO_DEPS, ImageManipulation::None), run_direct_xip());
70sim_test!(ram_load_first, make_no_upgrade_image(&NO_DEPS, ImageManipulation::None), run_ram_load());
71sim_test!(ram_load_split, make_no_upgrade_image(&NO_DEPS, ImageManipulation::None), run_split_ram_load());
Roland Mikheld6703522023-04-27 14:24:30 +020072sim_test!(hw_prot_failed_security_cnt_check, make_image_with_security_counter(Some(0)), run_hw_rollback_prot());
73sim_test!(hw_prot_missing_security_cnt, make_image_with_security_counter(None), run_hw_rollback_prot());
Roland Mikhel6945bb62023-04-11 15:57:49 +020074sim_test!(ram_load_out_of_bounds, make_no_upgrade_image(&NO_DEPS, ImageManipulation::WrongOffset), run_ram_load_boot_with_result(false));
75sim_test!(ram_load_missing_header_flag, make_no_upgrade_image(&NO_DEPS, ImageManipulation::IgnoreRamLoadFlag), run_ram_load_boot_with_result(false));
76sim_test!(ram_load_failed_validation, make_no_upgrade_image(&NO_DEPS, ImageManipulation::BadSignature), run_ram_load_boot_with_result(false));
77sim_test!(ram_load_corrupt_higher_version_image, make_no_upgrade_image(&NO_DEPS, ImageManipulation::CorruptHigherVersionImage), run_ram_load_boot_with_result(true));
78
79#[cfg(feature = "multiimage")]
80sim_test!(ram_load_overlapping_images_same_base, make_no_upgrade_image(&NO_DEPS, ImageManipulation::OverlapImages(true)), run_ram_load_boot_with_result(false));
81#[cfg(feature = "multiimage")]
82sim_test!(ram_load_overlapping_images_offset, make_no_upgrade_image(&NO_DEPS, ImageManipulation::OverlapImages(false)), run_ram_load_boot_with_result(false));
David Brown0dfb8102021-06-03 15:29:11 -060083
David Brownc3898d62019-08-05 14:20:02 -060084// Test various combinations of incorrect dependencies.
85test_shell!(dependency_combos, r, {
86 // Only test setups with two images.
87 if r.num_images() != 2 {
88 return;
89 }
90
David Brownfe5ab1c2019-08-13 15:35:23 -060091 for dep in TEST_DEPS {
David Brownc3898d62019-08-05 14:20:02 -060092 let image = r.clone().make_image(&dep, true);
David Brownfe5ab1c2019-08-13 15:35:23 -060093 dump_image(&image, "dependency_combos");
David Brownc3898d62019-08-05 14:20:02 -060094 assert!(!image.run_check_deps(&dep));
95 }
96});
97
98/// These are the variants of dependencies we will test.
99pub static TEST_DEPS: &[DepTest] = &[
David Browne4576b82019-09-03 12:26:18 -0600100 // A sanity test, no dependencies should upgrade.
David Brownc3898d62019-08-05 14:20:02 -0600101 DepTest {
102 depends: [DepType::Nothing, DepType::Nothing],
103 upgrades: [UpgradeInfo::Upgraded, UpgradeInfo::Upgraded],
David Brown2ee5f7f2020-01-13 14:04:01 -0700104 downgrade: false,
David Brownc3898d62019-08-05 14:20:02 -0600105 },
106
David Brown18d301f2019-09-03 11:37:39 -0600107 // If all of the dependencies are met, we should also upgrade.
Fabio Utzig135f7162019-08-28 11:03:44 -0300108 DepTest {
109 depends: [DepType::Correct, DepType::Correct],
110 upgrades: [UpgradeInfo::Upgraded, UpgradeInfo::Upgraded],
David Brown2ee5f7f2020-01-13 14:04:01 -0700111 downgrade: false,
Fabio Utzig135f7162019-08-28 11:03:44 -0300112 },
113
David Brown18d301f2019-09-03 11:37:39 -0600114 // If none of the dependencies are met, the images should be held.
David Brownc3898d62019-08-05 14:20:02 -0600115 DepTest {
116 depends: [DepType::Newer, DepType::Newer],
117 upgrades: [UpgradeInfo::Held, UpgradeInfo::Held],
David Brown2ee5f7f2020-01-13 14:04:01 -0700118 downgrade: false,
David Brownc3898d62019-08-05 14:20:02 -0600119 },
David Brown18d301f2019-09-03 11:37:39 -0600120
121 // If the first image is not met, we should hold back on the
122 // dependencies (it is not well defined what the correct behavior is
123 // here, it could also be correct to upgrade only the second image).
124 DepTest {
125 depends: [DepType::Newer, DepType::Correct],
126 upgrades: [UpgradeInfo::Held, UpgradeInfo::Held],
David Brown2ee5f7f2020-01-13 14:04:01 -0700127 downgrade: false,
David Brown18d301f2019-09-03 11:37:39 -0600128 },
129
130 // Test the variant in the other direction.
131 DepTest {
132 depends: [DepType::Correct, DepType::Newer],
133 upgrades: [UpgradeInfo::Held, UpgradeInfo::Held],
David Brown2ee5f7f2020-01-13 14:04:01 -0700134 downgrade: false,
David Brown18d301f2019-09-03 11:37:39 -0600135 },
136
David Browne4576b82019-09-03 12:26:18 -0600137 // Test where only the first image is upgraded, and there are no
138 // dependencies.
139 DepTest {
140 depends: [DepType::Nothing, DepType::NoUpgrade],
141 upgrades: [UpgradeInfo::Upgraded, UpgradeInfo::Held],
David Brown2ee5f7f2020-01-13 14:04:01 -0700142 downgrade: false,
David Browne4576b82019-09-03 12:26:18 -0600143 },
144
145 // Test one image with a valid dependency on the first image.
146 DepTest {
147 depends: [DepType::OldCorrect, DepType::NoUpgrade],
148 upgrades: [UpgradeInfo::Upgraded, UpgradeInfo::Held],
David Brown2ee5f7f2020-01-13 14:04:01 -0700149 downgrade: false,
David Browne4576b82019-09-03 12:26:18 -0600150 },
151
152 // Test one image with an invalid dependency on the first image.
153 DepTest {
154 depends: [DepType::Newer, DepType::NoUpgrade],
155 upgrades: [UpgradeInfo::Held, UpgradeInfo::Held],
David Brown2ee5f7f2020-01-13 14:04:01 -0700156 downgrade: false,
David Browne4576b82019-09-03 12:26:18 -0600157 },
158
159 // Test where only the second image is upgraded, and there are no
160 // dependencies.
161 DepTest {
162 depends: [DepType::NoUpgrade, DepType::Nothing],
163 upgrades: [UpgradeInfo::Held, UpgradeInfo::Upgraded],
David Brown2ee5f7f2020-01-13 14:04:01 -0700164 downgrade: false,
David Browne4576b82019-09-03 12:26:18 -0600165 },
166
167 // Test one image with a valid dependency on the second image.
168 DepTest {
169 depends: [DepType::NoUpgrade, DepType::OldCorrect],
170 upgrades: [UpgradeInfo::Held, UpgradeInfo::Upgraded],
David Brown2ee5f7f2020-01-13 14:04:01 -0700171 downgrade: false,
David Browne4576b82019-09-03 12:26:18 -0600172 },
173
174 // Test one image with an invalid dependency on the second image.
175 DepTest {
176 depends: [DepType::NoUpgrade, DepType::Newer],
177 upgrades: [UpgradeInfo::Held, UpgradeInfo::Held],
David Brown2ee5f7f2020-01-13 14:04:01 -0700178 downgrade: false,
David Browne4576b82019-09-03 12:26:18 -0600179 },
David Brownc3898d62019-08-05 14:20:02 -0600180];
David Brownfe5ab1c2019-08-13 15:35:23 -0600181
182/// Counter for the image number.
183static IMAGE_NUMBER: AtomicUsize = AtomicUsize::new(0);
184
185/// Dump an image if that makes sense. The name is the name of the test
186/// being run. If the MCUBOT_DEBUG_DUMP environment variable contains, in
187/// one of its comma separate strings a substring of this name, then this
188/// image will be dumped. As a special case, we will dump everything if
189/// this environment variable is set to all.
190fn dump_image(image: &Images, name: &str) {
191 if let Ok(request) = env::var("MCUBOOT_DEBUG_DUMP") {
192 if request.split(',').any(|req| {
193 req == "all" || name.contains(req)
194 }) {
195 let count = IMAGE_NUMBER.fetch_add(1, Ordering::SeqCst);
196 let full_name = format!("{}-{:04}", name, count);
197 log::info!("Dump {:?}", full_name);
198 image.debug_dump(&full_name);
199 }
200 }
201}