Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mm: Add unit tests for kernel mapping, guestmem ops, address checks #170

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions src/mm/address_space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,81 @@ pub const SVSM_PERTASK_END: VirtAddr = SVSM_PERTASK_BASE.const_add(SIZE_LEVEL3);

/// Kernel stack for a task
pub const SVSM_PERTASK_STACK_BASE: VirtAddr = SVSM_PERTASK_BASE;

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

static KERNEL_MAPPING_TEST: ImmutAfterInitCell<KernelMapping> = ImmutAfterInitCell::uninit();
static INITIALIZED: SpinLock<bool> = SpinLock::new(false);

#[test]
#[cfg_attr(test_in_svsm, ignore = "Offline testing")]
fn init_km_testing() {
let mut initialized = INITIALIZED.lock();
if *initialized {
return;
}
KERNEL_MAPPING_TEST
.init(&KernelMapping {
virt_start: VirtAddr::new(0x1000),
virt_end: VirtAddr::new(0x2000),
phys_start: PhysAddr::new(0x3000),
})
.unwrap();
*initialized = true;
}

#[test]
#[cfg_attr(test_in_svsm, ignore = "Offline testing")]
fn test_init_kernel_mapping_info() {
init_km_testing();

let km = &KERNEL_MAPPING_TEST;

assert_eq!(km.virt_start, VirtAddr::new(0x1000));
assert_eq!(km.virt_end, VirtAddr::new(0x2000));
assert_eq!(km.phys_start, PhysAddr::new(0x3000));
}

#[test]
#[cfg(target_os = "none")]
#[cfg_attr(test_in_svsm, ignore = "Offline testing")]
fn test_virt_to_phys() {
let vaddr = VirtAddr::new(0x1500);
let paddr = virt_to_phys(vaddr);

assert_eq!(paddr, PhysAddr::new(0x4500));
}
Zildj1an marked this conversation as resolved.
Show resolved Hide resolved

#[test]
#[cfg(not(target_os = "none"))]
#[cfg_attr(test_in_svsm, ignore = "Offline testing")]
fn test_virt_to_phys() {
let vaddr = VirtAddr::new(0x1500);
let paddr = virt_to_phys(vaddr);

assert_eq!(paddr, PhysAddr::new(0x1500));
}

#[test]
#[cfg(target_os = "none")]
#[cfg_attr(test_in_svsm, ignore = "Offline testing")]
fn test_phys_to_virt() {
let paddr = PhysAddr::new(0x4500);
let vaddr = phys_to_virt(paddr);

assert_eq!(vaddr, VirtAddr::new(0x1500));
}

#[test]
#[cfg(not(target_os = "none"))]
#[cfg_attr(test_in_svsm, ignore = "Offline testing")]
fn test_phys_to_virt() {
let paddr = PhysAddr::new(0x4500);
let vaddr = phys_to_virt(paddr);

assert_eq!(vaddr, VirtAddr::new(0x4500));
}
}
30 changes: 30 additions & 0 deletions src/mm/guestmem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,33 @@ impl<T: Sized + Copy> GuestPtr<T> {
unsafe { GuestPtr::from_ptr(self.ptr.offset(count)) }
}
}

mod tests {

#[test]
#[cfg_attr(miri, ignore)]
fn test_read_u8_valid_address() {
use crate::mm::guestmem::*;
// Create a region to read from
let test_buffer: [u8; 6] = [0; 6];
let test_address = VirtAddr::from(test_buffer.as_ptr());

let result = read_u8(test_address).unwrap();

assert_eq!(result, test_buffer[0]);
}

#[test]
#[cfg_attr(miri, ignore)]
fn test_write_u8_valid_address() {
use crate::mm::guestmem::*;
// Create a mutable region we can write into
let mut test_buffer: [u8; 6] = [0; 6];
let test_address = VirtAddr::from(test_buffer.as_mut_ptr());
let data_to_write = 0x42;

write_u8(test_address, data_to_write).unwrap();

assert_eq!(test_buffer[0], data_to_write);
}
}
22 changes: 22 additions & 0 deletions src/mm/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,25 @@ pub fn writable_phys_addr(paddr: PhysAddr) -> bool {

valid_phys_address(paddr)
}

#[cfg(test)]
#[cfg_attr(test_in_svsm, ignore = "Offline testing")]
mod tests {
use super::*;
use crate::address::PhysAddr;

#[test]
#[cfg_attr(test_in_svsm, ignore = "Offline testing")]
fn test_valid_phys_address() {
let start = PhysAddr::new(0x1000);
let end = PhysAddr::new(0x2000);
let region = MemoryRegion::from_addresses(start, end);

MEMORY_MAP.lock_write().push(region);

// Inside the region
assert!(valid_phys_address(PhysAddr::new(0x1500)));
// Outside the region
assert!(!valid_phys_address(PhysAddr::new(0x3000)));
}
}
Loading