Skip to content

Commit 210b9e8

Browse files
committed
add test for custom tags
1 parent 96a1313 commit 210b9e8

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

multiboot2/src/lib.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,70 @@ mod tests {
551551
assert!(bi.command_line_tag().is_none());
552552
}
553553

554+
#[test]
555+
fn custom_tag() {
556+
const CUSTOM_TAG_ID: u32 = 0x1337;
557+
558+
#[repr(C, align(8))]
559+
struct Bytes([u8; 32]);
560+
let bytes: Bytes = Bytes([
561+
32,
562+
0,
563+
0,
564+
0, // total_size
565+
0,
566+
0,
567+
0,
568+
0, // reserved
569+
// my custom tag
570+
CUSTOM_TAG_ID.to_ne_bytes()[0],
571+
CUSTOM_TAG_ID.to_ne_bytes()[1],
572+
CUSTOM_TAG_ID.to_ne_bytes()[2],
573+
CUSTOM_TAG_ID.to_ne_bytes()[3],
574+
13,
575+
0,
576+
0,
577+
0, // tag size
578+
110,
579+
97,
580+
109,
581+
101, // ASCII string 'name'
582+
0,
583+
0,
584+
0,
585+
0, // null byte + padding
586+
0,
587+
0,
588+
0,
589+
0, // end tag type
590+
8,
591+
0,
592+
0,
593+
0, // end tag size
594+
]);
595+
let addr = bytes.0.as_ptr() as usize;
596+
let bi = unsafe { load(addr) };
597+
let bi = bi.unwrap();
598+
assert_eq!(addr, bi.start_address());
599+
assert_eq!(addr + bytes.0.len(), bi.end_address());
600+
assert_eq!(bytes.0.len(), bi.total_size());
601+
602+
#[repr(C, align(8))]
603+
struct CustomTag {
604+
tag: SpecifiedOrCustomTagTypeSerialized,
605+
size: u32,
606+
name: u8,
607+
}
608+
609+
let tag = bi
610+
.get_tag(SpecifiedOrCustomTagTypeSerialized::from(CUSTOM_TAG_ID).into())
611+
.unwrap()
612+
.cast_tag::<CustomTag>();
613+
let name = &tag.name as *const u8 as *const c_char;
614+
let str = unsafe { CStr::from_ptr(name).to_str().unwrap() };
615+
assert_eq!(str, "name");
616+
}
617+
554618
#[test]
555619
/// Compile time test for `BootLoaderNameTag`.
556620
fn name_tag_size() {

0 commit comments

Comments
 (0)