cpython: fd1d994afe52 (original) (raw)
--- a/Lib/test/test_uuid.py +++ b/Lib/test/test_uuid.py @@ -15,9 +15,6 @@ def importable(name): return False class TestUUID(unittest.TestCase):
- def test_UUID(self): equal = self.assertEqual ascending = [] @@ -295,118 +292,13 @@ class TestUUID(unittest.TestCase): badtype(lambda: setattr(u, 'clock_seq_low', 0)) badtype(lambda: setattr(u, 'node', 0))
- def check_node(self, node, source):
message = "%012x is not an RFC 4122 node ID" % node[](#l1.18)
self.assertTrue(0 < node, message)[](#l1.19)
self.assertTrue(node < (1 << 48), message)[](#l1.20)
TestUUID.source2node[source] = node[](#l1.22)
if TestUUID.last_node:[](#l1.23)
if TestUUID.last_node != node:[](#l1.24)
msg = "different sources disagree on node:\n"[](#l1.25)
for s, n in TestUUID.source2node.items():[](#l1.26)
msg += " from source %r, node was %012x\n" % (s, n)[](#l1.27)
# There's actually no reason to expect the MAC addresses[](#l1.28)
# to agree across various methods -- e.g., a box may have[](#l1.29)
# multiple network interfaces, and different ways of getting[](#l1.30)
# a MAC address may favor different HW.[](#l1.31)
##self.fail(msg)[](#l1.32)
else:[](#l1.33)
TestUUID.last_node = node[](#l1.34)
- @unittest.skipUnless(os.name == 'posix', 'requires Posix')
- def test_ifconfig_getnode(self):
node = uuid._ifconfig_getnode()[](#l1.38)
if node is not None:[](#l1.39)
self.check_node(node, 'ifconfig')[](#l1.40)
- @unittest.skipUnless(os.name == 'posix', 'requires Posix')
- def test_ip_getnode(self):
node = uuid._ip_getnode()[](#l1.44)
if node is not None:[](#l1.45)
self.check_node(node, 'ip')[](#l1.46)
- @unittest.skipUnless(os.name == 'posix', 'requires Posix')
- def test_arp_getnode(self):
node = uuid._arp_getnode()[](#l1.50)
if node is not None:[](#l1.51)
self.check_node(node, 'arp')[](#l1.52)
- @unittest.skipUnless(os.name == 'posix', 'requires Posix')
- def test_lanscan_getnode(self):
node = uuid._lanscan_getnode()[](#l1.56)
if node is not None:[](#l1.57)
self.check_node(node, 'lanscan')[](#l1.58)
- @unittest.skipUnless(os.name == 'posix', 'requires Posix')
- def test_netstat_getnode(self):
node = uuid._netstat_getnode()[](#l1.62)
if node is not None:[](#l1.63)
self.check_node(node, 'netstat')[](#l1.64)
- @unittest.skipUnless(os.name == 'nt', 'requires Windows')
- def test_ipconfig_getnode(self):
node = uuid._ipconfig_getnode()[](#l1.68)
if node is not None:[](#l1.69)
self.check_node(node, 'ipconfig')[](#l1.70)
- @unittest.skipUnless(importable('win32wnet'), 'requires win32wnet')
- @unittest.skipUnless(importable('netbios'), 'requires netbios')
- def test_netbios_getnode(self):
self.check_node(uuid._netbios_getnode(), 'netbios')[](#l1.75)
- def test_random_getnode(self):
node = uuid._random_getnode()[](#l1.78)
# Least significant bit of first octet must be set.[](#l1.79)
self.assertTrue(node & 0x010000000000)[](#l1.80)
self.assertTrue(node < (1 << 48))[](#l1.81)
- @unittest.skipUnless(os.name == 'posix', 'requires Posix')
- @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
- def test_unixdll_getnode(self):
try: # Issues 1481, 3581: _uuid_generate_time() might be None.[](#l1.86)
self.check_node(uuid._unixdll_getnode(), 'unixdll')[](#l1.87)
except TypeError:[](#l1.88)
pass[](#l1.89)
- @unittest.skipUnless(os.name == 'nt', 'requires Windows')
- @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
- def test_windll_getnode(self):
self.check_node(uuid._windll_getnode(), 'windll')[](#l1.94)
- def test_getnode(self): node1 = uuid.getnode()
self.check_node(node1, "getnode1")[](#l1.98)
self.assertTrue(0 < node1 < (1 << 48), '%012x' % node1)[](#l1.99)
# Test it again to ensure consistency. node2 = uuid.getnode()
self.check_node(node2, "getnode2")[](#l1.103)
self.assertEqual(node1, node2)[](#l1.105)
- @unittest.skipUnless(os.name == 'posix', 'requires Posix')
- def test_find_mac(self):
data = '''[](#l1.109)
-fake hwaddr -cscotun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 -eth0 Link encap:Ethernet HWaddr 12:34:56:78:90:ab -''' -
popen = unittest.mock.MagicMock()[](#l1.115)
popen.stdout = io.BytesIO(data.encode())[](#l1.116)
with unittest.mock.patch.object(shutil, 'which',[](#l1.118)
return_value='/sbin/ifconfig'):[](#l1.119)
with unittest.mock.patch.object(subprocess, 'Popen',[](#l1.120)
return_value=popen):[](#l1.121)
mac = uuid._find_mac([](#l1.122)
command='ifconfig',[](#l1.123)
args='',[](#l1.124)
hw_identifiers=[b'hwaddr'],[](#l1.125)
get_index=lambda x: x + 1,[](#l1.126)
)[](#l1.127)
self.assertEqual(mac, 0x1234567890ab)[](#l1.129)
self.assertEqual(node1, node2, '%012x != %012x' % (node1, node2))[](#l1.130)
@unittest.skipUnless(importable('ctypes'), 'requires ctypes') def test_uuid1(self): @@ -518,5 +410,101 @@ eth0 Link encap:Ethernet HWaddr 12 self.assertNotEqual(parent_value, child_value) +class TestInternals(unittest.TestCase):
- @unittest.skipUnless(os.name == 'posix', 'requires Posix')
- def test_find_mac(self):
data = '''[](#l1.141)
+fake hwaddr +cscotun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 +eth0 Link encap:Ethernet HWaddr 12:34:56:78:90:ab +''' +
popen = unittest.mock.MagicMock()[](#l1.147)
popen.stdout = io.BytesIO(data.encode())[](#l1.148)
with unittest.mock.patch.object(shutil, 'which',[](#l1.150)
return_value='/sbin/ifconfig'):[](#l1.151)
with unittest.mock.patch.object(subprocess, 'Popen',[](#l1.152)
return_value=popen):[](#l1.153)
mac = uuid._find_mac([](#l1.154)
command='ifconfig',[](#l1.155)
args='',[](#l1.156)
hw_identifiers=[b'hwaddr'],[](#l1.157)
get_index=lambda x: x + 1,[](#l1.158)
)[](#l1.159)
self.assertEqual(mac, 0x1234567890ab)[](#l1.161)
- def check_node(self, node, requires=None, network=False):
if requires and node is None:[](#l1.164)
self.skipTest('requires ' + requires)[](#l1.165)
hex = '%012x' % node[](#l1.166)
if support.verbose >= 2:[](#l1.167)
print(hex, end=' ')[](#l1.168)
if network:[](#l1.169)
# 47 bit will never be set in IEEE 802 addresses obtained[](#l1.170)
# from network cards.[](#l1.171)
self.assertFalse(node & 0x010000000000, hex)[](#l1.172)
self.assertTrue(0 < node < (1 << 48),[](#l1.173)
"%s is not an RFC 4122 node ID" % hex)[](#l1.174)
- @unittest.skipUnless(os.name == 'posix', 'requires Posix')
- def test_ifconfig_getnode(self):
node = uuid._ifconfig_getnode()[](#l1.178)
self.check_node(node, 'ifconfig', True)[](#l1.179)
- @unittest.skipUnless(os.name == 'posix', 'requires Posix')
- def test_ip_getnode(self):
node = uuid._ip_getnode()[](#l1.183)
self.check_node(node, 'ip', True)[](#l1.184)
- @unittest.skipUnless(os.name == 'posix', 'requires Posix')
- def test_arp_getnode(self):
node = uuid._arp_getnode()[](#l1.188)
self.check_node(node, 'arp', True)[](#l1.189)
- @unittest.skipUnless(os.name == 'posix', 'requires Posix')
- def test_lanscan_getnode(self):
node = uuid._lanscan_getnode()[](#l1.193)
self.check_node(node, 'lanscan', True)[](#l1.194)
- @unittest.skipUnless(os.name == 'posix', 'requires Posix')
- def test_netstat_getnode(self):
node = uuid._netstat_getnode()[](#l1.198)
self.check_node(node, 'netstat', True)[](#l1.199)
- @unittest.skipUnless(os.name == 'nt', 'requires Windows')
- def test_ipconfig_getnode(self):
node = uuid._ipconfig_getnode()[](#l1.203)
self.check_node(node, 'ipconfig', True)[](#l1.204)
- @unittest.skipUnless(importable('win32wnet'), 'requires win32wnet')
- @unittest.skipUnless(importable('netbios'), 'requires netbios')
- def test_netbios_getnode(self):
node = uuid._netbios_getnode()[](#l1.209)
self.check_node(node, network=True)[](#l1.210)
- def test_random_getnode(self):
node = uuid._random_getnode()[](#l1.213)
# Least significant bit of first octet must be set.[](#l1.214)
self.assertTrue(node & 0x010000000000, '%012x' % node)[](#l1.215)
self.check_node(node)[](#l1.216)
- @unittest.skipUnless(os.name == 'posix', 'requires Posix')
- @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
- def test_unixdll_getnode(self):
try: # Issues 1481, 3581: _uuid_generate_time() might be None.[](#l1.221)
node = uuid._unixdll_getnode()[](#l1.222)
except TypeError:[](#l1.223)
self.skipTest('requires uuid_generate_time')[](#l1.224)
self.check_node(node)[](#l1.225)