Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -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.h" |
| 16 | |
| 17 | #include "gtest/gtest.h" |
| 18 | #include "pw_rpc/internal/packet.h" |
| 19 | #include "pw_rpc_private/internal_test_utils.h" |
| 20 | |
| 21 | namespace pw::rpc { |
| 22 | namespace { |
| 23 | |
| 24 | using internal::BaseClientCall; |
| 25 | using internal::Packet; |
| 26 | using internal::PacketType; |
| 27 | |
| 28 | class TestClientCall : public BaseClientCall { |
| 29 | public: |
| 30 | constexpr TestClientCall(Channel* channel, |
| 31 | uint32_t service_id, |
| 32 | uint32_t method_id) |
| 33 | : BaseClientCall(channel, service_id, method_id, ProcessPacket) {} |
| 34 | |
| 35 | static void ProcessPacket(BaseClientCall& call, const Packet& packet) { |
| 36 | static_cast<TestClientCall&>(call).HandlePacket(packet); |
| 37 | } |
| 38 | |
| 39 | void HandlePacket(const Packet&) { invoked_ = true; } |
| 40 | |
| 41 | constexpr bool invoked() const { return invoked_; } |
| 42 | |
| 43 | private: |
| 44 | bool invoked_ = false; |
| 45 | }; |
| 46 | |
| 47 | TEST(Client, ProcessPacket_InvokesARegisteredClientCall) { |
| 48 | ClientContextForTest context; |
| 49 | |
| 50 | TestClientCall call( |
| 51 | &context.channel(), context.kServiceId, context.kMethodId); |
| 52 | EXPECT_EQ(context.SendResponse(Status::Ok(), {}), Status::Ok()); |
| 53 | |
| 54 | EXPECT_TRUE(call.invoked()); |
| 55 | } |
| 56 | |
| 57 | TEST(Client, ProcessPacket_ReturnsNotFoundOnUnregisteredCall) { |
| 58 | ClientContextForTest context; |
| 59 | EXPECT_EQ(context.SendResponse(Status::OK, {}), Status::NotFound()); |
| 60 | } |
| 61 | |
| 62 | TEST(Client, ProcessPacket_ReturnsDataLossOnBadPacket) { |
| 63 | ClientContextForTest context; |
| 64 | |
| 65 | constexpr std::byte bad_packet[]{ |
| 66 | std::byte{0xab}, std::byte{0xcd}, std::byte{0xef}}; |
| 67 | EXPECT_EQ(context.client().ProcessPacket(bad_packet), Status::DataLoss()); |
| 68 | } |
| 69 | |
| 70 | TEST(Client, ProcessPacket_ReturnsInvalidArgumentOnServerPacket) { |
| 71 | ClientContextForTest context; |
| 72 | EXPECT_EQ(context.SendPacket(PacketType::REQUEST), Status::InvalidArgument()); |
| 73 | EXPECT_EQ(context.SendPacket(PacketType::CANCEL_SERVER_STREAM), |
| 74 | Status::InvalidArgument()); |
| 75 | } |
| 76 | |
| 77 | } // namespace |
| 78 | } // namespace pw::rpc |