aya: obj: improve parse_map_def tests

Add a test that checks that we handle ELF section padding correctly and
simplify the other tests.
pull/29/head
Alessandro Decina 3 years ago
parent b657930a3e
commit 21e01df242

@ -668,70 +668,61 @@ mod tests {
} }
#[test] #[test]
fn test_parse_map_def() { fn test_parse_map_def_error() {
assert!(matches!( assert!(matches!(
parse_map_def("foo", &[]), parse_map_def("foo", &[]),
Err(ParseError::InvalidMapDefinition { .. }) Err(ParseError::InvalidMapDefinition { .. })
)); ));
}
assert_eq!( #[test]
parse_map_def( fn test_parse_map_short() {
"foo", let def = bpf_map_def {
bytes_of(&bpf_map_def {
map_type: 1,
key_size: 2,
value_size: 3,
max_entries: 4,
map_flags: 5,
..Default::default()
})
)
.unwrap(),
bpf_map_def {
map_type: 1, map_type: 1,
key_size: 2, key_size: 2,
value_size: 3, value_size: 3,
max_entries: 4, max_entries: 4,
map_flags: 5, map_flags: 5,
..Default::default() id: 0,
} pinning: 0,
); };
assert_eq!( assert_eq!(
parse_map_def( parse_map_def("foo", &bytes_of(&def)[..MINIMUM_MAP_SIZE]).unwrap(),
"foo", def
&bytes_of(&bpf_map_def { );
map_type: 1, }
key_size: 2,
value_size: 3, #[test]
max_entries: 4, fn test_parse_map_def() {
map_flags: 5, let def = bpf_map_def {
..Default::default()
})[..(mem::size_of::<u32>() * 5)]
)
.unwrap(),
bpf_map_def {
map_type: 1, map_type: 1,
key_size: 2, key_size: 2,
value_size: 3, value_size: 3,
max_entries: 4, max_entries: 4,
map_flags: 5, map_flags: 5,
..Default::default() id: 6,
pinning: 7,
};
assert_eq!(parse_map_def("foo", bytes_of(&def)).unwrap(), def);
} }
);
let map = parse_map_def( #[test]
"foo", fn test_parse_map_def_with_padding() {
&bytes_of(&bpf_map_def { let def = bpf_map_def {
map_type: 1, map_type: 1,
key_size: 2, key_size: 2,
value_size: 3, value_size: 3,
max_entries: 4, max_entries: 4,
map_flags: 5, map_flags: 5,
..Default::default() id: 6,
})[..(mem::size_of::<u32>() * 5)], pinning: 7,
) };
.unwrap(); let mut buf = [0u8; 128];
assert!(map.id == 0 && map.pinning == 0) unsafe { ptr::write_unaligned(buf.as_mut_ptr() as *mut _, def.clone()) };
assert_eq!(parse_map_def("foo", &buf).unwrap(), def);
} }
#[test] #[test]

Loading…
Cancel
Save