-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathcall_tests.cpp
More file actions
261 lines (210 loc) · 10.5 KB
/
call_tests.cpp
File metadata and controls
261 lines (210 loc) · 10.5 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include <boost/test/unit_test.hpp>
#include <eosio/testing/tester.hpp>
#include <eosio/chain/abi_serializer.hpp>
#include <fc/variant_object.hpp>
#include <contracts.hpp>
using namespace eosio;
using namespace eosio::testing;
using namespace fc;
using mvo = fc::mutable_variant_object;
struct acct_and_code {
account_name acct;
std::vector<uint8_t> wasm;
char* abi = nullptr;
};
// The first account in the accounts vector is the action initiating the
// first sync call
struct call_tester: tester {
call_tester(const std::vector<acct_and_code>& accounts) {
for (auto i = 0u; i < accounts.size(); ++i) {
create_account(accounts[i].acct);
set_code(accounts[i].acct, accounts[i].wasm);
set_abi(accounts[i].acct, accounts[i].abi);
}
produce_block();
}
};
BOOST_AUTO_TEST_SUITE(sync_call_tests)
// Verify a sync call returns value correctly
BOOST_AUTO_TEST_CASE(return_value_test) { try {
call_tester t({
{"caller"_n, contracts::caller_wasm(), contracts::caller_abi().data()},
{"callee"_n, contracts::callee_wasm(), contracts::callee_abi().data()}
});
// Using host function directly
BOOST_REQUIRE_NO_THROW(t.push_action("caller"_n, "hstretvaltst"_n, "caller"_n, {}));
// Using call_wrapper
BOOST_REQUIRE_NO_THROW(t.push_action("caller"_n, "wrpretvaltst"_n, "caller"_n, {}));
} FC_LOG_AND_RETHROW() }
// Verify one parameter passing works correctly
BOOST_AUTO_TEST_CASE(param_basic_test) { try {
call_tester t({
{"caller"_n, contracts::caller_wasm(), contracts::caller_abi().data()},
{"callee"_n, contracts::callee_wasm(), contracts::callee_abi().data()}
});
// Using host function directly
BOOST_REQUIRE_NO_THROW(t.push_action("caller"_n, "hstoneprmtst"_n, "caller"_n, {}));
// Using call_wrapper
BOOST_REQUIRE_NO_THROW(t.push_action("caller"_n, "wrponeprmtst"_n, "caller"_n, {}));
} FC_LOG_AND_RETHROW() }
// Verify multiple parameters passing works correctly
BOOST_AUTO_TEST_CASE(multiple_params_test) { try {
call_tester t({
{"caller"_n, contracts::caller_wasm(), contracts::caller_abi().data()},
{"callee"_n, contracts::callee_wasm(), contracts::callee_abi().data()}
});
// Using host function directly
BOOST_REQUIRE_NO_THROW(t.push_action("caller"_n, "hstmulprmtst"_n, "caller"_n, {}));
// Using call_wrapper
BOOST_REQUIRE_NO_THROW(t.push_action("caller"_n, "wrpmulprmtst"_n, "caller"_n, {}));
} FC_LOG_AND_RETHROW() }
// Verify passing a struct parameter works correctly
BOOST_AUTO_TEST_CASE(struct_param_test) { try {
call_tester t({
{"caller"_n, contracts::caller_wasm(), contracts::caller_abi().data()},
{"callee"_n, contracts::callee_wasm(), contracts::callee_abi().data()}
});
BOOST_REQUIRE_NO_THROW(t.push_action("caller"_n, "structtest"_n, "caller"_n, {}));
} FC_LOG_AND_RETHROW() }
// Verify passing a mix of structs and integer works correctly
BOOST_AUTO_TEST_CASE(mix_struct_int_params_test) { try {
call_tester t({
{"caller"_n, contracts::caller_wasm(), contracts::caller_abi().data()},
{"callee"_n, contracts::callee_wasm(), contracts::callee_abi().data()}
});
BOOST_REQUIRE_NO_THROW(t.push_action("caller"_n, "structinttst"_n, "caller"_n, {}));
} FC_LOG_AND_RETHROW() }
// Verify a sync call to a void function works properly.
BOOST_AUTO_TEST_CASE(void_func_test) { try {
call_tester t({
{"caller"_n, contracts::caller_wasm(), contracts::caller_abi().data()},
{"callee"_n, contracts::callee_wasm(), contracts::callee_abi().data()}
});
auto check = [] (const transaction_trace_ptr& trx_trace) {
auto& atrace = trx_trace->action_traces;
auto& call_traces = atrace[0].call_traces;
BOOST_REQUIRE_EQUAL(call_traces.size(), 1u);
// Verify the print from the void function is correct.
// The test contract checks the return value size is 0.
auto& call_trace = call_traces[0];
BOOST_REQUIRE_EQUAL(call_trace.call_ordinal, 1u);
BOOST_REQUIRE_EQUAL(call_trace.sender_ordinal, 0u);
BOOST_REQUIRE_EQUAL(call_trace.console, "I am a void function");
};
// Using host function directly
auto trx_trace = t.push_action("caller"_n, "hstvodfuntst"_n, "caller"_n, {});
check(trx_trace);
// Using call_wrapper
trx_trace = t.push_action("caller"_n, "wrpvodfuntst"_n, "caller"_n, {});
check(trx_trace);
} FC_LOG_AND_RETHROW() }
// Verify a function tagged as both `action` and `call` works
BOOST_AUTO_TEST_CASE(mixed_action_call_tags_test) { try {
call_tester t({
{"caller"_n, contracts::caller_wasm(), contracts::caller_abi().data()},
{"callee"_n, contracts::callee_wasm(), contracts::callee_abi().data()}
});
// `sum` in `callee` contract is tagged as `action` and `call`
// Make sure we can make a sync call to `sum` (`mulparamtest` in `caller` does
// a sync call to `sum`)
BOOST_REQUIRE_NO_THROW(t.push_action("caller"_n, "hstmulprmtst"_n, "caller"_n, {}));
// Make sure we can push an action using `sum`.
BOOST_REQUIRE_NO_THROW(t.push_action("callee"_n, "sum"_n, "callee"_n,
mvo()
("a", 1)
("b", 2)
("c", 3)));
} FC_LOG_AND_RETHROW() }
// Verify the receiver contract with only one sync call function works
// (for testing the sync_call entry point dispatcher)
BOOST_AUTO_TEST_CASE(single_function_test) { try {
call_tester t({
{"caller"_n, contracts::caller_wasm(), contracts::caller_abi().data()},
{"callee"_n, contracts::single_func_wasm(), contracts::single_func_abi().data()}
});
// The single_func_wasm contains only one function and the caller contract
// hooks up with it
BOOST_REQUIRE_NO_THROW(t.push_action("caller"_n, "hstretvaltst"_n, "caller"_n, {}));
} FC_LOG_AND_RETHROW() }
// Verify support_mode for void and non-void sync calls if calls are a failure
BOOST_AUTO_TEST_CASE(sync_call_support_mode_failure_test) { try {
call_tester t({
{"caller"_n, contracts::caller_wasm(), contracts::caller_abi().data()},
{"callee"_n, contracts::not_supported_wasm(), contracts::not_supported_abi().data()}
});
// voidfncnoop uses support_mode::no_op
BOOST_REQUIRE_NO_THROW(t.push_action("caller"_n, "voidfncnoop"_n, "caller"_n, {}));
// voidfncabort uses default support_mode::abort
BOOST_CHECK_EXCEPTION(t.push_action("caller"_n, "voidfncabort"_n, "caller"_n, {}),
eosio_assert_message_exception,
fc_exception_message_contains("receiver does not support sync call but support_mode is set to abort"));
// intfuncnoop uses support_mode::no_op
BOOST_REQUIRE_NO_THROW(t.push_action("caller"_n, "intfuncnoop"_n, "caller"_n, {}));
// intfuncabort uses default support_mode::abort
BOOST_CHECK_EXCEPTION(t.push_action("caller"_n, "intfuncabort"_n, "caller"_n, {}),
eosio_assert_message_exception,
fc_exception_message_contains("receiver does not support sync call but support_mode is set to abort"));
} FC_LOG_AND_RETHROW() }
// Verify support_mode for void and non-void sync calls if call is successful
BOOST_AUTO_TEST_CASE(sync_call_support_mode_success_test) { try {
call_tester t({
{"caller"_n, contracts::caller_wasm(), contracts::caller_abi().data()},
{"callee"_n, contracts::callee_wasm(), contracts::callee_abi().data()}
});
// voidnoopsucc uses support_mode::no_op
BOOST_REQUIRE_NO_THROW(t.push_action("caller"_n, "voidnoopsucc"_n, "caller"_n, {}));
// sumnoopsucc uses support_mode::no_op
BOOST_REQUIRE_NO_THROW(t.push_action("caller"_n, "sumnoopsucc"_n, "caller"_n, {}));
} FC_LOG_AND_RETHROW() }
// Verify header validation
BOOST_AUTO_TEST_CASE(unknown_function_test) { try {
call_tester t({
{"caller"_n, contracts::caller_wasm(), contracts::caller_abi().data()},
{"callee"_n, contracts::callee_wasm(), contracts::callee_abi().data()}
});
BOOST_REQUIRE_NO_THROW(t.push_action("caller"_n, "hdrvaltest"_n, "caller"_n, {}));
} FC_LOG_AND_RETHROW() }
// Verify adding/reading entries to/from a table, and read-only enforcement work
BOOST_AUTO_TEST_CASE(addr_book_tests) { try {
call_tester t({
{"caller"_n, contracts::addr_book_caller_wasm(), contracts::addr_book_caller_abi().data()},
{"callee"_n, contracts::addr_book_callee_wasm(), contracts::addr_book_callee_abi().data()}
});
// Try to add an entry using a read-only sync call
BOOST_CHECK_EXCEPTION(t.push_action("caller"_n, "upsertrdonly"_n, "caller"_n, mvo()
("user", "alice")
("first_name", "alice")
("street", "123 Main St.")),
unaccessible_api,
fc_exception_message_contains("this API is not allowed in read only action/call"));
// Add an entry using a read-write sync call
t.push_action("caller"_n, "upsert"_n, "caller"_n, mvo()
("user", "alice")
("first_name", "alice")
("street", "123 Main St."));
// Read the inserted entry. "get"_n action will check the return value from the sync call
BOOST_REQUIRE_NO_THROW(t.push_action("caller"_n, "get"_n, "caller"_n, mvo() ("user", "alice")));
} FC_LOG_AND_RETHROW() }
// For a function tagged as both `action` and `call`, verify is_sync_call()
// returns true if tagged as `call` and false if tagged as `action`.
BOOST_AUTO_TEST_CASE(is_sync_call_test) { try {
call_tester t({
{"caller"_n, contracts::caller_wasm(), contracts::caller_abi().data()},
{"callee"_n, contracts::callee_wasm(), contracts::callee_abi().data()}
});
// issynccall() is tagged as both `action` and `call`, and returns
// is_sync_call(). makesynccall() calls issynccall() as a sync call
// and returns its result. So the current action return value must be
// true.
auto trx_trace = t.push_action("caller"_n, "makesynccall"_n, "caller"_n, {});
auto& action_trace = trx_trace->action_traces[0];
bool return_value = fc::raw::unpack<bool>(action_trace.return_value);
BOOST_REQUIRE(return_value == true);
// Call issynccall() directly as an action. Since issynccall()
// returns is_sync_call(), the current action return value must be false.
trx_trace = t.push_action("callee"_n, "issynccall"_n, "callee"_n, {});
auto& action_trace1 = trx_trace->action_traces[0];
return_value = fc::raw::unpack<bool>(action_trace1.return_value);
BOOST_REQUIRE(return_value == false);
} FC_LOG_AND_RETHROW() }
BOOST_AUTO_TEST_SUITE_END()