1 // Copyright 2020 The Pigweed Authors
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
7 // https://www.apache.org/licenses/LICENSE-2.0
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
15 #include "pw_rpc/internal/nanopb_method.h"
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"
28 namespace pw::rpc::internal {
35 // Create a fake service for use with the MethodImplTester.
36 class TestNanopbService final : public Service {
38 Status Unary(ServerContext&, const FakePb&, FakePb&) { return Status(); }
40 static Status StaticUnary(ServerContext&, const FakePb&, FakePb&) {
44 void ServerStreaming(ServerContext&, const FakePb&, ServerWriter<FakePb>&) {}
46 static void StaticServerStreaming(ServerContext&,
48 ServerWriter<FakePb>&) {}
50 Status UnaryWrongArg(ServerContext&, FakePb&, FakePb&) { return Status(); }
52 static void StaticUnaryVoidReturn(ServerContext&, const FakePb&, FakePb&) {}
54 int ServerStreamingBadReturn(ServerContext&,
56 ServerWriter<FakePb>&) {
60 static void StaticServerStreamingMissingArg(const FakePb&,
61 ServerWriter<FakePb>&) {}
64 TEST(MethodImplTester, NanopbMethod) {
65 constexpr MethodImplTester<NanopbMethod, TestNanopbService, nullptr, nullptr>
67 EXPECT_TRUE(method_tester.MethodImplIsValid());
70 pw_rpc_test_TestRequest last_request;
71 ServerWriter<pw_rpc_test_TestResponse> last_writer;
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();
81 Status DoNothing(ServerContext&, const pw_rpc_test_Empty&, pw_rpc_test_Empty&) {
82 return Status::Unknown();
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);
92 class FakeService : public Service {
94 FakeService(uint32_t id) : Service(id, kMethods) {}
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),
106 TEST(NanopbMethod, UnaryRpc_SendsResponse) {
108 pw_rpc_test_TestRequest, request, .integer = 123, .status_code = 0);
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));
115 const Packet& response = context.output().sent_packet();
116 EXPECT_EQ(response.status(), Status::Unauthenticated());
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}};
122 EXPECT_EQ(sizeof(expected), response.payload().size());
124 std::memcmp(expected, response.payload().data(), sizeof(expected)));
126 EXPECT_EQ(123, last_request.integer);
129 TEST(NanopbMethod, UnaryRpc_InvalidPayload_SendsError) {
130 std::array<byte, 8> bad_payload{byte{0xFF}, byte{0xAA}, byte{0xDD}};
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));
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());
144 TEST(NanopbMethod, UnaryRpc_BufferTooSmallForResponse_SendsInternalError) {
145 constexpr int64_t value = 0x7FFFFFFF'FFFFFF00ll;
147 pw_rpc_test_TestRequest, request, .integer = value, .status_code = 0);
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);
156 method.Invoke(context.get(), context.packet(request));
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());
164 EXPECT_EQ(value, last_request.integer);
167 TEST(NanopbMethod, ServerStreamingRpc_SendsNothingWhenInitiallyCalled) {
169 pw_rpc_test_TestRequest, request, .integer = 555, .status_code = 0);
171 const NanopbMethod& method =
172 std::get<2>(FakeService::kMethods).nanopb_method();
173 ServerContextForTest<FakeService> context(method);
175 method.Invoke(context.get(), context.packet(request));
177 EXPECT_EQ(0u, context.output().packet_count());
178 EXPECT_EQ(555, last_request.integer);
181 TEST(NanopbMethod, ServerWriter_SendsResponse) {
182 const NanopbMethod& method =
183 std::get<2>(FakeService::kMethods).nanopb_method();
184 ServerContextForTest<FakeService> context(method);
186 method.Invoke(context.get(), context.packet({}));
188 EXPECT_EQ(OkStatus(), last_writer.Write({.value = 100}));
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());
195 ASSERT_EQ(encoded.value().size(), context.output().sent_data().size());
197 std::memcmp(encoded.value().data(),
198 context.output().sent_data().data(),
199 encoded.value().size()));
202 TEST(NanopbMethod, ServerWriter_WriteWhenClosed_ReturnsFailedPrecondition) {
203 const NanopbMethod& method =
204 std::get<2>(FakeService::kMethods).nanopb_method();
205 ServerContextForTest<FakeService> context(method);
207 method.Invoke(context.get(), context.packet({}));
209 last_writer.Finish();
210 EXPECT_TRUE(last_writer.Write({.value = 100}).IsFailedPrecondition());
214 ServerStreamingRpc_ServerWriterBufferTooSmall_InternalError) {
215 const NanopbMethod& method =
216 std::get<2>(FakeService::kMethods).nanopb_method();
218 constexpr size_t kNoPayloadPacketSize = 2 /* type */ + 2 /* channel */ +
219 5 /* service */ + 5 /* method */ +
220 2 /* payload */ + 2 /* status */;
222 // Make the buffer barely fit a packet with no payload.
223 ServerContextForTest<FakeService, kNoPayloadPacketSize> context(method);
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());
231 method.Invoke(context.get(), context.packet({}));
233 EXPECT_EQ(OkStatus(), last_writer.Write({})); // Barely fits
234 EXPECT_EQ(Status::Internal(), last_writer.Write({.value = 1})); // Too big
238 } // namespace pw::rpc::internal