Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Feat/custom test names new architecture #274

Merged
merged 2 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ incremented upon a breaking change and the patch version will be incremented for

**Added**

-Added additional attributes to TridentAccounts, mut and signer ([268](https://github.com/Ackee-Blockchain/trident/pull/268))
- Added additional attributes to TridentAccounts, mut and signer ([268](https://github.com/Ackee-Blockchain/trident/pull/268))

- Users can now specify a program for which they want to add or initialize a fuzz test using `--program-name` flag ([273](https://github.com/Ackee-Blockchain/trident/pull/273))

- Allow custom test name specification in fuzz test creation with `--test-name` flag ([274](https://github.com/Ackee-Blockchain/trident/pull/274))

**Removed**

**Changed**
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ tokio = { version = "1" }
anyhow = { version = "1" }
fehler = { version = "1" }
termimad = "0.30.0"
heck = "0.4.0"
29 changes: 26 additions & 3 deletions crates/cli/src/command/fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ use anyhow::{bail, Error};

use clap::Subcommand;
use fehler::throws;
use heck::ToSnakeCase;
use trident_client::___private::{Commander, TestGenerator};

use crate::{_discover, show_howto};

pub const TRIDENT_TOML: &str = "Trident.toml";
pub const TRIDENT_TESTS: &str = "trident-tests";
pub const SKIP: &str = "\x1b[33mSkip\x1b[0m";

#[derive(Subcommand)]
#[allow(non_camel_case_types)]
Expand All @@ -23,6 +26,14 @@ pub enum FuzzCommand {
value_name = "FILE"
)]
program_name: Option<String>,
#[arg(
short,
long,
required = false,
help = "Name of the fuzz test to add.",
value_name = "NAME"
)]
test_name: Option<String>,
},
#[command(
about = "Run the AFL on desired fuzz test.",
Expand Down Expand Up @@ -142,10 +153,22 @@ pub async fn fuzz(subcmd: FuzzCommand) {
} => {
commander.run_hfuzz_debug(target, crash_file_path).await?;
}

FuzzCommand::Add { program_name } => {
FuzzCommand::Add {
program_name,
test_name,
} => {
let test_name_snake = test_name.map(|name| name.to_snake_case());
if let Some(name) = &test_name_snake {
let fuzz_test_dir = Path::new(&root).join(TRIDENT_TESTS).join(name);
if fuzz_test_dir.exists() {
println!("{SKIP} [{}/{}] already exists", TRIDENT_TESTS, name);
return;
}
}
let mut generator = TestGenerator::new_with_root(&root)?;
generator.add_fuzz_test(program_name).await?;
generator
.add_fuzz_test(program_name, test_name_snake)
.await?;
show_howto();
}
};
Expand Down
8 changes: 5 additions & 3 deletions crates/cli/src/command/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::path::Path;

use anyhow::{bail, Error};
use fehler::throws;
use heck::ToSnakeCase;
use trident_client::___private::TestGenerator;

use crate::{_discover, show_howto};
Expand All @@ -11,7 +12,7 @@ pub const TRIDENT_TOML: &str = "Trident.toml";
pub const SKIP: &str = "\x1b[33mSkip\x1b[0m";

#[throws]
pub async fn init(force: bool, program_name: Option<String>) {
pub async fn init(force: bool, program_name: Option<String>, test_name: Option<String>) {
// look for Anchor.toml
let root = if let Some(r) = _discover(ANCHOR_TOML)? {
r
Expand All @@ -21,8 +22,9 @@ pub async fn init(force: bool, program_name: Option<String>) {

let mut generator: TestGenerator = TestGenerator::new_with_root(&root)?;

let test_name_snake = test_name.map(|name| name.to_snake_case());
if force {
generator.initialize(program_name).await?;
generator.initialize(program_name, test_name_snake).await?;
show_howto();
} else {
let root_path = Path::new(&root).join(TRIDENT_TOML);
Expand All @@ -34,7 +36,7 @@ pub async fn init(force: bool, program_name: Option<String>) {
root
);
} else {
generator.initialize(program_name).await?;
generator.initialize(program_name, test_name_snake).await?;
show_howto();
}
}
Expand Down
11 changes: 10 additions & 1 deletion crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ enum Command {
value_name = "FILE"
)]
program_name: Option<String>,
#[arg(
short,
long,
required = false,
help = "Name of the fuzz test to initialize.",
value_name = "NAME"
)]
test_name: Option<String>,
},
#[command(
about = "Run fuzz subcommands.",
Expand Down Expand Up @@ -78,7 +86,8 @@ pub async fn start() {
Command::Init {
force,
program_name,
} => command::init(force, program_name).await?,
test_name,
} => command::init(force, program_name, test_name).await?,
Command::Clean => command::clean().await?,
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/client/src/test_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,27 @@ impl TestGenerator {
}
}
#[throws]
pub async fn initialize(&mut self, program_name: Option<String>) {
pub async fn initialize(&mut self, program_name: Option<String>, test_name: Option<String>) {
Commander::build_anchor_project(program_name.clone()).await?;

self.get_program_packages(program_name.clone()).await?;
self.load_programs_idl(program_name.clone())?;
self.create_template().await?;
self.add_new_fuzz_test().await?;
self.add_new_fuzz_test(test_name).await?;
self.create_trident_toml().await?;

self.update_gitignore(CARGO_TARGET_DIR_DEFAULT_HFUZZ)?;
self.update_gitignore(CARGO_TARGET_DIR_DEFAULT_AFL)?;
}

#[throws]
pub async fn add_fuzz_test(&mut self, program_name: Option<String>) {
pub async fn add_fuzz_test(&mut self, program_name: Option<String>, test_name: Option<String>) {
Commander::build_anchor_project(program_name.clone()).await?;

self.get_program_packages(program_name.clone()).await?;
self.load_programs_idl(program_name.clone())?;
self.create_template().await?;
self.add_new_fuzz_test().await?;
self.add_new_fuzz_test(test_name).await?;

self.update_gitignore(CARGO_TARGET_DIR_DEFAULT_HFUZZ)?;
self.update_gitignore(CARGO_TARGET_DIR_DEFAULT_AFL)?;
Expand Down
12 changes: 10 additions & 2 deletions crates/client/src/test_generator_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,21 @@ impl TestGenerator {
}

#[throws]
pub(crate) async fn add_new_fuzz_test(&self) {
pub(crate) async fn add_new_fuzz_test(&self, test_name: Option<String>) {
let trident_tests = construct_path!(self.root, TESTS_WORKSPACE_DIRECTORY);

let new_fuzz_test = format!("fuzz_{}", get_fuzz_id(&trident_tests)?);
let new_fuzz_test = match test_name {
Some(name) => name,
None => format!("fuzz_{}", get_fuzz_id(&trident_tests)?),
};

let new_fuzz_test_dir = construct_path!(trident_tests, &new_fuzz_test);

if new_fuzz_test_dir.exists() {
println!("{SKIP} [{}] already exists", new_fuzz_test_dir.display());
return;
}

self.create_instructions(&new_fuzz_test_dir).await?;
self.create_transactions(&new_fuzz_test_dir).await?;
self.create_test_fuzz(&new_fuzz_test_dir).await?;
Expand Down
Loading