Skip to content

Commit

Permalink
chore: improve fuzz scrape bytecode test
Browse files Browse the repository at this point in the history
  • Loading branch information
grandizzy committed Sep 25, 2024
1 parent 883bb1c commit bd28311
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 52 deletions.
69 changes: 52 additions & 17 deletions crates/forge/tests/it/fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,27 +155,62 @@ async fn test_persist_fuzz_failure() {
assert_ne!(initial_calldata, new_calldata);
}

#[tokio::test(flavor = "multi_thread")]
async fn test_scrape_bytecode() {
let filter = Filter::new(".*", ".*", ".*fuzz/FuzzScrapeBytecode.t.sol");
let mut runner = TEST_DATA_DEFAULT.runner();
runner.test_options.fuzz.runs = 2000;
runner.test_options.fuzz.seed = Some(U256::from(100u32));
let suite_result = runner.test_collect(&filter);
forgetest_init!(test_can_scrape_bytecode, |prj, cmd| {
prj.add_source(
"FuzzerDict.sol",
r#"
// https://github.com/foundry-rs/foundry/issues/1168
contract FuzzerDict {
// Immutables should get added to the dictionary.
address public immutable immutableOwner;
// Regular storage variables should also get added to the dictionary.
address public storageOwner;
constructor(address _immutableOwner, address _storageOwner) {
immutableOwner = _immutableOwner;
storageOwner = _storageOwner;
}
}
"#,
)
.unwrap();

assert!(!suite_result.is_empty());
prj.add_test(
"FuzzerDictTest.t.sol",
r#"
import {Test} from "forge-std/Test.sol";
import "src/FuzzerDict.sol";
for (_, SuiteResult { test_results, .. }) in suite_result {
for (test_name, result) in test_results {
match test_name.as_str() {
"testImmutableOwner(address)" | "testStorageOwner(address)" => {
assert_eq!(result.status, TestStatus::Failure)
}
_ => {}
}
}
contract FuzzerDictTest is Test {
FuzzerDict fuzzerDict;
function setUp() public {
fuzzerDict = new FuzzerDict(address(100), address(200));
}
// Fuzzer should try `fuzzerDict.immutableOwner()` as input, causing this to fail
/// forge-config: default.fuzz.runs = 2000
function testImmutableOwner(address who) public {
assertTrue(who != fuzzerDict.immutableOwner());
}
// Fuzzer should try `fuzzerDict.storageOwner()` as input, causing this to fail
/// forge-config: default.fuzz.runs = 2000
function testStorageOwner(address who) public {
assertTrue(who != fuzzerDict.storageOwner());
}
}
"#,
)
.unwrap();

// Test that immutable address is used as fuzzed input and failing test.
cmd.args(["test", "--fuzz-seed", "100", "--mt", "testImmutableOwner"]).assert_failure();
// Test that storage address is used as fuzzed input and failing test.
cmd.forge_fuse()
.args(["test", "--fuzz-seed", "100", "--mt", "testStorageOwner"])
.assert_failure();
});

// tests that inline max-test-rejects config is properly applied
forgetest_init!(test_inline_max_test_rejects, |prj, cmd| {
Expand Down
35 changes: 0 additions & 35 deletions testdata/default/fuzz/FuzzScrapeBytecode.t.sol

This file was deleted.

0 comments on commit bd28311

Please sign in to comment.