7e0d0dd761ecf0884bd11948d4af94e27ebcfa54
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_hdlc_lite / rpc_channel_test.cc
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 // Copyright 2020 The Pigweed Authors
16 //
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
19 // the License at
20 //
21 //     https://www.apache.org/licenses/LICENSE-2.0
22 //
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
27 // the License.
28
29 #include "pw_hdlc_lite/rpc_channel.h"
30
31 #include <algorithm>
32 #include <array>
33 #include <cstddef>
34
35 #include "gtest/gtest.h"
36 #include "pw_bytes/array.h"
37 #include "pw_stream/memory_stream.h"
38
39 using std::byte;
40
41 namespace pw::hdlc_lite {
42 namespace {
43
44 constexpr byte kFlag = byte{0x7E};
45 constexpr uint8_t kAddress = 0x7b;  // 123
46 constexpr byte kControl = byte{0};
47
48 // Size of the in-memory buffer to use for this test.
49 constexpr size_t kSinkBufferSize = 15;
50
51 TEST(RpcChannelOutput, 1BytePayload) {
52   std::array<byte, kSinkBufferSize> channel_output_buffer;
53   stream::MemoryWriterBuffer<kSinkBufferSize> memory_writer;
54
55   RpcChannelOutput output(
56       memory_writer, channel_output_buffer, kAddress, "RpcChannelOutput");
57
58   constexpr byte test_data = byte{'A'};
59   std::memcpy(output.AcquireBuffer().data(), &test_data, sizeof(test_data));
60
61   constexpr auto expected = bytes::Concat(
62       kFlag, kAddress, kControl, 'A', uint32_t{0xA63E2FA5}, kFlag);
63
64   EXPECT_EQ(Status::Ok(), output.SendAndReleaseBuffer(sizeof(test_data)));
65
66   ASSERT_EQ(memory_writer.bytes_written(), expected.size());
67   EXPECT_EQ(
68       std::memcmp(
69           memory_writer.data(), expected.data(), memory_writer.bytes_written()),
70       0);
71 }
72
73 TEST(RpcChannelOutput, EscapingPayloadTest) {
74   std::array<byte, kSinkBufferSize> channel_output_buffer;
75   stream::MemoryWriterBuffer<kSinkBufferSize> memory_writer;
76
77   RpcChannelOutput output(
78       memory_writer, channel_output_buffer, kAddress, "RpcChannelOutput");
79
80   constexpr auto test_data = bytes::Array<0x7D>();
81   std::memcpy(
82       output.AcquireBuffer().data(), test_data.data(), test_data.size());
83
84   constexpr auto expected = bytes::Concat(kFlag,
85                                           kAddress,
86                                           kControl,
87                                           byte{0x7d},
88                                           byte{0x7d} ^ byte{0x20},
89                                           uint32_t{0x89515322},
90                                           kFlag);
91   EXPECT_EQ(Status::Ok(), output.SendAndReleaseBuffer(test_data.size()));
92
93   ASSERT_EQ(memory_writer.bytes_written(), 10u);
94   EXPECT_EQ(
95       std::memcmp(
96           memory_writer.data(), expected.data(), memory_writer.bytes_written()),
97       0);
98 }
99
100 TEST(RpcChannelOutputBuffer, 1BytePayload) {
101   stream::MemoryWriterBuffer<kSinkBufferSize> memory_writer;
102
103   RpcChannelOutputBuffer<kSinkBufferSize> output(
104       memory_writer, kAddress, "RpcChannelOutput");
105
106   constexpr byte test_data = byte{'A'};
107   std::memcpy(output.AcquireBuffer().data(), &test_data, sizeof(test_data));
108
109   constexpr auto expected = bytes::Concat(
110       kFlag, kAddress, kControl, 'A', uint32_t{0xA63E2FA5}, kFlag);
111
112   EXPECT_EQ(Status::Ok(), output.SendAndReleaseBuffer(sizeof(test_data)));
113
114   ASSERT_EQ(memory_writer.bytes_written(), expected.size());
115   EXPECT_EQ(
116       std::memcmp(
117           memory_writer.data(), expected.data(), memory_writer.bytes_written()),
118       0);
119 }
120
121 }  // namespace
122 }  // namespace pw::hdlc_lite