|
1
|
|
|
#!/usr/bin/env python3 |
|
2
|
|
|
|
|
3
|
|
|
"""Tests for the elementenum class""" |
|
4
|
|
|
|
|
5
|
|
|
import unittest |
|
6
|
|
|
|
|
7
|
|
|
import enum |
|
8
|
|
|
from starstruct.elementenum import ElementEnum |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
class SimpleEnum(enum.Enum): |
|
12
|
|
|
"""Simple enum class for testing message pack/unpack""" |
|
13
|
|
|
zero = 0 |
|
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 TestElementEnum(unittest.TestCase): |
|
27
|
|
|
"""ElementEnum module tests""" |
|
28
|
|
|
|
|
29
|
|
|
def test_valid(self): |
|
30
|
|
|
"""Test field formats that are valid ElementEnum elements.""" |
|
31
|
|
|
|
|
32
|
|
|
test_fields = [ |
|
33
|
|
|
('a', 'b', SimpleEnum), # signed byte: -128, 127 |
|
34
|
|
|
('b', 'H', SimpleEnum), # unsigned short: 0, 65535 |
|
35
|
|
|
('d', 'L', SimpleEnum), # unsigned long: 0, 2^32-1 |
|
36
|
|
|
('e', '?', SimpleEnum), # bool: 0, 1 |
|
37
|
|
|
('d', '10s', StrEnum), # 10 byte string |
|
38
|
|
|
] |
|
39
|
|
|
|
|
40
|
|
|
for field in test_fields: |
|
41
|
|
|
with self.subTest(field): # pylint: disable=no-member |
|
42
|
|
|
out = ElementEnum.valid(field) |
|
43
|
|
|
self.assertTrue(out) |
|
44
|
|
|
|
|
45
|
|
|
def test_not_valid(self): |
|
46
|
|
|
"""Test field formats that are not valid ElementEnum elements.""" |
|
47
|
|
|
|
|
48
|
|
|
test_fields = [ |
|
49
|
|
|
('a', '4x', SimpleEnum), # 4 pad bytes |
|
50
|
|
|
('b', 'z', SimpleEnum), # invalid |
|
51
|
|
|
('c', '1', SimpleEnum), # invalid |
|
52
|
|
|
('e', '9S', SimpleEnum), # invalid (must be lowercase) |
|
53
|
|
|
('d', '/', SimpleEnum), # invalid |
|
54
|
|
|
('f', 'H'), # unsigned short (no class) |
|
55
|
|
|
] |
|
56
|
|
|
|
|
57
|
|
|
for field in test_fields: |
|
58
|
|
|
with self.subTest(field): # pylint: disable=no-member |
|
59
|
|
|
out = ElementEnum.valid(field) |
|
60
|
|
|
self.assertFalse(out) |
|
61
|
|
|
|
|
62
|
|
|
def test_valid_pack(self): |
|
63
|
|
|
"""Test packing valid enum values.""" |
|
64
|
|
|
|
|
65
|
|
|
field = ('a', 'b', SimpleEnum) # signed byte: -128, 127 |
|
66
|
|
|
out = ElementEnum.valid(field) |
|
67
|
|
|
self.assertTrue(out) |
|
68
|
|
|
elem = ElementEnum(field) |
|
69
|
|
|
test_values = [ |
|
70
|
|
|
({'a': 0}, b'\x00'), |
|
71
|
|
|
({'a': 2}, b'\x02'), |
|
72
|
|
|
({'a': SimpleEnum.one}, b'\x01'), |
|
73
|
|
|
({'a': SimpleEnum.two}, b'\x02'), |
|
74
|
|
|
({'a': 'two'}, b'\x02'), |
|
75
|
|
|
] |
|
76
|
|
|
for (in_val, out_val) in test_values: |
|
77
|
|
|
with self.subTest((out_val, in_val)): # pylint: disable=no-member |
|
78
|
|
|
ret = elem.pack(in_val) |
|
79
|
|
|
self.assertEqual(ret, out_val) |
|
80
|
|
|
|
|
81
|
|
|
def test_invalid_pack(self): |
|
82
|
|
|
"""Test packing invalid enum values.""" |
|
83
|
|
|
|
|
84
|
|
|
field = ('a', 'b', SimpleEnum) # signed byte: -128, 127 |
|
85
|
|
|
out = ElementEnum.valid(field) |
|
86
|
|
|
self.assertTrue(out) |
|
87
|
|
|
elem = ElementEnum(field) |
|
88
|
|
|
test_values = [ |
|
89
|
|
|
{'a': -1}, |
|
90
|
|
|
{'a': 3}, |
|
91
|
|
|
{'a': 'three'}, |
|
92
|
|
|
{'a': 'ONE'}, |
|
93
|
|
|
] |
|
94
|
|
|
msg = '{} is not a valid {}' |
|
95
|
|
|
for val in test_values: |
|
96
|
|
|
with self.subTest(val): # pylint: disable=no-member |
|
97
|
|
|
with self.assertRaises(ValueError) as cm: |
|
98
|
|
|
elem.pack(val) |
|
99
|
|
|
self.assertEqual(str(cm.exception), msg.format(val['a'], 'SimpleEnum')) |
|
100
|
|
|
|
|
101
|
|
|
def test_valid_unpack(self): |
|
102
|
|
|
"""Test unpacking valid enum values.""" |
|
103
|
|
|
|
|
104
|
|
|
field = ('a', 'b', SimpleEnum) # signed byte: -128, 127 |
|
105
|
|
|
out = ElementEnum.valid(field) |
|
106
|
|
|
self.assertTrue(out) |
|
107
|
|
|
elem = ElementEnum(field) |
|
108
|
|
|
test_values = [ |
|
109
|
|
|
(SimpleEnum.zero, b'\x00'), |
|
110
|
|
|
(SimpleEnum.one, b'\x01'), |
|
111
|
|
|
(SimpleEnum.two, b'\x02'), |
|
112
|
|
|
] |
|
113
|
|
|
for (out_val, in_val) in test_values: |
|
114
|
|
|
with self.subTest((out_val, in_val)): # pylint: disable=no-member |
|
115
|
|
|
(ret, unused) = elem.unpack({}, in_val) |
|
116
|
|
|
self.assertEqual(unused, b'') |
|
117
|
|
|
self.assertEqual(ret, out_val) |
|
118
|
|
|
|
|
119
|
|
|
def test_invalid_unpack(self): |
|
120
|
|
|
"""Test unpacking invalid enum values.""" |
|
121
|
|
|
|
|
122
|
|
|
field = ('a', 'b', SimpleEnum) # signed byte: -128, 127 |
|
123
|
|
|
out = ElementEnum.valid(field) |
|
124
|
|
|
self.assertTrue(out) |
|
125
|
|
|
elem = ElementEnum(field) |
|
126
|
|
|
test_values = [ |
|
127
|
|
|
(b'\xFF', -1), |
|
128
|
|
|
(b'\x03', 3), |
|
129
|
|
|
(b'\x7F', 127), |
|
130
|
|
|
(b'\x10', 16), |
|
131
|
|
|
(b'\x80', -128), |
|
132
|
|
|
] |
|
133
|
|
|
# msg = '{} is not a valid {}' |
|
134
|
|
|
for (in_val, out_val) in test_values: |
|
135
|
|
|
with self.subTest((in_val, out_val)): # pylint: disable=no-member |
|
136
|
|
|
# with self.assertRaises(ValueError) as cm: |
|
137
|
|
|
with self.assertRaises(ValueError): |
|
138
|
|
|
elem.unpack({}, in_val) |
|
139
|
|
|
# self.assertEqual(str(cm.exception), msg.format(out_val, 'SimpleEnum')) |
|
140
|
|
|
|