]> granicus.if.org Git - python/commitdiff
Issue #23015: Improved testing of the uuid module.
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 15 Dec 2014 10:06:22 +0000 (12:06 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Mon, 15 Dec 2014 10:06:22 +0000 (12:06 +0200)
1  2 
Lib/test/test_uuid.py

index bc83de2211d58b3040fd628c0b134bf2fc9a23e5,1e8cba30bdfec138925331091bc18e1b19b0d50e..fcb84540c12aaed67ff4d467b4405ca3e3971e2b
@@@ -518,5 -409,97 +410,101 @@@ class TestUUID(unittest.TestCase)
              self.assertNotEqual(parent_value, child_value)
  
  
 -        data = '''\
 -
+ class TestInternals(unittest.TestCase):
+     @unittest.skipUnless(os.name == 'posix', 'requires Posix')
+     def test_find_mac(self):
 -        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)
++        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
+ '''
++
++        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',
++                    args='',
++                    hw_identifiers=[b'hwaddr'],
++                    get_index=lambda x: x + 1,
++                )
++
++        self.assertEqual(mac, 0x1234567890ab)
+     def check_node(self, node, requires=None, network=False):
+         if requires and node is None:
+             self.skipTest('requires ' + requires)
+         hex = '%012x' % node
+         if support.verbose >= 2:
+             print(hex, end=' ')
+         if network:
+             # 47 bit will never be set in IEEE 802 addresses obtained
+             # from network cards.
+             self.assertFalse(node & 0x010000000000, hex)
+         self.assertTrue(0 < node < (1 << 48),
+                         "%s is not an RFC 4122 node ID" % hex)
+     @unittest.skipUnless(os.name == 'posix', 'requires Posix')
+     def test_ifconfig_getnode(self):
+         node = uuid._ifconfig_getnode()
+         self.check_node(node, 'ifconfig', True)
++    @unittest.skipUnless(os.name == 'posix', 'requires Posix')
++    def test_ip_getnode(self):
++        node = uuid._ip_getnode()
++        self.check_node(node, 'ip', True)
++
+     @unittest.skipUnless(os.name == 'posix', 'requires Posix')
+     def test_arp_getnode(self):
+         node = uuid._arp_getnode()
+         self.check_node(node, 'arp', True)
+     @unittest.skipUnless(os.name == 'posix', 'requires Posix')
+     def test_lanscan_getnode(self):
+         node = uuid._lanscan_getnode()
+         self.check_node(node, 'lanscan', True)
+     @unittest.skipUnless(os.name == 'posix', 'requires Posix')
+     def test_netstat_getnode(self):
+         node = uuid._netstat_getnode()
+         self.check_node(node, 'netstat', True)
+     @unittest.skipUnless(os.name == 'nt', 'requires Windows')
+     def test_ipconfig_getnode(self):
+         node = uuid._ipconfig_getnode()
+         self.check_node(node, 'ipconfig', True)
+     @unittest.skipUnless(importable('win32wnet'), 'requires win32wnet')
+     @unittest.skipUnless(importable('netbios'), 'requires netbios')
+     def test_netbios_getnode(self):
+         node = uuid._netbios_getnode()
+         self.check_node(node, network=True)
+     def test_random_getnode(self):
+         node = uuid._random_getnode()
+         # Least significant bit of first octet must be set.
+         self.assertTrue(node & 0x010000000000, '%012x' % node)
+         self.check_node(node)
+     @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.
+             node = uuid._unixdll_getnode()
+         except TypeError:
+             self.skipTest('requires uuid_generate_time')
+         self.check_node(node)
+     @unittest.skipUnless(os.name == 'nt', 'requires Windows')
+     @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
+     def test_windll_getnode(self):
+         node = uuid._windll_getnode()
+         self.check_node(node)
  if __name__ == '__main__':
      unittest.main()