diff --git a/test/Metadata.cpp b/test/Metadata.cpp index 61654ae00..687b9d73e 100644 --- a/test/Metadata.cpp +++ b/test/Metadata.cpp @@ -24,7 +24,6 @@ #include #include #include -#include #include using namespace std; @@ -170,30 +169,35 @@ std::optional> parseCBORMetadata(bytes const& _metadata) } } -bool isValidMetadata(string const& _metadata) +bool isValidMetadata(string const& _serialisedMetadata) { Json::Value metadata; - if (!util::jsonParseStrict(_metadata, metadata)) + if (!util::jsonParseStrict(_serialisedMetadata, metadata)) return false; + return isValidMetadata(metadata); +} + +bool isValidMetadata(Json::Value const& _metadata) +{ if ( - !metadata.isObject() || - !metadata.isMember("version") || - !metadata.isMember("language") || - !metadata.isMember("compiler") || - !metadata.isMember("settings") || - !metadata.isMember("sources") || - !metadata.isMember("output") || - !metadata["settings"].isMember("evmVersion") || - !metadata["settings"].isMember("metadata") || - !metadata["settings"]["metadata"].isMember("bytecodeHash") + !_metadata.isObject() || + !_metadata.isMember("version") || + !_metadata.isMember("language") || + !_metadata.isMember("compiler") || + !_metadata.isMember("settings") || + !_metadata.isMember("sources") || + !_metadata.isMember("output") || + !_metadata["settings"].isMember("evmVersion") || + !_metadata["settings"].isMember("metadata") || + !_metadata["settings"]["metadata"].isMember("bytecodeHash") ) return false; - if (!metadata["version"].isNumeric() || metadata["version"] != 1) + if (!_metadata["version"].isNumeric() || _metadata["version"] != 1) return false; - if (!metadata["language"].isString() || metadata["language"].asString() != "Solidity") + if (!_metadata["language"].isString() || _metadata["language"].asString() != "Solidity") return false; /// @TODO add more strict checks diff --git a/test/Metadata.h b/test/Metadata.h index c91ceb559..86b43e245 100644 --- a/test/Metadata.h +++ b/test/Metadata.h @@ -21,6 +21,7 @@ */ #include +#include #include #include @@ -48,8 +49,10 @@ std::string bytecodeSansMetadata(std::string const& _bytecode); /// - everything else is invalid std::optional> parseCBORMetadata(bytes const& _metadata); -/// Expects a serialised metadata JSON and returns true if the -/// content is valid metadata. -bool isValidMetadata(std::string const& _metadata); +/// Expects a serialised metadata JSON and returns true if the content is valid metadata. +bool isValidMetadata(std::string const& _serialisedMetadata); + +/// Expects a deserialised metadata JSON and returns true if the content is valid metadata. +bool isValidMetadata(Json::Value const& _metadata); } // end namespaces diff --git a/test/libsolidity/Metadata.cpp b/test/libsolidity/Metadata.cpp index ae2dd85db..706bf15d5 100644 --- a/test/libsolidity/Metadata.cpp +++ b/test/libsolidity/Metadata.cpp @@ -58,9 +58,9 @@ optional compileAndCheckLicenseMetadata(string const& _contractName, cha BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed"); std::string const& serialisedMetadata = compilerStack.metadata(_contractName); - BOOST_CHECK(solidity::test::isValidMetadata(serialisedMetadata)); Json::Value metadata; BOOST_REQUIRE(util::jsonParseStrict(serialisedMetadata, metadata)); + BOOST_CHECK(solidity::test::isValidMetadata(metadata)); BOOST_CHECK_EQUAL(metadata["sources"].size(), 1); BOOST_REQUIRE(metadata["sources"].isMember("A.sol")); @@ -250,9 +250,9 @@ BOOST_AUTO_TEST_CASE(metadata_relevant_sources) BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed"); std::string const& serialisedMetadata = compilerStack.metadata("A"); - BOOST_CHECK(solidity::test::isValidMetadata(serialisedMetadata)); Json::Value metadata; BOOST_REQUIRE(util::jsonParseStrict(serialisedMetadata, metadata)); + BOOST_CHECK(solidity::test::isValidMetadata(metadata)); BOOST_CHECK_EQUAL(metadata["sources"].size(), 1); BOOST_CHECK(metadata["sources"].isMember("A")); @@ -291,9 +291,9 @@ BOOST_AUTO_TEST_CASE(metadata_relevant_sources_imports) BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed"); std::string const& serialisedMetadata = compilerStack.metadata("C"); - BOOST_CHECK(solidity::test::isValidMetadata(serialisedMetadata)); Json::Value metadata; BOOST_REQUIRE(util::jsonParseStrict(serialisedMetadata, metadata)); + BOOST_CHECK(solidity::test::isValidMetadata(metadata)); BOOST_CHECK_EQUAL(metadata["sources"].size(), 3); BOOST_CHECK(metadata["sources"].isMember("A")); @@ -320,8 +320,8 @@ BOOST_AUTO_TEST_CASE(metadata_useLiteralContent) BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed"); string metadata_str = compilerStack.metadata("test"); Json::Value metadata; - util::jsonParseStrict(metadata_str, metadata); - BOOST_CHECK(solidity::test::isValidMetadata(metadata_str)); + BOOST_REQUIRE(util::jsonParseStrict(metadata_str, metadata)); + BOOST_CHECK(solidity::test::isValidMetadata(metadata)); BOOST_CHECK(metadata.isMember("settings")); BOOST_CHECK(metadata["settings"].isMember("metadata")); BOOST_CHECK(metadata["settings"]["metadata"].isMember("bytecodeHash")); @@ -354,8 +354,8 @@ BOOST_AUTO_TEST_CASE(metadata_viair) BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed"); string metadata_str = compilerStack.metadata("test"); Json::Value metadata; - util::jsonParseStrict(metadata_str, metadata); - BOOST_CHECK(solidity::test::isValidMetadata(metadata_str)); + BOOST_REQUIRE(util::jsonParseStrict(metadata_str, metadata)); + BOOST_CHECK(solidity::test::isValidMetadata(metadata)); BOOST_CHECK(metadata.isMember("settings")); if (_viair) { @@ -381,9 +381,9 @@ BOOST_AUTO_TEST_CASE(metadata_revert_strings) BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed"); std::string const& serialisedMetadata = compilerStack.metadata("A"); - BOOST_CHECK(solidity::test::isValidMetadata(serialisedMetadata)); Json::Value metadata; BOOST_REQUIRE(util::jsonParseStrict(serialisedMetadata, metadata)); + BOOST_CHECK(solidity::test::isValidMetadata(metadata)); BOOST_CHECK_EQUAL(metadata["settings"]["debug"]["revertStrings"], "strip"); }