Skip to content

Commit

Permalink
Merge pull request #640 from aya-rs/lossy-conversions
Browse files Browse the repository at this point in the history
integration-test: Remove integration-test-macros
  • Loading branch information
alessandrod authored Jul 8, 2023
2 parents e93e3c4 + e08c647 commit ed70a47
Show file tree
Hide file tree
Showing 23 changed files with 222 additions and 241 deletions.
30 changes: 26 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
[workspace]
members = [
"aya", "aya-obj", "aya-tool", "aya-log", "aya-log-common", "aya-log-parser", "test/integration-test", "test/integration-test-macros", "xtask",
"aya",
"aya-obj",
"aya-tool",
"aya-log",
"aya-log-common",
"aya-log-parser",
"test/integration-test",
"xtask",

# macros
"aya-bpf-macros", "aya-log-ebpf-macros",
"aya-bpf-macros",
"aya-log-ebpf-macros",

# ebpf crates
"bpf/aya-bpf", "bpf/aya-bpf-bindings", "bpf/aya-log-ebpf", "test/integration-ebpf"
"bpf/aya-bpf",
"bpf/aya-bpf-bindings",
"bpf/aya-log-ebpf",
"test/integration-ebpf",
]
resolver = "2"

default-members = [
"aya",
"aya-obj",
"aya-tool",
"aya-log",
"aya-bpf-macros",
"aya-log-ebpf-macros",
]
default-members = ["aya", "aya-obj", "aya-tool", "aya-log", "aya-bpf-macros", "aya-log-ebpf-macros"]

[profile.dev]
panic = "abort"
Expand Down
2 changes: 1 addition & 1 deletion aya-log-parser/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "aya-log-parser"
version = "0.1.11-dev.0"
edition = "2018"
edition = "2021"

[dependencies]
aya-log-common = { path = "../aya-log-common" }
Expand Down
1 change: 1 addition & 0 deletions bpf/aya-bpf-cty/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
authors = ["Jorge Aparicio <jorge@japaric.io>"]
edition = "2021"
categories = ["embedded", "external-ffi-bindings" ,"no-std"]
description = "Type aliases to C types like c_int for use with bindgen"
documentation = "https://docs.rs/cty"
Expand Down
6 changes: 3 additions & 3 deletions bpf/aya-bpf-cty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ mod ad {
target_arch = "riscv64"
))]
mod ad {
pub type c_char = ::c_uchar;
pub type c_char = super::c_uchar;

pub type c_int = i32;
pub type c_uint = u32;
Expand All @@ -63,15 +63,15 @@ mod ad {
target_arch = "xtensa"
))]
mod ad {
pub type c_char = ::c_schar;
pub type c_char = super::c_schar;

pub type c_int = i32;
pub type c_uint = u32;
}

