|
1
|
|
|
#!/usr/bin/env python3 |
|
2
|
|
|
|
|
3
|
|
|
"""Tests for the elementenum class""" |
|
4
|
|
|
|
|
5
|
|
|
import unittest |
|
6
|
|
|
|
|
7
|
|
|
import enum |
|
8
|
|
|
from starstruct.bitfield import BitField |
|
9
|
|
|
from starstruct.elementbitfield import ElementBitField |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class SimpleEnum(enum.Enum): |
|
13
|
|
|
"""Simple enum class for testing message pack/unpack""" |
|
14
|
|
|
one = 1 |
|
15
|
|
|
two = 2 |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
# pylint: disable=blacklisted-name |
|
19
|
|
|
class StrEnum(enum.Enum): |
|
20
|
|
|
"""string based enum class for testing message pack/unpack""" |
|
21
|
|
|
foo = 'foo' |
|
22
|
|
|
bar = 'bar' |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
# pylint: disable=line-too-long,invalid-name,no-self-use |
|
26
|
|
|
class TestElementBitField(unittest.TestCase): |
|
27
|
|
|
"""ElementBitField module tests""" |
|
28
|
|
|
|
|
29
|
|
|
def test_invalid_enum(self): |
|
30
|
|
|
"""Test field formats that are valid ElementBitField elements.""" |
|
31
|
|
|
# pylint: disable=unused-variable |
|
32
|
|
|
|
|
33
|
|
|
with self.assertRaises(TypeError) as cm: |
|
34
|
|
|
test_bitfield = BitField(StrEnum) # noqa: F841 |
|
35
|
|
|
msg = 'Enum {} members must have integer values'.format(repr(StrEnum)) |
|
36
|
|
|
self.assertEqual(str(cm.exception), msg) |
|
37
|
|
|
|
|
38
|
|
|
def test_not_valid(self): |
|
39
|
|
|
"""Test field formats that are not valid ElementEnum elements.""" |
|
40
|
|
|
|
|
41
|
|
|
test_bitfield = BitField(SimpleEnum) |
|
42
|
|
|
|
|
43
|
|
|
test_fields = [ |
|
44
|
|
|
('a', 'b', SimpleEnum), # enum field |
|
45
|
|
|
('a', 'b', StrEnum), # enum field |
|
46
|
|
|
('a', '4x', test_bitfield), # 4 pad bytes |
|
47
|
|
|
('b', 'z', test_bitfield), # invalid |
|
48
|
|
|
('c', '1', test_bitfield), # invalid |
|
49
|
|
|
('e', '9s', test_bitfield), # invalid (no strings allowed) |
|
50
|
|
|
('d', '/', test_bitfield), # invalid |
|
51
|
|
|
('f', 'H'), # unsigned short (no class) |
|
52
|
|
|
] |
|
53
|
|
|
|
|
54
|
|
|
for field in test_fields: |
|
55
|
|
|
with self.subTest(field): # pylint: disable=no-member |
|
56
|
|
|
out = ElementBitField.valid(field) |
|
57
|
|
|
self.assertFalse(out) |
|
58
|
|
|
|
|
59
|
|
|
def test_valid_pack(self): |
|
60
|
|
|
"""Test packing valid enum values.""" |
|
61
|
|
|
|
|
62
|
|
|
test_bitfield = BitField(SimpleEnum) |
|
63
|
|
|
|
|
64
|
|
|
field = ('a', 'b', test_bitfield) # signed byte: -128, 127 |
|
65
|
|
|
out = ElementBitField.valid(field) |
|
66
|
|
|
self.assertTrue(out) |
|
67
|
|
|
|
|
68
|
|
|
elem = ElementBitField(field) |
|
69
|
|
|
test_values = [ |
|
70
|
|
|
({'a': 2}, b'\x02'), |
|
71
|
|
|
({'a': []}, b'\x00'), |
|
72
|
|
|
({'a': [SimpleEnum.one]}, b'\x01'), |
|
73
|
|
|
({'a': [SimpleEnum.two]}, b'\x02'), |
|
74
|
|
|
({'a': [SimpleEnum.one, SimpleEnum.two]}, b'\x03'), |
|
75
|
|
|
({'a': [1, SimpleEnum.two]}, b'\x03'), |
|
76
|
|
|
] |
|
77
|
|
|
for (in_val, out_val) in test_values: |
|
78
|
|
|
with self.subTest((out_val, in_val)): # pylint: disable=no-member |
|
79
|
|
|
ret = elem.pack(in_val) |
|
80
|
|
|
self.assertEqual(ret, out_val) |
|
81
|
|
|
|
|
82
|
|
|
def test_out_of_range_values_pack(self): |
|
83
|
|
|
"""Test packing invalid enum values.""" |
|
84
|
|
|
|
|
85
|
|
|
test_bitfield = BitField(SimpleEnum) |
|
86
|
|
|
|
|
87
|
|
|
field = ('a', 'b', test_bitfield) # signed byte: -128, 127 |
|
88
|
|
|
out = ElementBitField.valid(field) |
|
89
|
|
|
self.assertTrue(out) |
|
90
|
|
|
|
|
91
|
|
|
elem = ElementBitField(field) |
|
92
|
|
|
test_values = [ |
|
93
|
|
|
({'a': 0}, 0), |
|
94
|
|
|
({'a': -1}, -1), |
|
95
|
|
|
({'a': [0, SimpleEnum.one]}, 0), |
|
96
|
|
|
({'a': [SimpleEnum.one, -1]}, -1), |
|
97
|
|
|
({'a': [SimpleEnum.two, 3]}, 3), |
|
98
|
|
|
] |
|
99
|
|
|
|
|
100
|
|
|
msg = '{} is not a valid {}' |
|
101
|
|
|
for (in_val, bad_val) in test_values: |
|
102
|
|
|
with self.subTest((in_val, bad_val)): # pylint: disable=no-member |
|
103
|
|
|
with self.assertRaises(ValueError) as cm: |
|
104
|
|
|
elem.pack(in_val) |
|
105
|
|
|
self.assertEqual(str(cm.exception), msg.format(bad_val, 'SimpleEnum')) |
|
106
|
|
|
|
|
107
|
|
|
def test_unpack(self): |
|
108
|
|
|
"""Test unpacking valid enum values.""" |
|
109
|
|
|
|
|
110
|
|
|
test_bitfield = BitField(SimpleEnum) |
|
111
|
|
|
|
|
112
|
|
|
field = ('a', 'b', test_bitfield) # signed byte: -128, 127 |
|
113
|
|
|
out = ElementBitField.valid(field) |
|
114
|
|
|
self.assertTrue(out) |
|
115
|
|
|
elem = ElementBitField(field) |
|
116
|
|
|
test_values = [ |
|
117
|
|
|
(b'\x00', frozenset([])), |
|
118
|
|
|
(b'\xFC', frozenset([])), |
|
119
|
|
|
(b'\x01', frozenset([SimpleEnum.one])), |
|
120
|
|
|
(b'\x02', frozenset([SimpleEnum.two])), |
|
121
|
|
|
(b'\x03', frozenset([SimpleEnum.one, SimpleEnum.two])), |
|
122
|
|
|
(b'\xF3', frozenset([SimpleEnum.one, SimpleEnum.two])), |
|
123
|
|
|
(b'\xAA', frozenset([SimpleEnum.two])), |
|
124
|
|
|
] |
|
125
|
|
|
for (in_val, out_val) in test_values: |
|
126
|
|
|
with self.subTest((in_val, out_val)): # pylint: disable=no-member |
|
127
|
|
|
(ret, unused) = elem.unpack({}, in_val) |
|
128
|
|
|
self.assertEqual(unused, b'') |
|
129
|
|
|
self.assertEqual(ret, out_val) |
|
130
|
|
|
|