Skip to content

Commit

Permalink
Tests check for compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-valerio committed Sep 3, 2024
1 parent e88b596 commit 4514603
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Docker build
name: Docker (building, compiling)

on:
push:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ jobs:
run: cargo afl config --build --plugins --verbose --force

- name: Compile samples in `sample`
working-directory: sample
working-directory: ./sample
run: bash build.sh

- name: Run unit & integration tests
- name: Run unit tests and integration tests
run: cargo test --verbose
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ RUN cp target/release/phink /usr/local/bin/phink

WORKDIR /phink
ENTRYPOINT ["phink"]
# Default command: instrument a contract
# If nothing is provided, we just start an instrumentumentation of a sampled contract
CMD ["instrument", "sample/dummy/"]
12 changes: 12 additions & 0 deletions tests/cli_fuzz_integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ mod tests {
ensure_while_fuzzing,
get_corpus_files,
instrument,
is_compiled,
is_instrumented,
samples::Sample,
with_modified_phink_config,
};
Expand Down Expand Up @@ -58,6 +60,16 @@ mod tests {
_ => 0,
};

let path_contract = &config
.clone()
.instrumented_contract_path
.unwrap_or_default()
.path;

ensure!(is_instrumented(path_contract), "Dummy wasn't instrumented ");

ensure!(is_compiled(path_contract), "Dummy wasn't compiled properly");

ensure!(
initial_corpus_len > 0,
"Corpus directory is empty after creation: {:?} files",
Expand Down
28 changes: 6 additions & 22 deletions tests/cli_instrument_integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ pub mod shared;
mod tests {
use super::*;
use crate::shared::{
find_string_in_rs_files,
instrument,
is_instrumented,
samples::Sample,
with_modified_phink_config,
DEFAULT_TEST_PHINK_TOML,
Expand Down Expand Up @@ -48,7 +48,7 @@ mod tests {
// `path_instrumented_contract`
let cargo_toml_exists = WalkDir::new(path_instrumented_contract.path)
.into_iter()
.filter_map(std::result::Result::ok) // Filter out errors
.filter_map(Result::ok) // Filter out errors
.any(|entry| {
entry.file_name() == "Cargo.toml"
});
Expand Down Expand Up @@ -85,30 +85,18 @@ mod tests {
"Instrumented contract not found"
);

let accumulator_contains_debug = find_string_in_rs_files(
&path_instrumented_contract.path.join("accumulator"),
"ink::env::debug_println!(\"COV={}\",",
);
ensure!(
accumulator_contains_debug,
is_instrumented(&path_instrumented_contract.path.join("accumulator")),
"Expected to find a trace of instrumentation in Accumulator"
);

let subber_contains_debug = find_string_in_rs_files(
&path_instrumented_contract.path.join("subber"),
"ink::env::debug_println!(\"COV={}\",",
);
ensure!(
subber_contains_debug,
is_instrumented(&path_instrumented_contract.path.join("subber")),
"Expected to find a trace of instrumentation in Subber"
);

let adder_contains_debug = find_string_in_rs_files(
&path_instrumented_contract.path.join("adder"),
"ink::env::debug_println!(\"COV={}\",",
);
ensure!(
adder_contains_debug,
is_instrumented(&path_instrumented_contract.path.join("adder")),
"Expected to find a trace of instrumentation in Adder"
);

Expand All @@ -134,12 +122,8 @@ mod tests {
let test = with_modified_phink_config(&config, || {
let _ = instrument(Sample::Dummy);

let contains_instrumented_code = find_string_in_rs_files(
&path_instrumented_contract.path,
"ink::env::debug_println!(\"COV={}\",",
);
ensure!(
contains_instrumented_code,
is_instrumented(&path_instrumented_contract.path),
"Expected to find a trace of instrumentation in at least one .rs file"
);
Ok(())
Expand Down
26 changes: 21 additions & 5 deletions tests/shared/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ pub mod samples;

use crate::shared::samples::Sample;
use anyhow::{
anyhow,
bail,
ensure,
Context,
Expand Down Expand Up @@ -210,9 +209,9 @@ pub fn fuzz(path_instrumented_contract: InstrumentedPath) -> Child {
child
}

/// Return `true` if `target` is found in any `*.rs` file of `dir`, otherwise `false`
/// Returns `true` if `matching_string` is found in any `*.rs` file of `dir`, otherwise `false`
#[must_use]
pub fn find_string_in_rs_files(dir: &Path, target: &str) -> bool {
fn find_string_in_rs_files(dir: &Path, matching_string: &str) -> bool {
fn file_contains_string(file_path: &Path, target: &str) -> bool {
let mut file = fs::File::open(file_path).expect("Unable to open file");
let mut content = String::new();
Expand All @@ -226,10 +225,11 @@ pub fn find_string_in_rs_files(dir: &Path, target: &str) -> bool {
let path = entry.path();

if path.is_dir() {
if find_string_in_rs_files(&path, target) {
if find_string_in_rs_files(&path, matching_string) {
return true;
}
} else if path.extension() == Some(OsStr::new("rs")) && file_contains_string(&path, target)
} else if path.extension() == Some(OsStr::new("rs"))
&& file_contains_string(&path, matching_string)
{
return true;
}
Expand All @@ -238,6 +238,22 @@ pub fn find_string_in_rs_files(dir: &Path, target: &str) -> bool {
false
}

pub fn is_instrumented(path_instrumented_contract: &PathBuf) -> bool {
find_string_in_rs_files(
path_instrumented_contract,
"ink::env::debug_println!(\"COV={}\",",
)
}

pub fn is_compiled(path_instrumented_contract: &PathBuf) -> bool {
let target_dir = path_instrumented_contract.join("target");

if !target_dir.exists() {
return false
}
target_dir.join("debug").exists() || target_dir.join("release").exists()
}

/// A function to get all entries from the corpus directory
pub fn get_corpus_files(corpus_path: &PathBuf) -> Result<HashSet<PathBuf>> {
println!(
Expand Down

0 comments on commit 4514603

Please sign in to comment.