forked from foundry-rs/foundry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMockCall.t.sol
323 lines (226 loc) · 9.4 KB
/
MockCall.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity 0.8.18;
import "ds-test/test.sol";
import "cheats/Vm.sol";
contract Mock {
uint256 state = 0;
function numberA() public pure returns (uint256) {
return 1;
}
function numberB() public pure returns (uint256) {
return 2;
}
function add(uint256 a, uint256 b) public pure returns (uint256) {
return a + b;
}
function pay(uint256 a) public payable returns (uint256) {
return a;
}
function noReturnValue() public {
// Does nothing of value, but also ensures that Solidity will 100%
// generate an `extcodesize` check.
state += 1;
}
}
contract NestedMock {
Mock private inner;
constructor(Mock _inner) {
inner = _inner;
}
function sum() public view returns (uint256) {
return inner.numberA() + inner.numberB();
}
}
contract NestedMockDelegateCall {
Mock private inner;
constructor(Mock _inner) {
inner = _inner;
}
function sum() public returns (uint256) {
(, bytes memory dataA) = address(inner).delegatecall(abi.encodeWithSelector(Mock.numberA.selector));
(, bytes memory dataB) = address(inner).delegatecall(abi.encodeWithSelector(Mock.numberB.selector));
return abi.decode(dataA, (uint256)) + abi.decode(dataB, (uint256));
}
}
contract MockCallTest is DSTest {
Vm constant vm = Vm(HEVM_ADDRESS);
function testMockGetters() public {
Mock target = new Mock();
// pre-mock
assertEq(target.numberA(), 1);
assertEq(target.numberB(), 2);
vm.mockCall(address(target), abi.encodeWithSelector(target.numberB.selector), abi.encode(10));
// post-mock
assertEq(target.numberA(), 1);
assertEq(target.numberB(), 10);
}
function testMockNested() public {
Mock inner = new Mock();
NestedMock target = new NestedMock(inner);
// pre-mock
assertEq(target.sum(), 3);
vm.mockCall(address(inner), abi.encodeWithSelector(inner.numberB.selector), abi.encode(9));
// post-mock
assertEq(target.sum(), 10);
}
// Ref: https://github.com/foundry-rs/foundry/issues/8066
function testMockNestedDelegate() public {
Mock inner = new Mock();
NestedMockDelegateCall target = new NestedMockDelegateCall(inner);
assertEq(target.sum(), 3);
vm.mockCall(address(inner), abi.encodeWithSelector(inner.numberB.selector), abi.encode(9));
assertEq(target.sum(), 10);
}
function testMockSelector() public {
Mock target = new Mock();
assertEq(target.add(5, 5), 10);
vm.mockCall(address(target), abi.encodeWithSelector(target.add.selector), abi.encode(11));
assertEq(target.add(5, 5), 11);
}
function testMockCalldata() public {
Mock target = new Mock();
assertEq(target.add(5, 5), 10);
assertEq(target.add(6, 4), 10);
vm.mockCall(address(target), abi.encodeWithSelector(target.add.selector, 5, 5), abi.encode(11));
assertEq(target.add(5, 5), 11);
assertEq(target.add(6, 4), 10);
}
function testClearMockedCalls() public {
Mock target = new Mock();
vm.mockCall(address(target), abi.encodeWithSelector(target.numberB.selector), abi.encode(10));
assertEq(target.numberA(), 1);
assertEq(target.numberB(), 10);
vm.clearMockedCalls();
assertEq(target.numberA(), 1);
assertEq(target.numberB(), 2);
}
function testMockCallMultiplePartialMatch() public {
Mock mock = new Mock();
vm.mockCall(address(mock), abi.encodeWithSelector(mock.add.selector), abi.encode(10));
vm.mockCall(address(mock), abi.encodeWithSelector(mock.add.selector, 2), abi.encode(20));
vm.mockCall(address(mock), abi.encodeWithSelector(mock.add.selector, 2, 3), abi.encode(30));
assertEq(mock.add(1, 2), 10);
assertEq(mock.add(2, 2), 20);
assertEq(mock.add(2, 3), 30);
}
function testMockCallWithValue() public {
Mock mock = new Mock();
vm.mockCall(address(mock), 10, abi.encodeWithSelector(mock.pay.selector), abi.encode(10));
assertEq(mock.pay{value: 10}(1), 10);
assertEq(mock.pay(1), 1);
for (uint256 i = 0; i < 100; i++) {
vm.mockCall(address(mock), i, abi.encodeWithSelector(mock.pay.selector), abi.encode(i * 2));
}
assertEq(mock.pay(1), 0);
assertEq(mock.pay{value: 10}(1), 20);
assertEq(mock.pay{value: 50}(1), 100);
}
function testMockCallWithValueCalldataPrecedence() public {
Mock mock = new Mock();
vm.mockCall(address(mock), 10, abi.encodeWithSelector(mock.pay.selector), abi.encode(10));
vm.mockCall(address(mock), abi.encodeWithSelector(mock.pay.selector, 2), abi.encode(2));
assertEq(mock.pay{value: 10}(1), 10);
assertEq(mock.pay{value: 10}(2), 2);
assertEq(mock.pay(2), 2);
}
function testMockCallEmptyAccount() public {
Mock mock = Mock(address(100));
vm.mockCall(address(mock), abi.encodeWithSelector(mock.add.selector), abi.encode(10));
vm.mockCall(address(mock), abi.encodeWithSelector(mock.noReturnValue.selector), abi.encode());
assertEq(mock.add(1, 2), 10);
mock.noReturnValue();
}
}
contract MockCallRevertTest is DSTest {
Vm constant vm = Vm(HEVM_ADDRESS);
error TestError(bytes msg);
bytes constant ERROR_MESSAGE = "ERROR_MESSAGE";
function testMockGettersRevert() public {
Mock target = new Mock();
// pre-mock
assertEq(target.numberA(), 1);
assertEq(target.numberB(), 2);
vm.mockCallRevert(address(target), abi.encodeWithSelector(target.numberB.selector), ERROR_MESSAGE);
// post-mock
assertEq(target.numberA(), 1);
vm.expectRevert();
target.numberB();
}
function testMockRevertWithCustomError() public {
Mock target = new Mock();
assertEq(target.numberA(), 1);
assertEq(target.numberB(), 2);
bytes memory customError = abi.encodeWithSelector(TestError.selector, ERROR_MESSAGE);
vm.mockCallRevert(address(target), abi.encodeWithSelector(target.numberB.selector), customError);
assertEq(target.numberA(), 1);
vm.expectRevert(customError);
target.numberB();
}
function testMockNestedRevert() public {
Mock inner = new Mock();
NestedMock target = new NestedMock(inner);
assertEq(target.sum(), 3);
vm.mockCallRevert(address(inner), abi.encodeWithSelector(inner.numberB.selector), ERROR_MESSAGE);
vm.expectRevert(ERROR_MESSAGE);
target.sum();
}
function testMockCalldataRevert() public {
Mock target = new Mock();
assertEq(target.add(5, 5), 10);
assertEq(target.add(6, 4), 10);
vm.mockCallRevert(address(target), abi.encodeWithSelector(target.add.selector, 5, 5), ERROR_MESSAGE);
assertEq(target.add(6, 4), 10);
vm.expectRevert(ERROR_MESSAGE);
target.add(5, 5);
}
function testClearMockRevertedCalls() public {
Mock target = new Mock();
vm.mockCallRevert(address(target), abi.encodeWithSelector(target.numberB.selector), ERROR_MESSAGE);
vm.clearMockedCalls();
assertEq(target.numberA(), 1);
assertEq(target.numberB(), 2);
}
function testMockCallRevertPartialMatch() public {
Mock mock = new Mock();
vm.mockCallRevert(address(mock), abi.encodeWithSelector(mock.add.selector, 2), ERROR_MESSAGE);
assertEq(mock.add(1, 2), 3);
vm.expectRevert(ERROR_MESSAGE);
mock.add(2, 3);
}
function testMockCallRevertWithValue() public {
Mock mock = new Mock();
vm.mockCallRevert(address(mock), 10, abi.encodeWithSelector(mock.pay.selector), ERROR_MESSAGE);
assertEq(mock.pay(1), 1);
assertEq(mock.pay(2), 2);
vm.expectRevert(ERROR_MESSAGE);
mock.pay{value: 10}(1);
}
function testMockCallResetsMockCallRevert() public {
Mock mock = new Mock();
vm.mockCallRevert(address(mock), abi.encodeWithSelector(mock.add.selector), ERROR_MESSAGE);
vm.mockCall(address(mock), abi.encodeWithSelector(mock.add.selector), abi.encode(5));
assertEq(mock.add(2, 3), 5);
}
function testMockCallRevertResetsMockCall() public {
Mock mock = new Mock();
vm.mockCall(address(mock), abi.encodeWithSelector(mock.add.selector), abi.encode(5));
assertEq(mock.add(2, 3), 5);
vm.mockCallRevert(address(mock), abi.encodeWithSelector(mock.add.selector), ERROR_MESSAGE);
vm.expectRevert(ERROR_MESSAGE);
mock.add(2, 3);
}
function testMockCallRevertWithCall() public {
Mock mock = new Mock();
bytes memory customError = abi.encodeWithSelector(TestError.selector, ERROR_MESSAGE);
vm.mockCallRevert(address(mock), abi.encodeWithSelector(mock.add.selector), customError);
(bool success, bytes memory data) = address(mock).call(abi.encodeWithSelector(Mock.add.selector, 2, 3));
assertEq(success, false);
assertEq(data, customError);
}
function testMockCallEmptyAccount() public {
Mock mock = Mock(address(100));
vm.mockCallRevert(address(mock), abi.encodeWithSelector(mock.add.selector), ERROR_MESSAGE);
vm.expectRevert(ERROR_MESSAGE);
mock.add(1, 2);
}
}