Skip to content

Commit d0a3249

Browse files
committed
Do not reverse bytes for nullvm
- moved pairs test into a new file test/pairs_util_test.cc - run pairs test on all platforms - made contextOrEffectiveContext() null check a bit more readable Fixes #294 Signed-off-by: Konstantin Maksimov <konstantin.maksimov@ibm.com>
1 parent dec6b94 commit d0a3249

File tree

4 files changed

+62
-29
lines changed

4 files changed

+62
-29
lines changed

src/pairs_util.cc

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ bool PairsUtil::marshalPairs(const Pairs &pairs, char *buffer, size_t size) {
4747

4848
// Write number of pairs.
4949
uint32_t num_pairs =
50-
htowasm(pairs.size(), contextOrEffectiveContext() == nullptr
51-
? false
52-
: contextOrEffectiveContext()->wasmVm()->usesWasmByteOrder());
50+
htowasm(pairs.size(), contextOrEffectiveContext() != nullptr
51+
? contextOrEffectiveContext()->wasmVm()->usesWasmByteOrder()
52+
: false);
5353
if (pos + sizeof(uint32_t) > end) {
5454
return false;
5555
}
@@ -59,9 +59,9 @@ bool PairsUtil::marshalPairs(const Pairs &pairs, char *buffer, size_t size) {
5959
for (const auto &p : pairs) {
6060
// Write name length.
6161
uint32_t name_len =
62-
htowasm(p.first.size(), contextOrEffectiveContext() == nullptr
63-
? false
64-
: contextOrEffectiveContext()->wasmVm()->usesWasmByteOrder());
62+
htowasm(p.first.size(), contextOrEffectiveContext() != nullptr
63+
? contextOrEffectiveContext()->wasmVm()->usesWasmByteOrder()
64+
: false);
6565
if (pos + sizeof(uint32_t) > end) {
6666
return false;
6767
}
@@ -70,9 +70,9 @@ bool PairsUtil::marshalPairs(const Pairs &pairs, char *buffer, size_t size) {
7070

7171
// Write value length.
7272
uint32_t value_len =
73-
htowasm(p.second.size(), contextOrEffectiveContext() == nullptr
74-
? false
75-
: contextOrEffectiveContext()->wasmVm()->usesWasmByteOrder());
73+
htowasm(p.second.size(), contextOrEffectiveContext() != nullptr
74+
? contextOrEffectiveContext()->wasmVm()->usesWasmByteOrder()
75+
: false);
7676
if (pos + sizeof(uint32_t) > end) {
7777
return false;
7878
}
@@ -114,9 +114,9 @@ Pairs PairsUtil::toPairs(std::string_view buffer) {
114114
return {};
115115
}
116116
uint32_t num_pairs = wasmtoh(*reinterpret_cast<const uint32_t *>(pos),
117-
contextOrEffectiveContext() == nullptr
118-
? false
119-
: contextOrEffectiveContext()->wasmVm()->usesWasmByteOrder());
117+
contextOrEffectiveContext() != nullptr
118+
? contextOrEffectiveContext()->wasmVm()->usesWasmByteOrder()
119+
: false);
120120
pos += sizeof(uint32_t);
121121

122122
// Check if we're not going to exceed the limit.
@@ -136,19 +136,19 @@ Pairs PairsUtil::toPairs(std::string_view buffer) {
136136
return {};
137137
}
138138
s.first = wasmtoh(*reinterpret_cast<const uint32_t *>(pos),
139-
contextOrEffectiveContext() == nullptr
140-
? false
141-
: contextOrEffectiveContext()->wasmVm()->usesWasmByteOrder());
139+
contextOrEffectiveContext() != nullptr
140+
? contextOrEffectiveContext()->wasmVm()->usesWasmByteOrder()
141+
: false);
142142
pos += sizeof(uint32_t);
143143

144144
// Read value length.
145145
if (pos + sizeof(uint32_t) > end) {
146146
return {};
147147
}
148148
s.second = wasmtoh(*reinterpret_cast<const uint32_t *>(pos),
149-
contextOrEffectiveContext() == nullptr
150-
? false
151-
: contextOrEffectiveContext()->wasmVm()->usesWasmByteOrder());
149+
contextOrEffectiveContext() != nullptr
150+
? contextOrEffectiveContext()->wasmVm()->usesWasmByteOrder()
151+
: false);
152152
pos += sizeof(uint32_t);
153153
}
154154

test/BUILD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,17 @@ cc_test(
165165
],
166166
)
167167

168+
cc_test(
169+
name = "pairs_util_test",
170+
srcs = ["pairs_util_test.cc"],
171+
linkstatic = 1,
172+
deps = [
173+
"//:lib",
174+
"@com_google_googletest//:gtest",
175+
"@com_google_googletest//:gtest_main",
176+
],
177+
)
178+
168179
cc_library(
169180
name = "utility_lib",
170181
testonly = True,

test/null_vm_test.cc

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,6 @@ TEST_F(BaseVmTest, ByteOrder) {
7878
auto wasm_vm = createNullVm();
7979
EXPECT_TRUE(wasm_vm->load("test_null_vm_plugin", {}, {}));
8080
EXPECT_FALSE(wasm_vm->usesWasmByteOrder());
81-
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
82-
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
83-
proxy_wasm::Pairs pairs1;
84-
std::string data1("some_data");
85-
pairs1.push_back({data1.data(), std::to_string(data1.size())});
86-
std::vector<char> buffer(PairsUtil::pairsSize(pairs1));
87-
EXPECT_TRUE(PairsUtil::marshalPairs(pairs1, buffer.data(), buffer.size()));
88-
auto pairs2 = PairsUtil::toPairs(std::string_view(buffer.data(), buffer.size()));
89-
EXPECT_EQ(pairs2.size(), pairs1.size());
90-
EXPECT_EQ(pairs2[0].second, pairs1[0].second);
91-
#endif
9281
}
9382

9483
} // namespace proxy_wasm

test/pairs_util_test.cc

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2022 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 "include/proxy-wasm/pairs_util.h"
16+
#include "gtest/gtest.h"
17+
18+
namespace proxy_wasm {
19+
20+
TEST(PairsUtilTest, EncodeDecode) {
21+
proxy_wasm::Pairs pairs1;
22+
std::string data1("some_data");
23+
auto size_str = std::to_string(data1.size());
24+
pairs1.push_back({data1, size_str});
25+
std::vector<char> buffer(PairsUtil::pairsSize(pairs1));
26+
EXPECT_TRUE(PairsUtil::marshalPairs(pairs1, buffer.data(), buffer.size()));
27+
auto pairs2 = PairsUtil::toPairs(std::string_view(buffer.data(), buffer.size()));
28+
EXPECT_EQ(pairs2.size(), pairs1.size());
29+
EXPECT_EQ(pairs2[0].first, pairs1[0].first);
30+
EXPECT_EQ(pairs2[0].second, pairs1[0].second);
31+
}
32+
33+
} // namespace proxy_wasm

0 commit comments

Comments
 (0)