forked from mentalisttraceur/python-macaddress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
285 lines (220 loc) · 7.82 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
from hypothesis import given
from hypothesis.strategies import (
binary,
booleans,
characters,
composite,
from_regex,
integers,
lists,
one_of,
sampled_from,
text,
)
import pytest
from macaddress import *
@composite
def _addresses(draw, random_formats=0):
Class = draw(_address_classes(random_formats))
address_as_an_integer = draw(_address_integers(Class.size))
return Class(address_as_an_integer)
@composite
def _addresses_with_several_random_formats(draw):
random_formats = draw(integers(min_value=2, max_value=8))
return draw(_addresses(random_formats=random_formats))
@composite
def _address_classes_and_invalid_integers(draw):
Class = draw(_address_classes())
invalid_integer = draw(one_of(
integers(max_value=-1),
integers(min_value=(1 << Class.size)),
))
return (Class, invalid_integer)
@composite
def _address_classes_and_invalid_bytes(draw):
Class = draw(_address_classes())
size_in_bytes = (Class.size + 7) >> 3
invalid_byte_string = draw(one_of(
binary(max_size=size_in_bytes-1),
binary(min_size=size_in_bytes+1),
))
return (Class, invalid_byte_string)
@composite
def _address_classes_and_invalid_strings(draw):
Class = draw(_address_classes())
size_in_nibbles = (Class.size + 3) >> 2
invalid_string = draw(one_of(
text(characters(), max_size=size_in_nibbles-1),
text(characters(), min_size=size_in_nibbles+1),
from_regex('[^0-9A-Fa-f]'),
))
return (Class, invalid_string)
@composite
def _lists_of_distinctly_formatted_addresses(draw):
return draw(lists(
_addresses(random_formats=1),
min_size=2,
max_size=8,
unique_by=lambda address: address.formats[0],
))
@composite
def _lists_of_distinctly_sized_addresses(draw):
return draw(lists(
_addresses(),
min_size=2,
max_size=8,
unique_by=lambda address: (address.size + 7) >> 3,
))
@composite
def _address_classes(draw, random_formats=0):
address_sizes = integers(min_value=1, max_value=64)
size_in_bits = draw(address_sizes)
size_in_nibbles = (size_in_bits + 3) >> 2
if random_formats > 0:
format_strings = draw(lists(
_address_format_strings(size_in_nibbles),
min_size=random_formats,
max_size=random_formats,
))
else:
format_string = 'x' * size_in_nibbles
format_strings = (format_string,)
class_should_be_slotted = draw(booleans())
class Class(HWAddress):
__slots__ = ()
size = size_in_bits
formats = format_strings
def __repr__(self):
name = type(self).__name__
address = repr(self._address)
formats = repr(type(self).formats)
slots = 'slots=' + repr(class_should_be_slotted)
return ' '.join(('<', name, address, formats, slots, '>'))
if not class_should_be_slotted:
# Subclassing again without defining __slots__ is effectively
# like "removing" slots from the class we just made.
class Class(Class):
pass
# This helpfully shows the size of each class and instance in
# pytest output when Hypothesis finds test-failing examples:
Class.__name__ += repr(size_in_bits)
Class.__qualname__ += repr(size_in_bits)
return Class
def _address_integers(size_in_bits):
return integers(min_value=0, max_value=((1 << size_in_bits) - 1))
_address_format_characters = sampled_from('x-:.')
@composite
def _address_format_strings(draw, size_in_nibbles):
characters = []
while size_in_nibbles:
character = draw(_address_format_characters)
if character == 'x':
size_in_nibbles -= 1
characters.append(character)
return ''.join(characters)
@given(_addresses())
def test_int(address):
Class = type(address)
assert Class(int(address)) == address
@given(_address_classes_and_invalid_integers())
def test_int_value_error(Class_and_integer):
Class, integer = Class_and_integer
with pytest.raises(ValueError):
Class(integer)
@given(_addresses())
def test_bytes(address):
Class = type(address)
assert Class(bytes(address)) == address
@given(_address_classes_and_invalid_bytes())
def test_bytes_value_error(Class_and_bytes):
Class, byte_string = Class_and_bytes
with pytest.raises(ValueError):
Class(byte_string)
@given(_addresses(random_formats=1))
def test_str(address):
Class = type(address)
assert Class(str(address)) == address
@given(_address_classes_and_invalid_strings())
def test_str_value_error(Class_and_string):
Class, string = Class_and_string
with pytest.raises(ValueError):
Class(string)
@given(_address_classes())
def test_str_x_literal_value_error(Class):
size_in_nibbles = (Class.size + 3) >> 2
with pytest.raises(ValueError):
Class('x' * size_in_nibbles)
@given(_addresses_with_several_random_formats())
def test_str_alternatives(address):
Class = type(address)
formats = Class.formats
for format in formats:
# Override instance formats to make this format the only
# format, because it will stringify using the first one.
# Note: we have to overwrite `.formats` on the class
# because it cannot be overwritten on the instance if
# the class is slotted.
Class.formats = (format,)
# Format to string using the newly chosen format:
formatted = str(address)
# Restore the original formats for comparison, so that
# the test verifies that the constructor parses each
# alternate format whether or not it is the first one:
Class.formats = formats
assert Class(formatted) == address
@given(_addresses())
def test_copy_construction(address):
Class = type(address)
assert Class(address) == address
@given(_addresses(random_formats=1))
def test_parse_str(address):
Class = type(address)
assert parse(str(address), Class) == address
@given(_lists_of_distinctly_formatted_addresses())
def test_parse_str_alternatives(addresses):
classes = [type(address) for address in addresses]
for address in addresses:
assert parse(str(address), *classes) == address
@given(_addresses())
def test_parse_bytes(address):
Class = type(address)
assert parse(bytes(address), Class) == address
@given(_lists_of_distinctly_sized_addresses())
def test_parse_bytes_alternatives(addresses):
classes = [type(address) for address in addresses]
for address in addresses:
assert parse(bytes(address), *classes) == address
@given(_addresses())
def test_parse_passthrough(address):
Class = type(address)
assert parse(address, Class) == address
@given(_addresses(), _addresses())
def test_ordering(address1, address2):
assert (address1 < address2) == (_bits(address1) < _bits(address2))
assert (address1 <= address2) == (_bits(address1) <= _bits(address2))
assert (address1 > address2) == (_bits(address1) > _bits(address2))
assert (address1 >= address2) == (_bits(address1) >= _bits(address2))
def _bits(address):
size = address.size
address = int(address)
bits = []
while size:
least_significant_bit = address & 1
bits.append(least_significant_bit)
address >>= 1
size -= 1
return ''.join(map(str, reversed(bits)))
def test_type_errors():
class Dummy:
pass
for thing in (None, [], {}, object, object(), Dummy, Dummy()):
with pytest.raises(TypeError):
MAC(thing)
with pytest.raises(TypeError):
parse(thing, MAC, OUI)
with pytest.raises(TypeError):
parse(thing)
def test_provided_classes():
for Class in OUI, CDI32, CDI40, MAC, EUI48, EUI60, EUI64:
for format in Class.formats:
assert (Class.size + 3) >> 2 == sum(1 for x in format if x == 'x')