Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(267356)

Delta Between Two Patch Sets: Lib/test/test_uuid.py

Issue 16510: Using appropriate checks in tests
Left Patch Set: Created 5 years, 11 months ago
Right Patch Set: Created 5 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_unicode_file.py ('k') | Lib/test/test_weakset.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 from unittest import TestCase 1 import unittest
2 from test import support 2 from test import support
3 import builtins 3 import builtins
4 import io
5 import os
6 import shutil
4 import uuid 7 import uuid
5 8
6 def importable(name): 9 def importable(name):
7 try: 10 try:
8 __import__(name) 11 __import__(name)
9 return True 12 return True
10 except: 13 except:
11 return False 14 return False
12 15
13 class TestUUID(TestCase): 16 class TestUUID(unittest.TestCase):
14 last_node = None 17 last_node = None
15 source2node = {} 18 source2node = {}
16 19
17 def test_UUID(self): 20 def test_UUID(self):
18 equal = self.assertEqual 21 equal = self.assertEqual
19 ascending = [] 22 ascending = []
20 for (string, curly, hex, bytes, bytes_le, fields, integer, urn, 23 for (string, curly, hex, bytes, bytes_le, fields, integer, urn,
21 time, clock_seq, variant, version) in [ 24 time, clock_seq, variant, version) in [
22 ('00000000-0000-0000-0000-000000000000', 25 ('00000000-0000-0000-0000-000000000000',
23 '{00000000-0000-0000-0000-000000000000}', 26 '{00000000-0000-0000-0000-000000000000}',
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 for s, n in TestUUID.source2node.items(): 306 for s, n in TestUUID.source2node.items():
304 msg += " from source %r, node was %012x\n" % (s, n) 307 msg += " from source %r, node was %012x\n" % (s, n)
305 # There's actually no reason to expect the MAC addresses 308 # There's actually no reason to expect the MAC addresses
306 # to agree across various methods -- e.g., a box may have 309 # to agree across various methods -- e.g., a box may have
307 # multiple network interfaces, and different ways of getting 310 # multiple network interfaces, and different ways of getting
308 # a MAC address may favor different HW. 311 # a MAC address may favor different HW.
309 ##self.fail(msg) 312 ##self.fail(msg)
310 else: 313 else:
311 TestUUID.last_node = node 314 TestUUID.last_node = node
312 315
316 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
313 def test_ifconfig_getnode(self): 317 def test_ifconfig_getnode(self):
314 import sys 318 node = uuid._ifconfig_getnode()
315 import os 319 if node is not None:
316 if os.name == 'posix': 320 self.check_node(node, 'ifconfig')
317 node = uuid._ifconfig_getnode() 321
318 if node is not None: 322 @unittest.skipUnless(os.name == 'nt', 'requires Windows')
319 self.check_node(node, 'ifconfig')
320
321 def test_ipconfig_getnode(self): 323 def test_ipconfig_getnode(self):
322 import os 324 node = uuid._ipconfig_getnode()
323 if os.name == 'nt': 325 if node is not None:
324 node = uuid._ipconfig_getnode() 326 self.check_node(node, 'ipconfig')
325 if node is not None: 327
326 self.check_node(node, 'ipconfig') 328 @unittest.skipUnless(importable('win32wnet'), 'requires win32wnet')
327 329 @unittest.skipUnless(importable('netbios'), 'requires netbios')
328 def test_netbios_getnode(self): 330 def test_netbios_getnode(self):
329 if importable('win32wnet') and importable('netbios'): 331 self.check_node(uuid._netbios_getnode(), 'netbios')
330 self.check_node(uuid._netbios_getnode(), 'netbios')
331 332
332 def test_random_getnode(self): 333 def test_random_getnode(self):
333 node = uuid._random_getnode() 334 node = uuid._random_getnode()
334 # Least significant bit of first octet must be set. 335 # Least significant bit of first octet must be set.
335 self.assertTrue(node & 0x010000000000) 336 self.assertTrue(node & 0x010000000000)
336 self.assertLess(node, 1 << 48) 337 self.assertLess(node, 1 << 48)
337 338
339 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
340 @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
338 def test_unixdll_getnode(self): 341 def test_unixdll_getnode(self):
339 import sys 342 try: # Issues 1481, 3581: _uuid_generate_time() might be None.
340 import os 343 self.check_node(uuid._unixdll_getnode(), 'unixdll')
341 if importable('ctypes') and os.name == 'posix': 344 except TypeError:
342 try: # Issues 1481, 3581: _uuid_generate_time() might be None. 345 pass
343 self.check_node(uuid._unixdll_getnode(), 'unixdll') 346
344 except TypeError: 347 @unittest.skipUnless(os.name == 'nt', 'requires Windows')
345 pass 348 @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
346
347 def test_windll_getnode(self): 349 def test_windll_getnode(self):
348 import os 350 self.check_node(uuid._windll_getnode(), 'windll')
349 if importable('ctypes') and os.name == 'nt':
350 self.check_node(uuid._windll_getnode(), 'windll')
351 351
352 def test_getnode(self): 352 def test_getnode(self):
353 import sys
354 node1 = uuid.getnode() 353 node1 = uuid.getnode()
355 self.check_node(node1, "getnode1") 354 self.check_node(node1, "getnode1")
356 355
357 # Test it again to ensure consistency. 356 # Test it again to ensure consistency.
358 node2 = uuid.getnode() 357 node2 = uuid.getnode()
359 self.check_node(node2, "getnode2") 358 self.check_node(node2, "getnode2")
360 359
361 self.assertEqual(node1, node2) 360 self.assertEqual(node1, node2)
362 361
362 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
363 def test_find_mac(self):
364 data = '''\
365
366 fake hwaddr
367 cscotun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 -00
368 eth0 Link encap:Ethernet HWaddr 12:34:56:78:90:ab
369 '''
370 def mock_popen(cmd):
371 return io.StringIO(data)
372
373 if shutil.which('ifconfig') is None:
374 path = os.pathsep.join(('/sbin', '/usr/sbin'))
375 if shutil.which('ifconfig', path=path) is None:
376 self.skipTest('requires ifconfig')
377
378 with support.swap_attr(os, 'popen', mock_popen):
379 mac = uuid._find_mac(
380 command='ifconfig',
381 args='',
382 hw_identifiers=['hwaddr'],
383 get_index=lambda x: x + 1,
384 )
385 self.assertEqual(mac, 0x1234567890ab)
386
387 @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
363 def test_uuid1(self): 388 def test_uuid1(self):
364 # uuid1 requires ctypes.
365 try:
366 import ctypes
367 except ImportError:
368 return
369
370 equal = self.assertEqual 389 equal = self.assertEqual
371 390
372 # Make sure uuid1() generates UUIDs that are actually version 1. 391 # Make sure uuid1() generates UUIDs that are actually version 1.
373 for u in [uuid.uuid1() for i in range(10)]: 392 for u in [uuid.uuid1() for i in range(10)]:
374 equal(u.variant, uuid.RFC_4122) 393 equal(u.variant, uuid.RFC_4122)
375 equal(u.version, 1) 394 equal(u.version, 1)
376 395
377 # Make sure the generated UUIDs are actually unique. 396 # Make sure the generated UUIDs are actually unique.
378 uuids = {} 397 uuids = {}
379 for u in [uuid.uuid1() for i in range(1000)]: 398 for u in [uuid.uuid1() for i in range(1000)]:
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
412 (uuid.uuid3(uuid.NAMESPACE_OID, '1.3.6.1'), 431 (uuid.uuid3(uuid.NAMESPACE_OID, '1.3.6.1'),
413 'dd1a1cef-13d5-368a-ad82-eca71acd4cd1'), 432 'dd1a1cef-13d5-368a-ad82-eca71acd4cd1'),
414 (uuid.uuid3(uuid.NAMESPACE_X500, 'c=ca'), 433 (uuid.uuid3(uuid.NAMESPACE_X500, 'c=ca'),
415 '658d3002-db6b-3040-a1d1-8ddd7d189a4d'), 434 '658d3002-db6b-3040-a1d1-8ddd7d189a4d'),
416 ]: 435 ]:
417 equal(u.variant, uuid.RFC_4122) 436 equal(u.variant, uuid.RFC_4122)
418 equal(u.version, 3) 437 equal(u.version, 3)
419 equal(u, uuid.UUID(v)) 438 equal(u, uuid.UUID(v))
420 equal(str(u), v) 439 equal(str(u), v)
421 440
441 @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
422 def test_uuid4(self): 442 def test_uuid4(self):
423 # uuid4 requires ctypes.
424 try:
425 import ctypes
426 except ImportError:
427 return
428
429 equal = self.assertEqual 443 equal = self.assertEqual
430 444
431 # Make sure uuid4() generates UUIDs that are actually version 4. 445 # Make sure uuid4() generates UUIDs that are actually version 4.
432 for u in [uuid.uuid4() for i in range(10)]: 446 for u in [uuid.uuid4() for i in range(10)]:
433 equal(u.variant, uuid.RFC_4122) 447 equal(u.variant, uuid.RFC_4122)
434 equal(u.version, 4) 448 equal(u.version, 4)
435 449
436 # Make sure the generated UUIDs are actually unique. 450 # Make sure the generated UUIDs are actually unique.
437 uuids = {} 451 uuids = {}
438 for u in [uuid.uuid4() for i in range(1000)]: 452 for u in [uuid.uuid4() for i in range(1000)]:
(...skipping 11 matching lines...) Expand all
450 (uuid.uuid5(uuid.NAMESPACE_OID, '1.3.6.1'), 464 (uuid.uuid5(uuid.NAMESPACE_OID, '1.3.6.1'),
451 '1447fa61-5277-5fef-a9b3-fbc6e44f4af3'), 465 '1447fa61-5277-5fef-a9b3-fbc6e44f4af3'),
452 (uuid.uuid5(uuid.NAMESPACE_X500, 'c=ca'), 466 (uuid.uuid5(uuid.NAMESPACE_X500, 'c=ca'),
453 'cc957dd1-a972-5349-98cd-874190002798'), 467 'cc957dd1-a972-5349-98cd-874190002798'),
454 ]: 468 ]:
455 equal(u.variant, uuid.RFC_4122) 469 equal(u.variant, uuid.RFC_4122)
456 equal(u.version, 5) 470 equal(u.version, 5)
457 equal(u, uuid.UUID(v)) 471 equal(u, uuid.UUID(v))
458 equal(str(u), v) 472 equal(str(u), v)
459 473
474 @unittest.skipUnless(os.name == 'posix', 'requires Posix')
460 def testIssue8621(self): 475 def testIssue8621(self):
461 import os
462 import sys
463 if os.name != 'posix':
464 return
465
466 # On at least some versions of OSX uuid.uuid4 generates 476 # On at least some versions of OSX uuid.uuid4 generates
467 # the same sequence of UUIDs in the parent and any 477 # the same sequence of UUIDs in the parent and any
468 # children started using fork. 478 # children started using fork.
469 fds = os.pipe() 479 fds = os.pipe()
470 pid = os.fork() 480 pid = os.fork()
471 if pid == 0: 481 if pid == 0:
472 os.close(fds[0]) 482 os.close(fds[0])
473 value = uuid.uuid4() 483 value = uuid.uuid4()
474 os.write(fds[1], value.hex.encode('latin-1')) 484 os.write(fds[1], value.hex.encode('latin-1'))
475 os._exit(0) 485 os._exit(0)
476 486
477 else: 487 else:
478 os.close(fds[1]) 488 os.close(fds[1])
489 self.addCleanup(os.close, fds[0])
479 parent_value = uuid.uuid4().hex 490 parent_value = uuid.uuid4().hex
480 os.waitpid(pid, 0) 491 os.waitpid(pid, 0)
481 child_value = os.read(fds[0], 100).decode('latin-1') 492 child_value = os.read(fds[0], 100).decode('latin-1')
482 493
483 self.assertNotEqual(parent_value, child_value) 494 self.assertNotEqual(parent_value, child_value)
484 495
485 496
486
487
488
489 def test_main():
490 support.run_unittest(TestUUID)
491
492 if __name__ == '__main__': 497 if __name__ == '__main__':
493 test_main() 498 unittest.main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+