blob: 55077354589df988e2fb659e4052dd91aa9321c4 [file] [log] [blame]
Neal Norwitze241ce82003-02-17 18:17:05 +00001"Test posix functions"
2
Benjamin Petersonee8712c2008-05-20 21:35:26 +00003from test import support
Antoine Pitrou346cbd32017-05-27 17:50:54 +02004from test.support.script_helper import assert_python_ok
R. David Murrayeb3615d2009-04-22 02:24:39 +00005
6# Skip these tests if there is no posix module.
7posix = support.import_module('posix')
Neal Norwitze241ce82003-02-17 18:17:05 +00008
Antoine Pitroub7572f02009-12-02 20:46:48 +00009import errno
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +000010import sys
Neal Norwitze241ce82003-02-17 18:17:05 +000011import time
12import os
Charles-François Nataliab2d58e2012-04-17 19:48:35 +020013import platform
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000014import pwd
Benjamin Peterson052a02b2010-08-17 01:27:09 +000015import stat
Ned Deilyba2eab22011-07-26 13:53:55 -070016import tempfile
Neal Norwitze241ce82003-02-17 18:17:05 +000017import unittest
18import warnings
R. David Murraya21e4ca2009-03-31 23:16:50 +000019
Ned Deilyba2eab22011-07-26 13:53:55 -070020_DUMMY_SYMLINK = os.path.join(tempfile.gettempdir(),
21 support.TESTFN + '-dummy-symlink')
Neal Norwitze241ce82003-02-17 18:17:05 +000022
Miss Islington (bot)3e4b6882018-07-31 02:20:06 -070023requires_32b = unittest.skipUnless(sys.maxsize < 2**32,
24 'test is only meaningful on 32-bit builds')
25
Benjamin Peterson0aef9092018-09-12 16:00:06 -070026def _supports_sched():
27 if not hasattr(posix, 'sched_getscheduler'):
28 return False
29 try:
30 posix.sched_getscheduler(0)
31 except OSError as e:
32 if e.errno == errno.ENOSYS:
33 return False
34 return True
35
36requires_sched = unittest.skipUnless(_supports_sched(), 'requires POSIX scheduler API')
37
Neal Norwitze241ce82003-02-17 18:17:05 +000038class PosixTester(unittest.TestCase):
39
40 def setUp(self):
41 # create empty file
Benjamin Petersonee8712c2008-05-20 21:35:26 +000042 fp = open(support.TESTFN, 'w+')
Neal Norwitze241ce82003-02-17 18:17:05 +000043 fp.close()
Ned Deily3eb67d52011-06-28 00:00:28 -070044 self.teardown_files = [ support.TESTFN ]
Brett Cannonc8d502e2010-03-20 21:53:28 +000045 self._warnings_manager = support.check_warnings()
46 self._warnings_manager.__enter__()
47 warnings.filterwarnings('ignore', '.* potential security risk .*',
48 RuntimeWarning)
Neal Norwitze241ce82003-02-17 18:17:05 +000049
50 def tearDown(self):
Ned Deily3eb67d52011-06-28 00:00:28 -070051 for teardown_file in self.teardown_files:
52 support.unlink(teardown_file)
Brett Cannonc8d502e2010-03-20 21:53:28 +000053 self._warnings_manager.__exit__(None, None, None)
Neal Norwitze241ce82003-02-17 18:17:05 +000054
55 def testNoArgFunctions(self):
56 # test posix functions which take no arguments and have
57 # no side-effects which we need to cleanup (e.g., fork, wait, abort)
Guido van Rossumf0af3e32008-10-02 18:55:37 +000058 NO_ARG_FUNCTIONS = [ "ctermid", "getcwd", "getcwdb", "uname",
Guido van Rossum687b9c02007-10-25 23:18:51 +000059 "times", "getloadavg",
Neal Norwitze241ce82003-02-17 18:17:05 +000060 "getegid", "geteuid", "getgid", "getgroups",
Ross Lagerwall7807c352011-03-17 20:20:30 +020061 "getpid", "getpgrp", "getppid", "getuid", "sync",
Neal Norwitze241ce82003-02-17 18:17:05 +000062 ]
Neal Norwitz71b13e82003-02-23 22:12:24 +000063
Neal Norwitze241ce82003-02-17 18:17:05 +000064 for name in NO_ARG_FUNCTIONS:
65 posix_func = getattr(posix, name, None)
66 if posix_func is not None:
67 posix_func()
Neal Norwitz2ff51a82003-02-17 22:40:31 +000068 self.assertRaises(TypeError, posix_func, 1)
Neal Norwitze241ce82003-02-17 18:17:05 +000069
Serhiy Storchaka43767632013-11-03 21:31:38 +020070 @unittest.skipUnless(hasattr(posix, 'getresuid'),
71 'test needs posix.getresuid()')
72 def test_getresuid(self):
73 user_ids = posix.getresuid()
74 self.assertEqual(len(user_ids), 3)
75 for val in user_ids:
76 self.assertGreaterEqual(val, 0)
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000077
Serhiy Storchaka43767632013-11-03 21:31:38 +020078 @unittest.skipUnless(hasattr(posix, 'getresgid'),
79 'test needs posix.getresgid()')
80 def test_getresgid(self):
81 group_ids = posix.getresgid()
82 self.assertEqual(len(group_ids), 3)
83 for val in group_ids:
84 self.assertGreaterEqual(val, 0)
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000085
Serhiy Storchaka43767632013-11-03 21:31:38 +020086 @unittest.skipUnless(hasattr(posix, 'setresuid'),
87 'test needs posix.setresuid()')
88 def test_setresuid(self):
89 current_user_ids = posix.getresuid()
90 self.assertIsNone(posix.setresuid(*current_user_ids))
91 # -1 means don't change that value.
92 self.assertIsNone(posix.setresuid(-1, -1, -1))
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000093
Serhiy Storchaka43767632013-11-03 21:31:38 +020094 @unittest.skipUnless(hasattr(posix, 'setresuid'),
95 'test needs posix.setresuid()')
96 def test_setresuid_exception(self):
97 # Don't do this test if someone is silly enough to run us as root.
98 current_user_ids = posix.getresuid()
99 if 0 not in current_user_ids:
100 new_user_ids = (current_user_ids[0]+1, -1, -1)
101 self.assertRaises(OSError, posix.setresuid, *new_user_ids)
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000102
Serhiy Storchaka43767632013-11-03 21:31:38 +0200103 @unittest.skipUnless(hasattr(posix, 'setresgid'),
104 'test needs posix.setresgid()')
105 def test_setresgid(self):
106 current_group_ids = posix.getresgid()
107 self.assertIsNone(posix.setresgid(*current_group_ids))
108 # -1 means don't change that value.
109 self.assertIsNone(posix.setresgid(-1, -1, -1))
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000110
Serhiy Storchaka43767632013-11-03 21:31:38 +0200111 @unittest.skipUnless(hasattr(posix, 'setresgid'),
112 'test needs posix.setresgid()')
113 def test_setresgid_exception(self):
114 # Don't do this test if someone is silly enough to run us as root.
115 current_group_ids = posix.getresgid()
116 if 0 not in current_group_ids:
117 new_group_ids = (current_group_ids[0]+1, -1, -1)
118 self.assertRaises(OSError, posix.setresgid, *new_group_ids)
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000119
Antoine Pitroub7572f02009-12-02 20:46:48 +0000120 @unittest.skipUnless(hasattr(posix, 'initgroups'),
121 "test needs os.initgroups()")
122 def test_initgroups(self):
123 # It takes a string and an integer; check that it raises a TypeError
124 # for other argument lists.
125 self.assertRaises(TypeError, posix.initgroups)
126 self.assertRaises(TypeError, posix.initgroups, None)
127 self.assertRaises(TypeError, posix.initgroups, 3, "foo")
128 self.assertRaises(TypeError, posix.initgroups, "foo", 3, object())
129
130 # If a non-privileged user invokes it, it should fail with OSError
131 # EPERM.
132 if os.getuid() != 0:
Charles-François Natalie8a255a2012-05-02 20:01:38 +0200133 try:
134 name = pwd.getpwuid(posix.getuid()).pw_name
135 except KeyError:
136 # the current UID may not have a pwd entry
137 raise unittest.SkipTest("need a pwd entry")
Antoine Pitroub7572f02009-12-02 20:46:48 +0000138 try:
139 posix.initgroups(name, 13)
140 except OSError as e:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000141 self.assertEqual(e.errno, errno.EPERM)
Antoine Pitroub7572f02009-12-02 20:46:48 +0000142 else:
143 self.fail("Expected OSError to be raised by initgroups")
144
Serhiy Storchaka43767632013-11-03 21:31:38 +0200145 @unittest.skipUnless(hasattr(posix, 'statvfs'),
146 'test needs posix.statvfs()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000147 def test_statvfs(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200148 self.assertTrue(posix.statvfs(os.curdir))
Neal Norwitze241ce82003-02-17 18:17:05 +0000149
Serhiy Storchaka43767632013-11-03 21:31:38 +0200150 @unittest.skipUnless(hasattr(posix, 'fstatvfs'),
151 'test needs posix.fstatvfs()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000152 def test_fstatvfs(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200153 fp = open(support.TESTFN)
154 try:
155 self.assertTrue(posix.fstatvfs(fp.fileno()))
156 self.assertTrue(posix.statvfs(fp.fileno()))
157 finally:
158 fp.close()
Neal Norwitze241ce82003-02-17 18:17:05 +0000159
Serhiy Storchaka43767632013-11-03 21:31:38 +0200160 @unittest.skipUnless(hasattr(posix, 'ftruncate'),
161 'test needs posix.ftruncate()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000162 def test_ftruncate(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200163 fp = open(support.TESTFN, 'w+')
164 try:
165 # we need to have some data to truncate
166 fp.write('test')
167 fp.flush()
168 posix.ftruncate(fp.fileno(), 0)
169 finally:
170 fp.close()
Neal Norwitze241ce82003-02-17 18:17:05 +0000171
Ross Lagerwall7807c352011-03-17 20:20:30 +0200172 @unittest.skipUnless(hasattr(posix, 'truncate'), "test needs posix.truncate()")
173 def test_truncate(self):
174 with open(support.TESTFN, 'w') as fp:
175 fp.write('test')
176 fp.flush()
177 posix.truncate(support.TESTFN, 0)
178
Larry Hastings9cf065c2012-06-22 16:30:09 -0700179 @unittest.skipUnless(getattr(os, 'execve', None) in os.supports_fd, "test needs execve() to support the fd parameter")
Ross Lagerwall7807c352011-03-17 20:20:30 +0200180 @unittest.skipUnless(hasattr(os, 'fork'), "test needs os.fork()")
Ross Lagerwalldedf6cf2011-03-20 18:27:05 +0200181 @unittest.skipUnless(hasattr(os, 'waitpid'), "test needs os.waitpid()")
Ross Lagerwall7807c352011-03-17 20:20:30 +0200182 def test_fexecve(self):
183 fp = os.open(sys.executable, os.O_RDONLY)
184 try:
185 pid = os.fork()
186 if pid == 0:
187 os.chdir(os.path.split(sys.executable)[0])
Larry Hastings9cf065c2012-06-22 16:30:09 -0700188 posix.execve(fp, [sys.executable, '-c', 'pass'], os.environ)
Ross Lagerwall7807c352011-03-17 20:20:30 +0200189 else:
Ross Lagerwalldedf6cf2011-03-20 18:27:05 +0200190 self.assertEqual(os.waitpid(pid, 0), (pid, 0))
Ross Lagerwall7807c352011-03-17 20:20:30 +0200191 finally:
192 os.close(fp)
193
Pablo Galindo6c6ddf92018-01-29 01:56:10 +0000194
Ross Lagerwall7807c352011-03-17 20:20:30 +0200195 @unittest.skipUnless(hasattr(posix, 'waitid'), "test needs posix.waitid()")
196 @unittest.skipUnless(hasattr(os, 'fork'), "test needs os.fork()")
197 def test_waitid(self):
198 pid = os.fork()
199 if pid == 0:
200 os.chdir(os.path.split(sys.executable)[0])
201 posix.execve(sys.executable, [sys.executable, '-c', 'pass'], os.environ)
202 else:
203 res = posix.waitid(posix.P_PID, pid, posix.WEXITED)
204 self.assertEqual(pid, res.si_pid)
205
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200206 @unittest.skipUnless(hasattr(os, 'fork'), "test needs os.fork()")
Gregory P. Smith163468a2017-05-29 10:03:41 -0700207 def test_register_at_fork(self):
208 with self.assertRaises(TypeError, msg="Positional args not allowed"):
209 os.register_at_fork(lambda: None)
210 with self.assertRaises(TypeError, msg="Args must be callable"):
211 os.register_at_fork(before=2)
212 with self.assertRaises(TypeError, msg="Args must be callable"):
213 os.register_at_fork(after_in_child="three")
214 with self.assertRaises(TypeError, msg="Args must be callable"):
215 os.register_at_fork(after_in_parent=b"Five")
216 with self.assertRaises(TypeError, msg="Args must not be None"):
217 os.register_at_fork(before=None)
218 with self.assertRaises(TypeError, msg="Args must not be None"):
219 os.register_at_fork(after_in_child=None)
220 with self.assertRaises(TypeError, msg="Args must not be None"):
221 os.register_at_fork(after_in_parent=None)
222 with self.assertRaises(TypeError, msg="Invalid arg was allowed"):
223 # Ensure a combination of valid and invalid is an error.
224 os.register_at_fork(before=None, after_in_parent=lambda: 3)
225 with self.assertRaises(TypeError, msg="Invalid arg was allowed"):
226 # Ensure a combination of valid and invalid is an error.
227 os.register_at_fork(before=lambda: None, after_in_child='')
228 # We test actual registrations in their own process so as not to
229 # pollute this one. There is no way to unregister for cleanup.
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200230 code = """if 1:
231 import os
232
233 r, w = os.pipe()
234 fin_r, fin_w = os.pipe()
235
Gregory P. Smith163468a2017-05-29 10:03:41 -0700236 os.register_at_fork(before=lambda: os.write(w, b'A'))
237 os.register_at_fork(after_in_parent=lambda: os.write(w, b'C'))
238 os.register_at_fork(after_in_child=lambda: os.write(w, b'E'))
239 os.register_at_fork(before=lambda: os.write(w, b'B'),
240 after_in_parent=lambda: os.write(w, b'D'),
241 after_in_child=lambda: os.write(w, b'F'))
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200242
243 pid = os.fork()
244 if pid == 0:
245 # At this point, after-forkers have already been executed
246 os.close(w)
247 # Wait for parent to tell us to exit
248 os.read(fin_r, 1)
249 os._exit(0)
250 else:
251 try:
252 os.close(w)
253 with open(r, "rb") as f:
254 data = f.read()
255 assert len(data) == 6, data
256 # Check before-fork callbacks
257 assert data[:2] == b'BA', data
258 # Check after-fork callbacks
259 assert sorted(data[2:]) == list(b'CDEF'), data
260 assert data.index(b'C') < data.index(b'D'), data
261 assert data.index(b'E') < data.index(b'F'), data
262 finally:
263 os.write(fin_w, b'!')
264 """
265 assert_python_ok('-c', code)
266
Ross Lagerwall7807c352011-03-17 20:20:30 +0200267 @unittest.skipUnless(hasattr(posix, 'lockf'), "test needs posix.lockf()")
268 def test_lockf(self):
269 fd = os.open(support.TESTFN, os.O_WRONLY | os.O_CREAT)
270 try:
271 os.write(fd, b'test')
272 os.lseek(fd, 0, os.SEEK_SET)
273 posix.lockf(fd, posix.F_LOCK, 4)
274 # section is locked
275 posix.lockf(fd, posix.F_ULOCK, 4)
276 finally:
277 os.close(fd)
278
279 @unittest.skipUnless(hasattr(posix, 'pread'), "test needs posix.pread()")
280 def test_pread(self):
281 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
282 try:
283 os.write(fd, b'test')
284 os.lseek(fd, 0, os.SEEK_SET)
285 self.assertEqual(b'es', posix.pread(fd, 2, 1))
Florent Xiclunae41f0de2011-11-11 19:39:25 +0100286 # the first pread() shouldn't disturb the file offset
Ross Lagerwall7807c352011-03-17 20:20:30 +0200287 self.assertEqual(b'te', posix.read(fd, 2))
288 finally:
289 os.close(fd)
290
Pablo Galindo4defba32018-01-27 16:16:37 +0000291 @unittest.skipUnless(hasattr(posix, 'preadv'), "test needs posix.preadv()")
292 def test_preadv(self):
293 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
294 try:
295 os.write(fd, b'test1tt2t3t5t6t6t8')
296 buf = [bytearray(i) for i in [5, 3, 2]]
297 self.assertEqual(posix.preadv(fd, buf, 3), 10)
298 self.assertEqual([b't1tt2', b't3t', b'5t'], list(buf))
299 finally:
300 os.close(fd)
301
Miss Islington (bot)3e4b6882018-07-31 02:20:06 -0700302 @unittest.skipUnless(hasattr(posix, 'preadv'), "test needs posix.preadv()")
Pablo Galindo4defba32018-01-27 16:16:37 +0000303 @unittest.skipUnless(hasattr(posix, 'RWF_HIPRI'), "test needs posix.RWF_HIPRI")
304 def test_preadv_flags(self):
305 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
306 try:
307 os.write(fd, b'test1tt2t3t5t6t6t8')
308 buf = [bytearray(i) for i in [5, 3, 2]]
309 self.assertEqual(posix.preadv(fd, buf, 3, os.RWF_HIPRI), 10)
310 self.assertEqual([b't1tt2', b't3t', b'5t'], list(buf))
Miss Islington (bot)d28aaa72019-04-13 09:25:20 -0700311 except OSError as inst:
312 # Is possible that the macro RWF_HIPRI was defined at compilation time
313 # but the option is not supported by the kernel or the runtime libc shared
314 # library.
315 if inst.errno in {errno.EINVAL, errno.ENOTSUP}:
316 raise unittest.SkipTest("RWF_HIPRI is not supported by the current system")
317 else:
318 raise
Pablo Galindo4defba32018-01-27 16:16:37 +0000319 finally:
320 os.close(fd)
321
Miss Islington (bot)3e4b6882018-07-31 02:20:06 -0700322 @unittest.skipUnless(hasattr(posix, 'preadv'), "test needs posix.preadv()")
323 @requires_32b
324 def test_preadv_overflow_32bits(self):
325 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
326 try:
327 buf = [bytearray(2**16)] * 2**15
328 with self.assertRaises(OSError) as cm:
329 os.preadv(fd, buf, 0)
330 self.assertEqual(cm.exception.errno, errno.EINVAL)
331 self.assertEqual(bytes(buf[0]), b'\0'* 2**16)
332 finally:
333 os.close(fd)
334
Ross Lagerwall7807c352011-03-17 20:20:30 +0200335 @unittest.skipUnless(hasattr(posix, 'pwrite'), "test needs posix.pwrite()")
336 def test_pwrite(self):
337 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
338 try:
339 os.write(fd, b'test')
340 os.lseek(fd, 0, os.SEEK_SET)
341 posix.pwrite(fd, b'xx', 1)
342 self.assertEqual(b'txxt', posix.read(fd, 4))
343 finally:
344 os.close(fd)
345
Pablo Galindo4defba32018-01-27 16:16:37 +0000346 @unittest.skipUnless(hasattr(posix, 'pwritev'), "test needs posix.pwritev()")
347 def test_pwritev(self):
348 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
349 try:
350 os.write(fd, b"xx")
351 os.lseek(fd, 0, os.SEEK_SET)
352 n = os.pwritev(fd, [b'test1', b'tt2', b't3'], 2)
353 self.assertEqual(n, 10)
354
355 os.lseek(fd, 0, os.SEEK_SET)
356 self.assertEqual(b'xxtest1tt2t3', posix.read(fd, 100))
357 finally:
358 os.close(fd)
359
Miss Islington (bot)3e4b6882018-07-31 02:20:06 -0700360 @unittest.skipUnless(hasattr(posix, 'pwritev'), "test needs posix.pwritev()")
Pablo Galindo4defba32018-01-27 16:16:37 +0000361 @unittest.skipUnless(hasattr(posix, 'os.RWF_SYNC'), "test needs os.RWF_SYNC")
362 def test_pwritev_flags(self):
363 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
364 try:
365 os.write(fd,b"xx")
366 os.lseek(fd, 0, os.SEEK_SET)
367 n = os.pwritev(fd, [b'test1', b'tt2', b't3'], 2, os.RWF_SYNC)
368 self.assertEqual(n, 10)
369
370 os.lseek(fd, 0, os.SEEK_SET)
371 self.assertEqual(b'xxtest1tt2', posix.read(fd, 100))
372 finally:
373 os.close(fd)
374
Miss Islington (bot)3e4b6882018-07-31 02:20:06 -0700375 @unittest.skipUnless(hasattr(posix, 'pwritev'), "test needs posix.pwritev()")
376 @requires_32b
377 def test_pwritev_overflow_32bits(self):
378 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
379 try:
380 with self.assertRaises(OSError) as cm:
381 os.pwritev(fd, [b"x" * 2**16] * 2**15, 0)
382 self.assertEqual(cm.exception.errno, errno.EINVAL)
383 finally:
384 os.close(fd)
385
Ross Lagerwall7807c352011-03-17 20:20:30 +0200386 @unittest.skipUnless(hasattr(posix, 'posix_fallocate'),
387 "test needs posix.posix_fallocate()")
388 def test_posix_fallocate(self):
389 fd = os.open(support.TESTFN, os.O_WRONLY | os.O_CREAT)
390 try:
391 posix.posix_fallocate(fd, 0, 10)
392 except OSError as inst:
393 # issue10812, ZFS doesn't appear to support posix_fallocate,
394 # so skip Solaris-based since they are likely to have ZFS.
Miss Islington (bot)96fb8282018-05-26 14:57:01 -0700395 # issue33655: Also ignore EINVAL on *BSD since ZFS is also
396 # often used there.
397 if inst.errno == errno.EINVAL and sys.platform.startswith(
398 ('sunos', 'freebsd', 'netbsd', 'openbsd', 'gnukfreebsd')):
399 raise unittest.SkipTest("test may fail on ZFS filesystems")
400 else:
Ross Lagerwall7807c352011-03-17 20:20:30 +0200401 raise
402 finally:
403 os.close(fd)
404
Коренберг Маркd4b93e22017-08-14 18:55:16 +0500405 # issue31106 - posix_fallocate() does not set error in errno.
406 @unittest.skipUnless(hasattr(posix, 'posix_fallocate'),
407 "test needs posix.posix_fallocate()")
408 def test_posix_fallocate_errno(self):
409 try:
410 posix.posix_fallocate(-42, 0, 10)
411 except OSError as inst:
412 if inst.errno != errno.EBADF:
413 raise
414
Ross Lagerwall7807c352011-03-17 20:20:30 +0200415 @unittest.skipUnless(hasattr(posix, 'posix_fadvise'),
416 "test needs posix.posix_fadvise()")
417 def test_posix_fadvise(self):
418 fd = os.open(support.TESTFN, os.O_RDONLY)
419 try:
420 posix.posix_fadvise(fd, 0, 0, posix.POSIX_FADV_WILLNEED)
421 finally:
422 os.close(fd)
423
Коренберг Маркd4b93e22017-08-14 18:55:16 +0500424 @unittest.skipUnless(hasattr(posix, 'posix_fadvise'),
425 "test needs posix.posix_fadvise()")
426 def test_posix_fadvise_errno(self):
427 try:
428 posix.posix_fadvise(-42, 0, 0, posix.POSIX_FADV_WILLNEED)
429 except OSError as inst:
430 if inst.errno != errno.EBADF:
431 raise
432
Larry Hastings9cf065c2012-06-22 16:30:09 -0700433 @unittest.skipUnless(os.utime in os.supports_fd, "test needs fd support in os.utime")
434 def test_utime_with_fd(self):
Ross Lagerwall7807c352011-03-17 20:20:30 +0200435 now = time.time()
436 fd = os.open(support.TESTFN, os.O_RDONLY)
437 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -0700438 posix.utime(fd)
439 posix.utime(fd, None)
440 self.assertRaises(TypeError, posix.utime, fd, (None, None))
441 self.assertRaises(TypeError, posix.utime, fd, (now, None))
442 self.assertRaises(TypeError, posix.utime, fd, (None, now))
443 posix.utime(fd, (int(now), int(now)))
444 posix.utime(fd, (now, now))
445 self.assertRaises(ValueError, posix.utime, fd, (now, now), ns=(now, now))
446 self.assertRaises(ValueError, posix.utime, fd, (now, 0), ns=(None, None))
447 self.assertRaises(ValueError, posix.utime, fd, (None, None), ns=(now, 0))
448 posix.utime(fd, (int(now), int((now - int(now)) * 1e9)))
449 posix.utime(fd, ns=(int(now), int((now - int(now)) * 1e9)))
450
Ross Lagerwall7807c352011-03-17 20:20:30 +0200451 finally:
452 os.close(fd)
453
Larry Hastings9cf065c2012-06-22 16:30:09 -0700454 @unittest.skipUnless(os.utime in os.supports_follow_symlinks, "test needs follow_symlinks support in os.utime")
455 def test_utime_nofollow_symlinks(self):
Ross Lagerwall7807c352011-03-17 20:20:30 +0200456 now = time.time()
Larry Hastings9cf065c2012-06-22 16:30:09 -0700457 posix.utime(support.TESTFN, None, follow_symlinks=False)
458 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, None), follow_symlinks=False)
459 self.assertRaises(TypeError, posix.utime, support.TESTFN, (now, None), follow_symlinks=False)
460 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, now), follow_symlinks=False)
461 posix.utime(support.TESTFN, (int(now), int(now)), follow_symlinks=False)
462 posix.utime(support.TESTFN, (now, now), follow_symlinks=False)
463 posix.utime(support.TESTFN, follow_symlinks=False)
Ross Lagerwall7807c352011-03-17 20:20:30 +0200464
465 @unittest.skipUnless(hasattr(posix, 'writev'), "test needs posix.writev()")
466 def test_writev(self):
467 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
468 try:
Victor Stinner57ddf782014-01-08 15:21:28 +0100469 n = os.writev(fd, (b'test1', b'tt2', b't3'))
470 self.assertEqual(n, 10)
471
Ross Lagerwall7807c352011-03-17 20:20:30 +0200472 os.lseek(fd, 0, os.SEEK_SET)
473 self.assertEqual(b'test1tt2t3', posix.read(fd, 10))
Victor Stinner57ddf782014-01-08 15:21:28 +0100474
475 # Issue #20113: empty list of buffers should not crash
Victor Stinnercd5ca6a2014-01-08 16:01:31 +0100476 try:
477 size = posix.writev(fd, [])
478 except OSError:
479 # writev(fd, []) raises OSError(22, "Invalid argument")
480 # on OpenIndiana
481 pass
482 else:
483 self.assertEqual(size, 0)
Ross Lagerwall7807c352011-03-17 20:20:30 +0200484 finally:
485 os.close(fd)
486
Miss Islington (bot)3e4b6882018-07-31 02:20:06 -0700487 @unittest.skipUnless(hasattr(posix, 'writev'), "test needs posix.writev()")
488 @requires_32b
489 def test_writev_overflow_32bits(self):
490 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
491 try:
492 with self.assertRaises(OSError) as cm:
493 os.writev(fd, [b"x" * 2**16] * 2**15)
494 self.assertEqual(cm.exception.errno, errno.EINVAL)
495 finally:
496 os.close(fd)
497
Ross Lagerwall7807c352011-03-17 20:20:30 +0200498 @unittest.skipUnless(hasattr(posix, 'readv'), "test needs posix.readv()")
499 def test_readv(self):
500 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
501 try:
502 os.write(fd, b'test1tt2t3')
503 os.lseek(fd, 0, os.SEEK_SET)
504 buf = [bytearray(i) for i in [5, 3, 2]]
505 self.assertEqual(posix.readv(fd, buf), 10)
506 self.assertEqual([b'test1', b'tt2', b't3'], [bytes(i) for i in buf])
Victor Stinner57ddf782014-01-08 15:21:28 +0100507
508 # Issue #20113: empty list of buffers should not crash
Victor Stinnercd5ca6a2014-01-08 16:01:31 +0100509 try:
510 size = posix.readv(fd, [])
511 except OSError:
512 # readv(fd, []) raises OSError(22, "Invalid argument")
513 # on OpenIndiana
514 pass
515 else:
516 self.assertEqual(size, 0)
Ross Lagerwall7807c352011-03-17 20:20:30 +0200517 finally:
518 os.close(fd)
519
Miss Islington (bot)3e4b6882018-07-31 02:20:06 -0700520 @unittest.skipUnless(hasattr(posix, 'readv'), "test needs posix.readv()")
521 @requires_32b
522 def test_readv_overflow_32bits(self):
523 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
524 try:
525 buf = [bytearray(2**16)] * 2**15
526 with self.assertRaises(OSError) as cm:
527 os.readv(fd, buf)
528 self.assertEqual(cm.exception.errno, errno.EINVAL)
529 self.assertEqual(bytes(buf[0]), b'\0'* 2**16)
530 finally:
531 os.close(fd)
532
Serhiy Storchaka43767632013-11-03 21:31:38 +0200533 @unittest.skipUnless(hasattr(posix, 'dup'),
534 'test needs posix.dup()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000535 def test_dup(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200536 fp = open(support.TESTFN)
537 try:
538 fd = posix.dup(fp.fileno())
539 self.assertIsInstance(fd, int)
540 os.close(fd)
541 finally:
542 fp.close()
Neal Norwitze241ce82003-02-17 18:17:05 +0000543
Serhiy Storchaka43767632013-11-03 21:31:38 +0200544 @unittest.skipUnless(hasattr(posix, 'confstr'),
545 'test needs posix.confstr()')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000546 def test_confstr(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200547 self.assertRaises(ValueError, posix.confstr, "CS_garbage")
548 self.assertEqual(len(posix.confstr("CS_PATH")) > 0, True)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000549
Serhiy Storchaka43767632013-11-03 21:31:38 +0200550 @unittest.skipUnless(hasattr(posix, 'dup2'),
551 'test needs posix.dup2()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000552 def test_dup2(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200553 fp1 = open(support.TESTFN)
554 fp2 = open(support.TESTFN)
555 try:
556 posix.dup2(fp1.fileno(), fp2.fileno())
557 finally:
558 fp1.close()
559 fp2.close()
Neal Norwitze241ce82003-02-17 18:17:05 +0000560
Charles-François Natali1e045b12011-05-22 20:42:32 +0200561 @unittest.skipUnless(hasattr(os, 'O_CLOEXEC'), "needs os.O_CLOEXEC")
Charles-François Natali239bb962011-06-03 12:55:15 +0200562 @support.requires_linux_version(2, 6, 23)
Charles-François Natali1e045b12011-05-22 20:42:32 +0200563 def test_oscloexec(self):
564 fd = os.open(support.TESTFN, os.O_RDONLY|os.O_CLOEXEC)
565 self.addCleanup(os.close, fd)
Victor Stinner1db9e7b2014-07-29 22:32:47 +0200566 self.assertFalse(os.get_inheritable(fd))
Charles-François Natali1e045b12011-05-22 20:42:32 +0200567
Serhiy Storchaka43767632013-11-03 21:31:38 +0200568 @unittest.skipUnless(hasattr(posix, 'O_EXLOCK'),
569 'test needs posix.O_EXLOCK')
Skip Montanaro98470002005-06-17 01:14:49 +0000570 def test_osexlock(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200571 fd = os.open(support.TESTFN,
572 os.O_WRONLY|os.O_EXLOCK|os.O_CREAT)
573 self.assertRaises(OSError, os.open, support.TESTFN,
574 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
575 os.close(fd)
576
577 if hasattr(posix, "O_SHLOCK"):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000578 fd = os.open(support.TESTFN,
Serhiy Storchaka43767632013-11-03 21:31:38 +0200579 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000580 self.assertRaises(OSError, os.open, support.TESTFN,
Skip Montanaro98470002005-06-17 01:14:49 +0000581 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
582 os.close(fd)
583
Serhiy Storchaka43767632013-11-03 21:31:38 +0200584 @unittest.skipUnless(hasattr(posix, 'O_SHLOCK'),
585 'test needs posix.O_SHLOCK')
Skip Montanaro98470002005-06-17 01:14:49 +0000586 def test_osshlock(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200587 fd1 = os.open(support.TESTFN,
588 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
589 fd2 = os.open(support.TESTFN,
590 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
591 os.close(fd2)
592 os.close(fd1)
593
594 if hasattr(posix, "O_EXLOCK"):
595 fd = os.open(support.TESTFN,
Skip Montanaro98470002005-06-17 01:14:49 +0000596 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
Serhiy Storchaka43767632013-11-03 21:31:38 +0200597 self.assertRaises(OSError, os.open, support.TESTFN,
598 os.O_RDONLY|os.O_EXLOCK|os.O_NONBLOCK)
599 os.close(fd)
Skip Montanaro98470002005-06-17 01:14:49 +0000600
Serhiy Storchaka43767632013-11-03 21:31:38 +0200601 @unittest.skipUnless(hasattr(posix, 'fstat'),
602 'test needs posix.fstat()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000603 def test_fstat(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200604 fp = open(support.TESTFN)
605 try:
606 self.assertTrue(posix.fstat(fp.fileno()))
607 self.assertTrue(posix.stat(fp.fileno()))
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200608
Serhiy Storchaka43767632013-11-03 21:31:38 +0200609 self.assertRaisesRegex(TypeError,
Brett Cannon3f9183b2016-08-26 14:44:48 -0700610 'should be string, bytes, os.PathLike or integer, not',
Serhiy Storchaka43767632013-11-03 21:31:38 +0200611 posix.stat, float(fp.fileno()))
612 finally:
613 fp.close()
Neal Norwitze241ce82003-02-17 18:17:05 +0000614
Serhiy Storchaka43767632013-11-03 21:31:38 +0200615 @unittest.skipUnless(hasattr(posix, 'stat'),
616 'test needs posix.stat()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000617 def test_stat(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200618 self.assertTrue(posix.stat(support.TESTFN))
619 self.assertTrue(posix.stat(os.fsencode(support.TESTFN)))
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200620
Serhiy Storchakad73c3182016-08-06 23:22:08 +0300621 self.assertWarnsRegex(DeprecationWarning,
Brett Cannon3f9183b2016-08-26 14:44:48 -0700622 'should be string, bytes, os.PathLike or integer, not',
Serhiy Storchakad73c3182016-08-06 23:22:08 +0300623 posix.stat, bytearray(os.fsencode(support.TESTFN)))
Serhiy Storchaka43767632013-11-03 21:31:38 +0200624 self.assertRaisesRegex(TypeError,
Brett Cannon3f9183b2016-08-26 14:44:48 -0700625 'should be string, bytes, os.PathLike or integer, not',
Serhiy Storchaka43767632013-11-03 21:31:38 +0200626 posix.stat, None)
627 self.assertRaisesRegex(TypeError,
Brett Cannon3f9183b2016-08-26 14:44:48 -0700628 'should be string, bytes, os.PathLike or integer, not',
Serhiy Storchaka43767632013-11-03 21:31:38 +0200629 posix.stat, list(support.TESTFN))
630 self.assertRaisesRegex(TypeError,
Brett Cannon3f9183b2016-08-26 14:44:48 -0700631 'should be string, bytes, os.PathLike or integer, not',
Serhiy Storchaka43767632013-11-03 21:31:38 +0200632 posix.stat, list(os.fsencode(support.TESTFN)))
Neal Norwitze241ce82003-02-17 18:17:05 +0000633
Benjamin Peterson052a02b2010-08-17 01:27:09 +0000634 @unittest.skipUnless(hasattr(posix, 'mkfifo'), "don't have mkfifo()")
635 def test_mkfifo(self):
636 support.unlink(support.TESTFN)
xdegaye92c2ca72017-11-12 17:31:07 +0100637 try:
638 posix.mkfifo(support.TESTFN, stat.S_IRUSR | stat.S_IWUSR)
639 except PermissionError as e:
640 self.skipTest('posix.mkfifo(): %s' % e)
Benjamin Peterson052a02b2010-08-17 01:27:09 +0000641 self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode))
642
643 @unittest.skipUnless(hasattr(posix, 'mknod') and hasattr(stat, 'S_IFIFO'),
644 "don't have mknod()/S_IFIFO")
645 def test_mknod(self):
646 # Test using mknod() to create a FIFO (the only use specified
647 # by POSIX).
648 support.unlink(support.TESTFN)
649 mode = stat.S_IFIFO | stat.S_IRUSR | stat.S_IWUSR
650 try:
651 posix.mknod(support.TESTFN, mode, 0)
652 except OSError as e:
653 # Some old systems don't allow unprivileged users to use
654 # mknod(), or only support creating device nodes.
xdegaye92c2ca72017-11-12 17:31:07 +0100655 self.assertIn(e.errno, (errno.EPERM, errno.EINVAL, errno.EACCES))
Benjamin Peterson052a02b2010-08-17 01:27:09 +0000656 else:
657 self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode))
658
Martin Panterbf19d162015-09-09 01:01:13 +0000659 # Keyword arguments are also supported
660 support.unlink(support.TESTFN)
661 try:
662 posix.mknod(path=support.TESTFN, mode=mode, device=0,
663 dir_fd=None)
664 except OSError as e:
xdegaye92c2ca72017-11-12 17:31:07 +0100665 self.assertIn(e.errno, (errno.EPERM, errno.EINVAL, errno.EACCES))
Martin Panterbf19d162015-09-09 01:01:13 +0000666
Serhiy Storchaka16b2e4f2015-04-20 09:22:13 +0300667 @unittest.skipUnless(hasattr(posix, 'stat'), 'test needs posix.stat()')
668 @unittest.skipUnless(hasattr(posix, 'makedev'), 'test needs posix.makedev()')
669 def test_makedev(self):
670 st = posix.stat(support.TESTFN)
671 dev = st.st_dev
672 self.assertIsInstance(dev, int)
673 self.assertGreaterEqual(dev, 0)
674
675 major = posix.major(dev)
676 self.assertIsInstance(major, int)
677 self.assertGreaterEqual(major, 0)
678 self.assertEqual(posix.major(dev), major)
679 self.assertRaises(TypeError, posix.major, float(dev))
680 self.assertRaises(TypeError, posix.major)
681 self.assertRaises((ValueError, OverflowError), posix.major, -1)
682
683 minor = posix.minor(dev)
684 self.assertIsInstance(minor, int)
685 self.assertGreaterEqual(minor, 0)
686 self.assertEqual(posix.minor(dev), minor)
687 self.assertRaises(TypeError, posix.minor, float(dev))
688 self.assertRaises(TypeError, posix.minor)
689 self.assertRaises((ValueError, OverflowError), posix.minor, -1)
690
Victor Stinner13ff2452018-01-22 18:32:50 +0100691 # FIXME: reenable these tests on FreeBSD with the kernel fix
Victor Stinner12953ff2017-07-27 16:55:54 +0200692 if sys.platform.startswith('freebsd') and dev >= 0x1_0000_0000:
693 self.skipTest("bpo-31044: on FreeBSD CURRENT, minor() truncates "
694 "64-bit dev to 32-bit")
695
Serhiy Storchaka16b2e4f2015-04-20 09:22:13 +0300696 self.assertEqual(posix.makedev(major, minor), dev)
697 self.assertRaises(TypeError, posix.makedev, float(major), minor)
698 self.assertRaises(TypeError, posix.makedev, major, float(minor))
699 self.assertRaises(TypeError, posix.makedev, major)
700 self.assertRaises(TypeError, posix.makedev)
701
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200702 def _test_all_chown_common(self, chown_func, first_param, stat_func):
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000703 """Common code for chown, fchown and lchown tests."""
Serhiy Storchaka54db2fd2013-02-20 19:40:25 +0200704 def check_stat(uid, gid):
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200705 if stat_func is not None:
706 stat = stat_func(first_param)
Serhiy Storchaka54db2fd2013-02-20 19:40:25 +0200707 self.assertEqual(stat.st_uid, uid)
708 self.assertEqual(stat.st_gid, gid)
709 uid = os.getuid()
710 gid = os.getgid()
Charles-François Nataliab2d58e2012-04-17 19:48:35 +0200711 # test a successful chown call
Serhiy Storchaka54db2fd2013-02-20 19:40:25 +0200712 chown_func(first_param, uid, gid)
713 check_stat(uid, gid)
714 chown_func(first_param, -1, gid)
715 check_stat(uid, gid)
716 chown_func(first_param, uid, -1)
717 check_stat(uid, gid)
Charles-François Nataliab2d58e2012-04-17 19:48:35 +0200718
Serhiy Storchaka54db2fd2013-02-20 19:40:25 +0200719 if uid == 0:
720 # Try an amusingly large uid/gid to make sure we handle
721 # large unsigned values. (chown lets you use any
722 # uid/gid you like, even if they aren't defined.)
723 #
724 # This problem keeps coming up:
725 # http://bugs.python.org/issue1747858
726 # http://bugs.python.org/issue4591
727 # http://bugs.python.org/issue15301
728 # Hopefully the fix in 4591 fixes it for good!
729 #
730 # This part of the test only runs when run as root.
731 # Only scary people run their tests as root.
732
733 big_value = 2**31
734 chown_func(first_param, big_value, big_value)
735 check_stat(big_value, big_value)
736 chown_func(first_param, -1, -1)
737 check_stat(big_value, big_value)
738 chown_func(first_param, uid, gid)
739 check_stat(uid, gid)
Charles-François Nataliab2d58e2012-04-17 19:48:35 +0200740 elif platform.system() in ('HP-UX', 'SunOS'):
741 # HP-UX and Solaris can allow a non-root user to chown() to root
742 # (issue #5113)
743 raise unittest.SkipTest("Skipping because of non-standard chown() "
744 "behavior")
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000745 else:
746 # non-root cannot chown to root, raises OSError
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200747 self.assertRaises(OSError, chown_func, first_param, 0, 0)
Serhiy Storchaka54db2fd2013-02-20 19:40:25 +0200748 check_stat(uid, gid)
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200749 self.assertRaises(OSError, chown_func, first_param, 0, -1)
Serhiy Storchaka54db2fd2013-02-20 19:40:25 +0200750 check_stat(uid, gid)
Serhiy Storchakaa2964b32013-02-21 14:34:36 +0200751 if 0 not in os.getgroups():
Serhiy Storchakab3d62ce2013-02-20 19:48:22 +0200752 self.assertRaises(OSError, chown_func, first_param, -1, 0)
753 check_stat(uid, gid)
Serhiy Storchaka54db2fd2013-02-20 19:40:25 +0200754 # test illegal types
755 for t in str, float:
756 self.assertRaises(TypeError, chown_func, first_param, t(uid), gid)
757 check_stat(uid, gid)
758 self.assertRaises(TypeError, chown_func, first_param, uid, t(gid))
759 check_stat(uid, gid)
Christian Heimesd5e2b6f2008-03-19 21:50:51 +0000760
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000761 @unittest.skipUnless(hasattr(posix, 'chown'), "test needs os.chown()")
762 def test_chown(self):
763 # raise an OSError if the file does not exist
764 os.unlink(support.TESTFN)
765 self.assertRaises(OSError, posix.chown, support.TESTFN, -1, -1)
Christian Heimesd5e2b6f2008-03-19 21:50:51 +0000766
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000767 # re-create the file
Victor Stinnerbf816222011-06-30 23:25:47 +0200768 support.create_empty_file(support.TESTFN)
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200769 self._test_all_chown_common(posix.chown, support.TESTFN,
770 getattr(posix, 'stat', None))
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000771
772 @unittest.skipUnless(hasattr(posix, 'fchown'), "test needs os.fchown()")
773 def test_fchown(self):
774 os.unlink(support.TESTFN)
775
776 # re-create the file
777 test_file = open(support.TESTFN, 'w')
778 try:
779 fd = test_file.fileno()
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200780 self._test_all_chown_common(posix.fchown, fd,
781 getattr(posix, 'fstat', None))
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000782 finally:
783 test_file.close()
784
785 @unittest.skipUnless(hasattr(posix, 'lchown'), "test needs os.lchown()")
786 def test_lchown(self):
787 os.unlink(support.TESTFN)
788 # create a symlink
Ned Deily3eb67d52011-06-28 00:00:28 -0700789 os.symlink(_DUMMY_SYMLINK, support.TESTFN)
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200790 self._test_all_chown_common(posix.lchown, support.TESTFN,
791 getattr(posix, 'lstat', None))
Christian Heimesd5e2b6f2008-03-19 21:50:51 +0000792
Serhiy Storchaka43767632013-11-03 21:31:38 +0200793 @unittest.skipUnless(hasattr(posix, 'chdir'), 'test needs posix.chdir()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000794 def test_chdir(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200795 posix.chdir(os.curdir)
796 self.assertRaises(OSError, posix.chdir, support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000797
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +0000798 def test_listdir(self):
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +0300799 self.assertIn(support.TESTFN, posix.listdir(os.curdir))
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +0000800
801 def test_listdir_default(self):
Larry Hastingsfdaea062012-06-25 04:42:23 -0700802 # When listdir is called without argument,
803 # it's the same as listdir(os.curdir).
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +0300804 self.assertIn(support.TESTFN, posix.listdir())
Neal Norwitze241ce82003-02-17 18:17:05 +0000805
Larry Hastingsfdaea062012-06-25 04:42:23 -0700806 def test_listdir_bytes(self):
807 # When listdir is called with a bytes object,
808 # the returned strings are of type bytes.
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +0300809 self.assertIn(os.fsencode(support.TESTFN), posix.listdir(b'.'))
810
811 def test_listdir_bytes_like(self):
812 for cls in bytearray, memoryview:
813 with self.assertWarns(DeprecationWarning):
814 names = posix.listdir(cls(b'.'))
815 self.assertIn(os.fsencode(support.TESTFN), names)
816 for name in names:
817 self.assertIs(type(name), bytes)
Larry Hastingsfdaea062012-06-25 04:42:23 -0700818
819 @unittest.skipUnless(posix.listdir in os.supports_fd,
820 "test needs fd support for posix.listdir()")
821 def test_listdir_fd(self):
Antoine Pitrou8250e232011-02-25 23:41:16 +0000822 f = posix.open(posix.getcwd(), posix.O_RDONLY)
Charles-François Natali7546ad32012-01-08 18:34:06 +0100823 self.addCleanup(posix.close, f)
Antoine Pitrou8250e232011-02-25 23:41:16 +0000824 self.assertEqual(
825 sorted(posix.listdir('.')),
Larry Hastings9cf065c2012-06-22 16:30:09 -0700826 sorted(posix.listdir(f))
Antoine Pitrou8250e232011-02-25 23:41:16 +0000827 )
Charles-François Natali7546ad32012-01-08 18:34:06 +0100828 # Check that the fd offset was reset (issue #13739)
Charles-François Natali7546ad32012-01-08 18:34:06 +0100829 self.assertEqual(
830 sorted(posix.listdir('.')),
Larry Hastings9cf065c2012-06-22 16:30:09 -0700831 sorted(posix.listdir(f))
Charles-François Natali7546ad32012-01-08 18:34:06 +0100832 )
Antoine Pitrou8250e232011-02-25 23:41:16 +0000833
Serhiy Storchaka43767632013-11-03 21:31:38 +0200834 @unittest.skipUnless(hasattr(posix, 'access'), 'test needs posix.access()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000835 def test_access(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200836 self.assertTrue(posix.access(support.TESTFN, os.R_OK))
Neal Norwitze241ce82003-02-17 18:17:05 +0000837
Serhiy Storchaka43767632013-11-03 21:31:38 +0200838 @unittest.skipUnless(hasattr(posix, 'umask'), 'test needs posix.umask()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000839 def test_umask(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200840 old_mask = posix.umask(0)
841 self.assertIsInstance(old_mask, int)
842 posix.umask(old_mask)
Neal Norwitze241ce82003-02-17 18:17:05 +0000843
Serhiy Storchaka43767632013-11-03 21:31:38 +0200844 @unittest.skipUnless(hasattr(posix, 'strerror'),
845 'test needs posix.strerror()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000846 def test_strerror(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200847 self.assertTrue(posix.strerror(0))
Neal Norwitze241ce82003-02-17 18:17:05 +0000848
Serhiy Storchaka43767632013-11-03 21:31:38 +0200849 @unittest.skipUnless(hasattr(posix, 'pipe'), 'test needs posix.pipe()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000850 def test_pipe(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200851 reader, writer = posix.pipe()
852 os.close(reader)
853 os.close(writer)
Neal Norwitze241ce82003-02-17 18:17:05 +0000854
Charles-François Natalidaafdd52011-05-29 20:07:40 +0200855 @unittest.skipUnless(hasattr(os, 'pipe2'), "test needs os.pipe2()")
Charles-François Natali239bb962011-06-03 12:55:15 +0200856 @support.requires_linux_version(2, 6, 27)
Charles-François Natalidaafdd52011-05-29 20:07:40 +0200857 def test_pipe2(self):
858 self.assertRaises(TypeError, os.pipe2, 'DEADBEEF')
859 self.assertRaises(TypeError, os.pipe2, 0, 0)
860
Charles-François Natali368f34b2011-06-06 19:49:47 +0200861 # try calling with flags = 0, like os.pipe()
862 r, w = os.pipe2(0)
Charles-François Natalidaafdd52011-05-29 20:07:40 +0200863 os.close(r)
864 os.close(w)
865
866 # test flags
867 r, w = os.pipe2(os.O_CLOEXEC|os.O_NONBLOCK)
868 self.addCleanup(os.close, r)
869 self.addCleanup(os.close, w)
Victor Stinnerbff989e2013-08-28 12:25:40 +0200870 self.assertFalse(os.get_inheritable(r))
871 self.assertFalse(os.get_inheritable(w))
Victor Stinner1db9e7b2014-07-29 22:32:47 +0200872 self.assertFalse(os.get_blocking(r))
873 self.assertFalse(os.get_blocking(w))
Charles-François Natalidaafdd52011-05-29 20:07:40 +0200874 # try reading from an empty pipe: this should fail, not block
875 self.assertRaises(OSError, os.read, r, 1)
876 # try a write big enough to fill-up the pipe: this should either
877 # fail or perform a partial write, not block
878 try:
879 os.write(w, b'x' * support.PIPE_MAX_SIZE)
880 except OSError:
881 pass
882
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +0200883 @support.cpython_only
884 @unittest.skipUnless(hasattr(os, 'pipe2'), "test needs os.pipe2()")
885 @support.requires_linux_version(2, 6, 27)
886 def test_pipe2_c_limits(self):
Serhiy Storchaka78980432013-01-15 01:12:17 +0200887 # Issue 15989
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +0200888 import _testcapi
Serhiy Storchaka78980432013-01-15 01:12:17 +0200889 self.assertRaises(OverflowError, os.pipe2, _testcapi.INT_MAX + 1)
890 self.assertRaises(OverflowError, os.pipe2, _testcapi.UINT_MAX + 1)
891
Serhiy Storchaka43767632013-11-03 21:31:38 +0200892 @unittest.skipUnless(hasattr(posix, 'utime'), 'test needs posix.utime()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000893 def test_utime(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200894 now = time.time()
895 posix.utime(support.TESTFN, None)
896 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, None))
897 self.assertRaises(TypeError, posix.utime, support.TESTFN, (now, None))
898 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, now))
899 posix.utime(support.TESTFN, (int(now), int(now)))
900 posix.utime(support.TESTFN, (now, now))
Neal Norwitze241ce82003-02-17 18:17:05 +0000901
Larry Hastings9cf065c2012-06-22 16:30:09 -0700902 def _test_chflags_regular_file(self, chflags_func, target_file, **kwargs):
Ned Deily3eb67d52011-06-28 00:00:28 -0700903 st = os.stat(target_file)
904 self.assertTrue(hasattr(st, 'st_flags'))
Trent Nelson75959cf2012-08-21 23:59:31 +0000905
906 # ZFS returns EOPNOTSUPP when attempting to set flag UF_IMMUTABLE.
907 flags = st.st_flags | stat.UF_IMMUTABLE
908 try:
909 chflags_func(target_file, flags, **kwargs)
910 except OSError as err:
911 if err.errno != errno.EOPNOTSUPP:
912 raise
913 msg = 'chflag UF_IMMUTABLE not supported by underlying fs'
914 self.skipTest(msg)
915
Ned Deily3eb67d52011-06-28 00:00:28 -0700916 try:
917 new_st = os.stat(target_file)
918 self.assertEqual(st.st_flags | stat.UF_IMMUTABLE, new_st.st_flags)
919 try:
920 fd = open(target_file, 'w+')
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200921 except OSError as e:
Ned Deily3eb67d52011-06-28 00:00:28 -0700922 self.assertEqual(e.errno, errno.EPERM)
923 finally:
924 posix.chflags(target_file, st.st_flags)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000925
Ned Deily3eb67d52011-06-28 00:00:28 -0700926 @unittest.skipUnless(hasattr(posix, 'chflags'), 'test needs os.chflags()')
927 def test_chflags(self):
928 self._test_chflags_regular_file(posix.chflags, support.TESTFN)
929
930 @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()')
931 def test_lchflags_regular_file(self):
932 self._test_chflags_regular_file(posix.lchflags, support.TESTFN)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700933 self._test_chflags_regular_file(posix.chflags, support.TESTFN, follow_symlinks=False)
Ned Deily3eb67d52011-06-28 00:00:28 -0700934
935 @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()')
936 def test_lchflags_symlink(self):
937 testfn_st = os.stat(support.TESTFN)
938
939 self.assertTrue(hasattr(testfn_st, 'st_flags'))
940
941 os.symlink(support.TESTFN, _DUMMY_SYMLINK)
942 self.teardown_files.append(_DUMMY_SYMLINK)
943 dummy_symlink_st = os.lstat(_DUMMY_SYMLINK)
944
Larry Hastings9cf065c2012-06-22 16:30:09 -0700945 def chflags_nofollow(path, flags):
946 return posix.chflags(path, flags, follow_symlinks=False)
Ned Deily3eb67d52011-06-28 00:00:28 -0700947
Larry Hastings9cf065c2012-06-22 16:30:09 -0700948 for fn in (posix.lchflags, chflags_nofollow):
Trent Nelson75959cf2012-08-21 23:59:31 +0000949 # ZFS returns EOPNOTSUPP when attempting to set flag UF_IMMUTABLE.
950 flags = dummy_symlink_st.st_flags | stat.UF_IMMUTABLE
951 try:
952 fn(_DUMMY_SYMLINK, flags)
953 except OSError as err:
954 if err.errno != errno.EOPNOTSUPP:
955 raise
956 msg = 'chflag UF_IMMUTABLE not supported by underlying fs'
957 self.skipTest(msg)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700958 try:
959 new_testfn_st = os.stat(support.TESTFN)
960 new_dummy_symlink_st = os.lstat(_DUMMY_SYMLINK)
961
962 self.assertEqual(testfn_st.st_flags, new_testfn_st.st_flags)
963 self.assertEqual(dummy_symlink_st.st_flags | stat.UF_IMMUTABLE,
964 new_dummy_symlink_st.st_flags)
965 finally:
966 fn(_DUMMY_SYMLINK, dummy_symlink_st.st_flags)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000967
Guido van Rossum98297ee2007-11-06 21:34:58 +0000968 def test_environ(self):
Victor Stinner17b490d2010-05-06 22:19:30 +0000969 if os.name == "nt":
970 item_type = str
971 else:
972 item_type = bytes
Guido van Rossum98297ee2007-11-06 21:34:58 +0000973 for k, v in posix.environ.items():
Victor Stinner17b490d2010-05-06 22:19:30 +0000974 self.assertEqual(type(k), item_type)
975 self.assertEqual(type(v), item_type)
Guido van Rossum98297ee2007-11-06 21:34:58 +0000976
Serhiy Storchaka77703942017-06-25 07:33:01 +0300977 @unittest.skipUnless(hasattr(os, "putenv"), "requires os.putenv()")
978 def test_putenv(self):
979 with self.assertRaises(ValueError):
980 os.putenv('FRUIT\0VEGETABLE', 'cabbage')
981 with self.assertRaises(ValueError):
982 os.putenv(b'FRUIT\0VEGETABLE', b'cabbage')
983 with self.assertRaises(ValueError):
984 os.putenv('FRUIT', 'orange\0VEGETABLE=cabbage')
985 with self.assertRaises(ValueError):
986 os.putenv(b'FRUIT', b'orange\0VEGETABLE=cabbage')
987 with self.assertRaises(ValueError):
988 os.putenv('FRUIT=ORANGE', 'lemon')
989 with self.assertRaises(ValueError):
990 os.putenv(b'FRUIT=ORANGE', b'lemon')
991
Serhiy Storchaka43767632013-11-03 21:31:38 +0200992 @unittest.skipUnless(hasattr(posix, 'getcwd'), 'test needs posix.getcwd()')
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000993 def test_getcwd_long_pathnames(self):
Benjamin Peterson3a7dffa2013-08-23 21:01:48 -0500994 dirname = 'getcwd-test-directory-0123456789abcdef-01234567890abcdef'
995 curdir = os.getcwd()
996 base_path = os.path.abspath(support.TESTFN) + '.getcwd'
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000997
Benjamin Peterson3a7dffa2013-08-23 21:01:48 -0500998 try:
999 os.mkdir(base_path)
1000 os.chdir(base_path)
1001 except:
1002 # Just returning nothing instead of the SkipTest exception, because
1003 # the test results in Error in that case. Is that ok?
1004 # raise unittest.SkipTest("cannot create directory for testing")
1005 return
Benjamin Petersondcf97b92008-07-02 17:30:14 +00001006
Benjamin Peterson3a7dffa2013-08-23 21:01:48 -05001007 def _create_and_do_getcwd(dirname, current_path_length = 0):
1008 try:
1009 os.mkdir(dirname)
1010 except:
1011 raise unittest.SkipTest("mkdir cannot create directory sufficiently deep for getcwd test")
Benjamin Petersondcf97b92008-07-02 17:30:14 +00001012
Benjamin Peterson3a7dffa2013-08-23 21:01:48 -05001013 os.chdir(dirname)
1014 try:
1015 os.getcwd()
1016 if current_path_length < 1027:
1017 _create_and_do_getcwd(dirname, current_path_length + len(dirname) + 1)
1018 finally:
1019 os.chdir('..')
1020 os.rmdir(dirname)
Benjamin Petersondcf97b92008-07-02 17:30:14 +00001021
Benjamin Peterson3a7dffa2013-08-23 21:01:48 -05001022 _create_and_do_getcwd(dirname)
Benjamin Petersondcf97b92008-07-02 17:30:14 +00001023
Benjamin Peterson3a7dffa2013-08-23 21:01:48 -05001024 finally:
1025 os.chdir(curdir)
1026 support.rmtree(base_path)
Benjamin Petersondcf97b92008-07-02 17:30:14 +00001027
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02001028 @unittest.skipUnless(hasattr(posix, 'getgrouplist'), "test needs posix.getgrouplist()")
1029 @unittest.skipUnless(hasattr(pwd, 'getpwuid'), "test needs pwd.getpwuid()")
1030 @unittest.skipUnless(hasattr(os, 'getuid'), "test needs os.getuid()")
1031 def test_getgrouplist(self):
Ross Lagerwalla0b315f2012-12-13 15:20:26 +00001032 user = pwd.getpwuid(os.getuid())[0]
1033 group = pwd.getpwuid(os.getuid())[3]
1034 self.assertIn(group, posix.getgrouplist(user, group))
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02001035
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02001036
Antoine Pitrou318b8f32011-01-12 18:45:27 +00001037 @unittest.skipUnless(hasattr(os, 'getegid'), "test needs os.getegid()")
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00001038 def test_getgroups(self):
Jesus Cea61f32cb2014-06-28 18:39:35 +02001039 with os.popen('id -G 2>/dev/null') as idg:
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00001040 groups = idg.read().strip()
Charles-François Natalie8a255a2012-05-02 20:01:38 +02001041 ret = idg.close()
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00001042
Xavier de Gaye24c3b492016-10-19 11:00:26 +02001043 try:
1044 idg_groups = set(int(g) for g in groups.split())
1045 except ValueError:
1046 idg_groups = set()
1047 if ret is not None or not idg_groups:
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00001048 raise unittest.SkipTest("need working 'id -G'")
1049
Ned Deily028915e2013-02-02 15:08:52 -08001050 # Issues 16698: OS X ABIs prior to 10.6 have limits on getgroups()
1051 if sys.platform == 'darwin':
1052 import sysconfig
1053 dt = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET') or '10.0'
Ned Deily04cdfa12014-06-25 13:36:14 -07001054 if tuple(int(n) for n in dt.split('.')[0:2]) < (10, 6):
Ned Deily028915e2013-02-02 15:08:52 -08001055 raise unittest.SkipTest("getgroups(2) is broken prior to 10.6")
1056
Ronald Oussoren7fb6f512010-08-01 19:18:13 +00001057 # 'id -G' and 'os.getgroups()' should return the same
Xavier de Gaye24c3b492016-10-19 11:00:26 +02001058 # groups, ignoring order, duplicates, and the effective gid.
1059 # #10822/#26944 - It is implementation defined whether
1060 # posix.getgroups() includes the effective gid.
1061 symdiff = idg_groups.symmetric_difference(posix.getgroups())
1062 self.assertTrue(not symdiff or symdiff == {posix.getegid()})
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00001063
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001064 # tests for the posix *at functions follow
1065
Larry Hastings9cf065c2012-06-22 16:30:09 -07001066 @unittest.skipUnless(os.access in os.supports_dir_fd, "test needs dir_fd support for os.access()")
1067 def test_access_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001068 f = posix.open(posix.getcwd(), posix.O_RDONLY)
1069 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -07001070 self.assertTrue(posix.access(support.TESTFN, os.R_OK, dir_fd=f))
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001071 finally:
1072 posix.close(f)
1073
Larry Hastings9cf065c2012-06-22 16:30:09 -07001074 @unittest.skipUnless(os.chmod in os.supports_dir_fd, "test needs dir_fd support in os.chmod()")
1075 def test_chmod_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001076 os.chmod(support.TESTFN, stat.S_IRUSR)
1077
1078 f = posix.open(posix.getcwd(), posix.O_RDONLY)
1079 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -07001080 posix.chmod(support.TESTFN, stat.S_IRUSR | stat.S_IWUSR, dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001081
1082 s = posix.stat(support.TESTFN)
1083 self.assertEqual(s[0] & stat.S_IRWXU, stat.S_IRUSR | stat.S_IWUSR)
1084 finally:
1085 posix.close(f)
1086
Larry Hastings9cf065c2012-06-22 16:30:09 -07001087 @unittest.skipUnless(os.chown in os.supports_dir_fd, "test needs dir_fd support in os.chown()")
1088 def test_chown_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001089 support.unlink(support.TESTFN)
Victor Stinnerbf816222011-06-30 23:25:47 +02001090 support.create_empty_file(support.TESTFN)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001091
1092 f = posix.open(posix.getcwd(), posix.O_RDONLY)
1093 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -07001094 posix.chown(support.TESTFN, os.getuid(), os.getgid(), dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001095 finally:
1096 posix.close(f)
1097
Larry Hastings9cf065c2012-06-22 16:30:09 -07001098 @unittest.skipUnless(os.stat in os.supports_dir_fd, "test needs dir_fd support in os.stat()")
1099 def test_stat_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001100 support.unlink(support.TESTFN)
1101 with open(support.TESTFN, 'w') as outfile:
1102 outfile.write("testline\n")
1103
1104 f = posix.open(posix.getcwd(), posix.O_RDONLY)
1105 try:
1106 s1 = posix.stat(support.TESTFN)
Larry Hastings9cf065c2012-06-22 16:30:09 -07001107 s2 = posix.stat(support.TESTFN, dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001108 self.assertEqual(s1, s2)
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001109 s2 = posix.stat(support.TESTFN, dir_fd=None)
1110 self.assertEqual(s1, s2)
Serhiy Storchaka7155b882016-04-08 08:48:20 +03001111 self.assertRaisesRegex(TypeError, 'should be integer or None, not',
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001112 posix.stat, support.TESTFN, dir_fd=posix.getcwd())
Serhiy Storchaka7155b882016-04-08 08:48:20 +03001113 self.assertRaisesRegex(TypeError, 'should be integer or None, not',
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001114 posix.stat, support.TESTFN, dir_fd=float(f))
1115 self.assertRaises(OverflowError,
1116 posix.stat, support.TESTFN, dir_fd=10**20)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001117 finally:
1118 posix.close(f)
1119
Larry Hastings9cf065c2012-06-22 16:30:09 -07001120 @unittest.skipUnless(os.utime in os.supports_dir_fd, "test needs dir_fd support in os.utime()")
1121 def test_utime_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001122 f = posix.open(posix.getcwd(), posix.O_RDONLY)
1123 try:
1124 now = time.time()
Larry Hastings9cf065c2012-06-22 16:30:09 -07001125 posix.utime(support.TESTFN, None, dir_fd=f)
1126 posix.utime(support.TESTFN, dir_fd=f)
1127 self.assertRaises(TypeError, posix.utime, support.TESTFN, now, dir_fd=f)
1128 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, None), dir_fd=f)
1129 self.assertRaises(TypeError, posix.utime, support.TESTFN, (now, None), dir_fd=f)
1130 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, now), dir_fd=f)
1131 self.assertRaises(TypeError, posix.utime, support.TESTFN, (now, "x"), dir_fd=f)
1132 posix.utime(support.TESTFN, (int(now), int(now)), dir_fd=f)
1133 posix.utime(support.TESTFN, (now, now), dir_fd=f)
1134 posix.utime(support.TESTFN,
1135 (int(now), int((now - int(now)) * 1e9)), dir_fd=f)
1136 posix.utime(support.TESTFN, dir_fd=f,
1137 times=(int(now), int((now - int(now)) * 1e9)))
1138
Larry Hastings90867a52012-06-22 17:01:41 -07001139 # try dir_fd and follow_symlinks together
Larry Hastings9cf065c2012-06-22 16:30:09 -07001140 if os.utime in os.supports_follow_symlinks:
Larry Hastings90867a52012-06-22 17:01:41 -07001141 try:
1142 posix.utime(support.TESTFN, follow_symlinks=False, dir_fd=f)
Georg Brandl969288e2012-06-26 09:25:44 +02001143 except ValueError:
Larry Hastings90867a52012-06-22 17:01:41 -07001144 # whoops! using both together not supported on this platform.
1145 pass
Larry Hastings9cf065c2012-06-22 16:30:09 -07001146
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001147 finally:
1148 posix.close(f)
1149
Larry Hastings9cf065c2012-06-22 16:30:09 -07001150 @unittest.skipUnless(os.link in os.supports_dir_fd, "test needs dir_fd support in os.link()")
1151 def test_link_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001152 f = posix.open(posix.getcwd(), posix.O_RDONLY)
1153 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -07001154 posix.link(support.TESTFN, support.TESTFN + 'link', src_dir_fd=f, dst_dir_fd=f)
xdegaye92c2ca72017-11-12 17:31:07 +01001155 except PermissionError as e:
1156 self.skipTest('posix.link(): %s' % e)
1157 else:
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001158 # should have same inodes
1159 self.assertEqual(posix.stat(support.TESTFN)[1],
1160 posix.stat(support.TESTFN + 'link')[1])
1161 finally:
1162 posix.close(f)
1163 support.unlink(support.TESTFN + 'link')
1164
Larry Hastings9cf065c2012-06-22 16:30:09 -07001165 @unittest.skipUnless(os.mkdir in os.supports_dir_fd, "test needs dir_fd support in os.mkdir()")
1166 def test_mkdir_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001167 f = posix.open(posix.getcwd(), posix.O_RDONLY)
1168 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -07001169 posix.mkdir(support.TESTFN + 'dir', dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001170 posix.stat(support.TESTFN + 'dir') # should not raise exception
1171 finally:
1172 posix.close(f)
1173 support.rmtree(support.TESTFN + 'dir')
1174
Larry Hastings9cf065c2012-06-22 16:30:09 -07001175 @unittest.skipUnless((os.mknod in os.supports_dir_fd) and hasattr(stat, 'S_IFIFO'),
1176 "test requires both stat.S_IFIFO and dir_fd support for os.mknod()")
1177 def test_mknod_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001178 # Test using mknodat() to create a FIFO (the only use specified
1179 # by POSIX).
1180 support.unlink(support.TESTFN)
1181 mode = stat.S_IFIFO | stat.S_IRUSR | stat.S_IWUSR
1182 f = posix.open(posix.getcwd(), posix.O_RDONLY)
1183 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -07001184 posix.mknod(support.TESTFN, mode, 0, dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001185 except OSError as e:
1186 # Some old systems don't allow unprivileged users to use
1187 # mknod(), or only support creating device nodes.
xdegaye92c2ca72017-11-12 17:31:07 +01001188 self.assertIn(e.errno, (errno.EPERM, errno.EINVAL, errno.EACCES))
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001189 else:
1190 self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode))
1191 finally:
1192 posix.close(f)
1193
Larry Hastings9cf065c2012-06-22 16:30:09 -07001194 @unittest.skipUnless(os.open in os.supports_dir_fd, "test needs dir_fd support in os.open()")
1195 def test_open_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001196 support.unlink(support.TESTFN)
1197 with open(support.TESTFN, 'w') as outfile:
1198 outfile.write("testline\n")
1199 a = posix.open(posix.getcwd(), posix.O_RDONLY)
Larry Hastings9cf065c2012-06-22 16:30:09 -07001200 b = posix.open(support.TESTFN, posix.O_RDONLY, dir_fd=a)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001201 try:
1202 res = posix.read(b, 9).decode(encoding="utf-8")
1203 self.assertEqual("testline\n", res)
1204 finally:
1205 posix.close(a)
1206 posix.close(b)
1207
Larry Hastings9cf065c2012-06-22 16:30:09 -07001208 @unittest.skipUnless(os.readlink in os.supports_dir_fd, "test needs dir_fd support in os.readlink()")
1209 def test_readlink_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001210 os.symlink(support.TESTFN, support.TESTFN + 'link')
1211 f = posix.open(posix.getcwd(), posix.O_RDONLY)
1212 try:
1213 self.assertEqual(posix.readlink(support.TESTFN + 'link'),
Larry Hastings9cf065c2012-06-22 16:30:09 -07001214 posix.readlink(support.TESTFN + 'link', dir_fd=f))
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001215 finally:
1216 support.unlink(support.TESTFN + 'link')
1217 posix.close(f)
1218
Larry Hastings9cf065c2012-06-22 16:30:09 -07001219 @unittest.skipUnless(os.rename in os.supports_dir_fd, "test needs dir_fd support in os.rename()")
1220 def test_rename_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001221 support.unlink(support.TESTFN)
Victor Stinnerbf816222011-06-30 23:25:47 +02001222 support.create_empty_file(support.TESTFN + 'ren')
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001223 f = posix.open(posix.getcwd(), posix.O_RDONLY)
1224 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -07001225 posix.rename(support.TESTFN + 'ren', support.TESTFN, src_dir_fd=f, dst_dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001226 except:
1227 posix.rename(support.TESTFN + 'ren', support.TESTFN)
1228 raise
1229 else:
Andrew Svetlov5b898402012-12-18 21:26:36 +02001230 posix.stat(support.TESTFN) # should not raise exception
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001231 finally:
1232 posix.close(f)
1233
Larry Hastings9cf065c2012-06-22 16:30:09 -07001234 @unittest.skipUnless(os.symlink in os.supports_dir_fd, "test needs dir_fd support in os.symlink()")
1235 def test_symlink_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001236 f = posix.open(posix.getcwd(), posix.O_RDONLY)
1237 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -07001238 posix.symlink(support.TESTFN, support.TESTFN + 'link', dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001239 self.assertEqual(posix.readlink(support.TESTFN + 'link'), support.TESTFN)
1240 finally:
1241 posix.close(f)
1242 support.unlink(support.TESTFN + 'link')
1243
Larry Hastings9cf065c2012-06-22 16:30:09 -07001244 @unittest.skipUnless(os.unlink in os.supports_dir_fd, "test needs dir_fd support in os.unlink()")
1245 def test_unlink_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001246 f = posix.open(posix.getcwd(), posix.O_RDONLY)
Victor Stinnerbf816222011-06-30 23:25:47 +02001247 support.create_empty_file(support.TESTFN + 'del')
Andrew Svetlov5b898402012-12-18 21:26:36 +02001248 posix.stat(support.TESTFN + 'del') # should not raise exception
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001249 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -07001250 posix.unlink(support.TESTFN + 'del', dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001251 except:
1252 support.unlink(support.TESTFN + 'del')
1253 raise
1254 else:
1255 self.assertRaises(OSError, posix.stat, support.TESTFN + 'link')
1256 finally:
1257 posix.close(f)
1258
Larry Hastings9cf065c2012-06-22 16:30:09 -07001259 @unittest.skipUnless(os.mkfifo in os.supports_dir_fd, "test needs dir_fd support in os.mkfifo()")
1260 def test_mkfifo_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001261 support.unlink(support.TESTFN)
1262 f = posix.open(posix.getcwd(), posix.O_RDONLY)
1263 try:
xdegaye92c2ca72017-11-12 17:31:07 +01001264 try:
1265 posix.mkfifo(support.TESTFN,
1266 stat.S_IRUSR | stat.S_IWUSR, dir_fd=f)
1267 except PermissionError as e:
1268 self.skipTest('posix.mkfifo(): %s' % e)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001269 self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode))
1270 finally:
1271 posix.close(f)
1272
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001273 requires_sched_h = unittest.skipUnless(hasattr(posix, 'sched_yield'),
1274 "don't have scheduling support")
Antoine Pitrou84869872012-08-04 16:16:35 +02001275 requires_sched_affinity = unittest.skipUnless(hasattr(posix, 'sched_setaffinity'),
Benjamin Peterson50ba2712011-08-02 22:15:40 -05001276 "don't have sched affinity support")
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001277
1278 @requires_sched_h
1279 def test_sched_yield(self):
1280 # This has no error conditions (at least on Linux).
1281 posix.sched_yield()
1282
1283 @requires_sched_h
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02001284 @unittest.skipUnless(hasattr(posix, 'sched_get_priority_max'),
1285 "requires sched_get_priority_max()")
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001286 def test_sched_priority(self):
1287 # Round-robin usually has interesting priorities.
1288 pol = posix.SCHED_RR
1289 lo = posix.sched_get_priority_min(pol)
1290 hi = posix.sched_get_priority_max(pol)
1291 self.assertIsInstance(lo, int)
1292 self.assertIsInstance(hi, int)
1293 self.assertGreaterEqual(hi, lo)
Benjamin Peterson539b6c42011-08-02 22:09:37 -05001294 # OSX evidently just returns 15 without checking the argument.
1295 if sys.platform != "darwin":
Benjamin Petersonc1581582011-08-02 22:10:55 -05001296 self.assertRaises(OSError, posix.sched_get_priority_min, -23)
1297 self.assertRaises(OSError, posix.sched_get_priority_max, -23)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001298
Benjamin Peterson0aef9092018-09-12 16:00:06 -07001299 @requires_sched
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001300 def test_get_and_set_scheduler_and_param(self):
1301 possible_schedulers = [sched for name, sched in posix.__dict__.items()
1302 if name.startswith("SCHED_")]
1303 mine = posix.sched_getscheduler(0)
1304 self.assertIn(mine, possible_schedulers)
1305 try:
Jesus Ceaceb5d162011-09-10 01:16:55 +02001306 parent = posix.sched_getscheduler(os.getppid())
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001307 except OSError as e:
Jesus Ceaceb5d162011-09-10 01:16:55 +02001308 if e.errno != errno.EPERM:
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001309 raise
1310 else:
Jesus Ceaceb5d162011-09-10 01:16:55 +02001311 self.assertIn(parent, possible_schedulers)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001312 self.assertRaises(OSError, posix.sched_getscheduler, -1)
1313 self.assertRaises(OSError, posix.sched_getparam, -1)
1314 param = posix.sched_getparam(0)
1315 self.assertIsInstance(param.sched_priority, int)
Charles-François Natali7b911cb2011-08-21 12:41:43 +02001316
Charles-François Natalib402a5c2013-01-13 14:13:25 +01001317 # POSIX states that calling sched_setparam() or sched_setscheduler() on
1318 # a process with a scheduling policy other than SCHED_FIFO or SCHED_RR
1319 # is implementation-defined: NetBSD and FreeBSD can return EINVAL.
1320 if not sys.platform.startswith(('freebsd', 'netbsd')):
1321 try:
1322 posix.sched_setscheduler(0, mine, param)
1323 posix.sched_setparam(0, param)
1324 except OSError as e:
1325 if e.errno != errno.EPERM:
1326 raise
Charles-François Natali7b911cb2011-08-21 12:41:43 +02001327 self.assertRaises(OSError, posix.sched_setparam, -1, param)
1328
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001329 self.assertRaises(OSError, posix.sched_setscheduler, -1, mine, param)
1330 self.assertRaises(TypeError, posix.sched_setscheduler, 0, mine, None)
1331 self.assertRaises(TypeError, posix.sched_setparam, 0, 43)
1332 param = posix.sched_param(None)
1333 self.assertRaises(TypeError, posix.sched_setparam, 0, param)
1334 large = 214748364700
1335 param = posix.sched_param(large)
1336 self.assertRaises(OverflowError, posix.sched_setparam, 0, param)
1337 param = posix.sched_param(sched_priority=-large)
1338 self.assertRaises(OverflowError, posix.sched_setparam, 0, param)
1339
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05001340 @unittest.skipUnless(hasattr(posix, "sched_rr_get_interval"), "no function")
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001341 def test_sched_rr_get_interval(self):
Benjamin Peterson43234ab2011-08-02 22:19:14 -05001342 try:
1343 interval = posix.sched_rr_get_interval(0)
1344 except OSError as e:
1345 # This likely means that sched_rr_get_interval is only valid for
1346 # processes with the SCHED_RR scheduler in effect.
1347 if e.errno != errno.EINVAL:
1348 raise
1349 self.skipTest("only works on SCHED_RR processes")
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001350 self.assertIsInstance(interval, float)
1351 # Reasonable constraints, I think.
1352 self.assertGreaterEqual(interval, 0.)
1353 self.assertLess(interval, 1.)
1354
Benjamin Peterson2740af82011-08-02 17:41:34 -05001355 @requires_sched_affinity
Antoine Pitrou84869872012-08-04 16:16:35 +02001356 def test_sched_getaffinity(self):
1357 mask = posix.sched_getaffinity(0)
1358 self.assertIsInstance(mask, set)
1359 self.assertGreaterEqual(len(mask), 1)
1360 self.assertRaises(OSError, posix.sched_getaffinity, -1)
1361 for cpu in mask:
1362 self.assertIsInstance(cpu, int)
1363 self.assertGreaterEqual(cpu, 0)
1364 self.assertLess(cpu, 1 << 32)
1365
1366 @requires_sched_affinity
1367 def test_sched_setaffinity(self):
1368 mask = posix.sched_getaffinity(0)
1369 if len(mask) > 1:
1370 # Empty masks are forbidden
1371 mask.pop()
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001372 posix.sched_setaffinity(0, mask)
Antoine Pitrou84869872012-08-04 16:16:35 +02001373 self.assertEqual(posix.sched_getaffinity(0), mask)
1374 self.assertRaises(OSError, posix.sched_setaffinity, 0, [])
1375 self.assertRaises(ValueError, posix.sched_setaffinity, 0, [-10])
1376 self.assertRaises(OverflowError, posix.sched_setaffinity, 0, [1<<128])
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001377 self.assertRaises(OSError, posix.sched_setaffinity, -1, mask)
1378
Victor Stinner8b905bd2011-10-25 13:34:04 +02001379 def test_rtld_constants(self):
1380 # check presence of major RTLD_* constants
1381 posix.RTLD_LAZY
1382 posix.RTLD_NOW
1383 posix.RTLD_GLOBAL
1384 posix.RTLD_LOCAL
1385
Jesus Cea60c13dd2012-06-23 02:58:14 +02001386 @unittest.skipUnless(hasattr(os, 'SEEK_HOLE'),
1387 "test needs an OS that reports file holes")
Hynek Schlawackf841e422012-06-24 09:51:46 +02001388 def test_fs_holes(self):
Jesus Cea94363612012-06-22 18:32:07 +02001389 # Even if the filesystem doesn't report holes,
1390 # if the OS supports it the SEEK_* constants
1391 # will be defined and will have a consistent
1392 # behaviour:
1393 # os.SEEK_DATA = current position
1394 # os.SEEK_HOLE = end of file position
Hynek Schlawackf841e422012-06-24 09:51:46 +02001395 with open(support.TESTFN, 'r+b') as fp:
Jesus Cea94363612012-06-22 18:32:07 +02001396 fp.write(b"hello")
1397 fp.flush()
1398 size = fp.tell()
1399 fno = fp.fileno()
Jesus Cead46f7d22012-07-07 14:56:04 +02001400 try :
1401 for i in range(size):
1402 self.assertEqual(i, os.lseek(fno, i, os.SEEK_DATA))
1403 self.assertLessEqual(size, os.lseek(fno, i, os.SEEK_HOLE))
1404 self.assertRaises(OSError, os.lseek, fno, size, os.SEEK_DATA)
1405 self.assertRaises(OSError, os.lseek, fno, size, os.SEEK_HOLE)
1406 except OSError :
1407 # Some OSs claim to support SEEK_HOLE/SEEK_DATA
1408 # but it is not true.
1409 # For instance:
1410 # http://lists.freebsd.org/pipermail/freebsd-amd64/2012-January/014332.html
1411 raise unittest.SkipTest("OSError raised!")
Jesus Cea94363612012-06-22 18:32:07 +02001412
Larry Hastingsb0827312014-02-09 22:05:19 -08001413 def test_path_error2(self):
1414 """
1415 Test functions that call path_error2(), providing two filenames in their exceptions.
1416 """
Victor Stinner047b7ae2014-10-05 17:37:41 +02001417 for name in ("rename", "replace", "link"):
Larry Hastingsb0827312014-02-09 22:05:19 -08001418 function = getattr(os, name, None)
Victor Stinnerbed04a72014-10-05 17:37:59 +02001419 if function is None:
1420 continue
Larry Hastingsb0827312014-02-09 22:05:19 -08001421
Victor Stinnerbed04a72014-10-05 17:37:59 +02001422 for dst in ("noodly2", support.TESTFN):
1423 try:
1424 function('doesnotexistfilename', dst)
1425 except OSError as e:
1426 self.assertIn("'doesnotexistfilename' -> '{}'".format(dst), str(e))
1427 break
1428 else:
1429 self.fail("No valid path_error2() test for os." + name)
Larry Hastingsb0827312014-02-09 22:05:19 -08001430
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001431 def test_path_with_null_character(self):
1432 fn = support.TESTFN
1433 fn_with_NUL = fn + '\0'
1434 self.addCleanup(support.unlink, fn)
1435 support.unlink(fn)
1436 fd = None
1437 try:
Serhiy Storchaka7e9d1d12015-04-20 10:12:28 +03001438 with self.assertRaises(ValueError):
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001439 fd = os.open(fn_with_NUL, os.O_WRONLY | os.O_CREAT) # raises
1440 finally:
1441 if fd is not None:
1442 os.close(fd)
1443 self.assertFalse(os.path.exists(fn))
Serhiy Storchaka7e9d1d12015-04-20 10:12:28 +03001444 self.assertRaises(ValueError, os.mkdir, fn_with_NUL)
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001445 self.assertFalse(os.path.exists(fn))
1446 open(fn, 'wb').close()
Serhiy Storchaka7e9d1d12015-04-20 10:12:28 +03001447 self.assertRaises(ValueError, os.stat, fn_with_NUL)
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001448
1449 def test_path_with_null_byte(self):
1450 fn = os.fsencode(support.TESTFN)
1451 fn_with_NUL = fn + b'\0'
1452 self.addCleanup(support.unlink, fn)
1453 support.unlink(fn)
1454 fd = None
1455 try:
1456 with self.assertRaises(ValueError):
1457 fd = os.open(fn_with_NUL, os.O_WRONLY | os.O_CREAT) # raises
1458 finally:
1459 if fd is not None:
1460 os.close(fd)
1461 self.assertFalse(os.path.exists(fn))
1462 self.assertRaises(ValueError, os.mkdir, fn_with_NUL)
1463 self.assertFalse(os.path.exists(fn))
1464 open(fn, 'wb').close()
1465 self.assertRaises(ValueError, os.stat, fn_with_NUL)
1466
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00001467class PosixGroupsTester(unittest.TestCase):
1468
1469 def setUp(self):
1470 if posix.getuid() != 0:
1471 raise unittest.SkipTest("not enough privileges")
1472 if not hasattr(posix, 'getgroups'):
1473 raise unittest.SkipTest("need posix.getgroups")
1474 if sys.platform == 'darwin':
1475 raise unittest.SkipTest("getgroups(2) is broken on OSX")
1476 self.saved_groups = posix.getgroups()
1477
1478 def tearDown(self):
1479 if hasattr(posix, 'setgroups'):
1480 posix.setgroups(self.saved_groups)
1481 elif hasattr(posix, 'initgroups'):
1482 name = pwd.getpwuid(posix.getuid()).pw_name
1483 posix.initgroups(name, self.saved_groups[0])
1484
1485 @unittest.skipUnless(hasattr(posix, 'initgroups'),
1486 "test needs posix.initgroups()")
1487 def test_initgroups(self):
1488 # find missing group
1489
Benjamin Peterson659a6f52014-03-01 19:14:12 -05001490 g = max(self.saved_groups or [0]) + 1
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00001491 name = pwd.getpwuid(posix.getuid()).pw_name
1492 posix.initgroups(name, g)
1493 self.assertIn(g, posix.getgroups())
1494
1495 @unittest.skipUnless(hasattr(posix, 'setgroups'),
1496 "test needs posix.setgroups()")
1497 def test_setgroups(self):
Antoine Pitroue5a91012010-09-04 17:32:06 +00001498 for groups in [[0], list(range(16))]:
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00001499 posix.setgroups(groups)
1500 self.assertListEqual(groups, posix.getgroups())
1501
Neal Norwitze241ce82003-02-17 18:17:05 +00001502def test_main():
Antoine Pitrou68c95922011-03-20 17:33:57 +01001503 try:
Pablo Galindo8e633a42018-05-14 17:52:43 -04001504 support.run_unittest(PosixTester, PosixGroupsTester)
Antoine Pitrou68c95922011-03-20 17:33:57 +01001505 finally:
1506 support.reap_children()
Neal Norwitze241ce82003-02-17 18:17:05 +00001507
1508if __name__ == '__main__':
1509 test_main()