David Brown | e2acfae | 2020-01-21 16:45:01 -0700 | [diff] [blame] | 1 | // Copyright (c) 2017-2019 Linaro LTD |
| 2 | // Copyright (c) 2017-2019 JUUL Labs |
| 3 | // |
| 4 | // SPDX-License-Identifier: Apache-2.0 |
| 5 | |
David Brown | dd2b118 | 2017-11-02 15:39:21 -0600 | [diff] [blame] | 6 | //! Core tests |
| 7 | //! |
| 8 | //! Run the existing testsuite as a Rust unit test. |
| 9 | |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 10 | use bootsim::{ |
| 11 | DepTest, DepType, UpgradeInfo, |
| 12 | ImagesBuilder, |
David Brown | fe5ab1c | 2019-08-13 15:35:23 -0600 | [diff] [blame] | 13 | Images, |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 14 | NO_DEPS, |
David Brown | 2ee5f7f | 2020-01-13 14:04:01 -0700 | [diff] [blame] | 15 | REV_DEPS, |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 16 | testlog, |
| 17 | }; |
David Brown | fe5ab1c | 2019-08-13 15:35:23 -0600 | [diff] [blame] | 18 | use std::{ |
| 19 | env, |
| 20 | sync::atomic::{AtomicUsize, Ordering}, |
| 21 | }; |
David Brown | dd2b118 | 2017-11-02 15:39:21 -0600 | [diff] [blame] | 22 | |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 23 | /// A single test, after setting up logging and such. Within the $body, |
| 24 | /// $arg will be bound to each device. |
| 25 | macro_rules! test_shell { |
| 26 | ($name:ident, $arg: ident, $body:expr) => { |
David Brown | a4167ef | 2017-11-06 14:30:05 -0700 | [diff] [blame] | 27 | #[test] |
| 28 | fn $name() { |
| 29 | testlog::setup(); |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 30 | ImagesBuilder::each_device(|$arg| { |
| 31 | $body; |
David Brown | a4167ef | 2017-11-06 14:30:05 -0700 | [diff] [blame] | 32 | }); |
David Brown | dd2b118 | 2017-11-02 15:39:21 -0600 | [diff] [blame] | 33 | } |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 34 | } |
| 35 | } |
| 36 | |
| 37 | /// A typical test calls a particular constructor, and runs a given test on |
| 38 | /// that constructor. |
| 39 | macro_rules! sim_test { |
| 40 | ($name:ident, $maker:ident($($margs:expr),*), $test:ident($($targs:expr),*)) => { |
| 41 | test_shell!($name, r, { |
| 42 | let image = r.$maker($($margs),*); |
David Brown | fe5ab1c | 2019-08-13 15:35:23 -0600 | [diff] [blame] | 43 | dump_image(&image, stringify!($name)); |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 44 | assert!(!image.$test($($targs),*)); |
| 45 | }); |
David Brown | a4167ef | 2017-11-06 14:30:05 -0700 | [diff] [blame] | 46 | }; |
David Brown | dd2b118 | 2017-11-02 15:39:21 -0600 | [diff] [blame] | 47 | } |
David Brown | a4167ef | 2017-11-06 14:30:05 -0700 | [diff] [blame] | 48 | |
David Brown | eebf502 | 2019-07-30 15:01:07 -0600 | [diff] [blame] | 49 | sim_test!(bad_secondary_slot, make_bad_secondary_slot_image(), run_signfail_upgrade()); |
Fabio Utzig | 2c3be5c | 2020-07-09 19:54:45 -0300 | [diff] [blame] | 50 | sim_test!(secondary_trailer_leftover, make_erased_secondary_image(), run_secondary_leftover_trailer()); |
Fabio Utzig | d015734 | 2020-10-02 15:22:11 -0300 | [diff] [blame] | 51 | sim_test!(bootstrap, make_bootstrap_image(), run_bootstrap()); |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 52 | sim_test!(norevert_newimage, make_no_upgrade_image(&NO_DEPS), run_norevert_newimage()); |
| 53 | sim_test!(basic_revert, make_image(&NO_DEPS, true), run_basic_revert()); |
| 54 | sim_test!(revert_with_fails, make_image(&NO_DEPS, false), run_revert_with_fails()); |
| 55 | sim_test!(perm_with_fails, make_image(&NO_DEPS, true), run_perm_with_fails()); |
| 56 | sim_test!(perm_with_random_fails, make_image(&NO_DEPS, true), run_perm_with_random_fails(5)); |
| 57 | sim_test!(norevert, make_image(&NO_DEPS, true), run_norevert()); |
| 58 | sim_test!(status_write_fails_complete, make_image(&NO_DEPS, true), run_with_status_fails_complete()); |
| 59 | sim_test!(status_write_fails_with_reset, make_image(&NO_DEPS, true), run_with_status_fails_with_reset()); |
David Brown | 2ee5f7f | 2020-01-13 14:04:01 -0700 | [diff] [blame] | 60 | sim_test!(downgrade_prevention, make_image(&REV_DEPS, true), run_nodowngrade()); |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 61 | |
| 62 | // Test various combinations of incorrect dependencies. |
| 63 | test_shell!(dependency_combos, r, { |
| 64 | // Only test setups with two images. |
| 65 | if r.num_images() != 2 { |
| 66 | return; |
| 67 | } |
| 68 | |
David Brown | fe5ab1c | 2019-08-13 15:35:23 -0600 | [diff] [blame] | 69 | for dep in TEST_DEPS { |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 70 | let image = r.clone().make_image(&dep, true); |
David Brown | fe5ab1c | 2019-08-13 15:35:23 -0600 | [diff] [blame] | 71 | dump_image(&image, "dependency_combos"); |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 72 | assert!(!image.run_check_deps(&dep)); |
| 73 | } |
| 74 | }); |
| 75 | |
| 76 | /// These are the variants of dependencies we will test. |
| 77 | pub static TEST_DEPS: &[DepTest] = &[ |
David Brown | e4576b8 | 2019-09-03 12:26:18 -0600 | [diff] [blame] | 78 | // A sanity test, no dependencies should upgrade. |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 79 | DepTest { |
| 80 | depends: [DepType::Nothing, DepType::Nothing], |
| 81 | upgrades: [UpgradeInfo::Upgraded, UpgradeInfo::Upgraded], |
David Brown | 2ee5f7f | 2020-01-13 14:04:01 -0700 | [diff] [blame] | 82 | downgrade: false, |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 83 | }, |
| 84 | |
David Brown | 18d301f | 2019-09-03 11:37:39 -0600 | [diff] [blame] | 85 | // If all of the dependencies are met, we should also upgrade. |
Fabio Utzig | 135f716 | 2019-08-28 11:03:44 -0300 | [diff] [blame] | 86 | DepTest { |
| 87 | depends: [DepType::Correct, DepType::Correct], |
| 88 | upgrades: [UpgradeInfo::Upgraded, UpgradeInfo::Upgraded], |
David Brown | 2ee5f7f | 2020-01-13 14:04:01 -0700 | [diff] [blame] | 89 | downgrade: false, |
Fabio Utzig | 135f716 | 2019-08-28 11:03:44 -0300 | [diff] [blame] | 90 | }, |
| 91 | |
David Brown | 18d301f | 2019-09-03 11:37:39 -0600 | [diff] [blame] | 92 | // If none of the dependencies are met, the images should be held. |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 93 | DepTest { |
| 94 | depends: [DepType::Newer, DepType::Newer], |
| 95 | upgrades: [UpgradeInfo::Held, UpgradeInfo::Held], |
David Brown | 2ee5f7f | 2020-01-13 14:04:01 -0700 | [diff] [blame] | 96 | downgrade: false, |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 97 | }, |
David Brown | 18d301f | 2019-09-03 11:37:39 -0600 | [diff] [blame] | 98 | |
| 99 | // If the first image is not met, we should hold back on the |
| 100 | // dependencies (it is not well defined what the correct behavior is |
| 101 | // here, it could also be correct to upgrade only the second image). |
| 102 | DepTest { |
| 103 | depends: [DepType::Newer, DepType::Correct], |
| 104 | upgrades: [UpgradeInfo::Held, UpgradeInfo::Held], |
David Brown | 2ee5f7f | 2020-01-13 14:04:01 -0700 | [diff] [blame] | 105 | downgrade: false, |
David Brown | 18d301f | 2019-09-03 11:37:39 -0600 | [diff] [blame] | 106 | }, |
| 107 | |
| 108 | // Test the variant in the other direction. |
| 109 | DepTest { |
| 110 | depends: [DepType::Correct, DepType::Newer], |
| 111 | upgrades: [UpgradeInfo::Held, UpgradeInfo::Held], |
David Brown | 2ee5f7f | 2020-01-13 14:04:01 -0700 | [diff] [blame] | 112 | downgrade: false, |
David Brown | 18d301f | 2019-09-03 11:37:39 -0600 | [diff] [blame] | 113 | }, |
| 114 | |
David Brown | e4576b8 | 2019-09-03 12:26:18 -0600 | [diff] [blame] | 115 | // Test where only the first image is upgraded, and there are no |
| 116 | // dependencies. |
| 117 | DepTest { |
| 118 | depends: [DepType::Nothing, DepType::NoUpgrade], |
| 119 | upgrades: [UpgradeInfo::Upgraded, UpgradeInfo::Held], |
David Brown | 2ee5f7f | 2020-01-13 14:04:01 -0700 | [diff] [blame] | 120 | downgrade: false, |
David Brown | e4576b8 | 2019-09-03 12:26:18 -0600 | [diff] [blame] | 121 | }, |
| 122 | |
| 123 | // Test one image with a valid dependency on the first image. |
| 124 | DepTest { |
| 125 | depends: [DepType::OldCorrect, DepType::NoUpgrade], |
| 126 | upgrades: [UpgradeInfo::Upgraded, UpgradeInfo::Held], |
David Brown | 2ee5f7f | 2020-01-13 14:04:01 -0700 | [diff] [blame] | 127 | downgrade: false, |
David Brown | e4576b8 | 2019-09-03 12:26:18 -0600 | [diff] [blame] | 128 | }, |
| 129 | |
| 130 | // Test one image with an invalid dependency on the first image. |
| 131 | DepTest { |
| 132 | depends: [DepType::Newer, DepType::NoUpgrade], |
| 133 | upgrades: [UpgradeInfo::Held, UpgradeInfo::Held], |
David Brown | 2ee5f7f | 2020-01-13 14:04:01 -0700 | [diff] [blame] | 134 | downgrade: false, |
David Brown | e4576b8 | 2019-09-03 12:26:18 -0600 | [diff] [blame] | 135 | }, |
| 136 | |
| 137 | // Test where only the second image is upgraded, and there are no |
| 138 | // dependencies. |
| 139 | DepTest { |
| 140 | depends: [DepType::NoUpgrade, DepType::Nothing], |
| 141 | upgrades: [UpgradeInfo::Held, UpgradeInfo::Upgraded], |
David Brown | 2ee5f7f | 2020-01-13 14:04:01 -0700 | [diff] [blame] | 142 | downgrade: false, |
David Brown | e4576b8 | 2019-09-03 12:26:18 -0600 | [diff] [blame] | 143 | }, |
| 144 | |
| 145 | // Test one image with a valid dependency on the second image. |
| 146 | DepTest { |
| 147 | depends: [DepType::NoUpgrade, DepType::OldCorrect], |
| 148 | upgrades: [UpgradeInfo::Held, UpgradeInfo::Upgraded], |
David Brown | 2ee5f7f | 2020-01-13 14:04:01 -0700 | [diff] [blame] | 149 | downgrade: false, |
David Brown | e4576b8 | 2019-09-03 12:26:18 -0600 | [diff] [blame] | 150 | }, |
| 151 | |
| 152 | // Test one image with an invalid dependency on the second image. |
| 153 | DepTest { |
| 154 | depends: [DepType::NoUpgrade, DepType::Newer], |
| 155 | upgrades: [UpgradeInfo::Held, UpgradeInfo::Held], |
David Brown | 2ee5f7f | 2020-01-13 14:04:01 -0700 | [diff] [blame] | 156 | downgrade: false, |
David Brown | e4576b8 | 2019-09-03 12:26:18 -0600 | [diff] [blame] | 157 | }, |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 158 | ]; |
David Brown | fe5ab1c | 2019-08-13 15:35:23 -0600 | [diff] [blame] | 159 | |
| 160 | /// Counter for the image number. |
| 161 | static IMAGE_NUMBER: AtomicUsize = AtomicUsize::new(0); |
| 162 | |
| 163 | /// Dump an image if that makes sense. The name is the name of the test |
| 164 | /// being run. If the MCUBOT_DEBUG_DUMP environment variable contains, in |
| 165 | /// one of its comma separate strings a substring of this name, then this |
| 166 | /// image will be dumped. As a special case, we will dump everything if |
| 167 | /// this environment variable is set to all. |
| 168 | fn dump_image(image: &Images, name: &str) { |
| 169 | if let Ok(request) = env::var("MCUBOOT_DEBUG_DUMP") { |
| 170 | if request.split(',').any(|req| { |
| 171 | req == "all" || name.contains(req) |
| 172 | }) { |
| 173 | let count = IMAGE_NUMBER.fetch_add(1, Ordering::SeqCst); |
| 174 | let full_name = format!("{}-{:04}", name, count); |
| 175 | log::info!("Dump {:?}", full_name); |
| 176 | image.debug_dump(&full_name); |
| 177 | } |
| 178 | } |
| 179 | } |