diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 83c696b..24c5995 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -39,7 +39,8 @@ jobs: - name: Run afl-system-config run: | curl https://raw.githubusercontent.com/AFLplusplus/AFLplusplus/stable/afl-system-config > afl-system-config.sh - chmod +x afl-system-config.sh && bash afl-system-config.sh + chmod +x afl-system-config.sh + bash afl-system-config.sh - name: Run rustfmt checks run: cargo fmt -- --check @@ -54,4 +55,7 @@ jobs: run: cargo test --release --test cli_instrument_integration_test - name: Run integration test for fuzzing - run: cargo test --release --test cli_fuzz_integration_test \ No newline at end of file + run: cargo test --release --test cli_fuzz_integration_test + + - name: Run unit test + run: cargo test \ No newline at end of file diff --git a/src/cli/config.rs b/src/cli/config.rs index 7f60082..5ddb929 100644 --- a/src/cli/config.rs +++ b/src/cli/config.rs @@ -163,6 +163,6 @@ impl Configuration { // Currently, TOML & Serde don't handle parsing `u128` 🤡 // So we need to parse it as a `string`... to then revert it to `u128` // (which is `BalanceOf`) - value.and_then(|s| s.parse::().ok()).map(|v| v) + value.and_then(|s| s.parse::().ok()) } } diff --git a/src/cli/ziggy.rs b/src/cli/ziggy.rs index 426fcf1..9a1221f 100644 --- a/src/cli/ziggy.rs +++ b/src/cli/ziggy.rs @@ -24,8 +24,6 @@ use std::{ Stdio, }, sync::mpsc, - thread, - time::Duration, }; use crate::cli::config::{ @@ -93,8 +91,8 @@ impl App { } } } -pub const AFL_DEBUG: &'static str = "1"; -pub const AFL_FORKSRV_INIT_TMOUT: &'static str = "10000000"; +pub const AFL_DEBUG: &str = "1"; +pub const AFL_FORKSRV_INIT_TMOUT: &str = "10000000"; #[derive(Copy, Clone, Debug)] pub enum ZiggyCommand { @@ -259,9 +257,8 @@ impl ZiggyConfig { .path(CoverageTracePath); // We clean up the old one first - match fs::remove_file(covpath) { - Ok(_) => println!("💨 Removed previous coverage file"), - Err(_) => {} + if fs::remove_file(covpath).is_ok() { + println!("💨 Removed previous coverage file") } self.start( diff --git a/src/contract/payload.rs b/src/contract/payload.rs index 81912f0..69fa8e9 100644 --- a/src/contract/payload.rs +++ b/src/contract/payload.rs @@ -15,8 +15,7 @@ pub struct PayloadCrafter {} /// # Example /// ``` /// #[ink(message)] -/// pub fn phink_assert_abc_dot_com_cant_be_registered(&self) -> bool -/// ... +/// pub fn phink_assert_abc_dot_com_cant_be_registered(&self) -> bool {} /// ``` pub const DEFAULT_PHINK_PREFIX: &str = "phink_"; #[derive(Deserialize)] @@ -43,6 +42,7 @@ impl PayloadCrafter { if let Ok(spec) = serde_json::from_value::(v["spec"].clone()) { let selectors = Self::parse_selectors(&spec); all_selectors.extend(selectors); + return all_selectors; } } } @@ -154,7 +154,10 @@ mod test { use sp_core::hexdisplay::AsBytesRef; use std::{ fs, - path::Path, + path::{ + Path, + PathBuf, + }, }; #[test] @@ -174,13 +177,13 @@ mod test { fn build() { let bash = Command::new("bash ./sample/build.sh"); + todo!() // bash. } #[test] fn fetch_correct_selectors() { - let specs = fs::read_to_string("sample/dns/target/ink/dns.json").unwrap(); - let extracted: String = PayloadCrafter::extract_all(specs.into()) + let extracted: String = PayloadCrafter::extract_all(PathBuf::from("sample/dns")) .iter() .map(|x| hex::encode(x) + " ") .collect(); @@ -188,7 +191,7 @@ mod test { // DNS selectors assert_eq!( extracted, - "9bae9d5e 229b553f b8a4d3d9 84a15da1 d259f7ba 07fcd0b1 2e15cab0 5d17ca7f " + "9bae9d5e 229b553f b8a4d3d9 84a15da1 d259f7ba 07fcd0b1 2093daa4 " ); } @@ -211,7 +214,7 @@ mod test { "re", "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY", ]; - let data = transcoder.encode(&constructor, args).unwrap(); + let data = transcoder.encode(constructor, args).unwrap(); let hex = hex::encode(data); println!("Encoded constructor data {}", hex); assert!(!hex.is_empty()) diff --git a/src/cover/coverage.rs b/src/cover/coverage.rs index daacf34..f7290db 100644 --- a/src/cover/coverage.rs +++ b/src/cover/coverage.rs @@ -17,7 +17,7 @@ use std::{ pub type CoverageTrace = Vec; -#[derive(Clone)] +#[derive(Clone, Default)] pub struct InputCoverage { /// One input might contains multiple messages messages_coverage: Vec, @@ -44,13 +44,6 @@ impl Debug for InputCoverage { } impl InputCoverage { - pub fn new() -> Self { - InputCoverage { - messages_coverage: Vec::new(), - raw_from_debug: Vec::new(), - } - } - pub fn add_cov(&mut self, coverage: &CoverageTrace) { let parsed = Self::parse_coverage(coverage); self.raw_from_debug.push(coverage.clone()); diff --git a/src/fuzzer/fuzz.rs b/src/fuzzer/fuzz.rs index b5e1ef7..47f57cf 100644 --- a/src/fuzzer/fuzz.rs +++ b/src/fuzzer/fuzz.rs @@ -163,7 +163,7 @@ impl FuzzerEngine for Fuzzer { let mut chain = BasicExternalities::new(client.setup.genesis.clone()); chain.execute_with(|| ::timestamp(0)); - let mut coverage = InputCoverage::new(); + let mut coverage: InputCoverage = Default::default(); let all_msg_responses = execute_messages(&client.clone(), &decoded_msgs, &mut chain, &mut coverage); @@ -252,7 +252,7 @@ fn write_dict_header(dict_file: &mut fs::File) -> io::Result<()> { } fn write_corpus_file(index: usize, selector: &Selector, corpus_dir: PathBuf) -> io::Result<()> { - let file_path = PathBuf::from(corpus_dir).join(format!("selector_{}.bin", index)); + let file_path = corpus_dir.join(format!("selector_{}.bin", index)); fs::write(file_path, selector) } diff --git a/src/instrumenter/instrumentation.rs b/src/instrumenter/instrumentation.rs index ed022ed..4ae982c 100644 --- a/src/instrumenter/instrumentation.rs +++ b/src/instrumenter/instrumentation.rs @@ -17,10 +17,7 @@ use std::{ use crate::{ cli::ziggy::ZiggyConfig, - instrumenter::{ - instrumentation::instrument::ContractCovUpdater, - instrumented_path::InstrumentedPath, - }, + instrumenter::instrumentation::instrument::ContractCovUpdater, }; use quote::quote; use syn::{ @@ -141,12 +138,11 @@ impl ContractForker for Instrumenter { .config .instrumented_contract_path .clone() - .unwrap_or(InstrumentedPath::default()) + .unwrap_or_default() .path; println!("🏗️ Creating new directory: {:?}", new_dir); - fs::create_dir_all(&new_dir) - .map_err(|e| format!("🙅 Failed to create directory: {}", e))?; + fs::create_dir_all(new_dir).map_err(|e| format!("🙅 Failed to create directory: {}", e))?; println!( "📁 Starting to copy files from {:?}", @@ -291,7 +287,7 @@ mod instrument { for mut stmt in stmts.drain(..) { let line_lit = LitInt::new(self.line_id.to_string().as_str(), Span::call_site()); - self.line_id = self.line_id + 1; + self.line_id += 1; let insert_expr: Expr = parse_quote! { ink::env::debug_println!("COV={}", #line_lit) diff --git a/src/main.rs b/src/main.rs index e0b9801..975e5ab 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,3 @@ -use phink_lib; fn main() { phink_lib::main(); } diff --git a/tests/cli_fuzz_integration_test.rs b/tests/cli_fuzz_integration_test.rs index 23bda16..b035890 100644 --- a/tests/cli_fuzz_integration_test.rs +++ b/tests/cli_fuzz_integration_test.rs @@ -12,7 +12,6 @@ mod tests { get_corpus_files, instrument, samples::Sample, - try_cleanup_fuzzoutput, with_modified_phink_config, }; use anyhow::{ @@ -61,7 +60,7 @@ mod tests { ensure!( initial_corpus_len > 0, - "Corpus directory is empty after creation: {:?}", + "Corpus directory is empty after creation: {:?} files", initial_corpus_len ); @@ -132,7 +131,6 @@ mod tests { with_modified_phink_config(&config, || { instrument(Sample::Dummy); - let phink_output = fuzz_output.join("phink"); let fuzzing = ensure_while_fuzzing(&config, Duration::from_secs(120), || { let fuzz_created = fs::metadata(fuzz_output.clone()).is_ok(); diff --git a/tests/shared/mod.rs b/tests/shared/mod.rs index 52e6730..0f3cd79 100644 --- a/tests/shared/mod.rs +++ b/tests/shared/mod.rs @@ -12,6 +12,7 @@ use phink_lib::{ instrumenter::instrumented_path::InstrumentedPath, }; +use assert_cmd::assert::Assert; use std::{ collections::HashSet, ffi::OsStr, @@ -57,7 +58,7 @@ where F: FnOnce() -> Result<()>, { try_cleanup_instrumented(config); - try_cleanup_fuzzoutput(&config); + try_cleanup_fuzzoutput(config); config.save_as_toml(DEFAULT_TEST_PHINK_TOML); // Executing the actual test @@ -67,7 +68,7 @@ where let _ = fs::remove_file(DEFAULT_TEST_PHINK_TOML); // We clean the instrumented path try_cleanup_instrumented(config); - try_cleanup_fuzzoutput(&config); + try_cleanup_fuzzoutput(config); test_result } @@ -118,7 +119,7 @@ where // we `Ok(())`, we don't need to fuzz furthermore. loop { let test_result = executed_test(); - if let Ok(_) = test_result { + if test_result.is_ok() { child.kill().context("Failed to kill Ziggy")?; return Ok(()); } @@ -136,7 +137,7 @@ where } } -pub fn afl_log_didnt_fail(output: &PathBuf) -> bool { +pub fn afl_log_didnt_fail(output: &Path) -> bool { let log_path = output.join("logs").join("afl.log"); match fs::read_to_string(log_path) { @@ -170,21 +171,20 @@ pub fn try_cleanup_fuzzoutput(config: &Configuration) { /// Simple `phink` bin pop from cargo to instrument `contract_path` /// ** Important ** /// This should only be used in test ! -pub fn instrument(contract_path: Sample) { +pub fn instrument(contract_path: Sample) -> Assert { let mut cmd = Command::cargo_bin("phink").unwrap(); - let _binding = cmd - .args(["--config", DEFAULT_TEST_PHINK_TOML]) + cmd.args(["--config", DEFAULT_TEST_PHINK_TOML]) .arg("instrument") .arg(contract_path.path()) .assert() - .success(); + .success() } /// Simple `phink` bin pop from cargo to fuzz `path_instrumented_contract` /// ** Important ** /// This should only be used in test ! pub fn fuzz(path_instrumented_contract: InstrumentedPath) -> Child { - let mut child = NativeCommand::new("cargo") + let child = NativeCommand::new("cargo") .arg("run") .arg("--") .args(["--config", DEFAULT_TEST_PHINK_TOML]) @@ -216,10 +216,9 @@ pub fn find_string_in_rs_files(dir: &Path, target: &str) -> bool { if find_string_in_rs_files(&path, target) { return true; } - } else if path.extension() == Some(OsStr::new("rs")) { - if file_contains_string(&path, target) { - return true; - } + } else if path.extension() == Some(OsStr::new("rs")) && file_contains_string(&path, target) + { + return true; } }