Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_rpc / nanopb / nanopb_method_test.cc
1 // 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
15 #include "pw_rpc/internal/nanopb_method.h"
16
17 #include <array>
18
19 #include "gtest/gtest.h"
20 #include "pw_rpc/internal/nanopb_method_union.h"
21 #include "pw_rpc/server_context.h"
22 #include "pw_rpc/service.h"
23 #include "pw_rpc_nanopb_private/internal_test_utils.h"
24 #include "pw_rpc_private/internal_test_utils.h"
25 #include "pw_rpc_private/method_impl_tester.h"
26 #include "pw_rpc_test_protos/test.pb.h"
27
28 namespace pw::rpc::internal {
29 namespace {
30
31 using std::byte;
32
33 struct FakePb {};
34
35 // Create a fake service for use with the MethodImplTester.
36 class TestNanopbService final : public Service {
37  public:
38   Status Unary(ServerContext&, const FakePb&, FakePb&) { return Status(); }
39
40   static Status StaticUnary(ServerContext&, const FakePb&, FakePb&) {
41     return Status();
42   }
43
44   void ServerStreaming(ServerContext&, const FakePb&, ServerWriter<FakePb>&) {}
45
46   static void StaticServerStreaming(ServerContext&,
47                                     const FakePb&,
48                                     ServerWriter<FakePb>&) {}
49
50   Status UnaryWrongArg(ServerContext&, FakePb&, FakePb&) { return Status(); }
51
52   static void StaticUnaryVoidReturn(ServerContext&, const FakePb&, FakePb&) {}
53
54   int ServerStreamingBadReturn(ServerContext&,
55                                const FakePb&,
56                                ServerWriter<FakePb>&) {
57     return 5;
58   }
59
60   static void StaticServerStreamingMissingArg(const FakePb&,
61                                               ServerWriter<FakePb>&) {}
62 };
63
64 TEST(MethodImplTester, NanopbMethod) {
65   constexpr MethodImplTester<NanopbMethod, TestNanopbService, nullptr, nullptr>
66       method_tester;
67   EXPECT_TRUE(method_tester.MethodImplIsValid());
68 }
69
70 pw_rpc_test_TestRequest last_request;
71 ServerWriter<pw_rpc_test_TestResponse> last_writer;
72
73 Status AddFive(ServerContext&,
74                const pw_rpc_test_TestRequest& request,
75                pw_rpc_test_TestResponse& response) {
76   last_request = request;
77   response.value = request.integer + 5;
78   return Status::Unauthenticated();
79 }
80
81 Status DoNothing(ServerContext&, const pw_rpc_test_Empty&, pw_rpc_test_Empty&) {
82   return Status::Unknown();
83 }
84
85 void StartStream(ServerContext&,
86                  const pw_rpc_test_TestRequest& request,
87                  ServerWriter<pw_rpc_test_TestResponse>& writer) {
88   last_request = request;
89   last_writer = std::move(writer);
90 }
91
92 class FakeService : public Service {
93  public:
94   FakeService(uint32_t id) : Service(id, kMethods) {}
95
96   static constexpr std::array<NanopbMethodUnion, 3> kMethods = {
97       NanopbMethod::Unary<DoNothing>(
98           10u, pw_rpc_test_Empty_fields, pw_rpc_test_Empty_fields),
99       NanopbMethod::Unary<AddFive>(
100           11u, pw_rpc_test_TestRequest_fields, pw_rpc_test_TestResponse_fields),
101       NanopbMethod::ServerStreaming<StartStream>(
102           12u, pw_rpc_test_TestRequest_fields, pw_rpc_test_TestResponse_fields),
103   };
104 };
105
106 TEST(NanopbMethod, UnaryRpc_SendsResponse) {
107   PW_ENCODE_PB(
108       pw_rpc_test_TestRequest, request, .integer = 123, .status_code = 0);
109
110   const NanopbMethod& method =
111       std::get<1>(FakeService::kMethods).nanopb_method();
112   ServerContextForTest<FakeService> context(method);
113   method.Invoke(context.get(), context.packet(request));
114
115   const Packet& response = context.output().sent_packet();
116   EXPECT_EQ(response.status(), Status::Unauthenticated());
117
118   // Field 1 (encoded as 1 << 3) with 128 as the value.
119   constexpr std::byte expected[]{
120       std::byte{0x08}, std::byte{0x80}, std::byte{0x01}};
121
122   EXPECT_EQ(sizeof(expected), response.payload().size());
123   EXPECT_EQ(0,
124             std::memcmp(expected, response.payload().data(), sizeof(expected)));
125
126   EXPECT_EQ(123, last_request.integer);
127 }
128
129 TEST(NanopbMethod, UnaryRpc_InvalidPayload_SendsError) {
130   std::array<byte, 8> bad_payload{byte{0xFF}, byte{0xAA}, byte{0xDD}};
131
132   const NanopbMethod& method =
133       std::get<0>(FakeService::kMethods).nanopb_method();
134   ServerContextForTest<FakeService> context(method);
135   method.Invoke(context.get(), context.packet(bad_payload));
136
137   const Packet& packet = context.output().sent_packet();
138   EXPECT_EQ(PacketType::SERVER_ERROR, packet.type());
139   EXPECT_EQ(Status::DataLoss(), packet.status());
140   EXPECT_EQ(context.kServiceId, packet.service_id());
141   EXPECT_EQ(method.id(), packet.method_id());
142 }
143
144 TEST(NanopbMethod, UnaryRpc_BufferTooSmallForResponse_SendsInternalError) {
145   constexpr int64_t value = 0x7FFFFFFF'FFFFFF00ll;
146   PW_ENCODE_PB(
147       pw_rpc_test_TestRequest, request, .integer = value, .status_code = 0);
148
149   const NanopbMethod& method =
150       std::get<1>(FakeService::kMethods).nanopb_method();
151   // Output buffer is too small for the response, but can fit an error packet.
152   ServerContextForTest<FakeService, 22> context(method);
153   ASSERT_LT(context.output().buffer_size(),
154             context.packet(request).MinEncodedSizeBytes() + request.size() + 1);
155
156   method.Invoke(context.get(), context.packet(request));
157
158   const Packet& packet = context.output().sent_packet();
159   EXPECT_EQ(PacketType::SERVER_ERROR, packet.type());
160   EXPECT_EQ(Status::Internal(), packet.status());
161   EXPECT_EQ(context.kServiceId, packet.service_id());
162   EXPECT_EQ(method.id(), packet.method_id());
163
164   EXPECT_EQ(value, last_request.integer);
165 }
166
167 TEST(NanopbMethod, ServerStreamingRpc_SendsNothingWhenInitiallyCalled) {
168   PW_ENCODE_PB(
169       pw_rpc_test_TestRequest, request, .integer = 555, .status_code = 0);
170
171   const NanopbMethod& method =
172       std::get<2>(FakeService::kMethods).nanopb_method();
173   ServerContextForTest<FakeService> context(method);
174
175   method.Invoke(context.get(), context.packet(request));
176
177   EXPECT_EQ(0u, context.output().packet_count());
178   EXPECT_EQ(555, last_request.integer);
179 }
180
181 TEST(NanopbMethod, ServerWriter_SendsResponse) {
182   const NanopbMethod& method =
183       std::get<2>(FakeService::kMethods).nanopb_method();
184   ServerContextForTest<FakeService> context(method);
185
186   method.Invoke(context.get(), context.packet({}));
187
188   EXPECT_EQ(OkStatus(), last_writer.Write({.value = 100}));
189
190   PW_ENCODE_PB(pw_rpc_test_TestResponse, payload, .value = 100);
191   std::array<byte, 128> encoded_response = {};
192   auto encoded = context.packet(payload).Encode(encoded_response);
193   ASSERT_EQ(OkStatus(), encoded.status());
194
195   ASSERT_EQ(encoded.value().size(), context.output().sent_data().size());
196   EXPECT_EQ(0,
197             std::memcmp(encoded.value().data(),
198                         context.output().sent_data().data(),
199                         encoded.value().size()));
200 }
201
202 TEST(NanopbMethod, ServerWriter_WriteWhenClosed_ReturnsFailedPrecondition) {
203   const NanopbMethod& method =
204       std::get<2>(FakeService::kMethods).nanopb_method();
205   ServerContextForTest<FakeService> context(method);
206
207   method.Invoke(context.get(), context.packet({}));
208
209   last_writer.Finish();
210   EXPECT_TRUE(last_writer.Write({.value = 100}).IsFailedPrecondition());
211 }
212
213 TEST(NanopbMethod,
214      ServerStreamingRpc_ServerWriterBufferTooSmall_InternalError) {
215   const NanopbMethod& method =
216       std::get<2>(FakeService::kMethods).nanopb_method();
217
218   constexpr size_t kNoPayloadPacketSize = 2 /* type */ + 2 /* channel */ +
219                                           5 /* service */ + 5 /* method */ +
220                                           2 /* payload */ + 2 /* status */;
221
222   // Make the buffer barely fit a packet with no payload.
223   ServerContextForTest<FakeService, kNoPayloadPacketSize> context(method);
224
225   // Verify that the encoded size of a packet with an empty payload is correct.
226   std::array<byte, 128> encoded_response = {};
227   auto encoded = context.packet({}).Encode(encoded_response);
228   ASSERT_EQ(OkStatus(), encoded.status());
229   ASSERT_EQ(kNoPayloadPacketSize, encoded.value().size());
230
231   method.Invoke(context.get(), context.packet({}));
232
233   EXPECT_EQ(OkStatus(), last_writer.Write({}));  // Barely fits
234   EXPECT_EQ(Status::Internal(), last_writer.Write({.value = 1}));  // Too big
235 }
236
237 }  // namespace
238 }  // namespace pw::rpc::internal