Skip to content

Commit 9277d02

Browse files
author
Alexandra Iordache
committed
boot configurator: more unit tests
Signed-off-by: Alexandra Iordache <aghecen@amazon.com>
1 parent 89de317 commit 9277d02

File tree

10 files changed

+150
-9
lines changed

10 files changed

+150
-9
lines changed

coverage_config_aarch64.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"coverage_score": 75.7,
2+
"coverage_score": 76.4,
33
"exclude_path": "",
44
"crate_features": ""
55
}

coverage_config_x86_64.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"coverage_score": 74.8,
2+
"coverage_score": 77.6,
33
"exclude_path": "",
44
"crate_features": ""
55
}

src/configurator/aarch64/fdt.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,62 @@ impl BootConfigurator for FdtBootConfigurator {
6666
.map_err(|_| Error::WriteFDTToMemory.into())
6767
}
6868
}
69+
70+
#[cfg(test)]
71+
mod tests {
72+
use super::*;
73+
use vm_memory::{Address, GuestAddress, GuestMemoryMmap};
74+
75+
const FDT_MAX_SIZE: usize = 0x20;
76+
const MEM_SIZE: u64 = 0x100_0000;
77+
78+
fn create_guest_mem() -> GuestMemoryMmap {
79+
GuestMemoryMmap::from_ranges(&[(GuestAddress(0x0), (MEM_SIZE as usize))]).unwrap()
80+
}
81+
82+
#[derive(Clone, Copy, Default)]
83+
struct FdtPlaceholder([u8; FDT_MAX_SIZE]);
84+
unsafe impl ByteValued for FdtPlaceholder {}
85+
86+
#[test]
87+
fn test_configure_fdt_boot() {
88+
let fdt = FdtPlaceholder([0u8; FDT_MAX_SIZE]);
89+
let guest_memory = create_guest_mem();
90+
91+
// Error case: FDT doesn't fit in guest memory.
92+
let fdt_addr = guest_memory
93+
.last_addr()
94+
.checked_sub(FDT_MAX_SIZE as u64 - 2)
95+
.unwrap();
96+
assert_eq!(
97+
FdtBootConfigurator::write_bootparams::<
98+
FdtPlaceholder,
99+
FdtPlaceholder,
100+
FdtPlaceholder,
101+
GuestMemoryMmap,
102+
>(BootParams::new(fdt, fdt_addr, None, None), &guest_memory,)
103+
.err(),
104+
Some(Error::WriteFDTToMemory.into())
105+
);
106+
107+
let fdt_addr = guest_memory
108+
.last_addr()
109+
.checked_sub(FDT_MAX_SIZE as u64 - 1)
110+
.unwrap();
111+
assert!(FdtBootConfigurator::write_bootparams::<
112+
FdtPlaceholder,
113+
FdtPlaceholder,
114+
FdtPlaceholder,
115+
GuestMemoryMmap,
116+
>(BootParams::new(fdt, fdt_addr, None, None), &guest_memory,)
117+
.is_ok());
118+
}
119+
120+
#[test]
121+
fn test_error_messages() {
122+
assert_eq!(
123+
format!("{}", Error::WriteFDTToMemory),
124+
"Device Tree Boot Configurator Error: Error writing FDT in guest memory."
125+
)
126+
}
127+
}

src/configurator/mod.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,55 @@ where
185185
}
186186
}
187187
}
188+
189+
#[cfg(test)]
190+
mod tests {
191+
use super::*;
192+
193+
#[test]
194+
fn test_error_messages() {
195+
#[cfg(target_arch = "x86_64")]
196+
{
197+
// Linux
198+
assert_eq!(
199+
format!("{}", Error::Linux(linux::Error::ZeroPagePastRamEnd)),
200+
"Boot Configurator Error: The zero page extends past the end of guest memory."
201+
);
202+
assert_eq!(
203+
format!("{}", Error::Linux(linux::Error::ZeroPageSetup)),
204+
"Boot Configurator Error: Error writing to the zero page of guest memory."
205+
);
206+
207+
// PVH
208+
assert_eq!(
209+
format!("{}", Error::Pvh(pvh::Error::MemmapTableMissing)),
210+
"Boot Configurator Error: No memory map was passed to the boot configurator."
211+
);
212+
assert_eq!(
213+
format!("{}", Error::Pvh(pvh::Error::MemmapTablePastRamEnd)),
214+
"Boot Configurator Error: \
215+
The memory map table extends past the end of guest memory."
216+
);
217+
assert_eq!(
218+
format!("{}", Error::Pvh(pvh::Error::MemmapTableSetup)),
219+
"Boot Configurator Error: Error writing memory map table to guest memory."
220+
);
221+
assert_eq!(
222+
format!("{}", Error::Pvh(pvh::Error::StartInfoPastRamEnd)),
223+
"Boot Configurator Error: \
224+
The hvm_start_info structure extends past the end of guest memory."
225+
);
226+
assert_eq!(
227+
format!("{}", Error::Pvh(pvh::Error::StartInfoSetup)),
228+
"Boot Configurator Error: Error writing hvm_start_info to guest memory."
229+
);
230+
}
231+
232+
#[cfg(target_arch = "aarch64")]
233+
// FDT
234+
assert_eq!(
235+
format!("{}", Error::Fdt(fdt::Error::WriteFDTToMemory)),
236+
"Boot Configurator Error: Error writing FDT in guest memory."
237+
);
238+
}
239+
}

