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 #include "pw_hdlc/decoder.h"
20 #include "gtest/gtest.h"
21 #include "pw_bytes/array.h"
22 #include "pw_hdlc_private/protocol.h"
30 static constexpr auto kFrameData = bytes::String("1234\xa3\xe0\xe3\x9b");
31 constexpr Frame frame(kFrameData);
33 static_assert(frame.address() == unsigned{'1'});
34 static_assert(frame.control() == byte{'2'});
36 static_assert(frame.data().size() == 2u);
37 static_assert(frame.data()[0] == byte{'3'});
38 static_assert(frame.data()[1] == byte{'4'});
41 TEST(Decoder, Clear) {
42 DecoderBuffer<8> decoder;
44 // Process a partial packet
45 decoder.Process(bytes::String("~1234abcd"),
46 [](const Result<Frame>&) { FAIL(); });
49 Status status = Status::Unknown();
52 bytes::String("~1234\xa3\xe0\xe3\x9b~"),
53 [&status](const Result<Frame>& result) { status = result.status(); });
55 EXPECT_EQ(OkStatus(), status);
58 TEST(Decoder, ExactFit) {
59 DecoderBuffer<8> decoder;
61 for (byte b : bytes::String("~1234\xa3\xe0\xe3\x9b")) {
62 EXPECT_EQ(Status::Unavailable(), decoder.Process(b).status());
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'});
71 TEST(Decoder, MinimumSizedBuffer) {
72 DecoderBuffer<6> decoder;
74 for (byte b : bytes::String("~12\xcd\x44\x53\x4f")) {
75 EXPECT_EQ(Status::Unavailable(), decoder.Process(b).status());
78 auto result = decoder.Process(kFlag);
79 ASSERT_EQ(OkStatus(), result.status());
80 EXPECT_EQ(result.value().data().size(), 0u);
83 TEST(Decoder, TooLargeForBuffer_ReportsResourceExhausted) {
84 DecoderBuffer<8> decoder;
86 for (byte b : bytes::String("~12345\x1c\x3a\xf5\xcb")) {
87 EXPECT_EQ(Status::Unavailable(), decoder.Process(b).status());
89 EXPECT_EQ(Status::ResourceExhausted(), decoder.Process(kFlag).status());
91 for (byte b : bytes::String("~12345678901234567890\xf2\x19\x63\x90")) {
92 EXPECT_EQ(Status::Unavailable(), decoder.Process(b).status());
94 EXPECT_EQ(Status::ResourceExhausted(), decoder.Process(kFlag).status());
97 TEST(Decoder, TooLargeForBuffer_StaysWithinBufferBoundaries) {
98 std::array<byte, 16> buffer = bytes::Initialized<16>('?');
100 Decoder decoder(std::span(buffer.data(), 8));
102 for (byte b : bytes::String("~12345678901234567890\xf2\x19\x63\x90")) {
103 EXPECT_EQ(Status::Unavailable(), decoder.Process(b).status());
106 for (size_t i = 8; i < buffer.size(); ++i) {
107 ASSERT_EQ(byte{'?'}, buffer[i]);
110 EXPECT_EQ(Status::ResourceExhausted(), decoder.Process(kFlag).status());
113 TEST(Decoder, TooLargeForBuffer_DecodesNextFrame) {
114 DecoderBuffer<8> decoder;
116 for (byte b : bytes::String("~12345678901234567890\xf2\x19\x63\x90")) {
117 EXPECT_EQ(Status::Unavailable(), decoder.Process(b).status());
119 EXPECT_EQ(Status::ResourceExhausted(), decoder.Process(kFlag).status());
121 for (byte b : bytes::String("1234\xa3\xe0\xe3\x9b")) {
122 EXPECT_EQ(Status::Unavailable(), decoder.Process(b).status());
124 EXPECT_EQ(OkStatus(), decoder.Process(kFlag).status());
128 } // namespace pw::hdlc