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>
|
2020-01-14 16:48:17 +00:00
|
|
|
#include <test/Common.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>
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/SwarmHash.h>
|
|
|
|
#include <libsolutil/IpfsHash.h>
|
|
|
|
#include <libsolutil/JSON.h>
|
2017-05-24 08:50:22 +00:00
|
|
|
|
2020-01-14 14:55:31 +00:00
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
2019-03-05 00:10:46 +00:00
|
|
|
|
2019-12-23 15:50:30 +00:00
|
|
|
namespace solidity::frontend::test
|
2017-05-24 08:50:22 +00:00
|
|
|
{
|
|
|
|
|
2019-03-05 00:10:46 +00:00
|
|
|
namespace
|
|
|
|
{
|
2021-01-15 14:58:32 +00:00
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
std::map<std::string, std::string> requireParsedCBORMetadata(bytes const& _bytecode, CompilerStack::MetadataFormat _metadataFormat)
|
2019-03-05 00:10:46 +00:00
|
|
|
{
|
2019-12-23 15:50:30 +00:00
|
|
|
bytes cborMetadata = solidity::test::onlyMetadata(_bytecode);
|
2021-02-11 08:47:03 +00:00
|
|
|
if (_metadataFormat != CompilerStack::MetadataFormat::NoMetadata)
|
|
|
|
{
|
|
|
|
BOOST_REQUIRE(!cborMetadata.empty());
|
2023-08-30 00:08:06 +00:00
|
|
|
std::optional<std::map<std::string, std::string>> tmp = solidity::test::parseCBORMetadata(cborMetadata);
|
2021-02-11 08:47:03 +00:00
|
|
|
BOOST_REQUIRE(tmp);
|
|
|
|
return *tmp;
|
|
|
|
}
|
|
|
|
BOOST_REQUIRE(cborMetadata.empty());
|
|
|
|
return {};
|
2019-03-05 00:10:46 +00:00
|
|
|
}
|
2021-01-15 14:58:32 +00:00
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
std::optional<std::string> compileAndCheckLicenseMetadata(std::string const& _contractName, char const* _sourceCode)
|
2021-01-15 14:58:32 +00:00
|
|
|
{
|
|
|
|
CompilerStack compilerStack;
|
2022-09-14 07:12:03 +00:00
|
|
|
compilerStack.setSources({{"A.sol", _sourceCode}});
|
2021-01-15 14:58:32 +00:00
|
|
|
BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed");
|
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
std::string const& serialisedMetadata = compilerStack.metadata(_contractName);
|
2021-01-15 14:58:32 +00:00
|
|
|
Json::Value metadata;
|
|
|
|
BOOST_REQUIRE(util::jsonParseStrict(serialisedMetadata, metadata));
|
2021-09-10 13:50:37 +00:00
|
|
|
BOOST_CHECK(solidity::test::isValidMetadata(metadata));
|
2021-01-15 14:58:32 +00:00
|
|
|
|
|
|
|
BOOST_CHECK_EQUAL(metadata["sources"].size(), 1);
|
|
|
|
BOOST_REQUIRE(metadata["sources"].isMember("A.sol"));
|
|
|
|
|
|
|
|
if (metadata["sources"]["A.sol"].isMember("license"))
|
|
|
|
{
|
|
|
|
BOOST_REQUIRE(metadata["sources"]["A.sol"]["license"].isString());
|
|
|
|
return metadata["sources"]["A.sol"]["license"].asString();
|
|
|
|
}
|
|
|
|
else
|
2023-08-30 00:08:06 +00:00
|
|
|
return std::nullopt;
|
2021-01-15 14:58:32 +00:00
|
|
|
}
|
|
|
|
|
2019-03-05 00:10:46 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
)";
|
2021-02-11 08:47:03 +00:00
|
|
|
for (auto metadataFormat: std::set<CompilerStack::MetadataFormat>{
|
|
|
|
CompilerStack::MetadataFormat::NoMetadata,
|
|
|
|
CompilerStack::MetadataFormat::WithReleaseVersionTag,
|
|
|
|
CompilerStack::MetadataFormat::WithPrereleaseVersionTag
|
|
|
|
})
|
2023-08-30 00:08:06 +00:00
|
|
|
for (auto metadataHash: std::set<CompilerStack::MetadataHash>{
|
2019-09-06 17:11:07 +00:00
|
|
|
CompilerStack::MetadataHash::IPFS,
|
|
|
|
CompilerStack::MetadataHash::Bzzr1,
|
|
|
|
CompilerStack::MetadataHash::None
|
|
|
|
})
|
|
|
|
{
|
|
|
|
CompilerStack compilerStack;
|
2021-02-11 08:47:03 +00:00
|
|
|
compilerStack.setMetadataFormat(metadataFormat);
|
2022-09-14 07:12:03 +00:00
|
|
|
compilerStack.setSources({{"", sourceCode}});
|
2020-01-14 16:48:17 +00:00
|
|
|
compilerStack.setEVMVersion(solidity::test::CommonOptions::get().evmVersion());
|
|
|
|
compilerStack.setOptimiserSettings(solidity::test::CommonOptions::get().optimize);
|
2019-09-06 17:11:07 +00:00
|
|
|
compilerStack.setMetadataHash(metadataHash);
|
|
|
|
BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed");
|
|
|
|
bytes const& bytecode = compilerStack.runtimeObject("test").bytecode;
|
2023-08-30 00:08:06 +00:00
|
|
|
std::string const& metadata = compilerStack.metadata("test");
|
2019-12-23 15:50:30 +00:00
|
|
|
BOOST_CHECK(solidity::test::isValidMetadata(metadata));
|
2019-09-06 17:11:07 +00:00
|
|
|
|
2021-02-11 08:47:03 +00:00
|
|
|
auto const cborMetadata = requireParsedCBORMetadata(bytecode, metadataFormat);
|
2019-09-06 17:11:07 +00:00
|
|
|
if (metadataHash == CompilerStack::MetadataHash::None)
|
2021-02-11 08:47:03 +00:00
|
|
|
BOOST_CHECK(cborMetadata.size() == (metadataFormat == CompilerStack::MetadataFormat::NoMetadata ? 0 : 1));
|
2019-09-06 17:11:07 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
bytes hash;
|
2023-08-30 00:08:06 +00:00
|
|
|
std::string hashMethod;
|
2019-09-06 17:11:07 +00:00
|
|
|
if (metadataHash == CompilerStack::MetadataHash::IPFS)
|
|
|
|
{
|
2019-12-23 15:50:30 +00:00
|
|
|
hash = util::ipfsHash(metadata);
|
2019-09-06 17:11:07 +00:00
|
|
|
BOOST_REQUIRE(hash.size() == 34);
|
|
|
|
hashMethod = "ipfs";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-12-23 15:50:30 +00:00
|
|
|
hash = util::bzzr1Hash(metadata).asBytes();
|
2019-09-06 17:11:07 +00:00
|
|
|
BOOST_REQUIRE(hash.size() == 32);
|
|
|
|
hashMethod = "bzzr1";
|
|
|
|
}
|
|
|
|
|
2021-02-11 08:47:03 +00:00
|
|
|
if (metadataFormat != CompilerStack::MetadataFormat::NoMetadata)
|
|
|
|
{
|
|
|
|
BOOST_CHECK(cborMetadata.size() == 2);
|
|
|
|
BOOST_CHECK(cborMetadata.count(hashMethod) == 1);
|
|
|
|
BOOST_CHECK(cborMetadata.at(hashMethod) == util::toHex(hash));
|
|
|
|
}
|
2019-09-06 17:11:07 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 08:47:03 +00:00
|
|
|
if (metadataFormat == CompilerStack::MetadataFormat::NoMetadata)
|
|
|
|
BOOST_CHECK(cborMetadata.count("solc") == 0);
|
2019-09-06 17:11:07 +00:00
|
|
|
else
|
2021-02-11 08:47:03 +00:00
|
|
|
{
|
|
|
|
BOOST_CHECK(cborMetadata.count("solc") == 1);
|
|
|
|
if (metadataFormat == CompilerStack::MetadataFormat::WithReleaseVersionTag)
|
|
|
|
BOOST_CHECK(cborMetadata.at("solc") == util::toHex(VersionCompactBytes));
|
|
|
|
else
|
|
|
|
BOOST_CHECK(cborMetadata.at("solc") == VersionStringStrict);
|
|
|
|
}
|
2019-09-06 17:11:07 +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
|
|
|
}
|
|
|
|
)";
|
2021-02-11 08:47:03 +00:00
|
|
|
for (auto metadataFormat: std::set<CompilerStack::MetadataFormat>{
|
|
|
|
CompilerStack::MetadataFormat::NoMetadata,
|
|
|
|
CompilerStack::MetadataFormat::WithReleaseVersionTag,
|
|
|
|
CompilerStack::MetadataFormat::WithPrereleaseVersionTag
|
|
|
|
})
|
2023-08-30 00:08:06 +00:00
|
|
|
for (auto metadataHash: std::set<CompilerStack::MetadataHash>{
|
2019-09-06 17:11:07 +00:00
|
|
|
CompilerStack::MetadataHash::IPFS,
|
|
|
|
CompilerStack::MetadataHash::Bzzr1,
|
|
|
|
CompilerStack::MetadataHash::None
|
|
|
|
})
|
|
|
|
{
|
|
|
|
CompilerStack compilerStack;
|
2021-02-11 08:47:03 +00:00
|
|
|
compilerStack.setMetadataFormat(metadataFormat);
|
2022-09-14 07:12:03 +00:00
|
|
|
compilerStack.setSources({{"", sourceCode}});
|
2020-01-14 16:48:17 +00:00
|
|
|
compilerStack.setEVMVersion(solidity::test::CommonOptions::get().evmVersion());
|
|
|
|
compilerStack.setOptimiserSettings(solidity::test::CommonOptions::get().optimize);
|
2019-09-06 17:11:07 +00:00
|
|
|
compilerStack.setMetadataHash(metadataHash);
|
|
|
|
BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed");
|
|
|
|
bytes const& bytecode = compilerStack.runtimeObject("test").bytecode;
|
2023-08-30 00:08:06 +00:00
|
|
|
std::string const& metadata = compilerStack.metadata("test");
|
2019-12-23 15:50:30 +00:00
|
|
|
BOOST_CHECK(solidity::test::isValidMetadata(metadata));
|
2019-09-06 17:11:07 +00:00
|
|
|
|
2021-02-11 08:47:03 +00:00
|
|
|
auto const cborMetadata = requireParsedCBORMetadata(bytecode, metadataFormat);
|
2019-09-06 17:11:07 +00:00
|
|
|
if (metadataHash == CompilerStack::MetadataHash::None)
|
2021-02-11 08:47:03 +00:00
|
|
|
BOOST_CHECK(cborMetadata.size() == (metadataFormat == CompilerStack::MetadataFormat::NoMetadata ? 0 : 2));
|
2019-09-06 17:11:07 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
bytes hash;
|
2023-08-30 00:08:06 +00:00
|
|
|
std::string hashMethod;
|
2019-09-06 17:11:07 +00:00
|
|
|
if (metadataHash == CompilerStack::MetadataHash::IPFS)
|
|
|
|
{
|
2019-12-23 15:50:30 +00:00
|
|
|
hash = util::ipfsHash(metadata);
|
2019-09-06 17:11:07 +00:00
|
|
|
BOOST_REQUIRE(hash.size() == 34);
|
|
|
|
hashMethod = "ipfs";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-12-23 15:50:30 +00:00
|
|
|
hash = util::bzzr1Hash(metadata).asBytes();
|
2019-09-06 17:11:07 +00:00
|
|
|
BOOST_REQUIRE(hash.size() == 32);
|
|
|
|
hashMethod = "bzzr1";
|
|
|
|
}
|
|
|
|
|
2021-02-11 08:47:03 +00:00
|
|
|
if (metadataFormat != CompilerStack::MetadataFormat::NoMetadata)
|
|
|
|
{
|
|
|
|
BOOST_CHECK(cborMetadata.size() == 3);
|
|
|
|
BOOST_CHECK(cborMetadata.count(hashMethod) == 1);
|
|
|
|
BOOST_CHECK(cborMetadata.at(hashMethod) == util::toHex(hash));
|
|
|
|
}
|
2019-09-06 17:11:07 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 08:47:03 +00:00
|
|
|
if (metadataFormat == CompilerStack::MetadataFormat::NoMetadata)
|
|
|
|
BOOST_CHECK(cborMetadata.count("solc") == 0);
|
2019-09-06 17:11:07 +00:00
|
|
|
else
|
2021-02-11 08:47:03 +00:00
|
|
|
{
|
|
|
|
BOOST_CHECK(cborMetadata.count("solc") == 1);
|
|
|
|
if (metadataFormat == CompilerStack::MetadataFormat::WithReleaseVersionTag)
|
|
|
|
BOOST_CHECK(cborMetadata.at("solc") == util::toHex(VersionCompactBytes));
|
|
|
|
else
|
|
|
|
BOOST_CHECK(cborMetadata.at("solc") == VersionStringStrict);
|
|
|
|
BOOST_CHECK(cborMetadata.count("experimental") == 1);
|
|
|
|
BOOST_CHECK(cborMetadata.at("experimental") == "true");
|
|
|
|
}
|
2019-09-06 17:11:07 +00:00
|
|
|
}
|
2017-08-08 20:32:57 +00:00
|
|
|
}
|
|
|
|
|
2022-11-22 12:05:20 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(metadata_eof_experimental)
|
|
|
|
{
|
|
|
|
// Check that setting an EOF version results in the experimental flag being set.
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
pragma solidity >=0.0;
|
|
|
|
contract test {
|
|
|
|
function g(function(uint) external returns (uint) x) public {}
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
for (auto metadataFormat: std::set<CompilerStack::MetadataFormat>{
|
|
|
|
CompilerStack::MetadataFormat::NoMetadata,
|
|
|
|
CompilerStack::MetadataFormat::WithReleaseVersionTag,
|
|
|
|
CompilerStack::MetadataFormat::WithPrereleaseVersionTag
|
|
|
|
})
|
|
|
|
{
|
|
|
|
CompilerStack compilerStack;
|
|
|
|
compilerStack.setMetadataFormat(metadataFormat);
|
|
|
|
compilerStack.setSources({{"", sourceCode}});
|
|
|
|
compilerStack.setEVMVersion({});
|
|
|
|
compilerStack.setViaIR(true);
|
|
|
|
compilerStack.setEOFVersion(1);
|
|
|
|
compilerStack.setOptimiserSettings(true);
|
|
|
|
BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed");
|
|
|
|
bytes const& bytecode = compilerStack.runtimeObject("test").bytecode;
|
2023-08-30 00:08:06 +00:00
|
|
|
std::string const& metadata = compilerStack.metadata("test");
|
2022-11-22 12:05:20 +00:00
|
|
|
BOOST_CHECK(solidity::test::isValidMetadata(metadata));
|
|
|
|
|
|
|
|
auto const cborMetadata = requireParsedCBORMetadata(bytecode, metadataFormat);
|
|
|
|
|
|
|
|
if (metadataFormat == CompilerStack::MetadataFormat::NoMetadata)
|
|
|
|
BOOST_CHECK(cborMetadata.count("experimental") == 0);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
BOOST_CHECK(cborMetadata.count("experimental") == 1);
|
|
|
|
BOOST_CHECK(cborMetadata.at("experimental") == "true");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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({
|
2022-09-14 07:12:03 +00:00
|
|
|
{"A", sourceCodeA},
|
|
|
|
{"B", sourceCodeB},
|
2019-03-20 18:07:12 +00:00
|
|
|
});
|
2020-01-14 16:48:17 +00:00
|
|
|
compilerStack.setEVMVersion(solidity::test::CommonOptions::get().evmVersion());
|
|
|
|
compilerStack.setOptimiserSettings(solidity::test::CommonOptions::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
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
std::string const& serialisedMetadata = compilerStack.metadata("A");
|
2017-07-18 18:28:58 +00:00
|
|
|
Json::Value metadata;
|
2019-12-23 15:50:30 +00:00
|
|
|
BOOST_REQUIRE(util::jsonParseStrict(serialisedMetadata, metadata));
|
2021-09-10 13:50:37 +00:00
|
|
|
BOOST_CHECK(solidity::test::isValidMetadata(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 {
|
2019-11-05 17:25:34 +00:00
|
|
|
function g(function(uint) external returns (uint) x) public virtual {}
|
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 {
|
2019-11-05 17:25:34 +00:00
|
|
|
function g(function(uint) external returns (uint) x) public virtual override {}
|
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 {
|
2019-09-16 12:33:43 +00:00
|
|
|
function g(function(uint) external returns (uint) x) public override {}
|
2017-07-18 18:28:58 +00:00
|
|
|
}
|
|
|
|
)";
|
2019-03-20 18:07:12 +00:00
|
|
|
compilerStack.setSources({
|
2022-09-14 07:12:03 +00:00
|
|
|
{"A", sourceCodeA},
|
|
|
|
{"B", sourceCodeB},
|
|
|
|
{"C", sourceCodeC}
|
2019-03-20 18:07:12 +00:00
|
|
|
});
|
2020-01-14 16:48:17 +00:00
|
|
|
compilerStack.setEVMVersion(solidity::test::CommonOptions::get().evmVersion());
|
|
|
|
compilerStack.setOptimiserSettings(solidity::test::CommonOptions::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
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
std::string const& serialisedMetadata = compilerStack.metadata("C");
|
2017-07-18 18:28:58 +00:00
|
|
|
Json::Value metadata;
|
2019-12-23 15:50:30 +00:00
|
|
|
BOOST_REQUIRE(util::jsonParseStrict(serialisedMetadata, metadata));
|
2021-09-10 13:50:37 +00:00
|
|
|
BOOST_CHECK(solidity::test::isValidMetadata(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;
|
2022-09-14 07:12:03 +00:00
|
|
|
compilerStack.setSources({{"", _src}});
|
2020-01-14 16:48:17 +00:00
|
|
|
compilerStack.setEVMVersion(solidity::test::CommonOptions::get().evmVersion());
|
|
|
|
compilerStack.setOptimiserSettings(solidity::test::CommonOptions::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");
|
2023-08-30 00:08:06 +00:00
|
|
|
std::string metadata_str = compilerStack.metadata("test");
|
2019-04-16 16:10:50 +00:00
|
|
|
Json::Value metadata;
|
2021-09-10 13:50:37 +00:00
|
|
|
BOOST_REQUIRE(util::jsonParseStrict(metadata_str, metadata));
|
|
|
|
BOOST_CHECK(solidity::test::isValidMetadata(metadata));
|
2019-04-23 19:53:24 +00:00
|
|
|
BOOST_CHECK(metadata.isMember("settings"));
|
2019-09-06 17:11:07 +00:00
|
|
|
BOOST_CHECK(metadata["settings"].isMember("metadata"));
|
|
|
|
BOOST_CHECK(metadata["settings"]["metadata"].isMember("bytecodeHash"));
|
2019-04-23 19:53:24 +00:00
|
|
|
if (_literal)
|
|
|
|
{
|
|
|
|
BOOST_CHECK(metadata["settings"]["metadata"].isMember("useLiteralContent"));
|
|
|
|
BOOST_CHECK(metadata["settings"]["metadata"]["useLiteralContent"].asBool());
|
|
|
|
}
|
2019-04-16 16:10:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
check(sourceCode, true);
|
|
|
|
check(sourceCode, false);
|
|
|
|
}
|
|
|
|
|
2020-10-28 18:59:19 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(metadata_viair)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
pragma solidity >=0.0;
|
|
|
|
contract test {
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
|
2021-09-10 13:54:32 +00:00
|
|
|
auto check = [](char const* _src, bool _viaIR)
|
2020-10-28 18:59:19 +00:00
|
|
|
{
|
|
|
|
CompilerStack compilerStack;
|
2022-09-14 07:12:03 +00:00
|
|
|
compilerStack.setSources({{"", _src}});
|
2020-10-28 18:59:19 +00:00
|
|
|
compilerStack.setEVMVersion(solidity::test::CommonOptions::get().evmVersion());
|
|
|
|
compilerStack.setOptimiserSettings(solidity::test::CommonOptions::get().optimize);
|
2021-09-10 13:54:32 +00:00
|
|
|
compilerStack.setViaIR(_viaIR);
|
2020-10-28 18:59:19 +00:00
|
|
|
BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed");
|
2021-09-10 13:54:32 +00:00
|
|
|
|
2020-10-28 18:59:19 +00:00
|
|
|
Json::Value metadata;
|
2021-09-10 13:54:32 +00:00
|
|
|
BOOST_REQUIRE(util::jsonParseStrict(compilerStack.metadata("test"), metadata));
|
2021-09-10 13:50:37 +00:00
|
|
|
BOOST_CHECK(solidity::test::isValidMetadata(metadata));
|
2020-10-28 18:59:19 +00:00
|
|
|
BOOST_CHECK(metadata.isMember("settings"));
|
2021-09-10 13:54:32 +00:00
|
|
|
if (_viaIR)
|
2020-10-28 18:59:19 +00:00
|
|
|
{
|
|
|
|
BOOST_CHECK(metadata["settings"].isMember("viaIR"));
|
|
|
|
BOOST_CHECK(metadata["settings"]["viaIR"].asBool());
|
|
|
|
}
|
2021-09-10 13:54:32 +00:00
|
|
|
else
|
|
|
|
BOOST_CHECK(!metadata["settings"].isMember("viaIR"));
|
|
|
|
|
|
|
|
BOOST_CHECK(compilerStack.cborMetadata("test") == compilerStack.cborMetadata("test", _viaIR));
|
|
|
|
BOOST_CHECK(compilerStack.cborMetadata("test") != compilerStack.cborMetadata("test", !_viaIR));
|
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
std::map<std::string, std::string> const parsedCBORMetadata = requireParsedCBORMetadata(
|
2021-09-10 13:54:32 +00:00
|
|
|
compilerStack.runtimeObject("test").bytecode,
|
|
|
|
CompilerStack::MetadataFormat::WithReleaseVersionTag
|
|
|
|
);
|
|
|
|
|
2022-03-09 15:02:31 +00:00
|
|
|
BOOST_CHECK(parsedCBORMetadata.count("experimental") == 0);
|
2020-10-28 18:59:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
check(sourceCode, true);
|
|
|
|
check(sourceCode, false);
|
|
|
|
}
|
|
|
|
|
2019-09-18 14:44:36 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(metadata_revert_strings)
|
|
|
|
{
|
|
|
|
CompilerStack compilerStack;
|
|
|
|
char const* sourceCodeA = R"(
|
|
|
|
pragma solidity >=0.0;
|
|
|
|
contract A {
|
|
|
|
}
|
|
|
|
)";
|
2022-09-14 07:12:03 +00:00
|
|
|
compilerStack.setSources({{"A", sourceCodeA}});
|
2019-09-18 14:44:36 +00:00
|
|
|
compilerStack.setRevertStringBehaviour(RevertStrings::Strip);
|
|
|
|
BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed");
|
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
std::string const& serialisedMetadata = compilerStack.metadata("A");
|
2019-09-18 14:44:36 +00:00
|
|
|
Json::Value metadata;
|
2019-12-23 15:50:30 +00:00
|
|
|
BOOST_REQUIRE(util::jsonParseStrict(serialisedMetadata, metadata));
|
2021-09-10 13:50:37 +00:00
|
|
|
BOOST_CHECK(solidity::test::isValidMetadata(metadata));
|
2019-09-18 14:44:36 +00:00
|
|
|
|
|
|
|
BOOST_CHECK_EQUAL(metadata["settings"]["debug"]["revertStrings"], "strip");
|
|
|
|
}
|
|
|
|
|
2022-09-05 07:52:17 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(metadata_optimiser_sequence)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
pragma solidity >=0.0;
|
2022-09-12 08:57:02 +00:00
|
|
|
contract C {
|
2022-09-05 07:52:17 +00:00
|
|
|
}
|
|
|
|
)";
|
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
std::vector<std::tuple<std::string, std::string>> sequences =
|
2022-09-05 07:52:17 +00:00
|
|
|
{
|
2022-09-12 08:57:02 +00:00
|
|
|
// {"<optimizer sequence>", "<optimizer cleanup sequence>"}
|
|
|
|
{"", ""},
|
|
|
|
{"", "fDn"},
|
|
|
|
{"dhfoDgvulfnTUtnIf", "" },
|
|
|
|
{"dhfoDgvulfnTUtnIf", "fDn"}
|
2022-09-05 07:52:17 +00:00
|
|
|
};
|
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
auto check = [sourceCode](std::string const& _optimizerSequence, std::string const& _optimizerCleanupSequence)
|
2022-09-05 07:52:17 +00:00
|
|
|
{
|
|
|
|
OptimiserSettings optimizerSettings = OptimiserSettings::minimal();
|
|
|
|
optimizerSettings.runYulOptimiser = true;
|
2022-09-12 08:57:02 +00:00
|
|
|
optimizerSettings.yulOptimiserSteps = _optimizerSequence;
|
|
|
|
optimizerSettings.yulOptimiserCleanupSteps = _optimizerCleanupSequence;
|
2022-09-05 07:52:17 +00:00
|
|
|
CompilerStack compilerStack;
|
2022-09-14 07:12:03 +00:00
|
|
|
compilerStack.setSources({{"", sourceCode}});
|
2022-09-05 07:52:17 +00:00
|
|
|
compilerStack.setEVMVersion(solidity::test::CommonOptions::get().evmVersion());
|
|
|
|
compilerStack.setOptimiserSettings(optimizerSettings);
|
|
|
|
|
|
|
|
BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed");
|
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
std::string const& serialisedMetadata = compilerStack.metadata("C");
|
2022-09-05 07:52:17 +00:00
|
|
|
Json::Value metadata;
|
|
|
|
BOOST_REQUIRE(util::jsonParseStrict(serialisedMetadata, metadata));
|
|
|
|
BOOST_CHECK(solidity::test::isValidMetadata(metadata));
|
|
|
|
BOOST_CHECK(metadata["settings"]["optimizer"].isMember("details"));
|
|
|
|
BOOST_CHECK(metadata["settings"]["optimizer"]["details"].isMember("yulDetails"));
|
|
|
|
BOOST_CHECK(metadata["settings"]["optimizer"]["details"]["yulDetails"].isMember("optimizerSteps"));
|
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
std::string const metadataOptimizerSteps = metadata["settings"]["optimizer"]["details"]["yulDetails"]["optimizerSteps"].asString();
|
|
|
|
std::string const expectedMetadataOptimiserSteps = _optimizerSequence + ":" + _optimizerCleanupSequence;
|
2022-09-05 07:52:17 +00:00
|
|
|
BOOST_CHECK_EQUAL(metadataOptimizerSteps, expectedMetadataOptimiserSteps);
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto const& [sequence, cleanupSequence] : sequences)
|
|
|
|
check(sequence, cleanupSequence);
|
|
|
|
}
|
|
|
|
|
2020-12-18 15:04:14 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(metadata_license_missing)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
pragma solidity >=0.0;
|
|
|
|
contract C {
|
|
|
|
}
|
|
|
|
)";
|
2023-08-30 00:08:06 +00:00
|
|
|
BOOST_CHECK(compileAndCheckLicenseMetadata("C", sourceCode) == std::nullopt);
|
2020-12-18 15:04:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(metadata_license_gpl3)
|
|
|
|
{
|
|
|
|
// Can't use a raw string here due to the stylechecker.
|
|
|
|
char const* sourceCode =
|
|
|
|
"// NOTE: we also add trailing whitespace after the license, to see it is trimmed.\n"
|
|
|
|
"// SPDX-License-Identifier: GPL-3.0 \n"
|
|
|
|
"pragma solidity >=0.0;\n"
|
|
|
|
"contract C {\n"
|
|
|
|
"}\n";
|
|
|
|
BOOST_CHECK(compileAndCheckLicenseMetadata("C", sourceCode) == "GPL-3.0");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(metadata_license_whitespace_before_spdx)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
|
|
|
contract C {}
|
|
|
|
)";
|
|
|
|
BOOST_CHECK(compileAndCheckLicenseMetadata("C", sourceCode) == "GPL-3.0");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(metadata_license_whitespace_after_colon)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
|
|
|
contract C {}
|
|
|
|
)";
|
|
|
|
BOOST_CHECK(compileAndCheckLicenseMetadata("C", sourceCode) == "GPL-3.0");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(metadata_license_gpl3_or_apache2)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
// SPDX-License-Identifier: GPL-3.0 OR Apache-2.0
|
|
|
|
pragma solidity >=0.0;
|
|
|
|
contract C {
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
BOOST_CHECK(compileAndCheckLicenseMetadata("C", sourceCode) == "GPL-3.0 OR Apache-2.0");
|
|
|
|
}
|
|
|
|
|
2021-01-12 16:07:23 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(metadata_license_bidi_marks)
|
|
|
|
{
|
|
|
|
char const* sourceCode =
|
|
|
|
"// \xE2\x80\xAE""0.3-LPG :reifitnedI-esneciL-XDPS\xE2\x80\xAC\n"
|
|
|
|
"// NOTE: The text above is reversed using Unicode directional marks. In raw form it would look like this:\n"
|
|
|
|
"// <LRO>0.3-LPG :reifitnedI-esneciL-XDPS<PDF>\n"
|
|
|
|
"contract C {}\n";
|
2023-08-30 00:08:06 +00:00
|
|
|
BOOST_CHECK(compileAndCheckLicenseMetadata("C", sourceCode) == std::nullopt);
|
2021-01-12 16:07:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(metadata_license_bottom)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract C {}
|
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
|
|
|
)";
|
|
|
|
BOOST_CHECK(compileAndCheckLicenseMetadata("C", sourceCode) == "GPL-3.0");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(metadata_cr_endings)
|
|
|
|
{
|
|
|
|
char const* sourceCode =
|
|
|
|
"// SPDX-License-Identifier: GPL-3.0\r"
|
|
|
|
"contract C {}\r";
|
|
|
|
BOOST_CHECK(compileAndCheckLicenseMetadata("C", sourceCode) == "GPL-3.0");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(metadata_crlf_endings)
|
|
|
|
{
|
|
|
|
char const* sourceCode =
|
|
|
|
"// SPDX-License-Identifier: GPL-3.0\r\n"
|
|
|
|
"contract C {}\r\n";
|
|
|
|
BOOST_CHECK(compileAndCheckLicenseMetadata("C", sourceCode) == "GPL-3.0");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(metadata_license_in_string)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract C {
|
|
|
|
bytes license = "// SPDX-License-Identifier: GPL-3.0";
|
|
|
|
}
|
|
|
|
)";
|
2023-08-30 00:08:06 +00:00
|
|
|
BOOST_CHECK(compileAndCheckLicenseMetadata("C", sourceCode) == std::nullopt);
|
2021-01-12 16:07:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(metadata_license_in_contract)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract C {
|
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
|
|
|
}
|
|
|
|
)";
|
2023-08-30 00:08:06 +00:00
|
|
|
BOOST_CHECK(compileAndCheckLicenseMetadata("C", sourceCode) == std::nullopt);
|
2021-01-12 16:07:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(metadata_license_missing_colon)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
// SPDX-License-Identifier GPL-3.0
|
|
|
|
contract C {}
|
|
|
|
)";
|
2023-08-30 00:08:06 +00:00
|
|
|
BOOST_CHECK(compileAndCheckLicenseMetadata("C", sourceCode) == std::nullopt);
|
2021-01-12 16:07:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(metadata_license_multiline)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
/* SPDX-License-Identifier: GPL-3.0 */
|
|
|
|
contract C {}
|
|
|
|
)";
|
|
|
|
BOOST_CHECK(compileAndCheckLicenseMetadata("C", sourceCode) == "GPL-3.0");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(metadata_license_natspec)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
/// SPDX-License-Identifier: GPL-3.0
|
|
|
|
contract C {}
|
|
|
|
)";
|
|
|
|
BOOST_CHECK(compileAndCheckLicenseMetadata("C", sourceCode) == "GPL-3.0");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(metadata_license_natspec_multiline)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
/** SPDX-License-Identifier: GPL-3.0 */
|
|
|
|
contract C {}
|
|
|
|
)";
|
|
|
|
BOOST_CHECK(compileAndCheckLicenseMetadata("C", sourceCode) == "GPL-3.0");
|
|
|
|
}
|
|
|
|
|
2021-09-02 13:25:10 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(metadata_license_no_whitespace_multiline)
|
2021-01-12 16:07:23 +00:00
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
2021-09-02 13:25:10 +00:00
|
|
|
/*SPDX-License-Identifier:GPL-3.0*/
|
2021-01-12 16:07:23 +00:00
|
|
|
contract C {}
|
|
|
|
)";
|
|
|
|
BOOST_CHECK(compileAndCheckLicenseMetadata("C", sourceCode) == "GPL-3.0");
|
|
|
|
}
|
|
|
|
|
2021-09-02 13:25:10 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(metadata_license_nonempty_line)
|
2021-01-12 16:07:23 +00:00
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
2021-09-02 13:25:10 +00:00
|
|
|
pragma solidity >= 0.0; // SPDX-License-Identifier: GPL-3.0
|
2021-01-12 16:07:23 +00:00
|
|
|
contract C {}
|
|
|
|
)";
|
|
|
|
BOOST_CHECK(compileAndCheckLicenseMetadata("C", sourceCode) == "GPL-3.0");
|
|
|
|
}
|
|
|
|
|
2021-09-02 13:25:10 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(metadata_license_no_whitespace)
|
2021-01-12 16:07:23 +00:00
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
2021-09-02 13:25:10 +00:00
|
|
|
//SPDX-License-Identifier:GPL-3.0
|
2021-01-12 16:07:23 +00:00
|
|
|
contract C {}
|
|
|
|
)";
|
|
|
|
BOOST_CHECK(compileAndCheckLicenseMetadata("C", sourceCode) == "GPL-3.0");
|
|
|
|
}
|
|
|
|
|
2017-05-24 08:50:22 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|
|
|
|
|
|
|
|
}
|