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>
|
|
|
|
|
2018-03-14 11:04:04 +00:00
|
|
|
#include <test/Options.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;
|
|
|
|
|
|
|
|
namespace dev
|
|
|
|
{
|
|
|
|
namespace solidity
|
|
|
|
{
|
|
|
|
namespace test
|
|
|
|
{
|
|
|
|
|
|
|
|
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)",
|
2018-03-02 16:58:27 +00:00
|
|
|
"gasleft()",
|
2017-08-29 13:32:20 +00:00
|
|
|
"msg.value",
|
|
|
|
"msg.sender",
|
|
|
|
"tx.origin",
|
|
|
|
"tx.gasprice",
|
|
|
|
"this",
|
|
|
|
"address(1).balance"
|
|
|
|
};
|
2018-03-07 09:48:10 +00:00
|
|
|
// ``block.blockhash`` and ``blockhash`` are tested seperately below because their usage will
|
|
|
|
// 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."
|
|
|
|
}));
|
|
|
|
|
2018-07-04 09:42:05 +00:00
|
|
|
CHECK_ERROR(
|
2018-03-07 09:48:10 +00:00
|
|
|
"contract C { function f() view public { block.blockhash; } }",
|
2018-07-04 09:42:05 +00:00
|
|
|
TypeError,
|
|
|
|
"\"block.blockhash()\" has been deprecated in favor of \"blockhash()\""
|
|
|
|
);
|
2017-08-29 13:32:20 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 11:13:35 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(assembly_staticcall)
|
|
|
|
{
|
|
|
|
string text = R"(
|
|
|
|
contract C {
|
2017-09-14 15:30:00 +00:00
|
|
|
function i() view public {
|
2017-10-16 11:28:44 +00:00
|
|
|
assembly { pop(staticcall(gas, 1, 2, 3, 4, 5)) }
|
2017-08-31 11:13:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)";
|
2018-02-26 18:53:38 +00:00
|
|
|
if (!dev::test::Options::get().evmVersion().hasStaticCall())
|
2018-02-23 18:29:42 +00:00
|
|
|
CHECK_WARNING(text, "\"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()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|