| 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 | |
| 19 | import unittest |
| 20 | import bt2 |
| 21 | from utils import get_default_trace_class |
| 22 | |
| 23 | |
| 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 | |
| 33 | class BitArrayFieldClassTestCase(unittest.TestCase): |
| 34 | def setUp(self): |
| 35 | self._tc = get_default_trace_class() |
| 36 | self._fc = self._tc.create_bit_array_field_class(17) |
| 37 | |
| 38 | def test_create_default(self): |
| 39 | self.assertIsNotNone(self._fc) |
| 40 | |
| 41 | def test_create_length_out_of_range(self): |
| 42 | with self.assertRaises(ValueError): |
| 43 | self._tc.create_bit_array_field_class(65) |
| 44 | |
| 45 | def test_create_length_zero(self): |
| 46 | with self.assertRaises(ValueError): |
| 47 | self._tc.create_bit_array_field_class(0) |
| 48 | |
| 49 | def test_create_length_invalid_type(self): |
| 50 | with self.assertRaises(TypeError): |
| 51 | self._tc.create_bit_array_field_class('lel') |
| 52 | |
| 53 | def test_length_prop(self): |
| 54 | self.assertEqual(self._fc.length, 17) |
| 55 | |
| 56 | |
| 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) |
| 62 | |
| 63 | def test_create_range(self): |
| 64 | fc = self._create_func(field_value_range=35) |
| 65 | self.assertEqual(fc.field_value_range, 35) |
| 66 | |
| 67 | fc = self._create_func(36) |
| 68 | self.assertEqual(fc.field_value_range, 36) |
| 69 | |
| 70 | def test_create_invalid_range(self): |
| 71 | with self.assertRaises(TypeError): |
| 72 | self._create_func('yes') |
| 73 | |
| 74 | with self.assertRaises(TypeError): |
| 75 | self._create_func(field_value_range='yes') |
| 76 | |
| 77 | with self.assertRaises(ValueError): |
| 78 | self._create_func(field_value_range=-2) |
| 79 | |
| 80 | with self.assertRaises(ValueError): |
| 81 | self._create_func(field_value_range=0) |
| 82 | |
| 83 | def test_create_base(self): |
| 84 | fc = self._create_func( |
| 85 | preferred_display_base=bt2.IntegerDisplayBase.HEXADECIMAL |
| 86 | ) |
| 87 | self.assertEqual(fc.preferred_display_base, bt2.IntegerDisplayBase.HEXADECIMAL) |
| 88 | |
| 89 | def test_create_invalid_base_type(self): |
| 90 | with self.assertRaises(TypeError): |
| 91 | self._create_func(preferred_display_base='yes') |
| 92 | |
| 93 | def test_create_invalid_base_value(self): |
| 94 | with self.assertRaises(ValueError): |
| 95 | self._create_func(preferred_display_base=444) |
| 96 | |
| 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) |
| 101 | |
| 102 | |
| 103 | class IntegerFieldClassTestCase(_TestIntegerFieldClassProps, unittest.TestCase): |
| 104 | def setUp(self): |
| 105 | self._tc = get_default_trace_class() |
| 106 | self._create_func = self._tc.create_signed_integer_field_class |
| 107 | |
| 108 | |
| 109 | class RealFieldClassTestCase(unittest.TestCase): |
| 110 | def setUp(self): |
| 111 | self._tc = get_default_trace_class() |
| 112 | |
| 113 | def test_create_default(self): |
| 114 | fc = self._tc.create_real_field_class() |
| 115 | self.assertFalse(fc.is_single_precision) |
| 116 | |
| 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) |
| 120 | |
| 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') |
| 124 | |
| 125 | |
| 126 | # Converts an _EnumerationFieldClassMapping to a list of ranges: |
| 127 | # |
| 128 | # [(lower0, upper0), (lower1, upper1), ...] |
| 129 | |
| 130 | |
| 131 | def enum_mapping_to_set(mapping): |
| 132 | return {(x.lower, x.upper) for x in mapping.ranges} |
| 133 | |
| 134 | |
| 135 | class _EnumerationFieldClassTestCase(_TestIntegerFieldClassProps): |
| 136 | def setUp(self): |
| 137 | self._tc = get_default_trace_class() |
| 138 | self._spec_set_up() |
| 139 | self._fc = self._create_func() |
| 140 | |
| 141 | def test_create_from_invalid_type(self): |
| 142 | with self.assertRaises(TypeError): |
| 143 | self._create_func('coucou') |
| 144 | |
| 145 | def test_add_mapping_simple(self): |
| 146 | self._fc.add_mapping('hello', self._ranges1) |
| 147 | mapping = self._fc['hello'] |
| 148 | self.assertEqual(mapping.label, 'hello') |
| 149 | self.assertEqual(mapping.ranges, self._ranges1) |
| 150 | |
| 151 | def test_add_mapping_simple_kwargs(self): |
| 152 | self._fc.add_mapping(label='hello', ranges=self._ranges1) |
| 153 | mapping = self._fc['hello'] |
| 154 | self.assertEqual(mapping.label, 'hello') |
| 155 | self.assertEqual(mapping.ranges, self._ranges1) |
| 156 | |
| 157 | def test_add_mapping_invalid_name(self): |
| 158 | with self.assertRaises(TypeError): |
| 159 | self._fc.add_mapping(17, self._ranges1) |
| 160 | |
| 161 | def test_add_mapping_invalid_range(self): |
| 162 | with self.assertRaises(TypeError): |
| 163 | self._fc.add_mapping('allo', 'meow') |
| 164 | |
| 165 | def test_add_mapping_dup_label(self): |
| 166 | with self.assertRaises(ValueError): |
| 167 | self._fc.add_mapping('a', self._ranges1) |
| 168 | self._fc.add_mapping('a', self._ranges2) |
| 169 | |
| 170 | def test_add_mapping_invalid_ranges_signedness(self): |
| 171 | with self.assertRaises(TypeError): |
| 172 | self._fc.add_mapping('allo', self._inval_ranges) |
| 173 | |
| 174 | def test_iadd(self): |
| 175 | self._fc.add_mapping('c', self._ranges1) |
| 176 | |
| 177 | self._fc += [('d', self._ranges2), ('e', self._ranges3)] |
| 178 | |
| 179 | self.assertEqual(len(self._fc), 3) |
| 180 | self.assertEqual(self._fc['c'].label, 'c') |
| 181 | self.assertEqual(self._fc['c'].ranges, self._ranges1) |
| 182 | self.assertEqual(self._fc['d'].label, 'd') |
| 183 | self.assertEqual(self._fc['d'].ranges, self._ranges2) |
| 184 | self.assertEqual(self._fc['e'].label, 'e') |
| 185 | self.assertEqual(self._fc['e'].ranges, self._ranges3) |
| 186 | |
| 187 | def test_bool_op(self): |
| 188 | self.assertFalse(self._fc) |
| 189 | self._fc.add_mapping('a', self._ranges1) |
| 190 | self.assertTrue(self._fc) |
| 191 | |
| 192 | def test_len(self): |
| 193 | self._fc.add_mapping('a', self._ranges1) |
| 194 | self._fc.add_mapping('b', self._ranges2) |
| 195 | self._fc.add_mapping('c', self._ranges3) |
| 196 | self.assertEqual(len(self._fc), 3) |
| 197 | |
| 198 | def test_getitem(self): |
| 199 | self._fc.add_mapping('a', self._ranges1) |
| 200 | self._fc.add_mapping('b', self._ranges2) |
| 201 | self._fc.add_mapping('c', self._ranges3) |
| 202 | mapping = self._fc['a'] |
| 203 | self.assertEqual(mapping.label, 'a') |
| 204 | self.assertEqual(mapping.ranges, self._ranges1) |
| 205 | |
| 206 | def test_getitem_nonexistent(self): |
| 207 | with self.assertRaises(KeyError): |
| 208 | self._fc['doesnotexist'] |
| 209 | |
| 210 | def test_iter(self): |
| 211 | self._fc.add_mapping('a', self._ranges1) |
| 212 | self._fc.add_mapping('b', self._ranges2) |
| 213 | self._fc.add_mapping('c', self._ranges3) |
| 214 | |
| 215 | # This exercises iteration. |
| 216 | labels = sorted(self._fc) |
| 217 | |
| 218 | self.assertEqual(labels, ['a', 'b', 'c']) |
| 219 | |
| 220 | def test_find_by_value(self): |
| 221 | self._fc.add_mapping('a', self._ranges1) |
| 222 | self._fc.add_mapping('b', self._ranges2) |
| 223 | self._fc.add_mapping('c', self._ranges3) |
| 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) |
| 228 | |
| 229 | |
| 230 | class UnsignedEnumerationFieldClassTestCase( |
| 231 | _EnumerationFieldClassTestCase, unittest.TestCase |
| 232 | ): |
| 233 | def _spec_set_up(self): |
| 234 | self._ranges1 = bt2.UnsignedIntegerRangeSet([(1, 4), (18, 47)]) |
| 235 | self._ranges2 = bt2.UnsignedIntegerRangeSet([(5, 5)]) |
| 236 | self._ranges3 = 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 |
| 240 | |
| 241 | |
| 242 | class SignedEnumerationFieldClassTestCase( |
| 243 | _EnumerationFieldClassTestCase, unittest.TestCase |
| 244 | ): |
| 245 | def _spec_set_up(self): |
| 246 | self._ranges1 = bt2.SignedIntegerRangeSet([(-10, -4), (18, 47)]) |
| 247 | self._ranges2 = bt2.SignedIntegerRangeSet([(-3, -3)]) |
| 248 | self._ranges3 = 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 |
| 252 | |
| 253 | |
| 254 | class StringFieldClassTestCase(unittest.TestCase): |
| 255 | def setUp(self): |
| 256 | tc = get_default_trace_class() |
| 257 | self._fc = tc.create_string_field_class() |
| 258 | |
| 259 | def test_create_default(self): |
| 260 | self.assertIsNotNone(self._fc) |
| 261 | |
| 262 | |
| 263 | class _TestElementContainer: |
| 264 | def setUp(self): |
| 265 | self._tc = get_default_trace_class() |
| 266 | self._fc = self._create_default_fc() |
| 267 | |
| 268 | def test_create_default(self): |
| 269 | self.assertIsNotNone(self._fc) |
| 270 | |
| 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) |
| 276 | |
| 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) |
| 282 | |
| 283 | def test_append_element_invalid_name(self): |
| 284 | sub_fc = self._tc.create_string_field_class() |
| 285 | |
| 286 | with self.assertRaises(TypeError): |
| 287 | self._append_element_method(self._fc, 23, sub_fc) |
| 288 | |
| 289 | def test_append_element_invalid_field_class(self): |
| 290 | with self.assertRaises(TypeError): |
| 291 | self._append_element_method(self._fc, 'yes', object()) |
| 292 | |
| 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() |
| 296 | |
| 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) |
| 300 | |
| 301 | def test_iadd(self): |
| 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( |
| 309 | field_value_range=32 |
| 310 | ) |
| 311 | e_field_class = self._tc.create_structure_field_class() |
| 312 | self._fc += [ |
| 313 | ('c_string', c_field_class), |
| 314 | ('d_enum', d_field_class), |
| 315 | ('e_struct', e_field_class), |
| 316 | ] |
| 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') |
| 327 | |
| 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) |
| 332 | |
| 333 | def test_len(self): |
| 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) |
| 338 | |
| 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') |
| 348 | |
| 349 | def test_getitem_invalid_key_type(self): |
| 350 | with self.assertRaises(TypeError): |
| 351 | self._fc[0] |
| 352 | |
| 353 | def test_getitem_invalid_key(self): |
| 354 | with self.assertRaises(KeyError): |
| 355 | self._fc['no way'] |
| 356 | |
| 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) |
| 361 | |
| 362 | def test_iter(self): |
| 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)) |
| 367 | |
| 368 | for elem in elements: |
| 369 | self._append_element_method(self._fc, *elem) |
| 370 | |
| 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) |
| 375 | |
| 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') |
| 386 | |
| 387 | def test_at_index_invalid(self): |
| 388 | self._append_element_method( |
| 389 | self._fc, 'c', self._tc.create_signed_integer_field_class(32) |
| 390 | ) |
| 391 | |
| 392 | with self.assertRaises(TypeError): |
| 393 | self._at_index_method(self._fc, 'yes') |
| 394 | |
| 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) |
| 398 | ) |
| 399 | |
| 400 | with self.assertRaises(IndexError): |
| 401 | self._at_index_method(self._fc, len(self._fc)) |
| 402 | |
| 403 | |
| 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) |
| 407 | |
| 408 | def _create_default_fc(self): |
| 409 | return self._tc.create_structure_field_class() |
| 410 | |
| 411 | |
| 412 | class OptionFieldClassTestCase(unittest.TestCase): |
| 413 | def setUp(self): |
| 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() |
| 417 | |
| 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) |
| 422 | |
| 423 | def _create_field_class_for_field_path_test(self): |
| 424 | fc = self._tc.create_option_field_class(self._content_fc, self._tag_fc) |
| 425 | |
| 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() |
| 429 | |
| 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) |
| 435 | |
| 436 | opt_struct_array_fc = self._tc.create_option_field_class(inner_struct_fc) |
| 437 | |
| 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) |
| 441 | |
| 442 | # The path to the selector field class is resolved when the |
| 443 | # option field class is actually used, for example in a packet |
| 444 | # context. |
| 445 | self._tc.create_stream_class( |
| 446 | packet_context_field_class=outer_struct_fc, supports_packets=True |
| 447 | ) |
| 448 | |
| 449 | return fc |
| 450 | |
| 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) |
| 454 | |
| 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) |
| 458 | |
| 459 | self.assertEqual(len(path_items), 3) |
| 460 | |
| 461 | self.assertIsInstance(path_items[0], bt2._IndexFieldPathItem) |
| 462 | self.assertEqual(path_items[0].index, 1) |
| 463 | |
| 464 | self.assertIsInstance(path_items[1], bt2._CurrentOptionContentFieldPathItem) |
| 465 | |
| 466 | self.assertIsInstance(path_items[2], bt2._IndexFieldPathItem) |
| 467 | self.assertEqual(path_items[2].index, 2) |
| 468 | |
| 469 | def test_field_path_root_scope(self): |
| 470 | fc = self._create_field_class_for_field_path_test() |
| 471 | self.assertEqual( |
| 472 | fc.selector_field_path.root_scope, bt2.FieldPathScope.PACKET_CONTEXT |
| 473 | ) |
| 474 | |
| 475 | def test_create_invalid_field_class(self): |
| 476 | with self.assertRaises(TypeError): |
| 477 | self._tc.create_option_field_class(object()) |
| 478 | |
| 479 | def test_create_invalid_selector_type(self): |
| 480 | with self.assertRaises(TypeError): |
| 481 | self._tc.create_option_field_class(self._content_fc, 17) |
| 482 | |
| 483 | |
| 484 | class VariantFieldClassWithoutSelectorTestCase( |
| 485 | _TestElementContainer, unittest.TestCase |
| 486 | ): |
| 487 | _append_element_method = staticmethod( |
| 488 | bt2._VariantFieldClassWithoutSelector.append_option |
| 489 | ) |
| 490 | _at_index_method = staticmethod( |
| 491 | bt2._VariantFieldClassWithoutSelector.option_at_index |
| 492 | ) |
| 493 | |
| 494 | def _create_default_fc(self): |
| 495 | return self._tc.create_variant_field_class() |
| 496 | |
| 497 | |
| 498 | class _VariantFieldClassWithSelectorTestCase: |
| 499 | def setUp(self): |
| 500 | self._tc = get_default_trace_class() |
| 501 | self._spec_set_up() |
| 502 | self._fc = self._create_default_fc() |
| 503 | |
| 504 | def _create_default_fc(self): |
| 505 | return self._tc.create_variant_field_class(self._selector_fc) |
| 506 | |
| 507 | def test_create_default(self): |
| 508 | self.assertIsNotNone(self._fc) |
| 509 | |
| 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._ranges1) |
| 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._ranges1.addr) |
| 517 | |
| 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._ranges1 |
| 522 | ) |
| 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._ranges1.addr) |
| 527 | |
| 528 | def test_append_element_invalid_name(self): |
| 529 | sub_fc = self._tc.create_string_field_class() |
| 530 | |
| 531 | with self.assertRaises(TypeError): |
| 532 | self._fc.append_option(self._fc, 23, sub_fc) |
| 533 | |
| 534 | def test_append_element_invalid_field_class(self): |
| 535 | with self.assertRaises(TypeError): |
| 536 | self._fc.append_option(self._fc, 'yes', object()) |
| 537 | |
| 538 | def test_append_element_invalid_ranges(self): |
| 539 | sub_fc = self._tc.create_string_field_class() |
| 540 | |
| 541 | with self.assertRaises(TypeError): |
| 542 | self._fc.append_option(self._fc, sub_fc, 'lel') |
| 543 | |
| 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() |
| 547 | |
| 548 | with self.assertRaises(ValueError): |
| 549 | self._fc.append_option('yes', sub_fc1, self._ranges1) |
| 550 | self._fc.append_option('yes', sub_fc2, self._ranges2) |
| 551 | |
| 552 | def test_append_element_invalid_ranges_signedness(self): |
| 553 | sub_fc = self._tc.create_string_field_class() |
| 554 | |
| 555 | with self.assertRaises(TypeError): |
| 556 | self._fc.append_option(self._fc, sub_fc, self._inval_ranges) |
| 557 | |
| 558 | def test_iadd(self): |
| 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._ranges1) |
| 562 | c_field_class = self._tc.create_string_field_class() |
| 563 | d_field_class = self._tc.create_signed_enumeration_field_class( |
| 564 | field_value_range=32 |
| 565 | ) |
| 566 | self._fc += [ |
| 567 | ('c_string', c_field_class, self._ranges2), |
| 568 | ('d_enum', d_field_class, self._ranges3), |
| 569 | ] |
| 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._ranges1) |
| 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._ranges2) |
| 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._ranges3) |
| 579 | |
| 580 | def test_bool_op(self): |
| 581 | self.assertFalse(self._fc) |
| 582 | self._fc.append_option('a', self._tc.create_string_field_class(), self._ranges1) |
| 583 | self.assertTrue(self._fc) |
| 584 | |
| 585 | def test_len(self): |
| 586 | self._fc.append_option('a', self._tc.create_string_field_class(), self._ranges1) |
| 587 | self._fc.append_option('b', self._tc.create_string_field_class(), self._ranges2) |
| 588 | self._fc.append_option('c', self._tc.create_string_field_class(), self._ranges3) |
| 589 | self.assertEqual(len(self._fc), 3) |
| 590 | |
| 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._ranges1) |
| 596 | self._fc.append_option('b', b_fc, self._ranges2) |
| 597 | self._fc.append_option('c', c_fc, self._ranges3) |
| 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._ranges2.addr) |
| 601 | |
| 602 | def test_getitem_invalid_key_type(self): |
| 603 | with self.assertRaises(TypeError): |
| 604 | self._fc[0] |
| 605 | |
| 606 | def test_getitem_invalid_key(self): |
| 607 | with self.assertRaises(KeyError): |
| 608 | self._fc['no way'] |
| 609 | |
| 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._ranges1) |
| 613 | self.assertTrue('a' in self._fc) |
| 614 | |
| 615 | def test_iter(self): |
| 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() |
| 619 | opts = ( |
| 620 | ('a', a_fc, self._ranges1), |
| 621 | ('b', b_fc, self._ranges2), |
| 622 | ('c', c_fc, self._ranges3), |
| 623 | ) |
| 624 | |
| 625 | for opt in opts: |
| 626 | self._fc.append_option(*opt) |
| 627 | |
| 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) |
| 633 | |
| 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._ranges1) |
| 639 | self._fc.append_option('a', a_fc, self._ranges2) |
| 640 | self._fc.append_option('b', b_fc, self._ranges3) |
| 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._ranges2.addr) |
| 644 | |
| 645 | def test_at_index_invalid(self): |
| 646 | self._fc.append_option( |
| 647 | 'c', self._tc.create_signed_integer_field_class(32), self._ranges3 |
| 648 | ) |
| 649 | |
| 650 | with self.assertRaises(TypeError): |
| 651 | self._fc.option_at_index('yes') |
| 652 | |
| 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._ranges3 |
| 656 | ) |
| 657 | |
| 658 | with self.assertRaises(IndexError): |
| 659 | self._fc.option_at_index(len(self._fc)) |
| 660 | |
| 661 | def _fill_default_fc_for_field_path_test(self): |
| 662 | # Create something equivalent to: |
| 663 | # |
| 664 | # struct outer_struct_fc { |
| 665 | # real foo; |
| 666 | # struct inner_struct_fc { |
| 667 | # [u]int64_t selector; |
| 668 | # string bar; |
| 669 | # string baz; |
| 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 |
| 674 | # } variant; |
| 675 | # } inner_struct[2]; |
| 676 | # }; |
| 677 | self._fc.append_option('a', self._tc.create_real_field_class(), self._ranges1) |
| 678 | self._fc.append_option( |
| 679 | 'b', self._tc.create_signed_integer_field_class(21), self._ranges2 |
| 680 | ) |
| 681 | self._fc.append_option( |
| 682 | 'c', self._tc.create_unsigned_integer_field_class(34), self._ranges3 |
| 683 | ) |
| 684 | |
| 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() |
| 688 | |
| 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) |
| 694 | |
| 695 | inner_struct_array_fc = self._tc.create_static_array_field_class( |
| 696 | inner_struct_fc, 2 |
| 697 | ) |
| 698 | |
| 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) |
| 702 | |
| 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 |
| 707 | ) |
| 708 | |
| 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) |
| 712 | |
| 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) |
| 716 | |
| 717 | self.assertEqual(len(path_items), 3) |
| 718 | |
| 719 | self.assertIsInstance(path_items[0], bt2._IndexFieldPathItem) |
| 720 | self.assertEqual(path_items[0].index, 1) |
| 721 | |
| 722 | self.assertIsInstance(path_items[1], bt2._CurrentArrayElementFieldPathItem) |
| 723 | |
| 724 | self.assertIsInstance(path_items[2], bt2._IndexFieldPathItem) |
| 725 | self.assertEqual(path_items[2].index, 0) |
| 726 | |
| 727 | def test_selector_field_path_root_scope(self): |
| 728 | self._fill_default_fc_for_field_path_test() |
| 729 | self.assertEqual( |
| 730 | self._fc.selector_field_path.root_scope, bt2.FieldPathScope.PACKET_CONTEXT |
| 731 | ) |
| 732 | |
| 733 | |
| 734 | class VariantFieldClassWithUnsignedSelectorTestCase( |
| 735 | _VariantFieldClassWithSelectorTestCase, unittest.TestCase |
| 736 | ): |
| 737 | def _spec_set_up(self): |
| 738 | self._ranges1 = bt2.UnsignedIntegerRangeSet([(1, 4), (18, 47)]) |
| 739 | self._ranges2 = bt2.UnsignedIntegerRangeSet([(5, 5)]) |
| 740 | self._ranges3 = 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() |
| 743 | |
| 744 | |
| 745 | class VariantFieldClassWithSignedSelectorTestCase( |
| 746 | _VariantFieldClassWithSelectorTestCase, unittest.TestCase |
| 747 | ): |
| 748 | def _spec_set_up(self): |
| 749 | self._ranges1 = bt2.SignedIntegerRangeSet([(-10, -4), (18, 47)]) |
| 750 | self._ranges2 = bt2.SignedIntegerRangeSet([(-3, -3)]) |
| 751 | self._ranges3 = 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() |
| 754 | |
| 755 | |
| 756 | class StaticArrayFieldClassTestCase(unittest.TestCase): |
| 757 | def setUp(self): |
| 758 | self._tc = get_default_trace_class() |
| 759 | self._elem_fc = self._tc.create_signed_integer_field_class(23) |
| 760 | |
| 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) |
| 765 | |
| 766 | def test_create_invalid_elem_field_class(self): |
| 767 | with self.assertRaises(TypeError): |
| 768 | self._tc.create_static_array_field_class(object(), 45) |
| 769 | |
| 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 |
| 774 | ) |
| 775 | |
| 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' |
| 780 | ) |
| 781 | |
| 782 | |
| 783 | class DynamicArrayFieldClassTestCase(unittest.TestCase): |
| 784 | def setUp(self): |
| 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) |
| 788 | |
| 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) |
| 793 | |
| 794 | def _create_field_class_for_field_path_test(self): |
| 795 | # Create something a field class that is equivalent to: |
| 796 | # |
| 797 | # struct outer_struct_fc { |
| 798 | # real foo; |
| 799 | # struct inner_struct_fc { |
| 800 | # string bar; |
| 801 | # string baz; |
| 802 | # uint12_t len; |
| 803 | # uint23_t dyn_array[len]; |
| 804 | # } inner_struct[2]; |
| 805 | # }; |
| 806 | |
| 807 | fc = self._tc.create_dynamic_array_field_class(self._elem_fc, self._len_fc) |
| 808 | |
| 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() |
| 812 | |
| 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) |
| 818 | |
| 819 | inner_struct_array_fc = self._tc.create_static_array_field_class( |
| 820 | inner_struct_fc, 2 |
| 821 | ) |
| 822 | |
| 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) |
| 826 | |
| 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 |
| 831 | ) |
| 832 | |
| 833 | return fc |
| 834 | |
| 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) |
| 838 | |
| 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) |
| 842 | |
| 843 | self.assertEqual(len(path_items), 3) |
| 844 | |
| 845 | self.assertIsInstance(path_items[0], bt2._IndexFieldPathItem) |
| 846 | self.assertEqual(path_items[0].index, 1) |
| 847 | |
| 848 | self.assertIsInstance(path_items[1], bt2._CurrentArrayElementFieldPathItem) |
| 849 | |
| 850 | self.assertIsInstance(path_items[2], bt2._IndexFieldPathItem) |
| 851 | self.assertEqual(path_items[2].index, 2) |
| 852 | |
| 853 | def test_field_path_root_scope(self): |
| 854 | fc = self._create_field_class_for_field_path_test() |
| 855 | self.assertEqual( |
| 856 | fc.length_field_path.root_scope, bt2.FieldPathScope.PACKET_CONTEXT |
| 857 | ) |
| 858 | |
| 859 | def test_create_invalid_field_class(self): |
| 860 | with self.assertRaises(TypeError): |
| 861 | self._tc.create_dynamic_array_field_class(object()) |
| 862 | |
| 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 |
| 867 | ) |
| 868 | |
| 869 | |
| 870 | if __name__ == "__main__": |
| 871 | unittest.main() |