Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_hdlc / 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/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 {
42 namespace {
43
44 constexpr byte kFlag = byte{0x7E};
45 constexpr uint8_t kAddress = 0x7b;    // 123
46 constexpr byte kControl = byte{0x3};  // UI-frame control sequence.
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   auto buffer = output.AcquireBuffer();
60   std::memcpy(buffer.data(), &test_data, sizeof(test_data));
61
62   constexpr auto expected = bytes::Concat(
63       kFlag, kAddress, kControl, 'A', uint32_t{0x8D137C66}, kFlag);
64
65   EXPECT_EQ(OkStatus(),
66             output.SendAndReleaseBuffer(buffer.first(sizeof(test_data))));
67
68   ASSERT_EQ(memory_writer.bytes_written(), expected.size());
69   EXPECT_EQ(
70       std::memcmp(
71           memory_writer.data(), expected.data(), memory_writer.bytes_written()),
72       0);
73 }
74
75 TEST(RpcChannelOutput, EscapingPayloadTest) {
76   std::array<byte, kSinkBufferSize> channel_output_buffer;
77   stream::MemoryWriterBuffer<kSinkBufferSize> memory_writer;
78
79   RpcChannelOutput output(
80       memory_writer, channel_output_buffer, kAddress, "RpcChannelOutput");
81
82   constexpr auto test_data = bytes::Array<0x7D>();
83   auto buffer = output.AcquireBuffer();
84   std::memcpy(buffer.data(), test_data.data(), test_data.size());
85
86   constexpr auto expected = bytes::Concat(kFlag,
87                                           kAddress,
88                                           kControl,
89                                           byte{0x7d},
90                                           byte{0x7d} ^ byte{0x20},
91                                           uint32_t{0xA27C00E1},
92                                           kFlag);
93   EXPECT_EQ(OkStatus(),
94             output.SendAndReleaseBuffer(buffer.first(test_data.size())));
95
96   ASSERT_EQ(memory_writer.bytes_written(), 10u);
97   EXPECT_EQ(
98       std::memcmp(
99           memory_writer.data(), expected.data(), memory_writer.bytes_written()),
100       0);
101 }
102
103 TEST(RpcChannelOutputBuffer, 1BytePayload) {
104   stream::MemoryWriterBuffer<kSinkBufferSize> memory_writer;
105
106   RpcChannelOutputBuffer<kSinkBufferSize> output(
107       memory_writer, kAddress, "RpcChannelOutput");
108
109   constexpr byte test_data = byte{'A'};
110   auto buffer = output.AcquireBuffer();
111   std::memcpy(buffer.data(), &test_data, sizeof(test_data));
112
113   constexpr auto expected = bytes::Concat(
114       kFlag, kAddress, kControl, 'A', uint32_t{0x8D137C66}, kFlag);
115
116   EXPECT_EQ(OkStatus(),
117             output.SendAndReleaseBuffer(buffer.first(sizeof(test_data))));
118
119   ASSERT_EQ(memory_writer.bytes_written(), expected.size());
120   EXPECT_EQ(
121       std::memcmp(
122           memory_writer.data(), expected.data(), memory_writer.bytes_written()),
123       0);
124 }
125
126 }  // namespace
127 }  // namespace pw::hdlc