1 // Copyright 2020 The Pigweed Authors
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
7 // https://www.apache.org/licenses/LICENSE-2.0
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
15 // Copyright 2020 The Pigweed Authors
17 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
18 // use this file except in compliance with the License. You may obtain a copy of
21 // https://www.apache.org/licenses/LICENSE-2.0
23 // Unless required by applicable law or agreed to in writing, software
24 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
26 // License for the specific language governing permissions and limitations under
29 #include "pw_hdlc_lite/rpc_channel.h"
35 #include "gtest/gtest.h"
36 #include "pw_bytes/array.h"
37 #include "pw_stream/memory_stream.h"
41 namespace pw::hdlc_lite {
44 constexpr byte kFlag = byte{0x7E};
45 constexpr uint8_t kAddress = 0x7b; // 123
46 constexpr byte kControl = byte{0};
48 // Size of the in-memory buffer to use for this test.
49 constexpr size_t kSinkBufferSize = 15;
51 TEST(RpcChannelOutput, 1BytePayload) {
52 std::array<byte, kSinkBufferSize> channel_output_buffer;
53 stream::MemoryWriterBuffer<kSinkBufferSize> memory_writer;
55 RpcChannelOutput output(
56 memory_writer, channel_output_buffer, kAddress, "RpcChannelOutput");
58 constexpr byte test_data = byte{'A'};
59 std::memcpy(output.AcquireBuffer().data(), &test_data, sizeof(test_data));
61 constexpr auto expected = bytes::Concat(
62 kFlag, kAddress, kControl, 'A', uint32_t{0xA63E2FA5}, kFlag);
64 EXPECT_EQ(Status::Ok(), output.SendAndReleaseBuffer(sizeof(test_data)));
66 ASSERT_EQ(memory_writer.bytes_written(), expected.size());
69 memory_writer.data(), expected.data(), memory_writer.bytes_written()),
73 TEST(RpcChannelOutput, EscapingPayloadTest) {
74 std::array<byte, kSinkBufferSize> channel_output_buffer;
75 stream::MemoryWriterBuffer<kSinkBufferSize> memory_writer;
77 RpcChannelOutput output(
78 memory_writer, channel_output_buffer, kAddress, "RpcChannelOutput");
80 constexpr auto test_data = bytes::Array<0x7D>();
82 output.AcquireBuffer().data(), test_data.data(), test_data.size());
84 constexpr auto expected = bytes::Concat(kFlag,
88 byte{0x7d} ^ byte{0x20},
91 EXPECT_EQ(Status::Ok(), output.SendAndReleaseBuffer(test_data.size()));
93 ASSERT_EQ(memory_writer.bytes_written(), 10u);
96 memory_writer.data(), expected.data(), memory_writer.bytes_written()),
100 TEST(RpcChannelOutputBuffer, 1BytePayload) {
101 stream::MemoryWriterBuffer<kSinkBufferSize> memory_writer;
103 RpcChannelOutputBuffer<kSinkBufferSize> output(
104 memory_writer, kAddress, "RpcChannelOutput");
106 constexpr byte test_data = byte{'A'};
107 std::memcpy(output.AcquireBuffer().data(), &test_data, sizeof(test_data));
109 constexpr auto expected = bytes::Concat(
110 kFlag, kAddress, kControl, 'A', uint32_t{0xA63E2FA5}, kFlag);
112 EXPECT_EQ(Status::Ok(), output.SendAndReleaseBuffer(sizeof(test_data)));
114 ASSERT_EQ(memory_writer.bytes_written(), expected.size());
117 memory_writer.data(), expected.data(), memory_writer.bytes_written()),
122 } // namespace pw::hdlc_lite