diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2014-04-26 12:33:03 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2014-04-26 12:33:03 (GMT) |
commit | e472aeafc3ce2c295fed8aee1ccdad1dcaaf634e (patch) | |
tree | c1a8df3bbc0a3fd89e37a2e6645d9bc115dc8c37 /Lib/test/test_os.py | |
parent | 86fe53e976435c22c6c73203c84fe9f1e7beaf4b (diff) | |
download | cpython-e472aeafc3ce2c295fed8aee1ccdad1dcaaf634e.zip cpython-e472aeafc3ce2c295fed8aee1ccdad1dcaaf634e.tar.gz cpython-e472aeafc3ce2c295fed8aee1ccdad1dcaaf634e.tar.bz2 |
Issue #21207: Detect when the os.urandom cached fd has been closed or replaced, and open it anew.
Diffstat (limited to 'Lib/test/test_os.py')
-rw-r--r-- | Lib/test/test_os.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index f744815..e129bef 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -1070,6 +1070,49 @@ class URandomTests(unittest.TestCase): """ assert_python_ok('-c', code) + def test_urandom_fd_closed(self): + # Issue #21207: urandom() should reopen its fd to /dev/urandom if + # closed. + code = """if 1: + import os + import sys + os.urandom(4) + os.closerange(3, 256) + sys.stdout.buffer.write(os.urandom(4)) + """ + rc, out, err = assert_python_ok('-Sc', code) + + def test_urandom_fd_reopened(self): + # Issue #21207: urandom() should detect its fd to /dev/urandom + # changed to something else, and reopen it. + with open(support.TESTFN, 'wb') as f: + f.write(b"x" * 256) + self.addCleanup(os.unlink, support.TESTFN) + code = """if 1: + import os + import sys + os.urandom(4) + for fd in range(3, 256): + try: + os.close(fd) + except OSError: + pass + else: + # Found the urandom fd (XXX hopefully) + break + os.closerange(3, 256) + with open({TESTFN!r}, 'rb') as f: + os.dup2(f.fileno(), fd) + sys.stdout.buffer.write(os.urandom(4)) + sys.stdout.buffer.write(os.urandom(4)) + """.format(TESTFN=support.TESTFN) + rc, out, err = assert_python_ok('-Sc', code) + self.assertEqual(len(out), 8) + self.assertNotEqual(out[0:4], out[4:8]) + rc, out2, err2 = assert_python_ok('-Sc', code) + self.assertEqual(len(out2), 8) + self.assertNotEqual(out2, out) + @contextlib.contextmanager def _execvpe_mockup(defpath=None): |