blob: bddcea849581c64fd2a68335b40e822294fb3a14 [file] [log] [blame]
Alexei Frolov3e280922021-04-12 14:53:06 -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_server.h"
16
17#include "gtest/gtest.h"
18#include "pw_rpc/internal/packet.h"
Wyatt Heplerfa6edcc2021-08-20 08:30:08 -070019#include "pw_rpc/internal/test_utils.h"
Wyatt Hepler32a2b672022-01-28 16:58:40 -080020#include "pw_rpc/raw/fake_channel_output.h"
Wyatt Heplerc0ea3f92021-07-09 17:03:22 -070021#include "pw_rpc/raw/internal/method_union.h"
Alexei Frolov3e280922021-04-12 14:53:06 -070022#include "pw_rpc/service.h"
Alexei Frolov3e280922021-04-12 14:53:06 -070023
24namespace pw::rpc::internal {
25namespace {
26
27constexpr uint32_t kFakeChannelId = 1;
28constexpr uint32_t kFakeServiceId = 3;
29constexpr uint32_t kFakeMethodId = 10;
30
Wyatt Hepler32a2b672022-01-28 16:58:40 -080031RawFakeChannelOutput<1> output;
Alexei Frolov3e280922021-04-12 14:53:06 -070032rpc::Channel channels[] = {Channel::Create<kFakeChannelId>(&output)};
33
Wyatt Heplerb15c55b2022-02-11 09:26:05 -080034void FakeMethod(ConstByteSpan, RawUnaryResponder& responder) {
35 ASSERT_EQ(OkStatus(), responder.Finish({}, Status::Unimplemented()));
Alexei Frolov3e280922021-04-12 14:53:06 -070036}
37
38class FakeService : public Service {
39 public:
40 FakeService(uint32_t id) : Service(id, kMethods) {}
41
42 static constexpr std::array<RawMethodUnion, 1> kMethods = {
Wyatt Heplerb15c55b2022-02-11 09:26:05 -080043 RawMethod::AsynchronousUnary<FakeMethod>(kFakeMethodId),
Alexei Frolov3e280922021-04-12 14:53:06 -070044 };
45};
46
47FakeService service(kFakeServiceId);
48
49TEST(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
62TEST(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
Wyatt Hepler82db4b12021-09-23 09:10:12 -070074 // No calls are registered on the client, so nothing should happen. The
75 // ProcessPacket call still returns OK since the client handled it.
76 EXPECT_EQ(client_server.ProcessPacket(result.value(), output), OkStatus());
Alexei Frolov3e280922021-04-12 14:53:06 -070077}
78
79TEST(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