blob: a5993fea4890f5cb9fdffb791ce79c8307a94a80 [file] [log] [blame]
Alexei Frolov4d2adde2020-08-04 10:19:24 -07001// 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
21namespace pw::rpc {
22namespace {
23
24using internal::BaseClientCall;
25using internal::Packet;
26using internal::PacketType;
27
28class 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
47TEST(Client, ProcessPacket_InvokesARegisteredClientCall) {
48 ClientContextForTest context;
49
50 TestClientCall call(
Ewout van Bekkum5ea33402021-03-31 11:00:02 -070051 &context.channel(), context.service_id(), context.method_id());
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -080052 EXPECT_EQ(context.SendResponse(OkStatus(), {}), OkStatus());
Alexei Frolov4d2adde2020-08-04 10:19:24 -070053
54 EXPECT_TRUE(call.invoked());
55}
56
Alexei Frolov062ed182020-09-28 16:23:06 -070057TEST(Client, ProcessPacket_SendsClientErrorOnUnregisteredCall) {
Alexei Frolov4d2adde2020-08-04 10:19:24 -070058 ClientContextForTest context;
Alexei Frolov062ed182020-09-28 16:23:06 -070059
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -080060 EXPECT_EQ(context.SendResponse(OkStatus(), {}), Status::NotFound());
Alexei Frolov062ed182020-09-28 16:23:06 -070061
62 ASSERT_EQ(context.output().packet_count(), 1u);
63 const Packet& packet = context.output().sent_packet();
64 EXPECT_EQ(packet.type(), PacketType::CLIENT_ERROR);
Ewout van Bekkum5ea33402021-03-31 11:00:02 -070065 EXPECT_EQ(packet.channel_id(), context.channel_id());
66 EXPECT_EQ(packet.service_id(), context.service_id());
67 EXPECT_EQ(packet.method_id(), context.method_id());
Alexei Frolov062ed182020-09-28 16:23:06 -070068 EXPECT_TRUE(packet.payload().empty());
69 EXPECT_EQ(packet.status(), Status::FailedPrecondition());
Alexei Frolov4d2adde2020-08-04 10:19:24 -070070}
71
72TEST(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
80TEST(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