Skip to content

Commit

Permalink
boot configurator: more unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandra Iordache <aghecen@amazon.com>
  • Loading branch information
Alexandra Iordache committed Apr 10, 2020
1 parent 89de317 commit 9277d02
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 9 deletions.
2 changes: 1 addition & 1 deletion coverage_config_aarch64.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"coverage_score": 75.7,
"coverage_score": 76.4,
"exclude_path": "",
"crate_features": ""
}
2 changes: 1 addition & 1 deletion coverage_config_x86_64.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"coverage_score": 74.8,
"coverage_score": 77.6,
"exclude_path": "",
"crate_features": ""
}
59 changes: 59 additions & 0 deletions src/configurator/aarch64/fdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,62 @@ impl BootConfigurator for FdtBootConfigurator {
.map_err(|_| Error::WriteFDTToMemory.into())
}
}

#[cfg(test)]
mod tests {
use super::*;
use vm_memory::{Address, GuestAddress, GuestMemoryMmap};

const FDT_MAX_SIZE: usize = 0x20;
const MEM_SIZE: u64 = 0x100_0000;

fn create_guest_mem() -> GuestMemoryMmap {
GuestMemoryMmap::from_ranges(&[(GuestAddress(0x0), (MEM_SIZE as usize))]).unwrap()
}

#[derive(Clone, Copy, Default)]
struct FdtPlaceholder([u8; FDT_MAX_SIZE]);
unsafe impl ByteValued for FdtPlaceholder {}

#[test]
fn test_configure_fdt_boot() {
let fdt = FdtPlaceholder([0u8; FDT_MAX_SIZE]);
let guest_memory = create_guest_mem();

// Error case: FDT doesn't fit in guest memory.
let fdt_addr = guest_memory
.last_addr()
.checked_sub(FDT_MAX_SIZE as u64 - 2)
.unwrap();
assert_eq!(
FdtBootConfigurator::write_bootparams::<
FdtPlaceholder,
FdtPlaceholder,
FdtPlaceholder,
GuestMemoryMmap,
>(BootParams::new(fdt, fdt_addr, None, None), &guest_memory,)
.err(),
Some(Error::WriteFDTToMemory.into())
);

let fdt_addr = guest_memory
.last_addr()
.checked_sub(FDT_MAX_SIZE as u64 - 1)
.unwrap();
assert!(FdtBootConfigurator::write_bootparams::<
FdtPlaceholder,
FdtPlaceholder,
FdtPlaceholder,
GuestMemoryMmap,
>(BootParams::new(fdt, fdt_addr, None, None), &guest_memory,)
.is_ok());
}

#[test]
fn test_error_messages() {
assert_eq!(
format!("{}", Error::WriteFDTToMemory),
"Device Tree Boot Configurator Error: Error writing FDT in guest memory."
)
}
}
52 changes: 52 additions & 0 deletions src/configurator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,55 @@ where
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_error_messages() {
#[cfg(target_arch = "x86_64")]
{
// Linux
assert_eq!(
format!("{}", Error::Linux(linux::Error::ZeroPagePastRamEnd)),
"Boot Configurator Error: The zero page extends past the end of guest memory."
);
assert_eq!(
format!("{}", Error::Linux(linux::Error::ZeroPageSetup)),
"Boot Configurator Error: Error writing to the zero page of guest memory."
);

// PVH
assert_eq!(
format!("{}", Error::Pvh(pvh::Error::MemmapTableMissing)),
"Boot Configurator Error: No memory map was passed to the boot configurator."
);
assert_eq!(
format!("{}", Error::Pvh(pvh::Error::MemmapTablePastRamEnd)),
"Boot Configurator Error: \
The memory map table extends past the end of guest memory."
);
assert_eq!(
format!("{}", Error::Pvh(pvh::Error::MemmapTableSetup)),
"Boot Configurator Error: Error writing memory map table to guest memory."
);
assert_eq!(
format!("{}", Error::Pvh(pvh::Error::StartInfoPastRamEnd)),
"Boot Configurator Error: \
The hvm_start_info structure extends past the end of guest memory."
);
assert_eq!(
format!("{}", Error::Pvh(pvh::Error::StartInfoSetup)),
"Boot Configurator Error: Error writing hvm_start_info to guest memory."
);
}

#[cfg(target_arch = "aarch64")]
// FDT
assert_eq!(
format!("{}", Error::Fdt(fdt::Error::WriteFDTToMemory)),
"Boot Configurator Error: Error writing FDT in guest memory."
);
}
}
14 changes: 13 additions & 1 deletion src/configurator/x86_64/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ mod tests {
const KERNEL_HDR_MAGIC: u32 = 0x53726448;
const KERNEL_LOADER_OTHER: u8 = 0xff;
const KERNEL_MIN_ALIGNMENT_BYTES: u32 = 0x1000000;
const MEM_SIZE: u64 = 0x1000000;
const MEM_SIZE: u64 = 0x100_0000;

fn create_guest_mem() -> GuestMemoryMmap {
GuestMemoryMmap::from_ranges(&[(GuestAddress(0x0), (MEM_SIZE as usize))]).unwrap()
Expand Down Expand Up @@ -153,4 +153,16 @@ mod tests {
)
.is_ok());
}

