aboutsummaryrefslogtreecommitdiff
path: root/pw_rpc/pwpb/pw_rpc_pwpb_private/internal_test_utils.h
diff options
context:
space:
mode:
authorXin Li <delphij@google.com>2023-08-14 15:38:30 -0700
committerXin Li <delphij@google.com>2023-08-14 15:38:30 -0700
commitbddf63953e111d742b591c1c0c7c34bcda8a51c7 (patch)
tree3a93128bff4b737b24b0c9581922c0b20410f0f4 /pw_rpc/pwpb/pw_rpc_pwpb_private/internal_test_utils.h
parentee890da55c82b95deca3518d5f3777e3d8ca9f0e (diff)
parentfbb9890f8922aa55fde183655a0017e69127ea4b (diff)
downloadpigweed-bddf63953e111d742b591c1c0c7c34bcda8a51c7.tar.gz
Merge Android U (ab/10368041)tmp_amf_298295554
Bug: 291102124 Merged-In: I10c41adb8fe3e126cfa4ff2f49b15863fff379de Change-Id: I66f7a6cccaafc173d3924dae62a736c6c53520c7
Diffstat (limited to 'pw_rpc/pwpb/pw_rpc_pwpb_private/internal_test_utils.h')
-rw-r--r--pw_rpc/pwpb/pw_rpc_pwpb_private/internal_test_utils.h69
1 files changed, 69 insertions, 0 deletions
diff --git a/pw_rpc/pwpb/pw_rpc_pwpb_private/internal_test_utils.h b/pw_rpc/pwpb/pw_rpc_pwpb_private/internal_test_utils.h
new file mode 100644
index 000000000..7208841f0
--- /dev/null
+++ b/pw_rpc/pwpb/pw_rpc_pwpb_private/internal_test_utils.h
@@ -0,0 +1,69 @@
+// Copyright 2022 The Pigweed Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+#pragma once
+
+#include <array>
+#include <cstddef>
+
+#include "pw_span/span.h"
+#include "pw_status/status.h"
+#include "pw_stream/memory_stream.h"
+
+namespace pw::rpc::internal {
+
+// Encodes a protobuf to a local span named by result from a list of pw_protobuf
+// struct initializers. Note that the proto namespace is passed, not the name
+// of the struct --- ie. exclude the "::Message" suffix.
+//
+// PW_ENCODE_PB(pw::rpc::TestProto, encoded, .value = 42);
+//
+#define PW_ENCODE_PB(proto, result, ...) \
+ _PW_ENCODE_PB_EXPAND(proto, result, __LINE__, __VA_ARGS__)
+
+#define _PW_ENCODE_PB_EXPAND(proto, result, unique, ...) \
+ _PW_ENCODE_PB_IMPL(proto, result, unique, __VA_ARGS__)
+
+#define _PW_ENCODE_PB_IMPL(proto, result, unique, ...) \
+ std::array<std::byte, 2 * sizeof(proto::Message)> _pb_buffer_##unique{}; \
+ const span result = \
+ ::pw::rpc::internal::EncodeProtobuf<proto::Message, \
+ proto::MemoryEncoder>( \
+ proto::Message{__VA_ARGS__}, _pb_buffer_##unique)
+
+template <typename Message, typename MemoryEncoder>
+span<const std::byte> EncodeProtobuf(const Message& message,
+ span<std::byte> buffer) {
+ MemoryEncoder encoder(buffer);
+ EXPECT_EQ(encoder.Write(message), OkStatus());
+ return buffer.first(encoder.size());
+}
+
+// Decodes a protobuf to a pw_protobuf struct named by result. Note that the
+// proto namespace is passed, not the name of the struct --- ie. exclude the
+// "::Message" suffix.
+//
+// PW_DECODE_PB(pw::rpc::TestProto, decoded, buffer);
+//
+#define PW_DECODE_PB(proto, result, buffer) \
+ proto::Message result; \
+ ::pw::rpc::internal::DecodeProtobuf<proto::Message, proto::StreamDecoder>( \
+ buffer, result);
+
+template <typename Message, typename StreamDecoder>
+void DecodeProtobuf(span<const std::byte> buffer, Message& message) {
+ stream::MemoryReader reader(buffer);
+ EXPECT_EQ(StreamDecoder(reader).Read(message), OkStatus());
+}
+
+} // namespace pw::rpc::internal