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