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_union.h"
16
17 #include <array>
18
19 #include "gtest/gtest.h"
20 #include "pw_rpc_nanopb_private/internal_test_utils.h"
21 #include "pw_rpc_private/internal_test_utils.h"
22 #include "pw_rpc_test_protos/test.pb.h"
23
24 namespace pw::rpc::internal {
25 namespace {
26
27 using std::byte;
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<NanopbMethodUnion, 4> kMethods = {
35 GetNanopbOrRawMethodFor<&Implementation::DoNothing,
36 MethodType::kUnary,
37 pw_rpc_test_Empty,
38 pw_rpc_test_Empty>(
39 10u, pw_rpc_test_Empty_fields, pw_rpc_test_Empty_fields),
40 GetNanopbOrRawMethodFor<&Implementation::RawStream,
41 MethodType::kServerStreaming,
42 pw_rpc_test_TestRequest,
43 pw_rpc_test_TestResponse>(
44 11u, pw_rpc_test_TestRequest_fields, pw_rpc_test_TestResponse_fields),
45 GetNanopbOrRawMethodFor<&Implementation::AddFive,
46 MethodType::kUnary,
47 pw_rpc_test_TestRequest,
48 pw_rpc_test_TestResponse>(
49 12u, pw_rpc_test_TestRequest_fields, pw_rpc_test_TestResponse_fields),
50 GetNanopbOrRawMethodFor<&Implementation::StartStream,
51 MethodType::kServerStreaming,
52 pw_rpc_test_TestRequest,
53 pw_rpc_test_TestResponse>(
54 13u, pw_rpc_test_TestRequest_fields, pw_rpc_test_TestResponse_fields),
55 };
56 };
57
58 pw_rpc_test_TestRequest last_request;
59 ServerWriter<pw_rpc_test_TestResponse> last_writer;
60 RawServerWriter last_raw_writer;
61
62 class FakeGeneratedServiceImpl
63 : public FakeGeneratedService<FakeGeneratedServiceImpl> {
64 public:
FakeGeneratedServiceImpl(uint32_t id)65 FakeGeneratedServiceImpl(uint32_t id) : FakeGeneratedService(id) {}
66
AddFive(ServerContext &,const pw_rpc_test_TestRequest & request,pw_rpc_test_TestResponse & response)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
DoNothing(ServerContext &,ConstByteSpan,ByteSpan)75 StatusWithSize DoNothing(ServerContext&, ConstByteSpan, ByteSpan) {
76 return StatusWithSize::Unknown();
77 }
78
RawStream(ServerContext &,ConstByteSpan,RawServerWriter & writer)79 void RawStream(ServerContext&, ConstByteSpan, RawServerWriter& writer) {
80 last_raw_writer = std::move(writer);
81 }
82
StartStream(ServerContext &,const pw_rpc_test_TestRequest & request,ServerWriter<pw_rpc_test_TestResponse> & writer)83 void StartStream(ServerContext&,
84 const pw_rpc_test_TestRequest& request,
85 ServerWriter<pw_rpc_test_TestResponse>& writer) {
86 last_request = request;
87 last_writer = std::move(writer);
88 }
89 };
90
TEST(NanopbMethodUnion,Raw_CallsUnaryMethod)91 TEST(NanopbMethodUnion, Raw_CallsUnaryMethod) {
92 const Method& method =
93 std::get<0>(FakeGeneratedServiceImpl::kMethods).method();
94 ServerContextForTest<FakeGeneratedServiceImpl> context(method);
95 method.Invoke(context.get(), context.packet({}));
96
97 const Packet& response = context.output().sent_packet();
98 EXPECT_EQ(response.status(), Status::Unknown());
99 }
100
TEST(NanopbMethodUnion,Raw_CallsServerStreamingMethod)101 TEST(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
109 method.Invoke(context.get(), context.packet(request));
110
111 EXPECT_TRUE(last_raw_writer.open());
112 EXPECT_EQ(OkStatus(), last_raw_writer.Finish());
113 EXPECT_EQ(context.output().sent_packet().type(),
114 PacketType::SERVER_STREAM_END);
115 }
116
TEST(NanopbMethodUnion,Nanopb_CallsUnaryMethod)117 TEST(NanopbMethodUnion, Nanopb_CallsUnaryMethod) {
118 PW_ENCODE_PB(
119 pw_rpc_test_TestRequest, request, .integer = 123, .status_code = 3);
120
121 const Method& method =
122 std::get<2>(FakeGeneratedServiceImpl::kMethods).method();
123 ServerContextForTest<FakeGeneratedServiceImpl> context(method);
124 method.Invoke(context.get(), context.packet(request));
125
126 const Packet& response = context.output().sent_packet();
127 EXPECT_EQ(response.status(), Status::Unauthenticated());
128
129 // Field 1 (encoded as 1 << 3) with 128 as the value.
130 constexpr std::byte expected[]{
131 std::byte{0x08}, std::byte{0x80}, std::byte{0x01}};
132
133 EXPECT_EQ(sizeof(expected), response.payload().size());
134 EXPECT_EQ(0,
135 std::memcmp(expected, response.payload().data(), sizeof(expected)));
136
137 EXPECT_EQ(123, last_request.integer);
138 EXPECT_EQ(3u, last_request.status_code);
139 }
140
TEST(NanopbMethodUnion,Nanopb_CallsServerStreamingMethod)141 TEST(NanopbMethodUnion, Nanopb_CallsServerStreamingMethod) {
142 PW_ENCODE_PB(
143 pw_rpc_test_TestRequest, request, .integer = 555, .status_code = 0);
144
145 const Method& method =
146 std::get<3>(FakeGeneratedServiceImpl::kMethods).method();
147 ServerContextForTest<FakeGeneratedServiceImpl> context(method);
148
149 method.Invoke(context.get(), context.packet(request));
150
151 EXPECT_EQ(555, last_request.integer);
152 EXPECT_TRUE(last_writer.open());
153
154 EXPECT_EQ(OkStatus(), last_writer.Finish());
155 EXPECT_EQ(context.output().sent_packet().type(),
156 PacketType::SERVER_STREAM_END);
157 }
158
159 } // namespace
160 } // namespace pw::rpc::internal
161