Add a version of isValidMetadata() that accepts already parsed metadata

This commit is contained in:
Kamil Śliwak 2021-09-10 15:50:37 +02:00
parent 4a02e9a340
commit 83b3bd0227
3 changed files with 33 additions and 26 deletions

View File

@ -24,7 +24,6 @@
#include <iostream> #include <iostream>
#include <libsolutil/Assertions.h> #include <libsolutil/Assertions.h>
#include <libsolutil/CommonData.h> #include <libsolutil/CommonData.h>
#include <libsolutil/JSON.h>
#include <test/Metadata.h> #include <test/Metadata.h>
using namespace std; using namespace std;
@ -170,30 +169,35 @@ std::optional<map<string, string>> parseCBORMetadata(bytes const& _metadata)
} }
} }
bool isValidMetadata(string const& _metadata) bool isValidMetadata(string const& _serialisedMetadata)
{ {
Json::Value metadata; Json::Value metadata;
if (!util::jsonParseStrict(_metadata, metadata)) if (!util::jsonParseStrict(_serialisedMetadata, metadata))
return false; return false;
return isValidMetadata(metadata);
}
bool isValidMetadata(Json::Value const& _metadata)
{
if ( if (
!metadata.isObject() || !_metadata.isObject() ||
!metadata.isMember("version") || !_metadata.isMember("version") ||
!metadata.isMember("language") || !_metadata.isMember("language") ||
!metadata.isMember("compiler") || !_metadata.isMember("compiler") ||
!metadata.isMember("settings") || !_metadata.isMember("settings") ||
!metadata.isMember("sources") || !_metadata.isMember("sources") ||
!metadata.isMember("output") || !_metadata.isMember("output") ||
!metadata["settings"].isMember("evmVersion") || !_metadata["settings"].isMember("evmVersion") ||
!metadata["settings"].isMember("metadata") || !_metadata["settings"].isMember("metadata") ||
!metadata["settings"]["metadata"].isMember("bytecodeHash") !_metadata["settings"]["metadata"].isMember("bytecodeHash")
) )
return false; return false;
if (!metadata["version"].isNumeric() || metadata["version"] != 1) if (!_metadata["version"].isNumeric() || _metadata["version"] != 1)
return false; return false;
if (!metadata["language"].isString() || metadata["language"].asString() != "Solidity") if (!_metadata["language"].isString() || _metadata["language"].asString() != "Solidity")
return false; return false;
/// @TODO add more strict checks /// @TODO add more strict checks

View File

@ -21,6 +21,7 @@
*/ */
#include <libsolutil/CommonData.h> #include <libsolutil/CommonData.h>
#include <libsolutil/JSON.h>
#include <map> #include <map>
#include <optional> #include <optional>
@ -48,8 +49,10 @@ std::string bytecodeSansMetadata(std::string const& _bytecode);
/// - everything else is invalid /// - everything else is invalid
std::optional<std::map<std::string, std::string>> parseCBORMetadata(bytes const& _metadata); std::optional<std::map<std::string, std::string>> parseCBORMetadata(bytes const& _metadata);
/// Expects a serialised metadata JSON and returns true if the /// Expects a serialised metadata JSON and returns true if the content is valid metadata.
/// content is valid metadata. bool isValidMetadata(std::string const& _serialisedMetadata);
bool isValidMetadata(std::string const& _metadata);
/// Expects a deserialised metadata JSON and returns true if the content is valid metadata.
bool isValidMetadata(Json::Value const& _metadata);
} // end namespaces } // end namespaces

View File

@ -58,9 +58,9 @@ optional<string> compileAndCheckLicenseMetadata(string const& _contractName, cha
BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed"); BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed");
std::string const& serialisedMetadata = compilerStack.metadata(_contractName); std::string const& serialisedMetadata = compilerStack.metadata(_contractName);
BOOST_CHECK(solidity::test::isValidMetadata(serialisedMetadata));
Json::Value metadata; Json::Value metadata;
BOOST_REQUIRE(util::jsonParseStrict(serialisedMetadata, metadata)); BOOST_REQUIRE(util::jsonParseStrict(serialisedMetadata, metadata));
BOOST_CHECK(solidity::test::isValidMetadata(metadata));
BOOST_CHECK_EQUAL(metadata["sources"].size(), 1); BOOST_CHECK_EQUAL(metadata["sources"].size(), 1);
BOOST_REQUIRE(metadata["sources"].isMember("A.sol")); 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"); BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed");
std::string const& serialisedMetadata = compilerStack.metadata("A"); std::string const& serialisedMetadata = compilerStack.metadata("A");
BOOST_CHECK(solidity::test::isValidMetadata(serialisedMetadata));
Json::Value metadata; Json::Value metadata;
BOOST_REQUIRE(util::jsonParseStrict(serialisedMetadata, metadata)); BOOST_REQUIRE(util::jsonParseStrict(serialisedMetadata, metadata));
BOOST_CHECK(solidity::test::isValidMetadata(metadata));
BOOST_CHECK_EQUAL(metadata["sources"].size(), 1); BOOST_CHECK_EQUAL(metadata["sources"].size(), 1);
BOOST_CHECK(metadata["sources"].isMember("A")); 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"); BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed");
std::string const& serialisedMetadata = compilerStack.metadata("C"); std::string const& serialisedMetadata = compilerStack.metadata("C");
BOOST_CHECK(solidity::test::isValidMetadata(serialisedMetadata));
Json::Value metadata; Json::Value metadata;
BOOST_REQUIRE(util::jsonParseStrict(serialisedMetadata, metadata)); BOOST_REQUIRE(util::jsonParseStrict(serialisedMetadata, metadata));
BOOST_CHECK(solidity::test::isValidMetadata(metadata));
BOOST_CHECK_EQUAL(metadata["sources"].size(), 3); BOOST_CHECK_EQUAL(metadata["sources"].size(), 3);
BOOST_CHECK(metadata["sources"].isMember("A")); BOOST_CHECK(metadata["sources"].isMember("A"));
@ -320,8 +320,8 @@ BOOST_AUTO_TEST_CASE(metadata_useLiteralContent)
BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed"); BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed");
string metadata_str = compilerStack.metadata("test"); string metadata_str = compilerStack.metadata("test");
Json::Value metadata; Json::Value metadata;
util::jsonParseStrict(metadata_str, metadata); BOOST_REQUIRE(util::jsonParseStrict(metadata_str, metadata));
BOOST_CHECK(solidity::test::isValidMetadata(metadata_str)); BOOST_CHECK(solidity::test::isValidMetadata(metadata));
BOOST_CHECK(metadata.isMember("settings")); BOOST_CHECK(metadata.isMember("settings"));
BOOST_CHECK(metadata["settings"].isMember("metadata")); BOOST_CHECK(metadata["settings"].isMember("metadata"));
BOOST_CHECK(metadata["settings"]["metadata"].isMember("bytecodeHash")); 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"); BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed");
string metadata_str = compilerStack.metadata("test"); string metadata_str = compilerStack.metadata("test");
Json::Value metadata; Json::Value metadata;
util::jsonParseStrict(metadata_str, metadata); BOOST_REQUIRE(util::jsonParseStrict(metadata_str, metadata));
BOOST_CHECK(solidity::test::isValidMetadata(metadata_str)); BOOST_CHECK(solidity::test::isValidMetadata(metadata));
BOOST_CHECK(metadata.isMember("settings")); BOOST_CHECK(metadata.isMember("settings"));
if (_viair) if (_viair)
{ {
@ -381,9 +381,9 @@ BOOST_AUTO_TEST_CASE(metadata_revert_strings)
BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed"); BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed");
std::string const& serialisedMetadata = compilerStack.metadata("A"); std::string const& serialisedMetadata = compilerStack.metadata("A");
BOOST_CHECK(solidity::test::isValidMetadata(serialisedMetadata));
Json::Value metadata; Json::Value metadata;
BOOST_REQUIRE(util::jsonParseStrict(serialisedMetadata, metadata)); BOOST_REQUIRE(util::jsonParseStrict(serialisedMetadata, metadata));
BOOST_CHECK(solidity::test::isValidMetadata(metadata));
BOOST_CHECK_EQUAL(metadata["settings"]["debug"]["revertStrings"], "strip"); BOOST_CHECK_EQUAL(metadata["settings"]["debug"]["revertStrings"], "strip");
} }