Skip to content

Commit 27b7a84

Browse files
committed
multiboot2: add test for custom tags
1 parent 3aeb2d8 commit 27b7a84

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

multiboot2/src/lib.rs

+67
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ impl Reader {
478478
#[cfg(test)]
479479
mod tests {
480480
use super::*;
481+
use std::{mem, slice};
481482

482483
#[test]
483484
fn no_tags() {
@@ -1481,4 +1482,70 @@ mod tests {
14811482
fn consumer<E: core::error::Error>(_e: E) {}
14821483
consumer(MbiLoadError::IllegalAddress)
14831484
}
1485+
1486+
fn custom_tag() {
1487+
const CUSTOM_TAG_ID: u32 = 0x1337;
1488+
1489+
#[repr(C, align(8))]
1490+
struct Bytes([u8; 32]);
1491+
let bytes: Bytes = Bytes([
1492+
32,
1493+
0,
1494+
0,
1495+
0, // total_size
1496+
0,
1497+
0,
1498+
0,
1499+
0, // reserved
1500+
// my custom tag
1501+
CUSTOM_TAG_ID.to_ne_bytes()[0],
1502+
CUSTOM_TAG_ID.to_ne_bytes()[1],
1503+
CUSTOM_TAG_ID.to_ne_bytes()[2],
1504+
CUSTOM_TAG_ID.to_ne_bytes()[3],
1505+
13,
1506+
0,
1507+
0,
1508+
0, // tag size
1509+
110,
1510+
97,
1511+
109,
1512+
101, // ASCII string 'name'
1513+
0,
1514+
0,
1515+
0,
1516+
0, // null byte + padding
1517+
0,
1518+
0,
1519+
0,
1520+
0, // end tag type
1521+
8,
1522+
0,
1523+
0,
1524+
0, // end tag size
1525+
]);
1526+
let addr = bytes.0.as_ptr() as usize;
1527+
let bi = unsafe { load(addr) };
1528+
let bi = bi.unwrap();
1529+
assert_eq!(addr, bi.start_address());
1530+
assert_eq!(addr + bytes.0.len(), bi.end_address());
1531+
assert_eq!(bytes.0.len(), bi.total_size());
1532+
1533+
#[repr(C, align(8))]
1534+
struct CustomTag {
1535+
tag: TagTypeId,
1536+
size: u32,
1537+
name: u8,
1538+
}
1539+
1540+
let tag = bi
1541+
.get_tag(CUSTOM_TAG_ID.into())
1542+
.unwrap()
1543+
.cast_tag::<CustomTag>();
1544+
1545+
// strlen without null byte
1546+
let strlen = tag.size as usize - mem::size_of::<CommandLineTag>();
1547+
let bytes = unsafe { slice::from_raw_parts((&tag.name) as *const u8, strlen) };
1548+
let name = core::str::from_utf8(bytes).unwrap();
1549+
assert_eq!(name, "name");
1550+
}
14841551
}

0 commit comments

Comments
 (0)