Skip to content

Commit

Permalink
mm: Add unit tests for kernel mapping, guestmem ops, address checks
Browse files Browse the repository at this point in the history
Add unit tests to evaluate offline correct behavior of memory components:
kernel mappings (address_space.rs), read and write operations (guestmem.rs)
and valid physical address checking (memory.rs).

Signed-off-by: Carlos Bilbao <[email protected]>
  • Loading branch information
Zildj1an committed Oct 26, 2023
1 parent 6138015 commit 44c99a7
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/mm/address_space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,65 @@ pub const SVSM_PERTASK_DYNAMIC_MEMORY: VirtAddr = SVSM_PERTASK_BASE;
/// Task stack
pub const SVSM_PERTASK_STACK_BASE: VirtAddr = SVSM_PERTASK_BASE.const_add(0xffffff0000);
pub const SVSM_PERTASK_STACK_TOP: VirtAddr = SVSM_PERTASK_STACK_BASE.const_add(0x10000);

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

fn init_km_testing() {
KERNEL_MAPPING
.init(&KernelMapping {
virt_start: VirtAddr::new(0x1000),
virt_end: VirtAddr::new(0x2000),
phys_start: PhysAddr::new(0x3000),
})
.unwrap();
}

#[test]
fn test_init_kernel_mapping_info() {
init_km_testing();

let km = &KERNEL_MAPPING;

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")]
fn test_virt_to_phys() {
let vaddr = VirtAddr::new(0x1500);
let paddr = virt_to_phys(vaddr);

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

#[test]
#[cfg(not(target_os = "none"))]
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")]
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"))]
fn test_phys_to_virt() {
let paddr = PhysAddr::new(0x4500);
let vaddr = phys_to_virt(paddr);

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

mod tests {

#[test]
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 = &test_buffer as *const u8 as usize;

let result = read_u8(VirtAddr::new(test_address)).unwrap();

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

#[test]
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 = &mut test_buffer as *mut u8 as usize;
let data_to_write = 0x42;

write_u8(VirtAddr::new(test_address), data_to_write).unwrap();

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

valid_phys_address(paddr)
}

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

#[test]
fn test_valid_phys_address() {
let region = MemoryRegion {
start: 0x1000,
end: 0x2000,
};

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)));
}
}

0 comments on commit 44c99a7

Please sign in to comment.