1 | n/a | "Test posix functions" |
---|
2 | n/a | |
---|
3 | n/a | from test import support |
---|
4 | n/a | android_not_root = support.android_not_root |
---|
5 | n/a | |
---|
6 | n/a | # Skip these tests if there is no posix module. |
---|
7 | n/a | posix = support.import_module('posix') |
---|
8 | n/a | |
---|
9 | n/a | import errno |
---|
10 | n/a | import sys |
---|
11 | n/a | import time |
---|
12 | n/a | import os |
---|
13 | n/a | import platform |
---|
14 | n/a | import pwd |
---|
15 | n/a | import stat |
---|
16 | n/a | import tempfile |
---|
17 | n/a | import unittest |
---|
18 | n/a | import warnings |
---|
19 | n/a | |
---|
20 | n/a | _DUMMY_SYMLINK = os.path.join(tempfile.gettempdir(), |
---|
21 | n/a | support.TESTFN + '-dummy-symlink') |
---|
22 | n/a | |
---|
23 | n/a | class PosixTester(unittest.TestCase): |
---|
24 | n/a | |
---|
25 | n/a | def setUp(self): |
---|
26 | n/a | # create empty file |
---|
27 | n/a | fp = open(support.TESTFN, 'w+') |
---|
28 | n/a | fp.close() |
---|
29 | n/a | self.teardown_files = [ support.TESTFN ] |
---|
30 | n/a | self._warnings_manager = support.check_warnings() |
---|
31 | n/a | self._warnings_manager.__enter__() |
---|
32 | n/a | warnings.filterwarnings('ignore', '.* potential security risk .*', |
---|
33 | n/a | RuntimeWarning) |
---|
34 | n/a | |
---|
35 | n/a | def tearDown(self): |
---|
36 | n/a | for teardown_file in self.teardown_files: |
---|
37 | n/a | support.unlink(teardown_file) |
---|
38 | n/a | self._warnings_manager.__exit__(None, None, None) |
---|
39 | n/a | |
---|
40 | n/a | def testNoArgFunctions(self): |
---|
41 | n/a | # test posix functions which take no arguments and have |
---|
42 | n/a | # no side-effects which we need to cleanup (e.g., fork, wait, abort) |
---|
43 | n/a | NO_ARG_FUNCTIONS = [ "ctermid", "getcwd", "getcwdb", "uname", |
---|
44 | n/a | "times", "getloadavg", |
---|
45 | n/a | "getegid", "geteuid", "getgid", "getgroups", |
---|
46 | n/a | "getpid", "getpgrp", "getppid", "getuid", "sync", |
---|
47 | n/a | ] |
---|
48 | n/a | |
---|
49 | n/a | for name in NO_ARG_FUNCTIONS: |
---|
50 | n/a | posix_func = getattr(posix, name, None) |
---|
51 | n/a | if posix_func is not None: |
---|
52 | n/a | posix_func() |
---|
53 | n/a | self.assertRaises(TypeError, posix_func, 1) |
---|
54 | n/a | |
---|
55 | n/a | @unittest.skipUnless(hasattr(posix, 'getresuid'), |
---|
56 | n/a | 'test needs posix.getresuid()') |
---|
57 | n/a | def test_getresuid(self): |
---|
58 | n/a | user_ids = posix.getresuid() |
---|
59 | n/a | self.assertEqual(len(user_ids), 3) |
---|
60 | n/a | for val in user_ids: |
---|
61 | n/a | self.assertGreaterEqual(val, 0) |
---|
62 | n/a | |
---|
63 | n/a | @unittest.skipUnless(hasattr(posix, 'getresgid'), |
---|
64 | n/a | 'test needs posix.getresgid()') |
---|
65 | n/a | def test_getresgid(self): |
---|
66 | n/a | group_ids = posix.getresgid() |
---|
67 | n/a | self.assertEqual(len(group_ids), 3) |
---|
68 | n/a | for val in group_ids: |
---|
69 | n/a | self.assertGreaterEqual(val, 0) |
---|
70 | n/a | |
---|
71 | n/a | @unittest.skipUnless(hasattr(posix, 'setresuid'), |
---|
72 | n/a | 'test needs posix.setresuid()') |
---|
73 | n/a | def test_setresuid(self): |
---|
74 | n/a | current_user_ids = posix.getresuid() |
---|
75 | n/a | self.assertIsNone(posix.setresuid(*current_user_ids)) |
---|
76 | n/a | # -1 means don't change that value. |
---|
77 | n/a | self.assertIsNone(posix.setresuid(-1, -1, -1)) |
---|
78 | n/a | |
---|
79 | n/a | @unittest.skipUnless(hasattr(posix, 'setresuid'), |
---|
80 | n/a | 'test needs posix.setresuid()') |
---|
81 | n/a | def test_setresuid_exception(self): |
---|
82 | n/a | # Don't do this test if someone is silly enough to run us as root. |
---|
83 | n/a | current_user_ids = posix.getresuid() |
---|
84 | n/a | if 0 not in current_user_ids: |
---|
85 | n/a | new_user_ids = (current_user_ids[0]+1, -1, -1) |
---|
86 | n/a | self.assertRaises(OSError, posix.setresuid, *new_user_ids) |
---|
87 | n/a | |
---|
88 | n/a | @unittest.skipUnless(hasattr(posix, 'setresgid'), |
---|
89 | n/a | 'test needs posix.setresgid()') |
---|
90 | n/a | def test_setresgid(self): |
---|
91 | n/a | current_group_ids = posix.getresgid() |
---|
92 | n/a | self.assertIsNone(posix.setresgid(*current_group_ids)) |
---|
93 | n/a | # -1 means don't change that value. |
---|
94 | n/a | self.assertIsNone(posix.setresgid(-1, -1, -1)) |
---|
95 | n/a | |
---|
96 | n/a | @unittest.skipUnless(hasattr(posix, 'setresgid'), |
---|
97 | n/a | 'test needs posix.setresgid()') |
---|
98 | n/a | def test_setresgid_exception(self): |
---|
99 | n/a | # Don't do this test if someone is silly enough to run us as root. |
---|
100 | n/a | current_group_ids = posix.getresgid() |
---|
101 | n/a | if 0 not in current_group_ids: |
---|
102 | n/a | new_group_ids = (current_group_ids[0]+1, -1, -1) |
---|
103 | n/a | self.assertRaises(OSError, posix.setresgid, *new_group_ids) |
---|
104 | n/a | |
---|
105 | n/a | @unittest.skipUnless(hasattr(posix, 'initgroups'), |
---|
106 | n/a | "test needs os.initgroups()") |
---|
107 | n/a | def test_initgroups(self): |
---|
108 | n/a | # It takes a string and an integer; check that it raises a TypeError |
---|
109 | n/a | # for other argument lists. |
---|
110 | n/a | self.assertRaises(TypeError, posix.initgroups) |
---|
111 | n/a | self.assertRaises(TypeError, posix.initgroups, None) |
---|
112 | n/a | self.assertRaises(TypeError, posix.initgroups, 3, "foo") |
---|
113 | n/a | self.assertRaises(TypeError, posix.initgroups, "foo", 3, object()) |
---|
114 | n/a | |
---|
115 | n/a | # If a non-privileged user invokes it, it should fail with OSError |
---|
116 | n/a | # EPERM. |
---|
117 | n/a | if os.getuid() != 0: |
---|
118 | n/a | try: |
---|
119 | n/a | name = pwd.getpwuid(posix.getuid()).pw_name |
---|
120 | n/a | except KeyError: |
---|
121 | n/a | # the current UID may not have a pwd entry |
---|
122 | n/a | raise unittest.SkipTest("need a pwd entry") |
---|
123 | n/a | try: |
---|
124 | n/a | posix.initgroups(name, 13) |
---|
125 | n/a | except OSError as e: |
---|
126 | n/a | self.assertEqual(e.errno, errno.EPERM) |
---|
127 | n/a | else: |
---|
128 | n/a | self.fail("Expected OSError to be raised by initgroups") |
---|
129 | n/a | |
---|
130 | n/a | @unittest.skipUnless(hasattr(posix, 'statvfs'), |
---|
131 | n/a | 'test needs posix.statvfs()') |
---|
132 | n/a | def test_statvfs(self): |
---|
133 | n/a | self.assertTrue(posix.statvfs(os.curdir)) |
---|
134 | n/a | |
---|
135 | n/a | @unittest.skipUnless(hasattr(posix, 'fstatvfs'), |
---|
136 | n/a | 'test needs posix.fstatvfs()') |
---|
137 | n/a | def test_fstatvfs(self): |
---|
138 | n/a | fp = open(support.TESTFN) |
---|
139 | n/a | try: |
---|
140 | n/a | self.assertTrue(posix.fstatvfs(fp.fileno())) |
---|
141 | n/a | self.assertTrue(posix.statvfs(fp.fileno())) |
---|
142 | n/a | finally: |
---|
143 | n/a | fp.close() |
---|
144 | n/a | |
---|
145 | n/a | @unittest.skipUnless(hasattr(posix, 'ftruncate'), |
---|
146 | n/a | 'test needs posix.ftruncate()') |
---|
147 | n/a | def test_ftruncate(self): |
---|
148 | n/a | fp = open(support.TESTFN, 'w+') |
---|
149 | n/a | try: |
---|
150 | n/a | # we need to have some data to truncate |
---|
151 | n/a | fp.write('test') |
---|
152 | n/a | fp.flush() |
---|
153 | n/a | posix.ftruncate(fp.fileno(), 0) |
---|
154 | n/a | finally: |
---|
155 | n/a | fp.close() |
---|
156 | n/a | |
---|
157 | n/a | @unittest.skipUnless(hasattr(posix, 'truncate'), "test needs posix.truncate()") |
---|
158 | n/a | def test_truncate(self): |
---|
159 | n/a | with open(support.TESTFN, 'w') as fp: |
---|
160 | n/a | fp.write('test') |
---|
161 | n/a | fp.flush() |
---|
162 | n/a | posix.truncate(support.TESTFN, 0) |
---|
163 | n/a | |
---|
164 | n/a | @unittest.skipUnless(getattr(os, 'execve', None) in os.supports_fd, "test needs execve() to support the fd parameter") |
---|
165 | n/a | @unittest.skipUnless(hasattr(os, 'fork'), "test needs os.fork()") |
---|
166 | n/a | @unittest.skipUnless(hasattr(os, 'waitpid'), "test needs os.waitpid()") |
---|
167 | n/a | def test_fexecve(self): |
---|
168 | n/a | fp = os.open(sys.executable, os.O_RDONLY) |
---|
169 | n/a | try: |
---|
170 | n/a | pid = os.fork() |
---|
171 | n/a | if pid == 0: |
---|
172 | n/a | os.chdir(os.path.split(sys.executable)[0]) |
---|
173 | n/a | posix.execve(fp, [sys.executable, '-c', 'pass'], os.environ) |
---|
174 | n/a | else: |
---|
175 | n/a | self.assertEqual(os.waitpid(pid, 0), (pid, 0)) |
---|
176 | n/a | finally: |
---|
177 | n/a | os.close(fp) |
---|
178 | n/a | |
---|
179 | n/a | @unittest.skipUnless(hasattr(posix, 'waitid'), "test needs posix.waitid()") |
---|
180 | n/a | @unittest.skipUnless(hasattr(os, 'fork'), "test needs os.fork()") |
---|
181 | n/a | def test_waitid(self): |
---|
182 | n/a | pid = os.fork() |
---|
183 | n/a | if pid == 0: |
---|
184 | n/a | os.chdir(os.path.split(sys.executable)[0]) |
---|
185 | n/a | posix.execve(sys.executable, [sys.executable, '-c', 'pass'], os.environ) |
---|
186 | n/a | else: |
---|
187 | n/a | res = posix.waitid(posix.P_PID, pid, posix.WEXITED) |
---|
188 | n/a | self.assertEqual(pid, res.si_pid) |
---|
189 | n/a | |
---|
190 | n/a | @unittest.skipUnless(hasattr(posix, 'lockf'), "test needs posix.lockf()") |
---|
191 | n/a | def test_lockf(self): |
---|
192 | n/a | fd = os.open(support.TESTFN, os.O_WRONLY | os.O_CREAT) |
---|
193 | n/a | try: |
---|
194 | n/a | os.write(fd, b'test') |
---|
195 | n/a | os.lseek(fd, 0, os.SEEK_SET) |
---|
196 | n/a | posix.lockf(fd, posix.F_LOCK, 4) |
---|
197 | n/a | # section is locked |
---|
198 | n/a | posix.lockf(fd, posix.F_ULOCK, 4) |
---|
199 | n/a | finally: |
---|
200 | n/a | os.close(fd) |
---|
201 | n/a | |
---|
202 | n/a | @unittest.skipUnless(hasattr(posix, 'pread'), "test needs posix.pread()") |
---|
203 | n/a | def test_pread(self): |
---|
204 | n/a | fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT) |
---|
205 | n/a | try: |
---|
206 | n/a | os.write(fd, b'test') |
---|
207 | n/a | os.lseek(fd, 0, os.SEEK_SET) |
---|
208 | n/a | self.assertEqual(b'es', posix.pread(fd, 2, 1)) |
---|
209 | n/a | # the first pread() shouldn't disturb the file offset |
---|
210 | n/a | self.assertEqual(b'te', posix.read(fd, 2)) |
---|
211 | n/a | finally: |
---|
212 | n/a | os.close(fd) |
---|
213 | n/a | |
---|
214 | n/a | @unittest.skipUnless(hasattr(posix, 'pwrite'), "test needs posix.pwrite()") |
---|
215 | n/a | def test_pwrite(self): |
---|
216 | n/a | fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT) |
---|
217 | n/a | try: |
---|
218 | n/a | os.write(fd, b'test') |
---|
219 | n/a | os.lseek(fd, 0, os.SEEK_SET) |
---|
220 | n/a | posix.pwrite(fd, b'xx', 1) |
---|
221 | n/a | self.assertEqual(b'txxt', posix.read(fd, 4)) |
---|
222 | n/a | finally: |
---|
223 | n/a | os.close(fd) |
---|
224 | n/a | |
---|
225 | n/a | @unittest.skipUnless(hasattr(posix, 'posix_fallocate'), |
---|
226 | n/a | "test needs posix.posix_fallocate()") |
---|
227 | n/a | def test_posix_fallocate(self): |
---|
228 | n/a | fd = os.open(support.TESTFN, os.O_WRONLY | os.O_CREAT) |
---|
229 | n/a | try: |
---|
230 | n/a | posix.posix_fallocate(fd, 0, 10) |
---|
231 | n/a | except OSError as inst: |
---|
232 | n/a | # issue10812, ZFS doesn't appear to support posix_fallocate, |
---|
233 | n/a | # so skip Solaris-based since they are likely to have ZFS. |
---|
234 | n/a | if inst.errno != errno.EINVAL or not sys.platform.startswith("sunos"): |
---|
235 | n/a | raise |
---|
236 | n/a | finally: |
---|
237 | n/a | os.close(fd) |
---|
238 | n/a | |
---|
239 | n/a | @unittest.skipUnless(hasattr(posix, 'posix_fadvise'), |
---|
240 | n/a | "test needs posix.posix_fadvise()") |
---|
241 | n/a | def test_posix_fadvise(self): |
---|
242 | n/a | fd = os.open(support.TESTFN, os.O_RDONLY) |
---|
243 | n/a | try: |
---|
244 | n/a | posix.posix_fadvise(fd, 0, 0, posix.POSIX_FADV_WILLNEED) |
---|
245 | n/a | finally: |
---|
246 | n/a | os.close(fd) |
---|
247 | n/a | |
---|
248 | n/a | @unittest.skipUnless(os.utime in os.supports_fd, "test needs fd support in os.utime") |
---|
249 | n/a | def test_utime_with_fd(self): |
---|
250 | n/a | now = time.time() |
---|
251 | n/a | fd = os.open(support.TESTFN, os.O_RDONLY) |
---|
252 | n/a | try: |
---|
253 | n/a | posix.utime(fd) |
---|
254 | n/a | posix.utime(fd, None) |
---|
255 | n/a | self.assertRaises(TypeError, posix.utime, fd, (None, None)) |
---|
256 | n/a | self.assertRaises(TypeError, posix.utime, fd, (now, None)) |
---|
257 | n/a | self.assertRaises(TypeError, posix.utime, fd, (None, now)) |
---|
258 | n/a | posix.utime(fd, (int(now), int(now))) |
---|
259 | n/a | posix.utime(fd, (now, now)) |
---|
260 | n/a | self.assertRaises(ValueError, posix.utime, fd, (now, now), ns=(now, now)) |
---|
261 | n/a | self.assertRaises(ValueError, posix.utime, fd, (now, 0), ns=(None, None)) |
---|
262 | n/a | self.assertRaises(ValueError, posix.utime, fd, (None, None), ns=(now, 0)) |
---|
263 | n/a | posix.utime(fd, (int(now), int((now - int(now)) * 1e9))) |
---|
264 | n/a | posix.utime(fd, ns=(int(now), int((now - int(now)) * 1e9))) |
---|
265 | n/a | |
---|
266 | n/a | finally: |
---|
267 | n/a | os.close(fd) |
---|
268 | n/a | |
---|
269 | n/a | @unittest.skipUnless(os.utime in os.supports_follow_symlinks, "test needs follow_symlinks support in os.utime") |
---|
270 | n/a | def test_utime_nofollow_symlinks(self): |
---|
271 | n/a | now = time.time() |
---|
272 | n/a | posix.utime(support.TESTFN, None, follow_symlinks=False) |
---|
273 | n/a | self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, None), follow_symlinks=False) |
---|
274 | n/a | self.assertRaises(TypeError, posix.utime, support.TESTFN, (now, None), follow_symlinks=False) |
---|
275 | n/a | self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, now), follow_symlinks=False) |
---|
276 | n/a | posix.utime(support.TESTFN, (int(now), int(now)), follow_symlinks=False) |
---|
277 | n/a | posix.utime(support.TESTFN, (now, now), follow_symlinks=False) |
---|
278 | n/a | posix.utime(support.TESTFN, follow_symlinks=False) |
---|
279 | n/a | |
---|
280 | n/a | @unittest.skipUnless(hasattr(posix, 'writev'), "test needs posix.writev()") |
---|
281 | n/a | def test_writev(self): |
---|
282 | n/a | fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT) |
---|
283 | n/a | try: |
---|
284 | n/a | n = os.writev(fd, (b'test1', b'tt2', b't3')) |
---|
285 | n/a | self.assertEqual(n, 10) |
---|
286 | n/a | |
---|
287 | n/a | os.lseek(fd, 0, os.SEEK_SET) |
---|
288 | n/a | self.assertEqual(b'test1tt2t3', posix.read(fd, 10)) |
---|
289 | n/a | |
---|
290 | n/a | # Issue #20113: empty list of buffers should not crash |
---|
291 | n/a | try: |
---|
292 | n/a | size = posix.writev(fd, []) |
---|
293 | n/a | except OSError: |
---|
294 | n/a | # writev(fd, []) raises OSError(22, "Invalid argument") |
---|
295 | n/a | # on OpenIndiana |
---|
296 | n/a | pass |
---|
297 | n/a | else: |
---|
298 | n/a | self.assertEqual(size, 0) |
---|
299 | n/a | finally: |
---|
300 | n/a | os.close(fd) |
---|
301 | n/a | |
---|
302 | n/a | @unittest.skipUnless(hasattr(posix, 'readv'), "test needs posix.readv()") |
---|
303 | n/a | def test_readv(self): |
---|
304 | n/a | fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT) |
---|
305 | n/a | try: |
---|
306 | n/a | os.write(fd, b'test1tt2t3') |
---|
307 | n/a | os.lseek(fd, 0, os.SEEK_SET) |
---|
308 | n/a | buf = [bytearray(i) for i in [5, 3, 2]] |
---|
309 | n/a | self.assertEqual(posix.readv(fd, buf), 10) |
---|
310 | n/a | self.assertEqual([b'test1', b'tt2', b't3'], [bytes(i) for i in buf]) |
---|
311 | n/a | |
---|
312 | n/a | # Issue #20113: empty list of buffers should not crash |
---|
313 | n/a | try: |
---|
314 | n/a | size = posix.readv(fd, []) |
---|
315 | n/a | except OSError: |
---|
316 | n/a | # readv(fd, []) raises OSError(22, "Invalid argument") |
---|
317 | n/a | # on OpenIndiana |
---|
318 | n/a | pass |
---|
319 | n/a | else: |
---|
320 | n/a | self.assertEqual(size, 0) |
---|
321 | n/a | finally: |
---|
322 | n/a | os.close(fd) |
---|
323 | n/a | |
---|
324 | n/a | @unittest.skipUnless(hasattr(posix, 'dup'), |
---|
325 | n/a | 'test needs posix.dup()') |
---|
326 | n/a | def test_dup(self): |
---|
327 | n/a | fp = open(support.TESTFN) |
---|
328 | n/a | try: |
---|
329 | n/a | fd = posix.dup(fp.fileno()) |
---|
330 | n/a | self.assertIsInstance(fd, int) |
---|
331 | n/a | os.close(fd) |
---|
332 | n/a | finally: |
---|
333 | n/a | fp.close() |
---|
334 | n/a | |
---|
335 | n/a | @unittest.skipUnless(hasattr(posix, 'confstr'), |
---|
336 | n/a | 'test needs posix.confstr()') |
---|
337 | n/a | def test_confstr(self): |
---|
338 | n/a | self.assertRaises(ValueError, posix.confstr, "CS_garbage") |
---|
339 | n/a | self.assertEqual(len(posix.confstr("CS_PATH")) > 0, True) |
---|
340 | n/a | |
---|
341 | n/a | @unittest.skipUnless(hasattr(posix, 'dup2'), |
---|
342 | n/a | 'test needs posix.dup2()') |
---|
343 | n/a | def test_dup2(self): |
---|
344 | n/a | fp1 = open(support.TESTFN) |
---|
345 | n/a | fp2 = open(support.TESTFN) |
---|
346 | n/a | try: |
---|
347 | n/a | posix.dup2(fp1.fileno(), fp2.fileno()) |
---|
348 | n/a | finally: |
---|
349 | n/a | fp1.close() |
---|
350 | n/a | fp2.close() |
---|
351 | n/a | |
---|
352 | n/a | @unittest.skipUnless(hasattr(os, 'O_CLOEXEC'), "needs os.O_CLOEXEC") |
---|
353 | n/a | @support.requires_linux_version(2, 6, 23) |
---|
354 | n/a | def test_oscloexec(self): |
---|
355 | n/a | fd = os.open(support.TESTFN, os.O_RDONLY|os.O_CLOEXEC) |
---|
356 | n/a | self.addCleanup(os.close, fd) |
---|
357 | n/a | self.assertFalse(os.get_inheritable(fd)) |
---|
358 | n/a | |
---|
359 | n/a | @unittest.skipUnless(hasattr(posix, 'O_EXLOCK'), |
---|
360 | n/a | 'test needs posix.O_EXLOCK') |
---|
361 | n/a | def test_osexlock(self): |
---|
362 | n/a | fd = os.open(support.TESTFN, |
---|
363 | n/a | os.O_WRONLY|os.O_EXLOCK|os.O_CREAT) |
---|
364 | n/a | self.assertRaises(OSError, os.open, support.TESTFN, |
---|
365 | n/a | os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK) |
---|
366 | n/a | os.close(fd) |
---|
367 | n/a | |
---|
368 | n/a | if hasattr(posix, "O_SHLOCK"): |
---|
369 | n/a | fd = os.open(support.TESTFN, |
---|
370 | n/a | os.O_WRONLY|os.O_SHLOCK|os.O_CREAT) |
---|
371 | n/a | self.assertRaises(OSError, os.open, support.TESTFN, |
---|
372 | n/a | os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK) |
---|
373 | n/a | os.close(fd) |
---|
374 | n/a | |
---|
375 | n/a | @unittest.skipUnless(hasattr(posix, 'O_SHLOCK'), |
---|
376 | n/a | 'test needs posix.O_SHLOCK') |
---|
377 | n/a | def test_osshlock(self): |
---|
378 | n/a | fd1 = os.open(support.TESTFN, |
---|
379 | n/a | os.O_WRONLY|os.O_SHLOCK|os.O_CREAT) |
---|
380 | n/a | fd2 = os.open(support.TESTFN, |
---|
381 | n/a | os.O_WRONLY|os.O_SHLOCK|os.O_CREAT) |
---|
382 | n/a | os.close(fd2) |
---|
383 | n/a | os.close(fd1) |
---|
384 | n/a | |
---|
385 | n/a | if hasattr(posix, "O_EXLOCK"): |
---|
386 | n/a | fd = os.open(support.TESTFN, |
---|
387 | n/a | os.O_WRONLY|os.O_SHLOCK|os.O_CREAT) |
---|
388 | n/a | self.assertRaises(OSError, os.open, support.TESTFN, |
---|
389 | n/a | os.O_RDONLY|os.O_EXLOCK|os.O_NONBLOCK) |
---|
390 | n/a | os.close(fd) |
---|
391 | n/a | |
---|
392 | n/a | @unittest.skipUnless(hasattr(posix, 'fstat'), |
---|
393 | n/a | 'test needs posix.fstat()') |
---|
394 | n/a | def test_fstat(self): |
---|
395 | n/a | fp = open(support.TESTFN) |
---|
396 | n/a | try: |
---|
397 | n/a | self.assertTrue(posix.fstat(fp.fileno())) |
---|
398 | n/a | self.assertTrue(posix.stat(fp.fileno())) |
---|
399 | n/a | |
---|
400 | n/a | self.assertRaisesRegex(TypeError, |
---|
401 | n/a | 'should be string, bytes, os.PathLike or integer, not', |
---|
402 | n/a | posix.stat, float(fp.fileno())) |
---|
403 | n/a | finally: |
---|
404 | n/a | fp.close() |
---|
405 | n/a | |
---|
406 | n/a | @unittest.skipUnless(hasattr(posix, 'stat'), |
---|
407 | n/a | 'test needs posix.stat()') |
---|
408 | n/a | def test_stat(self): |
---|
409 | n/a | self.assertTrue(posix.stat(support.TESTFN)) |
---|
410 | n/a | self.assertTrue(posix.stat(os.fsencode(support.TESTFN))) |
---|
411 | n/a | |
---|
412 | n/a | self.assertWarnsRegex(DeprecationWarning, |
---|
413 | n/a | 'should be string, bytes, os.PathLike or integer, not', |
---|
414 | n/a | posix.stat, bytearray(os.fsencode(support.TESTFN))) |
---|
415 | n/a | self.assertRaisesRegex(TypeError, |
---|
416 | n/a | 'should be string, bytes, os.PathLike or integer, not', |
---|
417 | n/a | posix.stat, None) |
---|
418 | n/a | self.assertRaisesRegex(TypeError, |
---|
419 | n/a | 'should be string, bytes, os.PathLike or integer, not', |
---|
420 | n/a | posix.stat, list(support.TESTFN)) |
---|
421 | n/a | self.assertRaisesRegex(TypeError, |
---|
422 | n/a | 'should be string, bytes, os.PathLike or integer, not', |
---|
423 | n/a | posix.stat, list(os.fsencode(support.TESTFN))) |
---|
424 | n/a | |
---|
425 | n/a | @unittest.skipUnless(hasattr(posix, 'mkfifo'), "don't have mkfifo()") |
---|
426 | n/a | @unittest.skipIf(android_not_root, "mkfifo not allowed, non root user") |
---|
427 | n/a | def test_mkfifo(self): |
---|
428 | n/a | support.unlink(support.TESTFN) |
---|
429 | n/a | posix.mkfifo(support.TESTFN, stat.S_IRUSR | stat.S_IWUSR) |
---|
430 | n/a | self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode)) |
---|
431 | n/a | |
---|
432 | n/a | @unittest.skipUnless(hasattr(posix, 'mknod') and hasattr(stat, 'S_IFIFO'), |
---|
433 | n/a | "don't have mknod()/S_IFIFO") |
---|
434 | n/a | @unittest.skipIf(android_not_root, "mknod not allowed, non root user") |
---|
435 | n/a | def test_mknod(self): |
---|
436 | n/a | # Test using mknod() to create a FIFO (the only use specified |
---|
437 | n/a | # by POSIX). |
---|
438 | n/a | support.unlink(support.TESTFN) |
---|
439 | n/a | mode = stat.S_IFIFO | stat.S_IRUSR | stat.S_IWUSR |
---|
440 | n/a | try: |
---|
441 | n/a | posix.mknod(support.TESTFN, mode, 0) |
---|
442 | n/a | except OSError as e: |
---|
443 | n/a | # Some old systems don't allow unprivileged users to use |
---|
444 | n/a | # mknod(), or only support creating device nodes. |
---|
445 | n/a | self.assertIn(e.errno, (errno.EPERM, errno.EINVAL)) |
---|
446 | n/a | else: |
---|
447 | n/a | self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode)) |
---|
448 | n/a | |
---|
449 | n/a | # Keyword arguments are also supported |
---|
450 | n/a | support.unlink(support.TESTFN) |
---|
451 | n/a | try: |
---|
452 | n/a | posix.mknod(path=support.TESTFN, mode=mode, device=0, |
---|
453 | n/a | dir_fd=None) |
---|
454 | n/a | except OSError as e: |
---|
455 | n/a | self.assertIn(e.errno, (errno.EPERM, errno.EINVAL)) |
---|
456 | n/a | |
---|
457 | n/a | @unittest.skipUnless(hasattr(posix, 'stat'), 'test needs posix.stat()') |
---|
458 | n/a | @unittest.skipUnless(hasattr(posix, 'makedev'), 'test needs posix.makedev()') |
---|
459 | n/a | def test_makedev(self): |
---|
460 | n/a | st = posix.stat(support.TESTFN) |
---|
461 | n/a | dev = st.st_dev |
---|
462 | n/a | self.assertIsInstance(dev, int) |
---|
463 | n/a | self.assertGreaterEqual(dev, 0) |
---|
464 | n/a | |
---|
465 | n/a | major = posix.major(dev) |
---|
466 | n/a | self.assertIsInstance(major, int) |
---|
467 | n/a | self.assertGreaterEqual(major, 0) |
---|
468 | n/a | self.assertEqual(posix.major(dev), major) |
---|
469 | n/a | self.assertRaises(TypeError, posix.major, float(dev)) |
---|
470 | n/a | self.assertRaises(TypeError, posix.major) |
---|
471 | n/a | self.assertRaises((ValueError, OverflowError), posix.major, -1) |
---|
472 | n/a | |
---|
473 | n/a | minor = posix.minor(dev) |
---|
474 | n/a | self.assertIsInstance(minor, int) |
---|
475 | n/a | self.assertGreaterEqual(minor, 0) |
---|
476 | n/a | self.assertEqual(posix.minor(dev), minor) |
---|
477 | n/a | self.assertRaises(TypeError, posix.minor, float(dev)) |
---|
478 | n/a | self.assertRaises(TypeError, posix.minor) |
---|
479 | n/a | self.assertRaises((ValueError, OverflowError), posix.minor, -1) |
---|
480 | n/a | |
---|
481 | n/a | self.assertEqual(posix.makedev(major, minor), dev) |
---|
482 | n/a | self.assertRaises(TypeError, posix.makedev, float(major), minor) |
---|
483 | n/a | self.assertRaises(TypeError, posix.makedev, major, float(minor)) |
---|
484 | n/a | self.assertRaises(TypeError, posix.makedev, major) |
---|
485 | n/a | self.assertRaises(TypeError, posix.makedev) |
---|
486 | n/a | |
---|
487 | n/a | def _test_all_chown_common(self, chown_func, first_param, stat_func): |
---|
488 | n/a | """Common code for chown, fchown and lchown tests.""" |
---|
489 | n/a | def check_stat(uid, gid): |
---|
490 | n/a | if stat_func is not None: |
---|
491 | n/a | stat = stat_func(first_param) |
---|
492 | n/a | self.assertEqual(stat.st_uid, uid) |
---|
493 | n/a | self.assertEqual(stat.st_gid, gid) |
---|
494 | n/a | uid = os.getuid() |
---|
495 | n/a | gid = os.getgid() |
---|
496 | n/a | # test a successful chown call |
---|
497 | n/a | chown_func(first_param, uid, gid) |
---|
498 | n/a | check_stat(uid, gid) |
---|
499 | n/a | chown_func(first_param, -1, gid) |
---|
500 | n/a | check_stat(uid, gid) |
---|
501 | n/a | chown_func(first_param, uid, -1) |
---|
502 | n/a | check_stat(uid, gid) |
---|
503 | n/a | |
---|
504 | n/a | if uid == 0: |
---|
505 | n/a | # Try an amusingly large uid/gid to make sure we handle |
---|
506 | n/a | # large unsigned values. (chown lets you use any |
---|
507 | n/a | # uid/gid you like, even if they aren't defined.) |
---|
508 | n/a | # |
---|
509 | n/a | # This problem keeps coming up: |
---|
510 | n/a | # http://bugs.python.org/issue1747858 |
---|
511 | n/a | # http://bugs.python.org/issue4591 |
---|
512 | n/a | # http://bugs.python.org/issue15301 |
---|
513 | n/a | # Hopefully the fix in 4591 fixes it for good! |
---|
514 | n/a | # |
---|
515 | n/a | # This part of the test only runs when run as root. |
---|
516 | n/a | # Only scary people run their tests as root. |
---|
517 | n/a | |
---|
518 | n/a | big_value = 2**31 |
---|
519 | n/a | chown_func(first_param, big_value, big_value) |
---|
520 | n/a | check_stat(big_value, big_value) |
---|
521 | n/a | chown_func(first_param, -1, -1) |
---|
522 | n/a | check_stat(big_value, big_value) |
---|
523 | n/a | chown_func(first_param, uid, gid) |
---|
524 | n/a | check_stat(uid, gid) |
---|
525 | n/a | elif platform.system() in ('HP-UX', 'SunOS'): |
---|
526 | n/a | # HP-UX and Solaris can allow a non-root user to chown() to root |
---|
527 | n/a | # (issue #5113) |
---|
528 | n/a | raise unittest.SkipTest("Skipping because of non-standard chown() " |
---|
529 | n/a | "behavior") |
---|
530 | n/a | else: |
---|
531 | n/a | # non-root cannot chown to root, raises OSError |
---|
532 | n/a | self.assertRaises(OSError, chown_func, first_param, 0, 0) |
---|
533 | n/a | check_stat(uid, gid) |
---|
534 | n/a | self.assertRaises(OSError, chown_func, first_param, 0, -1) |
---|
535 | n/a | check_stat(uid, gid) |
---|
536 | n/a | if 0 not in os.getgroups(): |
---|
537 | n/a | self.assertRaises(OSError, chown_func, first_param, -1, 0) |
---|
538 | n/a | check_stat(uid, gid) |
---|
539 | n/a | # test illegal types |
---|
540 | n/a | for t in str, float: |
---|
541 | n/a | self.assertRaises(TypeError, chown_func, first_param, t(uid), gid) |
---|
542 | n/a | check_stat(uid, gid) |
---|
543 | n/a | self.assertRaises(TypeError, chown_func, first_param, uid, t(gid)) |
---|
544 | n/a | check_stat(uid, gid) |
---|
545 | n/a | |
---|
546 | n/a | @unittest.skipUnless(hasattr(posix, 'chown'), "test needs os.chown()") |
---|
547 | n/a | def test_chown(self): |
---|
548 | n/a | # raise an OSError if the file does not exist |
---|
549 | n/a | os.unlink(support.TESTFN) |
---|
550 | n/a | self.assertRaises(OSError, posix.chown, support.TESTFN, -1, -1) |
---|
551 | n/a | |
---|
552 | n/a | # re-create the file |
---|
553 | n/a | support.create_empty_file(support.TESTFN) |
---|
554 | n/a | self._test_all_chown_common(posix.chown, support.TESTFN, |
---|
555 | n/a | getattr(posix, 'stat', None)) |
---|
556 | n/a | |
---|
557 | n/a | @unittest.skipUnless(hasattr(posix, 'fchown'), "test needs os.fchown()") |
---|
558 | n/a | def test_fchown(self): |
---|
559 | n/a | os.unlink(support.TESTFN) |
---|
560 | n/a | |
---|
561 | n/a | # re-create the file |
---|
562 | n/a | test_file = open(support.TESTFN, 'w') |
---|
563 | n/a | try: |
---|
564 | n/a | fd = test_file.fileno() |
---|
565 | n/a | self._test_all_chown_common(posix.fchown, fd, |
---|
566 | n/a | getattr(posix, 'fstat', None)) |
---|
567 | n/a | finally: |
---|
568 | n/a | test_file.close() |
---|
569 | n/a | |
---|
570 | n/a | @unittest.skipUnless(hasattr(posix, 'lchown'), "test needs os.lchown()") |
---|
571 | n/a | def test_lchown(self): |
---|
572 | n/a | os.unlink(support.TESTFN) |
---|
573 | n/a | # create a symlink |
---|
574 | n/a | os.symlink(_DUMMY_SYMLINK, support.TESTFN) |
---|
575 | n/a | self._test_all_chown_common(posix.lchown, support.TESTFN, |
---|
576 | n/a | getattr(posix, 'lstat', None)) |
---|
577 | n/a | |
---|
578 | n/a | @unittest.skipUnless(hasattr(posix, 'chdir'), 'test needs posix.chdir()') |
---|
579 | n/a | def test_chdir(self): |
---|
580 | n/a | posix.chdir(os.curdir) |
---|
581 | n/a | self.assertRaises(OSError, posix.chdir, support.TESTFN) |
---|
582 | n/a | |
---|
583 | n/a | def test_listdir(self): |
---|
584 | n/a | self.assertTrue(support.TESTFN in posix.listdir(os.curdir)) |
---|
585 | n/a | |
---|
586 | n/a | def test_listdir_default(self): |
---|
587 | n/a | # When listdir is called without argument, |
---|
588 | n/a | # it's the same as listdir(os.curdir). |
---|
589 | n/a | self.assertTrue(support.TESTFN in posix.listdir()) |
---|
590 | n/a | |
---|
591 | n/a | def test_listdir_bytes(self): |
---|
592 | n/a | # When listdir is called with a bytes object, |
---|
593 | n/a | # the returned strings are of type bytes. |
---|
594 | n/a | self.assertTrue(os.fsencode(support.TESTFN) in posix.listdir(b'.')) |
---|
595 | n/a | |
---|
596 | n/a | @unittest.skipUnless(posix.listdir in os.supports_fd, |
---|
597 | n/a | "test needs fd support for posix.listdir()") |
---|
598 | n/a | def test_listdir_fd(self): |
---|
599 | n/a | f = posix.open(posix.getcwd(), posix.O_RDONLY) |
---|
600 | n/a | self.addCleanup(posix.close, f) |
---|
601 | n/a | self.assertEqual( |
---|
602 | n/a | sorted(posix.listdir('.')), |
---|
603 | n/a | sorted(posix.listdir(f)) |
---|
604 | n/a | ) |
---|
605 | n/a | # Check that the fd offset was reset (issue #13739) |
---|
606 | n/a | self.assertEqual( |
---|
607 | n/a | sorted(posix.listdir('.')), |
---|
608 | n/a | sorted(posix.listdir(f)) |
---|
609 | n/a | ) |
---|
610 | n/a | |
---|
611 | n/a | @unittest.skipUnless(hasattr(posix, 'access'), 'test needs posix.access()') |
---|
612 | n/a | def test_access(self): |
---|
613 | n/a | self.assertTrue(posix.access(support.TESTFN, os.R_OK)) |
---|
614 | n/a | |
---|
615 | n/a | @unittest.skipUnless(hasattr(posix, 'umask'), 'test needs posix.umask()') |
---|
616 | n/a | def test_umask(self): |
---|
617 | n/a | old_mask = posix.umask(0) |
---|
618 | n/a | self.assertIsInstance(old_mask, int) |
---|
619 | n/a | posix.umask(old_mask) |
---|
620 | n/a | |
---|
621 | n/a | @unittest.skipUnless(hasattr(posix, 'strerror'), |
---|
622 | n/a | 'test needs posix.strerror()') |
---|
623 | n/a | def test_strerror(self): |
---|
624 | n/a | self.assertTrue(posix.strerror(0)) |
---|
625 | n/a | |
---|
626 | n/a | @unittest.skipUnless(hasattr(posix, 'pipe'), 'test needs posix.pipe()') |
---|
627 | n/a | def test_pipe(self): |
---|
628 | n/a | reader, writer = posix.pipe() |
---|
629 | n/a | os.close(reader) |
---|
630 | n/a | os.close(writer) |
---|
631 | n/a | |
---|
632 | n/a | @unittest.skipUnless(hasattr(os, 'pipe2'), "test needs os.pipe2()") |
---|
633 | n/a | @support.requires_linux_version(2, 6, 27) |
---|
634 | n/a | def test_pipe2(self): |
---|
635 | n/a | self.assertRaises(TypeError, os.pipe2, 'DEADBEEF') |
---|
636 | n/a | self.assertRaises(TypeError, os.pipe2, 0, 0) |
---|
637 | n/a | |
---|
638 | n/a | # try calling with flags = 0, like os.pipe() |
---|
639 | n/a | r, w = os.pipe2(0) |
---|
640 | n/a | os.close(r) |
---|
641 | n/a | os.close(w) |
---|
642 | n/a | |
---|
643 | n/a | # test flags |
---|
644 | n/a | r, w = os.pipe2(os.O_CLOEXEC|os.O_NONBLOCK) |
---|
645 | n/a | self.addCleanup(os.close, r) |
---|
646 | n/a | self.addCleanup(os.close, w) |
---|
647 | n/a | self.assertFalse(os.get_inheritable(r)) |
---|
648 | n/a | self.assertFalse(os.get_inheritable(w)) |
---|
649 | n/a | self.assertFalse(os.get_blocking(r)) |
---|
650 | n/a | self.assertFalse(os.get_blocking(w)) |
---|
651 | n/a | # try reading from an empty pipe: this should fail, not block |
---|
652 | n/a | self.assertRaises(OSError, os.read, r, 1) |
---|
653 | n/a | # try a write big enough to fill-up the pipe: this should either |
---|
654 | n/a | # fail or perform a partial write, not block |
---|
655 | n/a | try: |
---|
656 | n/a | os.write(w, b'x' * support.PIPE_MAX_SIZE) |
---|
657 | n/a | except OSError: |
---|
658 | n/a | pass |
---|
659 | n/a | |
---|
660 | n/a | @support.cpython_only |
---|
661 | n/a | @unittest.skipUnless(hasattr(os, 'pipe2'), "test needs os.pipe2()") |
---|
662 | n/a | @support.requires_linux_version(2, 6, 27) |
---|
663 | n/a | def test_pipe2_c_limits(self): |
---|
664 | n/a | # Issue 15989 |
---|
665 | n/a | import _testcapi |
---|
666 | n/a | self.assertRaises(OverflowError, os.pipe2, _testcapi.INT_MAX + 1) |
---|
667 | n/a | self.assertRaises(OverflowError, os.pipe2, _testcapi.UINT_MAX + 1) |
---|
668 | n/a | |
---|
669 | n/a | @unittest.skipUnless(hasattr(posix, 'utime'), 'test needs posix.utime()') |
---|
670 | n/a | def test_utime(self): |
---|
671 | n/a | now = time.time() |
---|
672 | n/a | posix.utime(support.TESTFN, None) |
---|
673 | n/a | self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, None)) |
---|
674 | n/a | self.assertRaises(TypeError, posix.utime, support.TESTFN, (now, None)) |
---|
675 | n/a | self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, now)) |
---|
676 | n/a | posix.utime(support.TESTFN, (int(now), int(now))) |
---|
677 | n/a | posix.utime(support.TESTFN, (now, now)) |
---|
678 | n/a | |
---|
679 | n/a | def _test_chflags_regular_file(self, chflags_func, target_file, **kwargs): |
---|
680 | n/a | st = os.stat(target_file) |
---|
681 | n/a | self.assertTrue(hasattr(st, 'st_flags')) |
---|
682 | n/a | |
---|
683 | n/a | # ZFS returns EOPNOTSUPP when attempting to set flag UF_IMMUTABLE. |
---|
684 | n/a | flags = st.st_flags | stat.UF_IMMUTABLE |
---|
685 | n/a | try: |
---|
686 | n/a | chflags_func(target_file, flags, **kwargs) |
---|
687 | n/a | except OSError as err: |
---|
688 | n/a | if err.errno != errno.EOPNOTSUPP: |
---|
689 | n/a | raise |
---|
690 | n/a | msg = 'chflag UF_IMMUTABLE not supported by underlying fs' |
---|
691 | n/a | self.skipTest(msg) |
---|
692 | n/a | |
---|
693 | n/a | try: |
---|
694 | n/a | new_st = os.stat(target_file) |
---|
695 | n/a | self.assertEqual(st.st_flags | stat.UF_IMMUTABLE, new_st.st_flags) |
---|
696 | n/a | try: |
---|
697 | n/a | fd = open(target_file, 'w+') |
---|
698 | n/a | except OSError as e: |
---|
699 | n/a | self.assertEqual(e.errno, errno.EPERM) |
---|
700 | n/a | finally: |
---|
701 | n/a | posix.chflags(target_file, st.st_flags) |
---|
702 | n/a | |
---|
703 | n/a | @unittest.skipUnless(hasattr(posix, 'chflags'), 'test needs os.chflags()') |
---|
704 | n/a | def test_chflags(self): |
---|
705 | n/a | self._test_chflags_regular_file(posix.chflags, support.TESTFN) |
---|
706 | n/a | |
---|
707 | n/a | @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()') |
---|
708 | n/a | def test_lchflags_regular_file(self): |
---|
709 | n/a | self._test_chflags_regular_file(posix.lchflags, support.TESTFN) |
---|
710 | n/a | self._test_chflags_regular_file(posix.chflags, support.TESTFN, follow_symlinks=False) |
---|
711 | n/a | |
---|
712 | n/a | @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()') |
---|
713 | n/a | def test_lchflags_symlink(self): |
---|
714 | n/a | testfn_st = os.stat(support.TESTFN) |
---|
715 | n/a | |
---|
716 | n/a | self.assertTrue(hasattr(testfn_st, 'st_flags')) |
---|
717 | n/a | |
---|
718 | n/a | os.symlink(support.TESTFN, _DUMMY_SYMLINK) |
---|
719 | n/a | self.teardown_files.append(_DUMMY_SYMLINK) |
---|
720 | n/a | dummy_symlink_st = os.lstat(_DUMMY_SYMLINK) |
---|
721 | n/a | |
---|
722 | n/a | def chflags_nofollow(path, flags): |
---|
723 | n/a | return posix.chflags(path, flags, follow_symlinks=False) |
---|
724 | n/a | |
---|
725 | n/a | for fn in (posix.lchflags, chflags_nofollow): |
---|
726 | n/a | # ZFS returns EOPNOTSUPP when attempting to set flag UF_IMMUTABLE. |
---|
727 | n/a | flags = dummy_symlink_st.st_flags | stat.UF_IMMUTABLE |
---|
728 | n/a | try: |
---|
729 | n/a | fn(_DUMMY_SYMLINK, flags) |
---|
730 | n/a | except OSError as err: |
---|
731 | n/a | if err.errno != errno.EOPNOTSUPP: |
---|
732 | n/a | raise |
---|
733 | n/a | msg = 'chflag UF_IMMUTABLE not supported by underlying fs' |
---|
734 | n/a | self.skipTest(msg) |
---|
735 | n/a | try: |
---|
736 | n/a | new_testfn_st = os.stat(support.TESTFN) |
---|
737 | n/a | new_dummy_symlink_st = os.lstat(_DUMMY_SYMLINK) |
---|
738 | n/a | |
---|
739 | n/a | self.assertEqual(testfn_st.st_flags, new_testfn_st.st_flags) |
---|
740 | n/a | self.assertEqual(dummy_symlink_st.st_flags | stat.UF_IMMUTABLE, |
---|
741 | n/a | new_dummy_symlink_st.st_flags) |
---|
742 | n/a | finally: |
---|
743 | n/a | fn(_DUMMY_SYMLINK, dummy_symlink_st.st_flags) |
---|
744 | n/a | |
---|
745 | n/a | def test_environ(self): |
---|
746 | n/a | if os.name == "nt": |
---|
747 | n/a | item_type = str |
---|
748 | n/a | else: |
---|
749 | n/a | item_type = bytes |
---|
750 | n/a | for k, v in posix.environ.items(): |
---|
751 | n/a | self.assertEqual(type(k), item_type) |
---|
752 | n/a | self.assertEqual(type(v), item_type) |
---|
753 | n/a | |
---|
754 | n/a | @unittest.skipUnless(hasattr(posix, 'getcwd'), 'test needs posix.getcwd()') |
---|
755 | n/a | def test_getcwd_long_pathnames(self): |
---|
756 | n/a | dirname = 'getcwd-test-directory-0123456789abcdef-01234567890abcdef' |
---|
757 | n/a | curdir = os.getcwd() |
---|
758 | n/a | base_path = os.path.abspath(support.TESTFN) + '.getcwd' |
---|
759 | n/a | |
---|
760 | n/a | try: |
---|
761 | n/a | os.mkdir(base_path) |
---|
762 | n/a | os.chdir(base_path) |
---|
763 | n/a | except: |
---|
764 | n/a | # Just returning nothing instead of the SkipTest exception, because |
---|
765 | n/a | # the test results in Error in that case. Is that ok? |
---|
766 | n/a | # raise unittest.SkipTest("cannot create directory for testing") |
---|
767 | n/a | return |
---|
768 | n/a | |
---|
769 | n/a | def _create_and_do_getcwd(dirname, current_path_length = 0): |
---|
770 | n/a | try: |
---|
771 | n/a | os.mkdir(dirname) |
---|
772 | n/a | except: |
---|
773 | n/a | raise unittest.SkipTest("mkdir cannot create directory sufficiently deep for getcwd test") |
---|
774 | n/a | |
---|
775 | n/a | os.chdir(dirname) |
---|
776 | n/a | try: |
---|
777 | n/a | os.getcwd() |
---|
778 | n/a | if current_path_length < 1027: |
---|
779 | n/a | _create_and_do_getcwd(dirname, current_path_length + len(dirname) + 1) |
---|
780 | n/a | finally: |
---|
781 | n/a | os.chdir('..') |
---|
782 | n/a | os.rmdir(dirname) |
---|
783 | n/a | |
---|
784 | n/a | _create_and_do_getcwd(dirname) |
---|
785 | n/a | |
---|
786 | n/a | finally: |
---|
787 | n/a | os.chdir(curdir) |
---|
788 | n/a | support.rmtree(base_path) |
---|
789 | n/a | |
---|
790 | n/a | @unittest.skipUnless(hasattr(posix, 'getgrouplist'), "test needs posix.getgrouplist()") |
---|
791 | n/a | @unittest.skipUnless(hasattr(pwd, 'getpwuid'), "test needs pwd.getpwuid()") |
---|
792 | n/a | @unittest.skipUnless(hasattr(os, 'getuid'), "test needs os.getuid()") |
---|
793 | n/a | def test_getgrouplist(self): |
---|
794 | n/a | user = pwd.getpwuid(os.getuid())[0] |
---|
795 | n/a | group = pwd.getpwuid(os.getuid())[3] |
---|
796 | n/a | self.assertIn(group, posix.getgrouplist(user, group)) |
---|
797 | n/a | |
---|
798 | n/a | |
---|
799 | n/a | @unittest.skipUnless(hasattr(os, 'getegid'), "test needs os.getegid()") |
---|
800 | n/a | def test_getgroups(self): |
---|
801 | n/a | with os.popen('id -G 2>/dev/null') as idg: |
---|
802 | n/a | groups = idg.read().strip() |
---|
803 | n/a | ret = idg.close() |
---|
804 | n/a | |
---|
805 | n/a | try: |
---|
806 | n/a | idg_groups = set(int(g) for g in groups.split()) |
---|
807 | n/a | except ValueError: |
---|
808 | n/a | idg_groups = set() |
---|
809 | n/a | if ret is not None or not idg_groups: |
---|
810 | n/a | raise unittest.SkipTest("need working 'id -G'") |
---|
811 | n/a | |
---|
812 | n/a | # Issues 16698: OS X ABIs prior to 10.6 have limits on getgroups() |
---|
813 | n/a | if sys.platform == 'darwin': |
---|
814 | n/a | import sysconfig |
---|
815 | n/a | dt = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET') or '10.0' |
---|
816 | n/a | if tuple(int(n) for n in dt.split('.')[0:2]) < (10, 6): |
---|
817 | n/a | raise unittest.SkipTest("getgroups(2) is broken prior to 10.6") |
---|
818 | n/a | |
---|
819 | n/a | # 'id -G' and 'os.getgroups()' should return the same |
---|
820 | n/a | # groups, ignoring order, duplicates, and the effective gid. |
---|
821 | n/a | # #10822/#26944 - It is implementation defined whether |
---|
822 | n/a | # posix.getgroups() includes the effective gid. |
---|
823 | n/a | symdiff = idg_groups.symmetric_difference(posix.getgroups()) |
---|
824 | n/a | self.assertTrue(not symdiff or symdiff == {posix.getegid()}) |
---|
825 | n/a | |
---|
826 | n/a | # tests for the posix *at functions follow |
---|
827 | n/a | |
---|
828 | n/a | @unittest.skipUnless(os.access in os.supports_dir_fd, "test needs dir_fd support for os.access()") |
---|
829 | n/a | def test_access_dir_fd(self): |
---|
830 | n/a | f = posix.open(posix.getcwd(), posix.O_RDONLY) |
---|
831 | n/a | try: |
---|
832 | n/a | self.assertTrue(posix.access(support.TESTFN, os.R_OK, dir_fd=f)) |
---|
833 | n/a | finally: |
---|
834 | n/a | posix.close(f) |
---|
835 | n/a | |
---|
836 | n/a | @unittest.skipUnless(os.chmod in os.supports_dir_fd, "test needs dir_fd support in os.chmod()") |
---|
837 | n/a | def test_chmod_dir_fd(self): |
---|
838 | n/a | os.chmod(support.TESTFN, stat.S_IRUSR) |
---|
839 | n/a | |
---|
840 | n/a | f = posix.open(posix.getcwd(), posix.O_RDONLY) |
---|
841 | n/a | try: |
---|
842 | n/a | posix.chmod(support.TESTFN, stat.S_IRUSR | stat.S_IWUSR, dir_fd=f) |
---|
843 | n/a | |
---|
844 | n/a | s = posix.stat(support.TESTFN) |
---|
845 | n/a | self.assertEqual(s[0] & stat.S_IRWXU, stat.S_IRUSR | stat.S_IWUSR) |
---|
846 | n/a | finally: |
---|
847 | n/a | posix.close(f) |
---|
848 | n/a | |
---|
849 | n/a | @unittest.skipUnless(os.chown in os.supports_dir_fd, "test needs dir_fd support in os.chown()") |
---|
850 | n/a | def test_chown_dir_fd(self): |
---|
851 | n/a | support.unlink(support.TESTFN) |
---|
852 | n/a | support.create_empty_file(support.TESTFN) |
---|
853 | n/a | |
---|
854 | n/a | f = posix.open(posix.getcwd(), posix.O_RDONLY) |
---|
855 | n/a | try: |
---|
856 | n/a | posix.chown(support.TESTFN, os.getuid(), os.getgid(), dir_fd=f) |
---|
857 | n/a | finally: |
---|
858 | n/a | posix.close(f) |
---|
859 | n/a | |
---|
860 | n/a | @unittest.skipUnless(os.stat in os.supports_dir_fd, "test needs dir_fd support in os.stat()") |
---|
861 | n/a | def test_stat_dir_fd(self): |
---|
862 | n/a | support.unlink(support.TESTFN) |
---|
863 | n/a | with open(support.TESTFN, 'w') as outfile: |
---|
864 | n/a | outfile.write("testline\n") |
---|
865 | n/a | |
---|
866 | n/a | f = posix.open(posix.getcwd(), posix.O_RDONLY) |
---|
867 | n/a | try: |
---|
868 | n/a | s1 = posix.stat(support.TESTFN) |
---|
869 | n/a | s2 = posix.stat(support.TESTFN, dir_fd=f) |
---|
870 | n/a | self.assertEqual(s1, s2) |
---|
871 | n/a | s2 = posix.stat(support.TESTFN, dir_fd=None) |
---|
872 | n/a | self.assertEqual(s1, s2) |
---|
873 | n/a | self.assertRaisesRegex(TypeError, 'should be integer or None, not', |
---|
874 | n/a | posix.stat, support.TESTFN, dir_fd=posix.getcwd()) |
---|
875 | n/a | self.assertRaisesRegex(TypeError, 'should be integer or None, not', |
---|
876 | n/a | posix.stat, support.TESTFN, dir_fd=float(f)) |
---|
877 | n/a | self.assertRaises(OverflowError, |
---|
878 | n/a | posix.stat, support.TESTFN, dir_fd=10**20) |
---|
879 | n/a | finally: |
---|
880 | n/a | posix.close(f) |
---|
881 | n/a | |
---|
882 | n/a | @unittest.skipUnless(os.utime in os.supports_dir_fd, "test needs dir_fd support in os.utime()") |
---|
883 | n/a | def test_utime_dir_fd(self): |
---|
884 | n/a | f = posix.open(posix.getcwd(), posix.O_RDONLY) |
---|
885 | n/a | try: |
---|
886 | n/a | now = time.time() |
---|
887 | n/a | posix.utime(support.TESTFN, None, dir_fd=f) |
---|
888 | n/a | posix.utime(support.TESTFN, dir_fd=f) |
---|
889 | n/a | self.assertRaises(TypeError, posix.utime, support.TESTFN, now, dir_fd=f) |
---|
890 | n/a | self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, None), dir_fd=f) |
---|
891 | n/a | self.assertRaises(TypeError, posix.utime, support.TESTFN, (now, None), dir_fd=f) |
---|
892 | n/a | self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, now), dir_fd=f) |
---|
893 | n/a | self.assertRaises(TypeError, posix.utime, support.TESTFN, (now, "x"), dir_fd=f) |
---|
894 | n/a | posix.utime(support.TESTFN, (int(now), int(now)), dir_fd=f) |
---|
895 | n/a | posix.utime(support.TESTFN, (now, now), dir_fd=f) |
---|
896 | n/a | posix.utime(support.TESTFN, |
---|
897 | n/a | (int(now), int((now - int(now)) * 1e9)), dir_fd=f) |
---|
898 | n/a | posix.utime(support.TESTFN, dir_fd=f, |
---|
899 | n/a | times=(int(now), int((now - int(now)) * 1e9))) |
---|
900 | n/a | |
---|
901 | n/a | # try dir_fd and follow_symlinks together |
---|
902 | n/a | if os.utime in os.supports_follow_symlinks: |
---|
903 | n/a | try: |
---|
904 | n/a | posix.utime(support.TESTFN, follow_symlinks=False, dir_fd=f) |
---|
905 | n/a | except ValueError: |
---|
906 | n/a | # whoops! using both together not supported on this platform. |
---|
907 | n/a | pass |
---|
908 | n/a | |
---|
909 | n/a | finally: |
---|
910 | n/a | posix.close(f) |
---|
911 | n/a | |
---|
912 | n/a | @unittest.skipUnless(os.link in os.supports_dir_fd, "test needs dir_fd support in os.link()") |
---|
913 | n/a | @unittest.skipIf(android_not_root, "hard link not allowed, non root user") |
---|
914 | n/a | def test_link_dir_fd(self): |
---|
915 | n/a | f = posix.open(posix.getcwd(), posix.O_RDONLY) |
---|
916 | n/a | try: |
---|
917 | n/a | posix.link(support.TESTFN, support.TESTFN + 'link', src_dir_fd=f, dst_dir_fd=f) |
---|
918 | n/a | # should have same inodes |
---|
919 | n/a | self.assertEqual(posix.stat(support.TESTFN)[1], |
---|
920 | n/a | posix.stat(support.TESTFN + 'link')[1]) |
---|
921 | n/a | finally: |
---|
922 | n/a | posix.close(f) |
---|
923 | n/a | support.unlink(support.TESTFN + 'link') |
---|
924 | n/a | |
---|
925 | n/a | @unittest.skipUnless(os.mkdir in os.supports_dir_fd, "test needs dir_fd support in os.mkdir()") |
---|
926 | n/a | def test_mkdir_dir_fd(self): |
---|
927 | n/a | f = posix.open(posix.getcwd(), posix.O_RDONLY) |
---|
928 | n/a | try: |
---|
929 | n/a | posix.mkdir(support.TESTFN + 'dir', dir_fd=f) |
---|
930 | n/a | posix.stat(support.TESTFN + 'dir') # should not raise exception |
---|
931 | n/a | finally: |
---|
932 | n/a | posix.close(f) |
---|
933 | n/a | support.rmtree(support.TESTFN + 'dir') |
---|
934 | n/a | |
---|
935 | n/a | @unittest.skipUnless((os.mknod in os.supports_dir_fd) and hasattr(stat, 'S_IFIFO'), |
---|
936 | n/a | "test requires both stat.S_IFIFO and dir_fd support for os.mknod()") |
---|
937 | n/a | @unittest.skipIf(android_not_root, "mknod not allowed, non root user") |
---|
938 | n/a | def test_mknod_dir_fd(self): |
---|
939 | n/a | # Test using mknodat() to create a FIFO (the only use specified |
---|
940 | n/a | # by POSIX). |
---|
941 | n/a | support.unlink(support.TESTFN) |
---|
942 | n/a | mode = stat.S_IFIFO | stat.S_IRUSR | stat.S_IWUSR |
---|
943 | n/a | f = posix.open(posix.getcwd(), posix.O_RDONLY) |
---|
944 | n/a | try: |
---|
945 | n/a | posix.mknod(support.TESTFN, mode, 0, dir_fd=f) |
---|
946 | n/a | except OSError as e: |
---|
947 | n/a | # Some old systems don't allow unprivileged users to use |
---|
948 | n/a | # mknod(), or only support creating device nodes. |
---|
949 | n/a | self.assertIn(e.errno, (errno.EPERM, errno.EINVAL)) |
---|
950 | n/a | else: |
---|
951 | n/a | self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode)) |
---|
952 | n/a | finally: |
---|
953 | n/a | posix.close(f) |
---|
954 | n/a | |
---|
955 | n/a | @unittest.skipUnless(os.open in os.supports_dir_fd, "test needs dir_fd support in os.open()") |
---|
956 | n/a | def test_open_dir_fd(self): |
---|
957 | n/a | support.unlink(support.TESTFN) |
---|
958 | n/a | with open(support.TESTFN, 'w') as outfile: |
---|
959 | n/a | outfile.write("testline\n") |
---|
960 | n/a | a = posix.open(posix.getcwd(), posix.O_RDONLY) |
---|
961 | n/a | b = posix.open(support.TESTFN, posix.O_RDONLY, dir_fd=a) |
---|
962 | n/a | try: |
---|
963 | n/a | res = posix.read(b, 9).decode(encoding="utf-8") |
---|
964 | n/a | self.assertEqual("testline\n", res) |
---|
965 | n/a | finally: |
---|
966 | n/a | posix.close(a) |
---|
967 | n/a | posix.close(b) |
---|
968 | n/a | |
---|
969 | n/a | @unittest.skipUnless(os.readlink in os.supports_dir_fd, "test needs dir_fd support in os.readlink()") |
---|
970 | n/a | def test_readlink_dir_fd(self): |
---|
971 | n/a | os.symlink(support.TESTFN, support.TESTFN + 'link') |
---|
972 | n/a | f = posix.open(posix.getcwd(), posix.O_RDONLY) |
---|
973 | n/a | try: |
---|
974 | n/a | self.assertEqual(posix.readlink(support.TESTFN + 'link'), |
---|
975 | n/a | posix.readlink(support.TESTFN + 'link', dir_fd=f)) |
---|
976 | n/a | finally: |
---|
977 | n/a | support.unlink(support.TESTFN + 'link') |
---|
978 | n/a | posix.close(f) |
---|
979 | n/a | |
---|
980 | n/a | @unittest.skipUnless(os.rename in os.supports_dir_fd, "test needs dir_fd support in os.rename()") |
---|
981 | n/a | def test_rename_dir_fd(self): |
---|
982 | n/a | support.unlink(support.TESTFN) |
---|
983 | n/a | support.create_empty_file(support.TESTFN + 'ren') |
---|
984 | n/a | f = posix.open(posix.getcwd(), posix.O_RDONLY) |
---|
985 | n/a | try: |
---|
986 | n/a | posix.rename(support.TESTFN + 'ren', support.TESTFN, src_dir_fd=f, dst_dir_fd=f) |
---|
987 | n/a | except: |
---|
988 | n/a | posix.rename(support.TESTFN + 'ren', support.TESTFN) |
---|
989 | n/a | raise |
---|
990 | n/a | else: |
---|
991 | n/a | posix.stat(support.TESTFN) # should not raise exception |
---|
992 | n/a | finally: |
---|
993 | n/a | posix.close(f) |
---|
994 | n/a | |
---|
995 | n/a | @unittest.skipUnless(os.symlink in os.supports_dir_fd, "test needs dir_fd support in os.symlink()") |
---|
996 | n/a | def test_symlink_dir_fd(self): |
---|
997 | n/a | f = posix.open(posix.getcwd(), posix.O_RDONLY) |
---|
998 | n/a | try: |
---|
999 | n/a | posix.symlink(support.TESTFN, support.TESTFN + 'link', dir_fd=f) |
---|
1000 | n/a | self.assertEqual(posix.readlink(support.TESTFN + 'link'), support.TESTFN) |
---|
1001 | n/a | finally: |
---|
1002 | n/a | posix.close(f) |
---|
1003 | n/a | support.unlink(support.TESTFN + 'link') |
---|
1004 | n/a | |
---|
1005 | n/a | @unittest.skipUnless(os.unlink in os.supports_dir_fd, "test needs dir_fd support in os.unlink()") |
---|
1006 | n/a | def test_unlink_dir_fd(self): |
---|
1007 | n/a | f = posix.open(posix.getcwd(), posix.O_RDONLY) |
---|
1008 | n/a | support.create_empty_file(support.TESTFN + 'del') |
---|
1009 | n/a | posix.stat(support.TESTFN + 'del') # should not raise exception |
---|
1010 | n/a | try: |
---|
1011 | n/a | posix.unlink(support.TESTFN + 'del', dir_fd=f) |
---|
1012 | n/a | except: |
---|
1013 | n/a | support.unlink(support.TESTFN + 'del') |
---|
1014 | n/a | raise |
---|
1015 | n/a | else: |
---|
1016 | n/a | self.assertRaises(OSError, posix.stat, support.TESTFN + 'link') |
---|
1017 | n/a | finally: |
---|
1018 | n/a | posix.close(f) |
---|
1019 | n/a | |
---|
1020 | n/a | @unittest.skipUnless(os.mkfifo in os.supports_dir_fd, "test needs dir_fd support in os.mkfifo()") |
---|
1021 | n/a | @unittest.skipIf(android_not_root, "mkfifo not allowed, non root user") |
---|
1022 | n/a | def test_mkfifo_dir_fd(self): |
---|
1023 | n/a | support.unlink(support.TESTFN) |
---|
1024 | n/a | f = posix.open(posix.getcwd(), posix.O_RDONLY) |
---|
1025 | n/a | try: |
---|
1026 | n/a | posix.mkfifo(support.TESTFN, stat.S_IRUSR | stat.S_IWUSR, dir_fd=f) |
---|
1027 | n/a | self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode)) |
---|
1028 | n/a | finally: |
---|
1029 | n/a | posix.close(f) |
---|
1030 | n/a | |
---|
1031 | n/a | requires_sched_h = unittest.skipUnless(hasattr(posix, 'sched_yield'), |
---|
1032 | n/a | "don't have scheduling support") |
---|
1033 | n/a | requires_sched_affinity = unittest.skipUnless(hasattr(posix, 'sched_setaffinity'), |
---|
1034 | n/a | "don't have sched affinity support") |
---|
1035 | n/a | |
---|
1036 | n/a | @requires_sched_h |
---|
1037 | n/a | def test_sched_yield(self): |
---|
1038 | n/a | # This has no error conditions (at least on Linux). |
---|
1039 | n/a | posix.sched_yield() |
---|
1040 | n/a | |
---|
1041 | n/a | @requires_sched_h |
---|
1042 | n/a | @unittest.skipUnless(hasattr(posix, 'sched_get_priority_max'), |
---|
1043 | n/a | "requires sched_get_priority_max()") |
---|
1044 | n/a | def test_sched_priority(self): |
---|
1045 | n/a | # Round-robin usually has interesting priorities. |
---|
1046 | n/a | pol = posix.SCHED_RR |
---|
1047 | n/a | lo = posix.sched_get_priority_min(pol) |
---|
1048 | n/a | hi = posix.sched_get_priority_max(pol) |
---|
1049 | n/a | self.assertIsInstance(lo, int) |
---|
1050 | n/a | self.assertIsInstance(hi, int) |
---|
1051 | n/a | self.assertGreaterEqual(hi, lo) |
---|
1052 | n/a | # OSX evidently just returns 15 without checking the argument. |
---|
1053 | n/a | if sys.platform != "darwin": |
---|
1054 | n/a | self.assertRaises(OSError, posix.sched_get_priority_min, -23) |
---|
1055 | n/a | self.assertRaises(OSError, posix.sched_get_priority_max, -23) |
---|
1056 | n/a | |
---|
1057 | n/a | @unittest.skipUnless(hasattr(posix, 'sched_setscheduler'), "can't change scheduler") |
---|
1058 | n/a | def test_get_and_set_scheduler_and_param(self): |
---|
1059 | n/a | possible_schedulers = [sched for name, sched in posix.__dict__.items() |
---|
1060 | n/a | if name.startswith("SCHED_")] |
---|
1061 | n/a | mine = posix.sched_getscheduler(0) |
---|
1062 | n/a | self.assertIn(mine, possible_schedulers) |
---|
1063 | n/a | try: |
---|
1064 | n/a | parent = posix.sched_getscheduler(os.getppid()) |
---|
1065 | n/a | except OSError as e: |
---|
1066 | n/a | if e.errno != errno.EPERM: |
---|
1067 | n/a | raise |
---|
1068 | n/a | else: |
---|
1069 | n/a | self.assertIn(parent, possible_schedulers) |
---|
1070 | n/a | self.assertRaises(OSError, posix.sched_getscheduler, -1) |
---|
1071 | n/a | self.assertRaises(OSError, posix.sched_getparam, -1) |
---|
1072 | n/a | param = posix.sched_getparam(0) |
---|
1073 | n/a | self.assertIsInstance(param.sched_priority, int) |
---|
1074 | n/a | |
---|
1075 | n/a | # POSIX states that calling sched_setparam() or sched_setscheduler() on |
---|
1076 | n/a | # a process with a scheduling policy other than SCHED_FIFO or SCHED_RR |
---|
1077 | n/a | # is implementation-defined: NetBSD and FreeBSD can return EINVAL. |
---|
1078 | n/a | if not sys.platform.startswith(('freebsd', 'netbsd')): |
---|
1079 | n/a | try: |
---|
1080 | n/a | posix.sched_setscheduler(0, mine, param) |
---|
1081 | n/a | posix.sched_setparam(0, param) |
---|
1082 | n/a | except OSError as e: |
---|
1083 | n/a | if e.errno != errno.EPERM: |
---|
1084 | n/a | raise |
---|
1085 | n/a | self.assertRaises(OSError, posix.sched_setparam, -1, param) |
---|
1086 | n/a | |
---|
1087 | n/a | self.assertRaises(OSError, posix.sched_setscheduler, -1, mine, param) |
---|
1088 | n/a | self.assertRaises(TypeError, posix.sched_setscheduler, 0, mine, None) |
---|
1089 | n/a | self.assertRaises(TypeError, posix.sched_setparam, 0, 43) |
---|
1090 | n/a | param = posix.sched_param(None) |
---|
1091 | n/a | self.assertRaises(TypeError, posix.sched_setparam, 0, param) |
---|
1092 | n/a | large = 214748364700 |
---|
1093 | n/a | param = posix.sched_param(large) |
---|
1094 | n/a | self.assertRaises(OverflowError, posix.sched_setparam, 0, param) |
---|
1095 | n/a | param = posix.sched_param(sched_priority=-large) |
---|
1096 | n/a | self.assertRaises(OverflowError, posix.sched_setparam, 0, param) |
---|
1097 | n/a | |
---|
1098 | n/a | @unittest.skipUnless(hasattr(posix, "sched_rr_get_interval"), "no function") |
---|
1099 | n/a | def test_sched_rr_get_interval(self): |
---|
1100 | n/a | try: |
---|
1101 | n/a | interval = posix.sched_rr_get_interval(0) |
---|
1102 | n/a | except OSError as e: |
---|
1103 | n/a | # This likely means that sched_rr_get_interval is only valid for |
---|
1104 | n/a | # processes with the SCHED_RR scheduler in effect. |
---|
1105 | n/a | if e.errno != errno.EINVAL: |
---|
1106 | n/a | raise |
---|
1107 | n/a | self.skipTest("only works on SCHED_RR processes") |
---|
1108 | n/a | self.assertIsInstance(interval, float) |
---|
1109 | n/a | # Reasonable constraints, I think. |
---|
1110 | n/a | self.assertGreaterEqual(interval, 0.) |
---|
1111 | n/a | self.assertLess(interval, 1.) |
---|
1112 | n/a | |
---|
1113 | n/a | @requires_sched_affinity |
---|
1114 | n/a | def test_sched_getaffinity(self): |
---|
1115 | n/a | mask = posix.sched_getaffinity(0) |
---|
1116 | n/a | self.assertIsInstance(mask, set) |
---|
1117 | n/a | self.assertGreaterEqual(len(mask), 1) |
---|
1118 | n/a | self.assertRaises(OSError, posix.sched_getaffinity, -1) |
---|
1119 | n/a | for cpu in mask: |
---|
1120 | n/a | self.assertIsInstance(cpu, int) |
---|
1121 | n/a | self.assertGreaterEqual(cpu, 0) |
---|
1122 | n/a | self.assertLess(cpu, 1 << 32) |
---|
1123 | n/a | |
---|
1124 | n/a | @requires_sched_affinity |
---|
1125 | n/a | def test_sched_setaffinity(self): |
---|
1126 | n/a | mask = posix.sched_getaffinity(0) |
---|
1127 | n/a | if len(mask) > 1: |
---|
1128 | n/a | # Empty masks are forbidden |
---|
1129 | n/a | mask.pop() |
---|
1130 | n/a | posix.sched_setaffinity(0, mask) |
---|
1131 | n/a | self.assertEqual(posix.sched_getaffinity(0), mask) |
---|
1132 | n/a | self.assertRaises(OSError, posix.sched_setaffinity, 0, []) |
---|
1133 | n/a | self.assertRaises(ValueError, posix.sched_setaffinity, 0, [-10]) |
---|
1134 | n/a | self.assertRaises(OverflowError, posix.sched_setaffinity, 0, [1<<128]) |
---|
1135 | n/a | self.assertRaises(OSError, posix.sched_setaffinity, -1, mask) |
---|
1136 | n/a | |
---|
1137 | n/a | def test_rtld_constants(self): |
---|
1138 | n/a | # check presence of major RTLD_* constants |
---|
1139 | n/a | posix.RTLD_LAZY |
---|
1140 | n/a | posix.RTLD_NOW |
---|
1141 | n/a | posix.RTLD_GLOBAL |
---|
1142 | n/a | posix.RTLD_LOCAL |
---|
1143 | n/a | |
---|
1144 | n/a | @unittest.skipUnless(hasattr(os, 'SEEK_HOLE'), |
---|
1145 | n/a | "test needs an OS that reports file holes") |
---|
1146 | n/a | def test_fs_holes(self): |
---|
1147 | n/a | # Even if the filesystem doesn't report holes, |
---|
1148 | n/a | # if the OS supports it the SEEK_* constants |
---|
1149 | n/a | # will be defined and will have a consistent |
---|
1150 | n/a | # behaviour: |
---|
1151 | n/a | # os.SEEK_DATA = current position |
---|
1152 | n/a | # os.SEEK_HOLE = end of file position |
---|
1153 | n/a | with open(support.TESTFN, 'r+b') as fp: |
---|
1154 | n/a | fp.write(b"hello") |
---|
1155 | n/a | fp.flush() |
---|
1156 | n/a | size = fp.tell() |
---|
1157 | n/a | fno = fp.fileno() |
---|
1158 | n/a | try : |
---|
1159 | n/a | for i in range(size): |
---|
1160 | n/a | self.assertEqual(i, os.lseek(fno, i, os.SEEK_DATA)) |
---|
1161 | n/a | self.assertLessEqual(size, os.lseek(fno, i, os.SEEK_HOLE)) |
---|
1162 | n/a | self.assertRaises(OSError, os.lseek, fno, size, os.SEEK_DATA) |
---|
1163 | n/a | self.assertRaises(OSError, os.lseek, fno, size, os.SEEK_HOLE) |
---|
1164 | n/a | except OSError : |
---|
1165 | n/a | # Some OSs claim to support SEEK_HOLE/SEEK_DATA |
---|
1166 | n/a | # but it is not true. |
---|
1167 | n/a | # For instance: |
---|
1168 | n/a | # http://lists.freebsd.org/pipermail/freebsd-amd64/2012-January/014332.html |
---|
1169 | n/a | raise unittest.SkipTest("OSError raised!") |
---|
1170 | n/a | |
---|
1171 | n/a | def test_path_error2(self): |
---|
1172 | n/a | """ |
---|
1173 | n/a | Test functions that call path_error2(), providing two filenames in their exceptions. |
---|
1174 | n/a | """ |
---|
1175 | n/a | for name in ("rename", "replace", "link"): |
---|
1176 | n/a | function = getattr(os, name, None) |
---|
1177 | n/a | if function is None: |
---|
1178 | n/a | continue |
---|
1179 | n/a | |
---|
1180 | n/a | for dst in ("noodly2", support.TESTFN): |
---|
1181 | n/a | try: |
---|
1182 | n/a | function('doesnotexistfilename', dst) |
---|
1183 | n/a | except OSError as e: |
---|
1184 | n/a | self.assertIn("'doesnotexistfilename' -> '{}'".format(dst), str(e)) |
---|
1185 | n/a | break |
---|
1186 | n/a | else: |
---|
1187 | n/a | self.fail("No valid path_error2() test for os." + name) |
---|
1188 | n/a | |
---|
1189 | n/a | def test_path_with_null_character(self): |
---|
1190 | n/a | fn = support.TESTFN |
---|
1191 | n/a | fn_with_NUL = fn + '\0' |
---|
1192 | n/a | self.addCleanup(support.unlink, fn) |
---|
1193 | n/a | support.unlink(fn) |
---|
1194 | n/a | fd = None |
---|
1195 | n/a | try: |
---|
1196 | n/a | with self.assertRaises(ValueError): |
---|
1197 | n/a | fd = os.open(fn_with_NUL, os.O_WRONLY | os.O_CREAT) # raises |
---|
1198 | n/a | finally: |
---|
1199 | n/a | if fd is not None: |
---|
1200 | n/a | os.close(fd) |
---|
1201 | n/a | self.assertFalse(os.path.exists(fn)) |
---|
1202 | n/a | self.assertRaises(ValueError, os.mkdir, fn_with_NUL) |
---|
1203 | n/a | self.assertFalse(os.path.exists(fn)) |
---|
1204 | n/a | open(fn, 'wb').close() |
---|
1205 | n/a | self.assertRaises(ValueError, os.stat, fn_with_NUL) |
---|
1206 | n/a | |
---|
1207 | n/a | def test_path_with_null_byte(self): |
---|
1208 | n/a | fn = os.fsencode(support.TESTFN) |
---|
1209 | n/a | fn_with_NUL = fn + b'\0' |
---|
1210 | n/a | self.addCleanup(support.unlink, fn) |
---|
1211 | n/a | support.unlink(fn) |
---|
1212 | n/a | fd = None |
---|
1213 | n/a | try: |
---|
1214 | n/a | with self.assertRaises(ValueError): |
---|
1215 | n/a | fd = os.open(fn_with_NUL, os.O_WRONLY | os.O_CREAT) # raises |
---|
1216 | n/a | finally: |
---|
1217 | n/a | if fd is not None: |
---|
1218 | n/a | os.close(fd) |
---|
1219 | n/a | self.assertFalse(os.path.exists(fn)) |
---|
1220 | n/a | self.assertRaises(ValueError, os.mkdir, fn_with_NUL) |
---|
1221 | n/a | self.assertFalse(os.path.exists(fn)) |
---|
1222 | n/a | open(fn, 'wb').close() |
---|
1223 | n/a | self.assertRaises(ValueError, os.stat, fn_with_NUL) |
---|
1224 | n/a | |
---|
1225 | n/a | class PosixGroupsTester(unittest.TestCase): |
---|
1226 | n/a | |
---|
1227 | n/a | def setUp(self): |
---|
1228 | n/a | if posix.getuid() != 0: |
---|
1229 | n/a | raise unittest.SkipTest("not enough privileges") |
---|
1230 | n/a | if not hasattr(posix, 'getgroups'): |
---|
1231 | n/a | raise unittest.SkipTest("need posix.getgroups") |
---|
1232 | n/a | if sys.platform == 'darwin': |
---|
1233 | n/a | raise unittest.SkipTest("getgroups(2) is broken on OSX") |
---|
1234 | n/a | self.saved_groups = posix.getgroups() |
---|
1235 | n/a | |
---|
1236 | n/a | def tearDown(self): |
---|
1237 | n/a | if hasattr(posix, 'setgroups'): |
---|
1238 | n/a | posix.setgroups(self.saved_groups) |
---|
1239 | n/a | elif hasattr(posix, 'initgroups'): |
---|
1240 | n/a | name = pwd.getpwuid(posix.getuid()).pw_name |
---|
1241 | n/a | posix.initgroups(name, self.saved_groups[0]) |
---|
1242 | n/a | |
---|
1243 | n/a | @unittest.skipUnless(hasattr(posix, 'initgroups'), |
---|
1244 | n/a | "test needs posix.initgroups()") |
---|
1245 | n/a | def test_initgroups(self): |
---|
1246 | n/a | # find missing group |
---|
1247 | n/a | |
---|
1248 | n/a | g = max(self.saved_groups or [0]) + 1 |
---|
1249 | n/a | name = pwd.getpwuid(posix.getuid()).pw_name |
---|
1250 | n/a | posix.initgroups(name, g) |
---|
1251 | n/a | self.assertIn(g, posix.getgroups()) |
---|
1252 | n/a | |
---|
1253 | n/a | @unittest.skipUnless(hasattr(posix, 'setgroups'), |
---|
1254 | n/a | "test needs posix.setgroups()") |
---|
1255 | n/a | def test_setgroups(self): |
---|
1256 | n/a | for groups in [[0], list(range(16))]: |
---|
1257 | n/a | posix.setgroups(groups) |
---|
1258 | n/a | self.assertListEqual(groups, posix.getgroups()) |
---|
1259 | n/a | |
---|
1260 | n/a | def test_main(): |
---|
1261 | n/a | try: |
---|
1262 | n/a | support.run_unittest(PosixTester, PosixGroupsTester) |
---|
1263 | n/a | finally: |
---|
1264 | n/a | support.reap_children() |
---|
1265 | n/a | |
---|
1266 | n/a | if __name__ == '__main__': |
---|
1267 | n/a | test_main() |
---|