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 #include "gtest/gtest.h"
16 #include "pw_rpc/pwpb/test_method_context.h"
17 #include "pw_rpc/raw/test_method_context.h"
18 #include "pw_rpc_test_protos/test.rpc.pwpb.h"
19
20 namespace pw::rpc {
21 namespace {
22
23 namespace TestRequest = ::pw::rpc::test::pwpb::TestRequest;
24 namespace TestResponse = ::pw::rpc::test::pwpb::TestResponse;
25 namespace TestStreamResponse = ::pw::rpc::test::pwpb::TestStreamResponse;
26
27 class MixedService1
28 : public test::pw_rpc::pwpb::TestService::Service<MixedService1> {
29 public:
TestUnaryRpc(ConstByteSpan,RawUnaryResponder & responder)30 void TestUnaryRpc(ConstByteSpan, RawUnaryResponder& responder) {
31 std::byte response[5] = {};
32 ASSERT_EQ(OkStatus(), responder.Finish(response, OkStatus()));
33 }
34
TestAnotherUnaryRpc(const TestRequest::Message &,PwpbUnaryResponder<TestResponse::Message> &)35 void TestAnotherUnaryRpc(const TestRequest::Message&,
36 PwpbUnaryResponder<TestResponse::Message>&) {
37 called_async_unary_method = true;
38 }
39
TestServerStreamRpc(const TestRequest::Message &,ServerWriter<TestStreamResponse::Message> &)40 void TestServerStreamRpc(const TestRequest::Message&,
41 ServerWriter<TestStreamResponse::Message>&) {
42 called_server_streaming_method = true;
43 }
44
TestClientStreamRpc(RawServerReader &)45 void TestClientStreamRpc(RawServerReader&) {
46 called_client_streaming_method = true;
47 }
48
TestBidirectionalStreamRpc(ServerReaderWriter<TestRequest::Message,TestStreamResponse::Message> &)49 void TestBidirectionalStreamRpc(
50 ServerReaderWriter<TestRequest::Message, TestStreamResponse::Message>&) {
51 called_bidirectional_streaming_method = true;
52 }
53
54 bool called_async_unary_method = false;
55 bool called_server_streaming_method = false;
56 bool called_client_streaming_method = false;
57 bool called_bidirectional_streaming_method = false;
58 };
59
60 class MixedService2
61 : public test::pw_rpc::pwpb::TestService::Service<MixedService2> {
62 public:
TestUnaryRpc(const TestRequest::Message &,TestResponse::Message &)63 Status TestUnaryRpc(const TestRequest::Message&, TestResponse::Message&) {
64 return Status::Unauthenticated();
65 }
66
TestAnotherUnaryRpc(ConstByteSpan,RawUnaryResponder &)67 void TestAnotherUnaryRpc(ConstByteSpan, RawUnaryResponder&) {
68 called_async_unary_method = true;
69 }
70
TestServerStreamRpc(ConstByteSpan,RawServerWriter &)71 void TestServerStreamRpc(ConstByteSpan, RawServerWriter&) {
72 called_server_streaming_method = true;
73 }
74
TestClientStreamRpc(ServerReader<TestRequest::Message,TestStreamResponse::Message> &)75 void TestClientStreamRpc(
76 ServerReader<TestRequest::Message, TestStreamResponse::Message>&) {
77 called_client_streaming_method = true;
78 }
79
TestBidirectionalStreamRpc(RawServerReaderWriter &)80 void TestBidirectionalStreamRpc(RawServerReaderWriter&) {
81 called_bidirectional_streaming_method = true;
82 }
83
84 bool called_async_unary_method = false;
85 bool called_server_streaming_method = false;
86 bool called_client_streaming_method = false;
87 bool called_bidirectional_streaming_method = false;
88 };
89
TEST(MixedService1,CallRawMethod_SyncUnary)90 TEST(MixedService1, CallRawMethod_SyncUnary) {
91 PW_RAW_TEST_METHOD_CONTEXT(MixedService1, TestUnaryRpc) context;
92 context.call({});
93 EXPECT_EQ(OkStatus(), context.status());
94 EXPECT_EQ(5u, context.response().size());
95 }
96
TEST(MixedService1,CallPwpbMethod_AsyncUnary)97 TEST(MixedService1, CallPwpbMethod_AsyncUnary) {
98 PW_PWPB_TEST_METHOD_CONTEXT(MixedService1, TestAnotherUnaryRpc) context;
99 ASSERT_FALSE(context.service().called_async_unary_method);
100 context.call({});
101 EXPECT_TRUE(context.service().called_async_unary_method);
102 }
103
TEST(MixedService1,CallPwpbMethod_ServerStreaming)104 TEST(MixedService1, CallPwpbMethod_ServerStreaming) {
105 PW_PWPB_TEST_METHOD_CONTEXT(MixedService1, TestServerStreamRpc) context;
106 ASSERT_FALSE(context.service().called_server_streaming_method);
107 context.call({});
108 EXPECT_TRUE(context.service().called_server_streaming_method);
109 }
110
TEST(MixedService1,CallRawMethod_ClientStreaming)111 TEST(MixedService1, CallRawMethod_ClientStreaming) {
112 PW_RAW_TEST_METHOD_CONTEXT(MixedService1, TestClientStreamRpc) context;
113 ASSERT_FALSE(context.service().called_client_streaming_method);
114 context.call();
115 EXPECT_TRUE(context.service().called_client_streaming_method);
116 }
117
TEST(MixedService1,CallPwpbMethod_BidirectionalStreaming)118 TEST(MixedService1, CallPwpbMethod_BidirectionalStreaming) {
119 PW_PWPB_TEST_METHOD_CONTEXT(MixedService1, TestBidirectionalStreamRpc)
120 context;
121 ASSERT_FALSE(context.service().called_bidirectional_streaming_method);
122 context.call();
123 EXPECT_TRUE(context.service().called_bidirectional_streaming_method);
124 }
125
TEST(MixedService2,CallPwpbMethod_SyncUnary)126 TEST(MixedService2, CallPwpbMethod_SyncUnary) {
127 PW_PWPB_TEST_METHOD_CONTEXT(MixedService2, TestUnaryRpc) context;
128 Status status = context.call({});
129 EXPECT_EQ(Status::Unauthenticated(), status);
130 }
131
TEST(MixedService2,CallRawMethod_AsyncUnary)132 TEST(MixedService2, CallRawMethod_AsyncUnary) {
133 PW_RAW_TEST_METHOD_CONTEXT(MixedService2, TestAnotherUnaryRpc) context;
134 ASSERT_FALSE(context.service().called_async_unary_method);
135 context.call({});
136 EXPECT_TRUE(context.service().called_async_unary_method);
137 }
138
TEST(MixedService2,CallRawMethod_ServerStreaming)139 TEST(MixedService2, CallRawMethod_ServerStreaming) {
140 PW_RAW_TEST_METHOD_CONTEXT(MixedService2, TestServerStreamRpc) context;
141 ASSERT_FALSE(context.service().called_server_streaming_method);
142 context.call({});
143 EXPECT_TRUE(context.service().called_server_streaming_method);
144 }
145
TEST(MixedService2,CallPwpbMethod_ClientStreaming)146 TEST(MixedService2, CallPwpbMethod_ClientStreaming) {
147 PW_PWPB_TEST_METHOD_CONTEXT(MixedService2, TestClientStreamRpc) context;
148 ASSERT_FALSE(context.service().called_client_streaming_method);
149 context.call();
150 EXPECT_TRUE(context.service().called_client_streaming_method);
151 }
152
TEST(MixedService2,CallRawMethod_BidirectionalStreaming)153 TEST(MixedService2, CallRawMethod_BidirectionalStreaming) {
154 PW_RAW_TEST_METHOD_CONTEXT(MixedService2, TestBidirectionalStreamRpc) context;
155 ASSERT_FALSE(context.service().called_bidirectional_streaming_method);
156 context.call();
157 EXPECT_TRUE(context.service().called_bidirectional_streaming_method);
158 }
159
160 } // namespace
161 } // namespace pw::rpc
162