Skip to content

Commit

Permalink
Make all C API tests unit tests
Browse files Browse the repository at this point in the history
This change integrates all end-to-end C API tests into the respective
modules in the form of unit tests. Doing so arguably makes more sense
for the capi crate, because
- there aren't really any visibility concerns for the C API and
- that means we no longer have to build as a `lib` crate type

Signed-off-by: Daniel Müller <[email protected]>
  • Loading branch information
d-e-s-o authored and danielocfb committed Nov 21, 2023
1 parent 5c0b6e3 commit 40f417b
Show file tree
Hide file tree
Showing 5 changed files with 387 additions and 421 deletions.
3 changes: 1 addition & 2 deletions capi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ rust-version = "1.64"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
# `lib` is necessary for end-to-end tests.
crate-type = ["lib", "cdylib", "staticlib"]
crate-type = ["cdylib", "staticlib"]

[features]
# Enable this feature to re-generate the library's C header file. An
Expand Down
43 changes: 43 additions & 0 deletions capi/src/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,12 @@ pub unsafe extern "C" fn blaze_inspector_free(inspector: *mut blaze_inspector) {
mod tests {
use super::*;

use std::ffi::CStr;
use std::ffi::CString;
use std::path::Path;
use std::ptr;
use std::slice;

use test_log::test;


Expand Down Expand Up @@ -463,4 +469,41 @@ mod tests {
let syms = (0..200).map(|_| vec![sym.clone()]).collect();
test(syms);
}

/// Make sure that we can create and free an inspector instance.
#[test]
fn inspector_creation() {
let inspector = blaze_inspector_new();
let () = unsafe { blaze_inspector_free(inspector) };
}

/// Make sure that we can lookup a function's address using DWARF information.
#[test]
fn lookup_dwarf() {
let test_dwarf = Path::new(&env!("CARGO_MANIFEST_DIR"))
.join("..")
.join("data")
.join("test-stable-addresses-dwarf-only.bin");

let src = blaze_inspect_elf_src::from(Elf::new(test_dwarf));
let factorial = CString::new("factorial").unwrap();
let names = [factorial.as_ptr()];

let inspector = blaze_inspector_new();
let result =
unsafe { blaze_inspect_syms_elf(inspector, &src, names.as_ptr(), names.len()) };
let _src = Elf::from(src);
assert!(!result.is_null());

let sym_infos = unsafe { slice::from_raw_parts(result, names.len()) };
let sym_info = unsafe { &*sym_infos[0] };
assert_eq!(
unsafe { CStr::from_ptr(sym_info.name) },
CStr::from_bytes_with_nul(b"factorial\0").unwrap()
);
assert_eq!(sym_info.addr, 0x2000100);

let () = unsafe { blaze_inspect_syms_free(result) };
let () = unsafe { blaze_inspector_free(inspector) };
}
}
62 changes: 62 additions & 0 deletions capi/src/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,4 +575,66 @@ mod tests {
let meta_new = UserMeta::from(blaze_user_meta::from(meta.clone()));
assert_eq!(meta_new, meta);
}

/// Make sure that we can create and free a normalizer instance.
#[test]
fn normalizer_creation() {
let normalizer = blaze_normalizer_new();
let () = unsafe { blaze_normalizer_free(normalizer) };
}

/// Check that we can normalize user space addresses.
#[test]
fn normalize_user_addrs() {
let addrs = [
libc::__errno_location as Addr,
libc::dlopen as Addr,
libc::fopen as Addr,
elf_conversion as Addr,
normalize_user_addrs as Addr,
];

let normalizer = blaze_normalizer_new();
assert_ne!(normalizer, ptr::null_mut());

let result = unsafe {
blaze_normalize_user_addrs(normalizer, addrs.as_slice().as_ptr(), addrs.len(), 0)
};
assert_ne!(result, ptr::null_mut());

let user_addrs = unsafe { &*result };
assert_eq!(user_addrs.meta_cnt, 2);
assert_eq!(user_addrs.output_cnt, 5);

let () = unsafe { blaze_user_output_free(result) };
let () = unsafe { blaze_normalizer_free(normalizer) };
}

/// Check that we can normalize sorted user space addresses.
#[test]
fn normalize_user_addrs_sorted() {
let mut addrs = [
libc::__errno_location as Addr,
libc::dlopen as Addr,
libc::fopen as Addr,
elf_conversion as Addr,
normalize_user_addrs as Addr,
];
let () = addrs.sort();

let normalizer = blaze_normalizer_new();
assert_ne!(normalizer, ptr::null_mut());

let result = unsafe {
blaze_normalize_user_addrs_sorted(normalizer, addrs.as_slice().as_ptr(), addrs.len(), 0)
};
assert_ne!(result, ptr::null_mut());

let user_addrs = unsafe { &*result };
assert_eq!(user_addrs.meta_cnt, 2);
assert_eq!(user_addrs.output_cnt, 5);

let () = unsafe { blaze_user_output_free(result) };
let () = unsafe { blaze_normalizer_free(normalizer) };
}
}
Loading

0 comments on commit 40f417b

Please sign in to comment.