forked from foundry-rs/foundry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
83 lines (71 loc) · 2.76 KB
/
lib.rs
File metadata and controls
83 lines (71 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use proptest::test_runner::{RngAlgorithm, TestRng, TestRunner};
use tracing::trace;
/// Gas reports
pub mod gas_report;
/// Coverage reports
pub mod coverage;
/// The Forge test runner
mod runner;
use ethers::types::U256;
pub use runner::ContractRunner;
/// Forge test runners for multiple contracts
mod multi_runner;
pub use multi_runner::{MultiContractRunner, MultiContractRunnerBuilder};
/// reexport
pub use foundry_common::traits::TestFilter;
pub mod result;
/// The Forge EVM backend
pub use foundry_evm::*;
/// Metadata on how to run fuzz/invariant tests
#[derive(Debug, Clone, Copy, Default)]
pub struct TestOptions {
/// The number of test cases that must execute for each fuzz test
pub fuzz_runs: u32,
/// The maximum number of global test case rejections allowed
/// by proptest, to be encountered during usage of `vm.assume`
/// cheatcode.
pub fuzz_max_local_rejects: u32,
/// The maximum number of local test case rejections allowed
/// by proptest, to be encountered during usage of `vm.assume`
/// cheatcode.
pub fuzz_max_global_rejects: u32,
/// Optional seed for the fuzzing RNG algorithm
pub fuzz_seed: Option<U256>,
/// The number of runs that must execute for each invariant test group.
pub invariant_runs: u32,
/// The number of calls executed to attempt to break invariants in one run.
pub invariant_depth: u32,
/// Fails the invariant fuzzing if a revert occurs
pub invariant_fail_on_revert: bool,
/// Allows overriding an unsafe external call when running invariant tests. eg. reetrancy
/// checks
pub invariant_call_override: bool,
}
impl TestOptions {
pub fn invariant_fuzzer(&self) -> TestRunner {
self.fuzzer_with_cases(self.invariant_runs)
}
pub fn fuzzer(&self) -> TestRunner {
self.fuzzer_with_cases(self.fuzz_runs)
}
pub fn fuzzer_with_cases(&self, cases: u32) -> TestRunner {
// TODO: Add Options to modify the persistence
let cfg = proptest::test_runner::Config {
failure_persistence: None,
cases,
max_local_rejects: self.fuzz_max_local_rejects,
max_global_rejects: self.fuzz_max_global_rejects,
..Default::default()
};
if let Some(ref fuzz_seed) = self.fuzz_seed {
trace!(target: "forge::test", "building deterministic fuzzer with seed {}", fuzz_seed);
let mut bytes: [u8; 32] = [0; 32];
fuzz_seed.to_big_endian(&mut bytes);
let rng = TestRng::from_seed(RngAlgorithm::ChaCha, &bytes);
proptest::test_runner::TestRunner::new_with_rng(cfg, rng)
} else {
trace!(target: "forge::test", "building stochastic fuzzer");
proptest::test_runner::TestRunner::new(cfg)
}
}
}