Skip to content

Commit 2abe692

Browse files
committed
uefi: mem: unit test cleanup and streamlining
1 parent ac36fce commit 2abe692

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

uefi/src/mem/memory_map/mod.rs

+13-17
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,20 @@ impl MemoryMapMeta {
9999
}
100100
}
101101

102+
/// Comprehensive unit test of the memory map functionality with the simplified
103+
/// data. Here, `desc_size` equals `size_of::<MemoryDescriptor`.
102104
#[cfg(test)]
103105
mod tests_mmap_artificial {
104106
use super::*;
105107
use core::mem::{size_of, size_of_val};
106108

107-
fn buffer_to_map(buffer: &mut [MemoryDescriptor]) -> MemoryMapOwned {
109+
fn buffer_to_map(buffer: &mut [MemoryDescriptor]) -> MemoryMapRefMut {
108110
let mmap_len = size_of_val(buffer);
109111
let mmap = {
110112
unsafe { core::slice::from_raw_parts_mut(buffer.as_mut_ptr() as *mut u8, mmap_len) }
111113
};
112114

113-
let mmap = MemoryMapBackingMemory::from_slice(mmap);
114-
MemoryMapOwned::from_initialized_mem(
115+
MemoryMapRefMut::new(
115116
mmap,
116117
MemoryMapMeta {
117118
map_size: mmap_len,
@@ -120,6 +121,7 @@ mod tests_mmap_artificial {
120121
desc_version: MemoryDescriptor::VERSION,
121122
},
122123
)
124+
.unwrap()
123125
}
124126

125127
#[test]
@@ -156,7 +158,7 @@ mod tests_mmap_artificial {
156158
mem_map.sort();
157159

158160
if !is_sorted(&mem_map.entries()) {
159-
panic!("mem_map is not sorted: {}", mem_map);
161+
panic!("mem_map is not sorted: {:?}", mem_map);
160162
}
161163
}
162164

@@ -209,17 +211,6 @@ mod tests_mmap_artificial {
209211
assert_ne!(*desc, BUFFER[2]);
210212
}
211213

212-
// Added for debug purposes on test failure
213-
impl core::fmt::Display for MemoryMapOwned {
214-
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
215-
writeln!(f)?;
216-
for desc in self.entries() {
217-
writeln!(f, "{:?}", desc)?;
218-
}
219-
Ok(())
220-
}
221-
}
222-
223214
fn is_sorted(iter: &MemoryMapIter) -> bool {
224215
let mut iter = iter.clone();
225216
let mut curr_start;
@@ -240,6 +231,9 @@ mod tests_mmap_artificial {
240231
}
241232
}
242233

234+
/// Comprehensive unit test of the memory map functionality with the data from a
235+
/// real UEFI memory map. The important property that we test here is that
236+
/// the reported `desc_size` doesn't equal `size_of::<MemoryDescriptor`.
243237
#[cfg(test)]
244238
mod tests_mmap_real {
245239
use super::*;
@@ -266,9 +260,11 @@ mod tests_mmap_real {
266260
let mut buf = MMAP_RAW;
267261
let buf =
268262
unsafe { slice::from_raw_parts_mut(buf.as_mut_ptr().cast::<u8>(), MMAP_META.map_size) };
269-
let buf = MemoryMapBackingMemory::from_slice(buf);
270-
let mut mmap = MemoryMapOwned::from_initialized_mem(buf, MMAP_META);
263+
let mut mmap = MemoryMapRefMut::new(buf, MMAP_META).unwrap();
264+
265+
assert!(mmap.is_sorted());
271266
mmap.sort();
267+
assert!(mmap.is_sorted());
272268

273269
let entries = mmap.entries().copied().collect::<Vec<_>>();
274270

0 commit comments

Comments
 (0)