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