Commit 0bcbfa43 authored by Michael Felt's avatar Michael Felt Committed by Tal Einat

bpo-28009: Fix uuid.uuid1() and uuid.get_node() on AIX (GH-8672)

parent 9f77268f
import unittest.mock import unittest
from test import support from test import support
import builtins import builtins
import contextlib import contextlib
...@@ -15,7 +15,6 @@ from unittest import mock ...@@ -15,7 +15,6 @@ from unittest import mock
py_uuid = support.import_fresh_module('uuid', blocked=['_uuid']) py_uuid = support.import_fresh_module('uuid', blocked=['_uuid'])
c_uuid = support.import_fresh_module('uuid', fresh=['_uuid']) c_uuid = support.import_fresh_module('uuid', fresh=['_uuid'])
def importable(name): def importable(name):
try: try:
__import__(name) __import__(name)
...@@ -459,7 +458,7 @@ class BaseTestUUID: ...@@ -459,7 +458,7 @@ class BaseTestUUID:
# uuid.getnode to fall back on uuid._random_getnode, which will # uuid.getnode to fall back on uuid._random_getnode, which will
# generate a valid value. # generate a valid value.
too_large_getter = lambda: 1 << 48 too_large_getter = lambda: 1 << 48
with unittest.mock.patch.multiple( with mock.patch.multiple(
self.uuid, self.uuid,
_node=None, # Ignore any cached node value. _node=None, # Ignore any cached node value.
_GETTERS=[too_large_getter], _GETTERS=[too_large_getter],
...@@ -538,8 +537,8 @@ class BaseTestUUID: ...@@ -538,8 +537,8 @@ class BaseTestUUID:
f = self.uuid._generate_time_safe f = self.uuid._generate_time_safe
if f is None: if f is None:
self.skipTest('need uuid._generate_time_safe') self.skipTest('need uuid._generate_time_safe')
with unittest.mock.patch.object(self.uuid, '_generate_time_safe', with mock.patch.object(self.uuid, '_generate_time_safe',
lambda: (f()[0], safe_value)): lambda: (f()[0], safe_value)):
yield yield
@unittest.skipUnless(os.name == 'posix', 'POSIX-only test') @unittest.skipUnless(os.name == 'posix', 'POSIX-only test')
...@@ -674,27 +673,57 @@ class TestUUIDWithExtModule(BaseTestUUID, unittest.TestCase): ...@@ -674,27 +673,57 @@ class TestUUIDWithExtModule(BaseTestUUID, unittest.TestCase):
class BaseTestInternals: class BaseTestInternals:
_uuid = py_uuid _uuid = py_uuid
@unittest.skipUnless(os.name == 'posix', 'requires Posix')
def test_find_mac(self): def test_find_under_heading(self):
data = '''\
Name Mtu Network Address Ipkts Ierrs Opkts Oerrs Coll
en0 1500 link#2 fe.ad.c.1.23.4 1714807956 0 711348489 0 0
01:00:5e:00:00:01
en0 1500 192.168.129 x071 1714807956 0 711348489 0 0
224.0.0.1
en0 1500 192.168.90 x071 1714807956 0 711348489 0 0
224.0.0.1
'''
def mock_get_command_stdout(command, args):
return io.BytesIO(data.encode())
# The above data is from AIX - with '.' as _MAC_DELIM and strings
# shorter than 17 bytes (no leading 0). (_MAC_OMITS_LEADING_ZEROES=True)
with mock.patch.multiple(self.uuid,
_MAC_DELIM=b'.',
_MAC_OMITS_LEADING_ZEROES=True,
_get_command_stdout=mock_get_command_stdout):
mac = self.uuid._find_mac_under_heading(
command='netstat',
args='-ian',
heading=b'Address',
)
self.assertEqual(mac, 0xfead0c012304)
def test_find_mac_near_keyword(self):
# key and value are on the same line
data = ''' data = '''
fake hwaddr fake Link encap:UNSPEC hwaddr 00-00
cscotun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 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 eth0 Link encap:Ethernet HWaddr 12:34:56:78:90:ab
''' '''
popen = unittest.mock.MagicMock() def mock_get_command_stdout(command, args):
popen.stdout = io.BytesIO(data.encode()) return io.BytesIO(data.encode())
with unittest.mock.patch.object(shutil, 'which', # The above data will only be parsed properly on non-AIX unixes.
return_value='/sbin/ifconfig'): with mock.patch.multiple(self.uuid,
with unittest.mock.patch.object(subprocess, 'Popen', _MAC_DELIM=b':',
return_value=popen): _MAC_OMITS_LEADING_ZEROES=False,
mac = self.uuid._find_mac( _get_command_stdout=mock_get_command_stdout):
command='ifconfig', mac = self.uuid._find_mac_near_keyword(
args='', command='ifconfig',
hw_identifiers=[b'hwaddr'], args='',
get_index=lambda x: x + 1, keywords=[b'hwaddr'],
) get_word_index=lambda x: x + 1,
)
self.assertEqual(mac, 0x1234567890ab) self.assertEqual(mac, 0x1234567890ab)
......
This diff is collapsed.
Fix uuid.getnode() on platforms with '.' as MAC Addr delimiter as well
fix for MAC Addr format that omits a leading 0 in MAC Addr values.
Currently, AIX is the only know platform with these settings.
Patch by Michael Felt.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment