Skip to content

Commit a57435e

Browse files
committed
uefi: mem: basic unit test for MemoryMapRef and MemoryMapRefMut
1 parent b53b7e2 commit a57435e

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`].
@@ -76,6 +77,22 @@ pub trait MemoryMap: Debug + Index<usize, Output = MemoryDescriptor> {
7677
/// might yield unexpected results.
7778
#[must_use]
7879
fn entries(&self) -> MemoryMapIter<'_>;
80+
81+
/// Returns if the underlying memory map is sorted regarding the physical
82+
/// address start.
83+
#[must_use]
84+
fn is_sorted(&self) -> bool {
85+
let iter = self.entries();
86+
let iter = iter.clone().zip(iter.skip(1));
87+
88+
for (curr, next) in iter {
89+
if next.phys_start < curr.phys_start {
90+
log::debug!("next.phys_start < curr.phys_start: curr={curr:?}, next={next:?}");
91+
return false;
92+
}
93+
}
94+
true
95+
}
7996
}
8097

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

0 commit comments

Comments
 (0)