summaryrefslogtreecommitdiffstats
path: root/netaddr/tests/eui/test_eui.py
blob: c17d0ceedeac0ff35b509ca4c07a19696d044d81 (plain)
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
import sys
import pickle
import random

import pytest

from netaddr import (EUI, mac_unix, mac_unix_expanded, mac_cisco,
    mac_bare, mac_pgsql, eui64_unix, eui64_unix_expanded,
    eui64_cisco, eui64_bare, OUI, IAB, IPAddress)


def test_mac_address_properties():
    mac = EUI('00-1B-77-49-54-FD')
    assert repr(mac) == "EUI('00-1B-77-49-54-FD')"
    assert str(mac)
    assert str(mac.oui) == '00-1B-77'
    assert mac.ei == '49-54-FD'
    assert mac.version == 48


def test_mac_address_numerical_operations():
    mac = EUI('00-1B-77-49-54-FD')
    assert int(mac) == 117965411581
    assert hex(mac) == '0x1b774954fd'
    assert mac.bits() == '00000000-00011011-01110111-01001001-01010100-11111101'
    assert mac.bin == '0b1101101110111010010010101010011111101'


@pytest.mark.skipif(sys.version_info > (3,), reason="requires python 2.x")
def test_eui_oct_format_py2():
    assert oct(EUI('00-1B-77-49-54-FD')) == '01556722252375'


@pytest.mark.skipif(sys.version_info < (3,), reason="requires python 3.x")
def test_eui_oct_format_py3():
    assert oct(EUI('00-1B-77-49-54-FD')) == '0o1556722252375'


def test_eui_constructor():
    assert str(EUI('00-1B-77-49-54-FD')) == '00-1B-77-49-54-FD'
    assert str(EUI('00-1b-77-49-54-fd')) == '00-1B-77-49-54-FD'
    assert str(EUI('0:1b:77:49:54:fd')) == '00-1B-77-49-54-FD'
    assert str(EUI('001b:7749:54fd')) == '00-1B-77-49-54-FD'
    assert str(EUI('1b:7749:54fd')) == '00-1B-77-49-54-FD'
    assert str(EUI('1B:7749:54FD')) == '00-1B-77-49-54-FD'
    assert str(EUI('001b774954fd')) == '00-1B-77-49-54-FD'
    assert str(EUI('01B774954FD')) == '00-1B-77-49-54-FD'
    assert str(EUI('001B77:4954FD')) == '00-1B-77-49-54-FD'

    eui = EUI('00-90-96-AF-CC-39')

    assert eui == EUI('0-90-96-AF-CC-39')
    assert eui == EUI('00-90-96-af-cc-39')
    assert eui == EUI('00:90:96:AF:CC:39')
    assert eui == EUI('00:90:96:af:cc:39')
    assert eui == EUI('0090-96AF-CC39')
    assert eui == EUI('0090:96af:cc39')
    assert eui == EUI('009096-AFCC39')
    assert eui == EUI('009096:AFCC39')
    assert eui == EUI('009096AFCC39')
    assert eui == EUI('009096afcc39')
    assert EUI('01-00-00-00-00-00') == EUI('010000000000')
    assert EUI('01-00-00-00-00-00') == EUI('10000000000')
    assert EUI('01-00-00-01-00-00') == EUI('010000:010000')
    assert EUI('01-00-00-01-00-00') == EUI('10000:10000')


def test_eui_dialects():
    mac = EUI('00-1B-77-49-54-FD')
    assert str(mac) == '00-1B-77-49-54-FD'

    mac = EUI('00-1B-77-49-54-FD', dialect=mac_unix)
    assert str(mac) == '0:1b:77:49:54:fd'

    mac = EUI('00-1B-77-49-54-FD', dialect=mac_unix_expanded)
    assert str(mac) == '00:1b:77:49:54:fd'

    mac = EUI('00-1B-77-49-54-FD', dialect=mac_cisco)
    assert str(mac) == '001b.7749.54fd'

    mac = EUI('00-1B-77-49-54-FD', dialect=mac_bare)
    assert str(mac) == '001B774954FD'

    mac = EUI('00-1B-77-49-54-FD', dialect=mac_pgsql)
    assert str(mac) == '001b77:4954fd'


def test_eui_dialect_property_assignment():
    mac = EUI('00-1B-77-49-54-FD')
    assert str(mac) == '00-1B-77-49-54-FD'

    mac.dialect = mac_pgsql
    assert str(mac) == '001b77:4954fd'


