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.
|
|
|
|
*/
|
|
|
|
|
2017-05-24 08:54:25 +00:00
|
|
|
#include "../Metadata.h"
|
2017-05-24 08:50:22 +00:00
|
|
|
#include "../TestHelper.h"
|
|
|
|
#include <libsolidity/interface/CompilerStack.h>
|
|
|
|
#include <libdevcore/SwarmHash.h>
|
|
|
|
|
|
|
|
namespace dev
|
|
|
|
{
|
|
|
|
namespace solidity
|
|
|
|
{
|
|
|
|
namespace test
|
|
|
|
{
|
|
|
|
|
|
|
|
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;
|
|
|
|
contract test {
|
|
|
|
function g(function(uint) external returns (uint) x) {}
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
CompilerStack compilerStack;
|
2017-07-18 13:34:22 +00:00
|
|
|
compilerStack.addSource("", std::string(sourceCode));
|
2017-07-17 10:54:02 +00:00
|
|
|
compilerStack.setOptimiserSettings(dev::test::Options::get().optimize);
|
|
|
|
ETH_TEST_REQUIRE_NO_THROW(compilerStack.compile(), "Compiling contract failed");
|
2017-05-24 08:50:22 +00:00
|
|
|
bytes const& bytecode = compilerStack.runtimeObject("test").bytecode;
|
2017-05-19 15:10:32 +00:00
|
|
|
std::string const& metadata = compilerStack.metadata("test");
|
2017-05-24 08:54:25 +00:00
|
|
|
BOOST_CHECK(dev::test::isValidMetadata(metadata));
|
|
|
|
bytes hash = dev::swarmHash(metadata).asBytes();
|
2017-05-24 08:50:22 +00:00
|
|
|
BOOST_REQUIRE(hash.size() == 32);
|
|
|
|
BOOST_REQUIRE(bytecode.size() >= 2);
|
|
|
|
size_t metadataCBORSize = (size_t(bytecode.end()[-2]) << 8) + size_t(bytecode.end()[-1]);
|
|
|
|
BOOST_REQUIRE(metadataCBORSize < bytecode.size() - 2);
|
|
|
|
bytes expectation = bytes{0xa1, 0x65, 'b', 'z', 'z', 'r', '0', 0x58, 0x20} + hash;
|
|
|
|
BOOST_CHECK(std::equal(expectation.begin(), expectation.end(), bytecode.end() - metadataCBORSize - 2));
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
function g(function(uint) external returns (uint) x) {}
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
CompilerStack compilerStack;
|
|
|
|
compilerStack.addSource("", std::string(sourceCode));
|
|
|
|
compilerStack.setOptimiserSettings(dev::test::Options::get().optimize);
|
|
|
|
ETH_TEST_REQUIRE_NO_THROW(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));
|
|
|
|
bytes hash = dev::swarmHash(metadata).asBytes();
|
|
|
|
BOOST_REQUIRE(hash.size() == 32);
|
|
|
|
BOOST_REQUIRE(bytecode.size() >= 2);
|
|
|
|
size_t metadataCBORSize = (size_t(bytecode.end()[-2]) << 8) + size_t(bytecode.end()[-1]);
|
|
|
|
BOOST_REQUIRE(metadataCBORSize < bytecode.size() - 2);
|
|
|
|
bytes expectation =
|
|
|
|
bytes{0xa2, 0x65, 'b', 'z', 'z', 'r', '0', 0x58, 0x20} +
|
|
|
|
hash +
|
|
|
|
bytes{0x6c, 'e', 'x', 'p', 'e', 'r', 'i', 'm', 'e', 'n', 't', 'a', 'l', 0xf5};
|
|
|
|
BOOST_CHECK(std::equal(expectation.begin(), expectation.end(), bytecode.end() - metadataCBORSize - 2));
|
|
|
|
}
|
|
|
|
|
2017-07-18 18:28:58 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(metadata_relevant_sources)
|
|
|
|
{
|
|
|
|
CompilerStack compilerStack;
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
pragma solidity >=0.0;
|
|
|
|
contract A {
|
|
|
|
function g(function(uint) external returns (uint) x) {}
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
compilerStack.addSource("A", std::string(sourceCode));
|
|
|
|
sourceCode = R"(
|
|
|
|
pragma solidity >=0.0;
|
|
|
|
contract B {
|
|
|
|
function g(function(uint) external returns (uint) x) {}
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
compilerStack.addSource("B", std::string(sourceCode));
|
2017-07-27 09:26:57 +00:00
|
|
|
compilerStack.setOptimiserSettings(dev::test::Options::get().optimize);
|
|
|
|
ETH_TEST_REQUIRE_NO_THROW(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;
|
|
|
|
BOOST_REQUIRE(Json::Reader().parse(serialisedMetadata, metadata, false));
|
|
|
|
|
|
|
|
BOOST_CHECK_EQUAL(metadata["sources"].size(), 1);
|
|
|
|
BOOST_CHECK(metadata["sources"].isMember("A"));
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(metadata_relevant_sources_imports)
|
|
|
|
{
|
|
|
|
CompilerStack compilerStack;
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
pragma solidity >=0.0;
|
|
|
|
contract A {
|
|
|
|
function g(function(uint) external returns (uint) x) {}
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
compilerStack.addSource("A", std::string(sourceCode));
|
|
|
|
sourceCode = R"(
|
|
|
|
pragma solidity >=0.0;
|
|
|
|
import "./A";
|
|
|
|
contract B is A {
|
|
|
|
function g(function(uint) external returns (uint) x) {}
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
compilerStack.addSource("B", std::string(sourceCode));
|
|
|
|
sourceCode = R"(
|
|
|
|
pragma solidity >=0.0;
|
|
|
|
import "./B";
|
|
|
|
contract C is B {
|
|
|
|
function g(function(uint) external returns (uint) x) {}
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
compilerStack.addSource("C", std::string(sourceCode));
|
2017-07-27 09:26:57 +00:00
|
|
|
compilerStack.setOptimiserSettings(dev::test::Options::get().optimize);
|
|
|
|
ETH_TEST_REQUIRE_NO_THROW(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;
|
|
|
|
BOOST_REQUIRE(Json::Reader().parse(serialisedMetadata, metadata, false));
|
|
|
|
|
|
|
|
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"));
|
|
|
|
}
|
|
|
|
|
2017-05-24 08:50:22 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|