Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_rpc / nanopb / method_lookup_test.cc
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 : public test::generated::TestService<MixedService1> {
24  public:
25   StatusWithSize TestRpc(ServerContext&, ConstByteSpan, ByteSpan) {
26     return StatusWithSize(123);
27   }
28
29   void TestStreamRpc(ServerContext&,
30                      const pw_rpc_test_TestRequest&,
31                      ServerWriter<pw_rpc_test_TestStreamResponse>&) {
32     called_streaming_method = true;
33   }
34
35   bool called_streaming_method = false;
36 };
37
38 class MixedService2 : public test::generated::TestService<MixedService2> {
39  public:
40   Status TestRpc(ServerContext&,
41                  const pw_rpc_test_TestRequest&,
42                  pw_rpc_test_TestResponse&) {
43     return Status::Unauthenticated();
44   }
45
46   void TestStreamRpc(ServerContext&, ConstByteSpan, RawServerWriter&) {
47     called_streaming_method = true;
48   }
49
50   bool called_streaming_method = false;
51 };
52
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());
58 }
59
60 TEST(MixedService1, CallNanopbMethod) {
61   PW_NANOPB_TEST_METHOD_CONTEXT(MixedService1, TestStreamRpc) context;
62   ASSERT_FALSE(context.service().called_streaming_method);
63   context.call({});
64   EXPECT_TRUE(context.service().called_streaming_method);
65 }
66
67 TEST(MixedService2, CallNanopbMethod) {
68   PW_NANOPB_TEST_METHOD_CONTEXT(MixedService2, TestRpc) context;
69   Status status = context.call({});
70   EXPECT_EQ(Status::Unauthenticated(), status);
71 }
72
73 TEST(MixedService2, CallRawMethod) {
74   PW_RAW_TEST_METHOD_CONTEXT(MixedService2, TestStreamRpc) context;
75   ASSERT_FALSE(context.service().called_streaming_method);
76   context.call({});
77   EXPECT_TRUE(context.service().called_streaming_method);
78 }
79
80 }  // namespace
81 }  // namespace pw::rpc