Skip to content

Commit 79b8173

Browse files
Issue #23015: Improved testing of the uuid module.
1 parent 0f3cde1 commit 79b8173

1 file changed

Lines changed: 94 additions & 105 deletions

File tree

Lib/test/test_uuid.py

Lines changed: 94 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ def importable(name):
1414
return False
1515

1616
class TestUUID(unittest.TestCase):
17-
last_node = None
18-
source2node = {}
19-
2017
def test_UUID(self):
2118
equal = self.assertEqual
2219
ascending = []
@@ -294,113 +291,13 @@ def test_exceptions(self):
294291
badtype(lambda: setattr(u, 'clock_seq_low', 0))
295292
badtype(lambda: setattr(u, 'node', 0))
296293

297-
def check_node(self, node, source):
298-
message = "%012x is not an RFC 4122 node ID" % node
299-
self.assertTrue(0 < node, message)
300-
self.assertTrue(node < (1 << 48), message)
301-
302-
TestUUID.source2node[source] = node
303-
if TestUUID.last_node:
304-
if TestUUID.last_node != node:
305-
msg = "different sources disagree on node:\n"
306-
for s, n in TestUUID.source2node.items():
307-
msg += " from source %r, node was %012x\n" % (s, n)
308-
# There's actually no reason to expect the MAC addresses
309-
# to agree across various methods -- e.g., a box may have
310-
# multiple network interfaces, and different ways of getting
311-
# a MAC address may favor different HW.
312-
##self.fail(msg)
313-
else:
314-
TestUUID.last_node = node
315-
316-
@unittest.skipUnless(os.name == 'posix', 'requires Posix')
317-
def test_ifconfig_getnode(self):
318-
node = uuid._ifconfig_getnode()
319-
if node is not None:
320-
self.check_node(node, 'ifconfig')
321-
322-
@unittest.skipUnless(os.name == 'posix', 'requires Posix')
323-
def test_arp_getnode(self):
324-
node = uuid._arp_getnode()
325-
if node is not None:
326-
self.check_node(node, 'arp')
327-
328-
@unittest.skipUnless(os.name == 'posix', 'requires Posix')
329-
def test_lanscan_getnode(self):
330-
node = uuid._lanscan_getnode()
331-
if node is not None:
332-
self.check_node(node, 'lanscan')
333-
334-
@unittest.skipUnless(os.name == 'posix', 'requires Posix')
335-
def test_netstat_getnode(self):
336-
node = uuid._netstat_getnode()
337-
if node is not None:
338-
self.check_node(node, 'netstat')
339-
340-
@unittest.skipUnless(os.name == 'nt', 'requires Windows')
341-
def test_ipconfig_getnode(self):
342-
node = uuid._ipconfig_getnode()
343-
if node is not None:
344-
self.check_node(node, 'ipconfig')
345-
346-
@unittest.skipUnless(importable('win32wnet'), 'requires win32wnet')
347-
@unittest.skipUnless(importable('netbios'), 'requires netbios')
348-
def test_netbios_getnode(self):
349-
self.check_node(uuid._netbios_getnode(), 'netbios')
350-
351-
def test_random_getnode(self):
352-
node = uuid._random_getnode()
353-
# Least significant bit of first octet must be set.
354-
self.assertTrue(node & 0x010000000000)
355-
self.assertTrue(node < (1 << 48))
356-
357-
@unittest.skipUnless(os.name == 'posix', 'requires Posix')
358-
@unittest.skipUnless(importable('ctypes'), 'requires ctypes')
359-
def test_unixdll_getnode(self):
360-
try: # Issues 1481, 3581: _uuid_generate_time() might be None.
361-
self.check_node(uuid._unixdll_getnode(), 'unixdll')
362-
except TypeError:
363-
pass
364-
365-
@unittest.skipUnless(os.name == 'nt', 'requires Windows')
366-
@unittest.skipUnless(importable('ctypes'), 'requires ctypes')
367-
def test_windll_getnode(self):
368-
self.check_node(uuid._windll_getnode(), 'windll')
369-
370294
def test_getnode(self):
371295
node1 = uuid.getnode()
372-
self.check_node(node1, "getnode1")
296+
self.assertTrue(0 < node1 < (1 << 48), '%012x' % node1)
373297

374298
# Test it again to ensure consistency.
375299
node2 = uuid.getnode()
376-
self.check_node(node2, "getnode2")
377-
378-
self.assertEqual(node1, node2)
379-
380-
@unittest.skipUnless(os.name == 'posix', 'requires Posix')
381-
def test_find_mac(self):
382-
data = '''\
383-
384-
fake hwaddr
385-
cscotun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
386-
eth0 Link encap:Ethernet HWaddr 12:34:56:78:90:ab
387-
'''
388-
def mock_popen(cmd):
389-
return io.StringIO(data)
390-
391-
if shutil.which('ifconfig') is None:
392-
path = os.pathsep.join(('/sbin', '/usr/sbin'))
393-
if shutil.which('ifconfig', path=path) is None:
394-
self.skipTest('requires ifconfig')
395-
396-
with support.swap_attr(os, 'popen', mock_popen):
397-
mac = uuid._find_mac(
398-
command='ifconfig',
399-
args='',
400-
hw_identifiers=['hwaddr'],
401-
get_index=lambda x: x + 1,
402-
)
403-
self.assertEqual(mac, 0x1234567890ab)
300+
self.assertEqual(node1, node2, '%012x != %012x' % (node1, node2))
404301

