Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_hdlc / decoder_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 #include "pw_hdlc/decoder.h"
16
17 #include <array>
18 #include <cstddef>
19
20 #include "gtest/gtest.h"
21 #include "pw_bytes/array.h"
22 #include "pw_hdlc_private/protocol.h"
23
24 namespace pw::hdlc {
25 namespace {
26
27 using std::byte;
28
29 TEST(Frame, Fields) {
30   static constexpr auto kFrameData = bytes::String("1234\xa3\xe0\xe3\x9b");
31   constexpr Frame frame(kFrameData);
32
33   static_assert(frame.address() == unsigned{'1'});
34   static_assert(frame.control() == byte{'2'});
35
36   static_assert(frame.data().size() == 2u);
37   static_assert(frame.data()[0] == byte{'3'});
38   static_assert(frame.data()[1] == byte{'4'});
39 }
40
41 TEST(Decoder, Clear) {
42   DecoderBuffer<8> decoder;
43
44   // Process a partial packet
45   decoder.Process(bytes::String("~1234abcd"),
46                   [](const Result<Frame>&) { FAIL(); });
47
48   decoder.Clear();
49   Status status = Status::Unknown();
50
51   decoder.Process(
52       bytes::String("~1234\xa3\xe0\xe3\x9b~"),
53       [&status](const Result<Frame>& result) { status = result.status(); });
54
55   EXPECT_EQ(OkStatus(), status);
56 }
57
58 TEST(Decoder, ExactFit) {
59   DecoderBuffer<8> decoder;
60
61   for (byte b : bytes::String("~1234\xa3\xe0\xe3\x9b")) {
62     EXPECT_EQ(Status::Unavailable(), decoder.Process(b).status());
63   }
64   auto result = decoder.Process(kFlag);
65   ASSERT_EQ(OkStatus(), result.status());
66   ASSERT_EQ(result.value().data().size(), 2u);
67   ASSERT_EQ(result.value().data()[0], byte{'3'});
68   ASSERT_EQ(result.value().data()[1], byte{'4'});
69 }
70
71 TEST(Decoder, MinimumSizedBuffer) {
72   DecoderBuffer<6> decoder;
73
74   for (byte b : bytes::String("~12\xcd\x44\x53\x4f")) {
75     EXPECT_EQ(Status::Unavailable(), decoder.Process(b).status());
76   }
77
78   auto result = decoder.Process(kFlag);
79   ASSERT_EQ(OkStatus(), result.status());
80   EXPECT_EQ(result.value().data().size(), 0u);
81 }
82
83 TEST(Decoder, TooLargeForBuffer_ReportsResourceExhausted) {
84   DecoderBuffer<8> decoder;
85
86   for (byte b : bytes::String("~12345\x1c\x3a\xf5\xcb")) {
87     EXPECT_EQ(Status::Unavailable(), decoder.Process(b).status());
88   }
89   EXPECT_EQ(Status::ResourceExhausted(), decoder.Process(kFlag).status());
90
91   for (byte b : bytes::String("~12345678901234567890\xf2\x19\x63\x90")) {
92     EXPECT_EQ(Status::Unavailable(), decoder.Process(b).status());
93   }
94   EXPECT_EQ(Status::ResourceExhausted(), decoder.Process(kFlag).status());
95 }
96
97 TEST(Decoder, TooLargeForBuffer_StaysWithinBufferBoundaries) {
98   std::array<byte, 16> buffer = bytes::Initialized<16>('?');
99
100   Decoder decoder(std::span(buffer.data(), 8));
101
102   for (byte b : bytes::String("~12345678901234567890\xf2\x19\x63\x90")) {
103     EXPECT_EQ(Status::Unavailable(), decoder.Process(b).status());
104   }
105
106   for (size_t i = 8; i < buffer.size(); ++i) {
107     ASSERT_EQ(byte{'?'}, buffer[i]);
108   }
109
110   EXPECT_EQ(Status::ResourceExhausted(), decoder.Process(kFlag).status());
111 }
112
113 TEST(Decoder, TooLargeForBuffer_DecodesNextFrame) {
114   DecoderBuffer<8> decoder;
115
116   for (byte b : bytes::String("~12345678901234567890\xf2\x19\x63\x90")) {
117     EXPECT_EQ(Status::Unavailable(), decoder.Process(b).status());
118   }
119   EXPECT_EQ(Status::ResourceExhausted(), decoder.Process(kFlag).status());
120
121   for (byte b : bytes::String("1234\xa3\xe0\xe3\x9b")) {
122     EXPECT_EQ(Status::Unavailable(), decoder.Process(b).status());
123   }
124   EXPECT_EQ(OkStatus(), decoder.Process(kFlag).status());
125 }
126
127 }  // namespace
128 }  // namespace pw::hdlc