blob: 2a82cf3cdfa8f8825e99aefc2e9ec853d599e521 [file] [log] [blame]
Alexei Frolova4d71502020-10-14 12:43:14 -07001// Copyright 2020 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://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, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14
Wyatt Hepler8ec2cf82021-07-09 17:18:41 -070015#include "pw_rpc/nanopb/internal/method_union.h"
Alexei Frolova4d71502020-10-14 12:43:14 -070016
17#include <array>
18
19#include "gtest/gtest.h"
Wyatt Heplerfa6edcc2021-08-20 08:30:08 -070020#include "pw_rpc/internal/test_utils.h"
Alexei Frolova4d71502020-10-14 12:43:14 -070021#include "pw_rpc_nanopb_private/internal_test_utils.h"
Alexei Frolova4d71502020-10-14 12:43:14 -070022#include "pw_rpc_test_protos/test.pb.h"
23
24namespace pw::rpc::internal {
25namespace {
26
27using std::byte;
28
29template <typename Implementation>
30class FakeGeneratedService : public Service {
31 public:
32 constexpr FakeGeneratedService(uint32_t id) : Service(id, kMethods) {}
33
34 static constexpr std::array<NanopbMethodUnion, 4> kMethods = {
Wyatt Hepler38f87082021-02-23 17:00:09 -080035 GetNanopbOrRawMethodFor<&Implementation::DoNothing,
36 MethodType::kUnary,
37 pw_rpc_test_Empty,
38 pw_rpc_test_Empty>(
Alexei Frolova4d71502020-10-14 12:43:14 -070039 10u, pw_rpc_test_Empty_fields, pw_rpc_test_Empty_fields),
Wyatt Heplere95bd722020-11-23 07:49:47 -080040 GetNanopbOrRawMethodFor<&Implementation::RawStream,
Wyatt Hepler38f87082021-02-23 17:00:09 -080041 MethodType::kServerStreaming,
42 pw_rpc_test_TestRequest,
43 pw_rpc_test_TestResponse>(
Alexei Frolova4d71502020-10-14 12:43:14 -070044 11u, pw_rpc_test_TestRequest_fields, pw_rpc_test_TestResponse_fields),
Wyatt Hepler38f87082021-02-23 17:00:09 -080045 GetNanopbOrRawMethodFor<&Implementation::AddFive,
46 MethodType::kUnary,
47 pw_rpc_test_TestRequest,
48 pw_rpc_test_TestResponse>(
Alexei Frolova4d71502020-10-14 12:43:14 -070049 12u, pw_rpc_test_TestRequest_fields, pw_rpc_test_TestResponse_fields),
Wyatt Heplere95bd722020-11-23 07:49:47 -080050 GetNanopbOrRawMethodFor<&Implementation::StartStream,
Wyatt Hepler38f87082021-02-23 17:00:09 -080051 MethodType::kServerStreaming,
52 pw_rpc_test_TestRequest,
53 pw_rpc_test_TestResponse>(
Alexei Frolova4d71502020-10-14 12:43:14 -070054 13u, pw_rpc_test_TestRequest_fields, pw_rpc_test_TestResponse_fields),
55 };
56};
57
58pw_rpc_test_TestRequest last_request;
Wyatt Heplerfa6edcc2021-08-20 08:30:08 -070059NanopbServerWriter<pw_rpc_test_TestResponse> last_writer;
Alexei Frolova4d71502020-10-14 12:43:14 -070060RawServerWriter last_raw_writer;
61
62class FakeGeneratedServiceImpl
63 : public FakeGeneratedService<FakeGeneratedServiceImpl> {
64 public:
65 FakeGeneratedServiceImpl(uint32_t id) : FakeGeneratedService(id) {}
66
67 Status AddFive(ServerContext&,
68 const pw_rpc_test_TestRequest& request,
69 pw_rpc_test_TestResponse& response) {
70 last_request = request;
71 response.value = request.integer + 5;
72 return Status::Unauthenticated();
73 }
74
75 StatusWithSize DoNothing(ServerContext&, ConstByteSpan, ByteSpan) {
76 return StatusWithSize::Unknown();
77 }
78
79 void RawStream(ServerContext&, ConstByteSpan, RawServerWriter& writer) {
80 last_raw_writer = std::move(writer);
81 }
82
83 void StartStream(ServerContext&,
84 const pw_rpc_test_TestRequest& request,
Wyatt Heplerfa6edcc2021-08-20 08:30:08 -070085 NanopbServerWriter<pw_rpc_test_TestResponse>& writer) {
Alexei Frolova4d71502020-10-14 12:43:14 -070086 last_request = request;
87 last_writer = std::move(writer);
88 }
89};
90
91TEST(NanopbMethodUnion, Raw_CallsUnaryMethod) {
92 const Method& method =
93 std::get<0>(FakeGeneratedServiceImpl::kMethods).method();
94 ServerContextForTest<FakeGeneratedServiceImpl> context(method);
Wyatt Hepler5ba80642021-06-18 12:56:17 -070095 method.Invoke(context.get(), context.request({}));
Alexei Frolova4d71502020-10-14 12:43:14 -070096
97 const Packet& response = context.output().sent_packet();
98 EXPECT_EQ(response.status(), Status::Unknown());
99}
100
101TEST(NanopbMethodUnion, Raw_CallsServerStreamingMethod) {
102 PW_ENCODE_PB(
103 pw_rpc_test_TestRequest, request, .integer = 555, .status_code = 0);
104
105 const Method& method =
106 std::get<1>(FakeGeneratedServiceImpl::kMethods).method();
107 ServerContextForTest<FakeGeneratedServiceImpl> context(method);
108
Wyatt Hepler5ba80642021-06-18 12:56:17 -0700109 method.Invoke(context.get(), context.request(request));
Alexei Frolova4d71502020-10-14 12:43:14 -0700110
111 EXPECT_TRUE(last_raw_writer.open());
Wyatt Heplerd08e5822021-02-18 17:53:38 -0800112 EXPECT_EQ(OkStatus(), last_raw_writer.Finish());
Wyatt Hepler5ba80642021-06-18 12:56:17 -0700113 EXPECT_EQ(context.output().sent_packet().type(), PacketType::RESPONSE);
Alexei Frolova4d71502020-10-14 12:43:14 -0700114}
115
116TEST(NanopbMethodUnion, Nanopb_CallsUnaryMethod) {
117 PW_ENCODE_PB(
118 pw_rpc_test_TestRequest, request, .integer = 123, .status_code = 3);
119
120 const Method& method =
121 std::get<2>(FakeGeneratedServiceImpl::kMethods).method();
122 ServerContextForTest<FakeGeneratedServiceImpl> context(method);
Wyatt Hepler5ba80642021-06-18 12:56:17 -0700123 method.Invoke(context.get(), context.request(request));
Alexei Frolova4d71502020-10-14 12:43:14 -0700124
125 const Packet& response = context.output().sent_packet();
126 EXPECT_EQ(response.status(), Status::Unauthenticated());
127
128 // Field 1 (encoded as 1 << 3) with 128 as the value.
129 constexpr std::byte expected[]{
130 std::byte{0x08}, std::byte{0x80}, std::byte{0x01}};
131
132 EXPECT_EQ(sizeof(expected), response.payload().size());
133 EXPECT_EQ(0,
134 std::memcmp(expected, response.payload().data(), sizeof(expected)));
135
136 EXPECT_EQ(123, last_request.integer);
137 EXPECT_EQ(3u, last_request.status_code);
138}
139
140TEST(NanopbMethodUnion, Nanopb_CallsServerStreamingMethod) {
141 PW_ENCODE_PB(
142 pw_rpc_test_TestRequest, request, .integer = 555, .status_code = 0);
143
144 const Method& method =
145 std::get<3>(FakeGeneratedServiceImpl::kMethods).method();
146 ServerContextForTest<FakeGeneratedServiceImpl> context(method);
147
Wyatt Hepler5ba80642021-06-18 12:56:17 -0700148 method.Invoke(context.get(), context.request(request));
Alexei Frolova4d71502020-10-14 12:43:14 -0700149
150 EXPECT_EQ(555, last_request.integer);
151 EXPECT_TRUE(last_writer.open());
152
Wyatt Heplerd08e5822021-02-18 17:53:38 -0800153 EXPECT_EQ(OkStatus(), last_writer.Finish());
Wyatt Hepler5ba80642021-06-18 12:56:17 -0700154 EXPECT_EQ(context.output().sent_packet().type(), PacketType::RESPONSE);
Alexei Frolova4d71502020-10-14 12:43:14 -0700155}
156
157} // namespace
158} // namespace pw::rpc::internal