blob: 6017ff696a92e3bd588864604a321a6c68515179 [file] [log] [blame]
Wyatt Heplerafd29142020-11-23 07:58:59 -08001// 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"
Wyatt Hepler8ec2cf82021-07-09 17:18:41 -070016#include "pw_rpc/nanopb/test_method_context.h"
Wyatt Heplerc0ea3f92021-07-09 17:03:22 -070017#include "pw_rpc/raw/test_method_context.h"
Wyatt Heplerafd29142020-11-23 07:58:59 -080018#include "pw_rpc_test_protos/test.rpc.pb.h"
19
20namespace pw::rpc {
21namespace {
22
23class MixedService1 : public test::generated::TestService<MixedService1> {
24 public:
Wyatt Hepler82110182021-08-13 11:43:19 -070025 StatusWithSize TestUnaryRpc(ServerContext&, ConstByteSpan, ByteSpan) {
Wyatt Heplerafd29142020-11-23 07:58:59 -080026 return StatusWithSize(123);
27 }
28
Wyatt Hepler82110182021-08-13 11:43:19 -070029 void TestServerStreamRpc(ServerContext&,
30 const pw_rpc_test_TestRequest&,
31 ServerWriter<pw_rpc_test_TestStreamResponse>&) {
Wyatt Hepler07e3ba02021-07-02 00:54:13 -070032 called_server_streaming_method = true;
Wyatt Heplerafd29142020-11-23 07:58:59 -080033 }
34
Wyatt Hepler07e3ba02021-07-02 00:54:13 -070035 void TestClientStreamRpc(ServerContext&, RawServerReader&) {
36 called_client_streaming_method = true;
37 }
38
39 void TestBidirectionalStreamRpc(
40 ServerContext&,
41 ServerReaderWriter<pw_rpc_test_TestRequest,
42 pw_rpc_test_TestStreamResponse>&) {
43 called_bidirectional_streaming_method = true;
44 }
45
46 bool called_server_streaming_method = false;
47 bool called_client_streaming_method = false;
48 bool called_bidirectional_streaming_method = false;
Wyatt Heplerafd29142020-11-23 07:58:59 -080049};
50
51class MixedService2 : public test::generated::TestService<MixedService2> {
52 public:
Wyatt Hepler82110182021-08-13 11:43:19 -070053 Status TestUnaryRpc(ServerContext&,
54 const pw_rpc_test_TestRequest&,
55 pw_rpc_test_TestResponse&) {
Wyatt Heplerafd29142020-11-23 07:58:59 -080056 return Status::Unauthenticated();
57 }
58
Wyatt Hepler82110182021-08-13 11:43:19 -070059 void TestServerStreamRpc(ServerContext&, ConstByteSpan, RawServerWriter&) {
Wyatt Hepler07e3ba02021-07-02 00:54:13 -070060 called_server_streaming_method = true;
Wyatt Heplerafd29142020-11-23 07:58:59 -080061 }
62
Wyatt Hepler07e3ba02021-07-02 00:54:13 -070063 void TestClientStreamRpc(
64 ServerContext&,
65 ServerReader<pw_rpc_test_TestRequest, pw_rpc_test_TestStreamResponse>&) {
66 called_client_streaming_method = true;
67 }
68
69 void TestBidirectionalStreamRpc(ServerContext&, RawServerReaderWriter&) {
70 called_bidirectional_streaming_method = true;
71 }
72
73 bool called_server_streaming_method = false;
74 bool called_client_streaming_method = false;
75 bool called_bidirectional_streaming_method = false;
Wyatt Heplerafd29142020-11-23 07:58:59 -080076};
77
Wyatt Hepler07e3ba02021-07-02 00:54:13 -070078TEST(MixedService1, CallRawMethod_Unary) {
Wyatt Hepler82110182021-08-13 11:43:19 -070079 PW_RAW_TEST_METHOD_CONTEXT(MixedService1, TestUnaryRpc) context;
Wyatt Heplerafd29142020-11-23 07:58:59 -080080 StatusWithSize sws = context.call({});
81 EXPECT_TRUE(sws.ok());
82 EXPECT_EQ(123u, sws.size());
83}
84
Wyatt Hepler07e3ba02021-07-02 00:54:13 -070085TEST(MixedService1, CallNanopbMethod_ServerStreaming) {
Wyatt Hepler82110182021-08-13 11:43:19 -070086 PW_NANOPB_TEST_METHOD_CONTEXT(MixedService1, TestServerStreamRpc) context;
Wyatt Hepler07e3ba02021-07-02 00:54:13 -070087 ASSERT_FALSE(context.service().called_server_streaming_method);
Wyatt Heplerafd29142020-11-23 07:58:59 -080088 context.call({});
Wyatt Hepler07e3ba02021-07-02 00:54:13 -070089 EXPECT_TRUE(context.service().called_server_streaming_method);
Wyatt Heplerafd29142020-11-23 07:58:59 -080090}
91
Wyatt Hepler07e3ba02021-07-02 00:54:13 -070092TEST(MixedService1, CallRawMethod_ClientStreaming) {
93 PW_RAW_TEST_METHOD_CONTEXT(MixedService1, TestClientStreamRpc) context;
94 ASSERT_FALSE(context.service().called_client_streaming_method);
95 context.call();
96 EXPECT_TRUE(context.service().called_client_streaming_method);
97}
98
99TEST(MixedService1, CallNanopbMethod_BidirectionalStreaming) {
Wyatt Heplerfa6edcc2021-08-20 08:30:08 -0700100 PW_NANOPB_TEST_METHOD_CONTEXT(MixedService1, TestBidirectionalStreamRpc)
101 context;
102 ASSERT_FALSE(context.service().called_bidirectional_streaming_method);
103 context.call();
104 EXPECT_TRUE(context.service().called_bidirectional_streaming_method);
Wyatt Hepler07e3ba02021-07-02 00:54:13 -0700105}
106
107TEST(MixedService2, CallNanopbMethod_Unary) {
Wyatt Hepler82110182021-08-13 11:43:19 -0700108 PW_NANOPB_TEST_METHOD_CONTEXT(MixedService2, TestUnaryRpc) context;
Wyatt Heplerafd29142020-11-23 07:58:59 -0800109 Status status = context.call({});
110 EXPECT_EQ(Status::Unauthenticated(), status);
111}
112
Wyatt Hepler07e3ba02021-07-02 00:54:13 -0700113TEST(MixedService2, CallRawMethod_ServerStreaming) {
Wyatt Hepler82110182021-08-13 11:43:19 -0700114 PW_RAW_TEST_METHOD_CONTEXT(MixedService2, TestServerStreamRpc) context;
Wyatt Hepler07e3ba02021-07-02 00:54:13 -0700115 ASSERT_FALSE(context.service().called_server_streaming_method);
Wyatt Heplerafd29142020-11-23 07:58:59 -0800116 context.call({});
Wyatt Hepler07e3ba02021-07-02 00:54:13 -0700117 EXPECT_TRUE(context.service().called_server_streaming_method);
118}
119
120TEST(MixedService2, CallNanopbMethod_ClientStreaming) {
Wyatt Heplerfa6edcc2021-08-20 08:30:08 -0700121 PW_NANOPB_TEST_METHOD_CONTEXT(MixedService2, TestClientStreamRpc) context;
122 ASSERT_FALSE(context.service().called_client_streaming_method);
123 context.call();
124 EXPECT_TRUE(context.service().called_client_streaming_method);
Wyatt Hepler07e3ba02021-07-02 00:54:13 -0700125}
126
127TEST(MixedService2, CallRawMethod_BidirectionalStreaming) {
128 PW_RAW_TEST_METHOD_CONTEXT(MixedService2, TestBidirectionalStreamRpc) context;
129 ASSERT_FALSE(context.service().called_bidirectional_streaming_method);
130 context.call();
131 EXPECT_TRUE(context.service().called_bidirectional_streaming_method);
Wyatt Heplerafd29142020-11-23 07:58:59 -0800132}
133
134} // namespace
135} // namespace pw::rpc