blob: 44b8d6a7ad07961eb3c623226adbcf1f8a8eb636 [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
23class PosixTester(unittest.TestCase):
24
25 def setUp(self):
26 # create empty file
Benjamin Petersonee8712c2008-05-20 21:35:26 +000027 fp = open(support.TESTFN, 'w+')
Neal Norwitze241ce82003-02-17 18:17:05 +000028 fp.close()
Ned Deily3eb67d52011-06-28 00:00:28 -070029 self.teardown_files = [ support.TESTFN ]
Brett Cannonc8d502e2010-03-20 21:53:28 +000030 self._warnings_manager = support.check_warnings()
31 self._warnings_manager.__enter__()
32 warnings.filterwarnings('ignore', '.* potential security risk .*',
33 RuntimeWarning)
Neal Norwitze241ce82003-02-17 18:17:05 +000034
35 def tearDown(self):
Ned Deily3eb67d52011-06-28 00:00:28 -070036 for teardown_file in self.teardown_files:
37 support.unlink(teardown_file)
Brett Cannonc8d502e2010-03-20 21:53:28 +000038 self._warnings_manager.__exit__(None, None, None)
Neal Norwitze241ce82003-02-17 18:17:05 +000039
40 def testNoArgFunctions(self):
41 # test posix functions which take no arguments and have
42 # no side-effects which we need to cleanup (e.g., fork, wait, abort)
Guido van Rossumf0af3e32008-10-02 18:55:37 +000043 NO_ARG_FUNCTIONS = [ "ctermid", "getcwd", "getcwdb", "uname",
Guido van Rossum687b9c02007-10-25 23:18:51 +000044 "times", "getloadavg",
Neal Norwitze241ce82003-02-17 18:17:05 +000045 "getegid", "geteuid", "getgid", "getgroups",
Ross Lagerwall7807c352011-03-17 20:20:30 +020046 "getpid", "getpgrp", "getppid", "getuid", "sync",
Neal Norwitze241ce82003-02-17 18:17:05 +000047 ]
Neal Norwitz71b13e82003-02-23 22:12:24 +000048
Neal Norwitze241ce82003-02-17 18:17:05 +000049 for name in NO_ARG_FUNCTIONS:
50 posix_func = getattr(posix, name, None)
51 if posix_func is not None:
52 posix_func()
Neal Norwitz2ff51a82003-02-17 22:40:31 +000053 self.assertRaises(TypeError, posix_func, 1)
Neal Norwitze241ce82003-02-17 18:17:05 +000054
Serhiy Storchaka43767632013-11-03 21:31:38 +020055 @unittest.skipUnless(hasattr(posix, 'getresuid'),
56 'test needs posix.getresuid()')
57 def test_getresuid(self):
58 user_ids = posix.getresuid()
59 self.assertEqual(len(user_ids), 3)
60 for val in user_ids:
61 self.assertGreaterEqual(val, 0)
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000062
Serhiy Storchaka43767632013-11-03 21:31:38 +020063 @unittest.skipUnless(hasattr(posix, 'getresgid'),
64 'test needs posix.getresgid()')
65 def test_getresgid(self):
66 group_ids = posix.getresgid()
67 self.assertEqual(len(group_ids), 3)
68 for val in group_ids:
69 self.assertGreaterEqual(val, 0)
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000070
Serhiy Storchaka43767632013-11-03 21:31:38 +020071 @unittest.skipUnless(hasattr(posix, 'setresuid'),
72 'test needs posix.setresuid()')
73 def test_setresuid(self):
74 current_user_ids = posix.getresuid()
75 self.assertIsNone(posix.setresuid(*current_user_ids))
76 # -1 means don't change that value.
77 self.assertIsNone(posix.setresuid(-1, -1, -1))
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000078
Serhiy Storchaka43767632013-11-03 21:31:38 +020079 @unittest.skipUnless(hasattr(posix, 'setresuid'),
80 'test needs posix.setresuid()')
81 def test_setresuid_exception(self):
82 # Don't do this test if someone is silly enough to run us as root.
83 current_user_ids = posix.getresuid()
84 if 0 not in current_user_ids:
85 new_user_ids = (current_user_ids[0]+1, -1, -1)
86 self.assertRaises(OSError, posix.setresuid, *new_user_ids)
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000087
Serhiy Storchaka43767632013-11-03 21:31:38 +020088 @unittest.skipUnless(hasattr(posix, 'setresgid'),
89 'test needs posix.setresgid()')
90 def test_setresgid(self):
91 current_group_ids = posix.getresgid()
92 self.assertIsNone(posix.setresgid(*current_group_ids))
93 # -1 means don't change that value.
94 self.assertIsNone(posix.setresgid(-1, -1, -1))
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000095
Serhiy Storchaka43767632013-11-03 21:31:38 +020096 @unittest.skipUnless(hasattr(posix, 'setresgid'),
97 'test needs posix.setresgid()')
98 def test_setresgid_exception(self):
99 # Don't do this test if someone is silly enough to run us as root.
100 current_group_ids = posix.getresgid()
101 if 0 not in current_group_ids:
102 new_group_ids = (current_group_ids[0]+1, -1, -1)
103 self.assertRaises(OSError, posix.setresgid, *new_group_ids)
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000104
Antoine Pitroub7572f02009-12-02 20:46:48 +0000105 @unittest.skipUnless(hasattr(posix, 'initgroups'),
106 "test needs os.initgroups()")
107 def test_initgroups(self):
108 # It takes a string and an integer; check that it raises a TypeError
109 # for other argument lists.
110 self.assertRaises(TypeError, posix.initgroups)
111 self.assertRaises(TypeError, posix.initgroups, None)
112 self.assertRaises(TypeError, posix.initgroups, 3, "foo")
113 self.assertRaises(TypeError, posix.initgroups, "foo", 3, object())
114
115 # If a non-privileged user invokes it, it should fail with OSError
116 # EPERM.
117 if os.getuid() != 0:
Charles-François Natalie8a255a2012-05-02 20:01:38 +0200118 try:
119 name = pwd.getpwuid(posix.getuid()).pw_name
120 except KeyError:
121 # the current UID may not have a pwd entry
122 raise unittest.SkipTest("need a pwd entry")
Antoine Pitroub7572f02009-12-02 20:46:48 +0000123 try:
124 posix.initgroups(name, 13)
125 except OSError as e:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000126 self.assertEqual(e.errno, errno.EPERM)
Antoine Pitroub7572f02009-12-02 20:46:48 +0000127 else:
128 self.fail("Expected OSError to be raised by initgroups")
129
Serhiy Storchaka43767632013-11-03 21:31:38 +0200130 @unittest.skipUnless(hasattr(posix, 'statvfs'),
131 'test needs posix.statvfs()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000132 def test_statvfs(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200133 self.assertTrue(posix.statvfs(os.curdir))
Neal Norwitze241ce82003-02-17 18:17:05 +0000134
Serhiy Storchaka43767632013-11-03 21:31:38 +0200135 @unittest.skipUnless(hasattr(posix, 'fstatvfs'),
136 'test needs posix.fstatvfs()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000137 def test_fstatvfs(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200138 fp = open(support.TESTFN)
139 try:
140 self.assertTrue(posix.fstatvfs(fp.fileno()))
141 self.assertTrue(posix.statvfs(fp.fileno()))
142 finally:
143 fp.close()
Neal Norwitze241ce82003-02-17 18:17:05 +0000144
Serhiy Storchaka43767632013-11-03 21:31:38 +0200145 @unittest.skipUnless(hasattr(posix, 'ftruncate'),
146 'test needs posix.ftruncate()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000147 def test_ftruncate(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200148 fp = open(support.TESTFN, 'w+')
149 try:
150 # we need to have some data to truncate
151 fp.write('test')
152 fp.flush()
153 posix.ftruncate(fp.fileno(), 0)
154 finally:
155 fp.close()
Neal Norwitze241ce82003-02-17 18:17:05 +0000156
Ross Lagerwall7807c352011-03-17 20:20:30 +0200157 @unittest.skipUnless(hasattr(posix, 'truncate'), "test needs posix.truncate()")
158 def test_truncate(self):
159 with open(support.TESTFN, 'w') as fp:
160 fp.write('test')
161 fp.flush()
162 posix.truncate(support.TESTFN, 0)
163
Larry Hastings9cf065c2012-06-22 16:30:09 -0700164 @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 +0200165 @unittest.skipUnless(hasattr(os, 'fork'), "test needs os.fork()")
Ross Lagerwalldedf6cf2011-03-20 18:27:05 +0200166 @unittest.skipUnless(hasattr(os, 'waitpid'), "test needs os.waitpid()")
Ross Lagerwall7807c352011-03-17 20:20:30 +0200167 def test_fexecve(self):
168 fp = os.open(sys.executable, os.O_RDONLY)
169 try:
170 pid = os.fork()
171 if pid == 0:
172 os.chdir(os.path.split(sys.executable)[0])
Larry Hastings9cf065c2012-06-22 16:30:09 -0700173 posix.execve(fp, [sys.executable, '-c', 'pass'], os.environ)
Ross Lagerwall7807c352011-03-17 20:20:30 +0200174 else:
Ross Lagerwalldedf6cf2011-03-20 18:27:05 +0200175 self.assertEqual(os.waitpid(pid, 0), (pid, 0))
Ross Lagerwall7807c352011-03-17 20:20:30 +0200176 finally:
177 os.close(fp)
178
179 @unittest.skipUnless(hasattr(posix, 'waitid'), "test needs posix.waitid()")
180 @unittest.skipUnless(hasattr(os, 'fork'), "test needs os.fork()")
181 def test_waitid(self):
182 pid = os.fork()
183 if pid == 0:
184 os.chdir(os.path.split(sys.executable)[0])
185 posix.execve(sys.executable, [sys.executable, '-c', 'pass'], os.environ)
186 else:
187 res = posix.waitid(posix.P_PID, pid, posix.WEXITED)
188 self.assertEqual(pid, res.si_pid)
189
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200190 @unittest.skipUnless(hasattr(os, 'fork'), "test needs os.fork()")
Gregory P. Smith163468a2017-05-29 10:03:41 -0700191 def test_register_at_fork(self):
192 with self.assertRaises(TypeError, msg="Positional args not allowed"):
193 os.register_at_fork(lambda: None)
194 with self.assertRaises(TypeError, msg="Args must be callable"):
195 os.register_at_fork(before=2)
196 with self.assertRaises(TypeError, msg="Args must be callable"):
197 os.register_at_fork(after_in_child="three")
198 with self.assertRaises(TypeError, msg="Args must be callable"):
199 os.register_at_fork(after_in_parent=b"Five")
200 with self.assertRaises(TypeError, msg="Args must not be None"):
201 os.register_at_fork(before=None)
202 with self.assertRaises(TypeError, msg="Args must not be None"):
203 os.register_at_fork(after_in_child=None)
204 with self.assertRaises(TypeError, msg="Args must not be None"):
205 os.register_at_fork(after_in_parent=None)
206 with self.assertRaises(TypeError, msg="Invalid arg was allowed"):
207 # Ensure a combination of valid and invalid is an error.
208 os.register_at_fork(before=None, after_in_parent=lambda: 3)
209 with self.assertRaises(TypeError, msg="Invalid arg was allowed"):
210 # Ensure a combination of valid and invalid is an error.
211 os.register_at_fork(before=lambda: None, after_in_child='')
212 # We test actual registrations in their own process so as not to
213 # pollute this one. There is no way to unregister for cleanup.
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200214 code = """if 1:
215 import os
216
217 r, w = os.pipe()
218 fin_r, fin_w = os.pipe()
219
Gregory P. Smith163468a2017-05-29 10:03:41 -0700220 os.register_at_fork(before=lambda: os.write(w, b'A'))
221 os.register_at_fork(after_in_parent=lambda: os.write(w, b'C'))
222 os.register_at_fork(after_in_child=lambda: os.write(w, b'E'))
223 os.register_at_fork(before=lambda: os.write(w, b'B'),
224 after_in_parent=lambda: os.write(w, b'D'),
225 after_in_child=lambda: os.write(w, b'F'))
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200226
227 pid = os.fork()
228 if pid == 0:
229 # At this point, after-forkers have already been executed
230 os.close(w)
231 # Wait for parent to tell us to exit
232 os.read(fin_r, 1)
233 os._exit(0)
234 else:
235 try:
236 os.close(w)
237 with open(r, "rb") as f:
238 data = f.read()
239 assert len(data) == 6, data
240 # Check before-fork callbacks
241 assert data[:2] == b'BA', data
242 # Check after-fork callbacks
243 assert sorted(data[2:]) == list(b'CDEF'), data
244 assert data.index(b'C') < data.index(b'D'), data
245 assert data.index(b'E') < data.index(b'F'), data
246 finally:
247 os.write(fin_w, b'!')
248 """
249 assert_python_ok('-c', code)
250
Ross Lagerwall7807c352011-03-17 20:20:30 +0200251 @unittest.skipUnless(hasattr(posix, 'lockf'), "test needs posix.lockf()")
252 def test_lockf(self):
253 fd = os.open(support.TESTFN, os.O_WRONLY | os.O_CREAT)
254 try:
255 os.write(fd, b'test')
256 os.lseek(fd, 0, os.SEEK_SET)
257 posix.lockf(fd, posix.F_LOCK, 4)
258 # section is locked
259 posix.lockf(fd, posix.F_ULOCK, 4)
260 finally:
261 os.close(fd)
262
263 @unittest.skipUnless(hasattr(posix, 'pread'), "test needs posix.pread()")
264 def test_pread(self):
265 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
266 try:
267 os.write(fd, b'test')
268 os.lseek(fd, 0, os.SEEK_SET)
269 self.assertEqual(b'es', posix.pread(fd, 2, 1))
Florent Xiclunae41f0de2011-11-11 19:39:25 +0100270 # the first pread() shouldn't disturb the file offset
Ross Lagerwall7807c352011-03-17 20:20:30 +0200271 self.assertEqual(b'te', posix.read(fd, 2))
272 finally:
273 os.close(fd)
274
275 @unittest.skipUnless(hasattr(posix, 'pwrite'), "test needs posix.pwrite()")
276 def test_pwrite(self):
277 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
278 try:
279 os.write(fd, b'test')
280 os.lseek(fd, 0, os.SEEK_SET)
281 posix.pwrite(fd, b'xx', 1)
282 self.assertEqual(b'txxt', posix.read(fd, 4))
283 finally:
284 os.close(fd)
285
286 @unittest.skipUnless(hasattr(posix, 'posix_fallocate'),
287 "test needs posix.posix_fallocate()")
288 def test_posix_fallocate(self):
289 fd = os.open(support.TESTFN, os.O_WRONLY | os.O_CREAT)
290 try:
291 posix.posix_fallocate(fd, 0, 10)
292 except OSError as inst:
293 # issue10812, ZFS doesn't appear to support posix_fallocate,
294 # so skip Solaris-based since they are likely to have ZFS.
295 if inst.errno != errno.EINVAL or not sys.platform.startswith("sunos"):
296 raise
297 finally:
298 os.close(fd)
299
Коренберг Маркd4b93e22017-08-14 18:55:16 +0500300 # issue31106 - posix_fallocate() does not set error in errno.
301 @unittest.skipUnless(hasattr(posix, 'posix_fallocate'),
302 "test needs posix.posix_fallocate()")
303 def test_posix_fallocate_errno(self):
304 try:
305 posix.posix_fallocate(-42, 0, 10)
306 except OSError as inst:
307 if inst.errno != errno.EBADF:
308 raise
309
Ross Lagerwall7807c352011-03-17 20:20:30 +0200310 @unittest.skipUnless(hasattr(posix, 'posix_fadvise'),
311 "test needs posix.posix_fadvise()")
312 def test_posix_fadvise(self):
313 fd = os.open(support.TESTFN, os.O_RDONLY)
314 try:
315 posix.posix_fadvise(fd, 0, 0, posix.POSIX_FADV_WILLNEED)
316 finally:
317 os.close(fd)
318
Коренберг Маркd4b93e22017-08-14 18:55:16 +0500319 @unittest.skipUnless(hasattr(posix, 'posix_fadvise'),
320 "test needs posix.posix_fadvise()")
321 def test_posix_fadvise_errno(self):
322 try:
323 posix.posix_fadvise(-42, 0, 0, posix.POSIX_FADV_WILLNEED)
324 except OSError as inst:
325 if inst.errno != errno.EBADF:
326 raise
327
Larry Hastings9cf065c2012-06-22 16:30:09 -0700328 @unittest.skipUnless(os.utime in os.supports_fd, "test needs fd support in os.utime")
329 def test_utime_with_fd(self):
Ross Lagerwall7807c352011-03-17 20:20:30 +0200330 now = time.time()
331 fd = os.open(support.TESTFN, os.O_RDONLY)
332 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -0700333 posix.utime(fd)
334 posix.utime(fd, None)
335 self.assertRaises(TypeError, posix.utime, fd, (None, None))
336 self.assertRaises(TypeError, posix.utime, fd, (now, None))
337 self.assertRaises(TypeError, posix.utime, fd, (None, now))
338 posix.utime(fd, (int(now), int(now)))
339 posix.utime(fd, (now, now))
340 self.assertRaises(ValueError, posix.utime, fd, (now, now), ns=(now, now))
341 self.assertRaises(ValueError, posix.utime, fd, (now, 0), ns=(None, None))
342 self.assertRaises(ValueError, posix.utime, fd, (None, None), ns=(now, 0))
343 posix.utime(fd, (int(now), int((now - int(now)) * 1e9)))
344 posix.utime(fd, ns=(int(now), int((now - int(now)) * 1e9)))
345
Ross Lagerwall7807c352011-03-17 20:20:30 +0200346 finally:
347 os.close(fd)
348
Larry Hastings9cf065c2012-06-22 16:30:09 -0700349 @unittest.skipUnless(os.utime in os.supports_follow_symlinks, "test needs follow_symlinks support in os.utime")
350 def test_utime_nofollow_symlinks(self):
Ross Lagerwall7807c352011-03-17 20:20:30 +0200351 now = time.time()
Larry Hastings9cf065c2012-06-22 16:30:09 -0700352 posix.utime(support.TESTFN, None, follow_symlinks=False)
353 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, None), follow_symlinks=False)
354 self.assertRaises(TypeError, posix.utime, support.TESTFN, (now, None), follow_symlinks=False)
355 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, now), follow_symlinks=False)
356 posix.utime(support.TESTFN, (int(now), int(now)), follow_symlinks=False)
357 posix.utime(support.TESTFN, (now, now), follow_symlinks=False)
358 posix.utime(support.TESTFN, follow_symlinks=False)
Ross Lagerwall7807c352011-03-17 20:20:30 +0200359
360 @unittest.skipUnless(hasattr(posix, 'writev'), "test needs posix.writev()")
361 def test_writev(self):
362 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
363 try:
Victor Stinner57ddf782014-01-08 15:21:28 +0100364 n = os.writev(fd, (b'test1', b'tt2', b't3'))
365 self.assertEqual(n, 10)
366
Ross Lagerwall7807c352011-03-17 20:20:30 +0200367 os.lseek(fd, 0, os.SEEK_SET)
368 self.assertEqual(b'test1tt2t3', posix.read(fd, 10))
Victor Stinner57ddf782014-01-08 15:21:28 +0100369
370 # Issue #20113: empty list of buffers should not crash
Victor Stinnercd5ca6a2014-01-08 16:01:31 +0100371 try:
372 size = posix.writev(fd, [])
373 except OSError:
374 # writev(fd, []) raises OSError(22, "Invalid argument")
375 # on OpenIndiana
376 pass
377 else:
378 self.assertEqual(size, 0)
Ross Lagerwall7807c352011-03-17 20:20:30 +0200379 finally:
380 os.close(fd)
381
382 @unittest.skipUnless(hasattr(posix, 'readv'), "test needs posix.readv()")
383 def test_readv(self):
384 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
385 try:
386 os.write(fd, b'test1tt2t3')
387 os.lseek(fd, 0, os.SEEK_SET)
388 buf = [bytearray(i) for i in [5, 3, 2]]
389 self.assertEqual(posix.readv(fd, buf), 10)
390 self.assertEqual([b'test1', b'tt2', b't3'], [bytes(i) for i in buf])
Victor Stinner57ddf782014-01-08 15:21:28 +0100391
392 # Issue #20113: empty list of buffers should not crash
Victor Stinnercd5ca6a2014-01-08 16:01:31 +0100393 try:
394 size = posix.readv(fd, [])
395 except OSError:
396 # readv(fd, []) raises OSError(22, "Invalid argument")
397 # on OpenIndiana
398 pass
399 else:
400 self.assertEqual(size, 0)
Ross Lagerwall7807c352011-03-17 20:20:30 +0200401 finally:
402 os.close(fd)
403
Serhiy Storchaka43767632013-11-03 21:31:38 +0200404 @unittest.skipUnless(hasattr(posix, 'dup'),
405 'test needs posix.dup()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000406 def test_dup(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200407 fp = open(support.TESTFN)
408 try:
409 fd = posix.dup(fp.fileno())
410 self.assertIsInstance(fd, int)
411 os.close(fd)
412 finally:
413 fp.close()
Neal Norwitze241ce82003-02-17 18:17:05 +0000414
Serhiy Storchaka43767632013-11-03 21:31:38 +0200415 @unittest.skipUnless(hasattr(posix, 'confstr'),
416 'test needs posix.confstr()')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000417 def test_confstr(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200418 self.assertRaises(ValueError, posix.confstr, "CS_garbage")
419 self.assertEqual(len(posix.confstr("CS_PATH")) > 0, True)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000420
Serhiy Storchaka43767632013-11-03 21:31:38 +0200421 @unittest.skipUnless(hasattr(posix, 'dup2'),
422 'test needs posix.dup2()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000423 def test_dup2(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200424 fp1 = open(support.TESTFN)
425 fp2 = open(support.TESTFN)
426 try:
427 posix.dup2(fp1.fileno(), fp2.fileno())
428 finally:
429 fp1.close()
430 fp2.close()
Neal Norwitze241ce82003-02-17 18:17:05 +0000431
Charles-François Natali1e045b12011-05-22 20:42:32 +0200432 @unittest.skipUnless(hasattr(os, 'O_CLOEXEC'), "needs os.O_CLOEXEC")
Charles-François Natali239bb962011-06-03 12:55:15 +0200433 @support.requires_linux_version(2, 6, 23)
Charles-François Natali1e045b12011-05-22 20:42:32 +0200434 def test_oscloexec(self):
435 fd = os.open(support.TESTFN, os.O_RDONLY|os.O_CLOEXEC)
436 self.addCleanup(os.close, fd)
Victor Stinner1db9e7b2014-07-29 22:32:47 +0200437 self.assertFalse(os.get_inheritable(fd))
Charles-François Natali1e045b12011-05-22 20:42:32 +0200438
Serhiy Storchaka43767632013-11-03 21:31:38 +0200439 @unittest.skipUnless(hasattr(posix, 'O_EXLOCK'),
440 'test needs posix.O_EXLOCK')
Skip Montanaro98470002005-06-17 01:14:49 +0000441 def test_osexlock(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200442 fd = os.open(support.TESTFN,
443 os.O_WRONLY|os.O_EXLOCK|os.O_CREAT)
444 self.assertRaises(OSError, os.open, support.TESTFN,
445 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
446 os.close(fd)
447
448 if hasattr(posix, "O_SHLOCK"):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000449 fd = os.open(support.TESTFN,
Serhiy Storchaka43767632013-11-03 21:31:38 +0200450 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000451 self.assertRaises(OSError, os.open, support.TESTFN,
Skip Montanaro98470002005-06-17 01:14:49 +0000452 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
453 os.close(fd)
454
Serhiy Storchaka43767632013-11-03 21:31:38 +0200455 @unittest.skipUnless(hasattr(posix, 'O_SHLOCK'),
456 'test needs posix.O_SHLOCK')
Skip Montanaro98470002005-06-17 01:14:49 +0000457 def test_osshlock(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200458 fd1 = os.open(support.TESTFN,
459 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
460 fd2 = os.open(support.TESTFN,
461 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
462 os.close(fd2)
463 os.close(fd1)
464
465 if hasattr(posix, "O_EXLOCK"):
466 fd = os.open(support.TESTFN,
Skip Montanaro98470002005-06-17 01:14:49 +0000467 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
Serhiy Storchaka43767632013-11-03 21:31:38 +0200468 self.assertRaises(OSError, os.open, support.TESTFN,
469 os.O_RDONLY|os.O_EXLOCK|os.O_NONBLOCK)
470 os.close(fd)
Skip Montanaro98470002005-06-17 01:14:49 +0000471
Serhiy Storchaka43767632013-11-03 21:31:38 +0200472 @unittest.skipUnless(hasattr(posix, 'fstat'),
473 'test needs posix.fstat()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000474 def test_fstat(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200475 fp = open(support.TESTFN)
476 try:
477 self.assertTrue(posix.fstat(fp.fileno()))
478 self.assertTrue(posix.stat(fp.fileno()))
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200479
Serhiy Storchaka43767632013-11-03 21:31:38 +0200480 self.assertRaisesRegex(TypeError,
Brett Cannon3f9183b2016-08-26 14:44:48 -0700481 'should be string, bytes, os.PathLike or integer, not',
Serhiy Storchaka43767632013-11-03 21:31:38 +0200482 posix.stat, float(fp.fileno()))
483 finally:
484 fp.close()
Neal Norwitze241ce82003-02-17 18:17:05 +0000485
Serhiy Storchaka43767632013-11-03 21:31:38 +0200486 @unittest.skipUnless(hasattr(posix, 'stat'),
487 'test needs posix.stat()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000488 def test_stat(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200489 self.assertTrue(posix.stat(support.TESTFN))
490 self.assertTrue(posix.stat(os.fsencode(support.TESTFN)))
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200491
Serhiy Storchakad73c3182016-08-06 23:22:08 +0300492 self.assertWarnsRegex(DeprecationWarning,
Brett Cannon3f9183b2016-08-26 14:44:48 -0700493 'should be string, bytes, os.PathLike or integer, not',
Serhiy Storchakad73c3182016-08-06 23:22:08 +0300494 posix.stat, bytearray(os.fsencode(support.TESTFN)))
Serhiy Storchaka43767632013-11-03 21:31:38 +0200495 self.assertRaisesRegex(TypeError,
Brett Cannon3f9183b2016-08-26 14:44:48 -0700496 'should be string, bytes, os.PathLike or integer, not',
Serhiy Storchaka43767632013-11-03 21:31:38 +0200497 posix.stat, None)
498 self.assertRaisesRegex(TypeError,
Brett Cannon3f9183b2016-08-26 14:44:48 -0700499 'should be string, bytes, os.PathLike or integer, not',
Serhiy Storchaka43767632013-11-03 21:31:38 +0200500 posix.stat, list(support.TESTFN))
501 self.assertRaisesRegex(TypeError,
Brett Cannon3f9183b2016-08-26 14:44:48 -0700502 'should be string, bytes, os.PathLike or integer, not',
Serhiy Storchaka43767632013-11-03 21:31:38 +0200503 posix.stat, list(os.fsencode(support.TESTFN)))
Neal Norwitze241ce82003-02-17 18:17:05 +0000504
Benjamin Peterson052a02b2010-08-17 01:27:09 +0000505 @unittest.skipUnless(hasattr(posix, 'mkfifo'), "don't have mkfifo()")
506 def test_mkfifo(self):
507 support.unlink(support.TESTFN)
xdegaye92c2ca72017-11-12 17:31:07 +0100508 try:
509 posix.mkfifo(support.TESTFN, stat.S_IRUSR | stat.S_IWUSR)
510 except PermissionError as e:
511 self.skipTest('posix.mkfifo(): %s' % e)
Benjamin Peterson052a02b2010-08-17 01:27:09 +0000512 self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode))
513
514 @unittest.skipUnless(hasattr(posix, 'mknod') and hasattr(stat, 'S_IFIFO'),
515 "don't have mknod()/S_IFIFO")
516 def test_mknod(self):
517 # Test using mknod() to create a FIFO (the only use specified
518 # by POSIX).
519 support.unlink(support.TESTFN)
520 mode = stat.S_IFIFO | stat.S_IRUSR | stat.S_IWUSR
521 try:
522 posix.mknod(support.TESTFN, mode, 0)
523 except OSError as e:
524 # Some old systems don't allow unprivileged users to use
525 # mknod(), or only support creating device nodes.
xdegaye92c2ca72017-11-12 17:31:07 +0100526 self.assertIn(e.errno, (errno.EPERM, errno.EINVAL, errno.EACCES))
Benjamin Peterson052a02b2010-08-17 01:27:09 +0000527 else:
528 self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode))
529
Martin Panterbf19d162015-09-09 01:01:13 +0000530 # Keyword arguments are also supported
531 support.unlink(support.TESTFN)
532 try:
533 posix.mknod(path=support.TESTFN, mode=mode, device=0,
534 dir_fd=None)
535 except OSError as e:
xdegaye92c2ca72017-11-12 17:31:07 +0100536 self.assertIn(e.errno, (errno.EPERM, errno.EINVAL, errno.EACCES))
Martin Panterbf19d162015-09-09 01:01:13 +0000537
Serhiy Storchaka16b2e4f2015-04-20 09:22:13 +0300538 @unittest.skipUnless(hasattr(posix, 'stat'), 'test needs posix.stat()')
539 @unittest.skipUnless(hasattr(posix, 'makedev'), 'test needs posix.makedev()')
540 def test_makedev(self):
541 st = posix.stat(support.TESTFN)
542 dev = st.st_dev
543 self.assertIsInstance(dev, int)
544 self.assertGreaterEqual(dev, 0)
545
546 major = posix.major(dev)
547 self.assertIsInstance(major, int)
548 self.assertGreaterEqual(major, 0)
549 self.assertEqual(posix.major(dev), major)
550 self.assertRaises(TypeError, posix.major, float(dev))
551 self.assertRaises(TypeError, posix.major)
552 self.assertRaises((ValueError, OverflowError), posix.major, -1)
553
554 minor = posix.minor(dev)
555 self.assertIsInstance(minor, int)
556 self.assertGreaterEqual(minor, 0)
557 self.assertEqual(posix.minor(dev), minor)
558 self.assertRaises(TypeError, posix.minor, float(dev))
559 self.assertRaises(TypeError, posix.minor)
560 self.assertRaises((ValueError, OverflowError), posix.minor, -1)
561
Victor Stinner12953ff2017-07-27 16:55:54 +0200562 if sys.platform.startswith('freebsd') and dev >= 0x1_0000_0000:
563 self.skipTest("bpo-31044: on FreeBSD CURRENT, minor() truncates "
564 "64-bit dev to 32-bit")
565
Serhiy Storchaka16b2e4f2015-04-20 09:22:13 +0300566 self.assertEqual(posix.makedev(major, minor), dev)
567 self.assertRaises(TypeError, posix.makedev, float(major), minor)
568 self.assertRaises(TypeError, posix.makedev, major, float(minor))
569 self.assertRaises(TypeError, posix.makedev, major)
570 self.assertRaises(TypeError, posix.makedev)
571
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200572 def _test_all_chown_common(self, chown_func, first_param, stat_func):
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000573 """Common code for chown, fchown and lchown tests."""
Serhiy Storchaka54db2fd2013-02-20 19:40:25 +0200574 def check_stat(uid, gid):
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200575 if stat_func is not None:
576 stat = stat_func(first_param)
Serhiy Storchaka54db2fd2013-02-20 19:40:25 +0200577 self.assertEqual(stat.st_uid, uid)
578 self.assertEqual(stat.st_gid, gid)
579 uid = os.getuid()
580 gid = os.getgid()
Charles-François Nataliab2d58e2012-04-17 19:48:35 +0200581 # test a successful chown call
Serhiy Storchaka54db2fd2013-02-20 19:40:25 +0200582 chown_func(first_param, uid, gid)
583 check_stat(uid, gid)
584 chown_func(first_param, -1, gid)
585 check_stat(uid, gid)
586 chown_func(first_param, uid, -1)
587 check_stat(uid, gid)
Charles-François Nataliab2d58e2012-04-17 19:48:35 +0200588
Serhiy Storchaka54db2fd2013-02-20 19:40:25 +0200589 if uid == 0:
590 # Try an amusingly large uid/gid to make sure we handle
591 # large unsigned values. (chown lets you use any
592 # uid/gid you like, even if they aren't defined.)
593 #
594 # This problem keeps coming up:
595 # http://bugs.python.org/issue1747858
596 # http://bugs.python.org/issue4591
597 # http://bugs.python.org/issue15301
598 # Hopefully the fix in 4591 fixes it for good!
599 #
600 # This part of the test only runs when run as root.
601 # Only scary people run their tests as root.
602
603 big_value = 2**31
604 chown_func(first_param, big_value, big_value)
605 check_stat(big_value, big_value)
606 chown_func(first_param, -1, -1)
607 check_stat(big_value, big_value)
608 chown_func(first_param, uid, gid)
609 check_stat(uid, gid)
Charles-François Nataliab2d58e2012-04-17 19:48:35 +0200610 elif platform.system() in ('HP-UX', 'SunOS'):
611 # HP-UX and Solaris can allow a non-root user to chown() to root
612 # (issue #5113)
613 raise unittest.SkipTest("Skipping because of non-standard chown() "
614 "behavior")
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000615 else:
616 # non-root cannot chown to root, raises OSError
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200617 self.assertRaises(OSError, chown_func, first_param, 0, 0)
Serhiy Storchaka54db2fd2013-02-20 19:40:25 +0200618 check_stat(uid, gid)
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200619 self.assertRaises(OSError, chown_func, first_param, 0, -1)
Serhiy Storchaka54db2fd2013-02-20 19:40:25 +0200620 check_stat(uid, gid)
Serhiy Storchakaa2964b32013-02-21 14:34:36 +0200621 if 0 not in os.getgroups():
Serhiy Storchakab3d62ce2013-02-20 19:48:22 +0200622 self.assertRaises(OSError, chown_func, first_param, -1, 0)
623 check_stat(uid, gid)
Serhiy Storchaka54db2fd2013-02-20 19:40:25 +0200624 # test illegal types
625 for t in str, float:
626 self.assertRaises(TypeError, chown_func, first_param, t(uid), gid)
627 check_stat(uid, gid)
628 self.assertRaises(TypeError, chown_func, first_param, uid, t(gid))
629 check_stat(uid, gid)
Christian Heimesd5e2b6f2008-03-19 21:50:51 +0000630
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000631 @unittest.skipUnless(hasattr(posix, 'chown'), "test needs os.chown()")
632 def test_chown(self):
633 # raise an OSError if the file does not exist
634 os.unlink(support.TESTFN)
635 self.assertRaises(OSError, posix.chown, support.TESTFN, -1, -1)
Christian Heimesd5e2b6f2008-03-19 21:50:51 +0000636
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000637 # re-create the file
Victor Stinnerbf816222011-06-30 23:25:47 +0200638 support.create_empty_file(support.TESTFN)
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200639 self._test_all_chown_common(posix.chown, support.TESTFN,
640 getattr(posix, 'stat', None))
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000641
642 @unittest.skipUnless(hasattr(posix, 'fchown'), "test needs os.fchown()")
643 def test_fchown(self):
644 os.unlink(support.TESTFN)
645
646 # re-create the file
647 test_file = open(support.TESTFN, 'w')
648 try:
649 fd = test_file.fileno()
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200650 self._test_all_chown_common(posix.fchown, fd,
651 getattr(posix, 'fstat', None))
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000652 finally:
653 test_file.close()
654
655 @unittest.skipUnless(hasattr(posix, 'lchown'), "test needs os.lchown()")
656 def test_lchown(self):
657 os.unlink(support.TESTFN)
658 # create a symlink
Ned Deily3eb67d52011-06-28 00:00:28 -0700659 os.symlink(_DUMMY_SYMLINK, support.TESTFN)
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200660 self._test_all_chown_common(posix.lchown, support.TESTFN,
661 getattr(posix, 'lstat', None))
Christian Heimesd5e2b6f2008-03-19 21:50:51 +0000662
Serhiy Storchaka43767632013-11-03 21:31:38 +0200663 @unittest.skipUnless(hasattr(posix, 'chdir'), 'test needs posix.chdir()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000664 def test_chdir(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200665 posix.chdir(os.curdir)
666 self.assertRaises(OSError, posix.chdir, support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000667
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +0000668 def test_listdir(self):
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +0300669 self.assertIn(support.TESTFN, posix.listdir(os.curdir))
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +0000670
671 def test_listdir_default(self):
Larry Hastingsfdaea062012-06-25 04:42:23 -0700672 # When listdir is called without argument,
673 # it's the same as listdir(os.curdir).
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +0300674 self.assertIn(support.TESTFN, posix.listdir())
Neal Norwitze241ce82003-02-17 18:17:05 +0000675
Larry Hastingsfdaea062012-06-25 04:42:23 -0700676 def test_listdir_bytes(self):
677 # When listdir is called with a bytes object,
678 # the returned strings are of type bytes.
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +0300679 self.assertIn(os.fsencode(support.TESTFN), posix.listdir(b'.'))
680
681 def test_listdir_bytes_like(self):
682 for cls in bytearray, memoryview:
683 with self.assertWarns(DeprecationWarning):
684 names = posix.listdir(cls(b'.'))
685 self.assertIn(os.fsencode(support.TESTFN), names)
686 for name in names:
687 self.assertIs(type(name), bytes)
Larry Hastingsfdaea062012-06-25 04:42:23 -0700688
689 @unittest.skipUnless(posix.listdir in os.supports_fd,
690 "test needs fd support for posix.listdir()")
691 def test_listdir_fd(self):
Antoine Pitrou8250e232011-02-25 23:41:16 +0000692 f = posix.open(posix.getcwd(), posix.O_RDONLY)
Charles-François Natali7546ad32012-01-08 18:34:06 +0100693 self.addCleanup(posix.close, f)
Antoine Pitrou8250e232011-02-25 23:41:16 +0000694 self.assertEqual(
695 sorted(posix.listdir('.')),
Larry Hastings9cf065c2012-06-22 16:30:09 -0700696 sorted(posix.listdir(f))
Antoine Pitrou8250e232011-02-25 23:41:16 +0000697 )
Charles-François Natali7546ad32012-01-08 18:34:06 +0100698 # Check that the fd offset was reset (issue #13739)
Charles-François Natali7546ad32012-01-08 18:34:06 +0100699 self.assertEqual(
700 sorted(posix.listdir('.')),
Larry Hastings9cf065c2012-06-22 16:30:09 -0700701 sorted(posix.listdir(f))
Charles-François Natali7546ad32012-01-08 18:34:06 +0100702 )
Antoine Pitrou8250e232011-02-25 23:41:16 +0000703
Serhiy Storchaka43767632013-11-03 21:31:38 +0200704 @unittest.skipUnless(hasattr(posix, 'access'), 'test needs posix.access()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000705 def test_access(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200706 self.assertTrue(posix.access(support.TESTFN, os.R_OK))
Neal Norwitze241ce82003-02-17 18:17:05 +0000707
Serhiy Storchaka43767632013-11-03 21:31:38 +0200708 @unittest.skipUnless(hasattr(posix, 'umask'), 'test needs posix.umask()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000709 def test_umask(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200710 old_mask = posix.umask(0)
711 self.assertIsInstance(old_mask, int)
712 posix.umask(old_mask)
Neal Norwitze241ce82003-02-17 18:17:05 +0000713
Serhiy Storchaka43767632013-11-03 21:31:38 +0200714 @unittest.skipUnless(hasattr(posix, 'strerror'),
715 'test needs posix.strerror()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000716 def test_strerror(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200717 self.assertTrue(posix.strerror(0))
Neal Norwitze241ce82003-02-17 18:17:05 +0000718
Serhiy Storchaka43767632013-11-03 21:31:38 +0200719 @unittest.skipUnless(hasattr(posix, 'pipe'), 'test needs posix.pipe()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000720 def test_pipe(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200721 reader, writer = posix.pipe()
722 os.close(reader)
723 os.close(writer)
Neal Norwitze241ce82003-02-17 18:17:05 +0000724
Charles-François Natalidaafdd52011-05-29 20:07:40 +0200725 @unittest.skipUnless(hasattr(os, 'pipe2'), "test needs os.pipe2()")
Charles-François Natali239bb962011-06-03 12:55:15 +0200726 @support.requires_linux_version(2, 6, 27)
Charles-François Natalidaafdd52011-05-29 20:07:40 +0200727 def test_pipe2(self):
728 self.assertRaises(TypeError, os.pipe2, 'DEADBEEF')
729 self.assertRaises(TypeError, os.pipe2, 0, 0)
730
Charles-François Natali368f34b2011-06-06 19:49:47 +0200731 # try calling with flags = 0, like os.pipe()
732 r, w = os.pipe2(0)
Charles-François Natalidaafdd52011-05-29 20:07:40 +0200733 os.close(r)
734 os.close(w)
735
736 # test flags
737 r, w = os.pipe2(os.O_CLOEXEC|os.O_NONBLOCK)
738 self.addCleanup(os.close, r)
739 self.addCleanup(os.close, w)
Victor Stinnerbff989e2013-08-28 12:25:40 +0200740 self.assertFalse(os.get_inheritable(r))
741 self.assertFalse(os.get_inheritable(w))
Victor Stinner1db9e7b2014-07-29 22:32:47 +0200742 self.assertFalse(os.get_blocking(r))
743 self.assertFalse(os.get_blocking(w))
Charles-François Natalidaafdd52011-05-29 20:07:40 +0200744 # try reading from an empty pipe: this should fail, not block
745 self.assertRaises(OSError, os.read, r, 1)
746 # try a write big enough to fill-up the pipe: this should either
747 # fail or perform a partial write, not block
748 try:
749 os.write(w, b'x' * support.PIPE_MAX_SIZE)
750 except OSError:
751 pass
752
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +0200753 @support.cpython_only
754 @unittest.skipUnless(hasattr(os, 'pipe2'), "test needs os.pipe2()")
755 @support.requires_linux_version(2, 6, 27)
756 def test_pipe2_c_limits(self):
Serhiy Storchaka78980432013-01-15 01:12:17 +0200757 # Issue 15989
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +0200758 import _testcapi
Serhiy Storchaka78980432013-01-15 01:12:17 +0200759 self.assertRaises(OverflowError, os.pipe2, _testcapi.INT_MAX + 1)
760 self.assertRaises(OverflowError, os.pipe2, _testcapi.UINT_MAX + 1)
761
Serhiy Storchaka43767632013-11-03 21:31:38 +0200762 @unittest.skipUnless(hasattr(posix, 'utime'), 'test needs posix.utime()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000763 def test_utime(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200764 now = time.time()
765 posix.utime(support.TESTFN, None)
766 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, None))
767 self.assertRaises(TypeError, posix.utime, support.TESTFN, (now, None))
768 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, now))
769 posix.utime(support.TESTFN, (int(now), int(now)))
770 posix.utime(support.TESTFN, (now, now))
Neal Norwitze241ce82003-02-17 18:17:05 +0000771
Larry Hastings9cf065c2012-06-22 16:30:09 -0700772 def _test_chflags_regular_file(self, chflags_func, target_file, **kwargs):
Ned Deily3eb67d52011-06-28 00:00:28 -0700773 st = os.stat(target_file)
774 self.assertTrue(hasattr(st, 'st_flags'))
Trent Nelson75959cf2012-08-21 23:59:31 +0000775
776 # ZFS returns EOPNOTSUPP when attempting to set flag UF_IMMUTABLE.
777 flags = st.st_flags | stat.UF_IMMUTABLE
778 try:
779 chflags_func(target_file, flags, **kwargs)
780 except OSError as err:
781 if err.errno != errno.EOPNOTSUPP:
782 raise
783 msg = 'chflag UF_IMMUTABLE not supported by underlying fs'
784 self.skipTest(msg)
785
Ned Deily3eb67d52011-06-28 00:00:28 -0700786 try:
787 new_st = os.stat(target_file)
788 self.assertEqual(st.st_flags | stat.UF_IMMUTABLE, new_st.st_flags)
789 try:
790 fd = open(target_file, 'w+')
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200791 except OSError as e:
Ned Deily3eb67d52011-06-28 00:00:28 -0700792 self.assertEqual(e.errno, errno.EPERM)
793 finally:
794 posix.chflags(target_file, st.st_flags)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000795
Ned Deily3eb67d52011-06-28 00:00:28 -0700796 @unittest.skipUnless(hasattr(posix, 'chflags'), 'test needs os.chflags()')
797 def test_chflags(self):
798 self._test_chflags_regular_file(posix.chflags, support.TESTFN)
799
800 @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()')
801 def test_lchflags_regular_file(self):
802 self._test_chflags_regular_file(posix.lchflags, support.TESTFN)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700803 self._test_chflags_regular_file(posix.chflags, support.TESTFN, follow_symlinks=False)
Ned Deily3eb67d52011-06-28 00:00:28 -0700804
805 @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()')
806 def test_lchflags_symlink(self):
807 testfn_st = os.stat(support.TESTFN)
808
809 self.assertTrue(hasattr(testfn_st, 'st_flags'))
810
811 os.symlink(support.TESTFN, _DUMMY_SYMLINK)
812 self.teardown_files.append(_DUMMY_SYMLINK)
813 dummy_symlink_st = os.lstat(_DUMMY_SYMLINK)
814
Larry Hastings9cf065c2012-06-22 16:30:09 -0700815 def chflags_nofollow(path, flags):
816 return posix.chflags(path, flags, follow_symlinks=False)
Ned Deily3eb67d52011-06-28 00:00:28 -0700817
Larry Hastings9cf065c2012-06-22 16:30:09 -0700818 for fn in (posix.lchflags, chflags_nofollow):
Trent Nelson75959cf2012-08-21 23:59:31 +0000819 # ZFS returns EOPNOTSUPP when attempting to set flag UF_IMMUTABLE.
820 flags = dummy_symlink_st.st_flags | stat.UF_IMMUTABLE
821 try:
822 fn(_DUMMY_SYMLINK, flags)
823 except OSError as err:
824 if err.errno != errno.EOPNOTSUPP:
825 raise
826 msg = 'chflag UF_IMMUTABLE not supported by underlying fs'
827 self.skipTest(msg)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700828 try:
829 new_testfn_st = os.stat(support.TESTFN)
830 new_dummy_symlink_st = os.lstat(_DUMMY_SYMLINK)
831
832 self.assertEqual(testfn_st.st_flags, new_testfn_st.st_flags)
833 self.assertEqual(dummy_symlink_st.st_flags | stat.UF_IMMUTABLE,
834 new_dummy_symlink_st.st_flags)
835 finally:
836 fn(_DUMMY_SYMLINK, dummy_symlink_st.st_flags)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000837
Guido van Rossum98297ee2007-11-06 21:34:58 +0000838 def test_environ(self):
Victor Stinner17b490d2010-05-06 22:19:30 +0000839 if os.name == "nt":
840 item_type = str
841 else:
842 item_type = bytes
Guido van Rossum98297ee2007-11-06 21:34:58 +0000843 for k, v in posix.environ.items():
Victor Stinner17b490d2010-05-06 22:19:30 +0000844 self.assertEqual(type(k), item_type)
845 self.assertEqual(type(v), item_type)
Guido van Rossum98297ee2007-11-06 21:34:58 +0000846
Serhiy Storchaka77703942017-06-25 07:33:01 +0300847 @unittest.skipUnless(hasattr(os, "putenv"), "requires os.putenv()")
848 def test_putenv(self):
849 with self.assertRaises(ValueError):
850 os.putenv('FRUIT\0VEGETABLE', 'cabbage')
851 with self.assertRaises(ValueError):
852 os.putenv(b'FRUIT\0VEGETABLE', b'cabbage')
853 with self.assertRaises(ValueError):
854 os.putenv('FRUIT', 'orange\0VEGETABLE=cabbage')
855 with self.assertRaises(ValueError):
856 os.putenv(b'FRUIT', b'orange\0VEGETABLE=cabbage')
857 with self.assertRaises(ValueError):
858 os.putenv('FRUIT=ORANGE', 'lemon')
859 with self.assertRaises(ValueError):
860 os.putenv(b'FRUIT=ORANGE', b'lemon')
861
Serhiy Storchaka43767632013-11-03 21:31:38 +0200862 @unittest.skipUnless(hasattr(posix, 'getcwd'), 'test needs posix.getcwd()')
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000863 def test_getcwd_long_pathnames(self):
Benjamin Peterson3a7dffa2013-08-23 21:01:48 -0500864 dirname = 'getcwd-test-directory-0123456789abcdef-01234567890abcdef'
865 curdir = os.getcwd()
866 base_path = os.path.abspath(support.TESTFN) + '.getcwd'
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000867
Benjamin Peterson3a7dffa2013-08-23 21:01:48 -0500868 try:
869 os.mkdir(base_path)
870 os.chdir(base_path)
871 except:
872 # Just returning nothing instead of the SkipTest exception, because
873 # the test results in Error in that case. Is that ok?
874 # raise unittest.SkipTest("cannot create directory for testing")
875 return
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000876
Benjamin Peterson3a7dffa2013-08-23 21:01:48 -0500877 def _create_and_do_getcwd(dirname, current_path_length = 0):
878 try:
879 os.mkdir(dirname)
880 except:
881 raise unittest.SkipTest("mkdir cannot create directory sufficiently deep for getcwd test")
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000882
Benjamin Peterson3a7dffa2013-08-23 21:01:48 -0500883 os.chdir(dirname)
884 try:
885 os.getcwd()
886 if current_path_length < 1027:
887 _create_and_do_getcwd(dirname, current_path_length + len(dirname) + 1)
888 finally:
889 os.chdir('..')
890 os.rmdir(dirname)
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000891
Benjamin Peterson3a7dffa2013-08-23 21:01:48 -0500892 _create_and_do_getcwd(dirname)
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000893
Benjamin Peterson3a7dffa2013-08-23 21:01:48 -0500894 finally:
895 os.chdir(curdir)
896 support.rmtree(base_path)
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000897
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +0200898 @unittest.skipUnless(hasattr(posix, 'getgrouplist'), "test needs posix.getgrouplist()")
899 @unittest.skipUnless(hasattr(pwd, 'getpwuid'), "test needs pwd.getpwuid()")
900 @unittest.skipUnless(hasattr(os, 'getuid'), "test needs os.getuid()")
901 def test_getgrouplist(self):
Ross Lagerwalla0b315f2012-12-13 15:20:26 +0000902 user = pwd.getpwuid(os.getuid())[0]
903 group = pwd.getpwuid(os.getuid())[3]
904 self.assertIn(group, posix.getgrouplist(user, group))
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +0200905
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +0200906
Antoine Pitrou318b8f32011-01-12 18:45:27 +0000907 @unittest.skipUnless(hasattr(os, 'getegid'), "test needs os.getegid()")
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +0000908 def test_getgroups(self):
Jesus Cea61f32cb2014-06-28 18:39:35 +0200909 with os.popen('id -G 2>/dev/null') as idg:
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +0000910 groups = idg.read().strip()
Charles-François Natalie8a255a2012-05-02 20:01:38 +0200911 ret = idg.close()
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +0000912
Xavier de Gaye24c3b492016-10-19 11:00:26 +0200913 try:
914 idg_groups = set(int(g) for g in groups.split())
915 except ValueError:
916 idg_groups = set()
917 if ret is not None or not idg_groups:
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +0000918 raise unittest.SkipTest("need working 'id -G'")
919
Ned Deily028915e2013-02-02 15:08:52 -0800920 # Issues 16698: OS X ABIs prior to 10.6 have limits on getgroups()
921 if sys.platform == 'darwin':
922 import sysconfig
923 dt = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET') or '10.0'
Ned Deily04cdfa12014-06-25 13:36:14 -0700924 if tuple(int(n) for n in dt.split('.')[0:2]) < (10, 6):
Ned Deily028915e2013-02-02 15:08:52 -0800925 raise unittest.SkipTest("getgroups(2) is broken prior to 10.6")
926
Ronald Oussoren7fb6f512010-08-01 19:18:13 +0000927 # 'id -G' and 'os.getgroups()' should return the same
Xavier de Gaye24c3b492016-10-19 11:00:26 +0200928 # groups, ignoring order, duplicates, and the effective gid.
929 # #10822/#26944 - It is implementation defined whether
930 # posix.getgroups() includes the effective gid.
931 symdiff = idg_groups.symmetric_difference(posix.getgroups())
932 self.assertTrue(not symdiff or symdiff == {posix.getegid()})
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +0000933
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000934 # tests for the posix *at functions follow
935
Larry Hastings9cf065c2012-06-22 16:30:09 -0700936 @unittest.skipUnless(os.access in os.supports_dir_fd, "test needs dir_fd support for os.access()")
937 def test_access_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000938 f = posix.open(posix.getcwd(), posix.O_RDONLY)
939 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -0700940 self.assertTrue(posix.access(support.TESTFN, os.R_OK, dir_fd=f))
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000941 finally:
942 posix.close(f)
943
Larry Hastings9cf065c2012-06-22 16:30:09 -0700944 @unittest.skipUnless(os.chmod in os.supports_dir_fd, "test needs dir_fd support in os.chmod()")
945 def test_chmod_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000946 os.chmod(support.TESTFN, stat.S_IRUSR)
947
948 f = posix.open(posix.getcwd(), posix.O_RDONLY)
949 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -0700950 posix.chmod(support.TESTFN, stat.S_IRUSR | stat.S_IWUSR, dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000951
952 s = posix.stat(support.TESTFN)
953 self.assertEqual(s[0] & stat.S_IRWXU, stat.S_IRUSR | stat.S_IWUSR)
954 finally:
955 posix.close(f)
956
Larry Hastings9cf065c2012-06-22 16:30:09 -0700957 @unittest.skipUnless(os.chown in os.supports_dir_fd, "test needs dir_fd support in os.chown()")
958 def test_chown_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000959 support.unlink(support.TESTFN)
Victor Stinnerbf816222011-06-30 23:25:47 +0200960 support.create_empty_file(support.TESTFN)
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000961
962 f = posix.open(posix.getcwd(), posix.O_RDONLY)
963 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -0700964 posix.chown(support.TESTFN, os.getuid(), os.getgid(), dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000965 finally:
966 posix.close(f)
967
Larry Hastings9cf065c2012-06-22 16:30:09 -0700968 @unittest.skipUnless(os.stat in os.supports_dir_fd, "test needs dir_fd support in os.stat()")
969 def test_stat_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000970 support.unlink(support.TESTFN)
971 with open(support.TESTFN, 'w') as outfile:
972 outfile.write("testline\n")
973
974 f = posix.open(posix.getcwd(), posix.O_RDONLY)
975 try:
976 s1 = posix.stat(support.TESTFN)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700977 s2 = posix.stat(support.TESTFN, dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000978 self.assertEqual(s1, s2)
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200979 s2 = posix.stat(support.TESTFN, dir_fd=None)
980 self.assertEqual(s1, s2)
Serhiy Storchaka7155b882016-04-08 08:48:20 +0300981 self.assertRaisesRegex(TypeError, 'should be integer or None, not',
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200982 posix.stat, support.TESTFN, dir_fd=posix.getcwd())
Serhiy Storchaka7155b882016-04-08 08:48:20 +0300983 self.assertRaisesRegex(TypeError, 'should be integer or None, not',
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200984 posix.stat, support.TESTFN, dir_fd=float(f))
985 self.assertRaises(OverflowError,
986 posix.stat, support.TESTFN, dir_fd=10**20)
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000987 finally:
988 posix.close(f)
989
Larry Hastings9cf065c2012-06-22 16:30:09 -0700990 @unittest.skipUnless(os.utime in os.supports_dir_fd, "test needs dir_fd support in os.utime()")
991 def test_utime_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000992 f = posix.open(posix.getcwd(), posix.O_RDONLY)
993 try:
994 now = time.time()
Larry Hastings9cf065c2012-06-22 16:30:09 -0700995 posix.utime(support.TESTFN, None, dir_fd=f)
996 posix.utime(support.TESTFN, dir_fd=f)
997 self.assertRaises(TypeError, posix.utime, support.TESTFN, now, dir_fd=f)
998 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, None), dir_fd=f)
999 self.assertRaises(TypeError, posix.utime, support.TESTFN, (now, None), dir_fd=f)
1000 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, now), dir_fd=f)
1001 self.assertRaises(TypeError, posix.utime, support.TESTFN, (now, "x"), dir_fd=f)
1002 posix.utime(support.TESTFN, (int(now), int(now)), dir_fd=f)
1003 posix.utime(support.TESTFN, (now, now), dir_fd=f)
1004 posix.utime(support.TESTFN,
1005 (int(now), int((now - int(now)) * 1e9)), dir_fd=f)
1006 posix.utime(support.TESTFN, dir_fd=f,
1007 times=(int(now), int((now - int(now)) * 1e9)))
1008
Larry Hastings90867a52012-06-22 17:01:41 -07001009 # try dir_fd and follow_symlinks together
Larry Hastings9cf065c2012-06-22 16:30:09 -07001010 if os.utime in os.supports_follow_symlinks:
Larry Hastings90867a52012-06-22 17:01:41 -07001011 try:
1012 posix.utime(support.TESTFN, follow_symlinks=False, dir_fd=f)
Georg Brandl969288e2012-06-26 09:25:44 +02001013 except ValueError:
Larry Hastings90867a52012-06-22 17:01:41 -07001014 # whoops! using both together not supported on this platform.
1015 pass
Larry Hastings9cf065c2012-06-22 16:30:09 -07001016
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001017 finally:
1018 posix.close(f)
1019
Larry Hastings9cf065c2012-06-22 16:30:09 -07001020 @unittest.skipUnless(os.link in os.supports_dir_fd, "test needs dir_fd support in os.link()")
1021 def test_link_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001022 f = posix.open(posix.getcwd(), posix.O_RDONLY)
1023 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -07001024 posix.link(support.TESTFN, support.TESTFN + 'link', src_dir_fd=f, dst_dir_fd=f)
xdegaye92c2ca72017-11-12 17:31:07 +01001025 except PermissionError as e:
1026 self.skipTest('posix.link(): %s' % e)
1027 else:
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001028 # should have same inodes
1029 self.assertEqual(posix.stat(support.TESTFN)[1],
1030 posix.stat(support.TESTFN + 'link')[1])
1031 finally:
1032 posix.close(f)
1033 support.unlink(support.TESTFN + 'link')
1034
Larry Hastings9cf065c2012-06-22 16:30:09 -07001035 @unittest.skipUnless(os.mkdir in os.supports_dir_fd, "test needs dir_fd support in os.mkdir()")
1036 def test_mkdir_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001037 f = posix.open(posix.getcwd(), posix.O_RDONLY)
1038 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -07001039 posix.mkdir(support.TESTFN + 'dir', dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001040 posix.stat(support.TESTFN + 'dir') # should not raise exception
1041 finally:
1042 posix.close(f)
1043 support.rmtree(support.TESTFN + 'dir')
1044
Larry Hastings9cf065c2012-06-22 16:30:09 -07001045 @unittest.skipUnless((os.mknod in os.supports_dir_fd) and hasattr(stat, 'S_IFIFO'),
1046 "test requires both stat.S_IFIFO and dir_fd support for os.mknod()")
1047 def test_mknod_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001048 # Test using mknodat() to create a FIFO (the only use specified
1049 # by POSIX).
1050 support.unlink(support.TESTFN)
1051 mode = stat.S_IFIFO | stat.S_IRUSR | stat.S_IWUSR
1052 f = posix.open(posix.getcwd(), posix.O_RDONLY)
1053 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -07001054 posix.mknod(support.TESTFN, mode, 0, dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001055 except OSError as e:
1056 # Some old systems don't allow unprivileged users to use
1057 # mknod(), or only support creating device nodes.
xdegaye92c2ca72017-11-12 17:31:07 +01001058 self.assertIn(e.errno, (errno.EPERM, errno.EINVAL, errno.EACCES))
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001059 else:
1060 self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode))
1061 finally:
1062 posix.close(f)
1063
Larry Hastings9cf065c2012-06-22 16:30:09 -07001064 @unittest.skipUnless(os.open in os.supports_dir_fd, "test needs dir_fd support in os.open()")
1065 def test_open_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001066 support.unlink(support.TESTFN)
1067 with open(support.TESTFN, 'w') as outfile:
1068 outfile.write("testline\n")
1069 a = posix.open(posix.getcwd(), posix.O_RDONLY)
Larry Hastings9cf065c2012-06-22 16:30:09 -07001070 b = posix.open(support.TESTFN, posix.O_RDONLY, dir_fd=a)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001071 try:
1072 res = posix.read(b, 9).decode(encoding="utf-8")
1073 self.assertEqual("testline\n", res)
1074 finally:
1075 posix.close(a)
1076 posix.close(b)
1077
Larry Hastings9cf065c2012-06-22 16:30:09 -07001078 @unittest.skipUnless(os.readlink in os.supports_dir_fd, "test needs dir_fd support in os.readlink()")
1079 def test_readlink_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001080 os.symlink(support.TESTFN, support.TESTFN + 'link')
1081 f = posix.open(posix.getcwd(), posix.O_RDONLY)
1082 try:
1083 self.assertEqual(posix.readlink(support.TESTFN + 'link'),
Larry Hastings9cf065c2012-06-22 16:30:09 -07001084 posix.readlink(support.TESTFN + 'link', dir_fd=f))
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001085 finally:
1086 support.unlink(support.TESTFN + 'link')
1087 posix.close(f)
1088
Larry Hastings9cf065c2012-06-22 16:30:09 -07001089 @unittest.skipUnless(os.rename in os.supports_dir_fd, "test needs dir_fd support in os.rename()")
1090 def test_rename_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001091 support.unlink(support.TESTFN)
Victor Stinnerbf816222011-06-30 23:25:47 +02001092 support.create_empty_file(support.TESTFN + 'ren')
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001093 f = posix.open(posix.getcwd(), posix.O_RDONLY)
1094 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -07001095 posix.rename(support.TESTFN + 'ren', support.TESTFN, src_dir_fd=f, dst_dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001096 except:
1097 posix.rename(support.TESTFN + 'ren', support.TESTFN)
1098 raise
1099 else:
Andrew Svetlov5b898402012-12-18 21:26:36 +02001100 posix.stat(support.TESTFN) # should not raise exception
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001101 finally:
1102 posix.close(f)
1103
Larry Hastings9cf065c2012-06-22 16:30:09 -07001104 @unittest.skipUnless(os.symlink in os.supports_dir_fd, "test needs dir_fd support in os.symlink()")
1105 def test_symlink_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001106 f = posix.open(posix.getcwd(), posix.O_RDONLY)
1107 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -07001108 posix.symlink(support.TESTFN, support.TESTFN + 'link', dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001109 self.assertEqual(posix.readlink(support.TESTFN + 'link'), support.TESTFN)
1110 finally:
1111 posix.close(f)
1112 support.unlink(support.TESTFN + 'link')
1113
Larry Hastings9cf065c2012-06-22 16:30:09 -07001114 @unittest.skipUnless(os.unlink in os.supports_dir_fd, "test needs dir_fd support in os.unlink()")
1115 def test_unlink_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001116 f = posix.open(posix.getcwd(), posix.O_RDONLY)
Victor Stinnerbf816222011-06-30 23:25:47 +02001117 support.create_empty_file(support.TESTFN + 'del')
Andrew Svetlov5b898402012-12-18 21:26:36 +02001118 posix.stat(support.TESTFN + 'del') # should not raise exception
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001119 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -07001120 posix.unlink(support.TESTFN + 'del', dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001121 except:
1122 support.unlink(support.TESTFN + 'del')
1123 raise
1124 else:
1125 self.assertRaises(OSError, posix.stat, support.TESTFN + 'link')
1126 finally:
1127 posix.close(f)
1128
Larry Hastings9cf065c2012-06-22 16:30:09 -07001129 @unittest.skipUnless(os.mkfifo in os.supports_dir_fd, "test needs dir_fd support in os.mkfifo()")
1130 def test_mkfifo_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001131 support.unlink(support.TESTFN)
1132 f = posix.open(posix.getcwd(), posix.O_RDONLY)
1133 try:
xdegaye92c2ca72017-11-12 17:31:07 +01001134 try:
1135 posix.mkfifo(support.TESTFN,
1136 stat.S_IRUSR | stat.S_IWUSR, dir_fd=f)
1137 except PermissionError as e:
1138 self.skipTest('posix.mkfifo(): %s' % e)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001139 self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode))
1140 finally:
1141 posix.close(f)
1142
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001143 requires_sched_h = unittest.skipUnless(hasattr(posix, 'sched_yield'),
1144 "don't have scheduling support")
Antoine Pitrou84869872012-08-04 16:16:35 +02001145 requires_sched_affinity = unittest.skipUnless(hasattr(posix, 'sched_setaffinity'),
Benjamin Peterson50ba2712011-08-02 22:15:40 -05001146 "don't have sched affinity support")
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001147
1148 @requires_sched_h
1149 def test_sched_yield(self):
1150 # This has no error conditions (at least on Linux).
1151 posix.sched_yield()
1152
1153 @requires_sched_h
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02001154 @unittest.skipUnless(hasattr(posix, 'sched_get_priority_max'),
1155 "requires sched_get_priority_max()")
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001156 def test_sched_priority(self):
1157 # Round-robin usually has interesting priorities.
1158 pol = posix.SCHED_RR
1159 lo = posix.sched_get_priority_min(pol)
1160 hi = posix.sched_get_priority_max(pol)
1161 self.assertIsInstance(lo, int)
1162 self.assertIsInstance(hi, int)
1163 self.assertGreaterEqual(hi, lo)
Benjamin Peterson539b6c42011-08-02 22:09:37 -05001164 # OSX evidently just returns 15 without checking the argument.
1165 if sys.platform != "darwin":
Benjamin Petersonc1581582011-08-02 22:10:55 -05001166 self.assertRaises(OSError, posix.sched_get_priority_min, -23)
1167 self.assertRaises(OSError, posix.sched_get_priority_max, -23)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001168
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05001169 @unittest.skipUnless(hasattr(posix, 'sched_setscheduler'), "can't change scheduler")
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001170 def test_get_and_set_scheduler_and_param(self):
1171 possible_schedulers = [sched for name, sched in posix.__dict__.items()
1172 if name.startswith("SCHED_")]
1173 mine = posix.sched_getscheduler(0)
1174 self.assertIn(mine, possible_schedulers)
1175 try:
Jesus Ceaceb5d162011-09-10 01:16:55 +02001176 parent = posix.sched_getscheduler(os.getppid())
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001177 except OSError as e:
Jesus Ceaceb5d162011-09-10 01:16:55 +02001178 if e.errno != errno.EPERM:
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001179 raise
1180 else:
Jesus Ceaceb5d162011-09-10 01:16:55 +02001181 self.assertIn(parent, possible_schedulers)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001182 self.assertRaises(OSError, posix.sched_getscheduler, -1)
1183 self.assertRaises(OSError, posix.sched_getparam, -1)
1184 param = posix.sched_getparam(0)
1185 self.assertIsInstance(param.sched_priority, int)
Charles-François Natali7b911cb2011-08-21 12:41:43 +02001186
Charles-François Natalib402a5c2013-01-13 14:13:25 +01001187 # POSIX states that calling sched_setparam() or sched_setscheduler() on
1188 # a process with a scheduling policy other than SCHED_FIFO or SCHED_RR
1189 # is implementation-defined: NetBSD and FreeBSD can return EINVAL.
1190 if not sys.platform.startswith(('freebsd', 'netbsd')):
1191 try:
1192 posix.sched_setscheduler(0, mine, param)
1193 posix.sched_setparam(0, param)
1194 except OSError as e:
1195 if e.errno != errno.EPERM:
1196 raise
Charles-François Natali7b911cb2011-08-21 12:41:43 +02001197 self.assertRaises(OSError, posix.sched_setparam, -1, param)
1198
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001199 self.assertRaises(OSError, posix.sched_setscheduler, -1, mine, param)
1200 self.assertRaises(TypeError, posix.sched_setscheduler, 0, mine, None)
1201 self.assertRaises(TypeError, posix.sched_setparam, 0, 43)
1202 param = posix.sched_param(None)
1203 self.assertRaises(TypeError, posix.sched_setparam, 0, param)
1204 large = 214748364700
1205 param = posix.sched_param(large)
1206 self.assertRaises(OverflowError, posix.sched_setparam, 0, param)
1207 param = posix.sched_param(sched_priority=-large)
1208 self.assertRaises(OverflowError, posix.sched_setparam, 0, param)
1209
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05001210 @unittest.skipUnless(hasattr(posix, "sched_rr_get_interval"), "no function")
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001211 def test_sched_rr_get_interval(self):
Benjamin Peterson43234ab2011-08-02 22:19:14 -05001212 try:
1213 interval = posix.sched_rr_get_interval(0)
1214 except OSError as e:
1215 # This likely means that sched_rr_get_interval is only valid for
1216 # processes with the SCHED_RR scheduler in effect.
1217 if e.errno != errno.EINVAL:
1218 raise
1219 self.skipTest("only works on SCHED_RR processes")
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001220 self.assertIsInstance(interval, float)
1221 # Reasonable constraints, I think.
1222 self.assertGreaterEqual(interval, 0.)
1223 self.assertLess(interval, 1.)
1224
Benjamin Peterson2740af82011-08-02 17:41:34 -05001225 @requires_sched_affinity
Antoine Pitrou84869872012-08-04 16:16:35 +02001226 def test_sched_getaffinity(self):
1227 mask = posix.sched_getaffinity(0)
1228 self.assertIsInstance(mask, set)
1229 self.assertGreaterEqual(len(mask), 1)
1230 self.assertRaises(OSError, posix.sched_getaffinity, -1)
1231 for cpu in mask:
1232 self.assertIsInstance(cpu, int)
1233 self.assertGreaterEqual(cpu, 0)
1234 self.assertLess(cpu, 1 << 32)
1235
1236 @requires_sched_affinity
1237 def test_sched_setaffinity(self):
1238 mask = posix.sched_getaffinity(0)
1239 if len(mask) > 1:
1240 # Empty masks are forbidden
1241 mask.pop()
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001242 posix.sched_setaffinity(0, mask)
Antoine Pitrou84869872012-08-04 16:16:35 +02001243 self.assertEqual(posix.sched_getaffinity(0), mask)
1244 self.assertRaises(OSError, posix.sched_setaffinity, 0, [])
1245 self.assertRaises(ValueError, posix.sched_setaffinity, 0, [-10])
1246 self.assertRaises(OverflowError, posix.sched_setaffinity, 0, [1<<128])
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001247 self.assertRaises(OSError, posix.sched_setaffinity, -1, mask)
1248
Victor Stinner8b905bd2011-10-25 13:34:04 +02001249 def test_rtld_constants(self):
1250 # check presence of major RTLD_* constants
1251 posix.RTLD_LAZY
1252 posix.RTLD_NOW
1253 posix.RTLD_GLOBAL
1254 posix.RTLD_LOCAL
1255
Jesus Cea60c13dd2012-06-23 02:58:14 +02001256 @unittest.skipUnless(hasattr(os, 'SEEK_HOLE'),
1257 "test needs an OS that reports file holes")
Hynek Schlawackf841e422012-06-24 09:51:46 +02001258 def test_fs_holes(self):
Jesus Cea94363612012-06-22 18:32:07 +02001259 # Even if the filesystem doesn't report holes,
1260 # if the OS supports it the SEEK_* constants
1261 # will be defined and will have a consistent
1262 # behaviour:
1263 # os.SEEK_DATA = current position
1264 # os.SEEK_HOLE = end of file position
Hynek Schlawackf841e422012-06-24 09:51:46 +02001265 with open(support.TESTFN, 'r+b') as fp:
Jesus Cea94363612012-06-22 18:32:07 +02001266 fp.write(b"hello")
1267 fp.flush()
1268 size = fp.tell()
1269 fno = fp.fileno()
Jesus Cead46f7d22012-07-07 14:56:04 +02001270 try :
1271 for i in range(size):
1272 self.assertEqual(i, os.lseek(fno, i, os.SEEK_DATA))
1273 self.assertLessEqual(size, os.lseek(fno, i, os.SEEK_HOLE))
1274 self.assertRaises(OSError, os.lseek, fno, size, os.SEEK_DATA)
1275 self.assertRaises(OSError, os.lseek, fno, size, os.SEEK_HOLE)
1276 except OSError :
1277 # Some OSs claim to support SEEK_HOLE/SEEK_DATA
1278 # but it is not true.
1279 # For instance:
1280 # http://lists.freebsd.org/pipermail/freebsd-amd64/2012-January/014332.html
1281 raise unittest.SkipTest("OSError raised!")
Jesus Cea94363612012-06-22 18:32:07 +02001282
Larry Hastingsb0827312014-02-09 22:05:19 -08001283 def test_path_error2(self):
1284 """
1285 Test functions that call path_error2(), providing two filenames in their exceptions.
1286 """
Victor Stinner047b7ae2014-10-05 17:37:41 +02001287 for name in ("rename", "replace", "link"):
Larry Hastingsb0827312014-02-09 22:05:19 -08001288 function = getattr(os, name, None)
Victor Stinnerbed04a72014-10-05 17:37:59 +02001289 if function is None:
1290 continue
Larry Hastingsb0827312014-02-09 22:05:19 -08001291
Victor Stinnerbed04a72014-10-05 17:37:59 +02001292 for dst in ("noodly2", support.TESTFN):
1293 try:
1294 function('doesnotexistfilename', dst)
1295 except OSError as e:
1296 self.assertIn("'doesnotexistfilename' -> '{}'".format(dst), str(e))
1297 break
1298 else:
1299 self.fail("No valid path_error2() test for os." + name)
Larry Hastingsb0827312014-02-09 22:05:19 -08001300
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001301 def test_path_with_null_character(self):
1302 fn = support.TESTFN
1303 fn_with_NUL = fn + '\0'
1304 self.addCleanup(support.unlink, fn)
1305 support.unlink(fn)
1306 fd = None
1307 try:
Serhiy Storchaka7e9d1d12015-04-20 10:12:28 +03001308 with self.assertRaises(ValueError):
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001309 fd = os.open(fn_with_NUL, os.O_WRONLY | os.O_CREAT) # raises
1310 finally:
1311 if fd is not None:
1312 os.close(fd)
1313 self.assertFalse(os.path.exists(fn))
Serhiy Storchaka7e9d1d12015-04-20 10:12:28 +03001314 self.assertRaises(ValueError, os.mkdir, fn_with_NUL)
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001315 self.assertFalse(os.path.exists(fn))
1316 open(fn, 'wb').close()
Serhiy Storchaka7e9d1d12015-04-20 10:12:28 +03001317 self.assertRaises(ValueError, os.stat, fn_with_NUL)
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001318
1319 def test_path_with_null_byte(self):
1320 fn = os.fsencode(support.TESTFN)
1321 fn_with_NUL = fn + b'\0'
1322 self.addCleanup(support.unlink, fn)
1323 support.unlink(fn)
1324 fd = None
1325 try:
1326 with self.assertRaises(ValueError):
1327 fd = os.open(fn_with_NUL, os.O_WRONLY | os.O_CREAT) # raises
1328 finally:
1329 if fd is not None:
1330 os.close(fd)
1331 self.assertFalse(os.path.exists(fn))
1332 self.assertRaises(ValueError, os.mkdir, fn_with_NUL)
1333 self.assertFalse(os.path.exists(fn))
1334 open(fn, 'wb').close()
1335 self.assertRaises(ValueError, os.stat, fn_with_NUL)
1336
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00001337class PosixGroupsTester(unittest.TestCase):
1338
1339 def setUp(self):
1340 if posix.getuid() != 0:
1341 raise unittest.SkipTest("not enough privileges")
1342 if not hasattr(posix, 'getgroups'):
1343 raise unittest.SkipTest("need posix.getgroups")
1344 if sys.platform == 'darwin':
1345 raise unittest.SkipTest("getgroups(2) is broken on OSX")
1346 self.saved_groups = posix.getgroups()
1347
1348 def tearDown(self):
1349 if hasattr(posix, 'setgroups'):
1350 posix.setgroups(self.saved_groups)
1351 elif hasattr(posix, 'initgroups'):
1352 name = pwd.getpwuid(posix.getuid()).pw_name
1353 posix.initgroups(name, self.saved_groups[0])
1354
1355 @unittest.skipUnless(hasattr(posix, 'initgroups'),
1356 "test needs posix.initgroups()")
1357 def test_initgroups(self):
1358 # find missing group
1359
Benjamin Peterson659a6f52014-03-01 19:14:12 -05001360 g = max(self.saved_groups or [0]) + 1
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00001361 name = pwd.getpwuid(posix.getuid()).pw_name
1362 posix.initgroups(name, g)
1363 self.assertIn(g, posix.getgroups())
1364
1365 @unittest.skipUnless(hasattr(posix, 'setgroups'),
1366 "test needs posix.setgroups()")
1367 def test_setgroups(self):
Antoine Pitroue5a91012010-09-04 17:32:06 +00001368 for groups in [[0], list(range(16))]:
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00001369 posix.setgroups(groups)
1370 self.assertListEqual(groups, posix.getgroups())
1371
Neal Norwitze241ce82003-02-17 18:17:05 +00001372def test_main():
Antoine Pitrou68c95922011-03-20 17:33:57 +01001373 try:
1374 support.run_unittest(PosixTester, PosixGroupsTester)
1375 finally:
1376 support.reap_children()
Neal Norwitze241ce82003-02-17 18:17:05 +00001377
1378if __name__ == '__main__':
1379 test_main()