solidity/test/libsolidity/ViewPureChecker.cpp

133 lines
3.3 KiB
C++
Raw Normal View History

2017-08-29 13:32:20 +00:00
/*
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 <test/Common.h>
2018-02-23 18:29:20 +00:00
2017-08-29 13:32:20 +00:00
#include <boost/test/unit_test.hpp>
#include <string>
2018-03-07 09:48:10 +00:00
#include <tuple>
2017-08-29 13:32:20 +00:00
using namespace std;
using namespace solidity::langutil;
2017-08-29 13:32:20 +00:00
namespace solidity::frontend::test
2017-08-29 13:32:20 +00:00
{
BOOST_FIXTURE_TEST_SUITE(ViewPureChecker, AnalysisFramework)
BOOST_AUTO_TEST_CASE(environment_access)
{
vector<string> view{
"block.coinbase",
"block.timestamp",
"block.difficulty",
"block.number",
"block.gaslimit",
2018-03-07 09:48:10 +00:00
"blockhash(7)",
"gasleft()",
2017-08-29 13:32:20 +00:00
"msg.value",
"msg.sender",
"tx.origin",
"tx.gasprice",
"this",
"address(1).balance",
2017-08-29 13:32:20 +00:00
};
if (solidity::test::CommonOptions::get().evmVersion().hasStaticCall())
view.emplace_back("address(0x4242).staticcall(\"\")");
// ``block.blockhash`` and ``blockhash`` are tested separately below because their usage will
2018-03-07 09:48:10 +00:00
// produce warnings that can't be handled in a generic way.
2017-08-29 13:32:20 +00:00
vector<string> pure{
"msg.data",
"msg.data[0]",
"msg.sig",
"msg",
"block",
"tx"
};
for (string const& x: view)
{
CHECK_ERROR(
2018-03-07 09:48:10 +00:00
"contract C { function f() pure public { " + x + "; } }",
2017-08-29 13:32:20 +00:00
TypeError,
2017-08-31 14:14:54 +00:00
"Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires \"view\""
2017-08-29 13:32:20 +00:00
);
}
for (string const& x: pure)
{
2018-03-07 09:48:10 +00:00
CHECK_WARNING(
"contract C { function f() view public { " + x + "; } }",
"Function state mutability can be restricted to pure"
);
2017-08-29 13:32:20 +00:00
}
2018-03-07 09:48:10 +00:00
CHECK_WARNING_ALLOW_MULTI(
"contract C { function f() view public { blockhash; } }",
(std::vector<std::string>{
"Function state mutability can be restricted to pure",
"Statement has no effect."
}));
CHECK_ERROR(
2018-03-07 09:48:10 +00:00
"contract C { function f() view public { block.blockhash; } }",
TypeError,
"\"block.blockhash()\" has been deprecated in favor of \"blockhash()\""
);
2017-08-29 13:32:20 +00:00
}
BOOST_AUTO_TEST_CASE(address_staticcall)
{
string text = R"(
contract C {
function i() view public returns (bool) {
2018-08-15 21:30:09 +00:00
(bool success,) = address(0x4242).staticcall("");
return success;
}
}
)";
if (!solidity::test::CommonOptions::get().evmVersion().hasStaticCall())
CHECK_ERROR(text, TypeError, "\"staticcall\" is not supported by the VM version.");
else
CHECK_SUCCESS_NO_WARNINGS(text);
}
2017-08-31 11:13:35 +00:00
BOOST_AUTO_TEST_CASE(assembly_staticcall)
{
string text = R"(
contract C {
function i() view public {
assembly { pop(staticcall(gas(), 1, 2, 3, 4, 5)) }
2017-08-31 11:13:35 +00:00
}
}
)";
if (!solidity::test::CommonOptions::get().evmVersion().hasStaticCall())
2019-02-26 18:21:32 +00:00
CHECK_ERROR(text, TypeError, "\"staticcall\" instruction is only available for Byzantium-compatible");
else
CHECK_SUCCESS_NO_WARNINGS(text);
2017-08-31 11:13:35 +00:00
}
2017-08-29 13:32:20 +00:00
BOOST_AUTO_TEST_SUITE_END()
}