blob: 1eceebe7ac95dc7b091a30d4fa96e79633ed4fc2 [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
R. David Murrayeb3615d2009-04-22 02:24:39 +00004
5# Skip these tests if there is no posix module.
6posix = support.import_module('posix')
Neal Norwitze241ce82003-02-17 18:17:05 +00007
Antoine Pitroub7572f02009-12-02 20:46:48 +00008import errno
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00009import sys
Neal Norwitze241ce82003-02-17 18:17:05 +000010import time
11import os
Charles-François Natali1e045b12011-05-22 20:42:32 +020012import fcntl
Charles-François Nataliab2d58e2012-04-17 19:48:35 +020013import platform
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000014import pwd
Benjamin Petersondcf97b92008-07-02 17:30:14 +000015import shutil
Benjamin Peterson052a02b2010-08-17 01:27:09 +000016import stat
Ned Deilyba2eab22011-07-26 13:53:55 -070017import tempfile
Neal Norwitze241ce82003-02-17 18:17:05 +000018import unittest
19import warnings
Serhiy Storchaka78980432013-01-15 01:12:17 +020020import _testcapi
R. David Murraya21e4ca2009-03-31 23:16:50 +000021
Ned Deilyba2eab22011-07-26 13:53:55 -070022_DUMMY_SYMLINK = os.path.join(tempfile.gettempdir(),
23 support.TESTFN + '-dummy-symlink')
Neal Norwitze241ce82003-02-17 18:17:05 +000024
25class PosixTester(unittest.TestCase):
26
27 def setUp(self):
28 # create empty file
Benjamin Petersonee8712c2008-05-20 21:35:26 +000029 fp = open(support.TESTFN, 'w+')
Neal Norwitze241ce82003-02-17 18:17:05 +000030 fp.close()
Ned Deily3eb67d52011-06-28 00:00:28 -070031 self.teardown_files = [ support.TESTFN ]
Brett Cannonc8d502e2010-03-20 21:53:28 +000032 self._warnings_manager = support.check_warnings()
33 self._warnings_manager.__enter__()
34 warnings.filterwarnings('ignore', '.* potential security risk .*',
35 RuntimeWarning)
Neal Norwitze241ce82003-02-17 18:17:05 +000036
37 def tearDown(self):
Ned Deily3eb67d52011-06-28 00:00:28 -070038 for teardown_file in self.teardown_files:
39 support.unlink(teardown_file)
Brett Cannonc8d502e2010-03-20 21:53:28 +000040 self._warnings_manager.__exit__(None, None, None)
Neal Norwitze241ce82003-02-17 18:17:05 +000041
42 def testNoArgFunctions(self):
43 # test posix functions which take no arguments and have
44 # no side-effects which we need to cleanup (e.g., fork, wait, abort)
Guido van Rossumf0af3e32008-10-02 18:55:37 +000045 NO_ARG_FUNCTIONS = [ "ctermid", "getcwd", "getcwdb", "uname",
Guido van Rossum687b9c02007-10-25 23:18:51 +000046 "times", "getloadavg",
Neal Norwitze241ce82003-02-17 18:17:05 +000047 "getegid", "geteuid", "getgid", "getgroups",
Ross Lagerwall7807c352011-03-17 20:20:30 +020048 "getpid", "getpgrp", "getppid", "getuid", "sync",
Neal Norwitze241ce82003-02-17 18:17:05 +000049 ]
Neal Norwitz71b13e82003-02-23 22:12:24 +000050
Neal Norwitze241ce82003-02-17 18:17:05 +000051 for name in NO_ARG_FUNCTIONS:
52 posix_func = getattr(posix, name, None)
53 if posix_func is not None:
54 posix_func()
Neal Norwitz2ff51a82003-02-17 22:40:31 +000055 self.assertRaises(TypeError, posix_func, 1)
Neal Norwitze241ce82003-02-17 18:17:05 +000056
Serhiy Storchaka43767632013-11-03 21:31:38 +020057 @unittest.skipUnless(hasattr(posix, 'getresuid'),
58 'test needs posix.getresuid()')
59 def test_getresuid(self):
60 user_ids = posix.getresuid()
61 self.assertEqual(len(user_ids), 3)
62 for val in user_ids:
63 self.assertGreaterEqual(val, 0)
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000064
Serhiy Storchaka43767632013-11-03 21:31:38 +020065 @unittest.skipUnless(hasattr(posix, 'getresgid'),
66 'test needs posix.getresgid()')
67 def test_getresgid(self):
68 group_ids = posix.getresgid()
69 self.assertEqual(len(group_ids), 3)
70 for val in group_ids:
71 self.assertGreaterEqual(val, 0)
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000072
Serhiy Storchaka43767632013-11-03 21:31:38 +020073 @unittest.skipUnless(hasattr(posix, 'setresuid'),
74 'test needs posix.setresuid()')
75 def test_setresuid(self):
76 current_user_ids = posix.getresuid()
77 self.assertIsNone(posix.setresuid(*current_user_ids))
78 # -1 means don't change that value.
79 self.assertIsNone(posix.setresuid(-1, -1, -1))
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000080
Serhiy Storchaka43767632013-11-03 21:31:38 +020081 @unittest.skipUnless(hasattr(posix, 'setresuid'),
82 'test needs posix.setresuid()')
83 def test_setresuid_exception(self):
84 # Don't do this test if someone is silly enough to run us as root.
85 current_user_ids = posix.getresuid()
86 if 0 not in current_user_ids:
87 new_user_ids = (current_user_ids[0]+1, -1, -1)
88 self.assertRaises(OSError, posix.setresuid, *new_user_ids)
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000089
Serhiy Storchaka43767632013-11-03 21:31:38 +020090 @unittest.skipUnless(hasattr(posix, 'setresgid'),
91 'test needs posix.setresgid()')
92 def test_setresgid(self):
93 current_group_ids = posix.getresgid()
94 self.assertIsNone(posix.setresgid(*current_group_ids))
95 # -1 means don't change that value.
96 self.assertIsNone(posix.setresgid(-1, -1, -1))
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000097
Serhiy Storchaka43767632013-11-03 21:31:38 +020098 @unittest.skipUnless(hasattr(posix, 'setresgid'),
99 'test needs posix.setresgid()')
100 def test_setresgid_exception(self):
101 # Don't do this test if someone is silly enough to run us as root.
102 current_group_ids = posix.getresgid()
103 if 0 not in current_group_ids:
104 new_group_ids = (current_group_ids[0]+1, -1, -1)
105 self.assertRaises(OSError, posix.setresgid, *new_group_ids)
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000106
Antoine Pitroub7572f02009-12-02 20:46:48 +0000107 @unittest.skipUnless(hasattr(posix, 'initgroups'),
108 "test needs os.initgroups()")
109 def test_initgroups(self):
110 # It takes a string and an integer; check that it raises a TypeError
111 # for other argument lists.
112 self.assertRaises(TypeError, posix.initgroups)
113 self.assertRaises(TypeError, posix.initgroups, None)
114 self.assertRaises(TypeError, posix.initgroups, 3, "foo")
115 self.assertRaises(TypeError, posix.initgroups, "foo", 3, object())
116
117 # If a non-privileged user invokes it, it should fail with OSError
118 # EPERM.
119 if os.getuid() != 0:
Charles-François Natalie8a255a2012-05-02 20:01:38 +0200120 try:
121 name = pwd.getpwuid(posix.getuid()).pw_name
122 except KeyError:
123 # the current UID may not have a pwd entry
124 raise unittest.SkipTest("need a pwd entry")
Antoine Pitroub7572f02009-12-02 20:46:48 +0000125 try:
126 posix.initgroups(name, 13)
127 except OSError as e:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000128 self.assertEqual(e.errno, errno.EPERM)
Antoine Pitroub7572f02009-12-02 20:46:48 +0000129 else:
130 self.fail("Expected OSError to be raised by initgroups")
131
Serhiy Storchaka43767632013-11-03 21:31:38 +0200132 @unittest.skipUnless(hasattr(posix, 'statvfs'),
133 'test needs posix.statvfs()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000134 def test_statvfs(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200135 self.assertTrue(posix.statvfs(os.curdir))
Neal Norwitze241ce82003-02-17 18:17:05 +0000136
Serhiy Storchaka43767632013-11-03 21:31:38 +0200137 @unittest.skipUnless(hasattr(posix, 'fstatvfs'),
138 'test needs posix.fstatvfs()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000139 def test_fstatvfs(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200140 fp = open(support.TESTFN)
141 try:
142 self.assertTrue(posix.fstatvfs(fp.fileno()))
143 self.assertTrue(posix.statvfs(fp.fileno()))
144 finally:
145 fp.close()
Neal Norwitze241ce82003-02-17 18:17:05 +0000146
Serhiy Storchaka43767632013-11-03 21:31:38 +0200147 @unittest.skipUnless(hasattr(posix, 'ftruncate'),
148 'test needs posix.ftruncate()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000149 def test_ftruncate(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200150 fp = open(support.TESTFN, 'w+')
151 try:
152 # we need to have some data to truncate
153 fp.write('test')
154 fp.flush()
155 posix.ftruncate(fp.fileno(), 0)
156 finally:
157 fp.close()
Neal Norwitze241ce82003-02-17 18:17:05 +0000158
Ross Lagerwall7807c352011-03-17 20:20:30 +0200159 @unittest.skipUnless(hasattr(posix, 'truncate'), "test needs posix.truncate()")
160 def test_truncate(self):
161 with open(support.TESTFN, 'w') as fp:
162 fp.write('test')
163 fp.flush()
164 posix.truncate(support.TESTFN, 0)
165
Larry Hastings9cf065c2012-06-22 16:30:09 -0700166 @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 +0200167 @unittest.skipUnless(hasattr(os, 'fork'), "test needs os.fork()")
Ross Lagerwalldedf6cf2011-03-20 18:27:05 +0200168 @unittest.skipUnless(hasattr(os, 'waitpid'), "test needs os.waitpid()")
Ross Lagerwall7807c352011-03-17 20:20:30 +0200169 def test_fexecve(self):
170 fp = os.open(sys.executable, os.O_RDONLY)
171 try:
172 pid = os.fork()
173 if pid == 0:
174 os.chdir(os.path.split(sys.executable)[0])
Larry Hastings9cf065c2012-06-22 16:30:09 -0700175 posix.execve(fp, [sys.executable, '-c', 'pass'], os.environ)
Ross Lagerwall7807c352011-03-17 20:20:30 +0200176 else:
Ross Lagerwalldedf6cf2011-03-20 18:27:05 +0200177 self.assertEqual(os.waitpid(pid, 0), (pid, 0))
Ross Lagerwall7807c352011-03-17 20:20:30 +0200178 finally:
179 os.close(fp)
180
181 @unittest.skipUnless(hasattr(posix, 'waitid'), "test needs posix.waitid()")
182 @unittest.skipUnless(hasattr(os, 'fork'), "test needs os.fork()")
183 def test_waitid(self):
184 pid = os.fork()
185 if pid == 0:
186 os.chdir(os.path.split(sys.executable)[0])
187 posix.execve(sys.executable, [sys.executable, '-c', 'pass'], os.environ)
188 else:
189 res = posix.waitid(posix.P_PID, pid, posix.WEXITED)
190 self.assertEqual(pid, res.si_pid)
191
192 @unittest.skipUnless(hasattr(posix, 'lockf'), "test needs posix.lockf()")
193 def test_lockf(self):
194 fd = os.open(support.TESTFN, os.O_WRONLY | os.O_CREAT)
195 try:
196 os.write(fd, b'test')
197 os.lseek(fd, 0, os.SEEK_SET)
198 posix.lockf(fd, posix.F_LOCK, 4)
199 # section is locked
200 posix.lockf(fd, posix.F_ULOCK, 4)
201 finally:
202 os.close(fd)
203
204 @unittest.skipUnless(hasattr(posix, 'pread'), "test needs posix.pread()")
205 def test_pread(self):
206 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
207 try:
208 os.write(fd, b'test')
209 os.lseek(fd, 0, os.SEEK_SET)
210 self.assertEqual(b'es', posix.pread(fd, 2, 1))
Florent Xiclunae41f0de2011-11-11 19:39:25 +0100211 # the first pread() shouldn't disturb the file offset
Ross Lagerwall7807c352011-03-17 20:20:30 +0200212 self.assertEqual(b'te', posix.read(fd, 2))
213 finally:
214 os.close(fd)
215
216 @unittest.skipUnless(hasattr(posix, 'pwrite'), "test needs posix.pwrite()")
217 def test_pwrite(self):
218 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
219 try:
220 os.write(fd, b'test')
221 os.lseek(fd, 0, os.SEEK_SET)
222 posix.pwrite(fd, b'xx', 1)
223 self.assertEqual(b'txxt', posix.read(fd, 4))
224 finally:
225 os.close(fd)
226
227 @unittest.skipUnless(hasattr(posix, 'posix_fallocate'),
228 "test needs posix.posix_fallocate()")
229 def test_posix_fallocate(self):
230 fd = os.open(support.TESTFN, os.O_WRONLY | os.O_CREAT)
231 try:
232 posix.posix_fallocate(fd, 0, 10)
233 except OSError as inst:
234 # issue10812, ZFS doesn't appear to support posix_fallocate,
235 # so skip Solaris-based since they are likely to have ZFS.
236 if inst.errno != errno.EINVAL or not sys.platform.startswith("sunos"):
237 raise
238 finally:
239 os.close(fd)
240
241 @unittest.skipUnless(hasattr(posix, 'posix_fadvise'),
242 "test needs posix.posix_fadvise()")
243 def test_posix_fadvise(self):
244 fd = os.open(support.TESTFN, os.O_RDONLY)
245 try:
246 posix.posix_fadvise(fd, 0, 0, posix.POSIX_FADV_WILLNEED)
247 finally:
248 os.close(fd)
249
Larry Hastings9cf065c2012-06-22 16:30:09 -0700250 @unittest.skipUnless(os.utime in os.supports_fd, "test needs fd support in os.utime")
251 def test_utime_with_fd(self):
Ross Lagerwall7807c352011-03-17 20:20:30 +0200252 now = time.time()
253 fd = os.open(support.TESTFN, os.O_RDONLY)
254 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -0700255 posix.utime(fd)
256 posix.utime(fd, None)
257 self.assertRaises(TypeError, posix.utime, fd, (None, None))
258 self.assertRaises(TypeError, posix.utime, fd, (now, None))
259 self.assertRaises(TypeError, posix.utime, fd, (None, now))
260 posix.utime(fd, (int(now), int(now)))
261 posix.utime(fd, (now, now))
262 self.assertRaises(ValueError, posix.utime, fd, (now, now), ns=(now, now))
263 self.assertRaises(ValueError, posix.utime, fd, (now, 0), ns=(None, None))
264 self.assertRaises(ValueError, posix.utime, fd, (None, None), ns=(now, 0))
265 posix.utime(fd, (int(now), int((now - int(now)) * 1e9)))
266 posix.utime(fd, ns=(int(now), int((now - int(now)) * 1e9)))
267
Ross Lagerwall7807c352011-03-17 20:20:30 +0200268 finally:
269 os.close(fd)
270
Larry Hastings9cf065c2012-06-22 16:30:09 -0700271 @unittest.skipUnless(os.utime in os.supports_follow_symlinks, "test needs follow_symlinks support in os.utime")
272 def test_utime_nofollow_symlinks(self):
Ross Lagerwall7807c352011-03-17 20:20:30 +0200273 now = time.time()
Larry Hastings9cf065c2012-06-22 16:30:09 -0700274 posix.utime(support.TESTFN, None, follow_symlinks=False)
275 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, None), follow_symlinks=False)
276 self.assertRaises(TypeError, posix.utime, support.TESTFN, (now, None), follow_symlinks=False)
277 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, now), follow_symlinks=False)
278 posix.utime(support.TESTFN, (int(now), int(now)), follow_symlinks=False)
279 posix.utime(support.TESTFN, (now, now), follow_symlinks=False)
280 posix.utime(support.TESTFN, follow_symlinks=False)
Ross Lagerwall7807c352011-03-17 20:20:30 +0200281
282 @unittest.skipUnless(hasattr(posix, 'writev'), "test needs posix.writev()")
283 def test_writev(self):
284 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
285 try:
286 os.writev(fd, (b'test1', b'tt2', b't3'))
287 os.lseek(fd, 0, os.SEEK_SET)
288 self.assertEqual(b'test1tt2t3', posix.read(fd, 10))
289 finally:
290 os.close(fd)
291
292 @unittest.skipUnless(hasattr(posix, 'readv'), "test needs posix.readv()")
293 def test_readv(self):
294 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
295 try:
296 os.write(fd, b'test1tt2t3')
297 os.lseek(fd, 0, os.SEEK_SET)
298 buf = [bytearray(i) for i in [5, 3, 2]]
299 self.assertEqual(posix.readv(fd, buf), 10)
300 self.assertEqual([b'test1', b'tt2', b't3'], [bytes(i) for i in buf])
301 finally:
302 os.close(fd)
303
Serhiy Storchaka43767632013-11-03 21:31:38 +0200304 @unittest.skipUnless(hasattr(posix, 'dup'),
305 'test needs posix.dup()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000306 def test_dup(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200307 fp = open(support.TESTFN)
308 try:
309 fd = posix.dup(fp.fileno())
310 self.assertIsInstance(fd, int)
311 os.close(fd)
312 finally:
313 fp.close()
Neal Norwitze241ce82003-02-17 18:17:05 +0000314
Serhiy Storchaka43767632013-11-03 21:31:38 +0200315 @unittest.skipUnless(hasattr(posix, 'confstr'),
316 'test needs posix.confstr()')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000317 def test_confstr(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200318 self.assertRaises(ValueError, posix.confstr, "CS_garbage")
319 self.assertEqual(len(posix.confstr("CS_PATH")) > 0, True)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000320
Serhiy Storchaka43767632013-11-03 21:31:38 +0200321 @unittest.skipUnless(hasattr(posix, 'dup2'),
322 'test needs posix.dup2()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000323 def test_dup2(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200324 fp1 = open(support.TESTFN)
325 fp2 = open(support.TESTFN)
326 try:
327 posix.dup2(fp1.fileno(), fp2.fileno())
328 finally:
329 fp1.close()
330 fp2.close()
Neal Norwitze241ce82003-02-17 18:17:05 +0000331
Charles-François Natali1e045b12011-05-22 20:42:32 +0200332 @unittest.skipUnless(hasattr(os, 'O_CLOEXEC'), "needs os.O_CLOEXEC")
Charles-François Natali239bb962011-06-03 12:55:15 +0200333 @support.requires_linux_version(2, 6, 23)
Charles-François Natali1e045b12011-05-22 20:42:32 +0200334 def test_oscloexec(self):
335 fd = os.open(support.TESTFN, os.O_RDONLY|os.O_CLOEXEC)
336 self.addCleanup(os.close, fd)
Victor Stinnere36f3752011-05-24 00:29:43 +0200337 self.assertTrue(fcntl.fcntl(fd, fcntl.F_GETFD) & fcntl.FD_CLOEXEC)
Charles-François Natali1e045b12011-05-22 20:42:32 +0200338
Serhiy Storchaka43767632013-11-03 21:31:38 +0200339 @unittest.skipUnless(hasattr(posix, 'O_EXLOCK'),
340 'test needs posix.O_EXLOCK')
Skip Montanaro98470002005-06-17 01:14:49 +0000341 def test_osexlock(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200342 fd = os.open(support.TESTFN,
343 os.O_WRONLY|os.O_EXLOCK|os.O_CREAT)
344 self.assertRaises(OSError, os.open, support.TESTFN,
345 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
346 os.close(fd)
347
348 if hasattr(posix, "O_SHLOCK"):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000349 fd = os.open(support.TESTFN,
Serhiy Storchaka43767632013-11-03 21:31:38 +0200350 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000351 self.assertRaises(OSError, os.open, support.TESTFN,
Skip Montanaro98470002005-06-17 01:14:49 +0000352 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
353 os.close(fd)
354
Serhiy Storchaka43767632013-11-03 21:31:38 +0200355 @unittest.skipUnless(hasattr(posix, 'O_SHLOCK'),
356 'test needs posix.O_SHLOCK')
Skip Montanaro98470002005-06-17 01:14:49 +0000357 def test_osshlock(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200358 fd1 = os.open(support.TESTFN,
359 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
360 fd2 = os.open(support.TESTFN,
361 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
362 os.close(fd2)
363 os.close(fd1)
364
365 if hasattr(posix, "O_EXLOCK"):
366 fd = os.open(support.TESTFN,
Skip Montanaro98470002005-06-17 01:14:49 +0000367 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
Serhiy Storchaka43767632013-11-03 21:31:38 +0200368 self.assertRaises(OSError, os.open, support.TESTFN,
369 os.O_RDONLY|os.O_EXLOCK|os.O_NONBLOCK)
370 os.close(fd)
Skip Montanaro98470002005-06-17 01:14:49 +0000371
Serhiy Storchaka43767632013-11-03 21:31:38 +0200372 @unittest.skipUnless(hasattr(posix, 'fstat'),
373 'test needs posix.fstat()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000374 def test_fstat(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200375 fp = open(support.TESTFN)
376 try:
377 self.assertTrue(posix.fstat(fp.fileno()))
378 self.assertTrue(posix.stat(fp.fileno()))
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200379
Serhiy Storchaka43767632013-11-03 21:31:38 +0200380 self.assertRaisesRegex(TypeError,
381 'should be string, bytes or integer, not',
382 posix.stat, float(fp.fileno()))
383 finally:
384 fp.close()
Neal Norwitze241ce82003-02-17 18:17:05 +0000385
Serhiy Storchaka43767632013-11-03 21:31:38 +0200386 @unittest.skipUnless(hasattr(posix, 'stat'),
387 'test needs posix.stat()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000388 def test_stat(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200389 self.assertTrue(posix.stat(support.TESTFN))
390 self.assertTrue(posix.stat(os.fsencode(support.TESTFN)))
391 self.assertTrue(posix.stat(bytearray(os.fsencode(support.TESTFN))))
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200392
Serhiy Storchaka43767632013-11-03 21:31:38 +0200393 self.assertRaisesRegex(TypeError,
394 'can\'t specify None for path argument',
395 posix.stat, None)
396 self.assertRaisesRegex(TypeError,
397 'should be string, bytes or integer, not',
398 posix.stat, list(support.TESTFN))
399 self.assertRaisesRegex(TypeError,
400 'should be string, bytes or integer, not',
401 posix.stat, list(os.fsencode(support.TESTFN)))
Neal Norwitze241ce82003-02-17 18:17:05 +0000402
Benjamin Peterson052a02b2010-08-17 01:27:09 +0000403 @unittest.skipUnless(hasattr(posix, 'mkfifo'), "don't have mkfifo()")
404 def test_mkfifo(self):
405 support.unlink(support.TESTFN)
406 posix.mkfifo(support.TESTFN, stat.S_IRUSR | stat.S_IWUSR)
407 self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode))
408
409 @unittest.skipUnless(hasattr(posix, 'mknod') and hasattr(stat, 'S_IFIFO'),
410 "don't have mknod()/S_IFIFO")
411 def test_mknod(self):
412 # Test using mknod() to create a FIFO (the only use specified
413 # by POSIX).
414 support.unlink(support.TESTFN)
415 mode = stat.S_IFIFO | stat.S_IRUSR | stat.S_IWUSR
416 try:
417 posix.mknod(support.TESTFN, mode, 0)
418 except OSError as e:
419 # Some old systems don't allow unprivileged users to use
420 # mknod(), or only support creating device nodes.
421 self.assertIn(e.errno, (errno.EPERM, errno.EINVAL))
422 else:
423 self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode))
424
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200425 def _test_all_chown_common(self, chown_func, first_param, stat_func):
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000426 """Common code for chown, fchown and lchown tests."""
Serhiy Storchaka54db2fd2013-02-20 19:40:25 +0200427 def check_stat(uid, gid):
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200428 if stat_func is not None:
429 stat = stat_func(first_param)
Serhiy Storchaka54db2fd2013-02-20 19:40:25 +0200430 self.assertEqual(stat.st_uid, uid)
431 self.assertEqual(stat.st_gid, gid)
432 uid = os.getuid()
433 gid = os.getgid()
Charles-François Nataliab2d58e2012-04-17 19:48:35 +0200434 # test a successful chown call
Serhiy Storchaka54db2fd2013-02-20 19:40:25 +0200435 chown_func(first_param, uid, gid)
436 check_stat(uid, gid)
437 chown_func(first_param, -1, gid)
438 check_stat(uid, gid)
439 chown_func(first_param, uid, -1)
440 check_stat(uid, gid)
Charles-François Nataliab2d58e2012-04-17 19:48:35 +0200441
Serhiy Storchaka54db2fd2013-02-20 19:40:25 +0200442 if uid == 0:
443 # Try an amusingly large uid/gid to make sure we handle
444 # large unsigned values. (chown lets you use any
445 # uid/gid you like, even if they aren't defined.)
446 #
447 # This problem keeps coming up:
448 # http://bugs.python.org/issue1747858
449 # http://bugs.python.org/issue4591
450 # http://bugs.python.org/issue15301
451 # Hopefully the fix in 4591 fixes it for good!
452 #
453 # This part of the test only runs when run as root.
454 # Only scary people run their tests as root.
455
456 big_value = 2**31
457 chown_func(first_param, big_value, big_value)
458 check_stat(big_value, big_value)
459 chown_func(first_param, -1, -1)
460 check_stat(big_value, big_value)
461 chown_func(first_param, uid, gid)
462 check_stat(uid, gid)
Charles-François Nataliab2d58e2012-04-17 19:48:35 +0200463 elif platform.system() in ('HP-UX', 'SunOS'):
464 # HP-UX and Solaris can allow a non-root user to chown() to root
465 # (issue #5113)
466 raise unittest.SkipTest("Skipping because of non-standard chown() "
467 "behavior")
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000468 else:
469 # non-root cannot chown to root, raises OSError
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200470 self.assertRaises(OSError, chown_func, first_param, 0, 0)
Serhiy Storchaka54db2fd2013-02-20 19:40:25 +0200471 check_stat(uid, gid)
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200472 self.assertRaises(OSError, chown_func, first_param, 0, -1)
Serhiy Storchaka54db2fd2013-02-20 19:40:25 +0200473 check_stat(uid, gid)
Serhiy Storchakaa2964b32013-02-21 14:34:36 +0200474 if 0 not in os.getgroups():
Serhiy Storchakab3d62ce2013-02-20 19:48:22 +0200475 self.assertRaises(OSError, chown_func, first_param, -1, 0)
476 check_stat(uid, gid)
Serhiy Storchaka54db2fd2013-02-20 19:40:25 +0200477 # test illegal types
478 for t in str, float:
479 self.assertRaises(TypeError, chown_func, first_param, t(uid), gid)
480 check_stat(uid, gid)
481 self.assertRaises(TypeError, chown_func, first_param, uid, t(gid))
482 check_stat(uid, gid)
Christian Heimesd5e2b6f2008-03-19 21:50:51 +0000483
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000484 @unittest.skipUnless(hasattr(posix, 'chown'), "test needs os.chown()")
485 def test_chown(self):
486 # raise an OSError if the file does not exist
487 os.unlink(support.TESTFN)
488 self.assertRaises(OSError, posix.chown, support.TESTFN, -1, -1)
Christian Heimesd5e2b6f2008-03-19 21:50:51 +0000489
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000490 # re-create the file
Victor Stinnerbf816222011-06-30 23:25:47 +0200491 support.create_empty_file(support.TESTFN)
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200492 self._test_all_chown_common(posix.chown, support.TESTFN,
493 getattr(posix, 'stat', None))
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000494
495 @unittest.skipUnless(hasattr(posix, 'fchown'), "test needs os.fchown()")
496 def test_fchown(self):
497 os.unlink(support.TESTFN)
498
499 # re-create the file
500 test_file = open(support.TESTFN, 'w')
501 try:
502 fd = test_file.fileno()
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200503 self._test_all_chown_common(posix.fchown, fd,
504 getattr(posix, 'fstat', None))
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000505 finally:
506 test_file.close()
507
508 @unittest.skipUnless(hasattr(posix, 'lchown'), "test needs os.lchown()")
509 def test_lchown(self):
510 os.unlink(support.TESTFN)
511 # create a symlink
Ned Deily3eb67d52011-06-28 00:00:28 -0700512 os.symlink(_DUMMY_SYMLINK, support.TESTFN)
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200513 self._test_all_chown_common(posix.lchown, support.TESTFN,
514 getattr(posix, 'lstat', None))
Christian Heimesd5e2b6f2008-03-19 21:50:51 +0000515
Serhiy Storchaka43767632013-11-03 21:31:38 +0200516 @unittest.skipUnless(hasattr(posix, 'chdir'), 'test needs posix.chdir()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000517 def test_chdir(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200518 posix.chdir(os.curdir)
519 self.assertRaises(OSError, posix.chdir, support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000520
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +0000521 def test_listdir(self):
Larry Hastingsfdaea062012-06-25 04:42:23 -0700522 self.assertTrue(support.TESTFN in posix.listdir(os.curdir))
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +0000523
524 def test_listdir_default(self):
Larry Hastingsfdaea062012-06-25 04:42:23 -0700525 # When listdir is called without argument,
526 # it's the same as listdir(os.curdir).
527 self.assertTrue(support.TESTFN in posix.listdir())
Neal Norwitze241ce82003-02-17 18:17:05 +0000528
Larry Hastingsfdaea062012-06-25 04:42:23 -0700529 def test_listdir_bytes(self):
530 # When listdir is called with a bytes object,
531 # the returned strings are of type bytes.
532 self.assertTrue(os.fsencode(support.TESTFN) in posix.listdir(b'.'))
533
534 @unittest.skipUnless(posix.listdir in os.supports_fd,
535 "test needs fd support for posix.listdir()")
536 def test_listdir_fd(self):
Antoine Pitrou8250e232011-02-25 23:41:16 +0000537 f = posix.open(posix.getcwd(), posix.O_RDONLY)
Charles-François Natali7546ad32012-01-08 18:34:06 +0100538 self.addCleanup(posix.close, f)
Antoine Pitrou8250e232011-02-25 23:41:16 +0000539 self.assertEqual(
540 sorted(posix.listdir('.')),
Larry Hastings9cf065c2012-06-22 16:30:09 -0700541 sorted(posix.listdir(f))
Antoine Pitrou8250e232011-02-25 23:41:16 +0000542 )
Charles-François Natali7546ad32012-01-08 18:34:06 +0100543 # Check that the fd offset was reset (issue #13739)
Charles-François Natali7546ad32012-01-08 18:34:06 +0100544 self.assertEqual(
545 sorted(posix.listdir('.')),
Larry Hastings9cf065c2012-06-22 16:30:09 -0700546 sorted(posix.listdir(f))
Charles-François Natali7546ad32012-01-08 18:34:06 +0100547 )
Antoine Pitrou8250e232011-02-25 23:41:16 +0000548
Serhiy Storchaka43767632013-11-03 21:31:38 +0200549 @unittest.skipUnless(hasattr(posix, 'access'), 'test needs posix.access()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000550 def test_access(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200551 self.assertTrue(posix.access(support.TESTFN, os.R_OK))
Neal Norwitze241ce82003-02-17 18:17:05 +0000552
Serhiy Storchaka43767632013-11-03 21:31:38 +0200553 @unittest.skipUnless(hasattr(posix, 'umask'), 'test needs posix.umask()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000554 def test_umask(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200555 old_mask = posix.umask(0)
556 self.assertIsInstance(old_mask, int)
557 posix.umask(old_mask)
Neal Norwitze241ce82003-02-17 18:17:05 +0000558
Serhiy Storchaka43767632013-11-03 21:31:38 +0200559 @unittest.skipUnless(hasattr(posix, 'strerror'),
560 'test needs posix.strerror()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000561 def test_strerror(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200562 self.assertTrue(posix.strerror(0))
Neal Norwitze241ce82003-02-17 18:17:05 +0000563
Serhiy Storchaka43767632013-11-03 21:31:38 +0200564 @unittest.skipUnless(hasattr(posix, 'pipe'), 'test needs posix.pipe()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000565 def test_pipe(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200566 reader, writer = posix.pipe()
567 os.close(reader)
568 os.close(writer)
Neal Norwitze241ce82003-02-17 18:17:05 +0000569
Charles-François Natalidaafdd52011-05-29 20:07:40 +0200570 @unittest.skipUnless(hasattr(os, 'pipe2'), "test needs os.pipe2()")
Charles-François Natali239bb962011-06-03 12:55:15 +0200571 @support.requires_linux_version(2, 6, 27)
Charles-François Natalidaafdd52011-05-29 20:07:40 +0200572 def test_pipe2(self):
573 self.assertRaises(TypeError, os.pipe2, 'DEADBEEF')
574 self.assertRaises(TypeError, os.pipe2, 0, 0)
575
Charles-François Natali368f34b2011-06-06 19:49:47 +0200576 # try calling with flags = 0, like os.pipe()
577 r, w = os.pipe2(0)
Charles-François Natalidaafdd52011-05-29 20:07:40 +0200578 os.close(r)
579 os.close(w)
580
581 # test flags
582 r, w = os.pipe2(os.O_CLOEXEC|os.O_NONBLOCK)
583 self.addCleanup(os.close, r)
584 self.addCleanup(os.close, w)
Victor Stinnerbff989e2013-08-28 12:25:40 +0200585 self.assertFalse(os.get_inheritable(r))
586 self.assertFalse(os.get_inheritable(w))
587 self.assertTrue(fcntl.fcntl(r, fcntl.F_GETFL) & os.O_NONBLOCK)
588 self.assertTrue(fcntl.fcntl(w, fcntl.F_GETFL) & os.O_NONBLOCK)
Charles-François Natalidaafdd52011-05-29 20:07:40 +0200589 # try reading from an empty pipe: this should fail, not block
590 self.assertRaises(OSError, os.read, r, 1)
591 # try a write big enough to fill-up the pipe: this should either
592 # fail or perform a partial write, not block
593 try:
594 os.write(w, b'x' * support.PIPE_MAX_SIZE)
595 except OSError:
596 pass
597
Serhiy Storchaka78980432013-01-15 01:12:17 +0200598 # Issue 15989
599 self.assertRaises(OverflowError, os.pipe2, _testcapi.INT_MAX + 1)
600 self.assertRaises(OverflowError, os.pipe2, _testcapi.UINT_MAX + 1)
601
Serhiy Storchaka43767632013-11-03 21:31:38 +0200602 @unittest.skipUnless(hasattr(posix, 'utime'), 'test needs posix.utime()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000603 def test_utime(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200604 now = time.time()
605 posix.utime(support.TESTFN, None)
606 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, None))
607 self.assertRaises(TypeError, posix.utime, support.TESTFN, (now, None))
608 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, now))
609 posix.utime(support.TESTFN, (int(now), int(now)))
610 posix.utime(support.TESTFN, (now, now))
Neal Norwitze241ce82003-02-17 18:17:05 +0000611
Larry Hastings9cf065c2012-06-22 16:30:09 -0700612 def _test_chflags_regular_file(self, chflags_func, target_file, **kwargs):
Ned Deily3eb67d52011-06-28 00:00:28 -0700613 st = os.stat(target_file)
614 self.assertTrue(hasattr(st, 'st_flags'))
Trent Nelson75959cf2012-08-21 23:59:31 +0000615
616 # ZFS returns EOPNOTSUPP when attempting to set flag UF_IMMUTABLE.
617 flags = st.st_flags | stat.UF_IMMUTABLE
618 try:
619 chflags_func(target_file, flags, **kwargs)
620 except OSError as err:
621 if err.errno != errno.EOPNOTSUPP:
622 raise
623 msg = 'chflag UF_IMMUTABLE not supported by underlying fs'
624 self.skipTest(msg)
625
Ned Deily3eb67d52011-06-28 00:00:28 -0700626 try:
627 new_st = os.stat(target_file)
628 self.assertEqual(st.st_flags | stat.UF_IMMUTABLE, new_st.st_flags)
629 try:
630 fd = open(target_file, 'w+')
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200631 except OSError as e:
Ned Deily3eb67d52011-06-28 00:00:28 -0700632 self.assertEqual(e.errno, errno.EPERM)
633 finally:
634 posix.chflags(target_file, st.st_flags)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000635
Ned Deily3eb67d52011-06-28 00:00:28 -0700636 @unittest.skipUnless(hasattr(posix, 'chflags'), 'test needs os.chflags()')
637 def test_chflags(self):
638 self._test_chflags_regular_file(posix.chflags, support.TESTFN)
639
640 @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()')
641 def test_lchflags_regular_file(self):
642 self._test_chflags_regular_file(posix.lchflags, support.TESTFN)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700643 self._test_chflags_regular_file(posix.chflags, support.TESTFN, follow_symlinks=False)
Ned Deily3eb67d52011-06-28 00:00:28 -0700644
645 @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()')
646 def test_lchflags_symlink(self):
647 testfn_st = os.stat(support.TESTFN)
648
649 self.assertTrue(hasattr(testfn_st, 'st_flags'))
650
651 os.symlink(support.TESTFN, _DUMMY_SYMLINK)
652 self.teardown_files.append(_DUMMY_SYMLINK)
653 dummy_symlink_st = os.lstat(_DUMMY_SYMLINK)
654
Larry Hastings9cf065c2012-06-22 16:30:09 -0700655 def chflags_nofollow(path, flags):
656 return posix.chflags(path, flags, follow_symlinks=False)
Ned Deily3eb67d52011-06-28 00:00:28 -0700657
Larry Hastings9cf065c2012-06-22 16:30:09 -0700658 for fn in (posix.lchflags, chflags_nofollow):
Trent Nelson75959cf2012-08-21 23:59:31 +0000659 # ZFS returns EOPNOTSUPP when attempting to set flag UF_IMMUTABLE.
660 flags = dummy_symlink_st.st_flags | stat.UF_IMMUTABLE
661 try:
662 fn(_DUMMY_SYMLINK, flags)
663 except OSError as err:
664 if err.errno != errno.EOPNOTSUPP:
665 raise
666 msg = 'chflag UF_IMMUTABLE not supported by underlying fs'
667 self.skipTest(msg)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700668 try:
669 new_testfn_st = os.stat(support.TESTFN)
670 new_dummy_symlink_st = os.lstat(_DUMMY_SYMLINK)
671
672 self.assertEqual(testfn_st.st_flags, new_testfn_st.st_flags)
673 self.assertEqual(dummy_symlink_st.st_flags | stat.UF_IMMUTABLE,
674 new_dummy_symlink_st.st_flags)
675 finally:
676 fn(_DUMMY_SYMLINK, dummy_symlink_st.st_flags)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000677
Guido van Rossum98297ee2007-11-06 21:34:58 +0000678 def test_environ(self):
Victor Stinner17b490d2010-05-06 22:19:30 +0000679 if os.name == "nt":
680 item_type = str
681 else:
682 item_type = bytes
Guido van Rossum98297ee2007-11-06 21:34:58 +0000683 for k, v in posix.environ.items():
Victor Stinner17b490d2010-05-06 22:19:30 +0000684 self.assertEqual(type(k), item_type)
685 self.assertEqual(type(v), item_type)
Guido van Rossum98297ee2007-11-06 21:34:58 +0000686
Serhiy Storchaka43767632013-11-03 21:31:38 +0200687 @unittest.skipUnless(hasattr(posix, 'getcwd'), 'test needs posix.getcwd()')
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000688 def test_getcwd_long_pathnames(self):
Benjamin Peterson3a7dffa2013-08-23 21:01:48 -0500689 dirname = 'getcwd-test-directory-0123456789abcdef-01234567890abcdef'
690 curdir = os.getcwd()
691 base_path = os.path.abspath(support.TESTFN) + '.getcwd'
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000692
Benjamin Peterson3a7dffa2013-08-23 21:01:48 -0500693 try:
694 os.mkdir(base_path)
695 os.chdir(base_path)
696 except:
697 # Just returning nothing instead of the SkipTest exception, because
698 # the test results in Error in that case. Is that ok?
699 # raise unittest.SkipTest("cannot create directory for testing")
700 return
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000701
Benjamin Peterson3a7dffa2013-08-23 21:01:48 -0500702 def _create_and_do_getcwd(dirname, current_path_length = 0):
703 try:
704 os.mkdir(dirname)
705 except:
706 raise unittest.SkipTest("mkdir cannot create directory sufficiently deep for getcwd test")
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000707
Benjamin Peterson3a7dffa2013-08-23 21:01:48 -0500708 os.chdir(dirname)
709 try:
710 os.getcwd()
711 if current_path_length < 1027:
712 _create_and_do_getcwd(dirname, current_path_length + len(dirname) + 1)
713 finally:
714 os.chdir('..')
715 os.rmdir(dirname)
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000716
Benjamin Peterson3a7dffa2013-08-23 21:01:48 -0500717 _create_and_do_getcwd(dirname)
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000718
Benjamin Peterson3a7dffa2013-08-23 21:01:48 -0500719 finally:
720 os.chdir(curdir)
721 support.rmtree(base_path)
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000722
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +0200723 @unittest.skipUnless(hasattr(posix, 'getgrouplist'), "test needs posix.getgrouplist()")
724 @unittest.skipUnless(hasattr(pwd, 'getpwuid'), "test needs pwd.getpwuid()")
725 @unittest.skipUnless(hasattr(os, 'getuid'), "test needs os.getuid()")
726 def test_getgrouplist(self):
Ross Lagerwalla0b315f2012-12-13 15:20:26 +0000727 user = pwd.getpwuid(os.getuid())[0]
728 group = pwd.getpwuid(os.getuid())[3]
729 self.assertIn(group, posix.getgrouplist(user, group))
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +0200730
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +0200731
Antoine Pitrou318b8f32011-01-12 18:45:27 +0000732 @unittest.skipUnless(hasattr(os, 'getegid'), "test needs os.getegid()")
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +0000733 def test_getgroups(self):
734 with os.popen('id -G') as idg:
735 groups = idg.read().strip()
Charles-François Natalie8a255a2012-05-02 20:01:38 +0200736 ret = idg.close()
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +0000737
Benjamin Petersonb29614e2012-10-09 11:16:03 -0400738 if ret is not None or not groups:
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +0000739 raise unittest.SkipTest("need working 'id -G'")
740
Ned Deily028915e2013-02-02 15:08:52 -0800741 # Issues 16698: OS X ABIs prior to 10.6 have limits on getgroups()
742 if sys.platform == 'darwin':
743 import sysconfig
744 dt = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET') or '10.0'
745 if float(dt) < 10.6:
746 raise unittest.SkipTest("getgroups(2) is broken prior to 10.6")
747
Ronald Oussoren7fb6f512010-08-01 19:18:13 +0000748 # 'id -G' and 'os.getgroups()' should return the same
749 # groups, ignoring order and duplicates.
Antoine Pitrou318b8f32011-01-12 18:45:27 +0000750 # #10822 - it is implementation defined whether posix.getgroups()
751 # includes the effective gid so we include it anyway, since id -G does
Ronald Oussorencb615e62010-07-24 14:15:19 +0000752 self.assertEqual(
Ronald Oussoren7fb6f512010-08-01 19:18:13 +0000753 set([int(x) for x in groups.split()]),
Antoine Pitrou318b8f32011-01-12 18:45:27 +0000754 set(posix.getgroups() + [posix.getegid()]))
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +0000755
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000756 # tests for the posix *at functions follow
757
Larry Hastings9cf065c2012-06-22 16:30:09 -0700758 @unittest.skipUnless(os.access in os.supports_dir_fd, "test needs dir_fd support for os.access()")
759 def test_access_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000760 f = posix.open(posix.getcwd(), posix.O_RDONLY)
761 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -0700762 self.assertTrue(posix.access(support.TESTFN, os.R_OK, dir_fd=f))
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000763 finally:
764 posix.close(f)
765
Larry Hastings9cf065c2012-06-22 16:30:09 -0700766 @unittest.skipUnless(os.chmod in os.supports_dir_fd, "test needs dir_fd support in os.chmod()")
767 def test_chmod_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000768 os.chmod(support.TESTFN, stat.S_IRUSR)
769
770 f = posix.open(posix.getcwd(), posix.O_RDONLY)
771 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -0700772 posix.chmod(support.TESTFN, stat.S_IRUSR | stat.S_IWUSR, dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000773
774 s = posix.stat(support.TESTFN)
775 self.assertEqual(s[0] & stat.S_IRWXU, stat.S_IRUSR | stat.S_IWUSR)
776 finally:
777 posix.close(f)
778
Larry Hastings9cf065c2012-06-22 16:30:09 -0700779 @unittest.skipUnless(os.chown in os.supports_dir_fd, "test needs dir_fd support in os.chown()")
780 def test_chown_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000781 support.unlink(support.TESTFN)
Victor Stinnerbf816222011-06-30 23:25:47 +0200782 support.create_empty_file(support.TESTFN)
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000783
784 f = posix.open(posix.getcwd(), posix.O_RDONLY)
785 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -0700786 posix.chown(support.TESTFN, os.getuid(), os.getgid(), dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000787 finally:
788 posix.close(f)
789
Larry Hastings9cf065c2012-06-22 16:30:09 -0700790 @unittest.skipUnless(os.stat in os.supports_dir_fd, "test needs dir_fd support in os.stat()")
791 def test_stat_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000792 support.unlink(support.TESTFN)
793 with open(support.TESTFN, 'w') as outfile:
794 outfile.write("testline\n")
795
796 f = posix.open(posix.getcwd(), posix.O_RDONLY)
797 try:
798 s1 = posix.stat(support.TESTFN)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700799 s2 = posix.stat(support.TESTFN, dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000800 self.assertEqual(s1, s2)
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200801 s2 = posix.stat(support.TESTFN, dir_fd=None)
802 self.assertEqual(s1, s2)
803 self.assertRaisesRegex(TypeError, 'should be integer, not',
804 posix.stat, support.TESTFN, dir_fd=posix.getcwd())
805 self.assertRaisesRegex(TypeError, 'should be integer, not',
806 posix.stat, support.TESTFN, dir_fd=float(f))
807 self.assertRaises(OverflowError,
808 posix.stat, support.TESTFN, dir_fd=10**20)
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000809 finally:
810 posix.close(f)
811
Larry Hastings9cf065c2012-06-22 16:30:09 -0700812 @unittest.skipUnless(os.utime in os.supports_dir_fd, "test needs dir_fd support in os.utime()")
813 def test_utime_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000814 f = posix.open(posix.getcwd(), posix.O_RDONLY)
815 try:
816 now = time.time()
Larry Hastings9cf065c2012-06-22 16:30:09 -0700817 posix.utime(support.TESTFN, None, dir_fd=f)
818 posix.utime(support.TESTFN, dir_fd=f)
819 self.assertRaises(TypeError, posix.utime, support.TESTFN, now, dir_fd=f)
820 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, None), dir_fd=f)
821 self.assertRaises(TypeError, posix.utime, support.TESTFN, (now, None), dir_fd=f)
822 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, now), dir_fd=f)
823 self.assertRaises(TypeError, posix.utime, support.TESTFN, (now, "x"), dir_fd=f)
824 posix.utime(support.TESTFN, (int(now), int(now)), dir_fd=f)
825 posix.utime(support.TESTFN, (now, now), dir_fd=f)
826 posix.utime(support.TESTFN,
827 (int(now), int((now - int(now)) * 1e9)), dir_fd=f)
828 posix.utime(support.TESTFN, dir_fd=f,
829 times=(int(now), int((now - int(now)) * 1e9)))
830
Larry Hastings90867a52012-06-22 17:01:41 -0700831 # try dir_fd and follow_symlinks together
Larry Hastings9cf065c2012-06-22 16:30:09 -0700832 if os.utime in os.supports_follow_symlinks:
Larry Hastings90867a52012-06-22 17:01:41 -0700833 try:
834 posix.utime(support.TESTFN, follow_symlinks=False, dir_fd=f)
Georg Brandl969288e2012-06-26 09:25:44 +0200835 except ValueError:
Larry Hastings90867a52012-06-22 17:01:41 -0700836 # whoops! using both together not supported on this platform.
837 pass
Larry Hastings9cf065c2012-06-22 16:30:09 -0700838
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000839 finally:
840 posix.close(f)
841
Larry Hastings9cf065c2012-06-22 16:30:09 -0700842 @unittest.skipUnless(os.link in os.supports_dir_fd, "test needs dir_fd support in os.link()")
843 def test_link_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000844 f = posix.open(posix.getcwd(), posix.O_RDONLY)
845 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -0700846 posix.link(support.TESTFN, support.TESTFN + 'link', src_dir_fd=f, dst_dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000847 # should have same inodes
848 self.assertEqual(posix.stat(support.TESTFN)[1],
849 posix.stat(support.TESTFN + 'link')[1])
850 finally:
851 posix.close(f)
852 support.unlink(support.TESTFN + 'link')
853
Larry Hastings9cf065c2012-06-22 16:30:09 -0700854 @unittest.skipUnless(os.mkdir in os.supports_dir_fd, "test needs dir_fd support in os.mkdir()")
855 def test_mkdir_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000856 f = posix.open(posix.getcwd(), posix.O_RDONLY)
857 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -0700858 posix.mkdir(support.TESTFN + 'dir', dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000859 posix.stat(support.TESTFN + 'dir') # should not raise exception
860 finally:
861 posix.close(f)
862 support.rmtree(support.TESTFN + 'dir')
863
Larry Hastings9cf065c2012-06-22 16:30:09 -0700864 @unittest.skipUnless((os.mknod in os.supports_dir_fd) and hasattr(stat, 'S_IFIFO'),
865 "test requires both stat.S_IFIFO and dir_fd support for os.mknod()")
866 def test_mknod_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000867 # Test using mknodat() to create a FIFO (the only use specified
868 # by POSIX).
869 support.unlink(support.TESTFN)
870 mode = stat.S_IFIFO | stat.S_IRUSR | stat.S_IWUSR
871 f = posix.open(posix.getcwd(), posix.O_RDONLY)
872 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -0700873 posix.mknod(support.TESTFN, mode, 0, dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000874 except OSError as e:
875 # Some old systems don't allow unprivileged users to use
876 # mknod(), or only support creating device nodes.
877 self.assertIn(e.errno, (errno.EPERM, errno.EINVAL))
878 else:
879 self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode))
880 finally:
881 posix.close(f)
882
Larry Hastings9cf065c2012-06-22 16:30:09 -0700883 @unittest.skipUnless(os.open in os.supports_dir_fd, "test needs dir_fd support in os.open()")
884 def test_open_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000885 support.unlink(support.TESTFN)
886 with open(support.TESTFN, 'w') as outfile:
887 outfile.write("testline\n")
888 a = posix.open(posix.getcwd(), posix.O_RDONLY)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700889 b = posix.open(support.TESTFN, posix.O_RDONLY, dir_fd=a)
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000890 try:
891 res = posix.read(b, 9).decode(encoding="utf-8")
892 self.assertEqual("testline\n", res)
893 finally:
894 posix.close(a)
895 posix.close(b)
896
Larry Hastings9cf065c2012-06-22 16:30:09 -0700897 @unittest.skipUnless(os.readlink in os.supports_dir_fd, "test needs dir_fd support in os.readlink()")
898 def test_readlink_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000899 os.symlink(support.TESTFN, support.TESTFN + 'link')
900 f = posix.open(posix.getcwd(), posix.O_RDONLY)
901 try:
902 self.assertEqual(posix.readlink(support.TESTFN + 'link'),
Larry Hastings9cf065c2012-06-22 16:30:09 -0700903 posix.readlink(support.TESTFN + 'link', dir_fd=f))
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000904 finally:
905 support.unlink(support.TESTFN + 'link')
906 posix.close(f)
907
Larry Hastings9cf065c2012-06-22 16:30:09 -0700908 @unittest.skipUnless(os.rename in os.supports_dir_fd, "test needs dir_fd support in os.rename()")
909 def test_rename_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000910 support.unlink(support.TESTFN)
Victor Stinnerbf816222011-06-30 23:25:47 +0200911 support.create_empty_file(support.TESTFN + 'ren')
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000912 f = posix.open(posix.getcwd(), posix.O_RDONLY)
913 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -0700914 posix.rename(support.TESTFN + 'ren', support.TESTFN, src_dir_fd=f, dst_dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000915 except:
916 posix.rename(support.TESTFN + 'ren', support.TESTFN)
917 raise
918 else:
Andrew Svetlov5b898402012-12-18 21:26:36 +0200919 posix.stat(support.TESTFN) # should not raise exception
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000920 finally:
921 posix.close(f)
922
Larry Hastings9cf065c2012-06-22 16:30:09 -0700923 @unittest.skipUnless(os.symlink in os.supports_dir_fd, "test needs dir_fd support in os.symlink()")
924 def test_symlink_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000925 f = posix.open(posix.getcwd(), posix.O_RDONLY)
926 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -0700927 posix.symlink(support.TESTFN, support.TESTFN + 'link', dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000928 self.assertEqual(posix.readlink(support.TESTFN + 'link'), support.TESTFN)
929 finally:
930 posix.close(f)
931 support.unlink(support.TESTFN + 'link')
932
Larry Hastings9cf065c2012-06-22 16:30:09 -0700933 @unittest.skipUnless(os.unlink in os.supports_dir_fd, "test needs dir_fd support in os.unlink()")
934 def test_unlink_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000935 f = posix.open(posix.getcwd(), posix.O_RDONLY)
Victor Stinnerbf816222011-06-30 23:25:47 +0200936 support.create_empty_file(support.TESTFN + 'del')
Andrew Svetlov5b898402012-12-18 21:26:36 +0200937 posix.stat(support.TESTFN + 'del') # should not raise exception
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000938 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -0700939 posix.unlink(support.TESTFN + 'del', dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000940 except:
941 support.unlink(support.TESTFN + 'del')
942 raise
943 else:
944 self.assertRaises(OSError, posix.stat, support.TESTFN + 'link')
945 finally:
946 posix.close(f)
947
Larry Hastings9cf065c2012-06-22 16:30:09 -0700948 @unittest.skipUnless(os.mkfifo in os.supports_dir_fd, "test needs dir_fd support in os.mkfifo()")
949 def test_mkfifo_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000950 support.unlink(support.TESTFN)
951 f = posix.open(posix.getcwd(), posix.O_RDONLY)
952 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -0700953 posix.mkfifo(support.TESTFN, stat.S_IRUSR | stat.S_IWUSR, dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +0000954 self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode))
955 finally:
956 posix.close(f)
957
Benjamin Peterson94b580d2011-08-02 17:30:04 -0500958 requires_sched_h = unittest.skipUnless(hasattr(posix, 'sched_yield'),
959 "don't have scheduling support")
Antoine Pitrou84869872012-08-04 16:16:35 +0200960 requires_sched_affinity = unittest.skipUnless(hasattr(posix, 'sched_setaffinity'),
Benjamin Peterson50ba2712011-08-02 22:15:40 -0500961 "don't have sched affinity support")
Benjamin Peterson94b580d2011-08-02 17:30:04 -0500962
963 @requires_sched_h
964 def test_sched_yield(self):
965 # This has no error conditions (at least on Linux).
966 posix.sched_yield()
967
968 @requires_sched_h
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +0200969 @unittest.skipUnless(hasattr(posix, 'sched_get_priority_max'),
970 "requires sched_get_priority_max()")
Benjamin Peterson94b580d2011-08-02 17:30:04 -0500971 def test_sched_priority(self):
972 # Round-robin usually has interesting priorities.
973 pol = posix.SCHED_RR
974 lo = posix.sched_get_priority_min(pol)
975 hi = posix.sched_get_priority_max(pol)
976 self.assertIsInstance(lo, int)
977 self.assertIsInstance(hi, int)
978 self.assertGreaterEqual(hi, lo)
Benjamin Peterson539b6c42011-08-02 22:09:37 -0500979 # OSX evidently just returns 15 without checking the argument.
980 if sys.platform != "darwin":
Benjamin Petersonc1581582011-08-02 22:10:55 -0500981 self.assertRaises(OSError, posix.sched_get_priority_min, -23)
982 self.assertRaises(OSError, posix.sched_get_priority_max, -23)
Benjamin Peterson94b580d2011-08-02 17:30:04 -0500983
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -0500984 @unittest.skipUnless(hasattr(posix, 'sched_setscheduler'), "can't change scheduler")
Benjamin Peterson94b580d2011-08-02 17:30:04 -0500985 def test_get_and_set_scheduler_and_param(self):
986 possible_schedulers = [sched for name, sched in posix.__dict__.items()
987 if name.startswith("SCHED_")]
988 mine = posix.sched_getscheduler(0)
989 self.assertIn(mine, possible_schedulers)
990 try:
Jesus Ceaceb5d162011-09-10 01:16:55 +0200991 parent = posix.sched_getscheduler(os.getppid())
Benjamin Peterson94b580d2011-08-02 17:30:04 -0500992 except OSError as e:
Jesus Ceaceb5d162011-09-10 01:16:55 +0200993 if e.errno != errno.EPERM:
Benjamin Peterson94b580d2011-08-02 17:30:04 -0500994 raise
995 else:
Jesus Ceaceb5d162011-09-10 01:16:55 +0200996 self.assertIn(parent, possible_schedulers)
Benjamin Peterson94b580d2011-08-02 17:30:04 -0500997 self.assertRaises(OSError, posix.sched_getscheduler, -1)
998 self.assertRaises(OSError, posix.sched_getparam, -1)
999 param = posix.sched_getparam(0)
1000 self.assertIsInstance(param.sched_priority, int)
Charles-François Natali7b911cb2011-08-21 12:41:43 +02001001
Charles-François Natalib402a5c2013-01-13 14:13:25 +01001002 # POSIX states that calling sched_setparam() or sched_setscheduler() on
1003 # a process with a scheduling policy other than SCHED_FIFO or SCHED_RR
1004 # is implementation-defined: NetBSD and FreeBSD can return EINVAL.
1005 if not sys.platform.startswith(('freebsd', 'netbsd')):
1006 try:
1007 posix.sched_setscheduler(0, mine, param)
1008 posix.sched_setparam(0, param)
1009 except OSError as e:
1010 if e.errno != errno.EPERM:
1011 raise
Charles-François Natali7b911cb2011-08-21 12:41:43 +02001012 self.assertRaises(OSError, posix.sched_setparam, -1, param)
1013
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001014 self.assertRaises(OSError, posix.sched_setscheduler, -1, mine, param)
1015 self.assertRaises(TypeError, posix.sched_setscheduler, 0, mine, None)
1016 self.assertRaises(TypeError, posix.sched_setparam, 0, 43)
1017 param = posix.sched_param(None)
1018 self.assertRaises(TypeError, posix.sched_setparam, 0, param)
1019 large = 214748364700
1020 param = posix.sched_param(large)
1021 self.assertRaises(OverflowError, posix.sched_setparam, 0, param)
1022 param = posix.sched_param(sched_priority=-large)
1023 self.assertRaises(OverflowError, posix.sched_setparam, 0, param)
1024
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05001025 @unittest.skipUnless(hasattr(posix, "sched_rr_get_interval"), "no function")
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001026 def test_sched_rr_get_interval(self):
Benjamin Peterson43234ab2011-08-02 22:19:14 -05001027 try:
1028 interval = posix.sched_rr_get_interval(0)
1029 except OSError as e:
1030 # This likely means that sched_rr_get_interval is only valid for
1031 # processes with the SCHED_RR scheduler in effect.
1032 if e.errno != errno.EINVAL:
1033 raise
1034 self.skipTest("only works on SCHED_RR processes")
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001035 self.assertIsInstance(interval, float)
1036 # Reasonable constraints, I think.
1037 self.assertGreaterEqual(interval, 0.)
1038 self.assertLess(interval, 1.)
1039
Benjamin Peterson2740af82011-08-02 17:41:34 -05001040 @requires_sched_affinity
Antoine Pitrou84869872012-08-04 16:16:35 +02001041 def test_sched_getaffinity(self):
1042 mask = posix.sched_getaffinity(0)
1043 self.assertIsInstance(mask, set)
1044 self.assertGreaterEqual(len(mask), 1)
1045 self.assertRaises(OSError, posix.sched_getaffinity, -1)
1046 for cpu in mask:
1047 self.assertIsInstance(cpu, int)
1048 self.assertGreaterEqual(cpu, 0)
1049 self.assertLess(cpu, 1 << 32)
1050
1051 @requires_sched_affinity
1052 def test_sched_setaffinity(self):
1053 mask = posix.sched_getaffinity(0)
1054 if len(mask) > 1:
1055 # Empty masks are forbidden
1056 mask.pop()
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001057 posix.sched_setaffinity(0, mask)
Antoine Pitrou84869872012-08-04 16:16:35 +02001058 self.assertEqual(posix.sched_getaffinity(0), mask)
1059 self.assertRaises(OSError, posix.sched_setaffinity, 0, [])
1060 self.assertRaises(ValueError, posix.sched_setaffinity, 0, [-10])
1061 self.assertRaises(OverflowError, posix.sched_setaffinity, 0, [1<<128])
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001062 self.assertRaises(OSError, posix.sched_setaffinity, -1, mask)
1063
Victor Stinner8b905bd2011-10-25 13:34:04 +02001064 def test_rtld_constants(self):
1065 # check presence of major RTLD_* constants
1066 posix.RTLD_LAZY
1067 posix.RTLD_NOW
1068 posix.RTLD_GLOBAL
1069 posix.RTLD_LOCAL
1070
Jesus Cea60c13dd2012-06-23 02:58:14 +02001071 @unittest.skipUnless(hasattr(os, 'SEEK_HOLE'),
1072 "test needs an OS that reports file holes")
Hynek Schlawackf841e422012-06-24 09:51:46 +02001073 def test_fs_holes(self):
Jesus Cea94363612012-06-22 18:32:07 +02001074 # Even if the filesystem doesn't report holes,
1075 # if the OS supports it the SEEK_* constants
1076 # will be defined and will have a consistent
1077 # behaviour:
1078 # os.SEEK_DATA = current position
1079 # os.SEEK_HOLE = end of file position
Hynek Schlawackf841e422012-06-24 09:51:46 +02001080 with open(support.TESTFN, 'r+b') as fp:
Jesus Cea94363612012-06-22 18:32:07 +02001081 fp.write(b"hello")
1082 fp.flush()
1083 size = fp.tell()
1084 fno = fp.fileno()
Jesus Cead46f7d22012-07-07 14:56:04 +02001085 try :
1086 for i in range(size):
1087 self.assertEqual(i, os.lseek(fno, i, os.SEEK_DATA))
1088 self.assertLessEqual(size, os.lseek(fno, i, os.SEEK_HOLE))
1089 self.assertRaises(OSError, os.lseek, fno, size, os.SEEK_DATA)
1090 self.assertRaises(OSError, os.lseek, fno, size, os.SEEK_HOLE)
1091 except OSError :
1092 # Some OSs claim to support SEEK_HOLE/SEEK_DATA
1093 # but it is not true.
1094 # For instance:
1095 # http://lists.freebsd.org/pipermail/freebsd-amd64/2012-January/014332.html
1096 raise unittest.SkipTest("OSError raised!")
Jesus Cea94363612012-06-22 18:32:07 +02001097
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00001098class PosixGroupsTester(unittest.TestCase):
1099
1100 def setUp(self):
1101 if posix.getuid() != 0:
1102 raise unittest.SkipTest("not enough privileges")
1103 if not hasattr(posix, 'getgroups'):
1104 raise unittest.SkipTest("need posix.getgroups")
1105 if sys.platform == 'darwin':
1106 raise unittest.SkipTest("getgroups(2) is broken on OSX")
1107 self.saved_groups = posix.getgroups()
1108
1109 def tearDown(self):
1110 if hasattr(posix, 'setgroups'):
1111 posix.setgroups(self.saved_groups)
1112 elif hasattr(posix, 'initgroups'):
1113 name = pwd.getpwuid(posix.getuid()).pw_name
1114 posix.initgroups(name, self.saved_groups[0])
1115
1116 @unittest.skipUnless(hasattr(posix, 'initgroups'),
1117 "test needs posix.initgroups()")
1118 def test_initgroups(self):
1119 # find missing group
1120
Antoine Pitroue5a91012010-09-04 17:32:06 +00001121 g = max(self.saved_groups) + 1
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00001122 name = pwd.getpwuid(posix.getuid()).pw_name
1123 posix.initgroups(name, g)
1124 self.assertIn(g, posix.getgroups())
1125
1126 @unittest.skipUnless(hasattr(posix, 'setgroups'),
1127 "test needs posix.setgroups()")
1128 def test_setgroups(self):
Antoine Pitroue5a91012010-09-04 17:32:06 +00001129 for groups in [[0], list(range(16))]:
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00001130 posix.setgroups(groups)
1131 self.assertListEqual(groups, posix.getgroups())
1132
Neal Norwitze241ce82003-02-17 18:17:05 +00001133def test_main():
Antoine Pitrou68c95922011-03-20 17:33:57 +01001134 try:
1135 support.run_unittest(PosixTester, PosixGroupsTester)
1136 finally:
1137 support.reap_children()
Neal Norwitze241ce82003-02-17 18:17:05 +00001138
1139if __name__ == '__main__':
1140 test_main()