Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 98 additions & 4 deletions src/efi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,6 @@ impl Efi {
Ok(())
}



/// Copy from /usr/lib/efi to boot/ESP.
fn package_mode_copy_to_boot_impl(&self) -> Result<()> {
let sysroot = Path::new("/");
Expand Down Expand Up @@ -1006,7 +1004,6 @@ Boot0003* test";
);
Ok(())
}
#[cfg(test)]
fn fixture() -> Result<cap_std_ext::cap_tempfile::TempDir> {
let tempdir = cap_std_ext::cap_tempfile::tempdir(cap_std::ambient_authority())?;
tempdir.create_dir("etc")?;
Expand Down Expand Up @@ -1036,7 +1033,7 @@ Boot0003* test";
{
tmpd.atomic_write(
"etc/system-release",
"Red Hat Enterprise Linux CoreOS release 4
r"Red Hat Enterprise Linux CoreOS release 4
",
)?;
let name = get_product_name(&tmpd)?;
Expand Down Expand Up @@ -1129,4 +1126,101 @@ Boot0003* test";

Ok(())
}

#[test]
fn test_package_mode_shim_installation() -> Result<()> {
// Test that shim can be installed from /usr/lib/efi to ESP
let tmpdir: &tempfile::TempDir = &tempfile::tempdir()?;
let tpath = tmpdir.path();

// Create mock /usr/lib/efi structure with shim
let efi_path = tpath.join("usr/lib/efi");
let shim_path = efi_path.join("shim/15.8-3/EFI/fedora");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are harding coding the versions here, and also in line 1168 and in the other commits also for Grub. Is there any reason? it will fail if we have a diff version.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, it makes sense to avoid hardcoding there. I’ve fixed it to match the pattern used in the other tests.

std::fs::create_dir_all(&shim_path)?;

// Write shim binary
let shim_content = b"mock shim binary content";
std::fs::write(shim_path.join(SHIM), shim_content)?;

// Create additional shim files that might be present
std::fs::write(shim_path.join("MokManager.efi"), b"mok manager content")?;
std::fs::write(shim_path.join("fbx64.efi"), b"fallback content")?;

// Create mock ESP directory structure (simulating /boot/efi in container)
let esp_path = tpath.join("boot/efi");
std::fs::create_dir_all(&esp_path)?;

// Create EFI directory in ESP
let esp_efi_path = esp_path.join("EFI");
std::fs::create_dir_all(&esp_efi_path)?;

// Set up sysroot directory
let sysroot_dir = openat::Dir::open(tpath)?;

// Get EFI components from usr/lib/efi
let utf8_tpath =
Utf8Path::from_path(tpath).ok_or_else(|| anyhow::anyhow!("Path is not valid UTF-8"))?;
let efi_comps = get_efi_component_from_usr(utf8_tpath, EFILIB)?;
assert!(efi_comps.is_some(), "Should find shim component");
let efi_comps = efi_comps.unwrap();
assert_eq!(efi_comps.len(), 1, "Should find exactly one component");
assert_eq!(efi_comps[0].name, "shim");
assert_eq!(efi_comps[0].version, "15.8-3");

// Create Efi instance and copy components to ESP
let efi = Efi::default();
efi.copy_efi_components_to_esp(&sysroot_dir, &esp_path, &efi_comps)?;

// Expected path: /boot/efi/EFI/fedora/shimx64.efi (or shimaa64.efi, etc.)
let copied_shim_path = esp_path.join("EFI/fedora").join(SHIM);
assert!(
copied_shim_path.exists(),
"Shim should be copied to ESP at {}",
copied_shim_path.display()
);

// Verify the shim file is actually a file, not a directory
assert!(
copied_shim_path.is_file(),
"Shim should be a file at {}",
copied_shim_path.display()
);

// Verify the content matches exactly
let copied_content = std::fs::read(&copied_shim_path)?;
assert_eq!(
copied_content, shim_content,
"Shim content should match exactly"
);

// Verify the directory structure is correct
assert!(
esp_path.join("EFI").exists(),
"EFI directory should exist in ESP at {}",
esp_path.join("EFI").display()
);
assert!(esp_path.join("EFI").is_dir(), "EFI should be a directory");

assert!(
esp_path.join("EFI/fedora").exists(),
"Vendor directory (fedora) should exist in ESP at {}",
esp_path.join("EFI/fedora").display()
);
assert!(
esp_path.join("EFI/fedora").is_dir(),
"EFI/fedora should be a directory"
);

// Verify the path structure matches expected package mode layout
// Source: /usr/lib/efi/shim/15.8-3/EFI/fedora/shimx64.efi
// Dest: /boot/efi/EFI/fedora/shimx64.efi
let expected_base = esp_path.join("EFI/fedora");
assert_eq!(
copied_shim_path.parent(),
Some(expected_base.as_path()),
"Shim should be directly under EFI/fedora/, not in a subdirectory"
);

Ok(())
}
}
Loading