Skip to content

Commit

Permalink
(c2rust-analyze/tests) Add tests for string literals and casts (#838)
Browse files Browse the repository at this point in the history
Add tests for string literals and casts.

They are currently unsupported:
- #833
- #837

so for now the tests are skipped over with `#[cfg(any())]`. Once those
issues are fixed, these tests will be turned back on, but it's easier to
start with the tests existing.

These tests just check if `c2rust-analyze` doesn't crash on them,
similar to the existing `lighttpd-minimal` test. For the `FileCheck`
tests, `FileCheck` requires at least one `CHECK:` command, which we
don't want. Thus, I've renamed `lighttpd.rs` to `analyze.rs` and it'll
be for `c2rust-analyze`-only tests, as opposed to `filecheck.rs` for
`c2rust-analyze` + `FileCheck` tests.

Furthermore, running `cargo` concurrently (due to multiple tests) for
`c2rust-analyze` crashes in macOS CI, so now the `c2rust-analyze` binary
is found to run directly, rather than going through `cargo` again.

Skipping going through `cargo` makes the tests run much faster, but more
importantly, prevents them from competing for the `target/` lock, which
caused issues in CI on macOS sometimes. It's also a bit simpler now, not
needing to go through `cargo`.
  • Loading branch information
kkysen authored Feb 16, 2023
2 parents 796473e + 275679b commit 327bd57
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 19 deletions.
18 changes: 18 additions & 0 deletions c2rust-analyze/tests/analyze.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use common::Analyze;

pub mod common;

#[test]
fn string_literals() {
Analyze::resolve().run("tests/analyze/string_literals.rs");
}

#[test]
fn string_casts() {
Analyze::resolve().run("tests/analyze/string_casts.rs");
}

#[test]
fn lighttpd_minimal() {
Analyze::resolve().run("../analysis/tests/lighttpd-minimal/src/main.rs");
}
9 changes: 9 additions & 0 deletions c2rust-analyze/tests/analyze/string_casts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[cfg(any())]
fn cast_only(s: *const u8) {
s as *const core::ffi::c_char;
}

#[cfg(any())]
fn cast_from_literal() {
b"" as *const u8 as *const core::ffi::c_char;
}
9 changes: 9 additions & 0 deletions c2rust-analyze/tests/analyze/string_literals.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[cfg(any())]
fn str() {
"";
}

#[cfg(any())]
fn bstr() {
b"";
}
21 changes: 11 additions & 10 deletions c2rust-analyze/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,23 @@ use std::{
use c2rust_build_paths::find_llvm_config;

#[derive(Default)]
pub struct Analyze;
pub struct Analyze {
path: PathBuf,
}

impl Analyze {
pub fn new() -> Self {
Self
pub fn resolve() -> Self {
let current_exe = env::current_exe().unwrap();
let bin_deps_dir = current_exe.parent().unwrap();
let bin_dir = bin_deps_dir.parent().unwrap();
let path = bin_dir.join(env!("CARGO_PKG_NAME"));
Self { path }
}

fn run_(&self, rs_path: &Path) -> PathBuf {
let dir = Path::new(env!("CARGO_MANIFEST_DIR"));
let lib_dir = Path::new(env!("C2RUST_TARGET_LIB_DIR"));

let manifest_path = dir.join("Cargo.toml");
let rs_path = dir.join(rs_path); // allow relative paths, or override with an absolute path
let output_path = {
let mut file_name = rs_path.file_name().unwrap().to_owned();
Expand All @@ -29,12 +34,8 @@ impl Analyze {
let output_stdout = File::create(&output_path).unwrap();
let output_stderr = File::try_clone(&output_stdout).unwrap();

let mut cmd = Command::new("cargo");
cmd.arg("run")
.arg("--manifest-path")
.arg(&manifest_path)
.arg("--")
.arg(&rs_path)
let mut cmd = Command::new(&self.path);
cmd.arg(&rs_path)
.arg("-L")
.arg(lib_dir)
.arg("--crate-type")
Expand Down
2 changes: 1 addition & 1 deletion c2rust-analyze/tests/filecheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use common::{Analyze, FileCheck};

#[test]
fn filecheck() {
let analyze = Analyze::new();
let analyze = Analyze::resolve();
let file_check = FileCheck::resolve();

for entry in fs::read_dir("tests/filecheck").unwrap() {
Expand Down
8 changes: 0 additions & 8 deletions c2rust-analyze/tests/lighttpd.rs

This file was deleted.

0 comments on commit 327bd57

Please sign in to comment.