Commit | Line | Data |
---|---|---|
32d2d479 MJ |
1 | # |
2 | # Copyright (C) 2019 EfficiOS Inc. | |
3 | # | |
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; only version 2 | |
7 | # of the License. | |
8 | # | |
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. | |
13 | # | |
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
17 | # | |
18 | ||
32656995 | 19 | import unittest |
32656995 | 20 | import bt2 |
c8820b76 | 21 | from utils import get_default_trace_class |
32656995 SM |
22 | |
23 | ||
a07f15cb PP |
24 | class BoolFieldClassTestCase(unittest.TestCase): |
25 | def setUp(self): | |
26 | tc = get_default_trace_class() | |
27 | self._fc = tc.create_bool_field_class() | |
28 | ||
29 | def test_create_default(self): | |
30 | self.assertIsNotNone(self._fc) | |
31 | ||
32 | ||
c8820b76 SM |
33 | class _TestIntegerFieldClassProps: |
34 | def test_create_default(self): | |
35 | fc = self._create_func() | |
36 | self.assertEqual(fc.field_value_range, 64) | |
37 | self.assertEqual(fc.preferred_display_base, bt2.IntegerDisplayBase.DECIMAL) | |
32656995 | 38 | |
c8820b76 SM |
39 | def test_create_range(self): |
40 | fc = self._create_func(field_value_range=35) | |
41 | self.assertEqual(fc.field_value_range, 35) | |
32656995 | 42 | |
c8820b76 SM |
43 | fc = self._create_func(36) |
44 | self.assertEqual(fc.field_value_range, 36) | |
32656995 | 45 | |
c8820b76 SM |
46 | def test_create_invalid_range(self): |
47 | with self.assertRaises(TypeError): | |
48 | self._create_func('yes') | |
32656995 | 49 | |
c8820b76 SM |
50 | with self.assertRaises(TypeError): |
51 | self._create_func(field_value_range='yes') | |
32656995 | 52 | |
32656995 | 53 | with self.assertRaises(ValueError): |
c8820b76 | 54 | self._create_func(field_value_range=-2) |
32656995 | 55 | |
c8820b76 SM |
56 | with self.assertRaises(ValueError): |
57 | self._create_func(field_value_range=0) | |
32656995 | 58 | |
c8820b76 | 59 | def test_create_base(self): |
61d96b89 FD |
60 | fc = self._create_func( |
61 | preferred_display_base=bt2.IntegerDisplayBase.HEXADECIMAL | |
62 | ) | |
c8820b76 | 63 | self.assertEqual(fc.preferred_display_base, bt2.IntegerDisplayBase.HEXADECIMAL) |
32656995 | 64 | |
c8820b76 | 65 | def test_create_invalid_base_type(self): |
32656995 | 66 | with self.assertRaises(TypeError): |
c8820b76 | 67 | self._create_func(preferred_display_base='yes') |
32656995 | 68 | |
c8820b76 SM |
69 | def test_create_invalid_base_value(self): |
70 | with self.assertRaises(ValueError): | |
71 | self._create_func(preferred_display_base=444) | |
32656995 | 72 | |
c8820b76 SM |
73 | def test_create_full(self): |
74 | fc = self._create_func(24, preferred_display_base=bt2.IntegerDisplayBase.OCTAL) | |
75 | self.assertEqual(fc.field_value_range, 24) | |
76 | self.assertEqual(fc.preferred_display_base, bt2.IntegerDisplayBase.OCTAL) | |
32656995 SM |
77 | |
78 | ||
c8820b76 SM |
79 | class IntegerFieldClassTestCase(_TestIntegerFieldClassProps, unittest.TestCase): |
80 | def setUp(self): | |
81 | self._tc = get_default_trace_class() | |
82 | self._create_func = self._tc.create_signed_integer_field_class | |
32656995 | 83 | |
32656995 | 84 | |
c8820b76 SM |
85 | class RealFieldClassTestCase(unittest.TestCase): |
86 | def setUp(self): | |
87 | self._tc = get_default_trace_class() | |
32656995 | 88 | |
c8820b76 SM |
89 | def test_create_default(self): |
90 | fc = self._tc.create_real_field_class() | |
91 | self.assertFalse(fc.is_single_precision) | |
32656995 | 92 | |
c8820b76 SM |
93 | def test_create_is_single_precision(self): |
94 | fc = self._tc.create_real_field_class(is_single_precision=True) | |
95 | self.assertTrue(fc.is_single_precision) | |
96 | ||
97 | def test_create_invalid_is_single_precision(self): | |
32656995 | 98 | with self.assertRaises(TypeError): |
c8820b76 | 99 | self._tc.create_real_field_class(is_single_precision='hohoho') |
32656995 | 100 | |
32656995 | 101 | |
c8820b76 SM |
102 | # Converts an _EnumerationFieldClassMapping to a list of ranges: |
103 | # | |
104 | # [(lower0, upper0), (lower1, upper1), ...] | |
105 | ||
61d96b89 | 106 | |
02b61fe0 PP |
107 | def enum_mapping_to_set(mapping): |
108 | return {(x.lower, x.upper) for x in mapping.ranges} | |
32656995 | 109 | |
32656995 | 110 | |
02b61fe0 | 111 | class _EnumerationFieldClassTestCase(_TestIntegerFieldClassProps): |
c8820b76 SM |
112 | def setUp(self): |
113 | self._tc = get_default_trace_class() | |
02b61fe0 PP |
114 | self._spec_set_up() |
115 | self._fc = self._create_func() | |
c8820b76 SM |
116 | |
117 | def test_create_from_invalid_type(self): | |
32656995 | 118 | with self.assertRaises(TypeError): |
c8820b76 | 119 | self._create_func('coucou') |
32656995 | 120 | |
c8820b76 | 121 | def test_add_mapping_simple(self): |
02b61fe0 | 122 | self._fc.add_mapping('hello', self._ranges1) |
c8820b76 SM |
123 | mapping = self._fc['hello'] |
124 | self.assertEqual(mapping.label, 'hello') | |
02b61fe0 | 125 | self.assertEqual(mapping.ranges, self._ranges1) |
32656995 | 126 | |
c8820b76 | 127 | def test_add_mapping_simple_kwargs(self): |
02b61fe0 | 128 | self._fc.add_mapping(label='hello', ranges=self._ranges1) |
c8820b76 SM |
129 | mapping = self._fc['hello'] |
130 | self.assertEqual(mapping.label, 'hello') | |
02b61fe0 | 131 | self.assertEqual(mapping.ranges, self._ranges1) |
32656995 | 132 | |
02b61fe0 PP |
133 | def test_add_mapping_invalid_name(self): |
134 | with self.assertRaises(TypeError): | |
135 | self._fc.add_mapping(17, self._ranges1) | |
32656995 | 136 | |
02b61fe0 PP |
137 | def test_add_mapping_invalid_range(self): |
138 | with self.assertRaises(TypeError): | |
139 | self._fc.add_mapping('allo', 'meow') | |
c8820b76 | 140 | |
02b61fe0 | 141 | def test_add_mapping_dup_label(self): |
3b2be708 | 142 | with self.assertRaises(ValueError): |
02b61fe0 PP |
143 | self._fc.add_mapping('a', self._ranges1) |
144 | self._fc.add_mapping('a', self._ranges2) | |
c8820b76 | 145 | |
02b61fe0 | 146 | def test_add_mapping_invalid_ranges_signedness(self): |
32656995 | 147 | with self.assertRaises(TypeError): |
02b61fe0 | 148 | self._fc.add_mapping('allo', self._inval_ranges) |
32656995 | 149 | |
c8820b76 | 150 | def test_iadd(self): |
02b61fe0 | 151 | self._fc.add_mapping('c', self._ranges1) |
32656995 | 152 | |
61d96b89 | 153 | self._fc += [('d', self._ranges2), ('e', self._ranges3)] |
32656995 | 154 | |
02b61fe0 | 155 | self.assertEqual(len(self._fc), 3) |
c8820b76 | 156 | self.assertEqual(self._fc['c'].label, 'c') |
02b61fe0 | 157 | self.assertEqual(self._fc['c'].ranges, self._ranges1) |
c8820b76 | 158 | self.assertEqual(self._fc['d'].label, 'd') |
02b61fe0 | 159 | self.assertEqual(self._fc['d'].ranges, self._ranges2) |
c8820b76 | 160 | self.assertEqual(self._fc['e'].label, 'e') |
02b61fe0 | 161 | self.assertEqual(self._fc['e'].ranges, self._ranges3) |
32656995 | 162 | |
c8820b76 SM |
163 | def test_bool_op(self): |
164 | self.assertFalse(self._fc) | |
02b61fe0 | 165 | self._fc.add_mapping('a', self._ranges1) |
c8820b76 | 166 | self.assertTrue(self._fc) |
32656995 | 167 | |
c8820b76 | 168 | def test_len(self): |
02b61fe0 PP |
169 | self._fc.add_mapping('a', self._ranges1) |
170 | self._fc.add_mapping('b', self._ranges2) | |
171 | self._fc.add_mapping('c', self._ranges3) | |
c8820b76 | 172 | self.assertEqual(len(self._fc), 3) |
32656995 | 173 | |
c8820b76 | 174 | def test_getitem(self): |
02b61fe0 PP |
175 | self._fc.add_mapping('a', self._ranges1) |
176 | self._fc.add_mapping('b', self._ranges2) | |
177 | self._fc.add_mapping('c', self._ranges3) | |
c8820b76 | 178 | mapping = self._fc['a'] |
c8820b76 | 179 | self.assertEqual(mapping.label, 'a') |
02b61fe0 | 180 | self.assertEqual(mapping.ranges, self._ranges1) |
32656995 | 181 | |
02b61fe0 | 182 | def test_getitem_nonexistent(self): |
c8820b76 SM |
183 | with self.assertRaises(KeyError): |
184 | self._fc['doesnotexist'] | |
32656995 | 185 | |
c8820b76 | 186 | def test_iter(self): |
02b61fe0 PP |
187 | self._fc.add_mapping('a', self._ranges1) |
188 | self._fc.add_mapping('b', self._ranges2) | |
189 | self._fc.add_mapping('c', self._ranges3) | |
32656995 | 190 | |
c8820b76 SM |
191 | # This exercises iteration. |
192 | labels = sorted(self._fc) | |
32656995 | 193 | |
02b61fe0 | 194 | self.assertEqual(labels, ['a', 'b', 'c']) |
32656995 | 195 | |
c8820b76 | 196 | def test_find_by_value(self): |
02b61fe0 PP |
197 | self._fc.add_mapping('a', self._ranges1) |
198 | self._fc.add_mapping('b', self._ranges2) | |
199 | self._fc.add_mapping('c', self._ranges3) | |
200 | mappings = self._fc.mappings_for_value(self._value_in_range_1_and_3) | |
201 | labels = set([mapping.label for mapping in mappings]) | |
202 | expected_labels = set(['a', 'c']) | |
203 | self.assertEqual(labels, expected_labels) | |
204 | ||
205 | ||
61d96b89 FD |
206 | class UnsignedEnumerationFieldClassTestCase( |
207 | _EnumerationFieldClassTestCase, unittest.TestCase | |
208 | ): | |
02b61fe0 PP |
209 | def _spec_set_up(self): |
210 | self._ranges1 = bt2.UnsignedIntegerRangeSet([(1, 4), (18, 47)]) | |
211 | self._ranges2 = bt2.UnsignedIntegerRangeSet([(5, 5)]) | |
212 | self._ranges3 = bt2.UnsignedIntegerRangeSet([(8, 22), (48, 99)]) | |
213 | self._inval_ranges = bt2.SignedIntegerRangeSet([(-8, -5), (48, 1928)]) | |
214 | self._value_in_range_1_and_3 = 20 | |
c8820b76 | 215 | self._create_func = self._tc.create_unsigned_enumeration_field_class |
32656995 | 216 | |
32656995 | 217 | |
61d96b89 FD |
218 | class SignedEnumerationFieldClassTestCase( |
219 | _EnumerationFieldClassTestCase, unittest.TestCase | |
220 | ): | |
02b61fe0 PP |
221 | def _spec_set_up(self): |
222 | self._ranges1 = bt2.SignedIntegerRangeSet([(-10, -4), (18, 47)]) | |
223 | self._ranges2 = bt2.SignedIntegerRangeSet([(-3, -3)]) | |
224 | self._ranges3 = bt2.SignedIntegerRangeSet([(-100, -1), (8, 16), (48, 99)]) | |
225 | self._inval_ranges = bt2.UnsignedIntegerRangeSet([(8, 16), (48, 99)]) | |
226 | self._value_in_range_1_and_3 = -7 | |
c8820b76 | 227 | self._create_func = self._tc.create_signed_enumeration_field_class |
32656995 | 228 | |
32656995 | 229 | |
c8820b76 | 230 | class StringFieldClassTestCase(unittest.TestCase): |
32656995 | 231 | def setUp(self): |
c8820b76 SM |
232 | tc = get_default_trace_class() |
233 | self._fc = tc.create_string_field_class() | |
32656995 SM |
234 | |
235 | def test_create_default(self): | |
c8820b76 | 236 | self.assertIsNotNone(self._fc) |
32656995 | 237 | |
32656995 | 238 | |
61d96b89 | 239 | class _TestElementContainer: |
02b61fe0 PP |
240 | def setUp(self): |
241 | self._tc = get_default_trace_class() | |
242 | self._fc = self._create_default_fc() | |
243 | ||
244 | def test_create_default(self): | |
245 | self.assertIsNotNone(self._fc) | |
246 | ||
c8820b76 SM |
247 | def test_append_element(self): |
248 | int_field_class = self._tc.create_signed_integer_field_class(32) | |
249 | self._append_element_method(self._fc, 'int32', int_field_class) | |
02b61fe0 | 250 | field_class = self._fc['int32'].field_class |
c8820b76 | 251 | self.assertEqual(field_class.addr, int_field_class.addr) |
32656995 | 252 | |
02b61fe0 | 253 | def test_append_element_kwargs(self): |
c8820b76 SM |
254 | int_field_class = self._tc.create_signed_integer_field_class(32) |
255 | self._append_element_method(self._fc, name='int32', field_class=int_field_class) | |
02b61fe0 | 256 | field_class = self._fc['int32'].field_class |
c8820b76 SM |
257 | self.assertEqual(field_class.addr, int_field_class.addr) |
258 | ||
259 | def test_append_element_invalid_name(self): | |
260 | sub_fc = self._tc.create_string_field_class() | |
32656995 | 261 | |
32656995 | 262 | with self.assertRaises(TypeError): |
c8820b76 | 263 | self._append_element_method(self._fc, 23, sub_fc) |
32656995 | 264 | |
c8820b76 | 265 | def test_append_element_invalid_field_class(self): |
32656995 | 266 | with self.assertRaises(TypeError): |
c8820b76 | 267 | self._append_element_method(self._fc, 'yes', object()) |
32656995 | 268 | |
02b61fe0 PP |
269 | def test_append_element_dup_name(self): |
270 | sub_fc1 = self._tc.create_string_field_class() | |
271 | sub_fc2 = self._tc.create_string_field_class() | |
272 | ||
3b2be708 | 273 | with self.assertRaises(ValueError): |
02b61fe0 PP |
274 | self._append_element_method(self._fc, 'yes', sub_fc1) |
275 | self._append_element_method(self._fc, 'yes', sub_fc2) | |
276 | ||
32656995 | 277 | def test_iadd(self): |
02b61fe0 | 278 | other_fc = self._create_default_fc() |
c8820b76 SM |
279 | a_field_class = self._tc.create_real_field_class() |
280 | b_field_class = self._tc.create_signed_integer_field_class(17) | |
281 | self._append_element_method(self._fc, 'a_float', a_field_class) | |
282 | self._append_element_method(self._fc, 'b_int', b_field_class) | |
02b61fe0 | 283 | c_field_class = self._tc.create_string_field_class() |
61d96b89 FD |
284 | d_field_class = self._tc.create_signed_enumeration_field_class( |
285 | field_value_range=32 | |
286 | ) | |
02b61fe0 PP |
287 | e_field_class = self._tc.create_structure_field_class() |
288 | self._fc += [ | |
289 | ('c_string', c_field_class), | |
290 | ('d_enum', d_field_class), | |
291 | ('e_struct', e_field_class), | |
292 | ] | |
293 | self.assertEqual(self._fc['a_float'].field_class.addr, a_field_class.addr) | |
294 | self.assertEqual(self._fc['a_float'].name, 'a_float') | |
295 | self.assertEqual(self._fc['b_int'].field_class.addr, b_field_class.addr) | |
296 | self.assertEqual(self._fc['b_int'].name, 'b_int') | |
297 | self.assertEqual(self._fc['c_string'].field_class.addr, c_field_class.addr) | |
298 | self.assertEqual(self._fc['c_string'].name, 'c_string') | |
299 | self.assertEqual(self._fc['d_enum'].field_class.addr, d_field_class.addr) | |
300 | self.assertEqual(self._fc['d_enum'].name, 'd_enum') | |
301 | self.assertEqual(self._fc['e_struct'].field_class.addr, e_field_class.addr) | |
302 | self.assertEqual(self._fc['e_struct'].name, 'e_struct') | |
32656995 SM |
303 | |
304 | def test_bool_op(self): | |
305 | self.assertFalse(self._fc) | |
c8820b76 | 306 | self._append_element_method(self._fc, 'a', self._tc.create_string_field_class()) |
32656995 SM |
307 | self.assertTrue(self._fc) |
308 | ||
309 | def test_len(self): | |
02b61fe0 PP |
310 | self._append_element_method(self._fc, 'a', self._tc.create_string_field_class()) |
311 | self._append_element_method(self._fc, 'b', self._tc.create_string_field_class()) | |
312 | self._append_element_method(self._fc, 'c', self._tc.create_string_field_class()) | |
32656995 SM |
313 | self.assertEqual(len(self._fc), 3) |
314 | ||
315 | def test_getitem(self): | |
c8820b76 SM |
316 | a_fc = self._tc.create_signed_integer_field_class(32) |
317 | b_fc = self._tc.create_string_field_class() | |
318 | c_fc = self._tc.create_real_field_class() | |
319 | self._append_element_method(self._fc, 'a', a_fc) | |
320 | self._append_element_method(self._fc, 'b', b_fc) | |
321 | self._append_element_method(self._fc, 'c', c_fc) | |
02b61fe0 PP |
322 | self.assertEqual(self._fc['b'].field_class.addr, b_fc.addr) |
323 | self.assertEqual(self._fc['b'].name, 'b') | |
32656995 SM |
324 | |
325 | def test_getitem_invalid_key_type(self): | |
326 | with self.assertRaises(TypeError): | |
327 | self._fc[0] | |
328 | ||
329 | def test_getitem_invalid_key(self): | |
330 | with self.assertRaises(KeyError): | |
331 | self._fc['no way'] | |
332 | ||
333 | def test_contains(self): | |
334 | self.assertFalse('a' in self._fc) | |
c8820b76 | 335 | self._append_element_method(self._fc, 'a', self._tc.create_string_field_class()) |
32656995 SM |
336 | self.assertTrue('a' in self._fc) |
337 | ||
338 | def test_iter(self): | |
c8820b76 SM |
339 | a_fc = self._tc.create_signed_integer_field_class(32) |
340 | b_fc = self._tc.create_string_field_class() | |
341 | c_fc = self._tc.create_real_field_class() | |
61d96b89 | 342 | elements = (('a', a_fc), ('b', b_fc), ('c', c_fc)) |
32656995 | 343 | |
02b61fe0 PP |
344 | for elem in elements: |
345 | self._append_element_method(self._fc, *elem) | |
32656995 | 346 | |
02b61fe0 PP |
347 | for (name, element), test_elem in zip(self._fc.items(), elements): |
348 | self.assertEqual(element.name, test_elem[0]) | |
349 | self.assertEqual(name, element.name) | |
350 | self.assertEqual(element.field_class.addr, test_elem[1].addr) | |
32656995 SM |
351 | |
352 | def test_at_index(self): | |
c8820b76 SM |
353 | a_fc = self._tc.create_signed_integer_field_class(32) |
354 | b_fc = self._tc.create_string_field_class() | |
355 | c_fc = self._tc.create_real_field_class() | |
356 | self._append_element_method(self._fc, 'c', c_fc) | |
357 | self._append_element_method(self._fc, 'a', a_fc) | |
358 | self._append_element_method(self._fc, 'b', b_fc) | |
02b61fe0 PP |
359 | elem = self._at_index_method(self._fc, 1) |
360 | self.assertEqual(elem.field_class.addr, a_fc.addr) | |
361 | self.assertEqual(elem.name, 'a') | |
32656995 SM |
362 | |
363 | def test_at_index_invalid(self): | |
61d96b89 FD |
364 | self._append_element_method( |
365 | self._fc, 'c', self._tc.create_signed_integer_field_class(32) | |
366 | ) | |
32656995 SM |
367 | |
368 | with self.assertRaises(TypeError): | |
c8820b76 | 369 | self._at_index_method(self._fc, 'yes') |
32656995 SM |
370 | |
371 | def test_at_index_out_of_bounds_after(self): | |
61d96b89 FD |
372 | self._append_element_method( |
373 | self._fc, 'c', self._tc.create_signed_integer_field_class(32) | |
374 | ) | |
32656995 SM |
375 | |
376 | with self.assertRaises(IndexError): | |
c8820b76 | 377 | self._at_index_method(self._fc, len(self._fc)) |
32656995 SM |
378 | |
379 | ||
02b61fe0 | 380 | class StructureFieldClassTestCase(_TestElementContainer, unittest.TestCase): |
c946c9de PP |
381 | _append_element_method = staticmethod(bt2._StructureFieldClass.append_member) |
382 | _at_index_method = staticmethod(bt2._StructureFieldClass.member_at_index) | |
02b61fe0 PP |
383 | |
384 | def _create_default_fc(self): | |
385 | return self._tc.create_structure_field_class() | |
386 | ||
387 | ||
84eba0d9 PP |
388 | class OptionFieldClassTestCase(unittest.TestCase): |
389 | def setUp(self): | |
390 | self._tc = get_default_trace_class() | |
391 | self._content_fc = self._tc.create_signed_integer_field_class(23) | |
392 | self._tag_fc = self._tc.create_bool_field_class() | |
393 | ||
394 | def test_create_default(self): | |
395 | fc = self._tc.create_option_field_class(self._content_fc) | |
396 | self.assertEqual(fc.field_class.addr, self._content_fc.addr) | |
397 | self.assertIsNone(fc.selector_field_path, None) | |
398 | ||
399 | def _create_field_class_for_field_path_test(self): | |
400 | fc = self._tc.create_option_field_class(self._content_fc, self._tag_fc) | |
401 | ||
402 | foo_fc = self._tc.create_real_field_class() | |
403 | bar_fc = self._tc.create_string_field_class() | |
404 | baz_fc = self._tc.create_string_field_class() | |
405 | ||
406 | inner_struct_fc = self._tc.create_structure_field_class() | |
407 | inner_struct_fc.append_member('bar', bar_fc) | |
408 | inner_struct_fc.append_member('baz', baz_fc) | |
409 | inner_struct_fc.append_member('tag', self._tag_fc) | |
410 | inner_struct_fc.append_member('opt', fc) | |
411 | ||
412 | opt_struct_array_fc = self._tc.create_option_field_class(inner_struct_fc) | |
413 | ||
414 | outer_struct_fc = self._tc.create_structure_field_class() | |
415 | outer_struct_fc.append_member('foo', foo_fc) | |
416 | outer_struct_fc.append_member('inner_opt', opt_struct_array_fc) | |
417 | ||
418 | # The path to the selector field class is resolved when the | |
419 | # option field class is actually used, for example in a packet | |
420 | # context. | |
421 | self._tc.create_stream_class( | |
422 | packet_context_field_class=outer_struct_fc, supports_packets=True | |
423 | ) | |
424 | ||
425 | return fc | |
426 | ||
427 | def test_field_path_len(self): | |
428 | fc = self._create_field_class_for_field_path_test() | |
429 | self.assertEqual(len(fc.selector_field_path), 3) | |
430 | ||
431 | def test_field_path_iter(self): | |
432 | fc = self._create_field_class_for_field_path_test() | |
433 | path_items = list(fc.selector_field_path) | |
434 | ||
435 | self.assertEqual(len(path_items), 3) | |
436 | ||
437 | self.assertIsInstance(path_items[0], bt2._IndexFieldPathItem) | |
438 | self.assertEqual(path_items[0].index, 1) | |
439 | ||
440 | self.assertIsInstance(path_items[1], bt2._CurrentOptionContentFieldPathItem) | |
441 | ||
442 | self.assertIsInstance(path_items[2], bt2._IndexFieldPathItem) | |
443 | self.assertEqual(path_items[2].index, 2) | |
444 | ||
445 | def test_field_path_root_scope(self): | |
446 | fc = self._create_field_class_for_field_path_test() | |
447 | self.assertEqual( | |
448 | fc.selector_field_path.root_scope, bt2.FieldPathScope.PACKET_CONTEXT | |
449 | ) | |
450 | ||
451 | def test_create_invalid_field_class(self): | |
452 | with self.assertRaises(TypeError): | |
453 | self._tc.create_option_field_class(object()) | |
454 | ||
455 | def test_create_invalid_selector_type(self): | |
456 | with self.assertRaises(TypeError): | |
457 | self._tc.create_option_field_class(self._content_fc, 17) | |
458 | ||
459 | ||
61d96b89 FD |
460 | class VariantFieldClassWithoutSelectorTestCase( |
461 | _TestElementContainer, unittest.TestCase | |
462 | ): | |
463 | _append_element_method = staticmethod( | |
c946c9de | 464 | bt2._VariantFieldClassWithoutSelector.append_option |
61d96b89 FD |
465 | ) |
466 | _at_index_method = staticmethod( | |
c946c9de | 467 | bt2._VariantFieldClassWithoutSelector.option_at_index |
61d96b89 | 468 | ) |
02b61fe0 PP |
469 | |
470 | def _create_default_fc(self): | |
471 | return self._tc.create_variant_field_class() | |
472 | ||
473 | ||
474 | class _VariantFieldClassWithSelectorTestCase: | |
32656995 | 475 | def setUp(self): |
c8820b76 | 476 | self._tc = get_default_trace_class() |
02b61fe0 PP |
477 | self._spec_set_up() |
478 | self._fc = self._create_default_fc() | |
479 | ||
480 | def _create_default_fc(self): | |
481 | return self._tc.create_variant_field_class(self._selector_fc) | |
c8820b76 SM |
482 | |
483 | def test_create_default(self): | |
484 | self.assertIsNotNone(self._fc) | |
32656995 | 485 | |
02b61fe0 PP |
486 | def test_append_element(self): |
487 | str_field_class = self._tc.create_string_field_class() | |
488 | self._fc.append_option('str', str_field_class, self._ranges1) | |
489 | opt = self._fc['str'] | |
490 | self.assertEqual(opt.field_class.addr, str_field_class.addr) | |
491 | self.assertEqual(opt.name, 'str') | |
492 | self.assertEqual(opt.ranges.addr, self._ranges1.addr) | |
493 | ||
494 | def test_append_element_kwargs(self): | |
495 | int_field_class = self._tc.create_signed_integer_field_class(32) | |
61d96b89 FD |
496 | self._fc.append_option( |
497 | name='int32', field_class=int_field_class, ranges=self._ranges1 | |
498 | ) | |
02b61fe0 PP |
499 | opt = self._fc['int32'] |
500 | self.assertEqual(opt.field_class.addr, int_field_class.addr) | |
501 | self.assertEqual(opt.name, 'int32') | |
502 | self.assertEqual(opt.ranges.addr, self._ranges1.addr) | |
c8820b76 | 503 | |
02b61fe0 PP |
504 | def test_append_element_invalid_name(self): |
505 | sub_fc = self._tc.create_string_field_class() | |
32656995 | 506 | |
02b61fe0 PP |
507 | with self.assertRaises(TypeError): |
508 | self._fc.append_option(self._fc, 23, sub_fc) | |
32656995 | 509 | |
02b61fe0 PP |
510 | def test_append_element_invalid_field_class(self): |
511 | with self.assertRaises(TypeError): | |
512 | self._fc.append_option(self._fc, 'yes', object()) | |
513 | ||
514 | def test_append_element_invalid_ranges(self): | |
515 | sub_fc = self._tc.create_string_field_class() | |
516 | ||
517 | with self.assertRaises(TypeError): | |
518 | self._fc.append_option(self._fc, sub_fc, 'lel') | |
519 | ||
520 | def test_append_element_dup_name(self): | |
521 | sub_fc1 = self._tc.create_string_field_class() | |
522 | sub_fc2 = self._tc.create_string_field_class() | |
523 | ||
3b2be708 | 524 | with self.assertRaises(ValueError): |
02b61fe0 PP |
525 | self._fc.append_option('yes', sub_fc1, self._ranges1) |
526 | self._fc.append_option('yes', sub_fc2, self._ranges2) | |
527 | ||
528 | def test_append_element_invalid_ranges_signedness(self): | |
529 | sub_fc = self._tc.create_string_field_class() | |
530 | ||
531 | with self.assertRaises(TypeError): | |
532 | self._fc.append_option(self._fc, sub_fc, self._inval_ranges) | |
533 | ||
534 | def test_iadd(self): | |
535 | other_fc = self._create_default_fc() | |
536 | a_field_class = self._tc.create_real_field_class() | |
537 | self._fc.append_option('a_float', a_field_class, self._ranges1) | |
538 | c_field_class = self._tc.create_string_field_class() | |
61d96b89 FD |
539 | d_field_class = self._tc.create_signed_enumeration_field_class( |
540 | field_value_range=32 | |
541 | ) | |
02b61fe0 PP |
542 | self._fc += [ |
543 | ('c_string', c_field_class, self._ranges2), | |
544 | ('d_enum', d_field_class, self._ranges3), | |
545 | ] | |
546 | self.assertEqual(self._fc['a_float'].field_class.addr, a_field_class.addr) | |
547 | self.assertEqual(self._fc['a_float'].name, 'a_float') | |
548 | self.assertEqual(self._fc['a_float'].ranges, self._ranges1) | |
549 | self.assertEqual(self._fc['c_string'].field_class.addr, c_field_class.addr) | |
550 | self.assertEqual(self._fc['c_string'].name, 'c_string') | |
551 | self.assertEqual(self._fc['c_string'].ranges, self._ranges2) | |
552 | self.assertEqual(self._fc['d_enum'].field_class.addr, d_field_class.addr) | |
553 | self.assertEqual(self._fc['d_enum'].name, 'd_enum') | |
554 | self.assertEqual(self._fc['d_enum'].ranges, self._ranges3) | |
555 | ||
556 | def test_bool_op(self): | |
557 | self.assertFalse(self._fc) | |
558 | self._fc.append_option('a', self._tc.create_string_field_class(), self._ranges1) | |
559 | self.assertTrue(self._fc) | |
560 | ||
561 | def test_len(self): | |
562 | self._fc.append_option('a', self._tc.create_string_field_class(), self._ranges1) | |
563 | self._fc.append_option('b', self._tc.create_string_field_class(), self._ranges2) | |
564 | self._fc.append_option('c', self._tc.create_string_field_class(), self._ranges3) | |
565 | self.assertEqual(len(self._fc), 3) | |
566 | ||
567 | def test_getitem(self): | |
568 | a_fc = self._tc.create_signed_integer_field_class(32) | |
569 | b_fc = self._tc.create_string_field_class() | |
570 | c_fc = self._tc.create_real_field_class() | |
571 | self._fc.append_option('a', a_fc, self._ranges1) | |
572 | self._fc.append_option('b', b_fc, self._ranges2) | |
573 | self._fc.append_option('c', c_fc, self._ranges3) | |
574 | self.assertEqual(self._fc['b'].field_class.addr, b_fc.addr) | |
575 | self.assertEqual(self._fc['b'].name, 'b') | |
576 | self.assertEqual(self._fc['b'].ranges.addr, self._ranges2.addr) | |
577 | ||
578 | def test_getitem_invalid_key_type(self): | |
579 | with self.assertRaises(TypeError): | |
580 | self._fc[0] | |
581 | ||
582 | def test_getitem_invalid_key(self): | |
583 | with self.assertRaises(KeyError): | |
584 | self._fc['no way'] | |
585 | ||
586 | def test_contains(self): | |
587 | self.assertFalse('a' in self._fc) | |
588 | self._fc.append_option('a', self._tc.create_string_field_class(), self._ranges1) | |
589 | self.assertTrue('a' in self._fc) | |
590 | ||
591 | def test_iter(self): | |
592 | a_fc = self._tc.create_signed_integer_field_class(32) | |
593 | b_fc = self._tc.create_string_field_class() | |
594 | c_fc = self._tc.create_real_field_class() | |
595 | opts = ( | |
596 | ('a', a_fc, self._ranges1), | |
597 | ('b', b_fc, self._ranges2), | |
598 | ('c', c_fc, self._ranges3), | |
599 | ) | |
600 | ||
601 | for opt in opts: | |
602 | self._fc.append_option(*opt) | |
32656995 | 603 | |
02b61fe0 PP |
604 | for (name, opt), test_opt in zip(self._fc.items(), opts): |
605 | self.assertEqual(opt.name, test_opt[0]) | |
606 | self.assertEqual(name, opt.name) | |
607 | self.assertEqual(opt.field_class.addr, test_opt[1].addr) | |
608 | self.assertEqual(opt.ranges.addr, test_opt[2].addr) | |
609 | ||
610 | def test_at_index(self): | |
611 | a_fc = self._tc.create_signed_integer_field_class(32) | |
612 | b_fc = self._tc.create_string_field_class() | |
613 | c_fc = self._tc.create_real_field_class() | |
614 | self._fc.append_option('c', c_fc, self._ranges1) | |
615 | self._fc.append_option('a', a_fc, self._ranges2) | |
616 | self._fc.append_option('b', b_fc, self._ranges3) | |
617 | self.assertEqual(self._fc.option_at_index(1).field_class.addr, a_fc.addr) | |
618 | self.assertEqual(self._fc.option_at_index(1).name, 'a') | |
619 | self.assertEqual(self._fc.option_at_index(1).ranges.addr, self._ranges2.addr) | |
620 | ||
621 | def test_at_index_invalid(self): | |
61d96b89 FD |
622 | self._fc.append_option( |
623 | 'c', self._tc.create_signed_integer_field_class(32), self._ranges3 | |
624 | ) | |
02b61fe0 PP |
625 | |
626 | with self.assertRaises(TypeError): | |
627 | self._fc.option_at_index('yes') | |
628 | ||
629 | def test_at_index_out_of_bounds_after(self): | |
61d96b89 FD |
630 | self._fc.append_option( |
631 | 'c', self._tc.create_signed_integer_field_class(32), self._ranges3 | |
632 | ) | |
02b61fe0 PP |
633 | |
634 | with self.assertRaises(IndexError): | |
635 | self._fc.option_at_index(len(self._fc)) | |
636 | ||
637 | def _fill_default_fc_for_field_path_test(self): | |
c8820b76 SM |
638 | # Create something equivalent to: |
639 | # | |
640 | # struct outer_struct_fc { | |
641 | # real foo; | |
642 | # struct inner_struct_fc { | |
02b61fe0 | 643 | # [u]int64_t selector; |
c8820b76 SM |
644 | # string bar; |
645 | # string baz; | |
02b61fe0 PP |
646 | # variant <selector> { |
647 | # real a; // selected with self._ranges1 | |
648 | # int21_t b; // selected with self._ranges2 | |
649 | # uint34_t c; // selected with self._ranges3 | |
c8820b76 SM |
650 | # } variant; |
651 | # } inner_struct[2]; | |
652 | # }; | |
02b61fe0 | 653 | self._fc.append_option('a', self._tc.create_real_field_class(), self._ranges1) |
61d96b89 FD |
654 | self._fc.append_option( |
655 | 'b', self._tc.create_signed_integer_field_class(21), self._ranges2 | |
656 | ) | |
657 | self._fc.append_option( | |
658 | 'c', self._tc.create_unsigned_integer_field_class(34), self._ranges3 | |
659 | ) | |
32656995 | 660 | |
c8820b76 SM |
661 | foo_fc = self._tc.create_real_field_class() |
662 | bar_fc = self._tc.create_string_field_class() | |
663 | baz_fc = self._tc.create_string_field_class() | |
32656995 | 664 | |
c8820b76 | 665 | inner_struct_fc = self._tc.create_structure_field_class() |
02b61fe0 | 666 | inner_struct_fc.append_member('selector', self._selector_fc) |
c8820b76 SM |
667 | inner_struct_fc.append_member('bar', bar_fc) |
668 | inner_struct_fc.append_member('baz', baz_fc) | |
02b61fe0 | 669 | inner_struct_fc.append_member('variant', self._fc) |
32656995 | 670 | |
61d96b89 FD |
671 | inner_struct_array_fc = self._tc.create_static_array_field_class( |
672 | inner_struct_fc, 2 | |
673 | ) | |
32656995 | 674 | |
c8820b76 SM |
675 | outer_struct_fc = self._tc.create_structure_field_class() |
676 | outer_struct_fc.append_member('foo', foo_fc) | |
677 | outer_struct_fc.append_member('inner_struct', inner_struct_array_fc) | |
32656995 | 678 | |
c8820b76 SM |
679 | # The path to the selector field is resolved when the sequence is |
680 | # actually used, for example in a packet context. | |
61d96b89 FD |
681 | self._tc.create_stream_class( |
682 | supports_packets=True, packet_context_field_class=outer_struct_fc | |
683 | ) | |
32656995 | 684 | |
c8820b76 | 685 | def test_selector_field_path_length(self): |
02b61fe0 PP |
686 | self._fill_default_fc_for_field_path_test() |
687 | self.assertEqual(len(self._fc.selector_field_path), 3) | |
32656995 | 688 | |
c8820b76 | 689 | def test_selector_field_path_iter(self): |
02b61fe0 PP |
690 | self._fill_default_fc_for_field_path_test() |
691 | path_items = list(self._fc.selector_field_path) | |
32656995 | 692 | |
c8820b76 | 693 | self.assertEqual(len(path_items), 3) |
32656995 | 694 | |
c946c9de | 695 | self.assertIsInstance(path_items[0], bt2._IndexFieldPathItem) |
c8820b76 | 696 | self.assertEqual(path_items[0].index, 1) |
32656995 | 697 | |
c946c9de | 698 | self.assertIsInstance(path_items[1], bt2._CurrentArrayElementFieldPathItem) |
32656995 | 699 | |
c946c9de | 700 | self.assertIsInstance(path_items[2], bt2._IndexFieldPathItem) |
c8820b76 | 701 | self.assertEqual(path_items[2].index, 0) |
32656995 | 702 | |
c8820b76 | 703 | def test_selector_field_path_root_scope(self): |
02b61fe0 | 704 | self._fill_default_fc_for_field_path_test() |
61d96b89 | 705 | self.assertEqual( |
056995e5 | 706 | self._fc.selector_field_path.root_scope, bt2.FieldPathScope.PACKET_CONTEXT |
61d96b89 | 707 | ) |
02b61fe0 PP |
708 | |
709 | ||
61d96b89 FD |
710 | class VariantFieldClassWithUnsignedSelectorTestCase( |
711 | _VariantFieldClassWithSelectorTestCase, unittest.TestCase | |
712 | ): | |
02b61fe0 PP |
713 | def _spec_set_up(self): |
714 | self._ranges1 = bt2.UnsignedIntegerRangeSet([(1, 4), (18, 47)]) | |
715 | self._ranges2 = bt2.UnsignedIntegerRangeSet([(5, 5)]) | |
716 | self._ranges3 = bt2.UnsignedIntegerRangeSet([(8, 16), (48, 99)]) | |
717 | self._inval_ranges = bt2.SignedIntegerRangeSet([(-8, 16), (48, 99)]) | |
718 | self._selector_fc = self._tc.create_unsigned_integer_field_class() | |
719 | ||
720 | ||
61d96b89 FD |
721 | class VariantFieldClassWithSignedSelectorTestCase( |
722 | _VariantFieldClassWithSelectorTestCase, unittest.TestCase | |
723 | ): | |
02b61fe0 PP |
724 | def _spec_set_up(self): |
725 | self._ranges1 = bt2.SignedIntegerRangeSet([(-10, -4), (18, 47)]) | |
726 | self._ranges2 = bt2.SignedIntegerRangeSet([(-3, -3)]) | |
727 | self._ranges3 = bt2.SignedIntegerRangeSet([(8, 16), (48, 99)]) | |
728 | self._inval_ranges = bt2.UnsignedIntegerRangeSet([(8, 16), (48, 99)]) | |
729 | self._selector_fc = self._tc.create_signed_integer_field_class() | |
32656995 | 730 | |
c8820b76 SM |
731 | |
732 | class StaticArrayFieldClassTestCase(unittest.TestCase): | |
733 | def setUp(self): | |
734 | self._tc = get_default_trace_class() | |
735 | self._elem_fc = self._tc.create_signed_integer_field_class(23) | |
32656995 SM |
736 | |
737 | def test_create_default(self): | |
c8820b76 SM |
738 | fc = self._tc.create_static_array_field_class(self._elem_fc, 45) |
739 | self.assertEqual(fc.element_field_class.addr, self._elem_fc.addr) | |
740 | self.assertEqual(fc.length, 45) | |
32656995 | 741 | |
c8820b76 | 742 | def test_create_invalid_elem_field_class(self): |
32656995 | 743 | with self.assertRaises(TypeError): |
c8820b76 | 744 | self._tc.create_static_array_field_class(object(), 45) |
32656995 SM |
745 | |
746 | def test_create_invalid_length(self): | |
747 | with self.assertRaises(ValueError): | |
61d96b89 FD |
748 | self._tc.create_static_array_field_class( |
749 | self._tc.create_string_field_class(), -17 | |
750 | ) | |
32656995 SM |
751 | |
752 | def test_create_invalid_length_type(self): | |
753 | with self.assertRaises(TypeError): | |
61d96b89 FD |
754 | self._tc.create_static_array_field_class( |
755 | self._tc.create_string_field_class(), 'the length' | |
756 | ) | |
c8820b76 SM |
757 | |
758 | ||
759 | class DynamicArrayFieldClassTestCase(unittest.TestCase): | |
760 | def setUp(self): | |
761 | self._tc = get_default_trace_class() | |
762 | self._elem_fc = self._tc.create_signed_integer_field_class(23) | |
763 | self._len_fc = self._tc.create_unsigned_integer_field_class(12) | |
32656995 | 764 | |
c8820b76 SM |
765 | def test_create_default(self): |
766 | fc = self._tc.create_dynamic_array_field_class(self._elem_fc) | |
767 | self.assertEqual(fc.element_field_class.addr, self._elem_fc.addr) | |
768 | self.assertIsNone(fc.length_field_path, None) | |
32656995 | 769 | |
c8820b76 SM |
770 | def _create_field_class_for_field_path_test(self): |
771 | # Create something a field class that is equivalent to: | |
772 | # | |
773 | # struct outer_struct_fc { | |
774 | # real foo; | |
775 | # struct inner_struct_fc { | |
776 | # string bar; | |
777 | # string baz; | |
778 | # uint12_t len; | |
779 | # uint23_t dyn_array[len]; | |
780 | # } inner_struct[2]; | |
781 | # }; | |
32656995 | 782 | |
c8820b76 | 783 | fc = self._tc.create_dynamic_array_field_class(self._elem_fc, self._len_fc) |
32656995 | 784 | |
c8820b76 SM |
785 | foo_fc = self._tc.create_real_field_class() |
786 | bar_fc = self._tc.create_string_field_class() | |
787 | baz_fc = self._tc.create_string_field_class() | |
32656995 | 788 | |
c8820b76 SM |
789 | inner_struct_fc = self._tc.create_structure_field_class() |
790 | inner_struct_fc.append_member('bar', bar_fc) | |
791 | inner_struct_fc.append_member('baz', baz_fc) | |
792 | inner_struct_fc.append_member('len', self._len_fc) | |
793 | inner_struct_fc.append_member('dyn_array', fc) | |
32656995 | 794 | |
61d96b89 FD |
795 | inner_struct_array_fc = self._tc.create_static_array_field_class( |
796 | inner_struct_fc, 2 | |
797 | ) | |
c8820b76 SM |
798 | |
799 | outer_struct_fc = self._tc.create_structure_field_class() | |
800 | outer_struct_fc.append_member('foo', foo_fc) | |
801 | outer_struct_fc.append_member('inner_struct', inner_struct_array_fc) | |
802 | ||
803 | # The path to the length field is resolved when the sequence is | |
804 | # actually used, for example in a packet context. | |
61d96b89 FD |
805 | self._tc.create_stream_class( |
806 | packet_context_field_class=outer_struct_fc, supports_packets=True | |
807 | ) | |
c8820b76 SM |
808 | |
809 | return fc | |
810 | ||
811 | def test_field_path_len(self): | |
812 | fc = self._create_field_class_for_field_path_test() | |
813 | self.assertEqual(len(fc.length_field_path), 3) | |
814 | ||
815 | def test_field_path_iter(self): | |
816 | fc = self._create_field_class_for_field_path_test() | |
817 | path_items = list(fc.length_field_path) | |
818 | ||
819 | self.assertEqual(len(path_items), 3) | |
820 | ||
c946c9de | 821 | self.assertIsInstance(path_items[0], bt2._IndexFieldPathItem) |
c8820b76 SM |
822 | self.assertEqual(path_items[0].index, 1) |
823 | ||
c946c9de | 824 | self.assertIsInstance(path_items[1], bt2._CurrentArrayElementFieldPathItem) |
c8820b76 | 825 | |
c946c9de | 826 | self.assertIsInstance(path_items[2], bt2._IndexFieldPathItem) |
c8820b76 SM |
827 | self.assertEqual(path_items[2].index, 2) |
828 | ||
829 | def test_field_path_root_scope(self): | |
830 | fc = self._create_field_class_for_field_path_test() | |
056995e5 PP |
831 | self.assertEqual( |
832 | fc.length_field_path.root_scope, bt2.FieldPathScope.PACKET_CONTEXT | |
833 | ) | |
32656995 SM |
834 | |
835 | def test_create_invalid_field_class(self): | |
836 | with self.assertRaises(TypeError): | |
c8820b76 | 837 | self._tc.create_dynamic_array_field_class(object()) |
32656995 SM |
838 | |
839 | def test_create_invalid_length_type(self): | |
840 | with self.assertRaises(TypeError): | |
61d96b89 FD |
841 | self._tc.create_dynamic_array_field_class( |
842 | self._tc.create_string_field_class(), 17 | |
843 | ) | |
32656995 | 844 | |
32656995 | 845 | |
c8820b76 SM |
846 | if __name__ == "__main__": |
847 | unittest.main() |