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