From 13afb1c642595987e668773055d09f58778ec73a Mon Sep 17 00:00:00 2001 From: Xiaobo Liu Date: Sat, 16 Aug 2025 16:04:50 +0800 Subject: [PATCH] add test Signed-off-by: Xiaobo Liu --- test/integration-common/src/lib.rs | 9 ++++ test/integration-ebpf/Cargo.toml | 4 ++ test/integration-ebpf/src/queue_test.rs | 47 +++++++++++++++++ test/integration-test/src/lib.rs | 1 + test/integration-test/src/tests.rs | 1 + test/integration-test/src/tests/queue.rs | 66 ++++++++++++++++++++++++ 6 files changed, 128 insertions(+) create mode 100644 test/integration-ebpf/src/queue_test.rs create mode 100644 test/integration-test/src/tests/queue.rs diff --git a/test/integration-common/src/lib.rs b/test/integration-common/src/lib.rs index f75a3939..13de7938 100644 --- a/test/integration-common/src/lib.rs +++ b/test/integration-common/src/lib.rs @@ -64,3 +64,12 @@ pub mod strncmp { #[cfg(feature = "user")] unsafe impl aya::Pod for TestResult {} } + +pub mod queue { + #[derive(Copy, Clone)] + #[repr(C)] + pub struct TestResult(pub u64); + + #[cfg(feature = "user")] + unsafe impl aya::Pod for TestResult {} +} diff --git a/test/integration-ebpf/Cargo.toml b/test/integration-ebpf/Cargo.toml index b0573882..562d968f 100644 --- a/test/integration-ebpf/Cargo.toml +++ b/test/integration-ebpf/Cargo.toml @@ -91,3 +91,7 @@ path = "src/xdp_sec.rs" [[bin]] name = "uprobe_cookie" path = "src/uprobe_cookie.rs" + +[[bin]] +name = "queue_test" +path = "src/queue_test.rs" diff --git a/test/integration-ebpf/src/queue_test.rs b/test/integration-ebpf/src/queue_test.rs new file mode 100644 index 00000000..b34c5382 --- /dev/null +++ b/test/integration-ebpf/src/queue_test.rs @@ -0,0 +1,47 @@ +#![no_std] +#![no_main] + +use aya_ebpf::maps::{Array, Queue}; +use aya_ebpf::{ + cty::c_long, + macros::{map, uprobe}, + programs::ProbeContext, +}; +#[cfg(not(test))] +extern crate ebpf_panic; + +const PEEK_INDEX: u32 = 0; +const POP_INDEX: u32 = 1; + +#[map] +static RESULT: Array = Array::::with_max_entries(2, 0); + +#[map] +static TEST_QUEUE: Queue = Queue::with_max_entries(10, 0); + +#[uprobe] +pub fn test_queue_push(ctx: ProbeContext) -> Result<(), c_long> { + let value: i64 = ctx.arg(0).ok_or(-1)?; + TEST_QUEUE.push(&value, 0)?; + Ok(()) +} + +#[uprobe] +pub fn test_queue_peek(_: ProbeContext) -> Result<(), c_long> { + let value = TEST_QUEUE.peek().unwrap_or(-1); + let result = RESULT.get_ptr_mut(PEEK_INDEX).ok_or(-1)?; + unsafe { + *result = value; + } + Ok(()) +} + +#[uprobe] +pub fn test_queue_pop(_: ProbeContext) -> Result<(), c_long> { + let value = TEST_QUEUE.pop().unwrap_or(-1); + let result = RESULT.get_ptr_mut(POP_INDEX).ok_or(-1)?; + unsafe { + *result = value; + } + Ok(()) +} diff --git a/test/integration-test/src/lib.rs b/test/integration-test/src/lib.rs index f7cc30c6..ed0d47ff 100644 --- a/test/integration-test/src/lib.rs +++ b/test/integration-test/src/lib.rs @@ -49,6 +49,7 @@ bpf_file!( RELOCATIONS => "relocations", RING_BUF => "ring_buf", SIMPLE_PROG => "simple_prog", + QUEUE_TEST => "queue_test", STRNCMP => "strncmp", TCX => "tcx", TEST => "test", diff --git a/test/integration-test/src/tests.rs b/test/integration-test/src/tests.rs index 43894366..d09776d7 100644 --- a/test/integration-test/src/tests.rs +++ b/test/integration-test/src/tests.rs @@ -6,6 +6,7 @@ mod info; mod iter; mod load; mod log; +mod queue; mod raw_tracepoint; mod rbpf; mod relocations; diff --git a/test/integration-test/src/tests/queue.rs b/test/integration-test/src/tests/queue.rs new file mode 100644 index 00000000..8a5b3805 --- /dev/null +++ b/test/integration-test/src/tests/queue.rs @@ -0,0 +1,66 @@ +use aya::{EbpfLoader, maps::Array, programs::UProbe}; + +const PEEK_INDEX: u32 = 0; +const POP_INDEX: u32 = 1; + +#[unsafe(no_mangle)] +#[inline(never)] +pub extern "C" fn trigger_push(arg: i64) { + core::hint::black_box(arg); +} + +#[unsafe(no_mangle)] +#[inline(never)] +pub extern "C" fn trigger_peek() { + core::hint::black_box(trigger_peek); +} + +#[unsafe(no_mangle)] +#[inline(never)] +pub extern "C" fn trigger_pop() { + core::hint::black_box(trigger_pop); +} + +#[test_log::test] +fn queue_basic() { + let mut bpf = EbpfLoader::new().load(crate::QUEUE_TEST).unwrap(); + + let prog: &mut UProbe = bpf + .program_mut("test_queue_push") + .unwrap() + .try_into() + .unwrap(); + prog.load().unwrap(); + prog.attach("trigger_push", "/proc/self/exe", None, None) + .unwrap(); + + let prog: &mut UProbe = bpf + .program_mut("test_queue_peek") + .unwrap() + .try_into() + .unwrap(); + prog.load().unwrap(); + prog.attach("trigger_peek", "/proc/self/exe", None, None) + .unwrap(); + + let prog: &mut UProbe = bpf + .program_mut("test_queue_pop") + .unwrap() + .try_into() + .unwrap(); + prog.load().unwrap(); + prog.attach("trigger_pop", "/proc/self/exe", None, None) + .unwrap(); + + //let queue = Queue::<_, u64>::try_from(bpf.take_map("TEST_QUEUE").unwrap()).unwrap(); + let array = Array::<_, i64>::try_from(bpf.map("RESULT").unwrap()).unwrap(); + + for i in 0..5 { + trigger_push(i); + trigger_peek(); + trigger_pop(); + + assert_eq!(array.get(&PEEK_INDEX, 0).unwrap(), i); + assert_eq!(array.get(&POP_INDEX, 0).unwrap(), i); + } +}