Alexei Frolov | 3e28092 | 2021-04-12 14:53:06 -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 | |
| 15 | #include "pw_rpc/client_server.h" |
| 16 | |
| 17 | #include "gtest/gtest.h" |
| 18 | #include "pw_rpc/internal/packet.h" |
Wyatt Hepler | c0ea3f9 | 2021-07-09 17:03:22 -0700 | [diff] [blame] | 19 | #include "pw_rpc/raw/internal/method_union.h" |
Alexei Frolov | 3e28092 | 2021-04-12 14:53:06 -0700 | [diff] [blame] | 20 | #include "pw_rpc/server_context.h" |
| 21 | #include "pw_rpc/service.h" |
| 22 | #include "pw_rpc_private/internal_test_utils.h" |
| 23 | |
| 24 | namespace pw::rpc::internal { |
| 25 | namespace { |
| 26 | |
| 27 | constexpr uint32_t kFakeChannelId = 1; |
| 28 | constexpr uint32_t kFakeServiceId = 3; |
| 29 | constexpr uint32_t kFakeMethodId = 10; |
| 30 | |
| 31 | TestOutput<32> output; |
| 32 | rpc::Channel channels[] = {Channel::Create<kFakeChannelId>(&output)}; |
| 33 | |
| 34 | StatusWithSize FakeMethod(ServerContext&, ConstByteSpan, ByteSpan) { |
| 35 | return StatusWithSize::Unimplemented(); |
| 36 | } |
| 37 | |
| 38 | class FakeService : public Service { |
| 39 | public: |
| 40 | FakeService(uint32_t id) : Service(id, kMethods) {} |
| 41 | |
| 42 | static constexpr std::array<RawMethodUnion, 1> kMethods = { |
| 43 | RawMethod::Unary<FakeMethod>(kFakeMethodId), |
| 44 | }; |
| 45 | }; |
| 46 | |
| 47 | FakeService service(kFakeServiceId); |
| 48 | |
| 49 | TEST(ClientServer, ProcessPacket_CallsServer) { |
| 50 | ClientServer client_server(channels); |
| 51 | client_server.server().RegisterService(service); |
| 52 | |
| 53 | Packet packet( |
| 54 | PacketType::REQUEST, kFakeChannelId, kFakeServiceId, kFakeMethodId); |
| 55 | std::array<std::byte, 32> buffer; |
| 56 | Result result = packet.Encode(buffer); |
| 57 | EXPECT_EQ(result.status(), OkStatus()); |
| 58 | |
| 59 | EXPECT_EQ(client_server.ProcessPacket(result.value(), output), OkStatus()); |
| 60 | } |
| 61 | |
| 62 | TEST(ClientServer, ProcessPacket_CallsClient) { |
| 63 | ClientServer client_server(channels); |
| 64 | client_server.server().RegisterService(service); |
| 65 | |
| 66 | // Same packet as above, but type RESPONSE will skip the server and call into |
| 67 | // the client. |
| 68 | Packet packet( |
| 69 | PacketType::RESPONSE, kFakeChannelId, kFakeServiceId, kFakeMethodId); |
| 70 | std::array<std::byte, 32> buffer; |
| 71 | Result result = packet.Encode(buffer); |
| 72 | EXPECT_EQ(result.status(), OkStatus()); |
| 73 | |
| 74 | // No calls are registered on the client, so this should fail. |
| 75 | EXPECT_EQ(client_server.ProcessPacket(result.value(), output), |
| 76 | Status::NotFound()); |
| 77 | } |
| 78 | |
| 79 | TEST(ClientServer, ProcessPacket_BadData) { |
| 80 | ClientServer client_server(channels); |
| 81 | EXPECT_EQ(client_server.ProcessPacket({}, output), Status::DataLoss()); |
| 82 | } |
| 83 | |
| 84 | } // namespace |
| 85 | } // namespace pw::rpc::internal |