blob: b63535b5e0b52f7f2797ea74cdea8251488e0ebe [file] [log] [blame]
Wyatt Hepler671946e2020-06-09 14:39:33 -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/channel.h"
16
17#include "gtest/gtest.h"
18#include "pw_rpc/internal/packet.h"
19#include "pw_rpc_private/test_utils.h"
20
21namespace pw::rpc::internal {
22namespace {
23
24using std::byte;
25
26TEST(ChannelOutput, Name) {
27 class NameTester : public ChannelOutput {
28 public:
29 NameTester(const char* name) : ChannelOutput(name) {}
Wyatt Heplere2cbadf2020-06-22 11:21:45 -070030 std::span<std::byte> AcquireBuffer() override { return {}; }
Wyatt Hepler671946e2020-06-09 14:39:33 -070031 void SendAndReleaseBuffer(size_t) override {}
32 };
33
34 EXPECT_STREQ("hello_world", NameTester("hello_world").name());
35 EXPECT_EQ(nullptr, NameTester(nullptr).name());
36}
37
38constexpr Packet kTestPacket(PacketType::RPC, 1, 42, 100);
39const size_t kReservedSize = 2 /* type */ + 2 /* channel */ + 2 /* service */ +
40 2 /* method */ + 2 /* payload key */ +
41 2 /* status */;
42
43TEST(Channel, TestPacket_ReservedSizeMatchesMinEncodedSizeBytes) {
44 EXPECT_EQ(kReservedSize, kTestPacket.MinEncodedSizeBytes());
45}
46
47TEST(Channel, OutputBuffer_EmptyBuffer) {
48 TestOutput<0> output;
49 internal::Channel channel(100, &output);
50
51 Channel::OutputBuffer buffer = channel.AcquireBuffer();
52 EXPECT_TRUE(buffer.payload(kTestPacket).empty());
53}
54
55TEST(Channel, OutputBuffer_TooSmall) {
56 TestOutput<kReservedSize - 1> output;
57 internal::Channel channel(100, &output);
58
59 Channel::OutputBuffer output_buffer = channel.AcquireBuffer();
60 EXPECT_TRUE(output_buffer.payload(kTestPacket).empty());
61
62 EXPECT_EQ(Status::INTERNAL, channel.Send(output_buffer, kTestPacket));
63}
64
65TEST(Channel, OutputBuffer_ExactFit) {
66 TestOutput<kReservedSize> output;
67 internal::Channel channel(100, &output);
68
69 Channel::OutputBuffer output_buffer(channel.AcquireBuffer());
Wyatt Heplere2cbadf2020-06-22 11:21:45 -070070 const std::span payload = output_buffer.payload(kTestPacket);
Wyatt Hepler671946e2020-06-09 14:39:33 -070071
72 EXPECT_EQ(payload.size(), output.buffer().size() - kReservedSize);
73 EXPECT_EQ(output.buffer().data() + kReservedSize, payload.data());
74
75 EXPECT_EQ(Status::OK, channel.Send(output_buffer, kTestPacket));
76}
77
78TEST(Channel, OutputBuffer_PayloadDoesNotFit_ReportsError) {
79 TestOutput<kReservedSize> output;
80 internal::Channel channel(100, &output);
81
82 Channel::OutputBuffer output_buffer(channel.AcquireBuffer());
83
84 Packet packet = kTestPacket;
85 byte data[1] = {};
86 packet.set_payload(data);
87
88 EXPECT_EQ(Status::INTERNAL, channel.Send(output_buffer, packet));
89}
90
91TEST(Channel, OutputBuffer_ExtraRoom) {
92 TestOutput<kReservedSize * 3> output;
93 internal::Channel channel(100, &output);
94
95 Channel::OutputBuffer output_buffer = channel.AcquireBuffer();
Wyatt Heplere2cbadf2020-06-22 11:21:45 -070096 const std::span payload = output_buffer.payload(kTestPacket);
Wyatt Hepler671946e2020-06-09 14:39:33 -070097
98 EXPECT_EQ(payload.size(), output.buffer().size() - kReservedSize);
99 EXPECT_EQ(output.buffer().data() + kReservedSize, payload.data());
100
101 EXPECT_EQ(Status::OK, channel.Send(output_buffer, kTestPacket));
102}
103
104} // namespace
105} // namespace pw::rpc::internal