Skip to content

Commit 08a8203

Browse files
committed
feat: add test cases
1 parent e9717ad commit 08a8203

File tree

2 files changed

+61
-8
lines changed

2 files changed

+61
-8
lines changed

src/test-cases/precompiles/CallInputs.sol

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,41 @@ contract TestCallInputs is Assertion, Test {
1212
constructor() payable {}
1313

1414
function testGetCallInputs() external view {
15-
PhEvm.CallInputs[] memory callInputs = ph.getCallInputs(address(TARGET), Target.readStorage.selector);
15+
PhEvm.CallInputs[] memory callInputs = ph.getCallInputs(
16+
address(TARGET),
17+
Target.readStorage.selector
18+
);
1619
require(callInputs.length == 1, "callInputs.length != 1");
1720
PhEvm.CallInputs memory callInput = callInputs[0];
1821

19-
require(callInput.target_address == address(TARGET), "callInput.target_address != target");
22+
require(
23+
callInput.target_address == address(TARGET),
24+
"callInput.target_address != target"
25+
);
2026
require(callInput.input.length == 0, "callInput.input.length != 0");
2127
require(callInput.value == 0, "callInput.value != 0");
2228

23-
callInputs = ph.getCallInputs(address(TARGET), Target.writeStorage.selector);
29+
callInputs = ph.getCallInputs(
30+
address(TARGET),
31+
Target.writeStorage.selector
32+
);
2433
require(callInputs.length == 2, "callInputs.length != 2");
2534

2635
callInput = callInputs[0];
27-
require(callInput.target_address == address(TARGET), "callInput.target_address != target");
36+
require(
37+
callInput.target_address == address(TARGET),
38+
"callInput.target_address != target"
39+
);
2840
require(callInput.input.length == 32, "callInput.input.length != 32");
2941
uint256 param = abi.decode(callInput.input, (uint256));
3042
require(param == 1, "First writeStorage param should be 1");
3143
require(callInput.value == 0, "callInput.value != 0");
3244

3345
callInput = callInputs[1];
34-
require(callInput.target_address == address(TARGET), "callInput.target_address != target");
46+
require(
47+
callInput.target_address == address(TARGET),
48+
"callInput.target_address != target"
49+
);
3550
require(callInput.input.length == 32, "callInput.input.length != 32");
3651
param = abi.decode(callInput.input, (uint256));
3752
require(param == 2, "Second writeStorage param should be 2");
@@ -40,7 +55,21 @@ contract TestCallInputs is Assertion, Test {
4055
callInputs = ph.getCallInputs(address(TARGET), bytes4(0));
4156
require(callInputs.length == 0, "callInputs.length != 0");
4257
}
43-
//TODO: validate same selector to multiple targets does not get included for wrong address
58+
function testCallInputsWrongTarget() external view {
59+
PhEvm.CallInputs[] memory callInputs = ph.getCallInputs(
60+
address(0x000000000000000000000000000000000000dEaD),
61+
Target.readStorage.selector
62+
);
63+
require(callInputs.length == 0, "callInputs.length != 0");
64+
}
65+
66+
function testCallNoSelector() external view {
67+
PhEvm.CallInputs[] memory callInputs = ph.getCallInputs(
68+
address(TARGET),
69+
bytes4(0)
70+
);
71+
require(callInputs.length == 0, "callInputs.length != 0");
72+
}
4473

4574
function triggers() external view override {
4675
registerCallTrigger(this.testGetCallInputs.selector);
@@ -52,5 +81,15 @@ contract TriggeringTx {
5281
TARGET.writeStorage(1);
5382
TARGET.writeStorage(2);
5483
TARGET.readStorage();
84+
85+
address fallbackTarget = address(0x000000000000000000000000000000000000dEaD);
86+
(bool success, ) = fallbackTarget.call(
87+
abi.encodeWithSelector(TARGET.readStorage.selector)
88+
);
89+
require(success, "call failed");
90+
91+
(success, ) = address(TARGET).call("");
92+
require(success, "call failed");
5593
}
5694
}
95+

src/test-cases/precompiles/Forking.sol

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ contract TestForking is Assertion, Test {
4444
"post state newTarget.code.length should not be 0"
4545
);
4646
}
47-
//TODO: add balance check on switching
47+
function testForkSwitchBalance() external {
48+
require(address(TARGET).balance == 1000, "balance != 1000");
49+
ph.forkPreState();
50+
require(address(TARGET).balance == 0, "balance != 0");
51+
ph.forkPostState();
52+
require(address(TARGET).balance == 1000, "balance != 1000");
53+
}
4854

4955
function testPersistTargetContracts() external {
5056
require(someInitValue == 1, "someInitValue != 1");
@@ -74,4 +80,12 @@ contract TestForking is Assertion, Test {
7480
registerCallTrigger(this.testForkSwitchNewDeployedContract.selector);
7581
registerCallTrigger(this.testPersistTargetContracts.selector);
7682
}
77-
}
83+
}
84+
85+
contract TriggeringTx {
86+
constructor() payable {
87+
TARGET.incrementStorage();
88+
(bool success, ) = address(TARGET).call{value: 1000}("");
89+
require(success, "call failed");
90+
}
91+
}

0 commit comments

Comments
 (0)