Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_hdlc / wire_packet_parser_test.cc
1 // Copyright 2021 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/wire_packet_parser.h"
16
17 #include "gtest/gtest.h"
18 #include "pw_bytes/array.h"
19 #include "pw_hdlc_private/protocol.h"
20
21 namespace pw::hdlc {
22 namespace {
23
24 constexpr uint8_t kAddress = 0x6a;
25 constexpr uint8_t kControl = 0x03;
26
27 TEST(WirePacketParser, Parse_ValidPacket) {
28   WirePacketParser parser;
29   EXPECT_TRUE(parser.Parse(bytes::Concat(
30       kFlag, kAddress, kControl, bytes::String("hello"), 0xc40cefe0, kFlag)));
31   auto maybe_address = parser.GetDestinationAddress();
32   EXPECT_TRUE(maybe_address.has_value());
33   EXPECT_EQ(maybe_address.value(), kAddress);
34 }
35
36 TEST(WirePacketParser, Parse_EscapedAddress) {
37   WirePacketParser parser;
38   EXPECT_TRUE(parser.Parse(bytes::Concat(kFlag,
39                                          kEscape,
40                                          uint8_t{0x7e ^ 0x20},
41                                          kControl,
42                                          bytes::String("hello"),
43                                          0x579d573d,
44                                          kFlag)));
45   auto maybe_address = parser.GetDestinationAddress();
46   EXPECT_TRUE(maybe_address.has_value());
47   EXPECT_EQ(maybe_address.value(), 0x7eu);
48 }
49
50 TEST(WirePacketParser, Parse_EscapedPayload) {
51   WirePacketParser parser;
52   EXPECT_TRUE(parser.Parse(bytes::Concat(kFlag,
53                                          kAddress,
54                                          kControl,
55                                          bytes::String("hello"),
56                                          kEscapedEscape,
57                                          bytes::String("world"),
58                                          0x9a9b934a,
59                                          kFlag)));
60   auto maybe_address = parser.GetDestinationAddress();
61   EXPECT_TRUE(maybe_address.has_value());
62   EXPECT_EQ(maybe_address.value(), kAddress);
63 }
64
65 TEST(WirePacketParser, Parse_EscapedFcs) {
66   WirePacketParser parser;
67   EXPECT_TRUE(parser.Parse(bytes::Concat(kFlag,
68                                          kAddress,
69                                          kControl,
70                                          bytes::String("qwertyu"),
71                                          // FCS: e0 7d e9 3b
72                                          bytes::String("\x3b\xe9\x7d\x5d\xe0"),
73                                          kFlag)));
74   auto maybe_address = parser.GetDestinationAddress();
75   EXPECT_TRUE(maybe_address.has_value());
76   EXPECT_EQ(maybe_address.value(), kAddress);
77 }
78
79 TEST(WirePacketParser, Parse_MultipleEscapes) {
80   WirePacketParser parser;
81   EXPECT_TRUE(parser.Parse(bytes::Concat(kFlag,
82                                          kEscapedFlag,
83                                          kControl,
84                                          kEscapedEscape,
85                                          kEscapedFlag,
86                                          kEscapedFlag,
87                                          0xc85df51d,
88                                          kFlag)));
89   auto maybe_address = parser.GetDestinationAddress();
90   EXPECT_TRUE(maybe_address.has_value());
91   EXPECT_EQ(maybe_address.value(), static_cast<uint32_t>(kFlag));
92 }
93
94 TEST(WirePacketParser, Parse_AddressBits) {
95   WirePacketParser parser(4);
96   EXPECT_TRUE(parser.Parse(bytes::Concat(kFlag,
97                                          std::byte{0xab},
98                                          kControl,
99                                          bytes::String("hello"),
100                                          0xae667bdf,
101                                          kFlag)));
102   auto maybe_address = parser.GetDestinationAddress();
103   EXPECT_TRUE(maybe_address.has_value());
104   EXPECT_EQ(maybe_address.value(), 0xau);
105 }
106
107 TEST(WirePacketParser, Parse_BadFcs) {
108   WirePacketParser parser;
109   EXPECT_FALSE(parser.Parse(bytes::Concat(
110       kFlag, kAddress, kControl, bytes::String("hello"), 0x1badda7a, kFlag)));
111 }
112
113 TEST(WirePacketParser, Parse_DoubleEscape) {
114   WirePacketParser parser;
115   EXPECT_FALSE(parser.Parse(bytes::Concat(
116       kFlag, kAddress, kControl, bytes::String("hello"), 0x01027d7d, kFlag)));
117 }
118
119 TEST(WirePacketParser, Parse_FlagInFrame) {
120   WirePacketParser parser;
121   EXPECT_FALSE(parser.Parse(bytes::Concat(
122       kFlag, kAddress, kControl, bytes::String("he~lo"), 0xdbae98fe, kFlag)));
123 }
124
125 TEST(WirePacketParser, Parse_EmptyPacket) {
126   WirePacketParser parser;
127   EXPECT_FALSE(parser.Parse({}));
128 }
129
130 }  // namespace
131 }  // namespace pw::hdlc