-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathpreconditions_tests.cpp
More file actions
234 lines (212 loc) · 8.09 KB
/
preconditions_tests.cpp
File metadata and controls
234 lines (212 loc) · 8.09 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
#include <eosio/vm/backend.hpp>
#include <eosio/vm/host_function.hpp>
#include "utils.hpp"
#include <catch2/catch.hpp>
#include <exception>
using namespace eosio::vm;
static int check = 0;
static int check2 = 0;
void test_func_a(int a, float b) {
}
void test_func_b(char* a, float* b, int& c) {
}
void test_func_c(float a, const char* b, span<const char> c, int d) {
check = d;
}
void test_func_d() {
}
void test_func_e(float a, const char* b, span<const char> c, int d) {
check = d;
}
template <typename Ex, typename F>
bool check_exception_msg(F&& func, const std::string& chck) {
try {
func();
} catch( Ex ex ) {
return ex.what() == chck;
}
return false;
}
#define CHECK_MSG(Msg, ...) \
CHECK(check_exception_msg<std::runtime_error>([&](){ __VA_ARGS__; }, Msg))
EOS_VM_PRECONDITION(int_equals_42,
EOS_VM_INVOKE_ON(int, [&](auto arg, auto&&... rest) {
if (arg != 42)
throw std::runtime_error("arg != 42");
}));
EOS_VM_PRECONDITION(float_equals_42,
EOS_VM_INVOKE_ON(float, [&](auto arg, auto&&... rest) {
if (arg != 42.42f)
throw std::runtime_error("arg != 42.42f");
}));
EOS_VM_PRECONDITION(str_equals_hello,
EOS_VM_INVOKE_ON(const char*, [&](auto arg, auto&&... rest) {
if (memcmp(arg, "hello", 5) != 0)
throw std::runtime_error("str != hello");
}));
EOS_VM_PRECONDITION(span_and_check,
EOS_VM_INVOKE_ON_ALL([&](auto arg, auto&&... rest) {
if constexpr (is_span_type_v<decltype(arg)>) {
if (memcmp(arg.data(), "hellohe", arg.size_bytes()) != 0)
throw std::runtime_error("span<T> != hellohe");
}
}));
EOS_VM_PRECONDITION(check2_once,
EOS_VM_INVOKE_ONCE([&](auto&&... args) {
check2++;
}));
EOS_VM_PRECONDITION(check2_all,
EOS_VM_INVOKE_ON_ALL([&](auto&&... args) {
check2++;
}));
struct cnv : type_converter<standalone_function_t> {
using type_converter::from_wasm;
using type_converter::type_converter;
EOS_VM_FROM_WASM(char*, (void* ptr)) { return static_cast<char*>(ptr); }
EOS_VM_FROM_WASM(const char*, (void* ptr)) { return static_cast<char*>(ptr); }
EOS_VM_FROM_WASM(float*, (void* ptr)) { return static_cast<float*>(ptr); }
EOS_VM_FROM_WASM(int&, (void* ptr)) { return *static_cast<int*>(ptr); }
};
BACKEND_TEST_CASE("Testing invoke_on", "[preconditions_tests]") {
using rhf_t = registered_host_functions<standalone_function_t, execution_interface, cnv>;
using backend_t = backend<rhf_t, TestType>;
/*
(module
(type (;0;) (func (param i32 i32 i32)))
(type (;1;) (func (param i32 f32)))
(type (;2;) (func (param)))
(type (;3;) (func (param f32 i32 i32 i32 i32)))
(import "env" "test_func_b" (func (;0;) (type 0)))
(import "env" "test_func_a" (func (;1;) (type 1)))
(import "env" "test_func_c" (func (;2;) (type 3)))
(import "env" "test_func_d" (func (;3;) (type 2)))
(import "env" "test_func_e" (func (;4;) (type 3)))
(func (;5;) (type 2)
i32.const 42
f32.const 42.42
call 1)
(func (;6;) (type 2)
i32.const 8208
i32.const 8232
i32.const 8232
call 0)
(func (;7;) (type 2)
i32.const 32
f32.const 42
call 1)
(func (;8;) (type 2)
i32.const 42
f32.const 32
call 1)
(func (;9;) (type 2)
i32.const 0
i32.const 0
i32.const 0
call 0)
(func (;10;) (type 2)
f32.const 42.42
i32.const 8208
i32.const 8208
i32.const 7
i32.const 77
call 2)
(func (;11;) (type 2)
f32.const 42.42
i32.const 8232
i32.const 8232
i32.const 4
i32.const 77
call 2)
(func (;12;) (type 2)
call 3)
(func (;13;) (type 2)
f32.const 42.42
i32.const 8232
i32.const 8232
i32.const 4
i32.const 77
call 4)
(memory (;0;) 1)
(export "call_test_a" (func 5))
(export "call_test_b" (func 6))
(export "call_test_c" (func 7))
(export "call_test_d" (func 8))
(export "call_test_e" (func 9))
(export "call_test_f" (func 10))
(export "call_test_g" (func 11))
(export "call_test_h" (func 12))
(export "call_test_j" (func 13))
(data (i32.const 8208) "hellohello\00")
(data (i32.const 8232) "\ff\ff\ff\ff"))
*/
wasm_code test_wasm = {
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x17, 0x04, 0x60,
0x03, 0x7f, 0x7f, 0x7f, 0x00, 0x60, 0x02, 0x7f, 0x7d, 0x00, 0x60, 0x00,
0x00, 0x60, 0x05, 0x7d, 0x7f, 0x7f, 0x7f, 0x7f, 0x00, 0x02, 0x5b, 0x05,
0x03, 0x65, 0x6e, 0x76, 0x0b, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x66, 0x75,
0x6e, 0x63, 0x5f, 0x62, 0x00, 0x00, 0x03, 0x65, 0x6e, 0x76, 0x0b, 0x74,
0x65, 0x73, 0x74, 0x5f, 0x66, 0x75, 0x6e, 0x63, 0x5f, 0x61, 0x00, 0x01,
0x03, 0x65, 0x6e, 0x76, 0x0b, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x66, 0x75,
0x6e, 0x63, 0x5f, 0x63, 0x00, 0x03, 0x03, 0x65, 0x6e, 0x76, 0x0b, 0x74,
0x65, 0x73, 0x74, 0x5f, 0x66, 0x75, 0x6e, 0x63, 0x5f, 0x64, 0x00, 0x02,
0x03, 0x65, 0x6e, 0x76, 0x0b, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x66, 0x75,
0x6e, 0x63, 0x5f, 0x65, 0x00, 0x03, 0x03, 0x0a, 0x09, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x05, 0x03, 0x01, 0x00, 0x01, 0x07,
0x7f, 0x09, 0x0b, 0x63, 0x61, 0x6c, 0x6c, 0x5f, 0x74, 0x65, 0x73, 0x74,
0x5f, 0x61, 0x00, 0x05, 0x0b, 0x63, 0x61, 0x6c, 0x6c, 0x5f, 0x74, 0x65,
0x73, 0x74, 0x5f, 0x62, 0x00, 0x06, 0x0b, 0x63, 0x61, 0x6c, 0x6c, 0x5f,
0x74, 0x65, 0x73, 0x74, 0x5f, 0x63, 0x00, 0x07, 0x0b, 0x63, 0x61, 0x6c,
0x6c, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x64, 0x00, 0x08, 0x0b, 0x63,
0x61, 0x6c, 0x6c, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x65, 0x00, 0x09,
0x0b, 0x63, 0x61, 0x6c, 0x6c, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x66,
0x00, 0x0a, 0x0b, 0x63, 0x61, 0x6c, 0x6c, 0x5f, 0x74, 0x65, 0x73, 0x74,
0x5f, 0x67, 0x00, 0x0b, 0x0b, 0x63, 0x61, 0x6c, 0x6c, 0x5f, 0x74, 0x65,
0x73, 0x74, 0x5f, 0x68, 0x00, 0x0c, 0x0b, 0x63, 0x61, 0x6c, 0x6c, 0x5f,
0x74, 0x65, 0x73, 0x74, 0x5f, 0x6a, 0x00, 0x0d, 0x0a, 0x8b, 0x01, 0x09,
0x0b, 0x00, 0x41, 0x2a, 0x43, 0x14, 0xae, 0x29, 0x42, 0x10, 0x01, 0x0b,
0x10, 0x00, 0x41, 0x90, 0xc0, 0x00, 0x41, 0xa8, 0xc0, 0x00, 0x41, 0xa8,
0xc0, 0x00, 0x10, 0x00, 0x0b, 0x0b, 0x00, 0x41, 0x20, 0x43, 0x00, 0x00,
0x28, 0x42, 0x10, 0x01, 0x0b, 0x0b, 0x00, 0x41, 0x2a, 0x43, 0x00, 0x00,
0x00, 0x42, 0x10, 0x01, 0x0b, 0x0a, 0x00, 0x41, 0x00, 0x41, 0x00, 0x41,
0x00, 0x10, 0x00, 0x0b, 0x16, 0x00, 0x43, 0x14, 0xae, 0x29, 0x42, 0x41,
0x90, 0xc0, 0x00, 0x41, 0x90, 0xc0, 0x00, 0x41, 0x07, 0x41, 0xcd, 0x00,
0x10, 0x02, 0x0b, 0x16, 0x00, 0x43, 0x14, 0xae, 0x29, 0x42, 0x41, 0xa8,
0xc0, 0x00, 0x41, 0xa8, 0xc0, 0x00, 0x41, 0x04, 0x41, 0xcd, 0x00, 0x10,
0x02, 0x0b, 0x04, 0x00, 0x10, 0x03, 0x0b, 0x16, 0x00, 0x43, 0x14, 0xae,
0x29, 0x42, 0x41, 0xa8, 0xc0, 0x00, 0x41, 0xa8, 0xc0, 0x00, 0x41, 0x04,
0x41, 0xcd, 0x00, 0x10, 0x04, 0x0b, 0x0b, 0x1e, 0x02, 0x00, 0x41, 0x90,
0xc0, 0x00, 0x0b, 0x0b, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x68, 0x65, 0x6c,
0x6c, 0x6f, 0x00, 0x00, 0x41, 0xa8, 0xc0, 0x00, 0x0b, 0x04, 0xff, 0xff,
0xff, 0xff
};
rhf_t::add<&test_func_a, int_equals_42, float_equals_42>("env", "test_func_a");
rhf_t::add<&test_func_b, str_equals_hello>("env", "test_func_b");
rhf_t::add<&test_func_c, float_equals_42, str_equals_hello, span_and_check>("env", "test_func_c");
rhf_t::add<&test_func_d, check2_once>("env", "test_func_d");
rhf_t::add<&test_func_e, check2_all>("env", "test_func_e");
wasm_allocator wa;
backend_t bkend(test_wasm, &wa);
check = 0;
check2 = 0;
bkend("env", "call_test_a");
bkend("env", "call_test_b");
CHECK_MSG("arg != 42", bkend("env", "call_test_c"));
CHECK_MSG("arg != 42.42f", bkend("env", "call_test_d"));
bkend("env", "call_test_e");
CHECK(check == 0);
bkend("env", "call_test_f");
CHECK(check == 77);
// reset check
check = 0;
CHECK_MSG("str != hello", bkend("env", "call_test_g"));
CHECK(check == 0);
bkend("env", "call_test_h");
CHECK(check2 == 1);
bkend("env", "call_test_h");
CHECK(check2 == 2);
check2 = 0;
bkend("env", "call_test_j");
CHECK(check2 == 4);
bkend("env", "call_test_j");
CHECK(check2 == 8);
}