From 2f9c71f8b5a93a325c7a09ed1400e3b0408b4684 Mon Sep 17 00:00:00 2001 From: Nathaniel Date: Sun, 26 Feb 2023 13:33:34 -0600 Subject: [PATCH] Resolve #88: Testing for get_code_hash --- tests/integration/contracts.hpp.in | 5 ++ tests/integration/get_code_hash_tests.cpp | 46 +++++++++++++++++++ tests/unit/test_contracts/CMakeLists.txt | 2 + .../test_contracts/get_code_hash_read.cpp | 27 +++++++++++ .../test_contracts/get_code_hash_table.hpp | 10 ++++ .../test_contracts/get_code_hash_write.cpp | 28 +++++++++++ 6 files changed, 118 insertions(+) create mode 100644 tests/integration/get_code_hash_tests.cpp create mode 100644 tests/unit/test_contracts/get_code_hash_read.cpp create mode 100644 tests/unit/test_contracts/get_code_hash_table.hpp create mode 100644 tests/unit/test_contracts/get_code_hash_write.cpp diff --git a/tests/integration/contracts.hpp.in b/tests/integration/contracts.hpp.in index abbb050c78..d9c40c74e1 100644 --- a/tests/integration/contracts.hpp.in +++ b/tests/integration/contracts.hpp.in @@ -22,5 +22,10 @@ namespace eosio::testing { static std::vector crypto_primitives_test_wasm() { return read_wasm("${CMAKE_BINARY_DIR}/../unit/test_contracts/crypto_primitives_tests.wasm"); } static std::vector crypto_primitives_test_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/crypto_primitives_tests.abi"); } + + static std::vector get_code_hash_write_test_wasm() { return read_wasm("${CMAKE_BINARY_DIR}/../unit/test_contracts/get_code_hash_write.wasm"); } + static std::vector get_code_hash_write_test_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/get_code_hash_write.abi"); } + static std::vector get_code_hash_read_test_wasm() { return read_wasm("${CMAKE_BINARY_DIR}/../unit/test_contracts/get_code_hash_read.wasm"); } + static std::vector get_code_hash_read_test_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/get_code_hash_read.abi"); } }; } //ns eosio::testing diff --git a/tests/integration/get_code_hash_tests.cpp b/tests/integration/get_code_hash_tests.cpp new file mode 100644 index 0000000000..cdce928176 --- /dev/null +++ b/tests/integration/get_code_hash_tests.cpp @@ -0,0 +1,46 @@ +#include +#include +#include + +#include + +#include + +#include + +using namespace eosio; +using namespace eosio::testing; +using namespace eosio::chain; +using namespace fc; + +using mvo = fc::mutable_variant_object; + +struct code_hash { + uint64_t id; + fc::sha256 hash; + uint64_t primary_key() const { return id; } +}; +FC_REFLECT(code_hash, (id)(hash)) + +BOOST_AUTO_TEST_SUITE(get_code_hash_tests_suite) + +BOOST_FIXTURE_TEST_CASE( get_code_hash_tests, tester ) try { + create_accounts( { "test"_n } ); + produce_block(); + + set_code( "test"_n, contracts::get_code_hash_write_test_wasm() ); + set_abi( "test"_n, contracts::get_code_hash_write_test_abi().data() ); + + produce_blocks(); + push_action("test"_n, "theaction"_n, "test"_n, mvo()); + code_hash entry; + get_table_entry(entry, "test"_n, "test"_n, "code.hash"_n, 0); + wdump((entry.hash)); + + set_code( "test"_n, contracts::get_code_hash_read_test_wasm() ); + produce_blocks(); + + push_action("test"_n, "theaction"_n, "test"_n, mvo()); +} FC_LOG_AND_RETHROW() + +BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/unit/test_contracts/CMakeLists.txt b/tests/unit/test_contracts/CMakeLists.txt index 1e418f38b0..3b1654fe9f 100644 --- a/tests/unit/test_contracts/CMakeLists.txt +++ b/tests/unit/test_contracts/CMakeLists.txt @@ -7,6 +7,8 @@ add_contract(explicit_nested_tests explicit_nested_tests explicit_nested_tests.c add_contract(transfer_contract transfer_contract transfer.cpp) add_contract(minimal_tests minimal_tests minimal_tests.cpp) add_contract(crypto_primitives_tests crypto_primitives_tests crypto_primitives_tests.cpp) +add_contract(get_code_hash_tests get_code_hash_write get_code_hash_write.cpp) +add_contract(get_code_hash_tests get_code_hash_read get_code_hash_read.cpp) add_contract(capi_tests capi_tests capi/capi.c capi/action.c capi/chain.c capi/crypto.c capi/db.c capi/permission.c capi/print.c capi/privileged.c capi/system.c capi/transaction.c) diff --git a/tests/unit/test_contracts/get_code_hash_read.cpp b/tests/unit/test_contracts/get_code_hash_read.cpp new file mode 100644 index 0000000000..05c51a70c4 --- /dev/null +++ b/tests/unit/test_contracts/get_code_hash_read.cpp @@ -0,0 +1,27 @@ +#include +#include +#include + +#include "get_code_hash_table.hpp" + +class [[eosio::contract]] get_code_hash_tests : public contract { +public: + using contract::contract; + + using hash_table = multi_index; + + // Read the old code's hash from database and verify new code's hash differs + [[eosio::action]] + void theaction() { + require_auth(get_self()); + hash_table hashes(get_self(), get_self().value); + + auto hash = get_code_hash(get_self()); + check(hash != checksum256(), "Code hash should not be null"); + + auto record = hashes.get(0, "Unable to find recorded hash"); + check(hash != record.hash, "Code hash has not changed"); + eosio::print("Old hash: ", record.hash, "; new hash: ", hash); + } +}; + diff --git a/tests/unit/test_contracts/get_code_hash_table.hpp b/tests/unit/test_contracts/get_code_hash_table.hpp new file mode 100644 index 0000000000..696558e40a --- /dev/null +++ b/tests/unit/test_contracts/get_code_hash_table.hpp @@ -0,0 +1,10 @@ +#pragma once + +using namespace eosio; + +TABLE code_hash { + uint64_t id; + checksum256 hash; + uint64_t primary_key() const { return id; } +}; + diff --git a/tests/unit/test_contracts/get_code_hash_write.cpp b/tests/unit/test_contracts/get_code_hash_write.cpp new file mode 100644 index 0000000000..ae61a06a07 --- /dev/null +++ b/tests/unit/test_contracts/get_code_hash_write.cpp @@ -0,0 +1,28 @@ +#include +#include +#include + +#include "get_code_hash_table.hpp" + +class [[eosio::contract]] get_code_hash_tests : public contract { +public: + using contract::contract; + + using hash_table = multi_index; + + // Write this code's hash to database + [[eosio::action]] + void theaction() { + require_auth(get_self()); + hash_table hashes(get_self(), get_self().value); + + auto hash = get_code_hash(get_self()); + check(hash != checksum256(), "Code hash should not be null"); + + hashes.emplace(get_self(), [&hash](auto& t) { + t.id = 0; + t.hash = hash; + }); + } +}; +