Debian Codegraph @

test/unit/test_gdbserver.py

raw symbols SCIP
1 # pyOCD debugger
2 # Copyright (c) 2021 Chris Reed
3 # SPDX-License-Identifier: Apache-2.0
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 from pyocd.gdbserver.gdbserver import (
18 escape,
19 unescape,
20 )
21
22 # escaped chars: '#$}*'
23 # escaped by prefixing with '}' and xor'ing the char with 0x20
24 #
25 # '#' (0x23) -> '}\x03'
26 # '$' (0x24) -> '}\x04'
27 # '}' (0x7d) -> '}]'
28 # '*' (0x2a) -> '}\x0a'
29
30 class TestGdbServerEscaping:
31 def test_escape_transparent(self):
32 assert escape(b"hello") == b"hello"
33
34 def test_escape_individual(self):
35 assert escape(b"hello#foo") == b"hello}\x03foo"
36 assert escape(b"hello$foo") == b"hello}\x04foo"
37 assert escape(b"hello}foo") == b"hello}]foo"
38 assert escape(b"hello*foo") == b"hello}\x0afoo"
39
40 def test_escape_single(self):
41 assert escape(b"#") == b"}\x03"
42 assert escape(b"$") == b"}\x04"
43 assert escape(b"}") == b"}]"
44 assert escape(b"*") == b"}\x0a"
45
46 def test_escape_combined(self):
47 assert escape(b"#$}*") == b"}\x03}\x04}]}\x0a"
48 assert escape(b'}}}') == b"}]}]}]"
49
50 def test_unescape_transparent(self):
51 assert unescape(b"bytes") == list(b"bytes")
52
53 def test_unescape_individual(self):
54 assert unescape(b"hello}\x03foo") == list(b"hello#foo")
55 assert unescape(b"hello}\x04foo") == list(b"hello$foo")
56 assert unescape(b"hello}]foo") == list(b"hello}foo")
57 assert unescape(b"hello}\x0afoo") == list(b"hello*foo")
58
59 def test_unescape_single(self):
60 assert unescape(b"}\x03") == [b'#'[0]]
61 assert unescape(b"}\x04") == [b'$'[0]]
62 assert unescape(b"}]") == [b'}'[0]]
63 assert unescape(b"}\x0a") == [b'*'[0]]
64
65 def test_unescape_combined(self):
66 assert unescape(b"}\x03}\x04}]}\x0a") == list(b"#$}*")
67 assert unescape(b"}]}]}]") == list(b"}}}")