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