From 7e1666fb83e5c2b270cb24becb84adebbe29be1a Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Tue, 30 Apr 2024 18:28:17 -0400 Subject: [PATCH] Deduplicate test helpers --- aya/src/maps/bloom_filter.rs | 50 ++++------------------ aya/src/maps/hash_map/hash_map.rs | 14 +++--- aya/src/maps/hash_map/mod.rs | 38 ----------------- aya/src/maps/hash_map/per_cpu_hash_map.rs | 8 ++-- aya/src/maps/lpm_trie.rs | 52 +++++------------------ aya/src/maps/mod.rs | 50 ++++++++++++++++------ 6 files changed, 67 insertions(+), 145 deletions(-) diff --git a/aya/src/maps/bloom_filter.rs b/aya/src/maps/bloom_filter.rs index 817c785c..14aef4d3 100644 --- a/aya/src/maps/bloom_filter.rs +++ b/aya/src/maps/bloom_filter.rs @@ -86,41 +86,20 @@ mod tests { use super::*; use crate::{ - bpf_map_def, generated::{ bpf_cmd, bpf_map_type::{BPF_MAP_TYPE_BLOOM_FILTER, BPF_MAP_TYPE_PERF_EVENT_ARRAY}, }, - maps::Map, - obj::{self, maps::LegacyMap, EbpfSectionKind}, + maps::{ + test_utils::{self, new_map}, + Map, + }, + obj, sys::{override_syscall, SysResult, Syscall}, }; fn new_obj_map() -> obj::Map { - obj::Map::Legacy(LegacyMap { - def: bpf_map_def { - map_type: BPF_MAP_TYPE_BLOOM_FILTER as u32, - key_size: 4, - value_size: 4, - max_entries: 1024, - ..Default::default() - }, - section_index: 0, - section_kind: EbpfSectionKind::Maps, - symbol_index: None, - data: Vec::new(), - }) - } - - fn new_map(obj: obj::Map) -> MapData { - override_syscall(|call| match call { - Syscall::Ebpf { - cmd: bpf_cmd::BPF_MAP_CREATE, - .. - } => Ok(1337), - call => panic!("unexpected syscall {:?}", call), - }); - MapData::create(obj, "foo", None).unwrap() + test_utils::new_obj_map::(BPF_MAP_TYPE_BLOOM_FILTER) } fn sys_error(value: i32) -> SysResult { @@ -141,20 +120,9 @@ mod tests { #[test] fn test_try_from_wrong_map() { - let map = new_map(obj::Map::Legacy(LegacyMap { - def: bpf_map_def { - map_type: BPF_MAP_TYPE_PERF_EVENT_ARRAY as u32, - key_size: 4, - value_size: 4, - max_entries: 1024, - ..Default::default() - }, - section_index: 0, - section_kind: EbpfSectionKind::Maps, - symbol_index: None, - data: Vec::new(), - })); - + let map = new_map(test_utils::new_obj_map::( + BPF_MAP_TYPE_PERF_EVENT_ARRAY, + )); let map = Map::PerfEventArray(map); assert_matches!( diff --git a/aya/src/maps/hash_map/hash_map.rs b/aya/src/maps/hash_map/hash_map.rs index f1f8fceb..a201ac37 100644 --- a/aya/src/maps/hash_map/hash_map.rs +++ b/aya/src/maps/hash_map/hash_map.rs @@ -108,22 +108,22 @@ mod tests { use assert_matches::assert_matches; use libc::{EFAULT, ENOENT}; - use super::{ - super::test_utils::{self, new_map}, - *, - }; + use super::*; use crate::{ generated::{ bpf_attr, bpf_cmd, bpf_map_type::{BPF_MAP_TYPE_HASH, BPF_MAP_TYPE_LRU_HASH}, }, - maps::Map, + maps::{ + test_utils::{self, new_map}, + Map, + }, obj, sys::{override_syscall, SysResult, Syscall}, }; fn new_obj_map() -> obj::Map { - test_utils::new_obj_map(BPF_MAP_TYPE_HASH) + test_utils::new_obj_map::(BPF_MAP_TYPE_HASH) } fn sys_error(value: i32) -> SysResult { @@ -192,7 +192,7 @@ mod tests { #[test] fn test_try_from_ok_lru() { - let map_data = || new_map(test_utils::new_obj_map(BPF_MAP_TYPE_LRU_HASH)); + let map_data = || new_map(test_utils::new_obj_map::(BPF_MAP_TYPE_LRU_HASH)); let map = Map::HashMap(map_data()); assert!(HashMap::<_, u32, u32>::try_from(&map).is_ok()); let map = Map::LruHashMap(map_data()); diff --git a/aya/src/maps/hash_map/mod.rs b/aya/src/maps/hash_map/mod.rs index 93f345bd..33f5292b 100644 --- a/aya/src/maps/hash_map/mod.rs +++ b/aya/src/maps/hash_map/mod.rs @@ -43,41 +43,3 @@ pub(crate) fn remove(map: &MapData, key: &K) -> Result<(), MapError> { .into() }) } - -#[cfg(test)] -mod test_utils { - use crate::{ - bpf_map_def, - generated::{bpf_cmd, bpf_map_type}, - maps::MapData, - obj::{self, maps::LegacyMap, EbpfSectionKind}, - sys::{override_syscall, Syscall}, - }; - - pub(super) fn new_map(obj: obj::Map) -> MapData { - override_syscall(|call| match call { - Syscall::Ebpf { - cmd: bpf_cmd::BPF_MAP_CREATE, - .. - } => Ok(1337), - call => panic!("unexpected syscall {:?}", call), - }); - MapData::create(obj, "foo", None).unwrap() - } - - pub(super) fn new_obj_map(map_type: bpf_map_type) -> obj::Map { - obj::Map::Legacy(LegacyMap { - def: bpf_map_def { - map_type: map_type as u32, - key_size: 4, - value_size: 4, - max_entries: 1024, - ..Default::default() - }, - section_index: 0, - section_kind: EbpfSectionKind::Maps, - data: Vec::new(), - symbol_index: None, - }) - } -} diff --git a/aya/src/maps/hash_map/per_cpu_hash_map.rs b/aya/src/maps/hash_map/per_cpu_hash_map.rs index 6dd3ee19..2dd8bcd7 100644 --- a/aya/src/maps/hash_map/per_cpu_hash_map.rs +++ b/aya/src/maps/hash_map/per_cpu_hash_map.rs @@ -150,15 +150,15 @@ impl, K: Pod, V: Pod> IterableMap> #[cfg(test)] mod tests { - use super::{super::test_utils, *}; + use super::*; use crate::{ generated::bpf_map_type::{BPF_MAP_TYPE_LRU_PERCPU_HASH, BPF_MAP_TYPE_PERCPU_HASH}, - maps::Map, + maps::{test_utils, Map}, }; #[test] fn test_try_from_ok() { - let map = Map::PerCpuHashMap(test_utils::new_map(test_utils::new_obj_map( + let map = Map::PerCpuHashMap(test_utils::new_map(test_utils::new_obj_map::( BPF_MAP_TYPE_PERCPU_HASH, ))); assert!(PerCpuHashMap::<_, u32, u32>::try_from(&map).is_ok()) @@ -166,7 +166,7 @@ mod tests { #[test] fn test_try_from_ok_lru() { let map_data = - || test_utils::new_map(test_utils::new_obj_map(BPF_MAP_TYPE_LRU_PERCPU_HASH)); + || test_utils::new_map(test_utils::new_obj_map::(BPF_MAP_TYPE_LRU_PERCPU_HASH)); let map = Map::PerCpuHashMap(map_data()); assert!(PerCpuHashMap::<_, u32, u32>::try_from(&map).is_ok()); let map = Map::PerCpuLruHashMap(map_data()); diff --git a/aya/src/maps/lpm_trie.rs b/aya/src/maps/lpm_trie.rs index c3dd528a..8e25ddd5 100644 --- a/aya/src/maps/lpm_trie.rs +++ b/aya/src/maps/lpm_trie.rs @@ -196,48 +196,27 @@ impl, K: Pod, V: Pod> IterableMap, V> for LpmTrie obj::Map { - obj::Map::Legacy(LegacyMap { - def: bpf_map_def { - map_type: BPF_MAP_TYPE_LPM_TRIE as u32, - key_size: mem::size_of::>() as u32, - value_size: 4, - max_entries: 1024, - ..Default::default() - }, - section_index: 0, - section_kind: EbpfSectionKind::Maps, - symbol_index: None, - data: Vec::new(), - }) - } - - fn new_map(obj: obj::Map) -> MapData { - override_syscall(|call| match call { - Syscall::Ebpf { - cmd: bpf_cmd::BPF_MAP_CREATE, - .. - } => Ok(1337), - call => panic!("unexpected syscall {:?}", call), - }); - MapData::create(obj, "foo", None).unwrap() + test_utils::new_obj_map::>(BPF_MAP_TYPE_LPM_TRIE) } fn sys_error(value: i32) -> SysResult { @@ -270,20 +249,9 @@ mod tests { #[test] fn test_try_from_wrong_map() { - let map = new_map(obj::Map::Legacy(LegacyMap { - def: bpf_map_def { - map_type: BPF_MAP_TYPE_PERF_EVENT_ARRAY as u32, - key_size: 4, - value_size: 4, - max_entries: 1024, - ..Default::default() - }, - section_index: 0, - section_kind: EbpfSectionKind::Maps, - symbol_index: None, - data: Vec::new(), - })); - + let map = new_map(test_utils::new_obj_map::( + BPF_MAP_TYPE_PERF_EVENT_ARRAY, + )); let map = Map::PerfEventArray(map); assert_matches!( diff --git a/aya/src/maps/mod.rs b/aya/src/maps/mod.rs index 7c1d2a84..e781b0c9 100644 --- a/aya/src/maps/mod.rs +++ b/aya/src/maps/mod.rs @@ -1021,35 +1021,59 @@ pub fn loaded_maps() -> impl Iterator> { } #[cfg(test)] -mod tests { - use std::os::fd::AsRawFd as _; - - use assert_matches::assert_matches; - use libc::{c_char, EFAULT}; - - use super::*; +mod test_utils { use crate::{ bpf_map_def, - generated::{bpf_cmd, bpf_map_type::BPF_MAP_TYPE_HASH}, - obj::maps::LegacyMap, + generated::{bpf_cmd, bpf_map_type}, + maps::MapData, + obj::{self, maps::LegacyMap, EbpfSectionKind}, sys::{override_syscall, Syscall}, }; - fn new_obj_map() -> obj::Map { + pub(super) fn new_map(obj: obj::Map) -> MapData { + override_syscall(|call| match call { + Syscall::Ebpf { + cmd: bpf_cmd::BPF_MAP_CREATE, + .. + } => Ok(1337), + call => panic!("unexpected syscall {:?}", call), + }); + MapData::create(obj, "foo", None).unwrap() + } + + pub(super) fn new_obj_map(map_type: bpf_map_type) -> obj::Map { obj::Map::Legacy(LegacyMap { def: bpf_map_def { - map_type: BPF_MAP_TYPE_HASH as u32, - key_size: 4, + map_type: map_type as u32, + key_size: std::mem::size_of::() as u32, value_size: 4, max_entries: 1024, ..Default::default() }, section_index: 0, section_kind: EbpfSectionKind::Maps, - symbol_index: Some(0), data: Vec::new(), + symbol_index: None, }) } +} + +#[cfg(test)] +mod tests { + use std::os::fd::AsRawFd as _; + + use assert_matches::assert_matches; + use libc::{c_char, EFAULT}; + + fn new_obj_map() -> obj::Map { + test_utils::new_obj_map::(crate::generated::bpf_map_type::BPF_MAP_TYPE_HASH) + } + + use super::*; + use crate::{ + generated::bpf_cmd, + sys::{override_syscall, Syscall}, + }; #[test] fn test_from_map_id() {