aboutsummaryrefslogtreecommitdiffstats
path: root/test/libsolidity/Metadata.cpp
blob: ce1a7b18b33bee8c536e1b1c3085335b8d858baf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
    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.
 */

#include "../Metadata.h"
#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;
    compilerStack.addSource("", std::string(sourceCode));
    ETH_TEST_REQUIRE_NO_THROW(compilerStack.compile(dev::test::Options::get().optimize), "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{0xa1, 0x65, 'b', 'z', 'z', 'r', '0', 0x58, 0x20} + hash;
    BOOST_CHECK(std::equal(expectation.begin(), expectation.end(), bytecode.end() - metadataCBORSize - 2));
}

BOOST_AUTO_TEST_SUITE_END()

}
}
}