Add test case

pull/431/head
Ricky Han 2 years ago
parent 1247ffc19b
commit e9ec257328

@ -1,6 +1,6 @@
use std::{ use std::{
convert::{AsMut, AsRef}, convert::{AsMut, AsRef},
marker::PhantomData, marker::PhantomData, borrow::Borrow,
}; };
use crate::{ use crate::{
@ -88,7 +88,7 @@ impl<T: AsMut<MapData>, V: Pod> Array<T, V> {
/// ///
/// Returns [`MapError::OutOfBounds`] if `index` is out of bounds, [`MapError::SyscallError`] /// Returns [`MapError::OutOfBounds`] if `index` is out of bounds, [`MapError::SyscallError`]
/// if `bpf_map_update_elem` fails. /// if `bpf_map_update_elem` fails.
pub fn set(&mut self, index: u32, value: V, flags: u64) -> Result<(), MapError> { pub fn set(&mut self, index: u32, value: impl Borrow<V>, flags: u64) -> Result<(), MapError> {
let data = self.inner.as_mut(); let data = self.inner.as_mut();
check_bounds(data, index)?; check_bounds(data, index)?;
let fd = data.fd_or_err()?; let fd = data.fd_or_err()?;

@ -1,5 +1,5 @@
//! A Bloom Filter. //! A Bloom Filter.
use std::{convert::AsRef, marker::PhantomData}; use std::{convert::AsRef, marker::PhantomData, borrow::Borrow};
use crate::{ use crate::{
maps::{check_v_size, MapData, MapError}, maps::{check_v_size, MapData, MapError},
@ -62,7 +62,7 @@ impl<T: AsRef<MapData>, V: Pod> BloomFilter<T, V> {
} }
/// Inserts a value into the map. /// Inserts a value into the map.
pub fn insert(&self, value: V, flags: u64) -> Result<(), MapError> { pub fn insert(&self, value: impl Borrow<V>, flags: u64) -> Result<(), MapError> {
let fd = self.inner.as_ref().fd_or_err()?; let fd = self.inner.as_ref().fd_or_err()?;
bpf_map_push_elem(fd, &value, flags).map_err(|(_, io_error)| MapError::SyscallError { bpf_map_push_elem(fd, &value, flags).map_err(|(_, io_error)| MapError::SyscallError {
call: "bpf_map_push_elem".to_owned(), call: "bpf_map_push_elem".to_owned(),

@ -285,7 +285,7 @@ mod tests {
let mut hm = HashMap::<_, u32, u32>::new(&mut map).unwrap(); let mut hm = HashMap::<_, u32, u32>::new(&mut map).unwrap();
assert!(matches!( assert!(matches!(
hm.insert(1u32, 42u32, 0), hm.insert(1, 42, 0),
Err(MapError::SyscallError { call, io_error }) if call == "bpf_map_update_elem" && io_error.raw_os_error() == Some(EFAULT) Err(MapError::SyscallError { call, io_error }) if call == "bpf_map_update_elem" && io_error.raw_os_error() == Some(EFAULT)
)); ));
} }
@ -311,6 +311,27 @@ mod tests {
assert!(hm.insert(1, 42, 0).is_ok()); assert!(hm.insert(1, 42, 0).is_ok());
} }
#[test]
fn test_insert_boxed_ok() {
override_syscall(|call| match call {
Syscall::Bpf {
cmd: bpf_cmd::BPF_MAP_UPDATE_ELEM,
..
} => Ok(1),
_ => sys_error(EFAULT),
});
let mut map = MapData {
obj: new_obj_map(),
fd: Some(42),
pinned: false,
btf_fd: None,
};
let mut hm = HashMap::<_, u32, u32>::new(&mut map).unwrap();
assert!(hm.insert(Box::new(1), Box::new(42), 0).is_ok());
}
#[test] #[test]
fn test_remove_syscall_error() { fn test_remove_syscall_error() {
override_syscall(|_| sys_error(EFAULT)); override_syscall(|_| sys_error(EFAULT));

@ -1,5 +1,4 @@
//! Hash map types. //! Hash map types.
use crate::{ use crate::{
maps::MapError, maps::MapError,
sys::{bpf_map_delete_elem, bpf_map_update_elem}, sys::{bpf_map_delete_elem, bpf_map_update_elem},

@ -1,7 +1,7 @@
//! Per-CPU hash map. //! Per-CPU hash map.
use std::{ use std::{
convert::{AsMut, AsRef}, convert::{AsMut, AsRef},
marker::PhantomData, marker::PhantomData, borrow::Borrow,
}; };
use crate::{ use crate::{
@ -115,7 +115,7 @@ impl<T: AsMut<MapData>, K: Pod, V: Pod> PerCpuHashMap<T, K, V> {
/// )?; /// )?;
/// # Ok::<(), Error>(()) /// # Ok::<(), Error>(())
/// ``` /// ```
pub fn insert(&mut self, key: K, values: PerCpuValues<V>, flags: u64) -> Result<(), MapError> { pub fn insert(&mut self, key: impl Borrow<K>, values: PerCpuValues<V>, flags: u64) -> Result<(), MapError> {
let fd = self.inner.as_mut().fd_or_err()?; let fd = self.inner.as_mut().fd_or_err()?;
bpf_map_update_elem_per_cpu(fd, &key, &values, flags).map_err(|(_, io_error)| { bpf_map_update_elem_per_cpu(fd, &key, &values, flags).map_err(|(_, io_error)| {
MapError::SyscallError { MapError::SyscallError {

@ -2,7 +2,7 @@
use std::{ use std::{
convert::{AsMut, AsRef}, convert::{AsMut, AsRef},
marker::PhantomData, marker::PhantomData,
mem, mem, borrow::Borrow,
}; };
use crate::{ use crate::{
@ -128,7 +128,7 @@ impl<T: AsRef<MapData>, K: Pod, V: Pod> LpmTrie<T, K, V> {
impl<T: AsMut<MapData>, K: Pod, V: Pod> LpmTrie<T, K, V> { impl<T: AsMut<MapData>, K: Pod, V: Pod> LpmTrie<T, K, V> {
/// Inserts a key value pair into the map. /// Inserts a key value pair into the map.
pub fn insert(&mut self, key: &Key<K>, value: V, flags: u64) -> Result<(), MapError> { pub fn insert(&mut self, key: &Key<K>, value: impl Borrow<V>, flags: u64) -> Result<(), MapError> {
let fd = self.inner.as_mut().fd_or_err()?; let fd = self.inner.as_mut().fd_or_err()?;
bpf_map_update_elem(fd, Some(key), &value, flags).map_err(|(_, io_error)| { bpf_map_update_elem(fd, Some(key), &value, flags).map_err(|(_, io_error)| {
MapError::SyscallError { MapError::SyscallError {

@ -1,7 +1,7 @@
//! A FIFO queue. //! A FIFO queue.
use std::{ use std::{
convert::{AsMut, AsRef}, convert::{AsMut, AsRef},
marker::PhantomData, marker::PhantomData, borrow::Borrow,
}; };
use crate::{ use crate::{
@ -78,7 +78,7 @@ impl<T: AsMut<MapData>, V: Pod> Queue<T, V> {
/// # Errors /// # Errors
/// ///
/// [`MapError::SyscallError`] if `bpf_map_update_elem` fails. /// [`MapError::SyscallError`] if `bpf_map_update_elem` fails.
pub fn push(&mut self, value: V, flags: u64) -> Result<(), MapError> { pub fn push(&mut self, value: impl Borrow<V>, flags: u64) -> Result<(), MapError> {
let fd = self.inner.as_mut().fd_or_err()?; let fd = self.inner.as_mut().fd_or_err()?;
bpf_map_push_elem(fd, &value, flags).map_err(|(_, io_error)| MapError::SyscallError { bpf_map_push_elem(fd, &value, flags).map_err(|(_, io_error)| MapError::SyscallError {
call: "bpf_map_push_elem".to_owned(), call: "bpf_map_push_elem".to_owned(),

@ -1,7 +1,7 @@
use std::{ use std::{
convert::{AsMut, AsRef}, convert::{AsMut, AsRef},
marker::PhantomData, marker::PhantomData,
os::unix::io::{AsRawFd, RawFd}, os::unix::io::{AsRawFd, RawFd}, borrow::Borrow,
}; };
use crate::{ use crate::{
@ -115,7 +115,7 @@ impl<T: AsRef<MapData>, K: Pod> SockHash<T, K> {
impl<T: AsMut<MapData>, K: Pod> SockHash<T, K> { impl<T: AsMut<MapData>, K: Pod> SockHash<T, K> {
/// Inserts a socket under the given key. /// Inserts a socket under the given key.
pub fn insert<I: AsRawFd>(&mut self, key: K, value: I, flags: u64) -> Result<(), MapError> { pub fn insert<I: AsRawFd>(&mut self, key: impl Borrow<K>, value: I, flags: u64) -> Result<(), MapError> {
hash_map::insert(self.inner.as_mut(), &key, &value.as_raw_fd(), flags) hash_map::insert(self.inner.as_mut(), &key, &value.as_raw_fd(), flags)
} }

@ -1,7 +1,7 @@
//! A LIFO stack. //! A LIFO stack.
use std::{ use std::{
convert::{AsMut, AsRef}, convert::{AsMut, AsRef},
marker::PhantomData, marker::PhantomData, borrow::Borrow,
}; };
use crate::{ use crate::{
@ -78,7 +78,7 @@ impl<T: AsMut<MapData>, V: Pod> Stack<T, V> {
/// # Errors /// # Errors
/// ///
/// [`MapError::SyscallError`] if `bpf_map_update_elem` fails. /// [`MapError::SyscallError`] if `bpf_map_update_elem` fails.
pub fn push(&mut self, value: V, flags: u64) -> Result<(), MapError> { pub fn push(&mut self, value: impl Borrow<V>, flags: u64) -> Result<(), MapError> {
let fd = self.inner.as_mut().fd_or_err()?; let fd = self.inner.as_mut().fd_or_err()?;
bpf_map_update_elem(fd, None::<&u32>, &value, flags).map_err(|(_, io_error)| { bpf_map_update_elem(fd, None::<&u32>, &value, flags).map_err(|(_, io_error)| {
MapError::SyscallError { MapError::SyscallError {

@ -1,4 +1,4 @@
use core::{cell::UnsafeCell, marker::PhantomData, mem}; use core::{cell::UnsafeCell, marker::PhantomData, mem, borrow::Borrow};
use aya_bpf_cty::c_void; use aya_bpf_cty::c_void;
@ -89,7 +89,7 @@ impl<K> SockHash<K> {
pub fn redirect_sk_lookup( pub fn redirect_sk_lookup(
&mut self, &mut self,
ctx: &SkLookupContext, ctx: &SkLookupContext,
key: K, key: impl Borrow<K>,
flags: u64, flags: u64,
) -> Result<(), u32> { ) -> Result<(), u32> {
unsafe { unsafe {

Loading…
Cancel
Save