David Brown | dd2b118 | 2017-11-02 15:39:21 -0600 | [diff] [blame] | 1 | //! Core tests |
| 2 | //! |
| 3 | //! Run the existing testsuite as a Rust unit test. |
| 4 | |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 5 | use bootsim::{ |
| 6 | DepTest, DepType, UpgradeInfo, |
| 7 | ImagesBuilder, |
| 8 | NO_DEPS, |
| 9 | testlog, |
| 10 | }; |
David Brown | 297029a | 2019-08-13 14:29:51 -0600 | [diff] [blame^] | 11 | use std::env; |
David Brown | dd2b118 | 2017-11-02 15:39:21 -0600 | [diff] [blame] | 12 | |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 13 | /// A single test, after setting up logging and such. Within the $body, |
| 14 | /// $arg will be bound to each device. |
| 15 | macro_rules! test_shell { |
| 16 | ($name:ident, $arg: ident, $body:expr) => { |
David Brown | a4167ef | 2017-11-06 14:30:05 -0700 | [diff] [blame] | 17 | #[test] |
| 18 | fn $name() { |
| 19 | testlog::setup(); |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 20 | ImagesBuilder::each_device(|$arg| { |
| 21 | $body; |
David Brown | a4167ef | 2017-11-06 14:30:05 -0700 | [diff] [blame] | 22 | }); |
David Brown | dd2b118 | 2017-11-02 15:39:21 -0600 | [diff] [blame] | 23 | } |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 24 | } |
| 25 | } |
| 26 | |
| 27 | /// A typical test calls a particular constructor, and runs a given test on |
| 28 | /// that constructor. |
| 29 | macro_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 Brown | a4167ef | 2017-11-06 14:30:05 -0700 | [diff] [blame] | 35 | }; |
David Brown | dd2b118 | 2017-11-02 15:39:21 -0600 | [diff] [blame] | 36 | } |
David Brown | a4167ef | 2017-11-06 14:30:05 -0700 | [diff] [blame] | 37 | |
David Brown | eebf502 | 2019-07-30 15:01:07 -0600 | [diff] [blame] | 38 | sim_test!(bad_secondary_slot, make_bad_secondary_slot_image(), run_signfail_upgrade()); |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 39 | sim_test!(norevert_newimage, make_no_upgrade_image(&NO_DEPS), run_norevert_newimage()); |
| 40 | sim_test!(basic_revert, make_image(&NO_DEPS, true), run_basic_revert()); |
| 41 | sim_test!(revert_with_fails, make_image(&NO_DEPS, false), run_revert_with_fails()); |
| 42 | sim_test!(perm_with_fails, make_image(&NO_DEPS, true), run_perm_with_fails()); |
| 43 | sim_test!(perm_with_random_fails, make_image(&NO_DEPS, true), run_perm_with_random_fails(5)); |
| 44 | sim_test!(norevert, make_image(&NO_DEPS, true), run_norevert()); |
| 45 | sim_test!(status_write_fails_complete, make_image(&NO_DEPS, true), run_with_status_fails_complete()); |
| 46 | sim_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. |
| 49 | test_shell!(dependency_combos, r, { |
| 50 | // Only test setups with two images. |
| 51 | if r.num_images() != 2 { |
| 52 | return; |
| 53 | } |
| 54 | |
David Brown | 297029a | 2019-08-13 14:29:51 -0600 | [diff] [blame^] | 55 | for (index, dep) in TEST_DEPS.iter().enumerate() { |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 56 | let image = r.clone().make_image(&dep, true); |
David Brown | 297029a | 2019-08-13 14:29:51 -0600 | [diff] [blame^] | 57 | |
| 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 Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 62 | assert!(!image.run_check_deps(&dep)); |
| 63 | } |
| 64 | }); |
| 65 | |
| 66 | /// These are the variants of dependencies we will test. |
| 67 | pub 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 | ]; |