2 # Copyright 2020 The Pigweed Authors
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 # use this file except in compliance with the License. You may obtain a copy of
8 # https://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations under
15 """Tests encoding HDLC frames."""
19 from pw_hdlc_lite import encode
20 from pw_hdlc_lite import protocol
21 from pw_hdlc_lite.protocol import frame_check_sequence as _fcs
23 FLAG = bytes([protocol.FLAG])
26 def _with_fcs(data: bytes) -> bytes:
27 return data + _fcs(data)
30 class TestEncodeInformationFrame(unittest.TestCase):
31 """Tests Encoding bytes with different arguments using a custom serial."""
33 self.assertEqual(encode.information_frame(0, b''),
34 FLAG + _with_fcs(b'\0\0') + FLAG)
35 self.assertEqual(encode.information_frame(0x1a, b''),
36 FLAG + _with_fcs(b'\x1a\0') + FLAG)
39 self.assertEqual(encode.information_frame(0, b'A'),
40 FLAG + _with_fcs(b'\0\0A') + FLAG)
42 def test_multibyte(self):
43 self.assertEqual(encode.information_frame(0, b'123456789'),
44 FLAG + _with_fcs(b'\x00\x00123456789') + FLAG)
46 def test_escape(self):
48 encode.information_frame(0x7e, b'\x7d'),
49 FLAG + b'\x7d\x5e\x00\x7d\x5d' + _fcs(b'\x7e\x00\x7d') + FLAG)
51 encode.information_frame(0x7d, b'A\x7e\x7dBC'),
52 FLAG + b'\x7d\x5d\x00A\x7d\x5e\x7d\x5dBC' +
53 _fcs(b'\x7d\x00A\x7e\x7dBC') + FLAG)
56 if __name__ == '__main__':