405302
@unittest.skipUnless(importable('ctypes'), 'requires ctypes')
406303
def test_uuid1(self):
@@ -512,5 +409,97 @@ def testIssue8621(self):
512409
self.assertNotEqual(parent_value, child_value)
513410

514411

412+
class TestInternals(unittest.TestCase):
413+
@unittest.skipUnless(os.name == 'posix', 'requires Posix')
414+
def test_find_mac(self):
415+
data = '''\
416+
417+
fake hwaddr
418+
cscotun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
419+
eth0 Link encap:Ethernet HWaddr 12:34:56:78:90:ab
420+
'''
421+
def mock_popen(cmd):
422+
return io.StringIO(data)
423+
424+
if shutil.which('ifconfig') is None:
425+
path = os.pathsep.join(('/sbin', '/usr/sbin'))
426+
if shutil.which('ifconfig', path=path) is None:
427+
self.skipTest('requires ifconfig')
428+
429+
with support.swap_attr(os, 'popen', mock_popen):
430+
mac = uuid._find_mac(
431+
command='ifconfig',
432+
args='',
433+
hw_identifiers=['hwaddr'],
434+
get_index=lambda x: x + 1,
435+
)
436+
self.assertEqual(mac, 0x1234567890ab)
437+
438+
def check_node(self, node, requires=None, network=False):
439+
if requires and node is None:
440+
self.skipTest('requires ' + requires)
441+
hex = '%012x' % node
442+
if support.verbose >= 2:
443+
print(hex, end=' ')
444+
if network:
445+
# 47 bit will never be set in IEEE 802 addresses obtained
446+
# from network cards.
447+
self.assertFalse(node & 0x010000000000, hex)
448+
self.assertTrue(0 < node < (1 << 48),
449+
"%s is not an RFC 4122 node ID" % hex)
450+
451+
@unittest.skipUnless(os.name == 'posix', 'requires Posix')
452+
def test_ifconfig_getnode(self):
453+
node = uuid._ifconfig_getnode()
454+
self.check_node(node, 'ifconfig', True)
455+
456+
@unittest.skipUnless(os.name == 'posix', 'requires Posix')
457+
def test_arp_getnode(self):
458+
node = uuid._arp_getnode()
459+
self.check_node(node, 'arp', True)
460+
461+
@unittest.skipUnless(os.name == 'posix', 'requires Posix')
462+
def test_lanscan_getnode(self):
463+
node = uuid._lanscan_getnode()
464+
self.check_node(node, 'lanscan', True)
465+
466+
@unittest.skipUnless(os.name == 'posix', 'requires Posix')
467+
def test_netstat_getnode(self):
468+
node = uuid._netstat_getnode()
469+
self.check_node(node, 'netstat', True)
470+
471+
@unittest.skipUnless(os.name == 'nt', 'requires Windows')
472+
def test_ipconfig_getnode(self):
473+
node = uuid._ipconfig_getnode()
474+
self.check_node(node, 'ipconfig', True)
475+
476+
@unittest.skipUnless(importable('win32wnet'), 'requires win32wnet')
477+
@unittest.skipUnless(importable('netbios'), 'requires netbios')
478+
def test_netbios_getnode(self):
479+
node = uuid._netbios_getnode()
480+
self.check_node(node, network=True)
481+
482+
def test_random_getnode(self):
483+
node = uuid._random_getnode()
484+
# Least significant bit of first octet must be set.
485+
self.assertTrue(node & 0x010000000000, '%012x' % node)
486+
self.check_node(node)
487+
488+
@unittest.skipUnless(os.name == 'posix', 'requires Posix')
489+
@unittest.skipUnless(importable('ctypes'), 'requires ctypes')
490+
def test_unixdll_getnode(self):
491+
try: # Issues 1481, 3581: _uuid_generate_time() might be None.
492+
node = uuid._unixdll_getnode()
493+
except TypeError:
494+
self.skipTest('requires uuid_generate_time')
495+
self.check_node(node)
496+
497+
@unittest.skipUnless(os.name == 'nt', 'requires Windows')
498+
@unittest.skipUnless(importable('ctypes'), 'requires ctypes')
499+
def test_windll_getnode(self):
500+
node = uuid._windll_getnode()
501+
self.check_node(node)
502+
503+
515504
if __name__ == '__main__':
516505
unittest.main()

0 commit comments

Comments
 (0)