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/rpc_channel.h"
35 #include "gtest/gtest.h"
36 #include "pw_bytes/array.h"
37 #include "pw_stream/memory_stream.h"
44 constexpr byte kFlag = byte{0x7E};
45 constexpr uint8_t kAddress = 0x7b; // 123
46 constexpr byte kControl = byte{0x3}; // UI-frame control sequence.
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 auto buffer = output.AcquireBuffer();
60 std::memcpy(buffer.data(), &test_data, sizeof(test_data));
62 constexpr auto expected = bytes::Concat(
63 kFlag, kAddress, kControl, 'A', uint32_t{0x8D137C66}, kFlag);
66 output.SendAndReleaseBuffer(buffer.first(sizeof(test_data))));
68 ASSERT_EQ(memory_writer.bytes_written(), expected.size());
71 memory_writer.data(), expected.data(), memory_writer.bytes_written()),
75 TEST(RpcChannelOutput, EscapingPayloadTest) {
76 std::array<byte, kSinkBufferSize> channel_output_buffer;
77 stream::MemoryWriterBuffer<kSinkBufferSize> memory_writer;
79 RpcChannelOutput output(
80 memory_writer, channel_output_buffer, kAddress, "RpcChannelOutput");
82 constexpr auto test_data = bytes::Array<0x7D>();
83 auto buffer = output.AcquireBuffer();
84 std::memcpy(buffer.data(), test_data.data(), test_data.size());
86 constexpr auto expected = bytes::Concat(kFlag,
90 byte{0x7d} ^ byte{0x20},
94 output.SendAndReleaseBuffer(buffer.first(test_data.size())));
96 ASSERT_EQ(memory_writer.bytes_written(), 10u);
99 memory_writer.data(), expected.data(), memory_writer.bytes_written()),
103 TEST(RpcChannelOutputBuffer, 1BytePayload) {
104 stream::MemoryWriterBuffer<kSinkBufferSize> memory_writer;
106 RpcChannelOutputBuffer<kSinkBufferSize> output(
107 memory_writer, kAddress, "RpcChannelOutput");
109 constexpr byte test_data = byte{'A'};
110 auto buffer = output.AcquireBuffer();
111 std::memcpy(buffer.data(), &test_data, sizeof(test_data));
113 constexpr auto expected = bytes::Concat(
114 kFlag, kAddress, kControl, 'A', uint32_t{0x8D137C66}, kFlag);
116 EXPECT_EQ(OkStatus(),
117 output.SendAndReleaseBuffer(buffer.first(sizeof(test_data))));
119 ASSERT_EQ(memory_writer.bytes_written(), expected.size());
122 memory_writer.data(), expected.data(), memory_writer.bytes_written()),
127 } // namespace pw::hdlc