Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_rpc / client_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 "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(OkStatus(), {}), OkStatus());
53
54   EXPECT_TRUE(call.invoked());
55 }
56
57 TEST(Client, ProcessPacket_SendsClientErrorOnUnregisteredCall) {
58   ClientContextForTest context;
59
60   EXPECT_EQ(context.SendResponse(OkStatus(), {}), Status::NotFound());
61
62   ASSERT_EQ(context.output().packet_count(), 1u);
63   const Packet& packet = context.output().sent_packet();
64   EXPECT_EQ(packet.type(), PacketType::CLIENT_ERROR);
65   EXPECT_EQ(packet.channel_id(), context.kChannelId);
66   EXPECT_EQ(packet.service_id(), context.kServiceId);
67   EXPECT_EQ(packet.method_id(), context.kMethodId);
68   EXPECT_TRUE(packet.payload().empty());
69   EXPECT_EQ(packet.status(), Status::FailedPrecondition());
70 }
71
72 TEST(Client, ProcessPacket_ReturnsDataLossOnBadPacket) {
73   ClientContextForTest context;
74
75   constexpr std::byte bad_packet[]{
76       std::byte{0xab}, std::byte{0xcd}, std::byte{0xef}};
77   EXPECT_EQ(context.client().ProcessPacket(bad_packet), Status::DataLoss());
78 }
79
80 TEST(Client, ProcessPacket_ReturnsInvalidArgumentOnServerPacket) {
81   ClientContextForTest context;
82   EXPECT_EQ(context.SendPacket(PacketType::REQUEST), Status::InvalidArgument());
83   EXPECT_EQ(context.SendPacket(PacketType::CANCEL_SERVER_STREAM),
84             Status::InvalidArgument());
85 }
86
87 }  // namespace
88 }  // namespace pw::rpc