Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

chore: improve fuzz scrape bytecode test #8953

Merged
merged 3 commits into from
Sep 25, 2024
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
67 changes: 50 additions & 17 deletions crates/forge/tests/it/fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,27 +155,60 @@ 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));
}

/// forge-config: default.fuzz.runs = 2000
function testImmutableOwner(address who) public {
assertTrue(who != fuzzerDict.immutableOwner());
}

/// 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, causing test to fail.
cmd.args(["test", "--fuzz-seed", "100", "--mt", "testImmutableOwner"]).assert_failure();
// Test that storage address is used as fuzzed input, causing test to fail.
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.

Loading