|
| 1 | +// Copyright 2020 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "gtest/gtest.h" |
| 16 | + |
| 17 | +#include <memory> |
| 18 | +#include <string> |
| 19 | +#include <vector> |
| 20 | + |
| 21 | +#include "include/proxy-wasm/wasm_vm.h" |
| 22 | + |
| 23 | +#include "test/utility.h" |
| 24 | + |
| 25 | +namespace proxy_wasm { |
| 26 | +namespace { |
| 27 | + |
| 28 | +std::vector<std::string> getDynEngines() { |
| 29 | + std::vector<std::string> engines = { |
| 30 | +#if defined(PROXY_WASM_HOST_ENGINE_DYN) |
| 31 | + "dyn", |
| 32 | +#endif |
| 33 | + "" |
| 34 | + }; |
| 35 | + engines.pop_back(); |
| 36 | + return engines; |
| 37 | +} |
| 38 | + |
| 39 | +INSTANTIATE_TEST_SUITE_P(WasmEngines, TestVm, testing::ValuesIn(getDynEngines()), |
| 40 | + [](const testing::TestParamInfo<std::string> &info) { |
| 41 | + return info.param; |
| 42 | + }); |
| 43 | + |
| 44 | +TEST_P(TestVm, Basic) { |
| 45 | + EXPECT_EQ(vm_->getEngineName(), engine_); |
| 46 | + EXPECT_EQ(vm_->getEngineName(), "dyn"); |
| 47 | + EXPECT_EQ(vm_->cloneable(), proxy_wasm::Cloneable::InstantiatedModule); |
| 48 | +} |
| 49 | + |
| 50 | +TEST_P(TestVm, Memory) { |
| 51 | + auto source = readTestWasmFile("abi_export.so"); |
| 52 | + ASSERT_TRUE(vm_->load(source, {}, {})); |
| 53 | + ASSERT_TRUE(vm_->link("")); |
| 54 | + |
| 55 | + uint64_t raw_word = 0; |
| 56 | + |
| 57 | + Word word; |
| 58 | + ASSERT_TRUE(vm_->setWord(reinterpret_cast<uint64_t>(&raw_word), Word(100))); |
| 59 | + ASSERT_TRUE(vm_->getWord(reinterpret_cast<uint64_t>(&raw_word), &word)); |
| 60 | + ASSERT_EQ(100, word.u64_); |
| 61 | + ASSERT_EQ(100, raw_word); |
| 62 | + |
| 63 | + uint32_t raw_data[2] = {0, 0}; |
| 64 | + |
| 65 | + uint32_t data[2] = {htowasm(static_cast<uint32_t>(-1), vm_->usesWasmByteOrder()), |
| 66 | + htowasm(200, vm_->usesWasmByteOrder())}; |
| 67 | + ASSERT_TRUE(vm_->setMemory(reinterpret_cast<uint64_t>(&raw_data[0]), sizeof(int32_t) * 2, |
| 68 | + static_cast<void *>(data))); |
| 69 | + ASSERT_TRUE(vm_->getWord(reinterpret_cast<uint64_t>(&raw_data[0]), &word)); |
| 70 | + ASSERT_EQ(-1, static_cast<int32_t>(word.u64_)); |
| 71 | + ASSERT_TRUE(vm_->getWord(reinterpret_cast<uint64_t>(&raw_data[1]), &word)); |
| 72 | + ASSERT_EQ(200, static_cast<int32_t>(word.u64_)); |
| 73 | +} |
| 74 | + |
| 75 | +TEST_P(TestVm, ReadWrite) { |
| 76 | + auto source = readTestWasmFile("abi_export.so"); |
| 77 | + ASSERT_TRUE(vm_->load(source, {}, {})); |
| 78 | + ASSERT_TRUE(vm_->link("")); |
| 79 | + BufferBase buffer; |
| 80 | + buffer.set("test data"); |
| 81 | + uint64_t buffer_raw = 0; |
| 82 | + uint64_t buffer_len = 0; |
| 83 | + auto wasm = TestWasm(std::move(vm_), {}); |
| 84 | + ASSERT_TRUE(wasm.initialize()); |
| 85 | + ASSERT_EQ(buffer.copyTo(&wasm, 0, buffer.size(), reinterpret_cast<uint64_t>(&buffer_raw), |
| 86 | + reinterpret_cast<uint64_t>(&buffer_len)), |
| 87 | + WasmResult::Ok); |
| 88 | + |
| 89 | + ASSERT_NE(buffer_raw, 0); |
| 90 | + ASSERT_NE(buffer_len, 0); |
| 91 | + |
| 92 | + char *buffer_ptr = reinterpret_cast<char *>(buffer_raw); |
| 93 | + ASSERT_EQ(buffer_len, buffer.size()); |
| 94 | + std::string buffer_str(buffer_ptr, buffer_ptr + buffer_len); |
| 95 | + ASSERT_EQ(buffer_str, "test data"); |
| 96 | +} |
| 97 | + |
| 98 | +} // namespace |
| 99 | +} // namespace proxy_wasm |
0 commit comments