2023-01-01 18:54:57 -06:00
|
|
|
use std::fs;
|
|
|
|
|
|
|
|
use anyhow::{Context, Result};
|
2023-04-24 04:48:46 -05:00
|
|
|
use expect_test::expect;
|
2023-01-01 18:54:57 -06:00
|
|
|
use tempfile::tempdir;
|
|
|
|
|
|
|
|
mod common;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn generate_expected_os_release() -> Result<()> {
|
|
|
|
let esp_mountpoint = tempdir()?;
|
|
|
|
let tmpdir = tempdir()?;
|
|
|
|
let profiles = tempdir()?;
|
2023-08-13 05:02:35 -05:00
|
|
|
let toplevel = common::setup_toplevel(tmpdir.path())?;
|
2023-01-01 18:54:57 -06:00
|
|
|
|
2023-08-13 05:02:35 -05:00
|
|
|
let generation_link =
|
|
|
|
common::setup_generation_link_from_toplevel(&toplevel, profiles.path(), 1)
|
|
|
|
.expect("Failed to setup generation link");
|
2023-01-01 18:54:57 -06:00
|
|
|
|
|
|
|
let output0 = common::lanzaboote_install(0, esp_mountpoint.path(), vec![generation_link])?;
|
|
|
|
assert!(output0.status.success());
|
|
|
|
|
2023-08-13 05:02:35 -05:00
|
|
|
let stub_data = fs::read(common::image_path(&esp_mountpoint, 1, &toplevel)?)?;
|
2023-01-01 18:54:57 -06:00
|
|
|
let os_release_section = pe_section(&stub_data, ".osrel")
|
|
|
|
.context("Failed to read .osrelease PE section.")?
|
|
|
|
.to_owned();
|
|
|
|
|
2023-04-24 04:48:46 -05:00
|
|
|
let expected = expect![[r#"
|
|
|
|
ID=lanza
|
2023-10-20 04:29:28 -05:00
|
|
|
PRETTY_NAME=LanzaOS (Generation 1, 1970-01-01)
|
|
|
|
VERSION_ID=Generation 1, 1970-01-01
|
2023-04-24 04:48:46 -05:00
|
|
|
"#]];
|
2023-01-01 18:54:57 -06:00
|
|
|
|
2023-04-24 04:48:46 -05:00
|
|
|
expected.assert_eq(&String::from_utf8(os_release_section)?);
|
2023-01-01 18:54:57 -06:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pe_section<'a>(file_data: &'a [u8], section_name: &str) -> Option<&'a [u8]> {
|
|
|
|
let pe_binary = goblin::pe::PE::parse(file_data).ok()?;
|
|
|
|
|
|
|
|
pe_binary
|
|
|
|
.sections
|
|
|
|
.iter()
|
|
|
|
.find(|s| s.name().unwrap() == section_name)
|
|
|
|
.and_then(|s| {
|
|
|
|
let section_start: usize = s.pointer_to_raw_data.try_into().ok()?;
|
|
|
|
assert!(s.virtual_size <= s.size_of_raw_data);
|
|
|
|
let section_end: usize = section_start + usize::try_from(s.virtual_size).ok()?;
|
|
|
|
Some(&file_data[section_start..section_end])
|
|
|
|
})
|
|
|
|
}
|