Skip to content

Commit 4069622

Browse files
committed
add test for custom tags
1 parent 5f3c5a4 commit 4069622

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

multiboot2/src/lib.rs

+64
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,70 @@ mod tests {
550550
assert!(bi.command_line_tag().is_none());
551551
}
552552

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

0 commit comments

Comments
 (0)