Add onlyMetadata helper in test/Metadata

This commit is contained in:
Alex Beregszaszi 2019-02-05 23:20:15 +00:00
parent 4d8c57006b
commit 12f34c8229
3 changed files with 18 additions and 9 deletions

View File

@ -31,7 +31,7 @@ namespace dev
namespace test
{
bytes bytecodeSansMetadata(bytes const& _bytecode)
bytes onlyMetadata(bytes const& _bytecode)
{
unsigned size = _bytecode.size();
if (size < 5)
@ -43,6 +43,14 @@ bytes bytecodeSansMetadata(bytes const& _bytecode)
unsigned char firstByte = _bytecode[size - metadataSize - 2];
if (firstByte != 0xa1 && firstByte != 0xa2)
return bytes{};
return bytes(_bytecode.end() - metadataSize - 2, _bytecode.end() - 2);
}
bytes bytecodeSansMetadata(bytes const& _bytecode)
{
unsigned metadataSize = onlyMetadata(_bytecode).size();
if (metadataSize == 0)
return bytes{};
return bytes(_bytecode.begin(), _bytecode.end() - metadataSize - 2);
}

View File

@ -27,6 +27,9 @@ namespace dev
namespace test
{
/// Returns only the CBOR metadata.
bytes onlyMetadata(bytes const& _bytecode);
/// Returns the bytecode with the metadata hash stripped out.
bytes bytecodeSansMetadata(bytes const& _bytecode);

View File

@ -54,11 +54,10 @@ BOOST_AUTO_TEST_CASE(metadata_stamp)
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 cborMetadata = dev::test::onlyMetadata(bytecode);
BOOST_REQUIRE(!cborMetadata.empty());
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_CHECK(std::equal(expectation.begin(), expectation.end(), cborMetadata.begin()));
}
BOOST_AUTO_TEST_CASE(metadata_stamp_experimental)
@ -81,14 +80,13 @@ BOOST_AUTO_TEST_CASE(metadata_stamp_experimental)
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 cborMetadata = dev::test::onlyMetadata(bytecode);
BOOST_REQUIRE(!cborMetadata.empty());
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));
BOOST_CHECK(std::equal(expectation.begin(), expectation.end(), cborMetadata.begin()));
}
BOOST_AUTO_TEST_CASE(metadata_relevant_sources)