blob: cbf31b401400ff1245fe14e3b703bdf4e687e71a [file] [log] [blame]
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +03001// 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.
Roman Okhrimenko977b3752022-03-31 14:40:48 +030025/// The "$skip_feature" allows to set a feature for which this test will be skipped.
David Brownc3898d62019-08-05 14:20:02 -060026macro_rules! test_shell {
Roman Okhrimenko977b3752022-03-31 14:40:48 +030027 ($name:ident, $arg:ident, $body:expr, $skip_feature:expr) => {
David Browna4167ef2017-11-06 14:30:05 -070028 #[test]
Roman Okhrimenko977b3752022-03-31 14:40:48 +030029 #[cfg_attr(feature = $skip_feature, ignore)]
David Browna4167ef2017-11-06 14:30:05 -070030 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.
Roman Okhrimenko977b3752022-03-31 14:40:48 +030041/// The "$skip_feature" allows to set a feature for which this test will be skipped.
David Brownc3898d62019-08-05 14:20:02 -060042macro_rules! sim_test {
Roman Okhrimenko977b3752022-03-31 14:40:48 +030043 ($name:ident, $maker:ident($($margs:expr),*), $test:ident($($targs:expr),*), $skip_feature:expr) => {
David Brownc3898d62019-08-05 14:20:02 -060044 test_shell!($name, r, {
45 let image = r.$maker($($margs),*);
David Brownfe5ab1c2019-08-13 15:35:23 -060046 dump_image(&image, stringify!($name));
David Brownc3898d62019-08-05 14:20:02 -060047 assert!(!image.$test($($targs),*));
Roman Okhrimenko977b3752022-03-31 14:40:48 +030048 }, $skip_feature);
David Browna4167ef2017-11-06 14:30:05 -070049 };
David Browndd2b1182017-11-02 15:39:21 -060050}
David Browna4167ef2017-11-06 14:30:05 -070051
Roman Okhrimenko977b3752022-03-31 14:40:48 +030052sim_test!(bad_secondary_slot, make_bad_secondary_slot_image(), run_signfail_upgrade(), "");
53sim_test!(secondary_trailer_leftover, make_erased_secondary_image(), run_secondary_leftover_trailer(), "");
54sim_test!(bootstrap, make_bootstrap_image(), run_bootstrap(), "");
55sim_test!(norevert_newimage, make_no_upgrade_image(&NO_DEPS), run_norevert_newimage(), "");
56sim_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(), "");
61sim_test!(status_write_fails_complete, make_image(&NO_DEPS, true), run_with_status_fails_complete(), "");
62
63sim_test!(status_write_fails_with_reset, make_image(&NO_DEPS, true),
64 run_with_status_fails_with_reset(), "swap-status");
65sim_test!(downgrade_prevention, make_image(&REV_DEPS, true), run_nodowngrade(), "");
66
67sim_test!(direct_xip_first, make_no_upgrade_image(&NO_DEPS), run_direct_xip(), "");
68sim_test!(ram_load_first, make_no_upgrade_image(&NO_DEPS), run_ram_load(), "");
David Brownc3898d62019-08-05 14:20:02 -060069
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +030070sim_test!(ram_load_split, make_no_upgrade_image(&NO_DEPS), run_split_ram_load(), "");
David Brownc3898d62019-08-05 14:20:02 -060071// Test various combinations of incorrect dependencies.
72test_shell!(dependency_combos, r, {
73 // Only test setups with two images.
74 if r.num_images() != 2 {
75 return;
76 }
77
David Brownfe5ab1c2019-08-13 15:35:23 -060078 for dep in TEST_DEPS {
David Brownc3898d62019-08-05 14:20:02 -060079 let image = r.clone().make_image(&dep, true);
David Brownfe5ab1c2019-08-13 15:35:23 -060080 dump_image(&image, "dependency_combos");
David Brownc3898d62019-08-05 14:20:02 -060081 assert!(!image.run_check_deps(&dep));
82 }
Roman Okhrimenko977b3752022-03-31 14:40:48 +030083}, "");
David Brownc3898d62019-08-05 14:20:02 -060084
85/// These are the variants of dependencies we will test.
86pub static TEST_DEPS: &[DepTest] = &[
David Browne4576b82019-09-03 12:26:18 -060087 // A sanity test, no dependencies should upgrade.
David Brownc3898d62019-08-05 14:20:02 -060088 DepTest {
89 depends: [DepType::Nothing, DepType::Nothing],
90 upgrades: [UpgradeInfo::Upgraded, UpgradeInfo::Upgraded],
David Brown2ee5f7f2020-01-13 14:04:01 -070091 downgrade: false,
David Brownc3898d62019-08-05 14:20:02 -060092 },
93
David Brown18d301f2019-09-03 11:37:39 -060094 // If all of the dependencies are met, we should also upgrade.
Fabio Utzig135f7162019-08-28 11:03:44 -030095 DepTest {
96 depends: [DepType::Correct, DepType::Correct],
97 upgrades: [UpgradeInfo::Upgraded, UpgradeInfo::Upgraded],
David Brown2ee5f7f2020-01-13 14:04:01 -070098 downgrade: false,
Fabio Utzig135f7162019-08-28 11:03:44 -030099 },
100
David Brown18d301f2019-09-03 11:37:39 -0600101 // If none of the dependencies are met, the images should be held.
David Brownc3898d62019-08-05 14:20:02 -0600102 DepTest {
103 depends: [DepType::Newer, DepType::Newer],
104 upgrades: [UpgradeInfo::Held, UpgradeInfo::Held],
David Brown2ee5f7f2020-01-13 14:04:01 -0700105 downgrade: false,
David Brownc3898d62019-08-05 14:20:02 -0600106 },
David Brown18d301f2019-09-03 11:37:39 -0600107
108 // If the first image is not met, we should hold back on the
109 // dependencies (it is not well defined what the correct behavior is
110 // here, it could also be correct to upgrade only the second image).
111 DepTest {
112 depends: [DepType::Newer, DepType::Correct],
113 upgrades: [UpgradeInfo::Held, UpgradeInfo::Held],
David Brown2ee5f7f2020-01-13 14:04:01 -0700114 downgrade: false,
David Brown18d301f2019-09-03 11:37:39 -0600115 },
116
117 // Test the variant in the other direction.
118 DepTest {
119 depends: [DepType::Correct, DepType::Newer],
120 upgrades: [UpgradeInfo::Held, UpgradeInfo::Held],
David Brown2ee5f7f2020-01-13 14:04:01 -0700121 downgrade: false,
David Brown18d301f2019-09-03 11:37:39 -0600122 },
123
David Browne4576b82019-09-03 12:26:18 -0600124 // Test where only the first image is upgraded, and there are no
125 // dependencies.
126 DepTest {
127 depends: [DepType::Nothing, DepType::NoUpgrade],
128 upgrades: [UpgradeInfo::Upgraded, UpgradeInfo::Held],
David Brown2ee5f7f2020-01-13 14:04:01 -0700129 downgrade: false,
David Browne4576b82019-09-03 12:26:18 -0600130 },
131
132 // Test one image with a valid dependency on the first image.
133 DepTest {
134 depends: [DepType::OldCorrect, DepType::NoUpgrade],
135 upgrades: [UpgradeInfo::Upgraded, UpgradeInfo::Held],
David Brown2ee5f7f2020-01-13 14:04:01 -0700136 downgrade: false,
David Browne4576b82019-09-03 12:26:18 -0600137 },
138
139 // Test one image with an invalid dependency on the first image.
140 DepTest {
141 depends: [DepType::Newer, DepType::NoUpgrade],
142 upgrades: [UpgradeInfo::Held, UpgradeInfo::Held],
David Brown2ee5f7f2020-01-13 14:04:01 -0700143 downgrade: false,
David Browne4576b82019-09-03 12:26:18 -0600144 },
145
146 // Test where only the second image is upgraded, and there are no
147 // dependencies.
148 DepTest {
149 depends: [DepType::NoUpgrade, DepType::Nothing],
150 upgrades: [UpgradeInfo::Held, UpgradeInfo::Upgraded],
David Brown2ee5f7f2020-01-13 14:04:01 -0700151 downgrade: false,
David Browne4576b82019-09-03 12:26:18 -0600152 },
153
154 // Test one image with a valid dependency on the second image.
155 DepTest {
156 depends: [DepType::NoUpgrade, DepType::OldCorrect],
157 upgrades: [UpgradeInfo::Held, UpgradeInfo::Upgraded],
David Brown2ee5f7f2020-01-13 14:04:01 -0700158 downgrade: false,
David Browne4576b82019-09-03 12:26:18 -0600159 },
160
161 // Test one image with an invalid dependency on the second image.
162 DepTest {
163 depends: [DepType::NoUpgrade, DepType::Newer],
164 upgrades: [UpgradeInfo::Held, UpgradeInfo::Held],
David Brown2ee5f7f2020-01-13 14:04:01 -0700165 downgrade: false,
David Browne4576b82019-09-03 12:26:18 -0600166 },
David Brownc3898d62019-08-05 14:20:02 -0600167];
David Brownfe5ab1c2019-08-13 15:35:23 -0600168
169/// Counter for the image number.
170static IMAGE_NUMBER: AtomicUsize = AtomicUsize::new(0);
171
172/// Dump an image if that makes sense. The name is the name of the test
173/// being run. If the MCUBOT_DEBUG_DUMP environment variable contains, in
174/// one of its comma separate strings a substring of this name, then this
175/// image will be dumped. As a special case, we will dump everything if
176/// this environment variable is set to all.
177fn dump_image(image: &Images, name: &str) {
178 if let Ok(request) = env::var("MCUBOOT_DEBUG_DUMP") {
179 if request.split(',').any(|req| {
180 req == "all" || name.contains(req)
181 }) {
182 let count = IMAGE_NUMBER.fetch_add(1, Ordering::SeqCst);
183 let full_name = format!("{}-{:04}", name, count);
184 log::info!("Dump {:?}", full_name);
185 image.debug_dump(&full_name);
186 }
187 }
188}