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, |
David Brown | fe5ab1c | 2019-08-13 15:35:23 -0600 | [diff] [blame] | 8 | Images, |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 9 | NO_DEPS, |
| 10 | testlog, |
| 11 | }; |
David Brown | fe5ab1c | 2019-08-13 15:35:23 -0600 | [diff] [blame] | 12 | use std::{ |
| 13 | env, |
| 14 | sync::atomic::{AtomicUsize, Ordering}, |
| 15 | }; |
David Brown | dd2b118 | 2017-11-02 15:39:21 -0600 | [diff] [blame] | 16 | |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 17 | /// A single test, after setting up logging and such. Within the $body, |
| 18 | /// $arg will be bound to each device. |
| 19 | macro_rules! test_shell { |
| 20 | ($name:ident, $arg: ident, $body:expr) => { |
David Brown | a4167ef | 2017-11-06 14:30:05 -0700 | [diff] [blame] | 21 | #[test] |
| 22 | fn $name() { |
| 23 | testlog::setup(); |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 24 | ImagesBuilder::each_device(|$arg| { |
| 25 | $body; |
David Brown | a4167ef | 2017-11-06 14:30:05 -0700 | [diff] [blame] | 26 | }); |
David Brown | dd2b118 | 2017-11-02 15:39:21 -0600 | [diff] [blame] | 27 | } |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 28 | } |
| 29 | } |
| 30 | |
| 31 | /// A typical test calls a particular constructor, and runs a given test on |
| 32 | /// that constructor. |
| 33 | macro_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 Brown | fe5ab1c | 2019-08-13 15:35:23 -0600 | [diff] [blame] | 37 | dump_image(&image, stringify!($name)); |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 38 | assert!(!image.$test($($targs),*)); |
| 39 | }); |
David Brown | a4167ef | 2017-11-06 14:30:05 -0700 | [diff] [blame] | 40 | }; |
David Brown | dd2b118 | 2017-11-02 15:39:21 -0600 | [diff] [blame] | 41 | } |
David Brown | a4167ef | 2017-11-06 14:30:05 -0700 | [diff] [blame] | 42 | |
David Brown | eebf502 | 2019-07-30 15:01:07 -0600 | [diff] [blame] | 43 | 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] | 44 | sim_test!(norevert_newimage, make_no_upgrade_image(&NO_DEPS), run_norevert_newimage()); |
| 45 | sim_test!(basic_revert, make_image(&NO_DEPS, true), run_basic_revert()); |
| 46 | sim_test!(revert_with_fails, make_image(&NO_DEPS, false), run_revert_with_fails()); |
| 47 | sim_test!(perm_with_fails, make_image(&NO_DEPS, true), run_perm_with_fails()); |
| 48 | sim_test!(perm_with_random_fails, make_image(&NO_DEPS, true), run_perm_with_random_fails(5)); |
| 49 | sim_test!(norevert, make_image(&NO_DEPS, true), run_norevert()); |
| 50 | sim_test!(status_write_fails_complete, make_image(&NO_DEPS, true), run_with_status_fails_complete()); |
| 51 | sim_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. |
| 54 | test_shell!(dependency_combos, r, { |
| 55 | // Only test setups with two images. |
| 56 | if r.num_images() != 2 { |
| 57 | return; |
| 58 | } |
| 59 | |
David Brown | fe5ab1c | 2019-08-13 15:35:23 -0600 | [diff] [blame] | 60 | for dep in TEST_DEPS { |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 61 | let image = r.clone().make_image(&dep, true); |
David Brown | fe5ab1c | 2019-08-13 15:35:23 -0600 | [diff] [blame] | 62 | dump_image(&image, "dependency_combos"); |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 63 | assert!(!image.run_check_deps(&dep)); |
| 64 | } |
| 65 | }); |
| 66 | |
| 67 | /// These are the variants of dependencies we will test. |
| 68 | pub 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 Brown | 18d301f | 2019-09-03 11:37:39 -0600 | [diff] [blame^] | 75 | // If all of the dependencies are met, we should also upgrade. |
Fabio Utzig | 135f716 | 2019-08-28 11:03:44 -0300 | [diff] [blame] | 76 | DepTest { |
| 77 | depends: [DepType::Correct, DepType::Correct], |
| 78 | upgrades: [UpgradeInfo::Upgraded, UpgradeInfo::Upgraded], |
| 79 | }, |
| 80 | |
David Brown | 18d301f | 2019-09-03 11:37:39 -0600 | [diff] [blame^] | 81 | // If none of the dependencies are met, the images should be held. |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 82 | DepTest { |
| 83 | depends: [DepType::Newer, DepType::Newer], |
| 84 | upgrades: [UpgradeInfo::Held, UpgradeInfo::Held], |
| 85 | }, |
David Brown | 18d301f | 2019-09-03 11:37:39 -0600 | [diff] [blame^] | 86 | |
| 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 Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 101 | ]; |
David Brown | fe5ab1c | 2019-08-13 15:35:23 -0600 | [diff] [blame] | 102 | |
| 103 | /// Counter for the image number. |
| 104 | static 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. |
| 111 | fn 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 | } |