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