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