Debian Codegraph @

test/unit/test_conversion.py

raw symbols SCIP
1 # pyOCD debugger
2 # Copyright (c) 2015 Paul Osborne <[email protected]>
3 # Copyright (c) 2018-2020 Arm Limited
4 # SPDX-License-Identifier: Apache-2.0
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 import pytest
19
20 from pyocd.utility.conversion import (
21 byte_list_to_nbit_le_list,
22 nbit_le_list_to_byte_list,
23 byte_list_to_u32le_list,
24 u32le_list_to_byte_list,
25 u16le_list_to_byte_list,
26 byte_list_to_u16le_list,
27 u32_to_float32,
28 float32_to_u32,
29 uint_to_hex_le,
30 hex_le_to_uint,
31 u32_to_hex8le,
32 u64_to_hex16le,
33 hex8_to_u32be,
34 hex16_to_u64be,
35 hex8_to_u32le,
36 hex16_to_u64le,
37 byte_to_hex2,
38 hex_to_byte_list,
39 hex_decode,
40 hex_encode,
41 pairwise,
42 )
43 from pyocd.utility.mask import (
44 align_up,
45 align_down,
46 )
47 from pyocd.gdbserver.gdbserver import (
48 escape,
49 unescape,
50 )
51
52 # pylint: disable=invalid-name
53 class TestConversionUtilities(object):
54 @pytest.mark.parametrize(("w",), [
55 (8,),
56 (16,),
57 (32,),
58 (64,),
59 (128,),
60 (256,),
61 ])
62 def test_byte_list_to_nbit_le_list_empty(self, w):
63 assert byte_list_to_nbit_le_list([], w) == []
64
65 @pytest.mark.parametrize(("w",), [
66 (8,),
67 (16,),
68 (32,),
69 (64,),
70 (128,),
71 (256,),
72 ])
73 def test_nbit_le_list_to_byte_list(self, w):
74 assert nbit_le_list_to_byte_list([], w) == []
75
76 @pytest.mark.parametrize(("w", "r"), [
77 (8, [0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]),
78 (16, [0x201, 0x403, 0x605, 0x807]),
79 (32, [0x4030201, 0x8070605]),
80 (64, [0x807060504030201]),
81 (128, [0x807060504030201]),
82 (256, [0x807060504030201]),
83 ])
84 def test_byte_list_to_nbit_le_list(self, w, r):
85 assert byte_list_to_nbit_le_list([1,2,3,4,5,6,7,8], w) == r
86
87 @pytest.mark.parametrize(("w", "r"), [
88 (8, [0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]),
89 (16, [0x201, 0x403, 0x605, 0x807]),
90 (32, [0x4030201, 0x8070605]),
91 (64, [0x807060504030201]),
92 (128, [0x55555555555555550807060504030201]),
93 (256, [0x5555555555555555555555555555555555555555555555550807060504030201]),
94 ])
95 def test_byte_list_to_nbit_le_list_nz_pad(self, w, r):
96 assert byte_list_to_nbit_le_list([1,2,3,4,5,6,7,8], w, pad=0x55) == r
97
98 @pytest.mark.parametrize(("w", "i"), [
99 (8, [1,2,3,4,5,6,7,8]),
100 (16, [0x201, 0x403, 0x605, 0x807]),
101 (32, [0x4030201, 0x8070605]),
102 (64, [0x807060504030201]),
103 ])
104 def test_nbit_le_list_to_byte_list(self, w, i):
105 assert nbit_le_list_to_byte_list(i, w) == [1,2,3,4,5,6,7,8]
106
107 @pytest.mark.parametrize(("w", "i", "r"), [
108 (128, [0x55555555555555550807060504030201], [1,2,3,4,5,6,7,8,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55]),
109 (256, [0x555555555555555555555555555555550807060504030201], [1,2,3,4,5,6,7,8,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0,0,0,0,0,0,0,0]),
110 ])
111 def test_nbit_le_list_to_byte_list_lg(self, w, i, r):
112 assert nbit_le_list_to_byte_list(i, w) == r
113
114 def test_byte_list_to_u32le_list_empty(self):
115 assert byte_list_to_u32le_list([]) == []
116
117 def test_byteListToU32leList(self):
118 data = range(32)
119 assert byte_list_to_u32le_list(data) == [
120 0x03020100,
121 0x07060504,
122 0x0B0A0908,
123 0x0F0E0D0C,
124 0x13121110,
125 0x17161514,
126 0x1B1A1918,
127 0x1F1E1D1C,
128 ]
129
130 def test_byte_list_to_u32le_list_unaligned(self):
131 assert byte_list_to_u32le_list([1]) == [0x00000001]
132 assert byte_list_to_u32le_list([1, 2, 3]) == [0x00030201]
133 assert byte_list_to_u32le_list([1, 2, 3, 4, 5]) == [0x04030201, 0x00000005]
134
135 assert byte_list_to_u32le_list([1], pad=0xcc) == [0xcccccc01]
136 assert byte_list_to_u32le_list([1, 2, 3], pad=0xdd) == [0xdd030201]
137 assert byte_list_to_u32le_list([1, 2, 3, 4, 5], pad=0xff) == [0x04030201, 0xffffff05]
138
139 def test_byte_list_to_u32le_list_bytearray(self):
140 assert byte_list_to_u32le_list(bytearray(b'abcd')) == [0x64636261]
141 assert byte_list_to_u32le_list(bytearray(b'a')) == [0x00000061]
142
143 def test_u32leListToByteList(self):
144 data = [
145 0x03020100,
146 0x07060504,
147 0x0B0A0908,
148 0x0F0E0D0C,
149 0x13121110,
150 0x17161514,
151 0x1B1A1918,
152 0x1F1E1D1C,
153 ]
154 assert u32le_list_to_byte_list(data) == list(range(32))
155
156 def test_u16leListToByteList(self):
157 data = [0x3412, 0xFEAB]
158 assert u16le_list_to_byte_list(data) == [
159 0x12,
160 0x34,
161 0xAB,
162 0xFE
163 ]
164
165 def test_byteListToU16leList(self):
166 data = [0x01, 0x00, 0xAB, 0xCD, ]
167 assert byte_list_to_u16le_list(data) == [
168 0x0001,
169 0xCDAB,
170 ]
171
172 def test_u32BEToFloat32BE(self):
173 assert u32_to_float32(0x012345678) == 5.690456613903524e-28
174
175 def test_float32beToU32be(self):
176 assert float32_to_u32(5.690456613903524e-28) == 0x012345678
177
178 def test_u32ToHex8le(self):
179 assert u32_to_hex8le(0x0102ABCD) == "cdab0201"
180
181 def test_u64ToHex16le(self):
182 assert u64_to_hex16le(0x0102ABCD171819EF) == "ef191817cdab0201"
183
184 def test_hex8ToU32be(self):
185 assert hex8_to_u32be("0102ABCD") == 0xCDAB0201
186
187 def test_hex16ToU64be(self):
188 assert hex16_to_u64be("0102ABCD171819EF") == 0xEF191817CDAB0201
189
190 def test_hex8ToU32le(self):
191 assert hex8_to_u32le("0102ABCD") == 0x0102ABCD
192
193 def test_hex16ToU64le(self):
194 assert hex16_to_u64le("0102ABCD171819EF") == 0x0102ABCD171819EF
195
196 def test_uint_to_hex_le_odd_width(self):
197 assert uint_to_hex_le(0xd0102ABCD, 36) == "cdab02010d"
198
199 def test_hex_le_to_uint_odd_width(self):
200 assert hex_le_to_uint("0102ABCD0d", 36) == 0x0dCDAB0201
201
202 def test_byteToHex2(self):
203 assert byte_to_hex2(0xC3) == "c3"
204
205 def test_hexToByteList(self):
206 assert hex_to_byte_list("ABCDEF1234") == [0xAB, 0xCD, 0xEF, 0x12, 0x34]
207
208 def test_hexDecode(self):
209 assert hex_decode('ABCDEF1234') == b'\xab\xcd\xef\x12\x34'
210
211 def test_hexEncode(self):
212 assert hex_encode(b'\xab\xcd\xef\x12\x34') == b'abcdef1234'
213
214 @pytest.mark.parametrize(("args", "result"), [
215 ((0, 1024), 0),
216 ((1, 1024), 1024),
217 ((0, 3), 0),
218 ((100, 3), 102),
219 ((8, 4), 8),
220 ((9, 4), 12),
221 ((13, 16), 16),
222 ((13, 8), 16),
223 ])
224 def test_align_up(self, args, result):
225 assert result == align_up(*args)
226
227 @pytest.mark.parametrize(("args", "result"), [
228 ((0, 1024), 0),
229 ((1, 1024), 0),
230 ((0, 3), 0),
231 ((100, 3), 99),
232 ((8, 4), 8),
233 ((9, 4), 8),
234 ((13, 16), 0),
235 ((13, 8), 8),
236 ])
237 def test_align_down(self, args, result):
238 assert result == align_down(*args)
239
240 # Characters that must be escaped.
241 ESCAPEES = (0x23, 0x24, 0x2a, 0x7d) # == ('#', '$', '}', '*')
242
243 # Test the gdbserver binary data escape/unescape routines.
244 class TestGdbEscape(object):
245 # Verify all chars that shouldn't be escaped pass through unmodified.
246 @pytest.mark.parametrize("data",
247 [bytes((x,)) for x in range(256) if (x not in ESCAPEES)])
248 def test_escape_passthrough(self, data):
249 assert escape(data) == data
250
251 @pytest.mark.parametrize(("data", "expected"), [
252 (b'#', b'}\x03'),
253 (b'$', b'}\x04'),
254 (b'}', b'}]'),
255 (b'*', b'}\x0a')
256 ])
257 def test_escape_1(self, data, expected):
258 assert escape(data) == expected
259
260 def test_escape_2(self):
261 assert escape(b'1234#09*xyz') == b'1234}\x0309}\x0axyz'
262
263 # Verify all chars that shouldn't be escaped pass through unmodified.
264 @pytest.mark.parametrize("data",
265 [bytes((x,)) for x in range(256) if (x not in ESCAPEES)])
266 def test_unescape_passthrough(self, data):
267 assert unescape(data) == [data[0]]
268
269 @pytest.mark.parametrize(("expected", "data"), [
270 (0x23, b'}\x03'),
271 (0x24, b'}\x04'),
272 (0x7d, b'}]'),
273 (0x2a, b'}\x0a')
274 ])
275 def test_unescape_1(self, data, expected):
276 assert unescape(data) == [expected]
277
278 def test_unescape_2(self):
279 assert unescape(b'1234}\x0309}\x0axyz') == \
280 [0x31, 0x32, 0x33, 0x34, 0x23, 0x30, 0x39, 0x2a, 0x78, 0x79, 0x7a]
281
282 class TestPairwise(object):
283 def test_empty(self):
284 assert list(pairwise([])) == []
285
286 def test_str(self):
287 assert list(pairwise('abcdef')) == [('a','b'), ('c','d'), ('e','f')]
288
289 def test_int(self):
290 assert list(pairwise([1, 2, 3, 4, 5, 6])) == [(1, 2), (3, 4), (5, 6)]