blob: d26fa70d7bc10d7ec05d986b314a53912d23c0e4 [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
3//
4// SPDX-License-Identifier: Apache-2.0
5
David Browndd2b1182017-11-02 15:39:21 -06006//! Core tests
7//!
8//! Run the existing testsuite as a Rust unit test.
9
David Brownc3898d62019-08-05 14:20:02 -060010use bootsim::{
11 DepTest, DepType, UpgradeInfo,
12 ImagesBuilder,
David Brownfe5ab1c2019-08-13 15:35:23 -060013 Images,
David Brownc3898d62019-08-05 14:20:02 -060014 NO_DEPS,
David Brown2ee5f7f2020-01-13 14:04:01 -070015 REV_DEPS,
David Brownc3898d62019-08-05 14:20:02 -060016 testlog,
17};
David Brownfe5ab1c2019-08-13 15:35:23 -060018use std::{
19 env,
20 sync::atomic::{AtomicUsize, Ordering},
21};
David Browndd2b1182017-11-02 15:39:21 -060022
David Brownc3898d62019-08-05 14:20:02 -060023/// A single test, after setting up logging and such. Within the $body,
24/// $arg will be bound to each device.
25macro_rules! test_shell {
26 ($name:ident, $arg: ident, $body:expr) => {
David Browna4167ef2017-11-06 14:30:05 -070027 #[test]
28 fn $name() {
29 testlog::setup();
David Brownc3898d62019-08-05 14:20:02 -060030 ImagesBuilder::each_device(|$arg| {
31 $body;
David Browna4167ef2017-11-06 14:30:05 -070032 });
David Browndd2b1182017-11-02 15:39:21 -060033 }
David Brownc3898d62019-08-05 14:20:02 -060034 }
35}
36
37/// A typical test calls a particular constructor, and runs a given test on
38/// that constructor.
39macro_rules! sim_test {
40 ($name:ident, $maker:ident($($margs:expr),*), $test:ident($($targs:expr),*)) => {
41 test_shell!($name, r, {
42 let image = r.$maker($($margs),*);
David Brownfe5ab1c2019-08-13 15:35:23 -060043 dump_image(&image, stringify!($name));
David Brownc3898d62019-08-05 14:20:02 -060044 assert!(!image.$test($($targs),*));
45 });
David Browna4167ef2017-11-06 14:30:05 -070046 };
David Browndd2b1182017-11-02 15:39:21 -060047}
David Browna4167ef2017-11-06 14:30:05 -070048
David Browneebf5022019-07-30 15:01:07 -060049sim_test!(bad_secondary_slot, make_bad_secondary_slot_image(), run_signfail_upgrade());
Fabio Utzig2c3be5c2020-07-09 19:54:45 -030050sim_test!(secondary_trailer_leftover, make_erased_secondary_image(), run_secondary_leftover_trailer());
Fabio Utzigd0157342020-10-02 15:22:11 -030051sim_test!(bootstrap, make_bootstrap_image(), run_bootstrap());
David Brownc3898d62019-08-05 14:20:02 -060052sim_test!(norevert_newimage, make_no_upgrade_image(&NO_DEPS), run_norevert_newimage());
53sim_test!(basic_revert, make_image(&NO_DEPS, true), run_basic_revert());
54sim_test!(revert_with_fails, make_image(&NO_DEPS, false), run_revert_with_fails());
55sim_test!(perm_with_fails, make_image(&NO_DEPS, true), run_perm_with_fails());
56sim_test!(perm_with_random_fails, make_image(&NO_DEPS, true), run_perm_with_random_fails(5));
57sim_test!(norevert, make_image(&NO_DEPS, true), run_norevert());
58sim_test!(status_write_fails_complete, make_image(&NO_DEPS, true), run_with_status_fails_complete());
59sim_test!(status_write_fails_with_reset, make_image(&NO_DEPS, true), run_with_status_fails_with_reset());
David Brown2ee5f7f2020-01-13 14:04:01 -070060sim_test!(downgrade_prevention, make_image(&REV_DEPS, true), run_nodowngrade());
David Brownc3898d62019-08-05 14:20:02 -060061
David Brown0dfb8102021-06-03 15:29:11 -060062sim_test!(direct_xip_first, make_no_upgrade_image(&NO_DEPS), run_direct_xip());
David Brown8a4e23b2021-06-11 10:29:01 -060063sim_test!(ram_load_first, make_no_upgrade_image(&NO_DEPS), run_ram_load());
Raef Coles3fd3ecc2021-10-15 11:14:12 +010064sim_test!(ram_load_split, make_no_upgrade_image(&NO_DEPS), run_split_ram_load());
David Brown0dfb8102021-06-03 15:29:11 -060065
David Brownc3898d62019-08-05 14:20:02 -060066// Test various combinations of incorrect dependencies.
67test_shell!(dependency_combos, r, {
68 // Only test setups with two images.
69 if r.num_images() != 2 {
70 return;
71 }
72
David Brownfe5ab1c2019-08-13 15:35:23 -060073 for dep in TEST_DEPS {
David Brownc3898d62019-08-05 14:20:02 -060074 let image = r.clone().make_image(&dep, true);
David Brownfe5ab1c2019-08-13 15:35:23 -060075 dump_image(&image, "dependency_combos");
David Brownc3898d62019-08-05 14:20:02 -060076 assert!(!image.run_check_deps(&dep));
77 }
78});
79
80/// These are the variants of dependencies we will test.
81pub static TEST_DEPS: &[DepTest] = &[
David Browne4576b82019-09-03 12:26:18 -060082 // A sanity test, no dependencies should upgrade.
David Brownc3898d62019-08-05 14:20:02 -060083 DepTest {
84 depends: [DepType::Nothing, DepType::Nothing],
85 upgrades: [UpgradeInfo::Upgraded, UpgradeInfo::Upgraded],
David Brown2ee5f7f2020-01-13 14:04:01 -070086 downgrade: false,
David Brownc3898d62019-08-05 14:20:02 -060087 },
88
David Brown18d301f2019-09-03 11:37:39 -060089 // If all of the dependencies are met, we should also upgrade.
Fabio Utzig135f7162019-08-28 11:03:44 -030090 DepTest {
91 depends: [DepType::Correct, DepType::Correct],
92 upgrades: [UpgradeInfo::Upgraded, UpgradeInfo::Upgraded],
David Brown2ee5f7f2020-01-13 14:04:01 -070093 downgrade: false,
Fabio Utzig135f7162019-08-28 11:03:44 -030094 },
95
David Brown18d301f2019-09-03 11:37:39 -060096 // If none of the dependencies are met, the images should be held.
David Brownc3898d62019-08-05 14:20:02 -060097 DepTest {
98 depends: [DepType::Newer, DepType::Newer],
99 upgrades: [UpgradeInfo::Held, UpgradeInfo::Held],
David Brown2ee5f7f2020-01-13 14:04:01 -0700100 downgrade: false,
David Brownc3898d62019-08-05 14:20:02 -0600101 },
David Brown18d301f2019-09-03 11:37:39 -0600102
103 // If the first image is not met, we should hold back on the
104 // dependencies (it is not well defined what the correct behavior is
105 // here, it could also be correct to upgrade only the second image).
106 DepTest {
107 depends: [DepType::Newer, DepType::Correct],
108 upgrades: [UpgradeInfo::Held, UpgradeInfo::Held],
David Brown2ee5f7f2020-01-13 14:04:01 -0700109 downgrade: false,
David Brown18d301f2019-09-03 11:37:39 -0600110 },
111
112 // Test the variant in the other direction.
113 DepTest {
114 depends: [DepType::Correct, DepType::Newer],
115 upgrades: [UpgradeInfo::Held, UpgradeInfo::Held],
David Brown2ee5f7f2020-01-13 14:04:01 -0700116 downgrade: false,
David Brown18d301f2019-09-03 11:37:39 -0600117 },
118
David Browne4576b82019-09-03 12:26:18 -0600119 // Test where only the first image is upgraded, and there are no
120 // dependencies.
121 DepTest {
122 depends: [DepType::Nothing, DepType::NoUpgrade],
123 upgrades: [UpgradeInfo::Upgraded, UpgradeInfo::Held],
David Brown2ee5f7f2020-01-13 14:04:01 -0700124 downgrade: false,
David Browne4576b82019-09-03 12:26:18 -0600125 },
126
127 // Test one image with a valid dependency on the first image.
128 DepTest {
129 depends: [DepType::OldCorrect, DepType::NoUpgrade],
130 upgrades: [UpgradeInfo::Upgraded, UpgradeInfo::Held],
David Brown2ee5f7f2020-01-13 14:04:01 -0700131 downgrade: false,
David Browne4576b82019-09-03 12:26:18 -0600132 },
133
134 // Test one image with an invalid dependency on the first image.
135 DepTest {
136 depends: [DepType::Newer, DepType::NoUpgrade],
137 upgrades: [UpgradeInfo::Held, UpgradeInfo::Held],
David Brown2ee5f7f2020-01-13 14:04:01 -0700138 downgrade: false,
David Browne4576b82019-09-03 12:26:18 -0600139 },
140
141 // Test where only the second image is upgraded, and there are no
142 // dependencies.
143 DepTest {
144 depends: [DepType::NoUpgrade, DepType::Nothing],
145 upgrades: [UpgradeInfo::Held, UpgradeInfo::Upgraded],
David Brown2ee5f7f2020-01-13 14:04:01 -0700146 downgrade: false,
David Browne4576b82019-09-03 12:26:18 -0600147 },
148
149 // Test one image with a valid dependency on the second image.
150 DepTest {
151 depends: [DepType::NoUpgrade, DepType::OldCorrect],
152 upgrades: [UpgradeInfo::Held, UpgradeInfo::Upgraded],
David Brown2ee5f7f2020-01-13 14:04:01 -0700153 downgrade: false,
David Browne4576b82019-09-03 12:26:18 -0600154 },
155
156 // Test one image with an invalid dependency on the second image.
157 DepTest {
158 depends: [DepType::NoUpgrade, DepType::Newer],
159 upgrades: [UpgradeInfo::Held, UpgradeInfo::Held],
David Brown2ee5f7f2020-01-13 14:04:01 -0700160 downgrade: false,
David Browne4576b82019-09-03 12:26:18 -0600161 },
David Brownc3898d62019-08-05 14:20:02 -0600162];
David Brownfe5ab1c2019-08-13 15:35:23 -0600163
164/// Counter for the image number.
165static IMAGE_NUMBER: AtomicUsize = AtomicUsize::new(0);
166
167/// Dump an image if that makes sense. The name is the name of the test
168/// being run. If the MCUBOT_DEBUG_DUMP environment variable contains, in
169/// one of its comma separate strings a substring of this name, then this
170/// image will be dumped. As a special case, we will dump everything if
171/// this environment variable is set to all.
172fn dump_image(image: &Images, name: &str) {
173 if let Ok(request) = env::var("MCUBOOT_DEBUG_DUMP") {
174 if request.split(',').any(|req| {
175 req == "all" || name.contains(req)
176 }) {
177 let count = IMAGE_NUMBER.fetch_add(1, Ordering::SeqCst);
178 let full_name = format!("{}-{:04}", name, count);
179 log::info!("Dump {:?}", full_name);
180 image.debug_dump(&full_name);
181 }
182 }
183}