/
TestDelegateProxy.sol
86 lines (65 loc) · 2.41 KB
/
TestDelegateProxy.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
pragma solidity 0.4.18;
import "truffle/Assert.sol";
import "./helpers/ThrowProxy.sol";
import "../contracts/common/DelegateProxy.sol";
import "../contracts/evmscript/ScriptHelpers.sol";
contract Target {
function returnSomething() public pure returns (bool) { return true; }
function dontReturn() {}
function fail() { revert(); }
function die() { selfdestruct(0); }
}
contract TestDelegateProxy is DelegateProxy {
using ScriptHelpers for *;
Target target;
ThrowProxy throwProxy;
function beforeAll() {
target = new Target();
}
function beforeEach() {
throwProxy = new ThrowProxy(address(this));
}
function testMinReturn0WithoutReturn() {
delegatedFwd(target, target.dontReturn.selector.toBytes(), 0);
}
function testMinReturn0WithReturn() {
delegatedFwd(target, target.returnSomething.selector.toBytes(), 0);
}
function testMinReturn32WithReturn() {
delegatedFwd(target, target.returnSomething.selector.toBytes(), 32);
}
function testFailsIfReturnLessThanMin() {
TestDelegateProxy(throwProxy).revertIfReturnLessThanMin();
throwProxy.assertThrows("should have reverted if return data was less than min");
}
function revertIfReturnLessThanMin() {
delegatedFwd(target, target.dontReturn.selector.toBytes(), 32);
}
function testSelfdestructIsRevertedWithMinReturn() {
TestDelegateProxy(throwProxy).revertIfReturnLessThanMinAndDie();
throwProxy.assertThrows("should have reverted to stop selfdestruct");
}
function revertIfReturnLessThanMinAndDie() {
delegatedFwd(target, target.die.selector.toBytes(), 32);
}
function testFailIfReverts() {
TestDelegateProxy(throwProxy).revertCall();
throwProxy.assertThrows("should have reverted if call reverted");
}
function revertCall() {
delegatedFwd(target, target.fail.selector.toBytes());
}
function testIsContractZero() {
bool result = isContract(address(0));
Assert.isFalse(result, "should return false");
}
function testIsContractAddress() {
address nonContract = 0x1234;
bool result = isContract(nonContract);
Assert.isFalse(result, "should return false");
}
// keep as last test as it will kill this contract
function testDieIfMinReturn0() {
delegatedFwd(target, target.die.selector.toBytes());
}
}