Skip to content

Commit

Permalink
add test for vm.etch + load interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
karmacoma-eth committed Sep 17, 2024
1 parent 6e8f97c commit 7dc60fc
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tests/expected/all.json
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,26 @@
"num_bounded_loops": null
}
],
"test/Foundry.t.sol:TestNotEtchFriendly": [
{
"name": "check_etch_no_owner(address)",
"exitcode": 0,
"num_models": 0,
"models": null,
"num_paths": null,
"time": null,
"num_bounded_loops": null
},
{
"name": "check_etch_then_store()",
"exitcode": 0,
"num_models": 0,
"models": null,
"num_paths": null,
"time": null,
"num_bounded_loops": null
}
],
"test/Getter.t.sol:GetterTest": [
{
"name": "check_Getter(uint256)",
Expand Down
44 changes: 44 additions & 0 deletions tests/regression/test/Foundry.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,47 @@ contract FoundryTest is Test {
// assertEq(code, who.code);
// }
}


contract NotEtchFriendly {
address owner;

constructor() {
owner = msg.sender;
}

function beepBoop() public {
console2.log("owner is", owner);
require(msg.sender == owner, "NotEtchFriendly: only owner can beep boop");
}
}

contract TestNotEtchFriendly is Test {
NotEtchFriendly target;

function setUp() public {
/// @dev this is supported in foundry, but not halmos (can't vm.store to uninitialized account)
// make address(this) the owner of the yet-to-be-deployed contract
// vm.store(address(42), 0, bytes32(uint256(uint160(address(this)))));

// etch does not run the constructor, so owner is not set by the constructor
// additionally, vm.etch does not reset storage (unlike CREATE2)
vm.etch(address(42), type(NotEtchFriendly).runtimeCode);

target = NotEtchFriendly(address(42));
}

function check_etch_no_owner(address sender) external {
vm.prank(sender);
target.beepBoop();

assertEq(sender, address(0));
}

function check_etch_then_store() external {
// make address(this) the owner of the contract, emulating the constructor that did not run
vm.store(address(42), 0, bytes32(uint256(uint160(address(this)))));

target.beepBoop();
}
}

0 comments on commit 7dc60fc

Please sign in to comment.