@@ -114,27 +114,18 @@ TEST_P(TestVm, GetOrCreateThreadLocalWasmFailCallbacks) {
114
114
}
115
115
116
116
// Tests the canary is always applied when making a call `createWasm`
117
- TEST_P (TestVm, AlwaysApplyCanaryForDifferentRootID ) {
117
+ TEST_P (TestVm, AlwaysApplyCanary ) {
118
118
// Use different root_id, but the others are the same
119
- const auto *const root_id_1 = " root_id_1" ;
120
- const auto *const root_id_2 = " root_id_2" ;
121
- const auto *const plugin_name = " plugin_name" ;
122
- const auto *const vm_id = " vm_id" ;
123
- const auto *const vm_config = " vm_config" ;
124
- const auto *const plugin_config = " plugin_config" ;
125
- const auto *const vm_key = " vm_key" ;
126
- const auto *const plugin_key = " plugin_key" ;
119
+ const std::string root_ids[2 ] = {" root_id_1" , " root_id_2" };
120
+ const std::string plugin_names[2 ] = {" plugin_name_1" , " plugin_name_2" };
121
+ const std::string vm_ids[2 ] = {" vm_id_1" , " vm_id_2" };
122
+ const std::string vm_configs[2 ] = {" vm_config_1" , " vm_config_2" };
123
+ const std::string plugin_configs[2 ] = {" plugin_config_1" , " plugin_config_2" };
124
+ const std::string vm_keys[2 ] = {" vm_key_1" , " vm_key_2" };
125
+ const std::string plugin_keys[2 ] = {" plugin_key_1" , " plugin_key_2" };
127
126
const auto fail_open = false ;
128
127
129
- // Define callbacks.
130
- WasmHandleFactory wasm_handle_factory =
131
- [this , vm_id, vm_config](std::string_view vm_key) -> std::shared_ptr<WasmHandleBase> {
132
- auto base_wasm = std::make_shared<TestWasm>(newVm (), vm_id, vm_config, vm_key,
133
- std::unordered_map<std::string, std::string>{},
134
- AllowedCapabilitiesMap{});
135
- return std::make_shared<WasmHandleBase>(base_wasm);
136
- };
137
-
128
+ // Define common callbacks
138
129
WasmHandleCloneFactory wasm_handle_clone_factory =
139
130
[this ](const std::shared_ptr<WasmHandleBase> &base_wasm_handle)
140
131
-> std::shared_ptr<WasmHandleBase> {
@@ -165,34 +156,80 @@ TEST_P(TestVm, AlwaysApplyCanaryForDifferentRootID) {
165
156
// Read the minimal loadable binary.
166
157
auto source = readTestWasmFile (" configure_check.wasm" );
167
158
168
- // Create a first plugin.
169
- const auto plugin_1 = std::make_shared<PluginBase>(plugin_name, root_id_1, vm_id, engine_,
170
- plugin_config, fail_open, plugin_key);
171
- // Create a base Wasm by createWasm.
172
- auto base_wasm_handle_1 = createWasm (vm_key, source, plugin_1, wasm_handle_factory,
173
- wasm_handle_clone_factory_for_canary, false );
174
- ASSERT_TRUE (base_wasm_handle_1 && base_wasm_handle_1->wasm ());
175
-
176
- // Check if it ran for root context 1
177
- EXPECT_TRUE (root_context_in_canary->isLogged (" onConfigure in TestRootContext1" ));
159
+ WasmHandleFactory wasm_handle_factory_baseline =
160
+ [this , vm_ids, vm_configs](std::string_view vm_key) -> std::shared_ptr<WasmHandleBase> {
161
+ auto base_wasm = std::make_shared<TestWasm>(newVm (), vm_ids[0 ], vm_configs[0 ], vm_key,
162
+ std::unordered_map<std::string, std::string>{},
163
+ AllowedCapabilitiesMap{});
164
+ return std::make_shared<WasmHandleBase>(base_wasm);
165
+ };
178
166
179
- // Create a first plugin.
180
- const auto plugin_2 = std::make_shared<PluginBase>(plugin_name, root_id_2, vm_id, engine_,
181
- plugin_config, fail_open, plugin_key);
167
+ // Create a baseline plugin.
168
+ const auto plugin_baseline =
169
+ std::make_shared<PluginBase>(plugin_names[0 ], root_ids[0 ], vm_ids[0 ], engine_,
170
+ plugin_configs[0 ], fail_open, plugin_keys[0 ]);
182
171
// Create a base Wasm by createWasm.
183
- auto base_wasm_handle_2 = createWasm (vm_key, source, plugin_2, wasm_handle_factory,
184
- wasm_handle_clone_factory_for_canary, false );
185
- ASSERT_TRUE (base_wasm_handle_2 && base_wasm_handle_2->wasm ());
186
-
187
- // Check if it ran for root context 2
188
- EXPECT_TRUE (root_context_in_canary->isLogged (" onConfigure in TestRootContext2" ));
172
+ auto wasm_handle_baseline =
173
+ createWasm (vm_keys[0 ], source, plugin_baseline, wasm_handle_factory_baseline,
174
+ wasm_handle_clone_factory_for_canary, false );
175
+ ASSERT_TRUE (wasm_handle_baseline && wasm_handle_baseline->wasm ());
189
176
190
- // Base Wasm should be equal, because the same vm_key is used.
191
- EXPECT_EQ (base_wasm_handle_1->wasm (), base_wasm_handle_2->wasm ());
192
- // Plugin key should be different with each other, because root_ids are different.
193
- EXPECT_NE (plugin_1->key (), plugin_2->key ());
177
+ // Check if it ran for baseline root context
178
+ EXPECT_TRUE (root_context_in_canary->isLogged (" onConfigure: " + root_ids[0 ]));
194
179
// For each create Wasm, canary should be done.
195
- EXPECT_EQ (canary_count, 2 );
180
+ EXPECT_EQ (canary_count, 1 );
181
+
182
+ for (const auto &root_id : root_ids) {
183
+ for (const auto &plugin_name : plugin_names) {
184
+ for (const auto &vm_id : vm_ids) {
185
+ for (const auto &vm_config : vm_configs) {
186
+ for (const auto &plugin_config : plugin_configs) {
187
+ for (const auto &vm_key : vm_keys) {
188
+ for (const auto &plugin_key : plugin_keys) {
189
+ canary_count = 0 ;
190
+ WasmHandleFactory wasm_handle_factory_comp =
191
+ [this , vm_id,
192
+ vm_config](std::string_view vm_key) -> std::shared_ptr<WasmHandleBase> {
193
+ auto base_wasm = std::make_shared<TestWasm>(
194
+ newVm (), vm_id, vm_config, vm_key,
195
+ std::unordered_map<std::string, std::string>{}, AllowedCapabilitiesMap{});
196
+ return std::make_shared<WasmHandleBase>(base_wasm);
197
+ };
198
+ const auto plugin_comp = std::make_shared<PluginBase>(
199
+ plugin_name, root_id, vm_id, engine_, plugin_config, fail_open, plugin_key);
200
+ // Create a base Wasm by createWasm.
201
+ auto wasm_handle_comp =
202
+ createWasm (vm_key, source, plugin_comp, wasm_handle_factory_comp,
203
+ wasm_handle_clone_factory_for_canary, false );
204
+ ASSERT_TRUE (wasm_handle_comp && wasm_handle_comp->wasm ());
205
+
206
+ // Check if it ran for root context 2.
207
+ EXPECT_TRUE (root_context_in_canary->isLogged (" onConfigure: " + root_id));
208
+
209
+ // Wasm VM is unique for vm_key.
210
+ if (vm_key == vm_keys[0 ]) {
211
+ EXPECT_EQ (wasm_handle_baseline->wasm (), wasm_handle_comp->wasm ());
212
+ } else {
213
+ EXPECT_NE (wasm_handle_baseline->wasm (), wasm_handle_comp->wasm ());
214
+ }
215
+
216
+ // plugin->key() is unique for root_id + plugin_config + plugin_key.
217
+ // plugin->key() is used as an identifier of local-specific plugins as well.
218
+ if (root_id == root_ids[0 ] && plugin_config == plugin_configs[0 ] &&
219
+ plugin_key == plugin_keys[0 ]) {
220
+ EXPECT_EQ (plugin_baseline->key (), plugin_comp->key ());
221
+ } else {
222
+ EXPECT_NE (plugin_baseline->key (), plugin_comp->key ());
223
+ }
224
+ // For each create Wasm, canary should be done.
225
+ EXPECT_EQ (canary_count, 1 );
226
+ }
227
+ }
228
+ }
229
+ }
230
+ }
231
+ }
232
+ }
196
233
}
197
234
198
- } // namespace proxy_wasm
235
+ } // namespace proxy_wasm
0 commit comments