29 releases

Uses old Rust 2015

0.2.22 Oct 10, 2019
0.2.18 Aug 25, 2019
0.2.17 Jul 3, 2019
0.2.13 Mar 4, 2019
0.1.1 Nov 16, 2016

#205 in Build Utils

Download history 4/week @ 2024-08-05 6/week @ 2024-08-19 47/week @ 2024-09-02 38/week @ 2024-09-09 19/week @ 2024-09-16 16/week @ 2024-09-23 2/week @ 2024-09-30 5/week @ 2024-10-07 10/week @ 2024-10-14 67/week @ 2024-11-04 42/week @ 2024-11-18

109 downloads per month

MIT/Apache

91KB
2K SLoC

ctest

Build Status Documentation

Automated testing of FFI bindings in Rust. This repository is intended to validate the *-sys crates that can be found on crates.io to ensure that the APIs in Rust match the APIs defined in C.

Example

Unfortunately the usage today is a little wonky, but to use this library, first, create a new Cargo project in your repo:

$ cargo new --bin systest

Then, edit systest/Cargo.toml to add these dependencies:

[package]
# ...
build = "build.rs"

[dependencies]
mylib-sys = { path = "../mylib-sys" }
libc = "0.2"

[build-dependencies]
ctest = "0.2"

Next, add a build script to systest/build.rs:

extern crate ctest;

fn main() {
    let mut cfg = ctest::TestGenerator::new();

    // Include the header files where the C APIs are defined
    cfg.header("foo.h")
       .header("bar.h");

    // Include the directory where the header files are defined
    cfg.include("path/to/include");

    // Generate the tests, passing the path to the `*-sys` library as well as
    // the module to generate.
    cfg.generate("../mylib-sys/lib.rs", "all.rs");
}

Next, add this to src/main.rs

#![allow(bad_style)]

extern crate mylib_sys;
extern crate libc;

use libc::*;
use mylib_sys::*;

include!(concat!(env!("OUT_DIR"), "/all.rs"));

And you're good to go! To run the tests execute cargo run in the systest directory, and everything should be kicked into action!

How it works

This library will parse the *-sys crate to learn about all extern fn definitions within. It will then generate a test suite to ensure that all function function signatures, constant values, struct layout/alignment, type size/alignment, etc, all match their C equivalent.

The generated tests come in two forms. One is a Rust file which contains the main function (hence the include! above), and another is a C file which is compiled as part of the build script. The C file is what includes all headers and returns information about the C side of things (which is validated in Rust).

A large amount of configuration can be applied to how the C file is generated, you can browse the documentation.

Projects using ctest

License

This project is licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in ctest by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~5MB
~104K SLoC