blob: dc0b0a5d8d6a68d7d28f026ad38da0323095d88d [file] [log] [blame]
David Browndd2b1182017-11-02 15:39:21 -06001//! Core tests
2//!
3//! Run the existing testsuite as a Rust unit test.
4
David Brownc3898d62019-08-05 14:20:02 -06005use bootsim::{
6 DepTest, DepType, UpgradeInfo,
7 ImagesBuilder,
David Brownfe5ab1c2019-08-13 15:35:23 -06008 Images,
David Brownc3898d62019-08-05 14:20:02 -06009 NO_DEPS,
10 testlog,
11};
David Brownfe5ab1c2019-08-13 15:35:23 -060012use std::{
13 env,
14 sync::atomic::{AtomicUsize, Ordering},
15};
David Browndd2b1182017-11-02 15:39:21 -060016
David Brownc3898d62019-08-05 14:20:02 -060017/// A single test, after setting up logging and such. Within the $body,
18/// $arg will be bound to each device.
19macro_rules! test_shell {
20 ($name:ident, $arg: ident, $body:expr) => {
David Browna4167ef2017-11-06 14:30:05 -070021 #[test]
22 fn $name() {
23 testlog::setup();
David Brownc3898d62019-08-05 14:20:02 -060024 ImagesBuilder::each_device(|$arg| {
25 $body;
David Browna4167ef2017-11-06 14:30:05 -070026 });
David Browndd2b1182017-11-02 15:39:21 -060027 }
David Brownc3898d62019-08-05 14:20:02 -060028 }
29}
30
31/// A typical test calls a particular constructor, and runs a given test on
32/// that constructor.
33macro_rules! sim_test {
34 ($name:ident, $maker:ident($($margs:expr),*), $test:ident($($targs:expr),*)) => {
35 test_shell!($name, r, {
36 let image = r.$maker($($margs),*);
David Brownfe5ab1c2019-08-13 15:35:23 -060037 dump_image(&image, stringify!($name));
David Brownc3898d62019-08-05 14:20:02 -060038 assert!(!image.$test($($targs),*));
39 });
David Browna4167ef2017-11-06 14:30:05 -070040 };
David Browndd2b1182017-11-02 15:39:21 -060041}
David Browna4167ef2017-11-06 14:30:05 -070042
David Browneebf5022019-07-30 15:01:07 -060043sim_test!(bad_secondary_slot, make_bad_secondary_slot_image(), run_signfail_upgrade());
David Brownc3898d62019-08-05 14:20:02 -060044sim_test!(norevert_newimage, make_no_upgrade_image(&NO_DEPS), run_norevert_newimage());
45sim_test!(basic_revert, make_image(&NO_DEPS, true), run_basic_revert());
46sim_test!(revert_with_fails, make_image(&NO_DEPS, false), run_revert_with_fails());
47sim_test!(perm_with_fails, make_image(&NO_DEPS, true), run_perm_with_fails());
48sim_test!(perm_with_random_fails, make_image(&NO_DEPS, true), run_perm_with_random_fails(5));
49sim_test!(norevert, make_image(&NO_DEPS, true), run_norevert());
50sim_test!(status_write_fails_complete, make_image(&NO_DEPS, true), run_with_status_fails_complete());
51sim_test!(status_write_fails_with_reset, make_image(&NO_DEPS, true), run_with_status_fails_with_reset());
52
53// Test various combinations of incorrect dependencies.
54test_shell!(dependency_combos, r, {
55 // Only test setups with two images.
56 if r.num_images() != 2 {
57 return;
58 }
59
David Brownfe5ab1c2019-08-13 15:35:23 -060060 for dep in TEST_DEPS {
David Brownc3898d62019-08-05 14:20:02 -060061 let image = r.clone().make_image(&dep, true);
David Brownfe5ab1c2019-08-13 15:35:23 -060062 dump_image(&image, "dependency_combos");
David Brownc3898d62019-08-05 14:20:02 -060063 assert!(!image.run_check_deps(&dep));
64 }
65});
66
67/// These are the variants of dependencies we will test.
68pub static TEST_DEPS: &[DepTest] = &[
69 // First is a sanity test, no dependencies should upgrade.
70 DepTest {
71 depends: [DepType::Nothing, DepType::Nothing],
72 upgrades: [UpgradeInfo::Upgraded, UpgradeInfo::Upgraded],
73 },
74
David Brown18d301f2019-09-03 11:37:39 -060075 // If all of the dependencies are met, we should also upgrade.
Fabio Utzig135f7162019-08-28 11:03:44 -030076 DepTest {
77 depends: [DepType::Correct, DepType::Correct],
78 upgrades: [UpgradeInfo::Upgraded, UpgradeInfo::Upgraded],
79 },
80
David Brown18d301f2019-09-03 11:37:39 -060081 // If none of the dependencies are met, the images should be held.
David Brownc3898d62019-08-05 14:20:02 -060082 DepTest {
83 depends: [DepType::Newer, DepType::Newer],
84 upgrades: [UpgradeInfo::Held, UpgradeInfo::Held],
85 },
David Brown18d301f2019-09-03 11:37:39 -060086
87 // If the first image is not met, we should hold back on the
88 // dependencies (it is not well defined what the correct behavior is
89 // here, it could also be correct to upgrade only the second image).
90 DepTest {
91 depends: [DepType::Newer, DepType::Correct],
92 upgrades: [UpgradeInfo::Held, UpgradeInfo::Held],
93 },
94
95 // Test the variant in the other direction.
96 DepTest {
97 depends: [DepType::Correct, DepType::Newer],
98 upgrades: [UpgradeInfo::Held, UpgradeInfo::Held],
99 },
100
David Brownc3898d62019-08-05 14:20:02 -0600101];
David Brownfe5ab1c2019-08-13 15:35:23 -0600102
103/// Counter for the image number.
104static IMAGE_NUMBER: AtomicUsize = AtomicUsize::new(0);
105
106/// Dump an image if that makes sense. The name is the name of the test
107/// being run. If the MCUBOT_DEBUG_DUMP environment variable contains, in
108/// one of its comma separate strings a substring of this name, then this
109/// image will be dumped. As a special case, we will dump everything if
110/// this environment variable is set to all.
111fn dump_image(image: &Images, name: &str) {
112 if let Ok(request) = env::var("MCUBOOT_DEBUG_DUMP") {
113 if request.split(',').any(|req| {
114 req == "all" || name.contains(req)
115 }) {
116 let count = IMAGE_NUMBER.fetch_add(1, Ordering::SeqCst);
117 let full_name = format!("{}-{:04}", name, count);
118 log::info!("Dump {:?}", full_name);
119 image.debug_dump(&full_name);
120 }
121 }
122}