Skip to content

Commit 2293f53

Browse files
committed
uefi: mem: basic unit test for MemoryMapRef and MemoryMapRefMut
1 parent 2cb3ff0 commit 2293f53

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

uefi/src/mem/memory_map/api.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ pub trait MemoryMap: Debug + Index<usize, Output = MemoryDescriptor> {
6565
}
6666

6767
/// Returns a reference to the underlying memory.
68+
#[must_use]
6869
fn buffer(&self) -> &[u8];
6970

7071
/// Returns an Iterator of type [`MemoryMapIter`].
@@ -74,6 +75,22 @@ pub trait MemoryMap: Debug + Index<usize, Output = MemoryDescriptor> {
7475
/// might yield unexpected results.
7576
#[must_use]
7677
fn entries(&self) -> MemoryMapIter<'_>;
78+
79+
/// Returns if the underlying memory map is sorted regarding the physical
80+
/// address start.
81+
#[must_use]
82+
fn is_sorted(&self) -> bool {
83+
let iter = self.entries();
84+
let iter = iter.clone().zip(iter.skip(1));
85+
86+
for (curr, next) in iter {
87+
if next.phys_start < curr.phys_start {
88+
log::debug!("next.phys_start < curr.phys_start: curr={curr:?}, next={next:?}");
89+
return false;
90+
}
91+
}
92+
true
93+
}
7794
}
7895

7996
/// Extension to [`MemoryMap`] that adds mutable operations. This also includes

uefi/src/mem/memory_map/impl_.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,3 +441,85 @@ impl IndexMut<usize> for MemoryMapOwned {
441441
self.get_mut(index).unwrap()
442442
}
443443
}
444+
445+
#[cfg(test)]
446+
mod tests {
447+
use super::*;
448+
use alloc::vec::Vec;
449+
use core::mem::size_of;
450+
451+
const BASE_MMAP_UNSORTED: [MemoryDescriptor; 3] = [
452+
MemoryDescriptor {
453+
ty: MemoryType::CONVENTIONAL,
454+
phys_start: 0x3000,
455+
virt_start: 0x3000,
456+
page_count: 1,
457+
att: MemoryAttribute::WRITE_BACK,
458+
},
459+
MemoryDescriptor {
460+
ty: MemoryType::CONVENTIONAL,
461+
phys_start: 0x2000,
462+
virt_start: 0x2000,
463+
page_count: 1,
464+
att: MemoryAttribute::WRITE_BACK,
465+
},
466+
MemoryDescriptor {
467+
ty: MemoryType::CONVENTIONAL,
468+
phys_start: 0x1000,
469+
virt_start: 0x1000,
470+
page_count: 1,
471+
att: MemoryAttribute::WRITE_BACK,
472+
},
473+
];
474+
475+
/// Returns a copy of [`BASE_MMAP_UNSORTED`] owned on the stack.
476+
fn new_mmap_memory() -> [MemoryDescriptor; 3] {
477+
BASE_MMAP_UNSORTED
478+
}
479+
480+
fn mmap_raw<'a>(memory: &mut [MemoryDescriptor]) -> (&'a mut [u8], MemoryMapMeta) {
481+
let desc_size = size_of::<MemoryDescriptor>();
482+
let len = memory.len() * desc_size;
483+
let ptr = memory.as_mut_ptr().cast::<u8>();
484+
let slice = unsafe { core::slice::from_raw_parts_mut(ptr, len) };
485+
let meta = MemoryMapMeta {
486+
map_size: len,
487+
desc_size,
488+
map_key: Default::default(),
489+
desc_version: MemoryDescriptor::VERSION,
490+
};
491+
(slice, meta)
492+
}
493+
494+
/// Basic sanity checks for the type [`MemoryMapRef`].
495+
#[test]
496+
fn memory_map_ref() {
497+
let mut memory = new_mmap_memory();
498+
let (mmap, meta) = mmap_raw(&mut memory);
499+
let mmap = MemoryMapRef::new(mmap, meta, None).unwrap();
500+
501+
assert_eq!(mmap.entries().count(), 3);
502+
assert_eq!(
503+
mmap.entries().copied().collect::<Vec<_>>().as_slice(),
504+
&BASE_MMAP_UNSORTED
505+
);
506+
assert!(!mmap.is_sorted());
507+
}
508+
509+
/// Basic sanity checks for the type [`MemoryMapRefMut`].
510+
#[test]
511+
fn memory_map_ref_mut() {
512+
let mut memory = new_mmap_memory();
513+
let (mmap, meta) = mmap_raw(&mut memory);
514+
let mut mmap = MemoryMapRefMut::new(mmap, meta, None).unwrap();
515+
516+
assert_eq!(mmap.entries().count(), 3);
517+
assert_eq!(
518+
mmap.entries().copied().collect::<Vec<_>>().as_slice(),
519+
&BASE_MMAP_UNSORTED
520+
);
521+
assert!(!mmap.is_sorted());
522+
mmap.sort();
523+
assert!(mmap.is_sorted());
524+
}
525+
}

0 commit comments

Comments
 (0)