From 21e01df242376d4a9d4f67664277263f0dc8d173 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alessandro=C2=A0Decina?= Date: Sat, 3 Jul 2021 11:32:07 +0000 Subject: [PATCH] aya: obj: improve parse_map_def tests Add a test that checks that we handle ELF section padding correctly and simplify the other tests. --- aya/src/obj/mod.rs | 103 +++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 56 deletions(-) diff --git a/aya/src/obj/mod.rs b/aya/src/obj/mod.rs index 7a37f36e..3e169d2c 100644 --- a/aya/src/obj/mod.rs +++ b/aya/src/obj/mod.rs @@ -668,70 +668,61 @@ mod tests { } #[test] - fn test_parse_map_def() { + fn test_parse_map_def_error() { assert!(matches!( parse_map_def("foo", &[]), Err(ParseError::InvalidMapDefinition { .. }) )); + } - assert_eq!( - parse_map_def( - "foo", - 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, - key_size: 2, - value_size: 3, - max_entries: 4, - map_flags: 5, - ..Default::default() - } - ); + #[test] + fn test_parse_map_short() { + let def = bpf_map_def { + map_type: 1, + key_size: 2, + value_size: 3, + max_entries: 4, + map_flags: 5, + id: 0, + pinning: 0, + }; assert_eq!( - parse_map_def( - "foo", - &bytes_of(&bpf_map_def { - map_type: 1, - key_size: 2, - value_size: 3, - max_entries: 4, - map_flags: 5, - ..Default::default() - })[..(mem::size_of::() * 5)] - ) - .unwrap(), - bpf_map_def { - map_type: 1, - key_size: 2, - value_size: 3, - max_entries: 4, - map_flags: 5, - ..Default::default() - } + parse_map_def("foo", &bytes_of(&def)[..MINIMUM_MAP_SIZE]).unwrap(), + def ); - let map = parse_map_def( - "foo", - &bytes_of(&bpf_map_def { - map_type: 1, - key_size: 2, - value_size: 3, - max_entries: 4, - map_flags: 5, - ..Default::default() - })[..(mem::size_of::() * 5)], - ) - .unwrap(); - assert!(map.id == 0 && map.pinning == 0) + } + + #[test] + fn test_parse_map_def() { + let def = bpf_map_def { + map_type: 1, + key_size: 2, + value_size: 3, + max_entries: 4, + map_flags: 5, + id: 6, + pinning: 7, + }; + + assert_eq!(parse_map_def("foo", bytes_of(&def)).unwrap(), def); + } + + #[test] + fn test_parse_map_def_with_padding() { + let def = bpf_map_def { + map_type: 1, + key_size: 2, + value_size: 3, + max_entries: 4, + map_flags: 5, + id: 6, + pinning: 7, + }; + let mut buf = [0u8; 128]; + unsafe { ptr::write_unaligned(buf.as_mut_ptr() as *mut _, def.clone()) }; + + assert_eq!(parse_map_def("foo", &buf).unwrap(), def); } #[test]