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