src/configurator/x86_64/linux.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ mod tests {
9999
const KERNEL_HDR_MAGIC: u32 = 0x53726448;
100100
const KERNEL_LOADER_OTHER: u8 = 0xff;
101101
const KERNEL_MIN_ALIGNMENT_BYTES: u32 = 0x1000000;
102-
const MEM_SIZE: u64 = 0x1000000;
102+
const MEM_SIZE: u64 = 0x100_0000;
103103

104104
fn create_guest_mem() -> GuestMemoryMmap {
105105
GuestMemoryMmap::from_ranges(&[(GuestAddress(0x0), (MEM_SIZE as usize))]).unwrap()
@@ -153,4 +153,16 @@ mod tests {
153153
)
154154
.is_ok());
155155
}
156+
157+
#[test]
158+
fn test_error_messages() {
159+
assert_eq!(
160+
format!("{}", Error::ZeroPagePastRamEnd),
161+
"Linux Boot Configurator Error: The zero page extends past the end of guest memory."
162+
);
163+
assert_eq!(
164+
format!("{}", Error::ZeroPageSetup),
165+
"Linux Boot Configurator Error: Error writing to the zero page of guest memory."
166+
);
167+
}
156168
}

src/configurator/x86_64/pvh.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl StdError for Error {
4343
fn description(&self) -> &str {
4444
use Error::*;
4545
match self {
46-
MemmapTableMissing => "No memory map wasn't passed to the boot configurator.",
46+
MemmapTableMissing => "No memory map was passed to the boot configurator.",
4747
MemmapTablePastRamEnd => "The memory map table extends past the end of guest memory.",
4848
MemmapTableSetup => "Error writing memory map table to guest memory.",
4949
StartInfoPastRamEnd => {
@@ -135,7 +135,7 @@ mod tests {
135135
use vm_memory::{Address, GuestAddress, GuestMemoryMmap};
136136

137137
const XEN_HVM_START_MAGIC_VALUE: u32 = 0x336ec578;
138-
const MEM_SIZE: u64 = 0x1000000;
138+
const MEM_SIZE: u64 = 0x100_0000;
139139
const E820_RAM: u32 = 1;
140140

141141
fn create_guest_mem() -> GuestMemoryMmap {
@@ -258,4 +258,22 @@ mod tests {
258258
)
259259
.is_ok());
260260
}
261+
262+
#[test]
263+
fn test_error_messages() {
264+
assert_eq!(
265+
format!("{}", Error::MemmapTableMissing),
266+
"PVH Boot Configurator Error: No memory map was passed to the boot configurator."
267+
);
268+
assert_eq!(format!("{}", Error::MemmapTablePastRamEnd), "PVH Boot Configurator Error: The memory map table extends past the end of guest memory.");
269+
assert_eq!(
270+
format!("{}", Error::MemmapTableSetup),
271+
"PVH Boot Configurator Error: Error writing memory map table to guest memory."
272+
);
273+
assert_eq!(format!("{}", Error::StartInfoPastRamEnd), "PVH Boot Configurator Error: The hvm_start_info structure extends past the end of guest memory.");
274+
assert_eq!(
275+
format!("{}", Error::StartInfoSetup),
276+
"PVH Boot Configurator Error: Error writing hvm_start_info to guest memory."
277+
);
278+
}
261279
}

src/loader/aarch64/pe/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ mod tests {
195195
use std::io::Cursor;
196196
use vm_memory::{Address, GuestAddress, GuestMemoryMmap};
197197

198-
const MEM_SIZE: u64 = 0x1000000;
198+
const MEM_SIZE: u64 = 0x100_0000;
199199

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

src/loader/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ mod tests {
196196
use super::*;
197197
use vm_memory::{Address, GuestAddress, GuestMemoryMmap};
198198

199-
const MEM_SIZE: u64 = 0x1000000;
199+
const MEM_SIZE: u64 = 0x100_0000;
200200

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

src/loader/x86_64/bzimage/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ mod tests {
179179
use std::io::Cursor;
180180
use vm_memory::{Address, GuestAddress, GuestMemoryMmap};
181181

182-
const MEM_SIZE: u64 = 0x1000000;
182+
const MEM_SIZE: u64 = 0x100_0000;
183183

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

src/loader/x86_64/elf/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ mod tests {
359359
use std::io::Cursor;
360360
use vm_memory::{Address, GuestAddress, GuestMemoryMmap};
361361

362-
const MEM_SIZE: u64 = 0x1000000;
362+
const MEM_SIZE: u64 = 0x100_0000;
363363

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

0 commit comments

Comments
 (0)