1 /*
2 This file is part of solidity.
3
4 solidity is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 solidity is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with solidity. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /**
18 * Unit tests for the view and pure checker.
19 */
20
21 #include <test/libsolidity/AnalysisFramework.h>
22
23 #include <test/Common.h>
24
25 #include <boost/test/unit_test.hpp>
26
27 #include <string>
28 #include <tuple>
29
30 using namespace std;
31 using namespace solidity::langutil;
32
33 namespace solidity::frontend::test
34 {
35
BOOST_FIXTURE_TEST_SUITE(ViewPureChecker,AnalysisFramework)36 BOOST_FIXTURE_TEST_SUITE(ViewPureChecker, AnalysisFramework)
37
38 BOOST_AUTO_TEST_CASE(environment_access)
39 {
40 vector<string> view{
41 "block.coinbase",
42 "block.timestamp",
43 "block.difficulty",
44 "block.number",
45 "block.gaslimit",
46 "blockhash(7)",
47 "gasleft()",
48 "msg.value",
49 "msg.sender",
50 "tx.origin",
51 "tx.gasprice",
52 "this",
53 "address(1).balance",
54 };
55 if (solidity::test::CommonOptions::get().evmVersion().hasStaticCall())
56 view.emplace_back("address(0x4242).staticcall(\"\")");
57
58 // ``block.blockhash`` and ``blockhash`` are tested separately below because their usage will
59 // produce warnings that can't be handled in a generic way.
60 vector<string> pure{
61 "msg.data",
62 "msg.data[0]",
63 "msg.sig",
64 "msg",
65 "block",
66 "tx"
67 };
68 for (string const& x: view)
69 {
70 CHECK_ERROR(
71 "contract C { function f() pure public { " + x + "; } }",
72 TypeError,
73 "Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires \"view\""
74 );
75 }
76 for (string const& x: pure)
77 {
78 CHECK_WARNING(
79 "contract C { function f() view public { " + x + "; } }",
80 "Function state mutability can be restricted to pure"
81 );
82 }
83
84 CHECK_WARNING_ALLOW_MULTI(
85 "contract C { function f() view public { blockhash; } }",
86 (std::vector<std::string>{
87 "Function state mutability can be restricted to pure",
88 "Statement has no effect."
89 }));
90
91 CHECK_ERROR(
92 "contract C { function f() view public { block.blockhash; } }",
93 TypeError,
94 "\"block.blockhash()\" has been deprecated in favor of \"blockhash()\""
95 );
96 }
97
BOOST_AUTO_TEST_CASE(address_staticcall)98 BOOST_AUTO_TEST_CASE(address_staticcall)
99 {
100 string text = R"(
101 contract C {
102 function i() view public returns (bool) {
103 (bool success,) = address(0x4242).staticcall("");
104 return success;
105 }
106 }
107 )";
108 if (!solidity::test::CommonOptions::get().evmVersion().hasStaticCall())
109 CHECK_ERROR(text, TypeError, "\"staticcall\" is not supported by the VM version.");
110 else
111 CHECK_SUCCESS_NO_WARNINGS(text);
112 }
113
114
BOOST_AUTO_TEST_CASE(assembly_staticcall)115 BOOST_AUTO_TEST_CASE(assembly_staticcall)
116 {
117 string text = R"(
118 contract C {
119 function i() view public {
120 assembly { pop(staticcall(gas(), 1, 2, 3, 4, 5)) }
121 }
122 }
123 )";
124 if (solidity::test::CommonOptions::get().evmVersion().hasStaticCall())
125 CHECK_SUCCESS_NO_WARNINGS(text);
126 }
127
128 BOOST_AUTO_TEST_SUITE_END()
129
130 }
131