diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2014-10-21 20:33:10 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2014-10-21 20:33:10 (GMT) |
commit | b9d0199c073fc963b27106f4b7752dcbe39bfed3 (patch) | |
tree | f1a04098cb32574266bbe03126fb673ec75bc4c7 /Lib/test/test_uuid.py | |
parent | 35cd53a940f03453a1dab782cac9a3cb4212174c (diff) | |
download | cpython-b9d0199c073fc963b27106f4b7752dcbe39bfed3.zip cpython-b9d0199c073fc963b27106f4b7752dcbe39bfed3.tar.gz cpython-b9d0199c073fc963b27106f4b7752dcbe39bfed3.tar.bz2 |
Issue #22637: avoid using a shell in uuid
Replace os.popen() with subprocess.Popen() in the uuid module.
Diffstat (limited to 'Lib/test/test_uuid.py')
-rw-r--r-- | Lib/test/test_uuid.py | 38 |
1 files changed, 19 insertions, 19 deletions
diff --git a/Lib/test/test_uuid.py b/Lib/test/test_uuid.py index 7264808..115e66c 100644 --- a/Lib/test/test_uuid.py +++ b/Lib/test/test_uuid.py @@ -1,9 +1,10 @@ -import unittest +import unittest.mock from test import support import builtins import io import os import shutil +import subprocess import uuid def importable(name): @@ -361,28 +362,27 @@ class TestUUID(unittest.TestCase): @unittest.skipUnless(os.name == 'posix', 'requires Posix') def test_find_mac(self): - data = '''\ - + data = ''' 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 ''' - def mock_popen(cmd): - return io.StringIO(data) - - if shutil.which('ifconfig') is None: - path = os.pathsep.join(('/sbin', '/usr/sbin')) - if shutil.which('ifconfig', path=path) is None: - self.skipTest('requires ifconfig') - - with support.swap_attr(os, 'popen', mock_popen): - mac = uuid._find_mac( - command='ifconfig', - args='', - hw_identifiers=['hwaddr'], - get_index=lambda x: x + 1, - ) - self.assertEqual(mac, 0x1234567890ab) + + popen = unittest.mock.MagicMock() + popen.stdout = io.BytesIO(data.encode()) + + with unittest.mock.patch.object(shutil, 'which', + return_value='/sbin/ifconfig'): + with unittest.mock.patch.object(subprocess, 'Popen', + return_value=popen): + mac = uuid._find_mac( + command='ifconfig', + arg='', + hw_identifiers=[b'hwaddr'], + get_index=lambda x: x + 1, + ) + + self.assertEqual(mac, 0x1234567890ab) @unittest.skipUnless(importable('ctypes'), 'requires ctypes') def test_uuid1(self): |