blob: 07a844938e9b226e0b1f95b179c9e2d6af802456 [file] [log] [blame]
David Browne2acfae2020-01-21 16:45:01 -07001// Copyright (c) 2017-2019 Linaro LTD
2// 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());
David Brownc3898d62019-08-05 14:20:02 -060050sim_test!(norevert_newimage, make_no_upgrade_image(&NO_DEPS), run_norevert_newimage());
51sim_test!(basic_revert, make_image(&NO_DEPS, true), run_basic_revert());
52sim_test!(revert_with_fails, make_image(&NO_DEPS, false), run_revert_with_fails());
53sim_test!(perm_with_fails, make_image(&NO_DEPS, true), run_perm_with_fails());
54sim_test!(perm_with_random_fails, make_image(&NO_DEPS, true), run_perm_with_random_fails(5));
55sim_test!(norevert, make_image(&NO_DEPS, true), run_norevert());
56sim_test!(status_write_fails_complete, make_image(&NO_DEPS, true), run_with_status_fails_complete());
57sim_test!(status_write_fails_with_reset, make_image(&NO_DEPS, true), run_with_status_fails_with_reset());
David Brown2ee5f7f2020-01-13 14:04:01 -070058sim_test!(downgrade_prevention, make_image(&REV_DEPS, true), run_nodowngrade());
David Brownc3898d62019-08-05 14:20:02 -060059
60// Test various combinations of incorrect dependencies.
61test_shell!(dependency_combos, r, {
62 // Only test setups with two images.
63 if r.num_images() != 2 {
64 return;
65 }
66
David Brownfe5ab1c2019-08-13 15:35:23 -060067 for dep in TEST_DEPS {
David Brownc3898d62019-08-05 14:20:02 -060068 let image = r.clone().make_image(&dep, true);
David Brownfe5ab1c2019-08-13 15:35:23 -060069 dump_image(&image, "dependency_combos");
David Brownc3898d62019-08-05 14:20:02 -060070 assert!(!image.run_check_deps(&dep));
71 }
72});
73
74/// These are the variants of dependencies we will test.
75pub static TEST_DEPS: &[DepTest] = &[
David Browne4576b82019-09-03 12:26:18 -060076 // A sanity test, no dependencies should upgrade.
David Brownc3898d62019-08-05 14:20:02 -060077 DepTest {
78 depends: [DepType::Nothing, DepType::Nothing],
79 upgrades: [UpgradeInfo::Upgraded, UpgradeInfo::Upgraded],
David Brown2ee5f7f2020-01-13 14:04:01 -070080 downgrade: false,
David Brownc3898d62019-08-05 14:20:02 -060081 },
82
David Brown18d301f2019-09-03 11:37:39 -060083 // If all of the dependencies are met, we should also upgrade.
Fabio Utzig135f7162019-08-28 11:03:44 -030084 DepTest {
85 depends: [DepType::Correct, DepType::Correct],
86 upgrades: [UpgradeInfo::Upgraded, UpgradeInfo::Upgraded],
David Brown2ee5f7f2020-01-13 14:04:01 -070087 downgrade: false,
Fabio Utzig135f7162019-08-28 11:03:44 -030088 },
89
David Brown18d301f2019-09-03 11:37:39 -060090 // If none of the dependencies are met, the images should be held.
David Brownc3898d62019-08-05 14:20:02 -060091 DepTest {
92 depends: [DepType::Newer, DepType::Newer],
93 upgrades: [UpgradeInfo::Held, UpgradeInfo::Held],
David Brown2ee5f7f2020-01-13 14:04:01 -070094 downgrade: false,
David Brownc3898d62019-08-05 14:20:02 -060095 },
David Brown18d301f2019-09-03 11:37:39 -060096
97 // If the first image is not met, we should hold back on the
98 // dependencies (it is not well defined what the correct behavior is
99 // here, it could also be correct to upgrade only the second image).
100 DepTest {
101 depends: [DepType::Newer, DepType::Correct],
102 upgrades: [UpgradeInfo::Held, UpgradeInfo::Held],
David Brown2ee5f7f2020-01-13 14:04:01 -0700103 downgrade: false,
David Brown18d301f2019-09-03 11:37:39 -0600104 },
105
106 // Test the variant in the other direction.
107 DepTest {
108 depends: [DepType::Correct, DepType::Newer],
109 upgrades: [UpgradeInfo::Held, UpgradeInfo::Held],
David Brown2ee5f7f2020-01-13 14:04:01 -0700110 downgrade: false,
David Brown18d301f2019-09-03 11:37:39 -0600111 },
112
David Browne4576b82019-09-03 12:26:18 -0600113 // Test where only the first image is upgraded, and there are no
114 // dependencies.
115 DepTest {
116 depends: [DepType::Nothing, DepType::NoUpgrade],
117 upgrades: [UpgradeInfo::Upgraded, UpgradeInfo::Held],
David Brown2ee5f7f2020-01-13 14:04:01 -0700118 downgrade: false,
David Browne4576b82019-09-03 12:26:18 -0600119 },
120
121 // Test one image with a valid dependency on the first image.
122 DepTest {
123 depends: [DepType::OldCorrect, DepType::NoUpgrade],
124 upgrades: [UpgradeInfo::Upgraded, UpgradeInfo::Held],
David Brown2ee5f7f2020-01-13 14:04:01 -0700125 downgrade: false,
David Browne4576b82019-09-03 12:26:18 -0600126 },
127
128 // Test one image with an invalid dependency on the first image.
129 DepTest {
130 depends: [DepType::Newer, DepType::NoUpgrade],
131 upgrades: [UpgradeInfo::Held, UpgradeInfo::Held],
David Brown2ee5f7f2020-01-13 14:04:01 -0700132 downgrade: false,
David Browne4576b82019-09-03 12:26:18 -0600133 },
134
135 // Test where only the second image is upgraded, and there are no
136 // dependencies.
137 DepTest {
138 depends: [DepType::NoUpgrade, DepType::Nothing],
139 upgrades: [UpgradeInfo::Held, UpgradeInfo::Upgraded],
David Brown2ee5f7f2020-01-13 14:04:01 -0700140 downgrade: false,
David Browne4576b82019-09-03 12:26:18 -0600141 },
142
143 // Test one image with a valid dependency on the second image.
144 DepTest {
145 depends: [DepType::NoUpgrade, DepType::OldCorrect],
146 upgrades: [UpgradeInfo::Held, UpgradeInfo::Upgraded],
David Brown2ee5f7f2020-01-13 14:04:01 -0700147 downgrade: false,
David Browne4576b82019-09-03 12:26:18 -0600148 },
149
150 // Test one image with an invalid dependency on the second image.
151 DepTest {
152 depends: [DepType::NoUpgrade, DepType::Newer],
153 upgrades: [UpgradeInfo::Held, UpgradeInfo::Held],
David Brown2ee5f7f2020-01-13 14:04:01 -0700154 downgrade: false,
David Browne4576b82019-09-03 12:26:18 -0600155 },
David Brownc3898d62019-08-05 14:20:02 -0600156];
David Brownfe5ab1c2019-08-13 15:35:23 -0600157
158/// Counter for the image number.
159static IMAGE_NUMBER: AtomicUsize = AtomicUsize::new(0);
160
161/// Dump an image if that makes sense. The name is the name of the test
162/// being run. If the MCUBOT_DEBUG_DUMP environment variable contains, in
163/// one of its comma separate strings a substring of this name, then this
164/// image will be dumped. As a special case, we will dump everything if
165/// this environment variable is set to all.
166fn dump_image(image: &Images, name: &str) {
167 if let Ok(request) = env::var("MCUBOOT_DEBUG_DUMP") {
168 if request.split(',').any(|req| {
169 req == "all" || name.contains(req)
170 }) {
171 let count = IMAGE_NUMBER.fetch_add(1, Ordering::SeqCst);
172 let full_name = format!("{}-{:04}", name, count);
173 log::info!("Dump {:?}", full_name);
174 image.debug_dump(&full_name);
175 }
176 }
177}