forked from proxy-wasm/proxy-wasm-cpp-host
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime_test.cc
191 lines (156 loc) · 6.06 KB
/
runtime_test.cc
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
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "gtest/gtest.h"
#include <fstream>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include <vector>
#include "include/proxy-wasm/context.h"
#include "include/proxy-wasm/wasm.h"
#include "test/utility.h"
namespace proxy_wasm {
namespace {
auto test_values = testing::ValuesIn(getRuntimes());
INSTANTIATE_TEST_SUITE_P(Runtimes, TestVM, test_values);
TEST_P(TestVM, Basic) {
EXPECT_EQ(wasmVm()->cloneable(), proxy_wasm::Cloneable::CompiledBytecode);
EXPECT_EQ(wasmVm()->runtime(), runtime());
}
TEST_P(TestVM, ABIVersion) {
initialize(readTestWasmFile("abi_export.wasm"));
ASSERT_EQ(wasmVm()->getAbiVersion(), AbiVersion::ProxyWasm_0_2_0);
}
TEST_P(TestVM, Memory) {
initialize(readTestWasmFile("abi_export.wasm"));
Word word;
ASSERT_TRUE(wasmVm()->setWord(0x2000, Word(100)));
ASSERT_TRUE(wasmVm()->getWord(0x2000, &word));
ASSERT_EQ(100, word.u64_);
int32_t data[2] = {-1, 200};
ASSERT_TRUE(wasmVm()->setMemory(0x200, sizeof(int32_t) * 2, static_cast<void *>(data)));
ASSERT_TRUE(wasmVm()->getWord(0x200, &word));
ASSERT_EQ(-1, static_cast<int32_t>(word.u64_));
ASSERT_TRUE(wasmVm()->getWord(0x204, &word));
ASSERT_EQ(200, static_cast<int32_t>(word.u64_));
}
TEST_P(TestVM, Clone) {
initialize(readTestWasmFile("abi_export.wasm"));
const auto address = 0x2000;
Word word;
{
auto clone = wasmVm()->clone();
ASSERT_TRUE(clone != nullptr);
ASSERT_NE(wasmVm(), clone.get());
ASSERT_TRUE(clone->link(""));
ASSERT_TRUE(clone->setWord(address, Word(100)));
ASSERT_TRUE(clone->getWord(address, &word));
ASSERT_EQ(100, word.u64_);
}
// check memory arrays are not overrapped
ASSERT_TRUE(wasmVm()->getWord(address, &word));
ASSERT_NE(100, word.u64_);
}
class TestContext : public ContextBase {
public:
TestContext(){};
void increment() { counter++; }
int64_t counter = 0;
};
void nopCallback(void *raw_context) {}
void callback(void *raw_context) {
TestContext *context = static_cast<TestContext *>(raw_context);
context->increment();
}
Word callback2(void *raw_context, Word val) { return val + 100; }
TEST_P(TestVM, StraceLogLevel) {
ASSERT_TRUE(wasmVm()->load(readTestWasmFile("callback.wasm"), false));
wasmVm()->registerCallback(
"env", "callback", &nopCallback,
&ConvertFunctionWordToUint32<decltype(nopCallback),
nopCallback>::convertFunctionWordToUint32);
wasmVm()->registerCallback(
"env", "callback2", &callback2,
&ConvertFunctionWordToUint32<decltype(callback2), callback2>::convertFunctionWordToUint32);
ASSERT_TRUE(wasmVm()->link(""));
WasmCallVoid<0> run;
wasmVm()->getFunction("run", &run);
run(nullptr);
// no trace message found since DummyIntegration's log_level_ defaults to LogLevel::info
EXPECT_EQ(integration()->trace_message_, "");
integration()->log_level_ = LogLevel::trace;
run(nullptr);
EXPECT_NE(integration()->trace_message_, "");
}
TEST_P(TestVM, Callback) {
ASSERT_TRUE(wasmVm()->load(readTestWasmFile("callback.wasm"), false));
TestContext context;
current_context_ = &context;
wasmVm()->registerCallback(
"env", "callback", &callback,
&ConvertFunctionWordToUint32<decltype(callback), callback>::convertFunctionWordToUint32);
wasmVm()->registerCallback(
"env", "callback2", &callback2,
&ConvertFunctionWordToUint32<decltype(callback2), callback2>::convertFunctionWordToUint32);
ASSERT_TRUE(wasmVm()->link(""));
WasmCallVoid<0> run;
wasmVm()->getFunction("run", &run);
EXPECT_TRUE(run != nullptr);
for (auto i = 0; i < 100; i++) {
run(current_context_);
}
ASSERT_EQ(context.counter, 100);
WasmCallWord<1> run2;
wasmVm()->getFunction("run2", &run2);
Word res = run2(current_context_, Word{0});
ASSERT_EQ(res.u32(), 100100); // 10000 (global) + 100(in callback)
}
TEST_P(TestVM, Trap) {
ASSERT_TRUE(wasmVm()->load(readTestWasmFile("trap.wasm"), false));
ASSERT_TRUE(wasmVm()->link(""));
WasmCallVoid<0> trigger;
wasmVm()->getFunction("trigger", &trigger);
EXPECT_TRUE(trigger != nullptr);
trigger(current_context_);
std::string exp_message = "Function: trigger failed";
ASSERT_TRUE(integration()->error_message_.find(exp_message) != std::string::npos);
WasmCallWord<1> trigger2;
wasmVm()->getFunction("trigger2", &trigger2);
EXPECT_TRUE(trigger2 != nullptr);
trigger2(current_context_, 0);
exp_message = "Function: trigger2 failed:";
ASSERT_TRUE(integration()->error_message_.find(exp_message) != std::string::npos);
}
TEST_P(TestVM, WithPrecompiledSection) {
// Verify that stripping precompile_* custom section works.
auto source = readTestWasmFile("abi_export.wasm");
// Append precompiled_test section
std::vector<char> custom_section = {// custom section id
0x00,
// section length
0x13,
// name length
0x10,
// name = precompiled_test
0x70, 0x72, 0x65, 0x63, 0x6f, 0x6d, 0x70, 0x69, 0x6c, 0x65,
0x64, 0x5f, 0x74, 0x65, 0x73, 0x74,
// content
0x01, 0x01};
source.append(custom_section.data(), custom_section.size());
ASSERT_TRUE(wasmVm()->load(source, false));
ASSERT_EQ(wasmVm()->getAbiVersion(), AbiVersion::ProxyWasm_0_2_0);
}
} // namespace
} // namespace proxy_wasm