| // Copyright 2020 The Pigweed Authors |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| // use this file except in compliance with the License. You may obtain a copy of |
| // the License at |
| // |
| // https://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| // License for the specific language governing permissions and limitations under |
| // the License. |
| |
| // Copyright 2020 The Pigweed Authors |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| // use this file except in compliance with the License. You may obtain a copy of |
| // the License at |
| // |
| // https://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| // License for the specific language governing permissions and limitations under |
| // the License. |
| |
| #include "pw_hdlc/rpc_channel.h" |
| |
| #include <algorithm> |
| #include <array> |
| #include <cstddef> |
| |
| #include "gtest/gtest.h" |
| #include "pw_bytes/array.h" |
| #include "pw_stream/memory_stream.h" |
| |
| using std::byte; |
| |
| namespace pw::hdlc { |
| namespace { |
| |
| constexpr byte kFlag = byte{0x7E}; |
| constexpr uint8_t kAddress = 0x7b; // 123 |
| constexpr byte kControl = byte{0x3}; // UI-frame control sequence. |
| |
| // Size of the in-memory buffer to use for this test. |
| constexpr size_t kSinkBufferSize = 15; |
| |
| TEST(RpcChannelOutput, 1BytePayload) { |
| std::array<byte, kSinkBufferSize> channel_output_buffer; |
| stream::MemoryWriterBuffer<kSinkBufferSize> memory_writer; |
| |
| RpcChannelOutput output( |
| memory_writer, channel_output_buffer, kAddress, "RpcChannelOutput"); |
| |
| constexpr byte test_data = byte{'A'}; |
| auto buffer = output.AcquireBuffer(); |
| std::memcpy(buffer.data(), &test_data, sizeof(test_data)); |
| |
| constexpr auto expected = bytes::Concat( |
| kFlag, kAddress, kControl, 'A', uint32_t{0x8D137C66}, kFlag); |
| |
| EXPECT_EQ(OkStatus(), |
| output.SendAndReleaseBuffer(buffer.first(sizeof(test_data)))); |
| |
| ASSERT_EQ(memory_writer.bytes_written(), expected.size()); |
| EXPECT_EQ( |
| std::memcmp( |
| memory_writer.data(), expected.data(), memory_writer.bytes_written()), |
| 0); |
| } |
| |
| TEST(RpcChannelOutput, EscapingPayloadTest) { |
| std::array<byte, kSinkBufferSize> channel_output_buffer; |
| stream::MemoryWriterBuffer<kSinkBufferSize> memory_writer; |
| |
| RpcChannelOutput output( |
| memory_writer, channel_output_buffer, kAddress, "RpcChannelOutput"); |
| |
| constexpr auto test_data = bytes::Array<0x7D>(); |
| auto buffer = output.AcquireBuffer(); |
| std::memcpy(buffer.data(), test_data.data(), test_data.size()); |
| |
| constexpr auto expected = bytes::Concat(kFlag, |
| kAddress, |
| kControl, |
| byte{0x7d}, |
| byte{0x7d} ^ byte{0x20}, |
| uint32_t{0xA27C00E1}, |
| kFlag); |
| EXPECT_EQ(OkStatus(), |
| output.SendAndReleaseBuffer(buffer.first(test_data.size()))); |
| |
| ASSERT_EQ(memory_writer.bytes_written(), 10u); |
| EXPECT_EQ( |
| std::memcmp( |
| memory_writer.data(), expected.data(), memory_writer.bytes_written()), |
| 0); |
| } |
| |
| TEST(RpcChannelOutputBuffer, 1BytePayload) { |
| stream::MemoryWriterBuffer<kSinkBufferSize> memory_writer; |
| |
| RpcChannelOutputBuffer<kSinkBufferSize> output( |
| memory_writer, kAddress, "RpcChannelOutput"); |
| |
| constexpr byte test_data = byte{'A'}; |
| auto buffer = output.AcquireBuffer(); |
| std::memcpy(buffer.data(), &test_data, sizeof(test_data)); |
| |
| constexpr auto expected = bytes::Concat( |
| kFlag, kAddress, kControl, 'A', uint32_t{0x8D137C66}, kFlag); |
| |
| EXPECT_EQ(OkStatus(), |
| output.SendAndReleaseBuffer(buffer.first(sizeof(test_data)))); |
| |
| ASSERT_EQ(memory_writer.bytes_written(), expected.size()); |
| EXPECT_EQ( |
| std::memcmp( |
| memory_writer.data(), expected.data(), memory_writer.bytes_written()), |
| 0); |
| } |
| |
| } // namespace |
| } // namespace pw::hdlc |