Automatic testing of FFI bindings for Rust
Rust C
Clone or download
alexcrichton Merge pull request #35 from gnzlbg/doc0
improve docs of cfg method
Latest commit e8cd0c2 Aug 12, 2018
Failed to load latest commit information.
.gitignore Initial commit Sep 16, 2015
.travis.yml Update travis token Nov 19, 2016
Cargo.toml Bump to 0.2.0 Aug 12, 2018
LICENSE-APACHE Add README/license Sep 16, 2015
appveyor.yml Test both GNU targets on windows Oct 7, 2015


Build Status Build status


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


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:

# ...
build = ""

my-sys-library = { path = "../my-sys-library" }
libc = "0.2"

ctest = "0.1"

Next, add a build script to systest/

extern crate ctest;

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

    // Include the header files where the C APIs are defined

    // Include the directory where the header files are defined

    // Generate the tests, passing the path to the `*-sys` library as well as
    // the module to generate.
    cfg.generate("../my-sys-library/", "");

Next, add this to src/


extern crate my_sys_library;
extern crate libc;

use libc::*;
use my_sys_library::*;

include!(concat!(env!("OUT_DIR"), "/"));

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


This project is licensed under either of

at your option.


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