Skip to content

Commit

Permalink
test(efiboot): add tests for linux partition retriever functions
Browse files Browse the repository at this point in the history
  • Loading branch information
iTrooz committed Oct 19, 2023
1 parent 3e9d5c1 commit 85b1c48
Showing 1 changed file with 52 additions and 7 deletions.
59 changes: 52 additions & 7 deletions efiboot/src/cli/boot/add/disk/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use itertools::Itertools;

/// get the partition UUID from its name
/// * `name`: the name of the partition, e.g. '/dev/sda1'
fn get_partition_uuid(name: &str) -> uuid::Uuid {
fn get_partition_uuid(name: &str) -> Option<uuid::Uuid> {
let output = Command::new("blkid").output().unwrap().stdout;

if output.is_empty() {
Expand All @@ -27,18 +27,18 @@ fn get_partition_uuid(name: &str) -> uuid::Uuid {
let (key, value) = pair.split_once('=').unwrap();
if key == "PARTUUID" {
let value = value.trim_matches('"');
return uuid::Uuid::parse_str(value).unwrap();
return Some(uuid::Uuid::parse_str(value).unwrap());
}
}

break;
}

panic!("No partition found");
None
}

/// Partition names are in the form '/dev/sda1', so just take the number at the end
fn get_partition_number(name: &str) -> u32 {
fn get_partition_number(name: &str) -> Option<u32> {
name.chars()
.rev()
.take_while(|c| c.is_ascii_digit())
Expand All @@ -47,7 +47,7 @@ fn get_partition_number(name: &str) -> u32 {
.rev()
.collect::<String>()
.parse::<u32>()
.unwrap()
.ok()
}

/// get partitition start and size
Expand All @@ -69,8 +69,8 @@ fn get_partition_location(name: &str) -> (u64, u64) {

/// retrieve data needed to generate a EFIHardDrive from the system, from a friendly name of the partition
pub fn retrieve_efi_partition_data(name: &str) -> EFIHardDrive {
let partition_sig = get_partition_uuid(name);
let partition_number = get_partition_number(name);
let partition_sig = get_partition_uuid(name).unwrap();
let partition_number = get_partition_number(name).unwrap();
let (partition_start, partition_size) = get_partition_location(name);

EFIHardDrive {
Expand All @@ -96,3 +96,48 @@ pub fn get_mount_point(name: &str) -> Option<PathBuf> {

None
}

#[cfg(test)]
mod tests {
use std::path::PathBuf;

use super::*;

fn get_real_partition() -> (String, String) {
//! Returns an actual partition existing on the system that runs it
//! Used for tests because we can't easily simulate partitions externally
for line in std::fs::read_to_string("/proc/mounts").unwrap().lines() {
let mut iter = line.splitn(3, ' ');
let partition_name = iter.next().unwrap();
let mount_point = iter.next().unwrap();
if partition_name.starts_with("/dev/")
&& partition_name.ends_with(|c: char| char::is_ascii_digit(&c))
{
println!("found partition {partition_name} with mount point {mount_point} in system. It will be used for this test");
return (partition_name.to_owned(), mount_point.to_owned());
}
}
core::panic!("Could not find a valid partition in system. Some tests will fail");
}

#[test]
fn real_mount_point() {
let (part_name, mount_point) = get_real_partition();
assert_eq!(
get_mount_point(&part_name).unwrap(),
PathBuf::from(mount_point)
);
}

#[test]
fn inexistent_mount_point() {
assert_eq!(get_mount_point("heythere"), None);
}

#[test]
fn partition_number() {
assert_eq!(get_partition_number("/dev/sda1"), Some(1));
assert_eq!(get_partition_number("/dev/sda13"), Some(13));
assert_eq!(get_partition_number("/dev/sda"), None);
}
}

0 comments on commit 85b1c48

Please sign in to comment.