1 // Copyright 2020 The Pigweed Authors
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
7 // https://www.apache.org/licenses/LICENSE-2.0
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
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"
23 class MixedService1 : public test::generated::TestService<MixedService1> {
25 StatusWithSize TestRpc(ServerContext&, ConstByteSpan, ByteSpan) {
26 return StatusWithSize(123);
29 void TestStreamRpc(ServerContext&,
30 const pw_rpc_test_TestRequest&,
31 ServerWriter<pw_rpc_test_TestStreamResponse>&) {
32 called_streaming_method = true;
35 bool called_streaming_method = false;
38 class MixedService2 : public test::generated::TestService<MixedService2> {
40 Status TestRpc(ServerContext&,
41 const pw_rpc_test_TestRequest&,
42 pw_rpc_test_TestResponse&) {
43 return Status::Unauthenticated();
46 void TestStreamRpc(ServerContext&, ConstByteSpan, RawServerWriter&) {
47 called_streaming_method = true;
50 bool called_streaming_method = false;
53 TEST(MixedService1, CallRawMethod) {
54 PW_RAW_TEST_METHOD_CONTEXT(MixedService1, TestRpc) context;
55 StatusWithSize sws = context.call({});
56 EXPECT_TRUE(sws.ok());
57 EXPECT_EQ(123u, sws.size());
60 TEST(MixedService1, CallNanopbMethod) {
61 PW_NANOPB_TEST_METHOD_CONTEXT(MixedService1, TestStreamRpc) context;
62 ASSERT_FALSE(context.service().called_streaming_method);
64 EXPECT_TRUE(context.service().called_streaming_method);
67 TEST(MixedService2, CallNanopbMethod) {
68 PW_NANOPB_TEST_METHOD_CONTEXT(MixedService2, TestRpc) context;
69 Status status = context.call({});
70 EXPECT_EQ(Status::Unauthenticated(), status);
73 TEST(MixedService2, CallRawMethod) {
74 PW_RAW_TEST_METHOD_CONTEXT(MixedService2, TestStreamRpc) context;
75 ASSERT_FALSE(context.service().called_streaming_method);
77 EXPECT_TRUE(context.service().called_streaming_method);
81 } // namespace pw::rpc