Skip to content

Commit 4a38578

Browse files
committed
Add test for prank() cheatcode
1 parent 4bd81d3 commit 4a38578

File tree

3 files changed

+104
-1
lines changed

3 files changed

+104
-1
lines changed

src/test/Tests/Cheat.hs

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Tests.Cheat (cheatTests) where
22

33
import Test.Tasty (TestTree, testGroup)
44

5-
import Common (testContract', solcV, solved)
5+
import Common (testContract', solcV, solved, passed)
66

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

@@ -15,4 +15,10 @@ cheatTests =
1515
[ ("echidna_ffi passed", solved "echidna_ffi") ]
1616
, testContract' "cheat/gas.sol" (Just "TestCheatGas") (Just (> solcV (0,5,0))) (Just "cheat/ffi.yaml") False FuzzWorker
1717
[ ("echidna_gas_zero passed", solved "echidna_gas_zero") ]
18+
, testContract' "cheat/prank.sol" (Just "TestPrank") (Just (> solcV (0,5,0))) (Just "cheat/prank.yaml") False FuzzWorker
19+
[ ("withPrank failed", passed "withPrank")
20+
, ("withStartPrank failed", passed "withStartPrank")
21+
, ("withStartPrankStopPrank failed", passed "withStartPrankStopPrank")
22+
, ("withNothing failed", passed "withNothing")
23+
]
1824
]

tests/solidity/cheat/prank.sol

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
interface IHevm {
2+
function prank(address newSender) external;
3+
function startPrank(address sender) external;
4+
function stopPrank() external;
5+
}
6+
7+
contract ExpectedCreator {
8+
event Sender(address);
9+
event AssertionFailed(string);
10+
IHevm constant vm = IHevm(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D);
11+
12+
constructor(address s) public {
13+
if (s != msg.sender) {
14+
emit Sender(msg.sender);
15+
emit AssertionFailed("fail on construct");
16+
}
17+
}
18+
}
19+
20+
contract SenderVerifierChild {
21+
event Sender(address);
22+
event AssertionFailed(string);
23+
IHevm constant vm = IHevm(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D);
24+
25+
function verifyMsgSender(address s) public {
26+
if (s != msg.sender) {
27+
emit Sender(msg.sender);
28+
emit AssertionFailed("fail on inner call");
29+
}
30+
}
31+
}
32+
33+
contract SenderVerifierParent {
34+
event Sender(address);
35+
event AssertionFailed(string);
36+
IHevm constant vm = IHevm(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D);
37+
38+
SenderVerifierChild c;
39+
constructor() public {
40+
c = new SenderVerifierChild();
41+
}
42+
43+
function verifyMsgSender(address s) public {
44+
if (s != msg.sender) {
45+
emit Sender(msg.sender);
46+
emit AssertionFailed("fail on first call");
47+
}
48+
c.verifyMsgSender(address(this));
49+
new ExpectedCreator(address(this));
50+
}
51+
}
52+
53+
contract TestPrank {
54+
IHevm constant vm = IHevm(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D);
55+
56+
SenderVerifierParent p;
57+
constructor() public {
58+
p = new SenderVerifierParent();
59+
}
60+
61+
function withPrank() public {
62+
vm.prank(address(0x123));
63+
p.verifyMsgSender(address(0x123));
64+
p.verifyMsgSender(address(this));
65+
66+
vm.prank(address(0x123));
67+
new ExpectedCreator(address(0x123));
68+
new ExpectedCreator(address(this));
69+
}
70+
71+
function withStartPrank() public {
72+
vm.startPrank(address(0x123));
73+
p.verifyMsgSender(address(0x123));
74+
p.verifyMsgSender(address(0x123));
75+
new ExpectedCreator(address(0x123));
76+
new ExpectedCreator(address(0x123));
77+
}
78+
79+
function withStartPrankStopPrank() public {
80+
vm.startPrank(address(0x123));
81+
p.verifyMsgSender(address(0x123));
82+
p.verifyMsgSender(address(0x123));
83+
new ExpectedCreator(address(0x123));
84+
new ExpectedCreator(address(0x123));
85+
vm.stopPrank();
86+
p.verifyMsgSender(address(this));
87+
new ExpectedCreator(address(this));
88+
}
89+
90+
function withNothing() public {
91+
p.verifyMsgSender(address(this));
92+
new ExpectedCreator(address(this));
93+
}
94+
}

tests/solidity/cheat/prank.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
testLimit: 1000
2+
seqLen: 10
3+
testMode: assertion

0 commit comments

Comments
 (0)