|
| 1 | +// Copyright 2026 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 | +#include "gmock/gmock.h" |
| 17 | + |
| 18 | +#include <optional> |
| 19 | + |
| 20 | +#include "include/proxy-wasm/context.h" |
| 21 | +#include "include/proxy-wasm/wasm.h" |
| 22 | + |
| 23 | +#include "test/utility.h" |
| 24 | + |
| 25 | +namespace proxy_wasm { |
| 26 | +namespace { |
| 27 | + |
| 28 | +class ArgPassingContext : public TestContext { |
| 29 | +public: |
| 30 | + using TestContext::TestContext; |
| 31 | + WasmResult getHeaderMapPairs(WasmHeaderMapType /* type */, Pairs * /* result */) override { |
| 32 | + return static_cast<WasmResult>(3333333333U); |
| 33 | + } |
| 34 | +}; |
| 35 | + |
| 36 | +class ArgPassingWasm : public TestWasm { |
| 37 | +public: |
| 38 | + using TestWasm::TestWasm; |
| 39 | + ContextBase *createVmContext() override { return new ArgPassingContext(this); }; |
| 40 | +}; |
| 41 | + |
| 42 | +class ArgPassingTest : public TestVm { |
| 43 | +public: |
| 44 | + void SetUp() { |
| 45 | + auto source = readTestWasmFile("arg_passing.wasm"); |
| 46 | + ASSERT_FALSE(source.empty()); |
| 47 | + wasm_.emplace(std::move(vm_)); |
| 48 | + ASSERT_TRUE(wasm_->load(source, false)); |
| 49 | + ASSERT_TRUE(wasm_->initialize()); |
| 50 | + context_ = dynamic_cast<ArgPassingContext *>(wasm_->vm_context()); |
| 51 | + ASSERT_NE(context_, nullptr); |
| 52 | + } |
| 53 | + |
| 54 | + std::optional<ArgPassingWasm> wasm_; |
| 55 | + ArgPassingContext *context_; |
| 56 | +}; |
| 57 | + |
| 58 | +INSTANTIATE_TEST_SUITE_P(WasmEngines, ArgPassingTest, testing::ValuesIn(getWasmEngines()), |
| 59 | + [](const testing::TestParamInfo<std::string> &info) { |
| 60 | + return info.param; |
| 61 | + }); |
| 62 | + |
| 63 | +TEST_P(ArgPassingTest, WasmCallReturnsWordValue) { |
| 64 | + WasmCallWord<0> test_return_u32; |
| 65 | + wasm_->wasm_vm()->getFunction("test_return_u32", &test_return_u32); |
| 66 | + |
| 67 | + EXPECT_EQ(test_return_u32(context_), 3333333333U) << context_->getLog(); |
| 68 | +} |
| 69 | + |
| 70 | +TEST_P(ArgPassingTest, WasmCallReturnsLongValue) { |
| 71 | + WasmCall_lf test_return_u64; |
| 72 | + wasm_->wasm_vm()->getFunction("test_return_u64", &test_return_u64); |
| 73 | + |
| 74 | + EXPECT_EQ(test_return_u64(context_, 1.0), 11111111111111111111UL) << context_->getLog(); |
| 75 | +} |
| 76 | + |
| 77 | +TEST_P(ArgPassingTest, WasmCallReturnsFloatValue) { |
| 78 | + WasmCall_fff test_return_f32; |
| 79 | + wasm_->wasm_vm()->getFunction("test_return_f32", &test_return_f32); |
| 80 | + |
| 81 | + EXPECT_THAT(test_return_f32(context_, 1.0, 1.0), |
| 82 | + testing::AllOf(testing::Lt(1112.0), testing::Gt(1110.0))) |
| 83 | + << context_->getLog(); |
| 84 | +} |
| 85 | + |
| 86 | +TEST_P(ArgPassingTest, WasmCallReturnsDoubleValue) { |
| 87 | + WasmCall_dfff test_return_f64; |
| 88 | + wasm_->wasm_vm()->getFunction("test_return_f64", &test_return_f64); |
| 89 | + |
| 90 | + EXPECT_THAT(test_return_f64(context_, 1.0, 1.0, 1.0), |
| 91 | + testing::AllOf(testing::Lt(1111111112.0), testing::Gt(1111111110.0))) |
| 92 | + << context_->getLog(); |
| 93 | +} |
| 94 | + |
| 95 | +TEST_P(ArgPassingTest, HostCallReturnsWordValue) { |
| 96 | + WasmCallWord<0> test_host_return; |
| 97 | + wasm_->wasm_vm()->getFunction("test_host_return", &test_host_return); |
| 98 | + |
| 99 | + EXPECT_TRUE(test_host_return(context_)) << context_->getLog(); |
| 100 | +} |
| 101 | + |
| 102 | +TEST_P(ArgPassingTest, HostPassesPrimitiveValues) { |
| 103 | + WasmCall_WWlfd test_primitives; |
| 104 | + wasm_->wasm_vm()->getFunction("test_primitives", &test_primitives); |
| 105 | + |
| 106 | + ASSERT_TRUE(test_primitives(context_, 3333333333U, 11111111111111111111UL, 1111, 1111111111)) |
| 107 | + << context_->getLog(); |
| 108 | +} |
| 109 | + |
| 110 | +TEST_P(ArgPassingTest, HostPassesNegativePrimitiveValues) { |
| 111 | + WasmCall_WWlfd test_negative_primitives; |
| 112 | + wasm_->wasm_vm()->getFunction("test_negative_primitives", &test_negative_primitives); |
| 113 | + |
| 114 | + ASSERT_TRUE( |
| 115 | + test_negative_primitives(context_, -1111111111, -1111111111111111111, -1111, -1111111111)) |
| 116 | + << context_->getLog(); |
| 117 | +} |
| 118 | + |
| 119 | +TEST_P(ArgPassingTest, HostReadsPointersToWasmMemory) { |
| 120 | + WasmCallWord<0> test_buffer_from_wasm; |
| 121 | + wasm_->wasm_vm()->getFunction("test_buffer_from_wasm", &test_buffer_from_wasm); |
| 122 | + |
| 123 | + ASSERT_TRUE(test_buffer_from_wasm(context_)) << context_->getLog(); |
| 124 | + |
| 125 | + context_->isLogged("hello from wasm land!"); |
| 126 | +} |
| 127 | + |
| 128 | +TEST_P(ArgPassingTest, WasmCallReadsBufferPassedByHost) { |
| 129 | + context_->setBuffer(0, "hello from host land!"); |
| 130 | + WasmCallWord<0> test_buffer_from_host; |
| 131 | + wasm_->wasm_vm()->getFunction("test_buffer_from_host", &test_buffer_from_host); |
| 132 | + |
| 133 | + ASSERT_TRUE(test_buffer_from_host(context_)) << context_->getLog(); |
| 134 | +} |
| 135 | + |
| 136 | +} // namespace |
| 137 | +} // namespace proxy_wasm |
0 commit comments