Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Add unit test to verify that the description digests of protocol feat… #8720

Merged
merged 4 commits into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,17 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/contracts.hpp.in ${CMAKE_CURRENT_BINA
add_subdirectory(snapshots)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/snapshots.hpp.in ${CMAKE_CURRENT_BINARY_DIR}/include/snapshots.hpp ESCAPE_QUOTES)

find_package(Python COMPONENTS Interpreter)
spoonincode marked this conversation as resolved.
Show resolved Hide resolved

add_custom_command(
OUTPUT protocol_feature_digest_tests.cpp
COMMAND ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/gen_protocol_feature_digest_tests.py ${CMAKE_CURRENT_SOURCE_DIR}/../libraries/chain/protocol_feature_manager.cpp > protocol_feature_digest_tests.cpp
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/../libraries/chain/protocol_feature_manager.cpp ${CMAKE_CURRENT_SOURCE_DIR}/gen_protocol_feature_digest_tests.py
)

### BUILD UNIT TEST EXECUTABLE ###
file(GLOB UNIT_TESTS "*.cpp") # find all unit test suites
add_executable( unit_test ${UNIT_TESTS}) # build unit tests as one executable
add_executable( unit_test ${UNIT_TESTS} protocol_feature_digest_tests.cpp) # build unit tests as one executable

target_link_libraries( unit_test eosio_chain chainbase eosio_testing fc appbase ${PLATFORM_SPECIFIC_LIBS} )

Expand All @@ -44,6 +52,8 @@ target_include_directories( unit_test PUBLIC
${CMAKE_CURRENT_BINARY_DIR}/include )

### MARK TEST SUITES FOR EXECUTION ###
add_test(NAME protocol_feature_digest_unit_test COMMAND unit_test --run_test=protocol_feature_digest_tests --report_level=detailed --color_output --catch_system_errors=no)
set(ctest_tests "protocol_feature_digest_tests")
foreach(TEST_SUITE ${UNIT_TESTS}) # create an independent target for each test suite
execute_process(COMMAND sh -c "grep -E 'BOOST_AUTO_TEST_SUITE\\s*[(]' ${TEST_SUITE} | grep -vE '//.*BOOST_AUTO_TEST_SUITE\\s*[(]' | cut -d ')' -f 1 | cut -d '(' -f 2" OUTPUT_VARIABLE SUITE_NAME OUTPUT_STRIP_TRAILING_WHITESPACE) # get the test suite name from the *.cpp file
if (NOT "" STREQUAL "${SUITE_NAME}") # ignore empty lines
Expand Down
29 changes: 29 additions & 0 deletions unittests/gen_protocol_feature_digest_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python3

import re
import hashlib
import sys

pattern = re.compile(r"\*\n(Builtin protocol feature: (.*)(?:\*[^/]|[^\*])*)\*/")

def main():
with open(sys.argv[1], "r") as f:
contents = f.read()
print('#include <eosio/chain/protocol_feature_manager.hpp>')
print('#include <map>')

print('#include <boost/test/unit_test.hpp>')

print('using namespace eosio::chain;')

print('BOOST_AUTO_TEST_CASE(protocol_feature_digest_tests) {')
print(' std::map<std::string, std::string> digests;')
for match in re.finditer(pattern, contents):
print(' digests.emplace("%s", "%s");' % (match.group(2), hashlib.sha256(match.group(1).encode('utf8')).hexdigest()))
print(' for(const auto& [id, spec] : builtin_protocol_feature_codenames) {')
print(' BOOST_TEST(digests[spec.codename] == fc::variant(spec.description_digest).as<std::string>());')
print(' }')
print('}')

if __name__ == "__main__":
main()