- Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED

environment variable, to provide an opt-in way to protect against denial of
  service attacks due to hash collisions within the dict and set types.  Patch
  by David Malcolm, based on work by Victor Stinner.
This commit is contained in:
Barry Warsaw 2012-02-20 20:42:21 -05:00
parent f5a5beb339
commit 1e13eb084f
27 changed files with 706 additions and 152 deletions

View file

@ -6,6 +6,8 @@ import os
import unittest
import warnings
import sys
import subprocess
from test import test_support
warnings.filterwarnings("ignore", "tempnam", RuntimeWarning, __name__)
@ -499,18 +501,46 @@ class DevNullTests (unittest.TestCase):
class URandomTests (unittest.TestCase):
def test_urandom(self):
try:
with test_support.check_warnings():
self.assertEqual(len(os.urandom(1)), 1)
self.assertEqual(len(os.urandom(10)), 10)
self.assertEqual(len(os.urandom(100)), 100)
self.assertEqual(len(os.urandom(1000)), 1000)
# see http://bugs.python.org/issue3708
self.assertEqual(len(os.urandom(0.9)), 0)
self.assertEqual(len(os.urandom(1.1)), 1)
self.assertEqual(len(os.urandom(2.0)), 2)
except NotImplementedError:
pass
with test_support.check_warnings():
self.assertEqual(len(os.urandom(1)), 1)
self.assertEqual(len(os.urandom(10)), 10)
self.assertEqual(len(os.urandom(100)), 100)
self.assertEqual(len(os.urandom(1000)), 1000)
# see http://bugs.python.org/issue3708
self.assertEqual(len(os.urandom(0.9)), 0)
self.assertEqual(len(os.urandom(1.1)), 1)
self.assertEqual(len(os.urandom(2.0)), 2)
def test_urandom_length(self):
self.assertEqual(len(os.urandom(0)), 0)
self.assertEqual(len(os.urandom(1)), 1)
self.assertEqual(len(os.urandom(10)), 10)
self.assertEqual(len(os.urandom(100)), 100)
self.assertEqual(len(os.urandom(1000)), 1000)
def test_urandom_value(self):
data1 = os.urandom(16)
data2 = os.urandom(16)
self.assertNotEqual(data1, data2)
def get_urandom_subprocess(self, count):
code = '\n'.join((
'import os, sys',
'data = os.urandom(%s)' % count,
'sys.stdout.write(data)',
'sys.stdout.flush()'))
cmd_line = [sys.executable, '-c', code]
p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
out, err = p.communicate()
out = test_support.strip_python_stderr(out)
self.assertEqual(len(out), count)
return out
def test_urandom_subprocess(self):
data1 = self.get_urandom_subprocess(16)
data2 = self.get_urandom_subprocess(16)
self.assertNotEqual(data1, data2)
class Win32ErrorTests(unittest.TestCase):
def test_rename(self):