-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchain_plugin_tests.cpp
125 lines (102 loc) · 4.26 KB
/
chain_plugin_tests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* @file
* @copyright defined in eos/LICENSE.txt
*/
#include <boost/test/unit_test.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <eosio/testing/tester.hpp>
#include <eosio/chain/abi_serializer.hpp>
#include <eosio/chain/wasm_eosio_constraints.hpp>
#include <eosio/chain/resource_limits.hpp>
#include <eosio/chain/exceptions.hpp>
#include <eosio/chain/wast_to_wasm.hpp>
#include <eosio/chain_plugin/chain_plugin.hpp>
#include <contracts.hpp>
#include <fc/io/fstream.hpp>
#include <Runtime/Runtime.h>
#include <fc/variant_object.hpp>
#include <fc/io/json.hpp>
#include <array>
#include <utility>
#ifdef NON_VALIDATING_TEST
#define TESTER tester
#else
#define TESTER validating_tester
#endif
using namespace eosio;
using namespace eosio::chain;
using namespace eosio::testing;
using namespace fc;
BOOST_AUTO_TEST_SUITE(chain_plugin_tests)
BOOST_FIXTURE_TEST_CASE( get_block_with_invalid_abi, TESTER ) try {
produce_blocks(2);
create_accounts( {N(asserter)} );
produce_block();
// setup contract and abi
set_code( N(asserter), contracts::asserter_wasm() );
set_abi( N(asserter), contracts::asserter_abi().data() );
produce_blocks(1);
auto resolver = [&,this]( const account_name& name ) -> optional<abi_serializer> {
try {
const auto& accnt = this->control->db().get<account_object,by_name>( name );
abi_def abi;
if (abi_serializer::to_abi(accnt.abi, abi)) {
return abi_serializer(abi, abi_serializer_max_time);
}
return optional<abi_serializer>();
} FC_RETHROW_EXCEPTIONS(error, "resolver failed at chain_plugin_tests::abi_invalid_type");
};
// abi should be resolved
BOOST_REQUIRE_EQUAL(true, resolver(N(asserter)).valid());
// make an action using the valid contract & abi
variant pretty_trx = mutable_variant_object()
("actions", variants({
mutable_variant_object()
("account", "asserter")
("name", "procassert")
("authorization", variants({
mutable_variant_object()
("actor", "asserter")
("permission", name(config::active_name).to_string())
}))
("data", mutable_variant_object()
("condition", 1)
("message", "Should Not Assert!")
)
})
);
signed_transaction trx;
abi_serializer::from_variant(pretty_trx, trx, resolver, abi_serializer_max_time);
set_transaction_headers(trx);
trx.sign( get_private_key( N(asserter), "active" ), control->get_chain_id() );
push_transaction( trx );
produce_blocks(1);
// retrieve block num
uint32_t headnum = this->control->head_block_num();
char headnumstr[20];
sprintf(headnumstr, "%d", headnum);
chain_apis::read_only::get_block_params param{headnumstr};
chain_apis::read_only plugin(*(this->control), fc::microseconds::maximum());
// block should be decoded successfully
std::string block_str = json::to_pretty_string(plugin.get_block(param));
BOOST_TEST(block_str.find("procassert") != std::string::npos);
BOOST_TEST(block_str.find("condition") != std::string::npos);
BOOST_TEST(block_str.find("Should Not Assert!") != std::string::npos);
BOOST_TEST(block_str.find("011253686f756c64204e6f742041737365727421") != std::string::npos); //action data
// set an invalid abi (int8->xxxx)
std::string abi2 = contracts::asserter_abi().data();
auto pos = abi2.find("int8");
BOOST_TEST(pos != std::string::npos);
abi2.replace(pos, 4, "xxxx");
set_abi(N(asserter), abi2.c_str());
produce_blocks(1);
// resolving the invalid abi result in exception
BOOST_CHECK_THROW(resolver(N(asserter)), invalid_type_inside_abi);
// get the same block as string, results in decode failed(invalid abi) but not exception
std::string block_str2 = json::to_pretty_string(plugin.get_block(param));
BOOST_TEST(block_str2.find("procassert") != std::string::npos);
BOOST_TEST(block_str2.find("condition") == std::string::npos); // decode failed
BOOST_TEST(block_str2.find("Should Not Assert!") == std::string::npos); // decode failed
BOOST_TEST(block_str2.find("011253686f756c64204e6f742041737365727421") != std::string::npos); //action data
} FC_LOG_AND_RETHROW() /// get_block_with_invalid_abi
BOOST_AUTO_TEST_SUITE_END()