Skip to content

Commit 6d499ac

Browse files
committed
multiboot2: add test for custom tags
1 parent f41410c commit 6d499ac

File tree

1 file changed

+70
-2
lines changed

1 file changed

+70
-2
lines changed

multiboot2/src/lib.rs

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ impl Reader {
472472
#[cfg(test)]
473473
mod tests {
474474
use super::*;
475+
use std::{mem, slice};
475476

476477
#[test]
477478
fn no_tags() {
@@ -1358,7 +1359,7 @@ mod tests {
13581359
let bi = bi.unwrap();
13591360
assert_eq!(addr, bi.start_address());
13601361
assert_eq!(addr + bytes.0.len(), bi.end_address());
1361-
assert_eq!(bytes.0.len(), bi.total_size() as usize);
1362+
assert_eq!(bytes.0.len(), bi.total_size());
13621363
let es = bi.elf_sections_tag().unwrap();
13631364
let mut s = es.sections();
13641365
let s1 = s.next().unwrap();
@@ -1412,7 +1413,7 @@ mod tests {
14121413
let bi = bi.unwrap();
14131414
assert_eq!(addr, bi.start_address());
14141415
assert_eq!(addr + bytes.0.len(), bi.end_address());
1415-
assert_eq!(bytes.0.len(), bi.total_size() as usize);
1416+
assert_eq!(bytes.0.len(), bi.total_size());
14161417
let efi_memory_map = bi.efi_memory_map_tag().unwrap();
14171418
let mut efi_mmap_iter = efi_memory_map.memory_areas();
14181419
let desc = efi_mmap_iter.next().unwrap();
@@ -1459,4 +1460,71 @@ mod tests {
14591460
core::mem::transmute::<[u8; 56], EFIMemoryMapTag>([0u8; 56]);
14601461
}
14611462
}
1463+
1464+
#[test]
1465+
fn custom_tag() {
1466+
const CUSTOM_TAG_ID: u32 = 0x1337;
1467+
1468+
#[repr(C, align(8))]
1469+
struct Bytes([u8; 32]);
1470+
let bytes: Bytes = Bytes([
1471+
32,
1472+
0,
1473+
0,
1474+
0, // total_size
1475+
0,
1476+
0,
1477+
0,
1478+
0, // reserved
1479+
// my custom tag
1480+
CUSTOM_TAG_ID.to_ne_bytes()[0],
1481+
CUSTOM_TAG_ID.to_ne_bytes()[1],
1482+
CUSTOM_TAG_ID.to_ne_bytes()[2],
1483+
CUSTOM_TAG_ID.to_ne_bytes()[3],
1484+
13,
1485+
0,
1486+
0,
1487+
0, // tag size
1488+
110,
1489+
97,
1490+
109,
1491+
101, // ASCII string 'name'
1492+
0,
1493+
0,
1494+
0,
1495+
0, // null byte + padding
1496+
0,
1497+
0,
1498+
0,
1499+
0, // end tag type
1500+
8,
1501+
0,
1502+
0,
1503+
0, // end tag size
1504+
]);
1505+
let addr = bytes.0.as_ptr() as usize;
1506+
let bi = unsafe { load(addr) };
1507+
let bi = bi.unwrap();
1508+
assert_eq!(addr, bi.start_address());
1509+
assert_eq!(addr + bytes.0.len(), bi.end_address());
1510+
assert_eq!(bytes.0.len(), bi.total_size());
1511+
1512+
#[repr(C, align(8))]
1513+
struct CustomTag {
1514+
tag: TagTypeId,
1515+
size: u32,
1516+
name: u8,
1517+
}
1518+
1519+
let tag = bi
1520+
.get_tag(CUSTOM_TAG_ID.into())
1521+
.unwrap()
1522+
.cast_tag::<CustomTag>();
1523+
1524+
// strlen without null byte
1525+
let strlen = tag.size as usize - mem::size_of::<CommandLineTag>();
1526+
let bytes = unsafe { slice::from_raw_parts((&tag.name) as *const u8, strlen) };
1527+
let name = core::str::from_utf8(bytes).unwrap();
1528+
assert_eq!(name, "name");
1529+
}
14621530
}

0 commit comments

Comments
 (0)