|
15 | 15 | #include "include/proxy-wasm/wasm.h"
|
16 | 16 |
|
17 | 17 | #include "gtest/gtest.h"
|
| 18 | +#include <memory> |
18 | 19 |
|
19 | 20 | #include "test/utility.h"
|
20 | 21 |
|
@@ -111,4 +112,57 @@ TEST_P(TestVM, GetOrCreateThreadLocalWasmFailCallbacks) {
|
111 | 112 | ASSERT_NE(thread_local_plugin3->wasm(), thread_local_plugin2->wasm());
|
112 | 113 | }
|
113 | 114 |
|
| 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 | + |
114 | 168 | } // namespace proxy_wasm
|
0 commit comments