Skip to content

Commit 43adb6d

Browse files
author
chaoqin-li1123
committed
add tests
Signed-off-by: chaoqin-li1123 <[email protected]>
1 parent 9ec1f94 commit 43adb6d

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

test/runtime_test.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,32 @@ TEST_P(TestVM, StraceLogLevel) {
137137
EXPECT_NE(integration->trace_message_, "");
138138
}
139139

140+
TEST_P(TestVM, BadExportFunction) {
141+
auto source = readTestWasmFile("callback.wasm");
142+
ASSERT_TRUE(vm_->load(source, {}, {}));
143+
144+
TestContext context;
145+
vm_->registerCallback(
146+
"env", "callback", &callback,
147+
&ConvertFunctionWordToUint32<decltype(callback), callback>::convertFunctionWordToUint32);
148+
ASSERT_TRUE(vm_->link(""));
149+
150+
WasmCallVoid<0> run;
151+
vm_->getFunction("non-existent", &run);
152+
EXPECT_TRUE(run == nullptr);
153+
154+
WasmCallWord<2> bad_signature_run;
155+
vm_->getFunction("run", &bad_signature_run);
156+
EXPECT_TRUE(bad_signature_run == nullptr);
157+
158+
vm_->getFunction("run", &run);
159+
EXPECT_TRUE(run != nullptr);
160+
for (auto i = 0; i < 100; i++) {
161+
run(&context);
162+
}
163+
ASSERT_EQ(context.counter, 100);
164+
}
165+
140166
TEST_P(TestVM, Callback) {
141167
auto source = readTestWasmFile("callback.wasm");
142168
ASSERT_TRUE(vm_->load(source, {}, {}));

test/wasm_test.cc

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "include/proxy-wasm/wasm.h"
1616

1717
#include "gtest/gtest.h"
18+
#include <memory>
1819

1920
#include "test/utility.h"
2021

@@ -111,4 +112,57 @@ TEST_P(TestVM, GetOrCreateThreadLocalWasmFailCallbacks) {
111112
ASSERT_NE(thread_local_plugin3->wasm(), thread_local_plugin2->wasm());
112113
}
113114

115+
TEST_P(TestVM, DifferentRootContextsFromDifferentPluginKeys) {
116+
const std::string plugin_name = "plugin_name";
117+
const std::string root_id = "root_id";
118+
const std::string vm_id = "vm_id";
119+
const std::string vm_config = "vm_config";
120+
const std::string plugin_config = "plugin_config";
121+
const bool fail_open = false;
122+
123+
const std::shared_ptr<PluginBase> plugin1 = std::make_shared<PluginBase>(
124+
plugin_name, root_id, vm_id, runtime_, plugin_config, fail_open, "plugin1_key");
125+
126+
// Define callbacks.
127+
WasmHandleFactory wasm_handle_factory =
128+
[this, vm_id, vm_config](std::string_view vm_key) -> std::shared_ptr<WasmHandleBase> {
129+
auto base_wasm = std::make_shared<WasmBase>(newVm(), vm_id, vm_config, vm_key,
130+
std::unordered_map<std::string, std::string>{},
131+
AllowedCapabilitiesMap{});
132+
return std::make_shared<WasmHandleBase>(base_wasm);
133+
};
134+
135+
WasmHandleCloneFactory wasm_handle_clone_factory =
136+
[this](std::shared_ptr<WasmHandleBase> base_wasm_handle) -> std::shared_ptr<WasmHandleBase> {
137+
std::shared_ptr<WasmBase> wasm = std::make_shared<WasmBase>(
138+
base_wasm_handle, [this]() -> std::unique_ptr<WasmVm> { return newVm(); });
139+
return std::make_shared<WasmHandleBase>(wasm);
140+
};
141+
142+
// Read the minimal loadable binary.
143+
std::string source = readTestWasmFile("abi_export.wasm");
144+
145+
// Create base Wasm via createWasm.
146+
std::shared_ptr<WasmHandleBase> base_wasm_handle =
147+
createWasm("vm_key", source, plugin1, wasm_handle_factory, wasm_handle_clone_factory, false);
148+
EXPECT_TRUE(getThreadLocalWasm(vm_id) == base_wasm_handle);
149+
ContextBase *root_context1 = base_wasm_handle->wasm()->getRootContext(plugin1, false);
150+
EXPECT_TRUE(root_context1 != nullptr);
151+
152+
// Create a new plugin with different key.
153+
const std::shared_ptr<PluginBase> plugin2 = std::make_shared<PluginBase>(
154+
plugin_name, root_id, vm_id, runtime_, plugin_config, fail_open, "plugin2_key");
155+
EXPECT_TRUE(base_wasm_handle->wasm()->getRootContext(plugin2, false) == nullptr);
156+
157+
// Create context from a plugin2.
158+
base_wasm_handle->wasm()->createContext(plugin2);
159+
ContextBase *root_context2 = base_wasm_handle->wasm()->getRootContext(plugin2, false);
160+
EXPECT_TRUE(root_context2 != nullptr);
161+
162+
// Verify that the 2 root contexts are different contexts with the same root id.
163+
EXPECT_TRUE(root_context1->isRootContext() && root_context2->isRootContext());
164+
EXPECT_TRUE(root_context1 != root_context2);
165+
EXPECT_TRUE(root_context1->root_id() == root_context2->root_id());
166+
}
167+
114168
} // namespace proxy_wasm

0 commit comments

Comments
 (0)