Skip to content

Commit

Permalink
Refactor tests.verify.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
Urhengulas committed May 7, 2024
1 parent a30c097 commit dad0b75
Showing 1 changed file with 23 additions and 32 deletions.
55 changes: 23 additions & 32 deletions tests/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn should_link_example_firmware(#[case] default_features: bool) {
cargo::check_flip_link();

// Act
let cmd = cargo::build_example_firmware(CRATE, default_features);
let cmd = cargo::build_example_firmware(default_features);

// Assert
cmd.success();
Expand All @@ -31,7 +31,7 @@ fn should_verify_memory_layout() -> Result<()> {
cargo::check_flip_link();

// Act
cargo::build_example_firmware(CRATE, true).success();
cargo::build_example_firmware(true).success();

// Assert
for elf_path in elf::paths() {
Expand All @@ -40,7 +40,7 @@ fn should_verify_memory_layout() -> Result<()> {
let object = object::File::parse(&*elf)?;

// get the relevant sections
let (bss, data, uninit, vector_table) = elf::get_sections(&object)?;
let [bss, data, uninit, vector_table] = elf::get_sections(&object);
// compute the initial stack-pointer from `.vector_table`
let initial_sp = elf::compute_initial_sp(&vector_table)?;
// get the bounds of 'static RAM'
Expand All @@ -59,12 +59,14 @@ mod cargo {

use assert_cmd::{assert::Assert, prelude::*};

use super::*;

/// Build all examples in `$REPO/$rel_path`
#[must_use]
pub fn build_example_firmware(rel_path: &str, default_features: bool) -> Assert {
pub(crate) fn build_example_firmware(default_features: bool) -> Assert {
// append `rel_path` to the current working directory
let mut firmware_dir = std::env::current_dir().unwrap();
firmware_dir.push(rel_path);
firmware_dir.push(CRATE);

// disable default features or use `-v` as a no-op
let default_features = match default_features {
Expand All @@ -80,7 +82,7 @@ mod cargo {
}

/// Check that `flip-link` is present on the system
pub fn check_flip_link() {
pub(crate) fn check_flip_link() {
Command::new("which")
.arg("flip-link")
.unwrap()
Expand All @@ -100,18 +102,18 @@ mod elf {
///
/// It is the first 32-bit word in the `.vector_table` section,
/// according to the "ARMv6-M Architecture Reference Manual".
pub fn compute_initial_sp(vector_table: &Section) -> Result<u64> {
pub(crate) fn compute_initial_sp(vector_table: &Section) -> Result<u64> {
let data = vector_table.uncompressed_data()?;
let sp = u32::from_le_bytes(data[..4].try_into()?);
Ok(sp as u64)
}

/// Get [`RangeInclusive`] from lowest to highest address of all sections
pub fn get_bounds(sections: &[Section]) -> Result<RangeInclusive<u64>> {
pub(crate) fn get_bounds(sections: &[Section]) -> Result<RangeInclusive<u64>> {
// get beginning and end of all sections
let addresses = sections
.iter()
.flat_map(|sec| vec![sec.address(), sec.address() + sec.size()])
.flat_map(|sec| [sec.address(), sec.address() + sec.size()])
.collect::<Vec<_>>();

// get highest and lowest address of all sections
Expand All @@ -126,34 +128,23 @@ mod elf {
/// * `.data`
/// * `.uninit`
/// * `.vector_table`
pub fn get_sections<'data, 'file>(
object: &'file File<'data>,
) -> Result<(
Section<'data, 'file>,
Section<'data, 'file>,
Section<'data, 'file>,
Section<'data, 'file>,
)> {
pub(crate) fn get_sections<'file>(object: &'file File<'_>) -> [Section<'file, 'file>; 4] {
// try to get section, else error
let get_section = |section_name| {
object
.section_by_name(section_name)
.ok_or(format!("error getting section `{}`", section_name))
};

Ok((
get_section(".bss")?,
get_section(".data")?,
get_section(".uninit")?,
get_section(".vector_table")?,
))
let get_section = |section_name| object.section_by_name(section_name).expect(section_name);

[
get_section(".bss"),
get_section(".data"),
get_section(".uninit"),
get_section(".vector_table"),
]
}

/// Paths to firmware binaries.
pub fn paths() -> Vec<PathBuf> {
pub(crate) fn paths() -> Vec<PathBuf> {
FILES
.iter()
.map(|file_name| format!("{}/target/{}/debug/examples/{}", CRATE, TARGET, file_name))
.into_iter()
.map(|file_name| format!("{CRATE}/target/{TARGET}/debug/examples/{file_name}"))
.map(PathBuf::from)
.collect()
}
Expand Down

0 comments on commit dad0b75

Please sign in to comment.