use clap::Parser;
use efivar::{
boot::{BootEntry, BootEntryAttributes, FilePath, FilePathList},
efi::Variable,
store::MemoryStore,
test_utils::assert_var_not_found,
utils, VarReader,
};
use crate::{
cli::{boot::tests::standard_setup, Command},
exit_code::ExitCode,
};
#[test]
fn add() {
let manager = &mut MemoryStore::new();
let setup_entry = standard_setup(manager, 0x0001);
assert_eq!(
ExitCode::SUCCESS,
crate::run(
Command::parse_from([
"efivarcli",
"boot",
"add",
"--file",
"\\a\\b\\c",
"--description",
"Some entry"
]),
manager,
)
);
let (data, _) = manager.read(&Variable::new("Boot0002")).unwrap();
let entry = BootEntry::parse(data).unwrap();
assert_eq!(
entry,
BootEntry {
attributes: BootEntryAttributes::LOAD_OPTION_ACTIVE,
description: "Some entry".to_owned(),
file_path_list: Some(FilePathList {
file_path: FilePath {
path: "\\a\\b\\c".into()
},
hard_drive: setup_entry.file_path_list.unwrap().hard_drive }),
optional_data: vec![]
}
);
let (data, _) = manager.read(&Variable::new("BootOrder")).unwrap();
assert_eq!(data, utils::u16_to_u8(&[0x0002, 0x0001]));
}
#[test]
fn add_set_id() {
let manager = &mut MemoryStore::new();
let setup_entry = standard_setup(manager, 0x0001);
assert_eq!(
ExitCode::SUCCESS,
crate::run(
Command::parse_from([
"efivarcli",
"boot",
"add",
"--file",
"\\a\\b\\c",
"--description",
"Some entry",
"--id",
"1000"
]),
manager,
)
);
assert_var_not_found(manager, &Variable::new("Boot0000"));
let (data, _) = manager.read(&Variable::new("Boot1000")).unwrap();
let entry = BootEntry::parse(data).unwrap();
assert_eq!(
entry,
BootEntry {
attributes: BootEntryAttributes::LOAD_OPTION_ACTIVE,
description: "Some entry".to_owned(),
file_path_list: Some(FilePathList {
file_path: FilePath {
path: "\\a\\b\\c".into()
},
hard_drive: setup_entry.file_path_list.unwrap().hard_drive }),
optional_data: vec![]
}
);
let (data, _) = manager.read(&Variable::new("BootOrder")).unwrap();
assert_eq!(data, utils::u16_to_u8(&[0x1000, 0x0001]));
}
#[test]
fn add_verify_file_path_fix() {
let manager = &mut MemoryStore::new();
let setup_entry = standard_setup(manager, 0x0001);
assert_eq!(
ExitCode::SUCCESS,
crate::run(
Command::parse_from([
"efivarcli",
"boot",
"add",
"--file",
"a/b/c",
"--description",
"Some entry",
"--id",
"1000"
]),
manager,
)
);
assert_var_not_found(manager, &Variable::new("Boot0000"));
let (data, _) = manager.read(&Variable::new("Boot1000")).unwrap();
let entry = BootEntry::parse(data).unwrap();
assert_eq!(
entry,
BootEntry {
attributes: BootEntryAttributes::LOAD_OPTION_ACTIVE,
description: "Some entry".to_owned(),
file_path_list: Some(FilePathList {
file_path: FilePath {
path: "\\a\\b\\c".into()
},
hard_drive: setup_entry.file_path_list.unwrap().hard_drive }),
optional_data: vec![]
}
);
let (data, _) = manager.read(&Variable::new("BootOrder")).unwrap();
assert_eq!(data, utils::u16_to_u8(&[0x1000, 0x0001]));
}
#[test]
fn add_on_existing() {
let manager = &mut MemoryStore::new();
let setup_entry = standard_setup(manager, 0x0001);
let current_exe = std::env::current_exe()
.unwrap()
.to_str()
.unwrap()
.to_owned();
assert_eq!(
ExitCode::FAILURE,
crate::run(
Command::parse_from([
"efivarcli",
"boot",
"add",
"--file",
¤t_exe,
"--description",
"Some entry",
"--id",
"0001"
]),
manager,
)
);
assert_eq!(
BootEntry::read(manager, &Variable::new("Boot0001")).unwrap(),
setup_entry
);
}