2017-05-24 08:50:22 +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/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @date 2017
|
|
|
|
* Unit tests for the metadata output.
|
|
|
|
*/
|
|
|
|
|
2018-03-14 11:04:04 +00:00
|
|
|
#include <test/Metadata.h>
|
|
|
|
#include <test/Options.h>
|
2017-05-24 08:50:22 +00:00
|
|
|
#include <libsolidity/interface/CompilerStack.h>
|
2018-04-19 08:32:46 +00:00
|
|
|
#include <libsolidity/interface/Version.h>
|
2017-05-24 08:50:22 +00:00
|
|
|
#include <libdevcore/SwarmHash.h>
|
2018-02-07 01:05:20 +00:00
|
|
|
#include <libdevcore/JSON.h>
|
2017-05-24 08:50:22 +00:00
|
|
|
|
2019-03-05 00:10:46 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2017-05-24 08:50:22 +00:00
|
|
|
namespace dev
|
|
|
|
{
|
|
|
|
namespace solidity
|
|
|
|
{
|
|
|
|
namespace test
|
|
|
|
{
|
|
|
|
|
2019-03-05 00:10:46 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
map<string, string> requireParsedCBORMetadata(bytes const& _bytecode)
|
|
|
|
{
|
|
|
|
bytes cborMetadata = dev::test::onlyMetadata(_bytecode);
|
|
|
|
BOOST_REQUIRE(!cborMetadata.empty());
|
2019-10-28 10:39:30 +00:00
|
|
|
std::optional<map<string, string>> tmp = dev::test::parseCBORMetadata(cborMetadata);
|
2019-03-05 00:10:46 +00:00
|
|
|
BOOST_REQUIRE(tmp);
|
|
|
|
return *tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-24 08:50:22 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE(Metadata)
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(metadata_stamp)
|
|
|
|
{
|
|
|
|
// Check that the metadata stamp is at the end of the runtime bytecode.
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
pragma solidity >=0.0;
|
2017-08-10 16:16:55 +00:00
|
|
|
pragma experimental __testOnlyAnalysis;
|
2017-05-24 08:50:22 +00:00
|
|
|
contract test {
|
2018-07-11 13:57:07 +00:00
|
|
|
function g(function(uint) external returns (uint) x) public {}
|
2017-05-24 08:50:22 +00:00
|
|
|
}
|
|
|
|
)";
|
2019-05-06 12:31:49 +00:00
|
|
|
for (auto release: std::set<bool>{true, VersionIsRelease})
|
|
|
|
{
|
|
|
|
CompilerStack compilerStack;
|
|
|
|
compilerStack.overwriteReleaseFlag(release);
|
|
|
|
compilerStack.setSources({{"", std::string(sourceCode)}});
|
|
|
|
compilerStack.setEVMVersion(dev::test::Options::get().evmVersion());
|
|
|
|
compilerStack.setOptimiserSettings(dev::test::Options::get().optimize);
|
|
|
|
BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed");
|
|
|
|
bytes const& bytecode = compilerStack.runtimeObject("test").bytecode;
|
|
|
|
std::string const& metadata = compilerStack.metadata("test");
|
|
|
|
BOOST_CHECK(dev::test::isValidMetadata(metadata));
|
2019-06-18 14:08:50 +00:00
|
|
|
bytes hash = dev::bzzr1Hash(metadata).asBytes();
|
2019-05-06 12:31:49 +00:00
|
|
|
BOOST_REQUIRE(hash.size() == 32);
|
|
|
|
auto const cborMetadata = requireParsedCBORMetadata(bytecode);
|
|
|
|
BOOST_CHECK(cborMetadata.size() == 2);
|
|
|
|
BOOST_CHECK(cborMetadata.count("solc") == 1);
|
|
|
|
if (release)
|
|
|
|
BOOST_CHECK(cborMetadata.at("solc") == toHex(VersionCompactBytes));
|
|
|
|
else
|
|
|
|
BOOST_CHECK(cborMetadata.at("solc") == VersionStringStrict);
|
2019-06-18 14:08:50 +00:00
|
|
|
BOOST_CHECK(cborMetadata.count("bzzr1") == 1);
|
|
|
|
BOOST_CHECK(cborMetadata.at("bzzr1") == toHex(hash));
|
2019-05-06 12:31:49 +00:00
|
|
|
}
|
2017-05-24 08:50:22 +00:00
|
|
|
}
|
|
|
|
|
2017-08-08 20:32:57 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(metadata_stamp_experimental)
|
|
|
|
{
|
|
|
|
// Check that the metadata stamp is at the end of the runtime bytecode.
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
pragma solidity >=0.0;
|
|
|
|
pragma experimental __test;
|
|
|
|
contract test {
|
2018-07-11 13:57:07 +00:00
|
|
|
function g(function(uint) external returns (uint) x) public {}
|
2017-08-08 20:32:57 +00:00
|
|
|
}
|
|
|
|
)";
|
2019-05-06 12:31:49 +00:00
|
|
|
for(auto release: std::set<bool>{true, VersionIsRelease})
|
|
|
|
{
|
|
|
|
CompilerStack compilerStack;
|
|
|
|
compilerStack.overwriteReleaseFlag(release);
|
|
|
|
compilerStack.setSources({{"", std::string(sourceCode)}});
|
|
|
|
compilerStack.setEVMVersion(dev::test::Options::get().evmVersion());
|
|
|
|
compilerStack.setOptimiserSettings(dev::test::Options::get().optimize);
|
|
|
|
BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed");
|
|
|
|
bytes const& bytecode = compilerStack.runtimeObject("test").bytecode;
|
|
|
|
std::string const& metadata = compilerStack.metadata("test");
|
|
|
|
BOOST_CHECK(dev::test::isValidMetadata(metadata));
|
2019-06-18 14:08:50 +00:00
|
|
|
bytes hash = dev::bzzr1Hash(metadata).asBytes();
|
2019-05-06 12:31:49 +00:00
|
|
|
BOOST_REQUIRE(hash.size() == 32);
|
|
|
|
auto const cborMetadata = requireParsedCBORMetadata(bytecode);
|
|
|
|
BOOST_CHECK(cborMetadata.size() == 3);
|
|
|
|
BOOST_CHECK(cborMetadata.count("solc") == 1);
|
|
|
|
if (release)
|
|
|
|
BOOST_CHECK(cborMetadata.at("solc") == toHex(VersionCompactBytes));
|
|
|
|
else
|
|
|
|
BOOST_CHECK(cborMetadata.at("solc") == VersionStringStrict);
|
2019-06-18 14:08:50 +00:00
|
|
|
BOOST_CHECK(cborMetadata.count("bzzr1") == 1);
|
|
|
|
BOOST_CHECK(cborMetadata.at("bzzr1") == toHex(hash));
|
2019-05-06 12:31:49 +00:00
|
|
|
BOOST_CHECK(cborMetadata.count("experimental") == 1);
|
|
|
|
BOOST_CHECK(cborMetadata.at("experimental") == "true");
|
|
|
|
}
|
2017-08-08 20:32:57 +00:00
|
|
|
}
|
|
|
|
|
2017-07-18 18:28:58 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(metadata_relevant_sources)
|
|
|
|
{
|
|
|
|
CompilerStack compilerStack;
|
2019-03-20 18:07:12 +00:00
|
|
|
char const* sourceCodeA = R"(
|
2017-07-18 18:28:58 +00:00
|
|
|
pragma solidity >=0.0;
|
|
|
|
contract A {
|
2018-07-11 13:57:07 +00:00
|
|
|
function g(function(uint) external returns (uint) x) public {}
|
2017-07-18 18:28:58 +00:00
|
|
|
}
|
|
|
|
)";
|
2019-03-20 18:07:12 +00:00
|
|
|
char const* sourceCodeB = R"(
|
2017-07-18 18:28:58 +00:00
|
|
|
pragma solidity >=0.0;
|
|
|
|
contract B {
|
2018-07-11 13:57:07 +00:00
|
|
|
function g(function(uint) external returns (uint) x) public {}
|
2017-07-18 18:28:58 +00:00
|
|
|
}
|
|
|
|
)";
|
2019-03-20 18:07:12 +00:00
|
|
|
compilerStack.setSources({
|
|
|
|
{"A", std::string(sourceCodeA)},
|
|
|
|
{"B", std::string(sourceCodeB)},
|
|
|
|
});
|
2018-02-23 18:29:20 +00:00
|
|
|
compilerStack.setEVMVersion(dev::test::Options::get().evmVersion());
|
2017-07-27 09:26:57 +00:00
|
|
|
compilerStack.setOptimiserSettings(dev::test::Options::get().optimize);
|
2017-09-20 09:52:41 +00:00
|
|
|
BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed");
|
2017-07-18 18:28:58 +00:00
|
|
|
|
|
|
|
std::string const& serialisedMetadata = compilerStack.metadata("A");
|
|
|
|
BOOST_CHECK(dev::test::isValidMetadata(serialisedMetadata));
|
|
|
|
Json::Value metadata;
|
2018-02-07 01:05:20 +00:00
|
|
|
BOOST_REQUIRE(jsonParseStrict(serialisedMetadata, metadata));
|
2017-07-18 18:28:58 +00:00
|
|
|
|
|
|
|
BOOST_CHECK_EQUAL(metadata["sources"].size(), 1);
|
|
|
|
BOOST_CHECK(metadata["sources"].isMember("A"));
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(metadata_relevant_sources_imports)
|
|
|
|
{
|
|
|
|
CompilerStack compilerStack;
|
2019-03-20 18:07:12 +00:00
|
|
|
char const* sourceCodeA = R"(
|
2017-07-18 18:28:58 +00:00
|
|
|
pragma solidity >=0.0;
|
|
|
|
contract A {
|
2018-07-11 13:57:07 +00:00
|
|
|
function g(function(uint) external returns (uint) x) public {}
|
2017-07-18 18:28:58 +00:00
|
|
|
}
|
|
|
|
)";
|
2019-03-20 18:07:12 +00:00
|
|
|
char const* sourceCodeB = R"(
|
2017-07-18 18:28:58 +00:00
|
|
|
pragma solidity >=0.0;
|
|
|
|
import "./A";
|
|
|
|
contract B is A {
|
2018-07-11 13:57:07 +00:00
|
|
|
function g(function(uint) external returns (uint) x) public {}
|
2017-07-18 18:28:58 +00:00
|
|
|
}
|
|
|
|
)";
|
2019-03-20 18:07:12 +00:00
|
|
|
char const* sourceCodeC = R"(
|
2017-07-18 18:28:58 +00:00
|
|
|
pragma solidity >=0.0;
|
|
|
|
import "./B";
|
|
|
|
contract C is B {
|
2018-07-11 13:57:07 +00:00
|
|
|
function g(function(uint) external returns (uint) x) public {}
|
2017-07-18 18:28:58 +00:00
|
|
|
}
|
|
|
|
)";
|
2019-03-20 18:07:12 +00:00
|
|
|
compilerStack.setSources({
|
|
|
|
{"A", std::string(sourceCodeA)},
|
|
|
|
{"B", std::string(sourceCodeB)},
|
|
|
|
{"C", std::string(sourceCodeC)}
|
|
|
|
});
|
2018-02-23 18:29:20 +00:00
|
|
|
compilerStack.setEVMVersion(dev::test::Options::get().evmVersion());
|
2017-07-27 09:26:57 +00:00
|
|
|
compilerStack.setOptimiserSettings(dev::test::Options::get().optimize);
|
2017-09-20 09:52:41 +00:00
|
|
|
BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed");
|
2017-07-18 18:28:58 +00:00
|
|
|
|
|
|
|
std::string const& serialisedMetadata = compilerStack.metadata("C");
|
|
|
|
BOOST_CHECK(dev::test::isValidMetadata(serialisedMetadata));
|
|
|
|
Json::Value metadata;
|
2018-02-07 01:05:20 +00:00
|
|
|
BOOST_REQUIRE(jsonParseStrict(serialisedMetadata, metadata));
|
2017-07-18 18:28:58 +00:00
|
|
|
|
|
|
|
BOOST_CHECK_EQUAL(metadata["sources"].size(), 3);
|
|
|
|
BOOST_CHECK(metadata["sources"].isMember("A"));
|
|
|
|
BOOST_CHECK(metadata["sources"].isMember("B"));
|
|
|
|
BOOST_CHECK(metadata["sources"].isMember("C"));
|
|
|
|
}
|
|
|
|
|
2019-04-16 16:10:50 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(metadata_useLiteralContent)
|
|
|
|
{
|
|
|
|
// Check that the metadata contains "useLiteralContent"
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
pragma solidity >=0.0;
|
|
|
|
contract test {
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
|
2019-04-23 19:53:24 +00:00
|
|
|
auto check = [](char const* _src, bool _literal)
|
2019-04-16 16:10:50 +00:00
|
|
|
{
|
|
|
|
CompilerStack compilerStack;
|
|
|
|
compilerStack.setSources({{"", std::string(_src)}});
|
|
|
|
compilerStack.setEVMVersion(dev::test::Options::get().evmVersion());
|
|
|
|
compilerStack.setOptimiserSettings(dev::test::Options::get().optimize);
|
2019-04-23 19:53:24 +00:00
|
|
|
compilerStack.useMetadataLiteralSources(_literal);
|
2019-04-16 16:10:50 +00:00
|
|
|
BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed");
|
|
|
|
string metadata_str = compilerStack.metadata("test");
|
|
|
|
Json::Value metadata;
|
|
|
|
jsonParse(metadata_str, metadata);
|
|
|
|
BOOST_CHECK(dev::test::isValidMetadata(metadata_str));
|
2019-04-23 19:53:24 +00:00
|
|
|
BOOST_CHECK(metadata.isMember("settings"));
|
|
|
|
if (_literal)
|
|
|
|
{
|
|
|
|
BOOST_CHECK(metadata["settings"].isMember("metadata"));
|
|
|
|
BOOST_CHECK(metadata["settings"]["metadata"].isMember("useLiteralContent"));
|
|
|
|
BOOST_CHECK(metadata["settings"]["metadata"]["useLiteralContent"].asBool());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
BOOST_CHECK(!metadata["settings"].isMember("metadata"));
|
|
|
|
}
|
2019-04-16 16:10:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
check(sourceCode, true);
|
|
|
|
check(sourceCode, false);
|
|
|
|
}
|
|
|
|
|
2017-05-24 08:50:22 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|