blob: 10a05da830e66ea6f83a4c2532a39472e49e5fc2 [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,
8 NO_DEPS,
9 testlog,
10};
David Brown297029a2019-08-13 14:29:51 -060011use std::env;
David Browndd2b1182017-11-02 15:39:21 -060012
David Brownc3898d62019-08-05 14:20:02 -060013/// A single test, after setting up logging and such. Within the $body,
14/// $arg will be bound to each device.
15macro_rules! test_shell {
16 ($name:ident, $arg: ident, $body:expr) => {
David Browna4167ef2017-11-06 14:30:05 -070017 #[test]
18 fn $name() {
19 testlog::setup();
David Brownc3898d62019-08-05 14:20:02 -060020 ImagesBuilder::each_device(|$arg| {
21 $body;
David Browna4167ef2017-11-06 14:30:05 -070022 });
David Browndd2b1182017-11-02 15:39:21 -060023 }
David Brownc3898d62019-08-05 14:20:02 -060024 }
25}
26
27/// A typical test calls a particular constructor, and runs a given test on
28/// that constructor.
29macro_rules! sim_test {
30 ($name:ident, $maker:ident($($margs:expr),*), $test:ident($($targs:expr),*)) => {
31 test_shell!($name, r, {
32 let image = r.$maker($($margs),*);
33 assert!(!image.$test($($targs),*));
34 });
David Browna4167ef2017-11-06 14:30:05 -070035 };
David Browndd2b1182017-11-02 15:39:21 -060036}
David Browna4167ef2017-11-06 14:30:05 -070037
David Browneebf5022019-07-30 15:01:07 -060038sim_test!(bad_secondary_slot, make_bad_secondary_slot_image(), run_signfail_upgrade());
David Brownc3898d62019-08-05 14:20:02 -060039sim_test!(norevert_newimage, make_no_upgrade_image(&NO_DEPS), run_norevert_newimage());
40sim_test!(basic_revert, make_image(&NO_DEPS, true), run_basic_revert());
41sim_test!(revert_with_fails, make_image(&NO_DEPS, false), run_revert_with_fails());
42sim_test!(perm_with_fails, make_image(&NO_DEPS, true), run_perm_with_fails());
43sim_test!(perm_with_random_fails, make_image(&NO_DEPS, true), run_perm_with_random_fails(5));
44sim_test!(norevert, make_image(&NO_DEPS, true), run_norevert());
45sim_test!(status_write_fails_complete, make_image(&NO_DEPS, true), run_with_status_fails_complete());
46sim_test!(status_write_fails_with_reset, make_image(&NO_DEPS, true), run_with_status_fails_with_reset());
47
48// Test various combinations of incorrect dependencies.
49test_shell!(dependency_combos, r, {
50 // Only test setups with two images.
51 if r.num_images() != 2 {
52 return;
53 }
54
David Brown297029a2019-08-13 14:29:51 -060055 for (index, dep) in TEST_DEPS.iter().enumerate() {
David Brownc3898d62019-08-05 14:20:02 -060056 let image = r.clone().make_image(&dep, true);
David Brown297029a2019-08-13 14:29:51 -060057
58 if env::var_os("MCUBOOT_DEBUG_DUMP").is_some() {
59 let name = format!("dep-test-{:0>}", index);
60 image.debug_dump(&name);
61 }
David Brownc3898d62019-08-05 14:20:02 -060062 assert!(!image.run_check_deps(&dep));
63 }
64});
65
66/// These are the variants of dependencies we will test.
67pub static TEST_DEPS: &[DepTest] = &[
68 // First is a sanity test, no dependencies should upgrade.
69 DepTest {
70 depends: [DepType::Nothing, DepType::Nothing],
71 upgrades: [UpgradeInfo::Upgraded, UpgradeInfo::Upgraded],
72 },
73
74 // If all of the dependencies are unmet, there should be no upgrades.
75 // TODO: Disabled because it fails.
76 /*
77 DepTest {
78 depends: [DepType::Newer, DepType::Newer],
79 upgrades: [UpgradeInfo::Held, UpgradeInfo::Held],
80 },
81 */
82];