Skip to content

Commit

Permalink
Add test for prank() cheatcode
Browse files Browse the repository at this point in the history
  • Loading branch information
elopez committed Dec 13, 2024
1 parent ee747c7 commit c2db857
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/test/Tests/Cheat.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Tests.Cheat (cheatTests) where

import Test.Tasty (TestTree, testGroup)

import Common (testContract', solcV, solved)
import Common (testContract', solcV, solved, passed)

import Echidna.Types.Campaign (WorkerType(..))

Expand All @@ -15,4 +15,10 @@ cheatTests =
[ ("echidna_ffi passed", solved "echidna_ffi") ]
, testContract' "cheat/gas.sol" (Just "TestCheatGas") (Just (> solcV (0,5,0))) (Just "cheat/ffi.yaml") False FuzzWorker
[ ("echidna_gas_zero passed", solved "echidna_gas_zero") ]
, testContract' "cheat/prank.sol" (Just "TestPrank") (Just (> solcV (0,5,0))) (Just "cheat/prank.yaml") False FuzzWorker
[ ("withPrank failed", passed "withPrank")
, ("withStartPrank failed", passed "withStartPrank")
, ("withStartPrankStopPrank failed", passed "withStartPrankStopPrank")
, ("withNothing failed", passed "withNothing")
]
]
94 changes: 94 additions & 0 deletions tests/solidity/cheat/prank.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
interface IHevm {
function prank(address newSender) external;
function startPrank(address sender) external;
function stopPrank() external;
}

contract ExpectedCreator {
event Sender(address);
event AssertionFailed(string);
IHevm constant vm = IHevm(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D);

constructor(address s) public {
if (s != msg.sender) {
emit Sender(msg.sender);
emit AssertionFailed("fail on construct");
}
}
}

contract SenderVerifierChild {
event Sender(address);
event AssertionFailed(string);
IHevm constant vm = IHevm(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D);

function verifyMsgSender(address s) public {
if (s != msg.sender) {
emit Sender(msg.sender);
emit AssertionFailed("fail on inner call");
}
}
}

contract SenderVerifierParent {
event Sender(address);
event AssertionFailed(string);
IHevm constant vm = IHevm(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D);

SenderVerifierChild c;
constructor() public {
c = new SenderVerifierChild();
}

function verifyMsgSender(address s) public {
if (s != msg.sender) {
emit Sender(msg.sender);
emit AssertionFailed("fail on first call");
}
c.verifyMsgSender(address(this));
new ExpectedCreator(address(this));
}
}

contract TestPrank {
IHevm constant vm = IHevm(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D);

SenderVerifierParent p;
constructor() public {
p = new SenderVerifierParent();
}

function withPrank() public {
vm.prank(address(0x123));
p.verifyMsgSender(address(0x123));
p.verifyMsgSender(address(this));

vm.prank(address(0x123));
new ExpectedCreator(address(0x123));
new ExpectedCreator(address(this));
}

function withStartPrank() public {
vm.startPrank(address(0x123));
p.verifyMsgSender(address(0x123));
p.verifyMsgSender(address(0x123));
new ExpectedCreator(address(0x123));
new ExpectedCreator(address(0x123));
}

function withStartPrankStopPrank() public {
vm.startPrank(address(0x123));
p.verifyMsgSender(address(0x123));
p.verifyMsgSender(address(0x123));
new ExpectedCreator(address(0x123));
new ExpectedCreator(address(0x123));
vm.stopPrank();
p.verifyMsgSender(address(this));
new ExpectedCreator(address(this));
}

function withNothing() public {
p.verifyMsgSender(address(this));
new ExpectedCreator(address(this));
}
}
3 changes: 3 additions & 0 deletions tests/solidity/cheat/prank.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
testLimit: 1000
seqLen: 10
testMode: assertion

0 comments on commit c2db857

Please sign in to comment.