blob: 86736b37ed31b3447f2763e0fbd79afba9c00ade [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,
15 testlog,
16};
David Brownfe5ab1c2019-08-13 15:35:23 -060017use std::{
18 env,
19 sync::atomic::{AtomicUsize, Ordering},
20};
David Browndd2b1182017-11-02 15:39:21 -060021
David Brownc3898d62019-08-05 14:20:02 -060022/// A single test, after setting up logging and such. Within the $body,
23/// $arg will be bound to each device.
24macro_rules! test_shell {
25 ($name:ident, $arg: ident, $body:expr) => {
David Browna4167ef2017-11-06 14:30:05 -070026 #[test]
27 fn $name() {
28 testlog::setup();
David Brownc3898d62019-08-05 14:20:02 -060029 ImagesBuilder::each_device(|$arg| {
30 $body;
David Browna4167ef2017-11-06 14:30:05 -070031 });
David Browndd2b1182017-11-02 15:39:21 -060032 }
David Brownc3898d62019-08-05 14:20:02 -060033 }
34}
35
36/// A typical test calls a particular constructor, and runs a given test on
37/// that constructor.
38macro_rules! sim_test {
39 ($name:ident, $maker:ident($($margs:expr),*), $test:ident($($targs:expr),*)) => {
40 test_shell!($name, r, {
41 let image = r.$maker($($margs),*);
David Brownfe5ab1c2019-08-13 15:35:23 -060042 dump_image(&image, stringify!($name));
David Brownc3898d62019-08-05 14:20:02 -060043 assert!(!image.$test($($targs),*));
44 });
David Browna4167ef2017-11-06 14:30:05 -070045 };
David Browndd2b1182017-11-02 15:39:21 -060046}
David Browna4167ef2017-11-06 14:30:05 -070047
David Browneebf5022019-07-30 15:01:07 -060048sim_test!(bad_secondary_slot, make_bad_secondary_slot_image(), run_signfail_upgrade());
David Brownc3898d62019-08-05 14:20:02 -060049sim_test!(norevert_newimage, make_no_upgrade_image(&NO_DEPS), run_norevert_newimage());
50sim_test!(basic_revert, make_image(&NO_DEPS, true), run_basic_revert());
51sim_test!(revert_with_fails, make_image(&NO_DEPS, false), run_revert_with_fails());
52sim_test!(perm_with_fails, make_image(&NO_DEPS, true), run_perm_with_fails());
53sim_test!(perm_with_random_fails, make_image(&NO_DEPS, true), run_perm_with_random_fails(5));
54sim_test!(norevert, make_image(&NO_DEPS, true), run_norevert());
55sim_test!(status_write_fails_complete, make_image(&NO_DEPS, true), run_with_status_fails_complete());
56sim_test!(status_write_fails_with_reset, make_image(&NO_DEPS, true), run_with_status_fails_with_reset());
57
58// Test various combinations of incorrect dependencies.
59test_shell!(dependency_combos, r, {
60 // Only test setups with two images.
61 if r.num_images() != 2 {
62 return;
63 }
64
David Brownfe5ab1c2019-08-13 15:35:23 -060065 for dep in TEST_DEPS {
David Brownc3898d62019-08-05 14:20:02 -060066 let image = r.clone().make_image(&dep, true);
David Brownfe5ab1c2019-08-13 15:35:23 -060067 dump_image(&image, "dependency_combos");
David Brownc3898d62019-08-05 14:20:02 -060068 assert!(!image.run_check_deps(&dep));
69 }
70});
71
72/// These are the variants of dependencies we will test.
73pub static TEST_DEPS: &[DepTest] = &[
David Browne4576b82019-09-03 12:26:18 -060074 // A sanity test, no dependencies should upgrade.
David Brownc3898d62019-08-05 14:20:02 -060075 DepTest {
76 depends: [DepType::Nothing, DepType::Nothing],
77 upgrades: [UpgradeInfo::Upgraded, UpgradeInfo::Upgraded],
78 },
79
David Brown18d301f2019-09-03 11:37:39 -060080 // If all of the dependencies are met, we should also upgrade.
Fabio Utzig135f7162019-08-28 11:03:44 -030081 DepTest {
82 depends: [DepType::Correct, DepType::Correct],
83 upgrades: [UpgradeInfo::Upgraded, UpgradeInfo::Upgraded],
84 },
85
David Brown18d301f2019-09-03 11:37:39 -060086 // If none of the dependencies are met, the images should be held.
David Brownc3898d62019-08-05 14:20:02 -060087 DepTest {
88 depends: [DepType::Newer, DepType::Newer],
89 upgrades: [UpgradeInfo::Held, UpgradeInfo::Held],
90 },
David Brown18d301f2019-09-03 11:37:39 -060091
92 // If the first image is not met, we should hold back on the
93 // dependencies (it is not well defined what the correct behavior is
94 // here, it could also be correct to upgrade only the second image).
95 DepTest {
96 depends: [DepType::Newer, DepType::Correct],
97 upgrades: [UpgradeInfo::Held, UpgradeInfo::Held],
98 },
99
100 // Test the variant in the other direction.
101 DepTest {
102 depends: [DepType::Correct, DepType::Newer],
103 upgrades: [UpgradeInfo::Held, UpgradeInfo::Held],
104 },
105
David Browne4576b82019-09-03 12:26:18 -0600106 // Test where only the first image is upgraded, and there are no
107 // dependencies.
108 DepTest {
109 depends: [DepType::Nothing, DepType::NoUpgrade],
110 upgrades: [UpgradeInfo::Upgraded, UpgradeInfo::Held],
111 },
112
113 // Test one image with a valid dependency on the first image.
114 DepTest {
115 depends: [DepType::OldCorrect, DepType::NoUpgrade],
116 upgrades: [UpgradeInfo::Upgraded, UpgradeInfo::Held],
117 },
118
119 // Test one image with an invalid dependency on the first image.
120 DepTest {
121 depends: [DepType::Newer, DepType::NoUpgrade],
122 upgrades: [UpgradeInfo::Held, UpgradeInfo::Held],
123 },
124
125 // Test where only the second image is upgraded, and there are no
126 // dependencies.
127 DepTest {
128 depends: [DepType::NoUpgrade, DepType::Nothing],
129 upgrades: [UpgradeInfo::Held, UpgradeInfo::Upgraded],
130 },
131
132 // Test one image with a valid dependency on the second image.
133 DepTest {
134 depends: [DepType::NoUpgrade, DepType::OldCorrect],
135 upgrades: [UpgradeInfo::Held, UpgradeInfo::Upgraded],
136 },
137
138 // Test one image with an invalid dependency on the second image.
139 DepTest {
140 depends: [DepType::NoUpgrade, DepType::Newer],
141 upgrades: [UpgradeInfo::Held, UpgradeInfo::Held],
142 },
David Brownc3898d62019-08-05 14:20:02 -0600143];
David Brownfe5ab1c2019-08-13 15:35:23 -0600144
145/// Counter for the image number.
146static IMAGE_NUMBER: AtomicUsize = AtomicUsize::new(0);
147
148/// Dump an image if that makes sense. The name is the name of the test
149/// being run. If the MCUBOT_DEBUG_DUMP environment variable contains, in
150/// one of its comma separate strings a substring of this name, then this
151/// image will be dumped. As a special case, we will dump everything if
152/// this environment variable is set to all.
153fn dump_image(image: &Images, name: &str) {
154 if let Ok(request) = env::var("MCUBOOT_DEBUG_DUMP") {
155 if request.split(',').any(|req| {
156 req == "all" || name.contains(req)
157 }) {
158 let count = IMAGE_NUMBER.fetch_add(1, Ordering::SeqCst);
159 let full_name = format!("{}-{:04}", name, count);
160 log::info!("Dump {:?}", full_name);
161 image.debug_dump(&full_name);
162 }
163 }
164}