-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.py
363 lines (279 loc) · 9.44 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import weakref
from hypothesis import given
from hypothesis.strategies import (
binary,
booleans,
characters,
composite,
from_regex,
integers,
lists,
one_of,
sampled_from,
text,
)
import reprshed
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):
if class_should_be_slotted:
__slots__ = ()
size = size_in_bits
formats = format_strings
def __repr__(self):
return reprshed.impure(
self,
size=type(self).size,
formats=type(self).formats,
slots=class_should_be_slotted,
address=self._address,
)
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.
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
class ChildClass(Class):
pass
assert Class(ChildClass(address)) == address
@given(_addresses())
def test_copy_construction_wrong_type(address):
Class = type(address)
class SiblingClass(HWAddress):
size = Class.size
with pytest.raises(TypeError):
SiblingClass(address)
address2 = SiblingClass(int(address))
with pytest.raises(TypeError):
Class(address2)
@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_equality(address1, address2):
assert (address1 == address2) == (_key(address1) == _key(address2))
assert (address1 != address2) == (_key(address1) != _key(address2))
@given(_addresses(), _addresses())
def test_ordering(address1, address2):
assert (address1 < address2) == (_key(address1) < _key(address2))
assert (address1 <= address2) == (_key(address1) <= _key(address2))
assert (address1 > address2) == (_key(address1) > _key(address2))
assert (address1 >= address2) == (_key(address1) >= _key(address2))
def _key(address):
return (_bits(address), id(type(address)))
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)))
@given(_addresses(), _addresses())
def test_comparison_consistency(address1, address2):
eq = (address1 == address2)
ne = (address1 != address2)
lt = (address1 < address2)
le = (address1 <= address2)
gt = (address1 > address2)
ge = (address1 >= address2)
assert eq == (not ne)
assert lt == (not ge)
assert gt == (not le)
assert eq == (ge and le)
assert ne == (lt or gt)
@given(_addresses(), _addresses())
def test_hash(address1, address2):
Class = type(address1)
assert hash(Class(address1)) == hash(address1)
if address1 == address2:
assert hash(address1) == hash(address2)
@given(_addresses())
def test_repr(address):
Class = type(address)
del Class.__repr__
assert eval(repr(address)) == address
@given(_addresses())
def test_repr_no_formats(address):
Class = type(address)
del Class.__repr__
del Class.formats
assert eval(repr(address)) == address
@given(_addresses())
def test_str_no_formats(address):
Class = type(address)
del Class.formats
with pytest.raises(TypeError):
str(address)
with pytest.raises(TypeError):
Class("")
@given(_addresses())
def test_weak_reference(address):
weakref.ref(address)
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_equality_not_implemented():
class Dummy:
pass
for thing in (None, [], {}, object, object(), Dummy, Dummy()):
assert MAC(0).__eq__(thing) is NotImplemented
assert MAC(0).__ne__(thing) is NotImplemented
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')