• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 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 //
15 
16 #include "pw_rpc/pwpb/internal/method_union.h"
17 
18 #include <array>
19 
20 #include "gtest/gtest.h"
21 #include "pw_rpc/internal/test_utils.h"
22 #include "pw_rpc/service.h"
23 #include "pw_rpc_pwpb_private/internal_test_utils.h"
24 #include "pw_rpc_test_protos/test.pwpb.h"
25 
26 namespace pw::rpc::internal {
27 namespace {
28 
29 template <typename Implementation>
30 class FakeGeneratedService : public Service {
31  public:
FakeGeneratedService(uint32_t id)32   constexpr FakeGeneratedService(uint32_t id) : Service(id, kMethods) {}
33 
34   static constexpr std::array<PwpbMethodUnion, 4> kMethods = {
35       GetPwpbOrRawMethodFor<&Implementation::DoNothing,
36                             MethodType::kUnary,
37                             pw::rpc::test::pwpb::Empty::Message,
38                             pw::rpc::test::pwpb::Empty::Message>(
39           10u,
40           kPwpbMethodSerde<&pw::rpc::test::pwpb::Empty::kMessageFields,
41                            &pw::rpc::test::pwpb::Empty::kMessageFields>),
42       GetPwpbOrRawMethodFor<&Implementation::RawStream,
43                             MethodType::kServerStreaming,
44                             pw::rpc::test::pwpb::TestRequest::Message,
45                             pw::rpc::test::pwpb::TestResponse::Message>(
46           11u,
47           kPwpbMethodSerde<&pw::rpc::test::pwpb::TestRequest::kMessageFields,
48                            &pw::rpc::test::pwpb::TestResponse::kMessageFields>),
49       GetPwpbOrRawMethodFor<&Implementation::AddFive,
50                             MethodType::kUnary,
51                             pw::rpc::test::pwpb::TestRequest::Message,
52                             pw::rpc::test::pwpb::TestResponse::Message>(
53           12u,
54           kPwpbMethodSerde<&pw::rpc::test::pwpb::TestRequest::kMessageFields,
55                            &pw::rpc::test::pwpb::TestResponse::kMessageFields>),
56       GetPwpbOrRawMethodFor<&Implementation::StartStream,
57                             MethodType::kServerStreaming,
58                             pw::rpc::test::pwpb::TestRequest::Message,
59                             pw::rpc::test::pwpb::TestResponse::Message>(
60           13u,
61           kPwpbMethodSerde<&pw::rpc::test::pwpb::TestRequest::kMessageFields,
62                            &pw::rpc::test::pwpb::TestResponse::kMessageFields>),
63   };
64 };
65 
66 class FakeGeneratedServiceImpl
67     : public FakeGeneratedService<FakeGeneratedServiceImpl> {
68  public:
FakeGeneratedServiceImpl(uint32_t id)69   FakeGeneratedServiceImpl(uint32_t id) : FakeGeneratedService(id) {}
70 
AddFive(const pw::rpc::test::pwpb::TestRequest::Message & request,pw::rpc::test::pwpb::TestResponse::Message & response)71   Status AddFive(const pw::rpc::test::pwpb::TestRequest::Message& request,
72                  pw::rpc::test::pwpb::TestResponse::Message& response) {
73     last_request = request;
74     response.value = request.integer + 5;
75     return Status::Unauthenticated();
76   }
77 
DoNothing(ConstByteSpan,RawUnaryResponder & responder)78   void DoNothing(ConstByteSpan, RawUnaryResponder& responder) {
79     ASSERT_EQ(OkStatus(), responder.Finish({}, Status::Unknown()));
80   }
81 
RawStream(ConstByteSpan,RawServerWriter & writer)82   void RawStream(ConstByteSpan, RawServerWriter& writer) {
83     last_raw_writer = std::move(writer);
84   }
85 
StartStream(const pw::rpc::test::pwpb::TestRequest::Message & request,PwpbServerWriter<pw::rpc::test::pwpb::TestResponse::Message> & writer)86   void StartStream(
87       const pw::rpc::test::pwpb::TestRequest::Message& request,
88       PwpbServerWriter<pw::rpc::test::pwpb::TestResponse::Message>& writer) {
89     last_request = request;
90     last_writer = std::move(writer);
91   }
92 
93   pw::rpc::test::pwpb::TestRequest::Message last_request;
94   PwpbServerWriter<pw::rpc::test::pwpb::TestResponse::Message> last_writer;
95   RawServerWriter last_raw_writer;
96 };
97 
TEST(PwpbMethodUnion,Raw_CallsUnaryMethod)98 TEST(PwpbMethodUnion, Raw_CallsUnaryMethod) {
99   const Method& method =
100       std::get<0>(FakeGeneratedServiceImpl::kMethods).method();
101   ServerContextForTest<FakeGeneratedServiceImpl> context(method);
102   rpc_lock().lock();
103   method.Invoke(context.get(), context.request({}));
104 
105   const Packet& response = context.output().last_packet();
106   EXPECT_EQ(response.status(), Status::Unknown());
107 }
108 
TEST(PwpbMethodUnion,Raw_CallsServerStreamingMethod)109 TEST(PwpbMethodUnion, Raw_CallsServerStreamingMethod) {
110   PW_ENCODE_PB(pw::rpc::test::pwpb::TestRequest,
111                request,
112                .integer = 555,
113                .status_code = 0);
114 
115   const Method& method =
116       std::get<1>(FakeGeneratedServiceImpl::kMethods).method();
117   ServerContextForTest<FakeGeneratedServiceImpl> context(method);
118 
119   rpc_lock().lock();
120   method.Invoke(context.get(), context.request(request));
121 
122   EXPECT_TRUE(context.service().last_raw_writer.active());
123   EXPECT_EQ(OkStatus(), context.service().last_raw_writer.Finish());
124   EXPECT_EQ(context.output().last_packet().type(), pwpb::PacketType::RESPONSE);
125 }
126 
TEST(PwpbMethodUnion,Pwpb_CallsUnaryMethod)127 TEST(PwpbMethodUnion, Pwpb_CallsUnaryMethod) {
128   PW_ENCODE_PB(pw::rpc::test::pwpb::TestRequest,
129                request,
130                .integer = 123,
131                .status_code = 3);
132 
133   const Method& method =
134       std::get<2>(FakeGeneratedServiceImpl::kMethods).method();
135   ServerContextForTest<FakeGeneratedServiceImpl> context(method);
136   rpc_lock().lock();
137   method.Invoke(context.get(), context.request(request));
138 
139   const Packet& response = context.output().last_packet();
140   EXPECT_EQ(response.status(), Status::Unauthenticated());
141 
142   // Field 1 (encoded as 1 << 3) with 128 as the value.
143   constexpr std::byte expected[]{
144       std::byte{0x08}, std::byte{0x80}, std::byte{0x01}};
145 
146   EXPECT_EQ(sizeof(expected), response.payload().size());
147   EXPECT_EQ(0,
148             std::memcmp(expected, response.payload().data(), sizeof(expected)));
149 
150   EXPECT_EQ(123, context.service().last_request.integer);
151   EXPECT_EQ(3u, context.service().last_request.status_code);
152 }
153 
TEST(PwpbMethodUnion,Pwpb_CallsServerStreamingMethod)154 TEST(PwpbMethodUnion, Pwpb_CallsServerStreamingMethod) {
155   PW_ENCODE_PB(pw::rpc::test::pwpb::TestRequest,
156                request,
157                .integer = 555,
158                .status_code = 0);
159 
160   const Method& method =
161       std::get<3>(FakeGeneratedServiceImpl::kMethods).method();
162   ServerContextForTest<FakeGeneratedServiceImpl> context(method);
163 
164   rpc_lock().lock();
165   method.Invoke(context.get(), context.request(request));
166 
167   EXPECT_EQ(555, context.service().last_request.integer);
168   EXPECT_TRUE(context.service().last_writer.active());
169 
170   EXPECT_EQ(OkStatus(), context.service().last_writer.Finish());
171   EXPECT_EQ(context.output().last_packet().type(), pwpb::PacketType::RESPONSE);
172 }
173 
174 }  // namespace
175 }  // namespace pw::rpc::internal
176