def test_eui_format():
    mac = EUI('00-1B-77-49-54-FD')
    assert mac.format() == '00-1B-77-49-54-FD'
    assert mac.format(mac_pgsql) == '001b77:4954fd'
    assert mac.format(mac_unix_expanded) == '00:1b:77:49:54:fd'


def test_eui_custom_dialect():
    class mac_custom(mac_unix):
        word_fmt = '%.2X'

    mac = EUI('00-1B-77-49-54-FD', dialect=mac_custom)
    assert str(mac) == '00:1B:77:49:54:FD'


def test_eui64_dialects():
    mac = EUI('00-1B-77-49-54-FD-12-34')
    assert str(mac) == '00-1B-77-49-54-FD-12-34'

    mac = EUI('00-1B-77-49-54-FD-12-34', dialect=eui64_unix)
    assert str(mac) == '0:1b:77:49:54:fd:12:34'

    mac = EUI('00-1B-77-49-54-FD-12-34', dialect=eui64_unix_expanded)
    assert str(mac) == '00:1b:77:49:54:fd:12:34'

    mac = EUI('00-1B-77-49-54-FD-12-34', dialect=eui64_cisco)
    assert str(mac) == '001b.7749.54fd.1234'

    mac = EUI('00-1B-77-49-54-FD-12-34', dialect=eui64_bare)
    assert str(mac) == '001B774954FD1234'


def test_eui64_dialect_property_assignment():
    mac = EUI('00-1B-77-49-54-FD-12-34')
    assert str(mac) == '00-1B-77-49-54-FD-12-34'

    mac.dialect = eui64_cisco
    assert str(mac) == '001b.7749.54fd.1234'


def test_eui64_custom_dialect():
    class eui64_custom(eui64_unix):
        word_fmt = '%.2X'

    mac = EUI('00-1B-77-49-54-FD-12-34', dialect=eui64_custom)
    assert str(mac) == '00:1B:77:49:54:FD:12:34'


def test_eui_oui_information():
    mac = EUI('00-1B-77-49-54-FD')
    oui = mac.oui
    assert str(oui) == '00-1B-77'

    assert oui.registration().address == [
        'Lot 8, Jalan Hi-Tech 2/3',
        'Kulim  Kedah  09000',
        'MY'
    ]

    assert oui.registration().org == 'Intel Corporate'


def test_oui_constructor():
    oui = OUI(524336)
    assert str(oui) == '08-00-30'
    assert oui == OUI('08-00-30')

    assert oui.registration(0).address == [
        '2380 N. ROSE AVENUE',
        'OXNARD  CA  93010',
        'US'
    ]
    assert oui.registration(0).org == 'NETWORK RESEARCH CORPORATION'
    assert oui.registration(0).oui == '08-00-30'

    assert oui.registration(1).address == [
        'GPO BOX 2476V',
        'MELBOURNE  VIC  3001',
        'AU',
    ]
    assert oui.registration(1).org == 'ROYAL MELBOURNE INST OF TECH'
    assert oui.registration(1).oui == '08-00-30'

    assert oui.registration(2).address == [
        'CH-1211',
        'GENEVE  SUISSE/SWITZ  023',
        'CH'
    ]
    assert oui.registration(2).org == 'CERN'
    assert oui.registration(2).oui == '08-00-30'
    assert oui.reg_count == 3


def test_oui_hash():
    oui0 = OUI(0)
    oui1 = OUI(1)
    oui_dict = {oui0: None, oui1: None}

    assert list(oui_dict.keys()) == [OUI(0), OUI(1)]


def test_eui_iab():
    mac = EUI('00-50-C2-00-0F-01')
    assert mac.is_iab()

    iab = mac.iab
    assert str(iab) == '00-50-C2-00-00-00'
    assert iab == IAB('00-50-C2-00-00-00')

    reg_info = iab.registration()

    assert reg_info.address == [
        '1241 Superieor Ave E',
        'Cleveland  OH  44114',
        'US',
    ]

    assert reg_info.iab == '00-50-C2-00-00-00'
    assert reg_info.org == 'T.L.S. Corp.'


def test_eui64():
    eui = EUI('00-1B-77-FF-FE-49-54-FD')
    assert eui == EUI('00-1B-77-FF-FE-49-54-FD')
    assert eui.oui == OUI('00-1B-77')
    assert eui.ei == 'FF-FE-49-54-FD'
    assert eui.eui64() == EUI('00-1B-77-FF-FE-49-54-FD')



