MCPcopy Index your code
hub / github.com/python/cpython / test_hash_randomization

Method test_hash_randomization

Lib/test/test_cmd_line.py:575–610  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

573 self._test_no_stdio(['stdin', 'stdout', 'stderr'])
574
575 def test_hash_randomization(self):
576 # Verify that -R enables hash randomization:
577 self.verify_valid_flag('-R')
578 hashes = []
579 if os.environ.get('PYTHONHASHSEED', 'random') != 'random':
580 env = dict(os.environ) # copy
581 # We need to test that it is enabled by default without
582 # the environment variable enabling it for us.
583 del env['PYTHONHASHSEED']
584 env['__cleanenv'] = '1' # consumed by assert_python_ok()
585 else:
586 env = {}
587 for i in range(3):
588 code = 'print(hash("spam"))'
589 rc, out, err = assert_python_ok('-c', code, **env)
590 self.assertEqual(rc, 0)
591 hashes.append(out)
592 hashes = sorted(set(hashes)) # uniq
593 # Rare chance of failure due to 3 random seeds honestly being equal.
594 self.assertGreater(len(hashes), 1,
595 msg='3 runs produced an identical random hash '
596 ' for "spam": {}'.format(hashes))
597
598 # Verify that sys.flags contains hash_randomization
599 code = 'import sys; print("random is", sys.flags.hash_randomization)'
600 rc, out, err = assert_python_ok('-c', code, PYTHONHASHSEED='')
601 self.assertIn(b'random is 1', out)
602
603 rc, out, err = assert_python_ok('-c', code, PYTHONHASHSEED='random')
604 self.assertIn(b'random is 1', out)
605
606 rc, out, err = assert_python_ok('-c', code, PYTHONHASHSEED='0')
607 self.assertIn(b'random is 0', out)
608
609 rc, out, err = assert_python_ok('-R', '-c', code, PYTHONHASHSEED='0')
610 self.assertIn(b'random is 1', out)
611
612 def test_del___main__(self):
613 # Issue #15001: PyRun_SimpleFileExFlags() did crash because it kept a

Callers

nothing calls this directly

Calls 9

verify_valid_flagMethod · 0.95
assert_python_okFunction · 0.90
setFunction · 0.85
assertGreaterMethod · 0.80
assertInMethod · 0.80
getMethod · 0.45
assertEqualMethod · 0.45
appendMethod · 0.45
formatMethod · 0.45

Tested by

no test coverage detected