blob: 654d8fdffd4d85990743969605ff51469c5cd9e5 [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
Fabio Utzig135f7162019-08-28 11:03:44 -030075 DepTest {
76 depends: [DepType::Correct, DepType::Correct],
77 upgrades: [UpgradeInfo::Upgraded, UpgradeInfo::Upgraded],
78 },
79
David Brownc3898d62019-08-05 14:20:02 -060080 DepTest {
81 depends: [DepType::Newer, DepType::Newer],
82 upgrades: [UpgradeInfo::Held, UpgradeInfo::Held],
83 },
David Brownc3898d62019-08-05 14:20:02 -060084];
David Brownfe5ab1c2019-08-13 15:35:23 -060085
86/// Counter for the image number.
87static IMAGE_NUMBER: AtomicUsize = AtomicUsize::new(0);
88
89/// Dump an image if that makes sense. The name is the name of the test
90/// being run. If the MCUBOT_DEBUG_DUMP environment variable contains, in
91/// one of its comma separate strings a substring of this name, then this
92/// image will be dumped. As a special case, we will dump everything if
93/// this environment variable is set to all.
94fn dump_image(image: &Images, name: &str) {
95 if let Ok(request) = env::var("MCUBOOT_DEBUG_DUMP") {
96 if request.split(',').any(|req| {
97 req == "all" || name.contains(req)
98 }) {
99 let count = IMAGE_NUMBER.fetch_add(1, Ordering::SeqCst);
100 let full_name = format!("{}-{:04}", name, count);
101 log::info!("Dump {:?}", full_name);
102 image.debug_dump(&full_name);
103 }
104 }
105}