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