Wyatt Hepler | 671946e | 2020-06-09 14:39:33 -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/channel.h" |
| 16 | |
| 17 | #include "gtest/gtest.h" |
| 18 | #include "pw_rpc/internal/packet.h" |
| 19 | #include "pw_rpc_private/test_utils.h" |
| 20 | |
| 21 | namespace pw::rpc::internal { |
| 22 | namespace { |
| 23 | |
| 24 | using std::byte; |
| 25 | |
| 26 | TEST(ChannelOutput, Name) { |
| 27 | class NameTester : public ChannelOutput { |
| 28 | public: |
| 29 | NameTester(const char* name) : ChannelOutput(name) {} |
Wyatt Hepler | e2cbadf | 2020-06-22 11:21:45 -0700 | [diff] [blame^] | 30 | std::span<std::byte> AcquireBuffer() override { return {}; } |
Wyatt Hepler | 671946e | 2020-06-09 14:39:33 -0700 | [diff] [blame] | 31 | 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 | |
| 38 | constexpr Packet kTestPacket(PacketType::RPC, 1, 42, 100); |
| 39 | const size_t kReservedSize = 2 /* type */ + 2 /* channel */ + 2 /* service */ + |
| 40 | 2 /* method */ + 2 /* payload key */ + |
| 41 | 2 /* status */; |
| 42 | |
| 43 | TEST(Channel, TestPacket_ReservedSizeMatchesMinEncodedSizeBytes) { |
| 44 | EXPECT_EQ(kReservedSize, kTestPacket.MinEncodedSizeBytes()); |
| 45 | } |
| 46 | |
| 47 | TEST(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 | |
| 55 | TEST(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 | |
| 65 | TEST(Channel, OutputBuffer_ExactFit) { |
| 66 | TestOutput<kReservedSize> output; |
| 67 | internal::Channel channel(100, &output); |
| 68 | |
| 69 | Channel::OutputBuffer output_buffer(channel.AcquireBuffer()); |
Wyatt Hepler | e2cbadf | 2020-06-22 11:21:45 -0700 | [diff] [blame^] | 70 | const std::span payload = output_buffer.payload(kTestPacket); |
Wyatt Hepler | 671946e | 2020-06-09 14:39:33 -0700 | [diff] [blame] | 71 | |
| 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 | |
| 78 | TEST(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 | |
| 91 | TEST(Channel, OutputBuffer_ExtraRoom) { |
| 92 | TestOutput<kReservedSize * 3> output; |
| 93 | internal::Channel channel(100, &output); |
| 94 | |
| 95 | Channel::OutputBuffer output_buffer = channel.AcquireBuffer(); |
Wyatt Hepler | e2cbadf | 2020-06-22 11:21:45 -0700 | [diff] [blame^] | 96 | const std::span payload = output_buffer.payload(kTestPacket); |
Wyatt Hepler | 671946e | 2020-06-09 14:39:33 -0700 | [diff] [blame] | 97 | |
| 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 |