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