|
| 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 EndiannessContext : public TestContext { |
| 29 | +public: |
| 30 | + using TestContext::TestContext; |
| 31 | + WasmResult getHeaderMapPairs(WasmHeaderMapType /* type */, Pairs * /* result */) override { |
| 32 | + // GetHeaderMapPairs passes this value as the hostcall return value as |
| 33 | + // opposed to output parameter. |
| 34 | + return static_cast<WasmResult>(3333333333U); |
| 35 | + } |
| 36 | +}; |
| 37 | + |
| 38 | +class EndiannessWasm : public TestWasm { |
| 39 | +public: |
| 40 | + using TestWasm::TestWasm; |
| 41 | + ContextBase *createVmContext() override { return new EndiannessContext(this); }; |
| 42 | +}; |
| 43 | + |
| 44 | +class EndiannessTest : public TestVm { |
| 45 | +public: |
| 46 | + void SetUp() { |
| 47 | + auto source = readTestWasmFile("endianness.wasm"); |
| 48 | + ASSERT_FALSE(source.empty()); |
| 49 | + wasm_.emplace(std::move(vm_)); |
| 50 | + ASSERT_TRUE(wasm_->load(source, false)); |
| 51 | + ASSERT_TRUE(wasm_->initialize()); |
| 52 | + context_ = dynamic_cast<EndiannessContext *>(wasm_->vm_context()); |
| 53 | + ASSERT_NE(context_, nullptr); |
| 54 | + } |
| 55 | + |
| 56 | + std::optional<EndiannessWasm> wasm_; |
| 57 | + EndiannessContext *context_; |
| 58 | +}; |
| 59 | + |
| 60 | +INSTANTIATE_TEST_SUITE_P(WasmEngines, EndiannessTest, testing::ValuesIn(getWasmEngines()), |
| 61 | + [](const testing::TestParamInfo<std::string> &info) { |
| 62 | + return info.param; |
| 63 | + }); |
| 64 | + |
| 65 | +TEST_P(EndiannessTest, WasmCallReturnsWordValue) { |
| 66 | + WasmCallWord<0> test_return_u32; |
| 67 | + wasm_->wasm_vm()->getFunction("test_return_u32", &test_return_u32); |
| 68 | + |
| 69 | + EXPECT_EQ(test_return_u32(context_).u32(), 3333333333U) << context_->getLog(); |
| 70 | +} |
| 71 | + |
| 72 | +TEST_P(EndiannessTest, WasmCallReturnsNegativeWordValue) { |
| 73 | + WasmCallWord<0> test_return_i32; |
| 74 | + wasm_->wasm_vm()->getFunction("test_return_i32", &test_return_i32); |
| 75 | + |
| 76 | + EXPECT_EQ(test_return_i32(context_).u32(), -1111111111) << context_->getLog(); |
| 77 | +} |
| 78 | + |
| 79 | +TEST_P(EndiannessTest, WasmCallReturnsUnsignedLongValue) { |
| 80 | + WasmCall_lf test_return_u64; |
| 81 | + wasm_->wasm_vm()->getFunction("test_return_u64", &test_return_u64); |
| 82 | + |
| 83 | + EXPECT_EQ(test_return_u64(context_, 1.0), 11111111111111111111UL) << context_->getLog(); |
| 84 | +} |
| 85 | + |
| 86 | +TEST_P(EndiannessTest, WasmCallReturnsNegativeLongValue) { |
| 87 | + WasmCall_lf test_return_i64; |
| 88 | + wasm_->wasm_vm()->getFunction("test_return_i64", &test_return_i64); |
| 89 | + |
| 90 | + EXPECT_EQ(test_return_i64(context_, 1.0), -111111111111111111L) << context_->getLog(); |
| 91 | +} |
| 92 | + |
| 93 | +TEST_P(EndiannessTest, WasmCallReturnsFloatValue) { |
| 94 | + WasmCall_fff test_return_f32; |
| 95 | + wasm_->wasm_vm()->getFunction("test_return_f32", &test_return_f32); |
| 96 | + |
| 97 | + EXPECT_THAT(test_return_f32(context_, 1.0, 1.0), |
| 98 | + testing::AllOf(testing::Lt(1112.0), testing::Gt(1110.0))) |
| 99 | + << context_->getLog(); |
| 100 | +} |
| 101 | + |
| 102 | +TEST_P(EndiannessTest, WasmCallReturnsDoubleValue) { |
| 103 | + WasmCall_dfff test_return_f64; |
| 104 | + wasm_->wasm_vm()->getFunction("test_return_f64", &test_return_f64); |
| 105 | + |
| 106 | + EXPECT_THAT(test_return_f64(context_, 1.0, 1.0, 1.0), |
| 107 | + testing::AllOf(testing::Lt(1111111112.0), testing::Gt(1111111110.0))) |
| 108 | + << context_->getLog(); |
| 109 | +} |
| 110 | + |
| 111 | +TEST_P(EndiannessTest, HostCallReturnsWordValue) { |
| 112 | + WasmCallWord<0> test_host_return; |
| 113 | + wasm_->wasm_vm()->getFunction("test_host_return", &test_host_return); |
| 114 | + |
| 115 | + EXPECT_TRUE(test_host_return(context_)) << context_->getLog(); |
| 116 | +} |
| 117 | + |
| 118 | +TEST_P(EndiannessTest, HostPassesPrimitiveValues) { |
| 119 | + WasmCall_WWlfd test_primitives; |
| 120 | + wasm_->wasm_vm()->getFunction("test_primitives", &test_primitives); |
| 121 | + |
| 122 | + ASSERT_TRUE(test_primitives(context_, 3333333333U, 11111111111111111111UL, 1111, 1111111111)) |
| 123 | + << context_->getLog(); |
| 124 | +} |
| 125 | + |
| 126 | +TEST_P(EndiannessTest, HostPassesNegativePrimitiveValues) { |
| 127 | + WasmCall_WWlfd test_negative_primitives; |
| 128 | + wasm_->wasm_vm()->getFunction("test_negative_primitives", &test_negative_primitives); |
| 129 | + |
| 130 | + ASSERT_TRUE( |
| 131 | + test_negative_primitives(context_, -1111111111, -1111111111111111111, -1111, -1111111111)) |
| 132 | + << context_->getLog(); |
| 133 | +} |
| 134 | + |
| 135 | +TEST_P(EndiannessTest, HostReadsPointersToWasmMemory) { |
| 136 | + WasmCallWord<0> test_buffer_from_wasm; |
| 137 | + wasm_->wasm_vm()->getFunction("test_buffer_from_wasm", &test_buffer_from_wasm); |
| 138 | + |
| 139 | + ASSERT_TRUE(test_buffer_from_wasm(context_)) << context_->getLog(); |
| 140 | + |
| 141 | + context_->isLogged("hello from wasm land!"); |
| 142 | +} |
| 143 | + |
| 144 | +TEST_P(EndiannessTest, WasmCallReadsBufferPassedByHost) { |
| 145 | + context_->setBuffer(0, "hello from host land!"); |
| 146 | + WasmCallWord<0> test_buffer_from_host; |
| 147 | + wasm_->wasm_vm()->getFunction("test_buffer_from_host", &test_buffer_from_host); |
| 148 | + |
| 149 | + ASSERT_TRUE(test_buffer_from_host(context_)) << context_->getLog(); |
| 150 | +} |
| 151 | + |
| 152 | +GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(EndiannessTest); |
| 153 | + |
| 154 | +} // namespace |
| 155 | +} // namespace proxy_wasm |
0 commit comments