Wyatt Hepler | afd2914 | 2020-11-23 07:58:59 -0800 | [diff] [blame] | 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" |
Wyatt Hepler | 8ec2cf8 | 2021-07-09 17:18:41 -0700 | [diff] [blame] | 16 | #include "pw_rpc/nanopb/test_method_context.h" |
Wyatt Hepler | c0ea3f9 | 2021-07-09 17:03:22 -0700 | [diff] [blame] | 17 | #include "pw_rpc/raw/test_method_context.h" |
Wyatt Hepler | afd2914 | 2020-11-23 07:58:59 -0800 | [diff] [blame] | 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: |
Wyatt Hepler | 8211018 | 2021-08-13 11:43:19 -0700 | [diff] [blame] | 25 | StatusWithSize TestUnaryRpc(ServerContext&, ConstByteSpan, ByteSpan) { |
Wyatt Hepler | afd2914 | 2020-11-23 07:58:59 -0800 | [diff] [blame] | 26 | return StatusWithSize(123); |
| 27 | } |
| 28 | |
Wyatt Hepler | 8211018 | 2021-08-13 11:43:19 -0700 | [diff] [blame] | 29 | void TestServerStreamRpc(ServerContext&, |
| 30 | const pw_rpc_test_TestRequest&, |
| 31 | ServerWriter<pw_rpc_test_TestStreamResponse>&) { |
Wyatt Hepler | 07e3ba0 | 2021-07-02 00:54:13 -0700 | [diff] [blame] | 32 | called_server_streaming_method = true; |
Wyatt Hepler | afd2914 | 2020-11-23 07:58:59 -0800 | [diff] [blame] | 33 | } |
| 34 | |
Wyatt Hepler | 07e3ba0 | 2021-07-02 00:54:13 -0700 | [diff] [blame] | 35 | 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 Hepler | afd2914 | 2020-11-23 07:58:59 -0800 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | class MixedService2 : public test::generated::TestService<MixedService2> { |
| 52 | public: |
Wyatt Hepler | 8211018 | 2021-08-13 11:43:19 -0700 | [diff] [blame] | 53 | Status TestUnaryRpc(ServerContext&, |
| 54 | const pw_rpc_test_TestRequest&, |
| 55 | pw_rpc_test_TestResponse&) { |
Wyatt Hepler | afd2914 | 2020-11-23 07:58:59 -0800 | [diff] [blame] | 56 | return Status::Unauthenticated(); |
| 57 | } |
| 58 | |
Wyatt Hepler | 8211018 | 2021-08-13 11:43:19 -0700 | [diff] [blame] | 59 | void TestServerStreamRpc(ServerContext&, ConstByteSpan, RawServerWriter&) { |
Wyatt Hepler | 07e3ba0 | 2021-07-02 00:54:13 -0700 | [diff] [blame] | 60 | called_server_streaming_method = true; |
Wyatt Hepler | afd2914 | 2020-11-23 07:58:59 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Wyatt Hepler | 07e3ba0 | 2021-07-02 00:54:13 -0700 | [diff] [blame] | 63 | 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 Hepler | afd2914 | 2020-11-23 07:58:59 -0800 | [diff] [blame] | 76 | }; |
| 77 | |
Wyatt Hepler | 07e3ba0 | 2021-07-02 00:54:13 -0700 | [diff] [blame] | 78 | TEST(MixedService1, CallRawMethod_Unary) { |
Wyatt Hepler | 8211018 | 2021-08-13 11:43:19 -0700 | [diff] [blame] | 79 | PW_RAW_TEST_METHOD_CONTEXT(MixedService1, TestUnaryRpc) context; |
Wyatt Hepler | afd2914 | 2020-11-23 07:58:59 -0800 | [diff] [blame] | 80 | StatusWithSize sws = context.call({}); |
| 81 | EXPECT_TRUE(sws.ok()); |
| 82 | EXPECT_EQ(123u, sws.size()); |
| 83 | } |
| 84 | |
Wyatt Hepler | 07e3ba0 | 2021-07-02 00:54:13 -0700 | [diff] [blame] | 85 | TEST(MixedService1, CallNanopbMethod_ServerStreaming) { |
Wyatt Hepler | 8211018 | 2021-08-13 11:43:19 -0700 | [diff] [blame] | 86 | PW_NANOPB_TEST_METHOD_CONTEXT(MixedService1, TestServerStreamRpc) context; |
Wyatt Hepler | 07e3ba0 | 2021-07-02 00:54:13 -0700 | [diff] [blame] | 87 | ASSERT_FALSE(context.service().called_server_streaming_method); |
Wyatt Hepler | afd2914 | 2020-11-23 07:58:59 -0800 | [diff] [blame] | 88 | context.call({}); |
Wyatt Hepler | 07e3ba0 | 2021-07-02 00:54:13 -0700 | [diff] [blame] | 89 | EXPECT_TRUE(context.service().called_server_streaming_method); |
Wyatt Hepler | afd2914 | 2020-11-23 07:58:59 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Wyatt Hepler | 07e3ba0 | 2021-07-02 00:54:13 -0700 | [diff] [blame] | 92 | TEST(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 | |
| 99 | TEST(MixedService1, CallNanopbMethod_BidirectionalStreaming) { |
Wyatt Hepler | fa6edcc | 2021-08-20 08:30:08 -0700 | [diff] [blame] | 100 | 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 Hepler | 07e3ba0 | 2021-07-02 00:54:13 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | TEST(MixedService2, CallNanopbMethod_Unary) { |
Wyatt Hepler | 8211018 | 2021-08-13 11:43:19 -0700 | [diff] [blame] | 108 | PW_NANOPB_TEST_METHOD_CONTEXT(MixedService2, TestUnaryRpc) context; |
Wyatt Hepler | afd2914 | 2020-11-23 07:58:59 -0800 | [diff] [blame] | 109 | Status status = context.call({}); |
| 110 | EXPECT_EQ(Status::Unauthenticated(), status); |
| 111 | } |
| 112 | |
Wyatt Hepler | 07e3ba0 | 2021-07-02 00:54:13 -0700 | [diff] [blame] | 113 | TEST(MixedService2, CallRawMethod_ServerStreaming) { |
Wyatt Hepler | 8211018 | 2021-08-13 11:43:19 -0700 | [diff] [blame] | 114 | PW_RAW_TEST_METHOD_CONTEXT(MixedService2, TestServerStreamRpc) context; |
Wyatt Hepler | 07e3ba0 | 2021-07-02 00:54:13 -0700 | [diff] [blame] | 115 | ASSERT_FALSE(context.service().called_server_streaming_method); |
Wyatt Hepler | afd2914 | 2020-11-23 07:58:59 -0800 | [diff] [blame] | 116 | context.call({}); |
Wyatt Hepler | 07e3ba0 | 2021-07-02 00:54:13 -0700 | [diff] [blame] | 117 | EXPECT_TRUE(context.service().called_server_streaming_method); |
| 118 | } |
| 119 | |
| 120 | TEST(MixedService2, CallNanopbMethod_ClientStreaming) { |
Wyatt Hepler | fa6edcc | 2021-08-20 08:30:08 -0700 | [diff] [blame] | 121 | 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 Hepler | 07e3ba0 | 2021-07-02 00:54:13 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | TEST(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 Hepler | afd2914 | 2020-11-23 07:58:59 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | } // namespace |
| 135 | } // namespace pw::rpc |