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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
|
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Unit tests for the view and pure checker.
*/
#include <test/libsolidity/AnalysisFramework.h>
#include <boost/test/unit_test.hpp>
#include <string>
using namespace std;
namespace dev
{
namespace solidity
{
namespace test
{
BOOST_FIXTURE_TEST_SUITE(ViewPureChecker, AnalysisFramework)
BOOST_AUTO_TEST_CASE(smoke_test)
{
char const* text = R"(
contract C {
uint x;
function g() pure public {}
function f() view public returns (uint) { return now; }
function h() public { x = 2; }
function i() payable public { x = 2; }
}
)";
CHECK_SUCCESS_NO_WARNINGS(text);
}
BOOST_AUTO_TEST_CASE(call_internal_functions_success)
{
char const* text = R"(
contract C {
function g() pure public { g(); }
function f() view public returns (uint) { f(); g(); }
function h() public { h(); g(); f(); }
function i() payable public { i(); h(); g(); f(); }
}
)";
CHECK_SUCCESS_NO_WARNINGS(text);
}
BOOST_AUTO_TEST_CASE(suggest_pure)
{
char const* text = R"(
contract C {
function g() view public { }
}
)";
CHECK_WARNING(text, "can be restricted to pure");
}
BOOST_AUTO_TEST_CASE(suggest_view)
{
char const* text = R"(
contract C {
uint x;
function g() public returns (uint) { return x; }
}
)";
CHECK_WARNING(text, "can be restricted to view");
}
BOOST_AUTO_TEST_CASE(call_internal_functions_fail)
{
CHECK_ERROR(
"contract C{ function f() pure public { g(); } function g() view public {} }",
TypeError,
"Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires \"view\""
);
}
BOOST_AUTO_TEST_CASE(write_storage_fail)
{
CHECK_WARNING(
"contract C{ uint x; function f() view public { x = 2; } }",
"Function declared as view, but this expression (potentially) modifies the state and thus requires non-payable (the default) or payable."
);
}
BOOST_AUTO_TEST_CASE(environment_access)
{
vector<string> view{
"block.coinbase",
"block.timestamp",
"block.blockhash(7)",
"block.difficulty",
"block.number",
"block.gaslimit",
"msg.gas",
"msg.value",
"msg.sender",
"tx.origin",
"tx.gasprice",
"this",
"address(1).balance"
};
vector<string> pure{
"msg.data",
"msg.data[0]",
"msg.sig",
"block.blockhash", // Not evaluating the function
"msg",
"block",
"tx"
};
for (string const& x: view)
{
CHECK_ERROR(
"contract C { function f() pure public { var x = " + x + "; x; } }",
TypeError,
"Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires \"view\""
);
}
for (string const& x: pure)
{
CHECK_WARNING_ALLOW_MULTI(
"contract C { function f() view public { var x = " + x + "; x; } }",
(std::vector<std::string>{
"Function state mutability can be restricted to pure",
"Use of the \"var\" keyword is deprecated."
}));
}
}
BOOST_AUTO_TEST_CASE(view_error_for_050)
{
CHECK_ERROR(
"pragma experimental \"v0.5.0\"; contract C { uint x; function f() view { x = 2; } }",
TypeError,
"Function declared as view, but this expression (potentially) modifies the state and thus requires non-payable (the default) or payable."
);
}
BOOST_AUTO_TEST_CASE(modifiers)
{
string text = R"(
contract D {
uint x;
modifier purem(uint) { _; }
modifier viewm(uint) { uint a = x; _; a; }
modifier nonpayablem(uint) { x = 2; _; }
}
contract C is D {
function f() purem(0) pure public {}
function g() viewm(0) view public {}
function h() nonpayablem(0) public {}
function i() purem(x) view public {}
function j() viewm(x) view public {}
function k() nonpayablem(x) public {}
function l() purem(x = 2) public {}
function m() viewm(x = 2) public {}
function n() nonpayablem(x = 2) public {}
}
)";
CHECK_SUCCESS_NO_WARNINGS(text);
}
BOOST_AUTO_TEST_CASE(interface)
{
string text = R"(
interface D {
function f() view external;
}
contract C is D {
function f() view external {}
}
)";
CHECK_SUCCESS_NO_WARNINGS(text);
}
BOOST_AUTO_TEST_CASE(overriding)
{
string text = R"(
contract D {
uint x;
function f() public { x = 2; }
}
contract C is D {
function f() public {}
}
)";
CHECK_SUCCESS_NO_WARNINGS(text);
}
BOOST_AUTO_TEST_CASE(returning_structs)
{
string text = R"(
contract C {
struct S { uint x; }
S s;
function f() view internal returns (S storage) {
return s;
}
function g() public {
f().x = 2;
}
function h() view public {
f();
f().x;
}
}
)";
CHECK_SUCCESS_NO_WARNINGS(text);
}
BOOST_AUTO_TEST_CASE(mappings)
{
string text = R"(
contract C {
mapping(uint => uint) a;
function f() view public {
a;
}
function g() view public {
a[2];
}
function h() public {
a[2] = 3;
}
}
)";
CHECK_SUCCESS_NO_WARNINGS(text);
}
BOOST_AUTO_TEST_CASE(local_storage_variables)
{
string text = R"(
contract C {
struct S { uint a; }
S s;
function f() view public {
S storage x = s;
x;
}
function g() view public {
S storage x = s;
x = s;
}
function i() public {
s.a = 2;
}
function h() public {
S storage x = s;
x.a = 2;
}
}
)";
CHECK_SUCCESS_NO_WARNINGS(text);
}
BOOST_AUTO_TEST_CASE(builtin_functions)
{
string text = R"(
contract C {
function f() public {
this.transfer(1);
require(this.send(2));
selfdestruct(this);
require(this.delegatecall());
require(this.call());
}
function g() pure public {
bytes32 x = keccak256("abc");
bytes32 y = sha256("abc");
address z = ecrecover(1, 2, 3, 4);
require(true);
assert(true);
x; y; z;
}
function() payable public {}
}
)";
CHECK_SUCCESS_NO_WARNINGS(text);
}
BOOST_AUTO_TEST_CASE(function_types)
{
string text = R"(
contract C {
function f() pure public {
function () external nonpayFun;
function () external view viewFun;
function () external pure pureFun;
nonpayFun;
viewFun;
pureFun;
pureFun();
}
function g() view public {
function () external view viewFun;
viewFun();
}
function h() public {
function () external nonpayFun;
nonpayFun();
}
}
)";
CHECK_SUCCESS_NO_WARNINGS(text);
}
BOOST_AUTO_TEST_CASE(selector)
{
string text = R"(
contract C {
uint public x;
function f() payable public {
}
function g() pure public returns (bytes4) {
return this.f.selector ^ this.x.selector;
}
}
)";
CHECK_SUCCESS_NO_WARNINGS(text);
}
BOOST_AUTO_TEST_CASE(selector_complex)
{
string text = R"(
contract C {
function f(C c) pure public returns (C) {
return c;
}
function g() pure public returns (bytes4) {
// By passing `this`, we read from the state, even if f itself is pure.
return f(this).f.selector;
}
}
)";
CHECK_ERROR(text, TypeError, "reads from the environment or state and thus requires \"view\"");
}
BOOST_AUTO_TEST_CASE(selector_complex2)
{
string text = R"(
contract C {
function f() payable public returns (C) {
return this;
}
function g() pure public returns (bytes4) {
C x = C(0x123);
return x.f.selector;
}
}
)";
CHECK_SUCCESS_NO_WARNINGS(text);
}
BOOST_AUTO_TEST_CASE(creation)
{
string text = R"(
contract D {}
contract C {
function f() public { new D(); }
}
)";
CHECK_SUCCESS_NO_WARNINGS(text);
}
BOOST_AUTO_TEST_CASE(assembly)
{
string text = R"(
contract C {
struct S { uint x; }
S s;
function e() pure public {
assembly { mstore(keccak256(0, 20), mul(s_slot, 2)) }
}
function f() pure public {
uint x;
assembly { x := 7 }
}
function g() view public {
assembly { for {} 1 { pop(sload(0)) } { } pop(gas) }
}
function h() view public {
assembly { function g() { pop(blockhash(20)) } }
}
function j() public {
assembly { pop(call(0, 1, 2, 3, 4, 5, 6)) }
}
function k() public {
assembly { pop(call(gas, 1, 2, 3, 4, 5, 6)) }
}
}
)";
CHECK_SUCCESS_NO_WARNINGS(text);
}
BOOST_AUTO_TEST_CASE(assembly_staticcall)
{
string text = R"(
contract C {
function i() view public {
assembly { pop(staticcall(gas, 1, 2, 3, 4, 5)) }
}
}
)";
CHECK_WARNING(text, "only available after the Metropolis");
}
BOOST_AUTO_TEST_CASE(assembly_jump)
{
string text = R"(
contract C {
function k() public {
assembly { jump(2) }
}
}
)";
CHECK_WARNING(text, "low-level EVM features");
}
BOOST_AUTO_TEST_CASE(constant)
{
string text = R"(
contract C {
uint constant x = 2;
function k() pure public returns (uint) {
return x;
}
}
)";
CHECK_SUCCESS_NO_WARNINGS(text);
}
BOOST_AUTO_TEST_SUITE_END()
}
}
}
|