Skip to content

Commit 6259ed5

Browse files
committed
uefi: mem: unit test cleanup and streamlining
1 parent d261ab1 commit 6259ed5

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

uefi/src/mem/memory_map/mod.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -100,19 +100,20 @@ impl MemoryMapMeta {
100100
}
101101
}
102102

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

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

114-
let mmap = MemoryMapBackingMemory::from_slice(mmap);
115-
MemoryMapOwned::from_initialized_mem(
116+
MemoryMapRefMut::new(
116117
mmap,
117118
MemoryMapMeta {
118119
map_size: mmap_len,
@@ -121,6 +122,7 @@ mod tests_mmap_artificial {
121122
desc_version: MemoryDescriptor::VERSION,
122123
},
123124
)
125+
.unwrap()
124126
}
125127

126128
#[test]
@@ -157,7 +159,7 @@ mod tests_mmap_artificial {
157159
mem_map.sort();
158160

159161
if !is_sorted(&mem_map.entries()) {
160-
panic!("mem_map is not sorted: {}", mem_map);
162+
panic!("mem_map is not sorted: {:?}", mem_map);
161163
}
162164
}
163165

@@ -210,17 +212,6 @@ mod tests_mmap_artificial {
210212
assert_ne!(*desc, BUFFER[2]);
211213
}
212214

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

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

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

0 commit comments

Comments
 (0)