Alexei Frolov | a4d7150 | 2020-10-14 12:43:14 -0700 | [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 | |
Wyatt Hepler | 8ec2cf8 | 2021-07-09 17:18:41 -0700 | [diff] [blame] | 15 | #include "pw_rpc/nanopb/internal/method_union.h" |
Alexei Frolov | a4d7150 | 2020-10-14 12:43:14 -0700 | [diff] [blame] | 16 | |
| 17 | #include <array> |
| 18 | |
| 19 | #include "gtest/gtest.h" |
Wyatt Hepler | fa6edcc | 2021-08-20 08:30:08 -0700 | [diff] [blame] | 20 | #include "pw_rpc/internal/test_utils.h" |
Alexei Frolov | a4d7150 | 2020-10-14 12:43:14 -0700 | [diff] [blame] | 21 | #include "pw_rpc_nanopb_private/internal_test_utils.h" |
Alexei Frolov | a4d7150 | 2020-10-14 12:43:14 -0700 | [diff] [blame] | 22 | #include "pw_rpc_test_protos/test.pb.h" |
| 23 | |
| 24 | namespace pw::rpc::internal { |
| 25 | namespace { |
| 26 | |
| 27 | using std::byte; |
| 28 | |
| 29 | template <typename Implementation> |
| 30 | class FakeGeneratedService : public Service { |
| 31 | public: |
| 32 | constexpr FakeGeneratedService(uint32_t id) : Service(id, kMethods) {} |
| 33 | |
| 34 | static constexpr std::array<NanopbMethodUnion, 4> kMethods = { |
Wyatt Hepler | 38f8708 | 2021-02-23 17:00:09 -0800 | [diff] [blame] | 35 | GetNanopbOrRawMethodFor<&Implementation::DoNothing, |
| 36 | MethodType::kUnary, |
| 37 | pw_rpc_test_Empty, |
| 38 | pw_rpc_test_Empty>( |
Alexei Frolov | a4d7150 | 2020-10-14 12:43:14 -0700 | [diff] [blame] | 39 | 10u, pw_rpc_test_Empty_fields, pw_rpc_test_Empty_fields), |
Wyatt Hepler | e95bd72 | 2020-11-23 07:49:47 -0800 | [diff] [blame] | 40 | GetNanopbOrRawMethodFor<&Implementation::RawStream, |
Wyatt Hepler | 38f8708 | 2021-02-23 17:00:09 -0800 | [diff] [blame] | 41 | MethodType::kServerStreaming, |
| 42 | pw_rpc_test_TestRequest, |
| 43 | pw_rpc_test_TestResponse>( |
Alexei Frolov | a4d7150 | 2020-10-14 12:43:14 -0700 | [diff] [blame] | 44 | 11u, pw_rpc_test_TestRequest_fields, pw_rpc_test_TestResponse_fields), |
Wyatt Hepler | 38f8708 | 2021-02-23 17:00:09 -0800 | [diff] [blame] | 45 | GetNanopbOrRawMethodFor<&Implementation::AddFive, |
| 46 | MethodType::kUnary, |
| 47 | pw_rpc_test_TestRequest, |
| 48 | pw_rpc_test_TestResponse>( |
Alexei Frolov | a4d7150 | 2020-10-14 12:43:14 -0700 | [diff] [blame] | 49 | 12u, pw_rpc_test_TestRequest_fields, pw_rpc_test_TestResponse_fields), |
Wyatt Hepler | e95bd72 | 2020-11-23 07:49:47 -0800 | [diff] [blame] | 50 | GetNanopbOrRawMethodFor<&Implementation::StartStream, |
Wyatt Hepler | 38f8708 | 2021-02-23 17:00:09 -0800 | [diff] [blame] | 51 | MethodType::kServerStreaming, |
| 52 | pw_rpc_test_TestRequest, |
| 53 | pw_rpc_test_TestResponse>( |
Alexei Frolov | a4d7150 | 2020-10-14 12:43:14 -0700 | [diff] [blame] | 54 | 13u, pw_rpc_test_TestRequest_fields, pw_rpc_test_TestResponse_fields), |
| 55 | }; |
| 56 | }; |
| 57 | |
| 58 | pw_rpc_test_TestRequest last_request; |
Wyatt Hepler | fa6edcc | 2021-08-20 08:30:08 -0700 | [diff] [blame] | 59 | NanopbServerWriter<pw_rpc_test_TestResponse> last_writer; |
Alexei Frolov | a4d7150 | 2020-10-14 12:43:14 -0700 | [diff] [blame] | 60 | RawServerWriter last_raw_writer; |
| 61 | |
| 62 | class FakeGeneratedServiceImpl |
| 63 | : public FakeGeneratedService<FakeGeneratedServiceImpl> { |
| 64 | public: |
| 65 | FakeGeneratedServiceImpl(uint32_t id) : FakeGeneratedService(id) {} |
| 66 | |
| 67 | Status AddFive(ServerContext&, |
| 68 | const pw_rpc_test_TestRequest& request, |
| 69 | pw_rpc_test_TestResponse& response) { |
| 70 | last_request = request; |
| 71 | response.value = request.integer + 5; |
| 72 | return Status::Unauthenticated(); |
| 73 | } |
| 74 | |
| 75 | StatusWithSize DoNothing(ServerContext&, ConstByteSpan, ByteSpan) { |
| 76 | return StatusWithSize::Unknown(); |
| 77 | } |
| 78 | |
| 79 | void RawStream(ServerContext&, ConstByteSpan, RawServerWriter& writer) { |
| 80 | last_raw_writer = std::move(writer); |
| 81 | } |
| 82 | |
| 83 | void StartStream(ServerContext&, |
| 84 | const pw_rpc_test_TestRequest& request, |
Wyatt Hepler | fa6edcc | 2021-08-20 08:30:08 -0700 | [diff] [blame] | 85 | NanopbServerWriter<pw_rpc_test_TestResponse>& writer) { |
Alexei Frolov | a4d7150 | 2020-10-14 12:43:14 -0700 | [diff] [blame] | 86 | last_request = request; |
| 87 | last_writer = std::move(writer); |
| 88 | } |
| 89 | }; |
| 90 | |
| 91 | TEST(NanopbMethodUnion, Raw_CallsUnaryMethod) { |
| 92 | const Method& method = |
| 93 | std::get<0>(FakeGeneratedServiceImpl::kMethods).method(); |
| 94 | ServerContextForTest<FakeGeneratedServiceImpl> context(method); |
Wyatt Hepler | 5ba8064 | 2021-06-18 12:56:17 -0700 | [diff] [blame] | 95 | method.Invoke(context.get(), context.request({})); |
Alexei Frolov | a4d7150 | 2020-10-14 12:43:14 -0700 | [diff] [blame] | 96 | |
| 97 | const Packet& response = context.output().sent_packet(); |
| 98 | EXPECT_EQ(response.status(), Status::Unknown()); |
| 99 | } |
| 100 | |
| 101 | TEST(NanopbMethodUnion, Raw_CallsServerStreamingMethod) { |
| 102 | PW_ENCODE_PB( |
| 103 | pw_rpc_test_TestRequest, request, .integer = 555, .status_code = 0); |
| 104 | |
| 105 | const Method& method = |
| 106 | std::get<1>(FakeGeneratedServiceImpl::kMethods).method(); |
| 107 | ServerContextForTest<FakeGeneratedServiceImpl> context(method); |
| 108 | |
Wyatt Hepler | 5ba8064 | 2021-06-18 12:56:17 -0700 | [diff] [blame] | 109 | method.Invoke(context.get(), context.request(request)); |
Alexei Frolov | a4d7150 | 2020-10-14 12:43:14 -0700 | [diff] [blame] | 110 | |
| 111 | EXPECT_TRUE(last_raw_writer.open()); |
Wyatt Hepler | d08e582 | 2021-02-18 17:53:38 -0800 | [diff] [blame] | 112 | EXPECT_EQ(OkStatus(), last_raw_writer.Finish()); |
Wyatt Hepler | 5ba8064 | 2021-06-18 12:56:17 -0700 | [diff] [blame] | 113 | EXPECT_EQ(context.output().sent_packet().type(), PacketType::RESPONSE); |
Alexei Frolov | a4d7150 | 2020-10-14 12:43:14 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | TEST(NanopbMethodUnion, Nanopb_CallsUnaryMethod) { |
| 117 | PW_ENCODE_PB( |
| 118 | pw_rpc_test_TestRequest, request, .integer = 123, .status_code = 3); |
| 119 | |
| 120 | const Method& method = |
| 121 | std::get<2>(FakeGeneratedServiceImpl::kMethods).method(); |
| 122 | ServerContextForTest<FakeGeneratedServiceImpl> context(method); |
Wyatt Hepler | 5ba8064 | 2021-06-18 12:56:17 -0700 | [diff] [blame] | 123 | method.Invoke(context.get(), context.request(request)); |
Alexei Frolov | a4d7150 | 2020-10-14 12:43:14 -0700 | [diff] [blame] | 124 | |
| 125 | const Packet& response = context.output().sent_packet(); |
| 126 | EXPECT_EQ(response.status(), Status::Unauthenticated()); |
| 127 | |
| 128 | // Field 1 (encoded as 1 << 3) with 128 as the value. |
| 129 | constexpr std::byte expected[]{ |
| 130 | std::byte{0x08}, std::byte{0x80}, std::byte{0x01}}; |
| 131 | |
| 132 | EXPECT_EQ(sizeof(expected), response.payload().size()); |
| 133 | EXPECT_EQ(0, |
| 134 | std::memcmp(expected, response.payload().data(), sizeof(expected))); |
| 135 | |
| 136 | EXPECT_EQ(123, last_request.integer); |
| 137 | EXPECT_EQ(3u, last_request.status_code); |
| 138 | } |
| 139 | |
| 140 | TEST(NanopbMethodUnion, Nanopb_CallsServerStreamingMethod) { |
| 141 | PW_ENCODE_PB( |
| 142 | pw_rpc_test_TestRequest, request, .integer = 555, .status_code = 0); |
| 143 | |
| 144 | const Method& method = |
| 145 | std::get<3>(FakeGeneratedServiceImpl::kMethods).method(); |
| 146 | ServerContextForTest<FakeGeneratedServiceImpl> context(method); |
| 147 | |
Wyatt Hepler | 5ba8064 | 2021-06-18 12:56:17 -0700 | [diff] [blame] | 148 | method.Invoke(context.get(), context.request(request)); |
Alexei Frolov | a4d7150 | 2020-10-14 12:43:14 -0700 | [diff] [blame] | 149 | |
| 150 | EXPECT_EQ(555, last_request.integer); |
| 151 | EXPECT_TRUE(last_writer.open()); |
| 152 | |
Wyatt Hepler | d08e582 | 2021-02-18 17:53:38 -0800 | [diff] [blame] | 153 | EXPECT_EQ(OkStatus(), last_writer.Finish()); |
Wyatt Hepler | 5ba8064 | 2021-06-18 12:56:17 -0700 | [diff] [blame] | 154 | EXPECT_EQ(context.output().sent_packet().type(), PacketType::RESPONSE); |
Alexei Frolov | a4d7150 | 2020-10-14 12:43:14 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | } // namespace |
| 158 | } // namespace pw::rpc::internal |