forked from dragonfly-xyz/useful-solidity-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DelegateCallModifiers.t.sol
66 lines (57 loc) · 2.47 KB
/
DelegateCallModifiers.t.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
pragma solidity ^0.8.17;
import "../patterns/only-delegatecall-no-delegatecall/DelegateCallModifiers.sol";
import "./TestUtils.sol";
contract DelegateCallModifiersTest is TestUtils {
Logic logic = new Logic();
SafeLogic safeLogic = new SafeLogic();
function test_unsafe_canSkimFromProxy() external {
Logic inst = Logic(payable(new Proxy(logic, address(this))));
payable(inst).transfer(100);
address payable receiver = _randomAddress();
inst.skim(receiver);
assertEq(receiver.balance, 100);
}
function test_safe_cannotSkimFromProxy() external {
Logic inst = Logic(payable(new Proxy(safeLogic, address(this))));
payable(inst).transfer(100);
address payable receiver = _randomAddress();
vm.expectRevert('must not be delegatecall');
inst.skim(receiver);
}
function test_unsafe_canDieProxy() external {
Logic inst = Logic(payable(new Proxy(logic, address(this))));
payable(inst).transfer(100);
address payable receiver = _randomAddress();
inst.die(receiver);
assertEq(receiver.balance, 100);
// Note: No way currently to assert selfdestruct in foundry since bytecode is
// cleared after the TX (this test) ends.
}
function test_safe_canDieProxy() external {
Logic inst = Logic(payable(new Proxy(safeLogic, address(this))));
payable(inst).transfer(100);
address payable receiver = _randomAddress();
inst.die(receiver);
assertEq(receiver.balance, 100);
// Note: No way currently to assert selfdestruct in foundry since bytecode is
// cleared after the TX (this test) ends.
}
function test_unsafe_canDieLogic() external {
payable(logic).transfer(100);
address payable receiver = _randomAddress();
logic.initialize(address(this));
logic.die(receiver);
assertEq(receiver.balance, 100);
// Note: No way currently to assert selfdestruct in foundry since bytecode is
// cleared after the TX (this test) ends.
}
function test_safe_cannotDieLogic() external {
payable(safeLogic).transfer(100);
address payable receiver = _randomAddress();
safeLogic.initialize(address(this));
vm.expectRevert('must be a delegatecall');
safeLogic.die(receiver);
// Note: No way currently to assert selfdestruct in foundry since bytecode is
// cleared after the TX (this test) ends.
}
}