Skip to content

Commit

Permalink
clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-valerio committed Aug 30, 2024
1 parent 2c3ba42 commit f24ffb4
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 52 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
run: cargo test --release --test cli_fuzz_integration_test

- name: Run unit test
run: cargo test
2 changes: 1 addition & 1 deletion src/cli/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>`)
value.and_then(|s| s.parse::<u128>().ok()).map(|v| v)
value.and_then(|s| s.parse::<u128>().ok())
}
}
11 changes: 4 additions & 7 deletions src/cli/ziggy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ use std::{
Stdio,
},
sync::mpsc,
thread,
time::Duration,
};

use crate::cli::config::{
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(
Expand Down
17 changes: 10 additions & 7 deletions src/contract/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -43,6 +42,7 @@ impl PayloadCrafter {
if let Ok(spec) = serde_json::from_value::<Spec>(v["spec"].clone()) {
let selectors = Self::parse_selectors(&spec);
all_selectors.extend(selectors);
return all_selectors;
}
}
}
Expand Down Expand Up @@ -154,7 +154,10 @@ mod test {
use sp_core::hexdisplay::AsBytesRef;
use std::{
fs,
path::Path,
path::{
Path,
PathBuf,
},
};

#[test]
Expand All @@ -174,21 +177,21 @@ 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();

// DNS selectors
assert_eq!(
extracted,
"9bae9d5e 229b553f b8a4d3d9 84a15da1 d259f7ba 07fcd0b1 2e15cab0 5d17ca7f "
"9bae9d5e 229b553f b8a4d3d9 84a15da1 d259f7ba 07fcd0b1 2093daa4 "
);
}

Expand All @@ -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())
Expand Down
9 changes: 1 addition & 8 deletions src/cover/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::{

pub type CoverageTrace = Vec<u8>;

#[derive(Clone)]
#[derive(Clone, Default)]
pub struct InputCoverage {
/// One input might contains multiple messages
messages_coverage: Vec<MessageCoverage>,
Expand All @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions src/fuzzer/fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl FuzzerEngine for Fuzzer {
let mut chain = BasicExternalities::new(client.setup.genesis.clone());
chain.execute_with(|| <Fuzzer as FuzzerEngine>::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);
Expand Down Expand Up @@ -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)
}

Expand Down
12 changes: 4 additions & 8 deletions src/instrumenter/instrumentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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 {:?}",
Expand Down Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use phink_lib;
fn main() {
phink_lib::main();
}
4 changes: 1 addition & 3 deletions tests/cli_fuzz_integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ mod tests {
get_corpus_files,
instrument,
samples::Sample,
try_cleanup_fuzzoutput,
with_modified_phink_config,
};
use anyhow::{
Expand Down Expand Up @@ -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
);

Expand Down Expand Up @@ -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();
Expand Down
25 changes: 12 additions & 13 deletions tests/shared/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use phink_lib::{
instrumenter::instrumented_path::InstrumentedPath,
};

use assert_cmd::assert::Assert;
use std::{
collections::HashSet,
ffi::OsStr,
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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(());
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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;
}
}

Expand Down

0 comments on commit f24ffb4

Please sign in to comment.