#[cfg(target_arch = "msp430")]
mod ad {
pub type c_char = ::c_uchar;
pub type c_char = super::c_uchar;

pub type c_int = i16;
pub type c_uint = u16;
Expand Down
21 changes: 11 additions & 10 deletions test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,20 @@ cargo xtask integration-test --libbpf-dir /path/to/libbpf

### Virtualized


```
./test/run.sh /path/to/libbpf
```
### Writing a test

### Writing an integration test

Tests should follow these guidelines:

- Rust eBPF code should live in `integration-ebpf/${NAME}.rs` and included in `integration-ebpf/Cargo.toml`
- C eBPF code should live in `integration-test/src/bpf/${NAME}.bpf.c`. It's automatically compiled and made available as `${OUT_DIR}/${NAME}.bpf.o`.
- Any bytecode should be included in the integration test binary using `include_bytes_aligned!`
- Tests should be added to `integration-test/src/test`
- You may add a new module, or use an existing one
- Integration tests must use the `#[integration_test]` macro to be included in the build
- Test functions should return `anyhow::Result<()>` since this allows the use of `?` to return errors.
- You may either `panic!` when an assertion fails or `bail!`. The former is preferred since the stack trace will point directly to the failed line.
- Rust eBPF code should live in `integration-ebpf/${NAME}.rs` and included in
`integration-ebpf/Cargo.toml`.
- C eBPF code should live in `integration-ebpf/src/bpf/${NAME}.bpf.c`. It's automatically compiled
and made available as `${OUT_DIR}/${NAME}.bpf.o`.
- Any bytecode should be included in the integration test binary using `include_bytes_aligned!`.
- Tests should be added to `integration-test/tests`.
- You may add a new module, or use an existing one.
- Test functions should not return `anyhow::Result<()>` since this produces errors without stack
traces. Prefer to `panic!` instead.
13 changes: 0 additions & 13 deletions test/integration-test-macros/Cargo.toml

This file was deleted.

46 changes: 0 additions & 46 deletions test/integration-test-macros/src/lib.rs

This file was deleted.

6 changes: 0 additions & 6 deletions test/integration-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,10 @@ anyhow = "1"
aya = { path = "../../aya" }
aya-log = { path = "../../aya-log" }
aya-obj = { path = "../../aya-obj" }
clap = { version = "4", features = ["derive"] }
env_logger = "0.10"
futures-core = "0.3"
inventory = "0.3"
integration-test-macros = { path = "../integration-test-macros" }
libc = { version = "0.2.105" }
log = "0.4"
object = { version = "0.31", default-features = false, features = ["std", "read_core", "elf"] }
rbpf = "0.2.0"
regex = "1"
tempfile = "3.3.0"
libtest-mimic = "0.6.0"
tokio = { version = "1.24", features = ["rt", "rt-multi-thread", "sync", "time"] }
1 change: 1 addition & 0 deletions test/integration-test/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

21 changes: 0 additions & 21 deletions test/integration-test/src/main.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use aya::{
programs::{ProgramError, UProbe},
Bpf,
};
use integration_test_macros::integration_test;

const RESULT_BUF_LEN: usize = 1024;

Expand All @@ -20,53 +19,53 @@ struct TestResult {

unsafe impl aya::Pod for TestResult {}

#[integration_test]
#[test]
fn bpf_probe_read_user_str_bytes() {
let bpf = set_user_buffer(b"foo\0", RESULT_BUF_LEN);
assert_eq!(result_bytes(&bpf), b"foo");
}

#[integration_test]
#[test]
fn bpf_probe_read_user_str_bytes_truncate() {
let s = vec![b'a'; RESULT_BUF_LEN];
let bpf = set_user_buffer(&s, RESULT_BUF_LEN);
// The kernel truncates the string and the last byte is the null terminator
assert_eq!(result_bytes(&bpf), &s[..RESULT_BUF_LEN - 1]);
}

#[integration_test]
#[test]
fn bpf_probe_read_user_str_bytes_empty_string() {
let bpf = set_user_buffer(b"\0", RESULT_BUF_LEN);
assert_eq!(result_bytes(&bpf), b"");
}

#[integration_test]
#[test]
fn bpf_probe_read_user_str_bytes_empty_dest() {
let bpf = set_user_buffer(b"foo\0", 0);
assert_eq!(result_bytes(&bpf), b"");
}

#[integration_test]
#[test]
fn bpf_probe_read_kernel_str_bytes() {
let bpf = set_kernel_buffer(b"foo\0", RESULT_BUF_LEN);
assert_eq!(result_bytes(&bpf), b"foo");
}

#[integration_test]
#[test]
fn bpf_probe_read_kernel_str_bytes_truncate() {
let s = vec![b'a'; RESULT_BUF_LEN];
let bpf = set_kernel_buffer(&s, RESULT_BUF_LEN);
// The kernel truncates the string and the last byte is the null terminator
assert_eq!(result_bytes(&bpf), &s[..RESULT_BUF_LEN - 1]);
}

#[integration_test]
#[test]
fn bpf_probe_read_kernel_str_bytes_empty_string() {
let bpf = set_kernel_buffer(b"\0", RESULT_BUF_LEN);
assert_eq!(result_bytes(&bpf), b"");
}

#[integration_test]
#[test]
fn bpf_probe_read_kernel_str_bytes_empty_dest() {
let bpf = set_kernel_buffer(b"foo\0", 0);
assert_eq!(result_bytes(&bpf), b"");
Expand All @@ -76,7 +75,7 @@ fn set_user_buffer(bytes: &[u8], dest_len: usize) -> Bpf {
let bpf = load_and_attach_uprobe(
"test_bpf_probe_read_user_str_bytes",
"trigger_bpf_probe_read_user",
include_bytes_aligned!("../../../../target/bpfel-unknown-none/release/bpf_probe_read"),
include_bytes_aligned!("../../../target/bpfel-unknown-none/release/bpf_probe_read"),
);
trigger_bpf_probe_read_user(bytes.as_ptr(), dest_len);
bpf
Expand All @@ -86,7 +85,7 @@ fn set_kernel_buffer(bytes: &[u8], dest_len: usize) -> Bpf {
let mut bpf = load_and_attach_uprobe(
"test_bpf_probe_read_kernel_str_bytes",
"trigger_bpf_probe_read_kernel",
include_bytes_aligned!("../../../../target/bpfel-unknown-none/release/bpf_probe_read"),
include_bytes_aligned!("../../../target/bpfel-unknown-none/release/bpf_probe_read"),
);
set_kernel_buffer_element(&mut bpf, bytes);
trigger_bpf_probe_read_kernel(dest_len);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ use tempfile::TempDir;

use aya::{maps::Array, programs::TracePoint, BpfLoader, Btf, Endianness};

use super::integration_test;

// In the tests below we often use values like 0xAAAAAAAA or -0x7AAAAAAA. Those values have no
// special meaning, they just have "nice" bit patterns that can be helpful while debugging.

#[integration_test]
#[test]
fn relocate_field() {
let test = RelocationTest {
local_definition: r#"
Expand Down Expand Up @@ -40,7 +38,7 @@ fn relocate_field() {
assert_eq!(test.run_no_btf().unwrap(), 3);
}

#[integration_test]
#[test]
fn relocate_enum() {
let test = RelocationTest {
local_definition: r#"
Expand All @@ -60,7 +58,7 @@ fn relocate_enum() {
assert_eq!(test.run_no_btf().unwrap(), 0xAAAAAAAA);
}

#[integration_test]
#[test]
fn relocate_enum_signed() {
let test = RelocationTest {
local_definition: r#"
Expand All @@ -80,7 +78,7 @@ fn relocate_enum_signed() {
assert_eq!(test.run_no_btf().unwrap() as i64, -0x7AAAAAAAi64);
}

#[integration_test]
#[test]
fn relocate_enum64() {
let test = RelocationTest {
local_definition: r#"
Expand All @@ -100,7 +98,7 @@ fn relocate_enum64() {
assert_eq!(test.run_no_btf().unwrap(), 0xAAAAAAAABBBBBBBB);
}

#[integration_test]
#[test]
fn relocate_enum64_signed() {
let test = RelocationTest {
local_definition: r#"
Expand All @@ -120,7 +118,7 @@ fn relocate_enum64_signed() {
assert_eq!(test.run_no_btf().unwrap() as i64, -0xAAAAAAABBBBBBBBi64);
}

#[integration_test]
#[test]
fn relocate_pointer() {
let test = RelocationTest {
local_definition: r#"
Expand All @@ -143,7 +141,7 @@ fn relocate_pointer() {
assert_eq!(test.run_no_btf().unwrap(), 42);
}

#[integration_test]
#[test]
fn relocate_struct_flavors() {
let definition = r#"
struct foo {};
Expand Down
Loading

0 comments on commit ed70a47

Please sign in to comment.