#[test]
fn test_error_messages() {
assert_eq!(
format!("{}", Error::ZeroPagePastRamEnd),
"Linux Boot Configurator Error: The zero page extends past the end of guest memory."
);
assert_eq!(
format!("{}", Error::ZeroPageSetup),
"Linux Boot Configurator Error: Error writing to the zero page of guest memory."
);
}
}
22 changes: 20 additions & 2 deletions src/configurator/x86_64/pvh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl StdError for Error {
fn description(&self) -> &str {
use Error::*;
match self {
MemmapTableMissing => "No memory map wasn't passed to the boot configurator.",
MemmapTableMissing => "No memory map was passed to the boot configurator.",
MemmapTablePastRamEnd => "The memory map table extends past the end of guest memory.",
MemmapTableSetup => "Error writing memory map table to guest memory.",
StartInfoPastRamEnd => {
Expand Down Expand Up @@ -135,7 +135,7 @@ mod tests {
use vm_memory::{Address, GuestAddress, GuestMemoryMmap};

const XEN_HVM_START_MAGIC_VALUE: u32 = 0x336ec578;
const MEM_SIZE: u64 = 0x1000000;
const MEM_SIZE: u64 = 0x100_0000;
const E820_RAM: u32 = 1;

fn create_guest_mem() -> GuestMemoryMmap {
Expand Down Expand Up @@ -258,4 +258,22 @@ mod tests {
)
.is_ok());
}

#[test]
fn test_error_messages() {
assert_eq!(
format!("{}", Error::MemmapTableMissing),
"PVH Boot Configurator Error: No memory map was passed to the boot configurator."
);
assert_eq!(format!("{}", Error::MemmapTablePastRamEnd), "PVH Boot Configurator Error: The memory map table extends past the end of guest memory.");
assert_eq!(
format!("{}", Error::MemmapTableSetup),
"PVH Boot Configurator Error: Error writing memory map table to guest memory."
);
assert_eq!(format!("{}", Error::StartInfoPastRamEnd), "PVH Boot Configurator Error: The hvm_start_info structure extends past the end of guest memory.");
assert_eq!(
format!("{}", Error::StartInfoSetup),
"PVH Boot Configurator Error: Error writing hvm_start_info to guest memory."
);
}
}
2 changes: 1 addition & 1 deletion src/loader/aarch64/pe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ mod tests {
use std::io::Cursor;
use vm_memory::{Address, GuestAddress, GuestMemoryMmap};

const MEM_SIZE: u64 = 0x1000000;
const MEM_SIZE: u64 = 0x100_0000;

fn create_guest_mem() -> GuestMemoryMmap {
GuestMemoryMmap::from_ranges(&[(GuestAddress(0x0), (MEM_SIZE as usize))]).unwrap()
Expand Down
2 changes: 1 addition & 1 deletion src/loader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ mod tests {
use super::*;
use vm_memory::{Address, GuestAddress, GuestMemoryMmap};

const MEM_SIZE: u64 = 0x1000000;
const MEM_SIZE: u64 = 0x100_0000;

fn create_guest_mem() -> GuestMemoryMmap {
GuestMemoryMmap::from_ranges(&[(GuestAddress(0x0), (MEM_SIZE as usize))]).unwrap()
Expand Down
2 changes: 1 addition & 1 deletion src/loader/x86_64/bzimage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ mod tests {
use std::io::Cursor;
use vm_memory::{Address, GuestAddress, GuestMemoryMmap};

const MEM_SIZE: u64 = 0x1000000;
const MEM_SIZE: u64 = 0x100_0000;

fn create_guest_mem() -> GuestMemoryMmap {
GuestMemoryMmap::from_ranges(&[(GuestAddress(0x0), (MEM_SIZE as usize))]).unwrap()
Expand Down
2 changes: 1 addition & 1 deletion src/loader/x86_64/elf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ mod tests {
use std::io::Cursor;
use vm_memory::{Address, GuestAddress, GuestMemoryMmap};

const MEM_SIZE: u64 = 0x1000000;
const MEM_SIZE: u64 = 0x100_0000;

fn create_guest_mem() -> GuestMemoryMmap {
GuestMemoryMmap::from_ranges(&[(GuestAddress(0x0), (MEM_SIZE as usize))]).unwrap()
Expand Down

0 comments on commit 9277d02

Please sign in to comment.