Skip to content

Commit

Permalink
✨ Add possibility to add account to the genesis from base64 data
Browse files Browse the repository at this point in the history
  • Loading branch information
lukacan committed Sep 13, 2024
1 parent 52a49c8 commit 792fcbc
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ incremented upon a breaking change and the patch version will be incremented for
## [dev] - Unreleased

**Changed**
- feat/ option to add account into Fuzz Test environment with base64 data ([197](https://github.com/Ackee-Blockchain/trident/pull/197))
- impr/ instead of parsing source code and creating our IDL, read anchor IDL ([196](https://github.com/Ackee-Blockchain/trident/pull/196))

## [0.7.0] - 2024-08-14
Expand Down
1 change: 1 addition & 0 deletions crates/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub mod fuzzing {
pub use trident_fuzz::*;

pub use solana_program_test::processor;
pub use trident_fuzz::program_test_client_blocking::FuzzingAccountBase64;
pub use trident_fuzz::program_test_client_blocking::FuzzingProgram;
pub use trident_fuzz::program_test_client_blocking::ProgramEntry;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub fn generate_source_code(idl_instructions: &[Idl]) -> String {

#(#fuzzing_programs)*

let mut client = ProgramTestClientBlocking::new(&#programs_array).unwrap();
let mut client = ProgramTestClientBlocking::new(&#programs_array,&[]).unwrap();

#run_with_runtime

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn fuzz_iteration<T: FuzzTestExecutor<U> + std::fmt::Display, U>(fuzz_data: Fuzz
processor!(convert_entry!(entry_dummy_example)),
);

let mut client = ProgramTestClientBlocking::new(&[fuzzing_program_dummy_example]).unwrap();
let mut client = ProgramTestClientBlocking::new(&[fuzzing_program_dummy_example], &[]).unwrap();

let _ = fuzz_data.run_with_runtime(PROGRAM_ID_DUMMY_EXAMPLE, &mut client);
}
Expand Down
32 changes: 31 additions & 1 deletion crates/fuzz/src/program_test_client_blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,34 @@ impl FuzzingProgram {
}
}

pub struct FuzzingAccountBase64<'a> {
pub address: Pubkey,
pub lamports: u64,
pub owner: Pubkey,
pub data_base64: &'a str,
}

impl<'a> FuzzingAccountBase64<'a> {
pub fn new(
address: Pubkey,
lamports: u64,
owner: Pubkey,
data_base64: &'a str,
) -> FuzzingAccountBase64<'a> {
Self {
address,
lamports,
owner,
data_base64,
}
}
}

impl ProgramTestClientBlocking {
pub fn new(program_: &[FuzzingProgram]) -> Result<Self, FuzzClientError> {
pub fn new(
program_: &[FuzzingProgram],
account_: &[FuzzingAccountBase64],
) -> Result<Self, FuzzClientError> {
let mut program_test = ProgramTest::default();
for x in program_ {
match x.entry {
Expand All @@ -74,6 +100,10 @@ impl ProgramTestClientBlocking {
}
}
}
for x in account_ {
program_test.add_account_with_base64_data(x.address, x.lamports, x.owner, x.data_base64)
}

let rt: tokio::runtime::Runtime = Builder::new_current_thread().enable_all().build()?;

let ctx = rt.block_on(program_test.start_with_context());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn fuzz_iteration<T: FuzzTestExecutor<U> + std::fmt::Display, U>(fuzz_data: Fuzz
);

let mut client =
ProgramTestClientBlocking::new(&[fuzzing_program_arbitrary_custom_types_4]).unwrap();
ProgramTestClientBlocking::new(&[fuzzing_program_arbitrary_custom_types_4], &[]).unwrap();

let _ = fuzz_data.run_with_runtime(PROGRAM_ID_ARBITRARY_CUSTOM_TYPES_4, &mut client);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn fuzz_iteration<T: FuzzTestExecutor<U> + std::fmt::Display, U>(fuzz_data: Fuzz
);

let mut client =
ProgramTestClientBlocking::new(&[fuzzing_program_arbitrary_limit_inputs_5]).unwrap();
ProgramTestClientBlocking::new(&[fuzzing_program_arbitrary_limit_inputs_5], &[]).unwrap();

let _ = fuzz_data.run_with_runtime(PROGRAM_ID_ARBITRARY_LIMIT_INPUTS_5, &mut client);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn fuzz_iteration<T: FuzzTestExecutor<U> + std::fmt::Display, U>(fuzz_data: Fuzz
let metaplex = FuzzingProgram::new("metaplex-token-metadata", &mpl_token_metadata::ID, None);

let mut client =
ProgramTestClientBlocking::new(&[fuzzing_program_cpi_metaplex_7, metaplex]).unwrap();
ProgramTestClientBlocking::new(&[fuzzing_program_cpi_metaplex_7, metaplex], &[]).unwrap();

let _ = fuzz_data.run_with_runtime(PROGRAM_ID_CPI_METAPLEX_7, &mut client);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,19 @@ struct MyFuzzData;
impl FuzzDataBuilder<FuzzInstruction> for MyFuzzData {}

fn fuzz_iteration<T: FuzzTestExecutor<U> + std::fmt::Display, U>(fuzz_data: FuzzData<T, U>) {

let fuzzing_program_hello_world = FuzzingProgram::new(
PROGRAM_NAME_HELLO_WORLD,
&PROGRAM_ID_HELLO_WORLD,
processor!(convert_entry!(entry_hello_world)),
);

let mut client = ProgramTestClientBlocking::new(&[fuzzing_program_hello_world]).unwrap();
let mut client = ProgramTestClientBlocking::new(&[fuzzing_program_hello_world], &[]).unwrap();

let _ = fuzz_data.run_with_runtime(PROGRAM_ID_HELLO_WORLD, &mut client);
}

fn main() {

loop {

fuzz_trident ! (fuzz_ix : FuzzInstruction , | fuzz_data : MyFuzzData | { fuzz_iteration (fuzz_data) ; });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ fn fuzz_iteration<T: FuzzTestExecutor<U> + std::fmt::Display, U>(fuzz_data: Fuzz
);

let mut client =
ProgramTestClientBlocking::new(&[fuzzing_program_incorrect_integer_arithmetic_3]).unwrap();
ProgramTestClientBlocking::new(&[fuzzing_program_incorrect_integer_arithmetic_3], &[])
.unwrap();

let _ = fuzz_data.run_with_runtime(PROGRAM_ID_INCORRECT_INTEGER_ARITHMETIC_3, &mut client);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn fuzz_iteration<T: FuzzTestExecutor<U> + std::fmt::Display, U>(fuzz_data: Fuzz
);

let mut client =
ProgramTestClientBlocking::new(&[fuzzing_program_incorrect_ix_sequence_1]).unwrap();
ProgramTestClientBlocking::new(&[fuzzing_program_incorrect_ix_sequence_1], &[]).unwrap();

let _ = fuzz_data.run_with_runtime(PROGRAM_ID_INCORRECT_IX_SEQUENCE_1, &mut client);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ fn fuzz_iteration<T: FuzzTestExecutor<U> + std::fmt::Display, U>(fuzz_data: Fuzz
);

let mut client =
ProgramTestClientBlocking::new(&[fuzzing_program_callee, fuzzing_program_caller]).unwrap();
ProgramTestClientBlocking::new(&[fuzzing_program_callee, fuzzing_program_caller], &[])
.unwrap();

let _ = fuzz_data.run_with_runtime(PROGRAM_ID_CALLER, &mut client);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn fuzz_iteration<T: FuzzTestExecutor<U> + std::fmt::Display, U>(fuzz_data: Fuzz
);

let mut client =
ProgramTestClientBlocking::new(&[fuzzing_program_unauthorized_access_2]).unwrap();
ProgramTestClientBlocking::new(&[fuzzing_program_unauthorized_access_2], &[]).unwrap();

let _ = fuzz_data.run_with_runtime(PROGRAM_ID_UNAUTHORIZED_ACCESS_2, &mut client);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn fuzz_iteration<T: FuzzTestExecutor<U> + std::fmt::Display, U>(fuzz_data: Fuzz
);

let mut client =
ProgramTestClientBlocking::new(&[fuzzing_program_unchecked_arithmetic_0]).unwrap();
ProgramTestClientBlocking::new(&[fuzzing_program_unchecked_arithmetic_0], &[]).unwrap();

let _ = fuzz_data.run_with_runtime(PROGRAM_ID_UNCHECKED_ARITHMETIC_0, &mut client);
}
Expand Down

0 comments on commit 792fcbc

Please sign in to comment.