Skip to content

Commit 73e97a0

Browse files
committed
multiboot2: add test for custom tags
1 parent 07e9a25 commit 73e97a0

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

multiboot2/src/lib.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ impl Reader {
483483
#[cfg(test)]
484484
mod tests {
485485
use super::*;
486+
use std::{mem, slice};
486487

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

0 commit comments

Comments
 (0)