2 # Copyright (C) 2019 EfficiOS Inc.
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
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.
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.
21 from utils
import get_default_trace_class
24 class BoolFieldClassTestCase(unittest
.TestCase
):
26 tc
= get_default_trace_class()
27 self
._fc
= tc
.create_bool_field_class()
29 def test_create_default(self
):
30 self
.assertIsNotNone(self
._fc
)
33 class BitArrayFieldClassTestCase(unittest
.TestCase
):
35 self
._tc
= get_default_trace_class()
36 self
._fc
= self
._tc
.create_bit_array_field_class(17)
38 def test_create_default(self
):
39 self
.assertIsNotNone(self
._fc
)
41 def test_create_length_out_of_range(self
):
42 with self
.assertRaises(ValueError):
43 self
._tc
.create_bit_array_field_class(65)
45 def test_create_length_zero(self
):
46 with self
.assertRaises(ValueError):
47 self
._tc
.create_bit_array_field_class(0)
49 def test_create_length_invalid_type(self
):
50 with self
.assertRaises(TypeError):
51 self
._tc
.create_bit_array_field_class('lel')
53 def test_length_prop(self
):
54 self
.assertEqual(self
._fc
.length
, 17)
57 class _TestIntegerFieldClassProps
:
58 def test_create_default(self
):
59 fc
= self
._create
_func
()
60 self
.assertEqual(fc
.field_value_range
, 64)
61 self
.assertEqual(fc
.preferred_display_base
, bt2
.IntegerDisplayBase
.DECIMAL
)
63 def test_create_range(self
):
64 fc
= self
._create
_func
(field_value_range
=35)
65 self
.assertEqual(fc
.field_value_range
, 35)
67 fc
= self
._create
_func
(36)
68 self
.assertEqual(fc
.field_value_range
, 36)
70 def test_create_invalid_range(self
):
71 with self
.assertRaises(TypeError):
72 self
._create
_func
('yes')
74 with self
.assertRaises(TypeError):
75 self
._create
_func
(field_value_range
='yes')
77 with self
.assertRaises(ValueError):
78 self
._create
_func
(field_value_range
=-2)
80 with self
.assertRaises(ValueError):
81 self
._create
_func
(field_value_range
=0)
83 def test_create_base(self
):
84 fc
= self
._create
_func
(
85 preferred_display_base
=bt2
.IntegerDisplayBase
.HEXADECIMAL
87 self
.assertEqual(fc
.preferred_display_base
, bt2
.IntegerDisplayBase
.HEXADECIMAL
)
89 def test_create_invalid_base_type(self
):
90 with self
.assertRaises(TypeError):
91 self
._create
_func
(preferred_display_base
='yes')
93 def test_create_invalid_base_value(self
):
94 with self
.assertRaises(ValueError):
95 self
._create
_func
(preferred_display_base
=444)
97 def test_create_full(self
):
98 fc
= self
._create
_func
(24, preferred_display_base
=bt2
.IntegerDisplayBase
.OCTAL
)
99 self
.assertEqual(fc
.field_value_range
, 24)
100 self
.assertEqual(fc
.preferred_display_base
, bt2
.IntegerDisplayBase
.OCTAL
)
103 class IntegerFieldClassTestCase(_TestIntegerFieldClassProps
, unittest
.TestCase
):
105 self
._tc
= get_default_trace_class()
106 self
._create
_func
= self
._tc
.create_signed_integer_field_class
109 class RealFieldClassTestCase(unittest
.TestCase
):
111 self
._tc
= get_default_trace_class()
113 def test_create_default(self
):
114 fc
= self
._tc
.create_real_field_class()
115 self
.assertFalse(fc
.is_single_precision
)
117 def test_create_is_single_precision(self
):
118 fc
= self
._tc
.create_real_field_class(is_single_precision
=True)
119 self
.assertTrue(fc
.is_single_precision
)
121 def test_create_invalid_is_single_precision(self
):
122 with self
.assertRaises(TypeError):
123 self
._tc
.create_real_field_class(is_single_precision
='hohoho')
126 # Converts an _EnumerationFieldClassMapping to a list of ranges:
128 # [(lower0, upper0), (lower1, upper1), ...]
131 def enum_mapping_to_set(mapping
):
132 return {(x
.lower
, x
.upper
) for x
in mapping
.ranges
}
135 class _EnumerationFieldClassTestCase(_TestIntegerFieldClassProps
):
137 self
._tc
= get_default_trace_class()
139 self
._fc
= self
._create
_func
()
141 def test_create_from_invalid_type(self
):
142 with self
.assertRaises(TypeError):
143 self
._create
_func
('coucou')
145 def test_add_mapping_simple(self
):
146 self
._fc
.add_mapping('hello', self
._ranges
1)
147 mapping
= self
._fc
['hello']
148 self
.assertEqual(mapping
.label
, 'hello')
149 self
.assertEqual(mapping
.ranges
, self
._ranges
1)
151 def test_add_mapping_simple_kwargs(self
):
152 self
._fc
.add_mapping(label
='hello', ranges
=self
._ranges
1)
153 mapping
= self
._fc
['hello']
154 self
.assertEqual(mapping
.label
, 'hello')
155 self
.assertEqual(mapping
.ranges
, self
._ranges
1)
157 def test_add_mapping_invalid_name(self
):
158 with self
.assertRaises(TypeError):
159 self
._fc
.add_mapping(17, self
._ranges
1)
161 def test_add_mapping_invalid_range(self
):
162 with self
.assertRaises(TypeError):
163 self
._fc
.add_mapping('allo', 'meow')
165 def test_add_mapping_dup_label(self
):
166 with self
.assertRaises(ValueError):
167 self
._fc
.add_mapping('a', self
._ranges
1)
168 self
._fc
.add_mapping('a', self
._ranges
2)
170 def test_add_mapping_invalid_ranges_signedness(self
):
171 with self
.assertRaises(TypeError):
172 self
._fc
.add_mapping('allo', self
._inval
_ranges
)
175 self
._fc
.add_mapping('c', self
._ranges
1)
177 self
._fc
+= [('d', self
._ranges
2), ('e', self
._ranges
3)]
179 self
.assertEqual(len(self
._fc
), 3)
180 self
.assertEqual(self
._fc
['c'].label
, 'c')
181 self
.assertEqual(self
._fc
['c'].ranges
, self
._ranges
1)
182 self
.assertEqual(self
._fc
['d'].label
, 'd')
183 self
.assertEqual(self
._fc
['d'].ranges
, self
._ranges
2)
184 self
.assertEqual(self
._fc
['e'].label
, 'e')
185 self
.assertEqual(self
._fc
['e'].ranges
, self
._ranges
3)
187 def test_bool_op(self
):
188 self
.assertFalse(self
._fc
)
189 self
._fc
.add_mapping('a', self
._ranges
1)
190 self
.assertTrue(self
._fc
)
193 self
._fc
.add_mapping('a', self
._ranges
1)
194 self
._fc
.add_mapping('b', self
._ranges
2)
195 self
._fc
.add_mapping('c', self
._ranges
3)
196 self
.assertEqual(len(self
._fc
), 3)
198 def test_getitem(self
):
199 self
._fc
.add_mapping('a', self
._ranges
1)
200 self
._fc
.add_mapping('b', self
._ranges
2)
201 self
._fc
.add_mapping('c', self
._ranges
3)
202 mapping
= self
._fc
['a']
203 self
.assertEqual(mapping
.label
, 'a')
204 self
.assertEqual(mapping
.ranges
, self
._ranges
1)
206 def test_getitem_nonexistent(self
):
207 with self
.assertRaises(KeyError):
208 self
._fc
['doesnotexist']
211 self
._fc
.add_mapping('a', self
._ranges
1)
212 self
._fc
.add_mapping('b', self
._ranges
2)
213 self
._fc
.add_mapping('c', self
._ranges
3)
215 # This exercises iteration.
216 labels
= sorted(self
._fc
)
218 self
.assertEqual(labels
, ['a', 'b', 'c'])
220 def test_find_by_value(self
):
221 self
._fc
.add_mapping('a', self
._ranges
1)
222 self
._fc
.add_mapping('b', self
._ranges
2)
223 self
._fc
.add_mapping('c', self
._ranges
3)
224 mappings
= self
._fc
.mappings_for_value(self
._value
_in
_range
_1_and
_3)
225 labels
= set([mapping
.label
for mapping
in mappings
])
226 expected_labels
= set(['a', 'c'])
227 self
.assertEqual(labels
, expected_labels
)
230 class UnsignedEnumerationFieldClassTestCase(
231 _EnumerationFieldClassTestCase
, unittest
.TestCase
233 def _spec_set_up(self
):
234 self
._ranges
1 = bt2
.UnsignedIntegerRangeSet([(1, 4), (18, 47)])
235 self
._ranges
2 = bt2
.UnsignedIntegerRangeSet([(5, 5)])
236 self
._ranges
3 = bt2
.UnsignedIntegerRangeSet([(8, 22), (48, 99)])
237 self
._inval
_ranges
= bt2
.SignedIntegerRangeSet([(-8, -5), (48, 1928)])
238 self
._value
_in
_range
_1_and
_3 = 20
239 self
._create
_func
= self
._tc
.create_unsigned_enumeration_field_class
242 class SignedEnumerationFieldClassTestCase(
243 _EnumerationFieldClassTestCase
, unittest
.TestCase
245 def _spec_set_up(self
):
246 self
._ranges
1 = bt2
.SignedIntegerRangeSet([(-10, -4), (18, 47)])
247 self
._ranges
2 = bt2
.SignedIntegerRangeSet([(-3, -3)])
248 self
._ranges
3 = bt2
.SignedIntegerRangeSet([(-100, -1), (8, 16), (48, 99)])
249 self
._inval
_ranges
= bt2
.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
250 self
._value
_in
_range
_1_and
_3 = -7
251 self
._create
_func
= self
._tc
.create_signed_enumeration_field_class
254 class StringFieldClassTestCase(unittest
.TestCase
):
256 tc
= get_default_trace_class()
257 self
._fc
= tc
.create_string_field_class()
259 def test_create_default(self
):
260 self
.assertIsNotNone(self
._fc
)
263 class _TestElementContainer
:
265 self
._tc
= get_default_trace_class()
266 self
._fc
= self
._create
_default
_fc
()
268 def test_create_default(self
):
269 self
.assertIsNotNone(self
._fc
)
271 def test_append_element(self
):
272 int_field_class
= self
._tc
.create_signed_integer_field_class(32)
273 self
._append
_element
_method
(self
._fc
, 'int32', int_field_class
)
274 field_class
= self
._fc
['int32'].field_class
275 self
.assertEqual(field_class
.addr
, int_field_class
.addr
)
277 def test_append_element_kwargs(self
):
278 int_field_class
= self
._tc
.create_signed_integer_field_class(32)
279 self
._append
_element
_method
(self
._fc
, name
='int32', field_class
=int_field_class
)
280 field_class
= self
._fc
['int32'].field_class
281 self
.assertEqual(field_class
.addr
, int_field_class
.addr
)
283 def test_append_element_invalid_name(self
):
284 sub_fc
= self
._tc
.create_string_field_class()
286 with self
.assertRaises(TypeError):
287 self
._append
_element
_method
(self
._fc
, 23, sub_fc
)
289 def test_append_element_invalid_field_class(self
):
290 with self
.assertRaises(TypeError):
291 self
._append
_element
_method
(self
._fc
, 'yes', object())
293 def test_append_element_dup_name(self
):
294 sub_fc1
= self
._tc
.create_string_field_class()
295 sub_fc2
= self
._tc
.create_string_field_class()
297 with self
.assertRaises(ValueError):
298 self
._append
_element
_method
(self
._fc
, 'yes', sub_fc1
)
299 self
._append
_element
_method
(self
._fc
, 'yes', sub_fc2
)
302 other_fc
= self
._create
_default
_fc
()
303 a_field_class
= self
._tc
.create_real_field_class()
304 b_field_class
= self
._tc
.create_signed_integer_field_class(17)
305 self
._append
_element
_method
(self
._fc
, 'a_float', a_field_class
)
306 self
._append
_element
_method
(self
._fc
, 'b_int', b_field_class
)
307 c_field_class
= self
._tc
.create_string_field_class()
308 d_field_class
= self
._tc
.create_signed_enumeration_field_class(
311 e_field_class
= self
._tc
.create_structure_field_class()
313 ('c_string', c_field_class
),
314 ('d_enum', d_field_class
),
315 ('e_struct', e_field_class
),
317 self
.assertEqual(self
._fc
['a_float'].field_class
.addr
, a_field_class
.addr
)
318 self
.assertEqual(self
._fc
['a_float'].name
, 'a_float')
319 self
.assertEqual(self
._fc
['b_int'].field_class
.addr
, b_field_class
.addr
)
320 self
.assertEqual(self
._fc
['b_int'].name
, 'b_int')
321 self
.assertEqual(self
._fc
['c_string'].field_class
.addr
, c_field_class
.addr
)
322 self
.assertEqual(self
._fc
['c_string'].name
, 'c_string')
323 self
.assertEqual(self
._fc
['d_enum'].field_class
.addr
, d_field_class
.addr
)
324 self
.assertEqual(self
._fc
['d_enum'].name
, 'd_enum')
325 self
.assertEqual(self
._fc
['e_struct'].field_class
.addr
, e_field_class
.addr
)
326 self
.assertEqual(self
._fc
['e_struct'].name
, 'e_struct')
328 def test_bool_op(self
):
329 self
.assertFalse(self
._fc
)
330 self
._append
_element
_method
(self
._fc
, 'a', self
._tc
.create_string_field_class())
331 self
.assertTrue(self
._fc
)
334 self
._append
_element
_method
(self
._fc
, 'a', self
._tc
.create_string_field_class())
335 self
._append
_element
_method
(self
._fc
, 'b', self
._tc
.create_string_field_class())
336 self
._append
_element
_method
(self
._fc
, 'c', self
._tc
.create_string_field_class())
337 self
.assertEqual(len(self
._fc
), 3)
339 def test_getitem(self
):
340 a_fc
= self
._tc
.create_signed_integer_field_class(32)
341 b_fc
= self
._tc
.create_string_field_class()
342 c_fc
= self
._tc
.create_real_field_class()
343 self
._append
_element
_method
(self
._fc
, 'a', a_fc
)
344 self
._append
_element
_method
(self
._fc
, 'b', b_fc
)
345 self
._append
_element
_method
(self
._fc
, 'c', c_fc
)
346 self
.assertEqual(self
._fc
['b'].field_class
.addr
, b_fc
.addr
)
347 self
.assertEqual(self
._fc
['b'].name
, 'b')
349 def test_getitem_invalid_key_type(self
):
350 with self
.assertRaises(TypeError):
353 def test_getitem_invalid_key(self
):
354 with self
.assertRaises(KeyError):
357 def test_contains(self
):
358 self
.assertFalse('a' in self
._fc
)
359 self
._append
_element
_method
(self
._fc
, 'a', self
._tc
.create_string_field_class())
360 self
.assertTrue('a' in self
._fc
)
363 a_fc
= self
._tc
.create_signed_integer_field_class(32)
364 b_fc
= self
._tc
.create_string_field_class()
365 c_fc
= self
._tc
.create_real_field_class()
366 elements
= (('a', a_fc
), ('b', b_fc
), ('c', c_fc
))
368 for elem
in elements
:
369 self
._append
_element
_method
(self
._fc
, *elem
)
371 for (name
, element
), test_elem
in zip(self
._fc
.items(), elements
):
372 self
.assertEqual(element
.name
, test_elem
[0])
373 self
.assertEqual(name
, element
.name
)
374 self
.assertEqual(element
.field_class
.addr
, test_elem
[1].addr
)
376 def test_at_index(self
):
377 a_fc
= self
._tc
.create_signed_integer_field_class(32)
378 b_fc
= self
._tc
.create_string_field_class()
379 c_fc
= self
._tc
.create_real_field_class()
380 self
._append
_element
_method
(self
._fc
, 'c', c_fc
)
381 self
._append
_element
_method
(self
._fc
, 'a', a_fc
)
382 self
._append
_element
_method
(self
._fc
, 'b', b_fc
)
383 elem
= self
._at
_index
_method
(self
._fc
, 1)
384 self
.assertEqual(elem
.field_class
.addr
, a_fc
.addr
)
385 self
.assertEqual(elem
.name
, 'a')
387 def test_at_index_invalid(self
):
388 self
._append
_element
_method
(
389 self
._fc
, 'c', self
._tc
.create_signed_integer_field_class(32)
392 with self
.assertRaises(TypeError):
393 self
._at
_index
_method
(self
._fc
, 'yes')
395 def test_at_index_out_of_bounds_after(self
):
396 self
._append
_element
_method
(
397 self
._fc
, 'c', self
._tc
.create_signed_integer_field_class(32)
400 with self
.assertRaises(IndexError):
401 self
._at
_index
_method
(self
._fc
, len(self
._fc
))
404 class StructureFieldClassTestCase(_TestElementContainer
, unittest
.TestCase
):
405 _append_element_method
= staticmethod(bt2
._StructureFieldClass
.append_member
)
406 _at_index_method
= staticmethod(bt2
._StructureFieldClass
.member_at_index
)
408 def _create_default_fc(self
):
409 return self
._tc
.create_structure_field_class()
412 class OptionFieldClassTestCase(unittest
.TestCase
):
414 self
._tc
= get_default_trace_class()
415 self
._content
_fc
= self
._tc
.create_signed_integer_field_class(23)
416 self
._tag
_fc
= self
._tc
.create_bool_field_class()
418 def test_create_default(self
):
419 fc
= self
._tc
.create_option_field_class(self
._content
_fc
)
420 self
.assertEqual(fc
.field_class
.addr
, self
._content
_fc
.addr
)
421 self
.assertIsNone(fc
.selector_field_path
, None)
423 def _create_field_class_for_field_path_test(self
):
424 fc
= self
._tc
.create_option_field_class(self
._content
_fc
, self
._tag
_fc
)
426 foo_fc
= self
._tc
.create_real_field_class()
427 bar_fc
= self
._tc
.create_string_field_class()
428 baz_fc
= self
._tc
.create_string_field_class()
430 inner_struct_fc
= self
._tc
.create_structure_field_class()
431 inner_struct_fc
.append_member('bar', bar_fc
)
432 inner_struct_fc
.append_member('baz', baz_fc
)
433 inner_struct_fc
.append_member('tag', self
._tag
_fc
)
434 inner_struct_fc
.append_member('opt', fc
)
436 opt_struct_array_fc
= self
._tc
.create_option_field_class(inner_struct_fc
)
438 outer_struct_fc
= self
._tc
.create_structure_field_class()
439 outer_struct_fc
.append_member('foo', foo_fc
)
440 outer_struct_fc
.append_member('inner_opt', opt_struct_array_fc
)
442 # The path to the selector field class is resolved when the
443 # option field class is actually used, for example in a packet
445 self
._tc
.create_stream_class(
446 packet_context_field_class
=outer_struct_fc
, supports_packets
=True
451 def test_field_path_len(self
):
452 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
453 self
.assertEqual(len(fc
.selector_field_path
), 3)
455 def test_field_path_iter(self
):
456 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
457 path_items
= list(fc
.selector_field_path
)
459 self
.assertEqual(len(path_items
), 3)
461 self
.assertIsInstance(path_items
[0], bt2
._IndexFieldPathItem
)
462 self
.assertEqual(path_items
[0].index
, 1)
464 self
.assertIsInstance(path_items
[1], bt2
._CurrentOptionContentFieldPathItem
)
466 self
.assertIsInstance(path_items
[2], bt2
._IndexFieldPathItem
)
467 self
.assertEqual(path_items
[2].index
, 2)
469 def test_field_path_root_scope(self
):
470 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
472 fc
.selector_field_path
.root_scope
, bt2
.FieldPathScope
.PACKET_CONTEXT
475 def test_create_invalid_field_class(self
):
476 with self
.assertRaises(TypeError):
477 self
._tc
.create_option_field_class(object())
479 def test_create_invalid_selector_type(self
):
480 with self
.assertRaises(TypeError):
481 self
._tc
.create_option_field_class(self
._content
_fc
, 17)
484 class VariantFieldClassWithoutSelectorTestCase(
485 _TestElementContainer
, unittest
.TestCase
487 _append_element_method
= staticmethod(
488 bt2
._VariantFieldClassWithoutSelector
.append_option
490 _at_index_method
= staticmethod(
491 bt2
._VariantFieldClassWithoutSelector
.option_at_index
494 def _create_default_fc(self
):
495 return self
._tc
.create_variant_field_class()
498 class _VariantFieldClassWithSelectorTestCase
:
500 self
._tc
= get_default_trace_class()
502 self
._fc
= self
._create
_default
_fc
()
504 def _create_default_fc(self
):
505 return self
._tc
.create_variant_field_class(self
._selector
_fc
)
507 def test_create_default(self
):
508 self
.assertIsNotNone(self
._fc
)
510 def test_append_element(self
):
511 str_field_class
= self
._tc
.create_string_field_class()
512 self
._fc
.append_option('str', str_field_class
, self
._ranges
1)
513 opt
= self
._fc
['str']
514 self
.assertEqual(opt
.field_class
.addr
, str_field_class
.addr
)
515 self
.assertEqual(opt
.name
, 'str')
516 self
.assertEqual(opt
.ranges
.addr
, self
._ranges
1.addr
)
518 def test_append_element_kwargs(self
):
519 int_field_class
= self
._tc
.create_signed_integer_field_class(32)
520 self
._fc
.append_option(
521 name
='int32', field_class
=int_field_class
, ranges
=self
._ranges
1
523 opt
= self
._fc
['int32']
524 self
.assertEqual(opt
.field_class
.addr
, int_field_class
.addr
)
525 self
.assertEqual(opt
.name
, 'int32')
526 self
.assertEqual(opt
.ranges
.addr
, self
._ranges
1.addr
)
528 def test_append_element_invalid_name(self
):
529 sub_fc
= self
._tc
.create_string_field_class()
531 with self
.assertRaises(TypeError):
532 self
._fc
.append_option(self
._fc
, 23, sub_fc
)
534 def test_append_element_invalid_field_class(self
):
535 with self
.assertRaises(TypeError):
536 self
._fc
.append_option(self
._fc
, 'yes', object())
538 def test_append_element_invalid_ranges(self
):
539 sub_fc
= self
._tc
.create_string_field_class()
541 with self
.assertRaises(TypeError):
542 self
._fc
.append_option(self
._fc
, sub_fc
, 'lel')
544 def test_append_element_dup_name(self
):
545 sub_fc1
= self
._tc
.create_string_field_class()
546 sub_fc2
= self
._tc
.create_string_field_class()
548 with self
.assertRaises(ValueError):
549 self
._fc
.append_option('yes', sub_fc1
, self
._ranges
1)
550 self
._fc
.append_option('yes', sub_fc2
, self
._ranges
2)
552 def test_append_element_invalid_ranges_signedness(self
):
553 sub_fc
= self
._tc
.create_string_field_class()
555 with self
.assertRaises(TypeError):
556 self
._fc
.append_option(self
._fc
, sub_fc
, self
._inval
_ranges
)
559 other_fc
= self
._create
_default
_fc
()
560 a_field_class
= self
._tc
.create_real_field_class()
561 self
._fc
.append_option('a_float', a_field_class
, self
._ranges
1)
562 c_field_class
= self
._tc
.create_string_field_class()
563 d_field_class
= self
._tc
.create_signed_enumeration_field_class(
567 ('c_string', c_field_class
, self
._ranges
2),
568 ('d_enum', d_field_class
, self
._ranges
3),
570 self
.assertEqual(self
._fc
['a_float'].field_class
.addr
, a_field_class
.addr
)
571 self
.assertEqual(self
._fc
['a_float'].name
, 'a_float')
572 self
.assertEqual(self
._fc
['a_float'].ranges
, self
._ranges
1)
573 self
.assertEqual(self
._fc
['c_string'].field_class
.addr
, c_field_class
.addr
)
574 self
.assertEqual(self
._fc
['c_string'].name
, 'c_string')
575 self
.assertEqual(self
._fc
['c_string'].ranges
, self
._ranges
2)
576 self
.assertEqual(self
._fc
['d_enum'].field_class
.addr
, d_field_class
.addr
)
577 self
.assertEqual(self
._fc
['d_enum'].name
, 'd_enum')
578 self
.assertEqual(self
._fc
['d_enum'].ranges
, self
._ranges
3)
580 def test_bool_op(self
):
581 self
.assertFalse(self
._fc
)
582 self
._fc
.append_option('a', self
._tc
.create_string_field_class(), self
._ranges
1)
583 self
.assertTrue(self
._fc
)
586 self
._fc
.append_option('a', self
._tc
.create_string_field_class(), self
._ranges
1)
587 self
._fc
.append_option('b', self
._tc
.create_string_field_class(), self
._ranges
2)
588 self
._fc
.append_option('c', self
._tc
.create_string_field_class(), self
._ranges
3)
589 self
.assertEqual(len(self
._fc
), 3)
591 def test_getitem(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 self
._fc
.append_option('a', a_fc
, self
._ranges
1)
596 self
._fc
.append_option('b', b_fc
, self
._ranges
2)
597 self
._fc
.append_option('c', c_fc
, self
._ranges
3)
598 self
.assertEqual(self
._fc
['b'].field_class
.addr
, b_fc
.addr
)
599 self
.assertEqual(self
._fc
['b'].name
, 'b')
600 self
.assertEqual(self
._fc
['b'].ranges
.addr
, self
._ranges
2.addr
)
602 def test_getitem_invalid_key_type(self
):
603 with self
.assertRaises(TypeError):
606 def test_getitem_invalid_key(self
):
607 with self
.assertRaises(KeyError):
610 def test_contains(self
):
611 self
.assertFalse('a' in self
._fc
)
612 self
._fc
.append_option('a', self
._tc
.create_string_field_class(), self
._ranges
1)
613 self
.assertTrue('a' in self
._fc
)
616 a_fc
= self
._tc
.create_signed_integer_field_class(32)
617 b_fc
= self
._tc
.create_string_field_class()
618 c_fc
= self
._tc
.create_real_field_class()
620 ('a', a_fc
, self
._ranges
1),
621 ('b', b_fc
, self
._ranges
2),
622 ('c', c_fc
, self
._ranges
3),
626 self
._fc
.append_option(*opt
)
628 for (name
, opt
), test_opt
in zip(self
._fc
.items(), opts
):
629 self
.assertEqual(opt
.name
, test_opt
[0])
630 self
.assertEqual(name
, opt
.name
)
631 self
.assertEqual(opt
.field_class
.addr
, test_opt
[1].addr
)
632 self
.assertEqual(opt
.ranges
.addr
, test_opt
[2].addr
)
634 def test_at_index(self
):
635 a_fc
= self
._tc
.create_signed_integer_field_class(32)
636 b_fc
= self
._tc
.create_string_field_class()
637 c_fc
= self
._tc
.create_real_field_class()
638 self
._fc
.append_option('c', c_fc
, self
._ranges
1)
639 self
._fc
.append_option('a', a_fc
, self
._ranges
2)
640 self
._fc
.append_option('b', b_fc
, self
._ranges
3)
641 self
.assertEqual(self
._fc
.option_at_index(1).field_class
.addr
, a_fc
.addr
)
642 self
.assertEqual(self
._fc
.option_at_index(1).name
, 'a')
643 self
.assertEqual(self
._fc
.option_at_index(1).ranges
.addr
, self
._ranges
2.addr
)
645 def test_at_index_invalid(self
):
646 self
._fc
.append_option(
647 'c', self
._tc
.create_signed_integer_field_class(32), self
._ranges
3
650 with self
.assertRaises(TypeError):
651 self
._fc
.option_at_index('yes')
653 def test_at_index_out_of_bounds_after(self
):
654 self
._fc
.append_option(
655 'c', self
._tc
.create_signed_integer_field_class(32), self
._ranges
3
658 with self
.assertRaises(IndexError):
659 self
._fc
.option_at_index(len(self
._fc
))
661 def _fill_default_fc_for_field_path_test(self
):
662 # Create something equivalent to:
664 # struct outer_struct_fc {
666 # struct inner_struct_fc {
667 # [u]int64_t selector;
670 # variant <selector> {
671 # real a; // selected with self._ranges1
672 # int21_t b; // selected with self._ranges2
673 # uint34_t c; // selected with self._ranges3
677 self
._fc
.append_option('a', self
._tc
.create_real_field_class(), self
._ranges
1)
678 self
._fc
.append_option(
679 'b', self
._tc
.create_signed_integer_field_class(21), self
._ranges
2
681 self
._fc
.append_option(
682 'c', self
._tc
.create_unsigned_integer_field_class(34), self
._ranges
3
685 foo_fc
= self
._tc
.create_real_field_class()
686 bar_fc
= self
._tc
.create_string_field_class()
687 baz_fc
= self
._tc
.create_string_field_class()
689 inner_struct_fc
= self
._tc
.create_structure_field_class()
690 inner_struct_fc
.append_member('selector', self
._selector
_fc
)
691 inner_struct_fc
.append_member('bar', bar_fc
)
692 inner_struct_fc
.append_member('baz', baz_fc
)
693 inner_struct_fc
.append_member('variant', self
._fc
)
695 inner_struct_array_fc
= self
._tc
.create_static_array_field_class(
699 outer_struct_fc
= self
._tc
.create_structure_field_class()
700 outer_struct_fc
.append_member('foo', foo_fc
)
701 outer_struct_fc
.append_member('inner_struct', inner_struct_array_fc
)
703 # The path to the selector field is resolved when the sequence is
704 # actually used, for example in a packet context.
705 self
._tc
.create_stream_class(
706 supports_packets
=True, packet_context_field_class
=outer_struct_fc
709 def test_selector_field_path_length(self
):
710 self
._fill
_default
_fc
_for
_field
_path
_test
()
711 self
.assertEqual(len(self
._fc
.selector_field_path
), 3)
713 def test_selector_field_path_iter(self
):
714 self
._fill
_default
_fc
_for
_field
_path
_test
()
715 path_items
= list(self
._fc
.selector_field_path
)
717 self
.assertEqual(len(path_items
), 3)
719 self
.assertIsInstance(path_items
[0], bt2
._IndexFieldPathItem
)
720 self
.assertEqual(path_items
[0].index
, 1)
722 self
.assertIsInstance(path_items
[1], bt2
._CurrentArrayElementFieldPathItem
)
724 self
.assertIsInstance(path_items
[2], bt2
._IndexFieldPathItem
)
725 self
.assertEqual(path_items
[2].index
, 0)
727 def test_selector_field_path_root_scope(self
):
728 self
._fill
_default
_fc
_for
_field
_path
_test
()
730 self
._fc
.selector_field_path
.root_scope
, bt2
.FieldPathScope
.PACKET_CONTEXT
734 class VariantFieldClassWithUnsignedSelectorTestCase(
735 _VariantFieldClassWithSelectorTestCase
, unittest
.TestCase
737 def _spec_set_up(self
):
738 self
._ranges
1 = bt2
.UnsignedIntegerRangeSet([(1, 4), (18, 47)])
739 self
._ranges
2 = bt2
.UnsignedIntegerRangeSet([(5, 5)])
740 self
._ranges
3 = bt2
.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
741 self
._inval
_ranges
= bt2
.SignedIntegerRangeSet([(-8, 16), (48, 99)])
742 self
._selector
_fc
= self
._tc
.create_unsigned_integer_field_class()
745 class VariantFieldClassWithSignedSelectorTestCase(
746 _VariantFieldClassWithSelectorTestCase
, unittest
.TestCase
748 def _spec_set_up(self
):
749 self
._ranges
1 = bt2
.SignedIntegerRangeSet([(-10, -4), (18, 47)])
750 self
._ranges
2 = bt2
.SignedIntegerRangeSet([(-3, -3)])
751 self
._ranges
3 = bt2
.SignedIntegerRangeSet([(8, 16), (48, 99)])
752 self
._inval
_ranges
= bt2
.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
753 self
._selector
_fc
= self
._tc
.create_signed_integer_field_class()
756 class StaticArrayFieldClassTestCase(unittest
.TestCase
):
758 self
._tc
= get_default_trace_class()
759 self
._elem
_fc
= self
._tc
.create_signed_integer_field_class(23)
761 def test_create_default(self
):
762 fc
= self
._tc
.create_static_array_field_class(self
._elem
_fc
, 45)
763 self
.assertEqual(fc
.element_field_class
.addr
, self
._elem
_fc
.addr
)
764 self
.assertEqual(fc
.length
, 45)
766 def test_create_invalid_elem_field_class(self
):
767 with self
.assertRaises(TypeError):
768 self
._tc
.create_static_array_field_class(object(), 45)
770 def test_create_invalid_length(self
):
771 with self
.assertRaises(ValueError):
772 self
._tc
.create_static_array_field_class(
773 self
._tc
.create_string_field_class(), -17
776 def test_create_invalid_length_type(self
):
777 with self
.assertRaises(TypeError):
778 self
._tc
.create_static_array_field_class(
779 self
._tc
.create_string_field_class(), 'the length'
783 class DynamicArrayFieldClassTestCase(unittest
.TestCase
):
785 self
._tc
= get_default_trace_class()
786 self
._elem
_fc
= self
._tc
.create_signed_integer_field_class(23)
787 self
._len
_fc
= self
._tc
.create_unsigned_integer_field_class(12)
789 def test_create_default(self
):
790 fc
= self
._tc
.create_dynamic_array_field_class(self
._elem
_fc
)
791 self
.assertEqual(fc
.element_field_class
.addr
, self
._elem
_fc
.addr
)
792 self
.assertIsNone(fc
.length_field_path
, None)
794 def _create_field_class_for_field_path_test(self
):
795 # Create something a field class that is equivalent to:
797 # struct outer_struct_fc {
799 # struct inner_struct_fc {
803 # uint23_t dyn_array[len];
807 fc
= self
._tc
.create_dynamic_array_field_class(self
._elem
_fc
, self
._len
_fc
)
809 foo_fc
= self
._tc
.create_real_field_class()
810 bar_fc
= self
._tc
.create_string_field_class()
811 baz_fc
= self
._tc
.create_string_field_class()
813 inner_struct_fc
= self
._tc
.create_structure_field_class()
814 inner_struct_fc
.append_member('bar', bar_fc
)
815 inner_struct_fc
.append_member('baz', baz_fc
)
816 inner_struct_fc
.append_member('len', self
._len
_fc
)
817 inner_struct_fc
.append_member('dyn_array', fc
)
819 inner_struct_array_fc
= self
._tc
.create_static_array_field_class(
823 outer_struct_fc
= self
._tc
.create_structure_field_class()
824 outer_struct_fc
.append_member('foo', foo_fc
)
825 outer_struct_fc
.append_member('inner_struct', inner_struct_array_fc
)
827 # The path to the length field is resolved when the sequence is
828 # actually used, for example in a packet context.
829 self
._tc
.create_stream_class(
830 packet_context_field_class
=outer_struct_fc
, supports_packets
=True
835 def test_field_path_len(self
):
836 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
837 self
.assertEqual(len(fc
.length_field_path
), 3)
839 def test_field_path_iter(self
):
840 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
841 path_items
= list(fc
.length_field_path
)
843 self
.assertEqual(len(path_items
), 3)
845 self
.assertIsInstance(path_items
[0], bt2
._IndexFieldPathItem
)
846 self
.assertEqual(path_items
[0].index
, 1)
848 self
.assertIsInstance(path_items
[1], bt2
._CurrentArrayElementFieldPathItem
)
850 self
.assertIsInstance(path_items
[2], bt2
._IndexFieldPathItem
)
851 self
.assertEqual(path_items
[2].index
, 2)
853 def test_field_path_root_scope(self
):
854 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
856 fc
.length_field_path
.root_scope
, bt2
.FieldPathScope
.PACKET_CONTEXT
859 def test_create_invalid_field_class(self
):
860 with self
.assertRaises(TypeError):
861 self
._tc
.create_dynamic_array_field_class(object())
863 def test_create_invalid_length_type(self
):
864 with self
.assertRaises(TypeError):
865 self
._tc
.create_dynamic_array_field_class(
866 self
._tc
.create_string_field_class(), 17
870 if __name__
== "__main__":