• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/internal/protocol.h"
20 
21 namespace pw::hdlc {
22 namespace {
23 
24 constexpr uint8_t kAddress = 0x6a;
25 constexpr uint8_t kEncodedAddress = (kAddress << 1) | 0x1;
26 constexpr uint8_t kControl = 0x03;
27 
TEST(WirePacketParser,Parse_ValidPacket)28 TEST(WirePacketParser, Parse_ValidPacket) {
29   WirePacketParser parser;
30   EXPECT_TRUE(parser.Parse(bytes::Concat(kFlag,
31                                          kEncodedAddress,
32                                          kControl,
33                                          bytes::String("hello"),
34                                          0x1231d0a9,
35                                          kFlag)));
36   auto maybe_address = parser.GetDestinationAddress();
37   EXPECT_TRUE(maybe_address.has_value());
38   EXPECT_EQ(maybe_address.value(), kAddress);
39 }
40 
TEST(WirePacketParser,Parse_MultibyteAddress)41 TEST(WirePacketParser, Parse_MultibyteAddress) {
42   WirePacketParser parser;
43   EXPECT_TRUE(parser.Parse(bytes::Concat(kFlag,
44                                          bytes::String("\xfe\xff"),
45                                          kControl,
46                                          bytes::String("hello"),
47                                          0x6b53b014,
48                                          kFlag)));
49   auto maybe_address = parser.GetDestinationAddress();
50   EXPECT_TRUE(maybe_address.has_value());
51   EXPECT_EQ(maybe_address.value(), 0x3fffu);
52 }
53 
TEST(WirePacketParser,Parse_EscapedAddress)54 TEST(WirePacketParser, Parse_EscapedAddress) {
55   WirePacketParser parser;
56   EXPECT_TRUE(parser.Parse(bytes::Concat(kFlag,
57                                          kEscape,
58                                          uint8_t{0x7d ^ 0x20},
59                                          kControl,
60                                          bytes::String("hello"),
61                                          0x66754da0,
62                                          kFlag)));
63   auto maybe_address = parser.GetDestinationAddress();
64   EXPECT_TRUE(maybe_address.has_value());
65   EXPECT_EQ(maybe_address.value(), 62u);
66 }
67 
TEST(WirePacketParser,Parse_EscapedPayload)68 TEST(WirePacketParser, Parse_EscapedPayload) {
69   WirePacketParser parser;
70   EXPECT_TRUE(parser.Parse(bytes::Concat(kFlag,
71                                          kEncodedAddress,
72                                          kControl,
73                                          bytes::String("hello"),
74                                          kEscapedEscape,
75                                          bytes::String("world"),
76                                          0x1af88e47,
77                                          kFlag)));
78   auto maybe_address = parser.GetDestinationAddress();
79   EXPECT_TRUE(maybe_address.has_value());
80   EXPECT_EQ(maybe_address.value(), kAddress);
81 }
82 
TEST(WirePacketParser,Parse_EscapedFcs)83 TEST(WirePacketParser, Parse_EscapedFcs) {
84   WirePacketParser parser;
85   EXPECT_TRUE(
86       parser.Parse(bytes::Concat(kFlag,
87                                  kEncodedAddress,
88                                  kControl,
89                                  uint8_t{'b'},
90                                  // FCS: fc 92 7d 7e
91                                  bytes::String("\x7d\x5e\x7d\x5d\x92\xfc"),
92                                  kFlag)));
93   auto maybe_address = parser.GetDestinationAddress();
94   EXPECT_TRUE(maybe_address.has_value());
95   EXPECT_EQ(maybe_address.value(), kAddress);
96 }
97 
TEST(WirePacketParser,Parse_MultipleEscapes)98 TEST(WirePacketParser, Parse_MultipleEscapes) {
99   WirePacketParser parser;
100   EXPECT_TRUE(parser.Parse(bytes::Concat(kFlag,
101                                          kEscapedEscape,
102                                          kControl,
103                                          kEscapedEscape,
104                                          kEscapedFlag,
105                                          kEscapedFlag,
106                                          0x8ffd8fcd,
107                                          kFlag)));
108   auto maybe_address = parser.GetDestinationAddress();
109   EXPECT_TRUE(maybe_address.has_value());
110   EXPECT_EQ(maybe_address.value(), 62u);
111 }
112 
TEST(WirePacketParser,Parse_BadFcs)113 TEST(WirePacketParser, Parse_BadFcs) {
114   WirePacketParser parser;
115   EXPECT_FALSE(parser.Parse(bytes::Concat(kFlag,
116                                           kEncodedAddress,
117                                           kControl,
118                                           bytes::String("hello"),
119                                           0x1badda7a,
120                                           kFlag)));
121 }
122 
TEST(WirePacketParser,Parse_DoubleEscape)123 TEST(WirePacketParser, Parse_DoubleEscape) {
124   WirePacketParser parser;
125   EXPECT_FALSE(parser.Parse(bytes::Concat(kFlag,
126                                           kEncodedAddress,
127                                           kControl,
128                                           bytes::String("hello"),
129                                           0x01027d7d,
130                                           kFlag)));
131 }
132 
TEST(WirePacketParser,Parse_FlagInFrame)133 TEST(WirePacketParser, Parse_FlagInFrame) {
134   WirePacketParser parser;
135   EXPECT_FALSE(parser.Parse(bytes::Concat(kFlag,
136                                           kEncodedAddress,
137                                           kControl,
138                                           // inclusive-language: ignore
139                                           bytes::String("he~lo"),
140                                           0xdbae98fe,
141                                           kFlag)));
142 }
143 
TEST(WirePacketParser,Parse_EmptyPacket)144 TEST(WirePacketParser, Parse_EmptyPacket) {
145   WirePacketParser parser;
146   EXPECT_FALSE(parser.Parse({}));
147 }
148 
149 }  // namespace
150 }  // namespace pw::hdlc
151