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

Side by Side Diff: Lib/test/test_os.py

Issue 27778: PEP 524: Add os.getrandom()
Patch Set: Created 3 years, 1 month 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:
View unified diff | Download patch
« no previous file with comments | « no previous file | Modules/clinic/posixmodule.c.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # As a test suite for the os module, this is woefully inadequate, but this 1 # As a test suite for the os module, this is woefully inadequate, but this
2 # does add tests for a few functions which have been determined to be more 2 # does add tests for a few functions which have been determined to be more
3 # portable than they had been thought to be. 3 # portable than they had been thought to be.
4 4
5 import asynchat 5 import asynchat
6 import asyncore 6 import asyncore
7 import codecs 7 import codecs
8 import contextlib 8 import contextlib
9 import decimal 9 import decimal
10 import errno 10 import errno
(...skipping 1210 matching lines...) Expand 10 before | Expand all | Expand 10 after
1221 class URandomTests(unittest.TestCase): 1221 class URandomTests(unittest.TestCase):
1222 def test_urandom_length(self): 1222 def test_urandom_length(self):
1223 self.assertEqual(len(os.urandom(0)), 0) 1223 self.assertEqual(len(os.urandom(0)), 0)
1224 self.assertEqual(len(os.urandom(1)), 1) 1224 self.assertEqual(len(os.urandom(1)), 1)
1225 self.assertEqual(len(os.urandom(10)), 10) 1225 self.assertEqual(len(os.urandom(10)), 10)
1226 self.assertEqual(len(os.urandom(100)), 100) 1226 self.assertEqual(len(os.urandom(100)), 100)
1227 self.assertEqual(len(os.urandom(1000)), 1000) 1227 self.assertEqual(len(os.urandom(1000)), 1000)
1228 1228
1229 def test_urandom_value(self): 1229 def test_urandom_value(self):
1230 data1 = os.urandom(16) 1230 data1 = os.urandom(16)
1231 self.assertIsInstance(data1, bytes)
1231 data2 = os.urandom(16) 1232 data2 = os.urandom(16)
1232 self.assertNotEqual(data1, data2) 1233 self.assertNotEqual(data1, data2)
1233 1234
1234 def get_urandom_subprocess(self, count): 1235 def get_urandom_subprocess(self, count):
1235 code = '\n'.join(( 1236 code = '\n'.join((
1236 'import os, sys', 1237 'import os, sys',
1237 'data = os.urandom(%s)' % count, 1238 'data = os.urandom(%s)' % count,
1238 'sys.stdout.buffer.write(data)', 1239 'sys.stdout.buffer.write(data)',
1239 'sys.stdout.buffer.flush()')) 1240 'sys.stdout.buffer.flush()'))
1240 out = assert_python_ok('-c', code) 1241 out = assert_python_ok('-c', code)
1241 stdout = out[1] 1242 stdout = out[1]
1242 self.assertEqual(len(stdout), 16) 1243 self.assertEqual(len(stdout), 16)
1243 return stdout 1244 return stdout
1244 1245
1245 def test_urandom_subprocess(self): 1246 def test_urandom_subprocess(self):
1246 data1 = self.get_urandom_subprocess(16) 1247 data1 = self.get_urandom_subprocess(16)
1247 data2 = self.get_urandom_subprocess(16) 1248 data2 = self.get_urandom_subprocess(16)
1248 self.assertNotEqual(data1, data2) 1249 self.assertNotEqual(data1, data2)
1250
1251
1252 @unittest.skipUnless(hasattr(os, 'getrandom'), 'need os.getrandom()')
1253 class GetRandomTests(unittest.TestCase):
1254 def test_getrandom_type(self):
1255 data = os.getrandom(16)
1256 self.assertIsInstance(data, bytes)
1257 self.assertEqual(len(data), 16)
1258
1259 def test_getrandom0(self):
1260 empty = os.getrandom(0)
1261 self.assertEqual(empty, b'')
1262
1263 def test_dev_random(self):
1264 self.assertTrue(hasattr(os, 'GRND_RANDOM'))
1265
1266 # Don't test os.getrandom(1, os.GRND_RANDOM) to not consume the rare
1267 # resource /dev/random
Nick Coghlan 2016/08/19 02:35:25 Re-reviewing this, I think it's fine to take a sin
1268
1269 def test_getrandom_nonblock(self):
1270 # The call must not fail. Check also that the flag exists
Nick Coghlan 2016/08/18 14:23:22 This comment is a little confusing, since "must no
haypo 2016/08/18 18:34:59 You're right. I hesitated to add the "except Block
Nick Coghlan 2016/08/19 02:35:25 Yeah, that would make sense - it tests the flag is
1271 try:
1272 os.getrandom(1, os.GRND_NONBLOCK)
1273 except BlockingIOError:
1274 # System urandom is not initialized yet
1275 pass
1276
1277 def test_getrandom_value(self):
1278 data1 = os.getrandom(16)
1279 data2 = os.getrandom(16)
1280 self.assertNotEqual(data1, data2)
1281
1249 1282
1250 1283
1251 # os.urandom() doesn't use a file descriptor when it is implemented with the 1284 # os.urandom() doesn't use a file descriptor when it is implemented with the
1252 # getentropy() function, the getrandom() function or the getrandom() syscall 1285 # getentropy() function, the getrandom() function or the getrandom() syscall
1253 OS_URANDOM_DONT_USE_FD = ( 1286 OS_URANDOM_DONT_USE_FD = (
1254 sysconfig.get_config_var('HAVE_GETENTROPY') == 1 1287 sysconfig.get_config_var('HAVE_GETENTROPY') == 1
1255 or sysconfig.get_config_var('HAVE_GETRANDOM') == 1 1288 or sysconfig.get_config_var('HAVE_GETRANDOM') == 1
1256 or sysconfig.get_config_var('HAVE_GETRANDOM_SYSCALL') == 1) 1289 or sysconfig.get_config_var('HAVE_GETRANDOM_SYSCALL') == 1)
1257 1290
1258 @unittest.skipIf(OS_URANDOM_DONT_USE_FD , 1291 @unittest.skipIf(OS_URANDOM_DONT_USE_FD ,
(...skipping 1923 matching lines...) Expand 10 before | Expand all | Expand 10 after
3182 if hasattr(os, "_fspath"): 3215 if hasattr(os, "_fspath"):
3183 class TestPEP519PurePython(TestPEP519): 3216 class TestPEP519PurePython(TestPEP519):
3184 3217
3185 """Explicitly test the pure Python implementation of os.fspath().""" 3218 """Explicitly test the pure Python implementation of os.fspath()."""
3186 3219
3187 fspath = staticmethod(os._fspath) 3220 fspath = staticmethod(os._fspath)
3188 3221
3189 3222
3190 if __name__ == "__main__": 3223 if __name__ == "__main__":
3191 unittest.main() 3224 unittest.main()
OLDNEW
« no previous file with comments | « no previous file | Modules/clinic/posixmodule.c.h » ('j') | no next file with comments »

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