diff --git a/test/Metadata.cpp b/test/Metadata.cpp index 41e98b486..d132e90c2 100644 --- a/test/Metadata.cpp +++ b/test/Metadata.cpp @@ -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); } diff --git a/test/Metadata.h b/test/Metadata.h index 113a54bee..4851aa033 100644 --- a/test/Metadata.h +++ b/test/Metadata.h @@ -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); diff --git a/test/libsolidity/Metadata.cpp b/test/libsolidity/Metadata.cpp index 007ee2b6b..3402959da 100644 --- a/test/libsolidity/Metadata.cpp +++ b/test/libsolidity/Metadata.cpp @@ -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)