def test_mac_to_ipv6_link_local():
    mac = EUI('00-0F-1F-12-E7-33')
    ip = mac.ipv6_link_local()
    assert ip == IPAddress('fe80::20f:1fff:fe12:e733')
    assert ip.is_link_local()
    assert mac.eui64() == EUI('00-0F-1F-FF-FE-12-E7-33')


def test_iab():
    eui = EUI('00-50-C2-05-C0-00')

    assert eui.is_iab()
    assert str(eui.oui) == '00-50-C2'
    assert str(eui.iab) == '00-50-C2-05-C0-00'
    assert eui.ei == '05-C0-00'
    assert int(eui.oui) == 0x0050c2
    assert int(eui.iab) == 0x0050c205c

    assert IAB(eui.value) == eui.iab


def test_new_iab():
    eui = EUI('40-D8-55-13-10-00')

    assert eui.is_iab()
    assert str(eui.oui) == '40-D8-55'
    assert str(eui.iab) == '40-D8-55-13-10-00'
    assert eui.ei == '13-10-00'
    assert int(eui.oui) == 0x40d855
    assert int(eui.iab) == 0x40d855131

    assert IAB(eui.value) == eui.iab


def test_eui48_vs_eui64():
    eui48 = EUI('01-00-00-01-00-00')
    assert int(eui48) == 1099511693312

    eui64 = EUI('00-00-01-00-00-01-00-00')
    assert int(eui64) == 1099511693312
    assert eui48 != eui64


def test_eui_sort_order():
    eui_list = [
        EUI(0, 64),
        EUI(0),
        EUI(0xffffffffffff, dialect=mac_unix),
        EUI(0x1000000000000),
    ]

    random.shuffle(eui_list)
    eui_list.sort()

    assert [(str(eui), eui.version) for eui in eui_list] == [
        ('00-00-00-00-00-00', 48),
        ('ff:ff:ff:ff:ff:ff', 48),
        ('00-00-00-00-00-00-00-00', 64),
        ('00-01-00-00-00-00-00-00', 64),
    ]


def test_eui_pickle_support():
    eui1 = EUI('00-00-00-01-02-03')
    eui2 = pickle.loads(pickle.dumps(eui1))
    assert eui1 == eui2

    eui1 = EUI('00-00-00-01-02-03', dialect=mac_cisco)
    eui2 = pickle.loads(pickle.dumps(eui1))
    assert eui1 == eui2
    assert eui1.dialect == eui2.dialect

    oui1 = EUI('00-00-00-01-02-03').oui
    oui2 = pickle.loads(pickle.dumps(oui1))
    assert oui1 == oui2
    assert oui1.records == oui2.records

    iab1 = EUI('00-50-C2-00-1F-FF').iab
    iab2 = pickle.loads(pickle.dumps(iab1))
    assert iab1 == iab2
    assert iab1.record == iab2.record


def test_mac_to_eui64_conversion():
    mac = EUI('00-1B-77-49-54-FD')
    assert mac == EUI('00-1B-77-49-54-FD')

    eui = mac.eui64()
    assert eui == EUI('00-1B-77-FF-FE-49-54-FD')
    eui.eui64() == EUI('00-1B-77-FF-FE-49-54-FD')

    assert int(eui) == 7731765737772285
    assert eui.packed == b'\x00\x1bw\xff\xfeIT\xfd'
    assert eui.bin == '0b11011011101111111111111111110010010010101010011111101'
    assert eui.bits() == '00000000-00011011-01110111-11111111-11111110-01001001-01010100-11111101'


def test_mac_to_ipv6():
    mac = EUI('00-1B-77-49-54-FD')
    eui = mac.eui64()
    assert mac == EUI('00-1B-77-49-54-FD')
    assert eui == EUI('00-1B-77-FF-FE-49-54-FD')

    assert mac.modified_eui64() == EUI('02-1B-77-FF-FE-49-54-FD')
    assert mac.ipv6_link_local() == IPAddress('fe80::21b:77ff:fe49:54fd')
    assert eui.ipv6_link_local() == IPAddress('fe80::21b:77ff:fe49:54fd')

    assert mac.ipv6(0x12340000000000000000000000000000) == IPAddress('1234::21b:77ff:fe49:54fd')
    assert eui.ipv6(0x12340000000000000000000000000000) == IPAddress('1234::21b:77ff:fe49:54fd')


def test_eui64_constructor():
    addr_colons = EUI('00:1B:77:49:54:FD:BB:34')
    assert addr_colons == EUI('00-1B-77-49-54-FD-BB-34')

    addr_no_delimiter = EUI('001B774954FDBB34')
    assert addr_no_delimiter == EUI('00-1B-77-49-54-FD-BB-34')