Add test case

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

@ -1,6 +1,6 @@
use std::{
convert::{AsMut, AsRef},
marker::PhantomData,
marker::PhantomData, borrow::Borrow,
};
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`]
/// 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();
check_bounds(data, index)?;
let fd = data.fd_or_err()?;

@ -1,5 +1,5 @@
//! A Bloom Filter.
use std::{convert::AsRef, marker::PhantomData};
use std::{convert::AsRef, marker::PhantomData, borrow::Borrow};
use crate::{
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.
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()?;
bpf_map_push_elem(fd, &value, flags).map_err(|(_, io_error)| MapError::SyscallError {
call: "bpf_map_push_elem".to_owned(),

@ -285,7 +285,7 @@ mod tests {
let mut hm = HashMap::<_, u32, u32>::new(&mut map).unwrap();
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)
));
}
@ -311,6 +311,27 @@ mod tests {
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]
fn test_remove_syscall_error() {
override_syscall(|_| sys_error(EFAULT));

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

@ -1,7 +1,7 @@
//! Per-CPU hash map.
use std::{
convert::{AsMut, AsRef},
marker::PhantomData,
marker::PhantomData, borrow::Borrow,
};
use crate::{
@ -115,7 +115,7 @@ impl<T: AsMut<MapData>, K: Pod, V: Pod> PerCpuHashMap<T, K, V> {
/// )?;
/// # 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()?;
bpf_map_update_elem_per_cpu(fd, &key, &values, flags).map_err(|(_, io_error)| {
MapError::SyscallError {

@ -2,7 +2,7 @@
use std::{
convert::{AsMut, AsRef},
marker::PhantomData,
mem,
mem, borrow::Borrow,
};
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> {
/// 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()?;
bpf_map_update_elem(fd, Some(key), &value, flags).map_err(|(_, io_error)| {
MapError::SyscallError {

@ -1,7 +1,7 @@
//! A FIFO queue.
use std::{
convert::{AsMut, AsRef},
marker::PhantomData,
marker::PhantomData, borrow::Borrow,
};
use crate::{
@ -78,7 +78,7 @@ impl<T: AsMut<MapData>, V: Pod> Queue<T, V> {
/// # Errors
///
/// [`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()?;
bpf_map_push_elem(fd, &value, flags).map_err(|(_, io_error)| MapError::SyscallError {
call: "bpf_map_push_elem".to_owned(),

@ -1,7 +1,7 @@
use std::{
convert::{AsMut, AsRef},
marker::PhantomData,
os::unix::io::{AsRawFd, RawFd},
os::unix::io::{AsRawFd, RawFd}, borrow::Borrow,
};
use crate::{
@ -115,7 +115,7 @@ impl<T: AsRef<MapData>, K: Pod> SockHash<T, K> {
impl<T: AsMut<MapData>, K: Pod> SockHash<T, K> {
/// 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)
}

@ -1,7 +1,7 @@
//! A LIFO stack.
use std::{
convert::{AsMut, AsRef},
marker::PhantomData,
marker::PhantomData, borrow::Borrow,
};
use crate::{
@ -78,7 +78,7 @@ impl<T: AsMut<MapData>, V: Pod> Stack<T, V> {
/// # Errors
///
/// [`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()?;
bpf_map_update_elem(fd, None::<&u32>, &value, flags).map_err(|(_, io_error)| {
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;
@ -89,7 +89,7 @@ impl<K> SockHash<K> {
pub fn redirect_sk_lookup(
&mut self,
ctx: &SkLookupContext,
key: K,
key: impl Borrow<K>,
flags: u64,
) -> Result<(), u32> {
unsafe {

Loading…
Cancel
Save