Skip to content

Commit 2496845

Browse files
committed
Add unit test that installs shim and ensure the right location
Addressing review: add unit test that installs shim into a container and ensures that the files are properly setup in the right place
1 parent d3b5f7b commit 2496845

File tree

1 file changed

+98
-4
lines changed

1 file changed

+98
-4
lines changed

src/efi.rs

Lines changed: 98 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,6 @@ impl Efi {
221221
Ok(())
222222
}
223223

224-
225-
226224
/// Copy from /usr/lib/efi to boot/ESP.
227225
fn package_mode_copy_to_boot_impl(&self) -> Result<()> {
228226
let sysroot = Path::new("/");
@@ -1006,7 +1004,6 @@ Boot0003* test";
10061004
);
10071005
Ok(())
10081006
}
1009-
#[cfg(test)]
10101007
fn fixture() -> Result<cap_std_ext::cap_tempfile::TempDir> {
10111008
let tempdir = cap_std_ext::cap_tempfile::tempdir(cap_std::ambient_authority())?;
10121009
tempdir.create_dir("etc")?;
@@ -1036,7 +1033,7 @@ Boot0003* test";
10361033
{
10371034
tmpd.atomic_write(
10381035
"etc/system-release",
1039-
"Red Hat Enterprise Linux CoreOS release 4
1036+
r"Red Hat Enterprise Linux CoreOS release 4
10401037
",
10411038
)?;
10421039
let name = get_product_name(&tmpd)?;
@@ -1129,4 +1126,101 @@ Boot0003* test";
11291126

11301127
Ok(())
11311128
}
1129+
1130+
#[test]
1131+
fn test_package_mode_shim_installation() -> Result<()> {
1132+
// Test that shim can be installed from /usr/lib/efi to ESP
1133+
let tmpdir: &tempfile::TempDir = &tempfile::tempdir()?;
1134+
let tpath = tmpdir.path();
1135+
1136+
// Create mock /usr/lib/efi structure with shim
1137+
let efi_path = tpath.join("usr/lib/efi");
1138+
let shim_path = efi_path.join("shim/15.8-3/EFI/fedora");
1139+
std::fs::create_dir_all(&shim_path)?;
1140+
1141+
// Write shim binary
1142+
let shim_content = b"mock shim binary content";
1143+
std::fs::write(shim_path.join(SHIM), shim_content)?;
1144+
1145+
// Create additional shim files that might be present
1146+
std::fs::write(shim_path.join("MokManager.efi"), b"mok manager content")?;
1147+
std::fs::write(shim_path.join("fbx64.efi"), b"fallback content")?;
1148+
1149+
// Create mock ESP directory structure (simulating /boot/efi in container)
1150+
let esp_path = tpath.join("boot/efi");
1151+
std::fs::create_dir_all(&esp_path)?;
1152+
1153+
// Create EFI directory in ESP
1154+
let esp_efi_path = esp_path.join("EFI");
1155+
std::fs::create_dir_all(&esp_efi_path)?;
1156+
1157+
// Set up sysroot directory
1158+
let sysroot_dir = openat::Dir::open(tpath)?;
1159+
1160+
// Get EFI components from usr/lib/efi
1161+
let utf8_tpath =
1162+
Utf8Path::from_path(tpath).ok_or_else(|| anyhow::anyhow!("Path is not valid UTF-8"))?;
1163+
let efi_comps = get_efi_component_from_usr(utf8_tpath, EFILIB)?;
1164+
assert!(efi_comps.is_some(), "Should find shim component");
1165+
let efi_comps = efi_comps.unwrap();
1166+
assert_eq!(efi_comps.len(), 1, "Should find exactly one component");
1167+
assert_eq!(efi_comps[0].name, "shim");
1168+
assert_eq!(efi_comps[0].version, "15.8-3");
1169+
1170+
// Create Efi instance and copy components to ESP
1171+
let efi = Efi::default();
1172+
efi.copy_efi_components_to_esp(&sysroot_dir, &esp_path, &efi_comps)?;
1173+
1174+
// Expected path: /boot/efi/EFI/fedora/shimx64.efi (or shimaa64.efi, etc.)
1175+
let copied_shim_path = esp_path.join("EFI/fedora").join(SHIM);
1176+
assert!(
1177+
copied_shim_path.exists(),
1178+
"Shim should be copied to ESP at {}",
1179+
copied_shim_path.display()
1180+
);
1181+
1182+
// Verify the shim file is actually a file, not a directory
1183+
assert!(
1184+
copied_shim_path.is_file(),
1185+
"Shim should be a file at {}",
1186+
copied_shim_path.display()
1187+
);
1188+
1189+
// Verify the content matches exactly
1190+
let copied_content = std::fs::read(&copied_shim_path)?;
1191+
assert_eq!(
1192+
copied_content, shim_content,
1193+
"Shim content should match exactly"
1194+
);
1195+
1196+
// Verify the directory structure is correct
1197+
assert!(
1198+
esp_path.join("EFI").exists(),
1199+
"EFI directory should exist in ESP at {}",
1200+
esp_path.join("EFI").display()
1201+
);
1202+
assert!(esp_path.join("EFI").is_dir(), "EFI should be a directory");
1203+
1204+
assert!(
1205+
esp_path.join("EFI/fedora").exists(),
1206+
"Vendor directory (fedora) should exist in ESP at {}",
1207+
esp_path.join("EFI/fedora").display()
1208+
);
1209+
assert!(
1210+
esp_path.join("EFI/fedora").is_dir(),
1211+
"EFI/fedora should be a directory"
1212+
);
1213+
1214+
// Verify the path structure matches expected package mode layout
1215+
// Source: /usr/lib/efi/shim/15.8-3/EFI/fedora/shimx64.efi
1216+
// Dest: /boot/efi/EFI/fedora/shimx64.efi
1217+
let expected_base = esp_path.join("EFI/fedora");
1218+
assert_eq!(
1219+
copied_shim_path.parent(),
1220+
Some(expected_base.as_path()),
1221+
"Shim should be directly under EFI/fedora/, not in a subdirectory"
1222+
);
1223+
1224+
Ok(())
1225+
}
11321226
}

0 commit comments

Comments
 (0)