Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1 | "Test posix functions" |
| 2 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 3 | from test import support |
R. David Murray | eb3615d | 2009-04-22 02:24:39 +0000 | [diff] [blame] | 4 | |
| 5 | # Skip these tests if there is no posix module. |
| 6 | posix = support.import_module('posix') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 8 | import errno |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 9 | import sys |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 10 | import time |
| 11 | import os |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 12 | import fcntl |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 13 | import pwd |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 14 | import shutil |
Benjamin Peterson | 052a02b | 2010-08-17 01:27:09 +0000 | [diff] [blame] | 15 | import stat |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 16 | import unittest |
| 17 | import warnings |
R. David Murray | a21e4ca | 2009-03-31 23:16:50 +0000 | [diff] [blame] | 18 | |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 19 | |
| 20 | class PosixTester(unittest.TestCase): |
| 21 | |
| 22 | def setUp(self): |
| 23 | # create empty file |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 24 | fp = open(support.TESTFN, 'w+') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 25 | fp.close() |
Brett Cannon | c8d502e | 2010-03-20 21:53:28 +0000 | [diff] [blame] | 26 | self._warnings_manager = support.check_warnings() |
| 27 | self._warnings_manager.__enter__() |
| 28 | warnings.filterwarnings('ignore', '.* potential security risk .*', |
| 29 | RuntimeWarning) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 30 | |
| 31 | def tearDown(self): |
Neal Norwitz | c34177c | 2008-08-25 01:04:16 +0000 | [diff] [blame] | 32 | support.unlink(support.TESTFN) |
Brett Cannon | c8d502e | 2010-03-20 21:53:28 +0000 | [diff] [blame] | 33 | self._warnings_manager.__exit__(None, None, None) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 34 | |
| 35 | def testNoArgFunctions(self): |
| 36 | # test posix functions which take no arguments and have |
| 37 | # 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] | 38 | NO_ARG_FUNCTIONS = [ "ctermid", "getcwd", "getcwdb", "uname", |
Guido van Rossum | 687b9c0 | 2007-10-25 23:18:51 +0000 | [diff] [blame] | 39 | "times", "getloadavg", |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 40 | "getegid", "geteuid", "getgid", "getgroups", |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 41 | "getpid", "getpgrp", "getppid", "getuid", "sync", |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 42 | ] |
Neal Norwitz | 71b13e8 | 2003-02-23 22:12:24 +0000 | [diff] [blame] | 43 | |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 44 | for name in NO_ARG_FUNCTIONS: |
| 45 | posix_func = getattr(posix, name, None) |
| 46 | if posix_func is not None: |
| 47 | posix_func() |
Neal Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 48 | self.assertRaises(TypeError, posix_func, 1) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 49 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 50 | if hasattr(posix, 'getresuid'): |
| 51 | def test_getresuid(self): |
| 52 | user_ids = posix.getresuid() |
| 53 | self.assertEqual(len(user_ids), 3) |
| 54 | for val in user_ids: |
| 55 | self.assertGreaterEqual(val, 0) |
| 56 | |
| 57 | if hasattr(posix, 'getresgid'): |
| 58 | def test_getresgid(self): |
| 59 | group_ids = posix.getresgid() |
| 60 | self.assertEqual(len(group_ids), 3) |
| 61 | for val in group_ids: |
| 62 | self.assertGreaterEqual(val, 0) |
| 63 | |
| 64 | if hasattr(posix, 'setresuid'): |
| 65 | def test_setresuid(self): |
| 66 | current_user_ids = posix.getresuid() |
| 67 | self.assertIsNone(posix.setresuid(*current_user_ids)) |
| 68 | # -1 means don't change that value. |
| 69 | self.assertIsNone(posix.setresuid(-1, -1, -1)) |
| 70 | |
| 71 | def test_setresuid_exception(self): |
| 72 | # Don't do this test if someone is silly enough to run us as root. |
| 73 | current_user_ids = posix.getresuid() |
| 74 | if 0 not in current_user_ids: |
| 75 | new_user_ids = (current_user_ids[0]+1, -1, -1) |
| 76 | self.assertRaises(OSError, posix.setresuid, *new_user_ids) |
| 77 | |
| 78 | if hasattr(posix, 'setresgid'): |
| 79 | def test_setresgid(self): |
| 80 | current_group_ids = posix.getresgid() |
| 81 | self.assertIsNone(posix.setresgid(*current_group_ids)) |
| 82 | # -1 means don't change that value. |
| 83 | self.assertIsNone(posix.setresgid(-1, -1, -1)) |
| 84 | |
| 85 | def test_setresgid_exception(self): |
| 86 | # Don't do this test if someone is silly enough to run us as root. |
| 87 | current_group_ids = posix.getresgid() |
| 88 | if 0 not in current_group_ids: |
| 89 | new_group_ids = (current_group_ids[0]+1, -1, -1) |
| 90 | self.assertRaises(OSError, posix.setresgid, *new_group_ids) |
| 91 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 92 | @unittest.skipUnless(hasattr(posix, 'initgroups'), |
| 93 | "test needs os.initgroups()") |
| 94 | def test_initgroups(self): |
| 95 | # It takes a string and an integer; check that it raises a TypeError |
| 96 | # for other argument lists. |
| 97 | self.assertRaises(TypeError, posix.initgroups) |
| 98 | self.assertRaises(TypeError, posix.initgroups, None) |
| 99 | self.assertRaises(TypeError, posix.initgroups, 3, "foo") |
| 100 | self.assertRaises(TypeError, posix.initgroups, "foo", 3, object()) |
| 101 | |
| 102 | # If a non-privileged user invokes it, it should fail with OSError |
| 103 | # EPERM. |
| 104 | if os.getuid() != 0: |
| 105 | name = pwd.getpwuid(posix.getuid()).pw_name |
| 106 | try: |
| 107 | posix.initgroups(name, 13) |
| 108 | except OSError as e: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 109 | self.assertEqual(e.errno, errno.EPERM) |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 110 | else: |
| 111 | self.fail("Expected OSError to be raised by initgroups") |
| 112 | |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 113 | def test_statvfs(self): |
| 114 | if hasattr(posix, 'statvfs'): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 115 | self.assertTrue(posix.statvfs(os.curdir)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 116 | |
| 117 | def test_fstatvfs(self): |
| 118 | if hasattr(posix, 'fstatvfs'): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 119 | fp = open(support.TESTFN) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 120 | try: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 121 | self.assertTrue(posix.fstatvfs(fp.fileno())) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 122 | finally: |
| 123 | fp.close() |
| 124 | |
| 125 | def test_ftruncate(self): |
| 126 | if hasattr(posix, 'ftruncate'): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 127 | fp = open(support.TESTFN, 'w+') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 128 | try: |
| 129 | # we need to have some data to truncate |
| 130 | fp.write('test') |
| 131 | fp.flush() |
| 132 | posix.ftruncate(fp.fileno(), 0) |
| 133 | finally: |
| 134 | fp.close() |
| 135 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 136 | @unittest.skipUnless(hasattr(posix, 'truncate'), "test needs posix.truncate()") |
| 137 | def test_truncate(self): |
| 138 | with open(support.TESTFN, 'w') as fp: |
| 139 | fp.write('test') |
| 140 | fp.flush() |
| 141 | posix.truncate(support.TESTFN, 0) |
| 142 | |
| 143 | @unittest.skipUnless(hasattr(posix, 'fexecve'), "test needs posix.fexecve()") |
| 144 | @unittest.skipUnless(hasattr(os, 'fork'), "test needs os.fork()") |
Ross Lagerwall | dedf6cf | 2011-03-20 18:27:05 +0200 | [diff] [blame] | 145 | @unittest.skipUnless(hasattr(os, 'waitpid'), "test needs os.waitpid()") |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 146 | def test_fexecve(self): |
| 147 | fp = os.open(sys.executable, os.O_RDONLY) |
| 148 | try: |
| 149 | pid = os.fork() |
| 150 | if pid == 0: |
| 151 | os.chdir(os.path.split(sys.executable)[0]) |
| 152 | posix.fexecve(fp, [sys.executable, '-c', 'pass'], os.environ) |
| 153 | else: |
Ross Lagerwall | dedf6cf | 2011-03-20 18:27:05 +0200 | [diff] [blame] | 154 | self.assertEqual(os.waitpid(pid, 0), (pid, 0)) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 155 | finally: |
| 156 | os.close(fp) |
| 157 | |
| 158 | @unittest.skipUnless(hasattr(posix, 'waitid'), "test needs posix.waitid()") |
| 159 | @unittest.skipUnless(hasattr(os, 'fork'), "test needs os.fork()") |
| 160 | def test_waitid(self): |
| 161 | pid = os.fork() |
| 162 | if pid == 0: |
| 163 | os.chdir(os.path.split(sys.executable)[0]) |
| 164 | posix.execve(sys.executable, [sys.executable, '-c', 'pass'], os.environ) |
| 165 | else: |
| 166 | res = posix.waitid(posix.P_PID, pid, posix.WEXITED) |
| 167 | self.assertEqual(pid, res.si_pid) |
| 168 | |
| 169 | @unittest.skipUnless(hasattr(posix, 'lockf'), "test needs posix.lockf()") |
| 170 | def test_lockf(self): |
| 171 | fd = os.open(support.TESTFN, os.O_WRONLY | os.O_CREAT) |
| 172 | try: |
| 173 | os.write(fd, b'test') |
| 174 | os.lseek(fd, 0, os.SEEK_SET) |
| 175 | posix.lockf(fd, posix.F_LOCK, 4) |
| 176 | # section is locked |
| 177 | posix.lockf(fd, posix.F_ULOCK, 4) |
| 178 | finally: |
| 179 | os.close(fd) |
| 180 | |
| 181 | @unittest.skipUnless(hasattr(posix, 'pread'), "test needs posix.pread()") |
| 182 | def test_pread(self): |
| 183 | fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT) |
| 184 | try: |
| 185 | os.write(fd, b'test') |
| 186 | os.lseek(fd, 0, os.SEEK_SET) |
| 187 | self.assertEqual(b'es', posix.pread(fd, 2, 1)) |
| 188 | # the first pread() shoudn't disturb the file offset |
| 189 | self.assertEqual(b'te', posix.read(fd, 2)) |
| 190 | finally: |
| 191 | os.close(fd) |
| 192 | |
| 193 | @unittest.skipUnless(hasattr(posix, 'pwrite'), "test needs posix.pwrite()") |
| 194 | def test_pwrite(self): |
| 195 | fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT) |
| 196 | try: |
| 197 | os.write(fd, b'test') |
| 198 | os.lseek(fd, 0, os.SEEK_SET) |
| 199 | posix.pwrite(fd, b'xx', 1) |
| 200 | self.assertEqual(b'txxt', posix.read(fd, 4)) |
| 201 | finally: |
| 202 | os.close(fd) |
| 203 | |
| 204 | @unittest.skipUnless(hasattr(posix, 'posix_fallocate'), |
| 205 | "test needs posix.posix_fallocate()") |
| 206 | def test_posix_fallocate(self): |
| 207 | fd = os.open(support.TESTFN, os.O_WRONLY | os.O_CREAT) |
| 208 | try: |
| 209 | posix.posix_fallocate(fd, 0, 10) |
| 210 | except OSError as inst: |
| 211 | # issue10812, ZFS doesn't appear to support posix_fallocate, |
| 212 | # so skip Solaris-based since they are likely to have ZFS. |
| 213 | if inst.errno != errno.EINVAL or not sys.platform.startswith("sunos"): |
| 214 | raise |
| 215 | finally: |
| 216 | os.close(fd) |
| 217 | |
| 218 | @unittest.skipUnless(hasattr(posix, 'posix_fadvise'), |
| 219 | "test needs posix.posix_fadvise()") |
| 220 | def test_posix_fadvise(self): |
| 221 | fd = os.open(support.TESTFN, os.O_RDONLY) |
| 222 | try: |
| 223 | posix.posix_fadvise(fd, 0, 0, posix.POSIX_FADV_WILLNEED) |
| 224 | finally: |
| 225 | os.close(fd) |
| 226 | |
| 227 | @unittest.skipUnless(hasattr(posix, 'futimes'), "test needs posix.futimes()") |
| 228 | def test_futimes(self): |
| 229 | now = time.time() |
| 230 | fd = os.open(support.TESTFN, os.O_RDONLY) |
| 231 | try: |
| 232 | posix.futimes(fd, None) |
| 233 | self.assertRaises(TypeError, posix.futimes, fd, (None, None)) |
| 234 | self.assertRaises(TypeError, posix.futimes, fd, (now, None)) |
| 235 | self.assertRaises(TypeError, posix.futimes, fd, (None, now)) |
| 236 | posix.futimes(fd, (int(now), int(now))) |
| 237 | posix.futimes(fd, (now, now)) |
| 238 | finally: |
| 239 | os.close(fd) |
| 240 | |
| 241 | @unittest.skipUnless(hasattr(posix, 'lutimes'), "test needs posix.lutimes()") |
| 242 | def test_lutimes(self): |
| 243 | now = time.time() |
| 244 | posix.lutimes(support.TESTFN, None) |
| 245 | self.assertRaises(TypeError, posix.lutimes, support.TESTFN, (None, None)) |
| 246 | self.assertRaises(TypeError, posix.lutimes, support.TESTFN, (now, None)) |
| 247 | self.assertRaises(TypeError, posix.lutimes, support.TESTFN, (None, now)) |
| 248 | posix.lutimes(support.TESTFN, (int(now), int(now))) |
| 249 | posix.lutimes(support.TESTFN, (now, now)) |
| 250 | |
| 251 | @unittest.skipUnless(hasattr(posix, 'futimens'), "test needs posix.futimens()") |
| 252 | def test_futimens(self): |
| 253 | now = time.time() |
| 254 | fd = os.open(support.TESTFN, os.O_RDONLY) |
| 255 | try: |
| 256 | self.assertRaises(TypeError, posix.futimens, fd, (None, None), (None, None)) |
| 257 | self.assertRaises(TypeError, posix.futimens, fd, (now, 0), None) |
| 258 | self.assertRaises(TypeError, posix.futimens, fd, None, (now, 0)) |
| 259 | posix.futimens(fd, (int(now), int((now - int(now)) * 1e9)), |
| 260 | (int(now), int((now - int(now)) * 1e9))) |
| 261 | finally: |
| 262 | os.close(fd) |
| 263 | |
| 264 | @unittest.skipUnless(hasattr(posix, 'writev'), "test needs posix.writev()") |
| 265 | def test_writev(self): |
| 266 | fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT) |
| 267 | try: |
| 268 | os.writev(fd, (b'test1', b'tt2', b't3')) |
| 269 | os.lseek(fd, 0, os.SEEK_SET) |
| 270 | self.assertEqual(b'test1tt2t3', posix.read(fd, 10)) |
| 271 | finally: |
| 272 | os.close(fd) |
| 273 | |
| 274 | @unittest.skipUnless(hasattr(posix, 'readv'), "test needs posix.readv()") |
| 275 | def test_readv(self): |
| 276 | fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT) |
| 277 | try: |
| 278 | os.write(fd, b'test1tt2t3') |
| 279 | os.lseek(fd, 0, os.SEEK_SET) |
| 280 | buf = [bytearray(i) for i in [5, 3, 2]] |
| 281 | self.assertEqual(posix.readv(fd, buf), 10) |
| 282 | self.assertEqual([b'test1', b'tt2', b't3'], [bytes(i) for i in buf]) |
| 283 | finally: |
| 284 | os.close(fd) |
| 285 | |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 286 | def test_dup(self): |
| 287 | if hasattr(posix, 'dup'): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 288 | fp = open(support.TESTFN) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 289 | try: |
| 290 | fd = posix.dup(fp.fileno()) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 291 | self.assertIsInstance(fd, int) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 292 | os.close(fd) |
| 293 | finally: |
| 294 | fp.close() |
| 295 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 296 | def test_confstr(self): |
| 297 | if hasattr(posix, 'confstr'): |
| 298 | self.assertRaises(ValueError, posix.confstr, "CS_garbage") |
| 299 | self.assertEqual(len(posix.confstr("CS_PATH")) > 0, True) |
| 300 | |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 301 | def test_dup2(self): |
| 302 | if hasattr(posix, 'dup2'): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 303 | fp1 = open(support.TESTFN) |
| 304 | fp2 = open(support.TESTFN) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 305 | try: |
| 306 | posix.dup2(fp1.fileno(), fp2.fileno()) |
| 307 | finally: |
| 308 | fp1.close() |
| 309 | fp2.close() |
| 310 | |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 311 | @unittest.skipUnless(hasattr(os, 'O_CLOEXEC'), "needs os.O_CLOEXEC") |
Charles-François Natali | 239bb96 | 2011-06-03 12:55:15 +0200 | [diff] [blame] | 312 | @support.requires_linux_version(2, 6, 23) |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 313 | def test_oscloexec(self): |
| 314 | fd = os.open(support.TESTFN, os.O_RDONLY|os.O_CLOEXEC) |
| 315 | self.addCleanup(os.close, fd) |
Victor Stinner | e36f375 | 2011-05-24 00:29:43 +0200 | [diff] [blame] | 316 | self.assertTrue(fcntl.fcntl(fd, fcntl.F_GETFD) & fcntl.FD_CLOEXEC) |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 317 | |
Skip Montanaro | 9847000 | 2005-06-17 01:14:49 +0000 | [diff] [blame] | 318 | def test_osexlock(self): |
| 319 | if hasattr(posix, "O_EXLOCK"): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 320 | fd = os.open(support.TESTFN, |
Skip Montanaro | 9847000 | 2005-06-17 01:14:49 +0000 | [diff] [blame] | 321 | os.O_WRONLY|os.O_EXLOCK|os.O_CREAT) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 322 | self.assertRaises(OSError, os.open, support.TESTFN, |
Skip Montanaro | 9847000 | 2005-06-17 01:14:49 +0000 | [diff] [blame] | 323 | os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK) |
| 324 | os.close(fd) |
| 325 | |
| 326 | if hasattr(posix, "O_SHLOCK"): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 327 | fd = os.open(support.TESTFN, |
Skip Montanaro | 9847000 | 2005-06-17 01:14:49 +0000 | [diff] [blame] | 328 | os.O_WRONLY|os.O_SHLOCK|os.O_CREAT) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 329 | self.assertRaises(OSError, os.open, support.TESTFN, |
Skip Montanaro | 9847000 | 2005-06-17 01:14:49 +0000 | [diff] [blame] | 330 | os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK) |
| 331 | os.close(fd) |
| 332 | |
| 333 | def test_osshlock(self): |
| 334 | if hasattr(posix, "O_SHLOCK"): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 335 | fd1 = os.open(support.TESTFN, |
Skip Montanaro | 9847000 | 2005-06-17 01:14:49 +0000 | [diff] [blame] | 336 | os.O_WRONLY|os.O_SHLOCK|os.O_CREAT) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 337 | fd2 = os.open(support.TESTFN, |
Skip Montanaro | 9847000 | 2005-06-17 01:14:49 +0000 | [diff] [blame] | 338 | os.O_WRONLY|os.O_SHLOCK|os.O_CREAT) |
| 339 | os.close(fd2) |
| 340 | os.close(fd1) |
| 341 | |
| 342 | if hasattr(posix, "O_EXLOCK"): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 343 | fd = os.open(support.TESTFN, |
Skip Montanaro | 9847000 | 2005-06-17 01:14:49 +0000 | [diff] [blame] | 344 | os.O_WRONLY|os.O_SHLOCK|os.O_CREAT) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 345 | self.assertRaises(OSError, os.open, support.TESTFN, |
Skip Montanaro | 9847000 | 2005-06-17 01:14:49 +0000 | [diff] [blame] | 346 | os.O_RDONLY|os.O_EXLOCK|os.O_NONBLOCK) |
| 347 | os.close(fd) |
| 348 | |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 349 | def test_fstat(self): |
| 350 | if hasattr(posix, 'fstat'): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 351 | fp = open(support.TESTFN) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 352 | try: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 353 | self.assertTrue(posix.fstat(fp.fileno())) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 354 | finally: |
| 355 | fp.close() |
| 356 | |
| 357 | def test_stat(self): |
| 358 | if hasattr(posix, 'stat'): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 359 | self.assertTrue(posix.stat(support.TESTFN)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 360 | |
Benjamin Peterson | 052a02b | 2010-08-17 01:27:09 +0000 | [diff] [blame] | 361 | @unittest.skipUnless(hasattr(posix, 'mkfifo'), "don't have mkfifo()") |
| 362 | def test_mkfifo(self): |
| 363 | support.unlink(support.TESTFN) |
| 364 | posix.mkfifo(support.TESTFN, stat.S_IRUSR | stat.S_IWUSR) |
| 365 | self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode)) |
| 366 | |
| 367 | @unittest.skipUnless(hasattr(posix, 'mknod') and hasattr(stat, 'S_IFIFO'), |
| 368 | "don't have mknod()/S_IFIFO") |
| 369 | def test_mknod(self): |
| 370 | # Test using mknod() to create a FIFO (the only use specified |
| 371 | # by POSIX). |
| 372 | support.unlink(support.TESTFN) |
| 373 | mode = stat.S_IFIFO | stat.S_IRUSR | stat.S_IWUSR |
| 374 | try: |
| 375 | posix.mknod(support.TESTFN, mode, 0) |
| 376 | except OSError as e: |
| 377 | # Some old systems don't allow unprivileged users to use |
| 378 | # mknod(), or only support creating device nodes. |
| 379 | self.assertIn(e.errno, (errno.EPERM, errno.EINVAL)) |
| 380 | else: |
| 381 | self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode)) |
| 382 | |
Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 383 | def _test_all_chown_common(self, chown_func, first_param): |
| 384 | """Common code for chown, fchown and lchown tests.""" |
| 385 | if os.getuid() == 0: |
| 386 | try: |
| 387 | # Many linux distros have a nfsnobody user as MAX_UID-2 |
| 388 | # that makes a good test case for signedness issues. |
| 389 | # http://bugs.python.org/issue1747858 |
| 390 | # This part of the test only runs when run as root. |
| 391 | # Only scary people run their tests as root. |
| 392 | ent = pwd.getpwnam('nfsnobody') |
| 393 | chown_func(first_param, ent.pw_uid, ent.pw_gid) |
| 394 | except KeyError: |
| 395 | pass |
| 396 | else: |
| 397 | # non-root cannot chown to root, raises OSError |
| 398 | self.assertRaises(OSError, chown_func, |
| 399 | first_param, 0, 0) |
| 400 | # test a successful chown call |
| 401 | chown_func(first_param, os.getuid(), os.getgid()) |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 402 | |
Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 403 | @unittest.skipUnless(hasattr(posix, 'chown'), "test needs os.chown()") |
| 404 | def test_chown(self): |
| 405 | # raise an OSError if the file does not exist |
| 406 | os.unlink(support.TESTFN) |
| 407 | self.assertRaises(OSError, posix.chown, support.TESTFN, -1, -1) |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 408 | |
Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 409 | # re-create the file |
| 410 | open(support.TESTFN, 'w').close() |
| 411 | self._test_all_chown_common(posix.chown, support.TESTFN) |
| 412 | |
| 413 | @unittest.skipUnless(hasattr(posix, 'fchown'), "test needs os.fchown()") |
| 414 | def test_fchown(self): |
| 415 | os.unlink(support.TESTFN) |
| 416 | |
| 417 | # re-create the file |
| 418 | test_file = open(support.TESTFN, 'w') |
| 419 | try: |
| 420 | fd = test_file.fileno() |
| 421 | self._test_all_chown_common(posix.fchown, fd) |
| 422 | finally: |
| 423 | test_file.close() |
| 424 | |
| 425 | @unittest.skipUnless(hasattr(posix, 'lchown'), "test needs os.lchown()") |
| 426 | def test_lchown(self): |
| 427 | os.unlink(support.TESTFN) |
| 428 | # create a symlink |
| 429 | os.symlink('/tmp/dummy-symlink-target', support.TESTFN) |
| 430 | self._test_all_chown_common(posix.lchown, support.TESTFN) |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 431 | |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 432 | def test_chdir(self): |
| 433 | if hasattr(posix, 'chdir'): |
| 434 | posix.chdir(os.curdir) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 435 | self.assertRaises(OSError, posix.chdir, support.TESTFN) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 436 | |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 437 | def test_listdir(self): |
| 438 | if hasattr(posix, 'listdir'): |
| 439 | self.assertTrue(support.TESTFN in posix.listdir(os.curdir)) |
| 440 | |
| 441 | def test_listdir_default(self): |
| 442 | # When listdir is called without argument, it's the same as listdir(os.curdir) |
| 443 | if hasattr(posix, 'listdir'): |
| 444 | self.assertTrue(support.TESTFN in posix.listdir()) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 445 | |
Antoine Pitrou | 8250e23 | 2011-02-25 23:41:16 +0000 | [diff] [blame] | 446 | @unittest.skipUnless(hasattr(posix, 'fdlistdir'), "test needs posix.fdlistdir()") |
| 447 | def test_fdlistdir(self): |
| 448 | f = posix.open(posix.getcwd(), posix.O_RDONLY) |
| 449 | self.assertEqual( |
| 450 | sorted(posix.listdir('.')), |
| 451 | sorted(posix.fdlistdir(f)) |
| 452 | ) |
| 453 | # Check the fd was closed by fdlistdir |
| 454 | with self.assertRaises(OSError) as ctx: |
| 455 | posix.close(f) |
| 456 | self.assertEqual(ctx.exception.errno, errno.EBADF) |
| 457 | |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 458 | def test_access(self): |
| 459 | if hasattr(posix, 'access'): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 460 | self.assertTrue(posix.access(support.TESTFN, os.R_OK)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 461 | |
| 462 | def test_umask(self): |
| 463 | if hasattr(posix, 'umask'): |
| 464 | old_mask = posix.umask(0) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 465 | self.assertIsInstance(old_mask, int) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 466 | posix.umask(old_mask) |
| 467 | |
| 468 | def test_strerror(self): |
| 469 | if hasattr(posix, 'strerror'): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 470 | self.assertTrue(posix.strerror(0)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 471 | |
| 472 | def test_pipe(self): |
| 473 | if hasattr(posix, 'pipe'): |
| 474 | reader, writer = posix.pipe() |
| 475 | os.close(reader) |
| 476 | os.close(writer) |
| 477 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 478 | @unittest.skipUnless(hasattr(os, 'pipe2'), "test needs os.pipe2()") |
Charles-François Natali | 239bb96 | 2011-06-03 12:55:15 +0200 | [diff] [blame] | 479 | @support.requires_linux_version(2, 6, 27) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 480 | def test_pipe2(self): |
| 481 | self.assertRaises(TypeError, os.pipe2, 'DEADBEEF') |
| 482 | self.assertRaises(TypeError, os.pipe2, 0, 0) |
| 483 | |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 484 | # try calling with flags = 0, like os.pipe() |
| 485 | r, w = os.pipe2(0) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 486 | os.close(r) |
| 487 | os.close(w) |
| 488 | |
| 489 | # test flags |
| 490 | r, w = os.pipe2(os.O_CLOEXEC|os.O_NONBLOCK) |
| 491 | self.addCleanup(os.close, r) |
| 492 | self.addCleanup(os.close, w) |
| 493 | self.assertTrue(fcntl.fcntl(r, fcntl.F_GETFD) & fcntl.FD_CLOEXEC) |
| 494 | self.assertTrue(fcntl.fcntl(w, fcntl.F_GETFD) & fcntl.FD_CLOEXEC) |
| 495 | # try reading from an empty pipe: this should fail, not block |
| 496 | self.assertRaises(OSError, os.read, r, 1) |
| 497 | # try a write big enough to fill-up the pipe: this should either |
| 498 | # fail or perform a partial write, not block |
| 499 | try: |
| 500 | os.write(w, b'x' * support.PIPE_MAX_SIZE) |
| 501 | except OSError: |
| 502 | pass |
| 503 | |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 504 | def test_utime(self): |
| 505 | if hasattr(posix, 'utime'): |
| 506 | now = time.time() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 507 | posix.utime(support.TESTFN, None) |
| 508 | self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, None)) |
| 509 | self.assertRaises(TypeError, posix.utime, support.TESTFN, (now, None)) |
| 510 | self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, now)) |
| 511 | posix.utime(support.TESTFN, (int(now), int(now))) |
| 512 | posix.utime(support.TESTFN, (now, now)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 513 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 514 | def test_chflags(self): |
| 515 | if hasattr(posix, 'chflags'): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 516 | st = os.stat(support.TESTFN) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 517 | if hasattr(st, 'st_flags'): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 518 | posix.chflags(support.TESTFN, st.st_flags) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 519 | |
| 520 | def test_lchflags(self): |
| 521 | if hasattr(posix, 'lchflags'): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 522 | st = os.stat(support.TESTFN) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 523 | if hasattr(st, 'st_flags'): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 524 | posix.lchflags(support.TESTFN, st.st_flags) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 525 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 526 | def test_environ(self): |
Victor Stinner | 17b490d | 2010-05-06 22:19:30 +0000 | [diff] [blame] | 527 | if os.name == "nt": |
| 528 | item_type = str |
| 529 | else: |
| 530 | item_type = bytes |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 531 | for k, v in posix.environ.items(): |
Victor Stinner | 17b490d | 2010-05-06 22:19:30 +0000 | [diff] [blame] | 532 | self.assertEqual(type(k), item_type) |
| 533 | self.assertEqual(type(v), item_type) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 534 | |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 535 | def test_getcwd_long_pathnames(self): |
| 536 | if hasattr(posix, 'getcwd'): |
| 537 | dirname = 'getcwd-test-directory-0123456789abcdef-01234567890abcdef' |
| 538 | curdir = os.getcwd() |
| 539 | base_path = os.path.abspath(support.TESTFN) + '.getcwd' |
| 540 | |
| 541 | try: |
| 542 | os.mkdir(base_path) |
| 543 | os.chdir(base_path) |
| 544 | except: |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 545 | # Just returning nothing instead of the SkipTest exception, |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 546 | # because the test results in Error in that case. |
| 547 | # Is that ok? |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 548 | # raise unittest.SkipTest("cannot create directory for testing") |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 549 | return |
| 550 | |
| 551 | def _create_and_do_getcwd(dirname, current_path_length = 0): |
| 552 | try: |
| 553 | os.mkdir(dirname) |
| 554 | except: |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 555 | raise unittest.SkipTest("mkdir cannot create directory sufficiently deep for getcwd test") |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 556 | |
| 557 | os.chdir(dirname) |
| 558 | try: |
| 559 | os.getcwd() |
| 560 | if current_path_length < 1027: |
| 561 | _create_and_do_getcwd(dirname, current_path_length + len(dirname) + 1) |
| 562 | finally: |
| 563 | os.chdir('..') |
| 564 | os.rmdir(dirname) |
| 565 | |
| 566 | _create_and_do_getcwd(dirname) |
| 567 | |
| 568 | finally: |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 569 | os.chdir(curdir) |
R. David Murray | 414c91f | 2009-07-09 20:12:31 +0000 | [diff] [blame] | 570 | support.rmtree(base_path) |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 571 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 572 | @unittest.skipUnless(hasattr(posix, 'getgrouplist'), "test needs posix.getgrouplist()") |
| 573 | @unittest.skipUnless(hasattr(pwd, 'getpwuid'), "test needs pwd.getpwuid()") |
| 574 | @unittest.skipUnless(hasattr(os, 'getuid'), "test needs os.getuid()") |
| 575 | def test_getgrouplist(self): |
| 576 | with os.popen('id -G') as idg: |
| 577 | groups = idg.read().strip() |
| 578 | |
| 579 | if not groups: |
| 580 | raise unittest.SkipTest("need working 'id -G'") |
| 581 | |
| 582 | self.assertEqual( |
| 583 | set([int(x) for x in groups.split()]), |
| 584 | set(posix.getgrouplist(pwd.getpwuid(os.getuid())[0], |
| 585 | pwd.getpwuid(os.getuid())[3]))) |
| 586 | |
Antoine Pitrou | 318b8f3 | 2011-01-12 18:45:27 +0000 | [diff] [blame] | 587 | @unittest.skipUnless(hasattr(os, 'getegid'), "test needs os.getegid()") |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 588 | def test_getgroups(self): |
| 589 | with os.popen('id -G') as idg: |
| 590 | groups = idg.read().strip() |
| 591 | |
| 592 | if not groups: |
| 593 | raise unittest.SkipTest("need working 'id -G'") |
| 594 | |
Ronald Oussoren | 7fb6f51 | 2010-08-01 19:18:13 +0000 | [diff] [blame] | 595 | # 'id -G' and 'os.getgroups()' should return the same |
| 596 | # groups, ignoring order and duplicates. |
Antoine Pitrou | 318b8f3 | 2011-01-12 18:45:27 +0000 | [diff] [blame] | 597 | # #10822 - it is implementation defined whether posix.getgroups() |
| 598 | # includes the effective gid so we include it anyway, since id -G does |
Ronald Oussoren | cb615e6 | 2010-07-24 14:15:19 +0000 | [diff] [blame] | 599 | self.assertEqual( |
Ronald Oussoren | 7fb6f51 | 2010-08-01 19:18:13 +0000 | [diff] [blame] | 600 | set([int(x) for x in groups.split()]), |
Antoine Pitrou | 318b8f3 | 2011-01-12 18:45:27 +0000 | [diff] [blame] | 601 | set(posix.getgroups() + [posix.getegid()])) |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 602 | |
Antoine Pitrou | f65132d | 2011-02-25 23:25:17 +0000 | [diff] [blame] | 603 | # tests for the posix *at functions follow |
| 604 | |
| 605 | @unittest.skipUnless(hasattr(posix, 'faccessat'), "test needs posix.faccessat()") |
| 606 | def test_faccessat(self): |
| 607 | f = posix.open(posix.getcwd(), posix.O_RDONLY) |
| 608 | try: |
| 609 | self.assertTrue(posix.faccessat(f, support.TESTFN, os.R_OK)) |
| 610 | finally: |
| 611 | posix.close(f) |
| 612 | |
| 613 | @unittest.skipUnless(hasattr(posix, 'fchmodat'), "test needs posix.fchmodat()") |
| 614 | def test_fchmodat(self): |
| 615 | os.chmod(support.TESTFN, stat.S_IRUSR) |
| 616 | |
| 617 | f = posix.open(posix.getcwd(), posix.O_RDONLY) |
| 618 | try: |
| 619 | posix.fchmodat(f, support.TESTFN, stat.S_IRUSR | stat.S_IWUSR) |
| 620 | |
| 621 | s = posix.stat(support.TESTFN) |
| 622 | self.assertEqual(s[0] & stat.S_IRWXU, stat.S_IRUSR | stat.S_IWUSR) |
| 623 | finally: |
| 624 | posix.close(f) |
| 625 | |
| 626 | @unittest.skipUnless(hasattr(posix, 'fchownat'), "test needs posix.fchownat()") |
| 627 | def test_fchownat(self): |
| 628 | support.unlink(support.TESTFN) |
| 629 | open(support.TESTFN, 'w').close() |
| 630 | |
| 631 | f = posix.open(posix.getcwd(), posix.O_RDONLY) |
| 632 | try: |
| 633 | posix.fchownat(f, support.TESTFN, os.getuid(), os.getgid()) |
| 634 | finally: |
| 635 | posix.close(f) |
| 636 | |
| 637 | @unittest.skipUnless(hasattr(posix, 'fstatat'), "test needs posix.fstatat()") |
| 638 | def test_fstatat(self): |
| 639 | support.unlink(support.TESTFN) |
| 640 | with open(support.TESTFN, 'w') as outfile: |
| 641 | outfile.write("testline\n") |
| 642 | |
| 643 | f = posix.open(posix.getcwd(), posix.O_RDONLY) |
| 644 | try: |
| 645 | s1 = posix.stat(support.TESTFN) |
| 646 | s2 = posix.fstatat(f, support.TESTFN) |
| 647 | self.assertEqual(s1, s2) |
| 648 | finally: |
| 649 | posix.close(f) |
| 650 | |
| 651 | @unittest.skipUnless(hasattr(posix, 'futimesat'), "test needs posix.futimesat()") |
| 652 | def test_futimesat(self): |
| 653 | f = posix.open(posix.getcwd(), posix.O_RDONLY) |
| 654 | try: |
| 655 | now = time.time() |
| 656 | posix.futimesat(f, support.TESTFN, None) |
| 657 | self.assertRaises(TypeError, posix.futimesat, f, support.TESTFN, (None, None)) |
| 658 | self.assertRaises(TypeError, posix.futimesat, f, support.TESTFN, (now, None)) |
| 659 | self.assertRaises(TypeError, posix.futimesat, f, support.TESTFN, (None, now)) |
| 660 | posix.futimesat(f, support.TESTFN, (int(now), int(now))) |
| 661 | posix.futimesat(f, support.TESTFN, (now, now)) |
| 662 | finally: |
| 663 | posix.close(f) |
| 664 | |
| 665 | @unittest.skipUnless(hasattr(posix, 'linkat'), "test needs posix.linkat()") |
| 666 | def test_linkat(self): |
| 667 | f = posix.open(posix.getcwd(), posix.O_RDONLY) |
| 668 | try: |
| 669 | posix.linkat(f, support.TESTFN, f, support.TESTFN + 'link') |
| 670 | # should have same inodes |
| 671 | self.assertEqual(posix.stat(support.TESTFN)[1], |
| 672 | posix.stat(support.TESTFN + 'link')[1]) |
| 673 | finally: |
| 674 | posix.close(f) |
| 675 | support.unlink(support.TESTFN + 'link') |
| 676 | |
| 677 | @unittest.skipUnless(hasattr(posix, 'mkdirat'), "test needs posix.mkdirat()") |
| 678 | def test_mkdirat(self): |
| 679 | f = posix.open(posix.getcwd(), posix.O_RDONLY) |
| 680 | try: |
| 681 | posix.mkdirat(f, support.TESTFN + 'dir') |
| 682 | posix.stat(support.TESTFN + 'dir') # should not raise exception |
| 683 | finally: |
| 684 | posix.close(f) |
| 685 | support.rmtree(support.TESTFN + 'dir') |
| 686 | |
| 687 | @unittest.skipUnless(hasattr(posix, 'mknodat') and hasattr(stat, 'S_IFIFO'), |
| 688 | "don't have mknodat()/S_IFIFO") |
| 689 | def test_mknodat(self): |
| 690 | # Test using mknodat() to create a FIFO (the only use specified |
| 691 | # by POSIX). |
| 692 | support.unlink(support.TESTFN) |
| 693 | mode = stat.S_IFIFO | stat.S_IRUSR | stat.S_IWUSR |
| 694 | f = posix.open(posix.getcwd(), posix.O_RDONLY) |
| 695 | try: |
| 696 | posix.mknodat(f, support.TESTFN, mode, 0) |
| 697 | except OSError as e: |
| 698 | # Some old systems don't allow unprivileged users to use |
| 699 | # mknod(), or only support creating device nodes. |
| 700 | self.assertIn(e.errno, (errno.EPERM, errno.EINVAL)) |
| 701 | else: |
| 702 | self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode)) |
| 703 | finally: |
| 704 | posix.close(f) |
| 705 | |
| 706 | @unittest.skipUnless(hasattr(posix, 'openat'), "test needs posix.openat()") |
| 707 | def test_openat(self): |
| 708 | support.unlink(support.TESTFN) |
| 709 | with open(support.TESTFN, 'w') as outfile: |
| 710 | outfile.write("testline\n") |
| 711 | a = posix.open(posix.getcwd(), posix.O_RDONLY) |
| 712 | b = posix.openat(a, support.TESTFN, posix.O_RDONLY) |
| 713 | try: |
| 714 | res = posix.read(b, 9).decode(encoding="utf-8") |
| 715 | self.assertEqual("testline\n", res) |
| 716 | finally: |
| 717 | posix.close(a) |
| 718 | posix.close(b) |
| 719 | |
| 720 | @unittest.skipUnless(hasattr(posix, 'readlinkat'), "test needs posix.readlinkat()") |
| 721 | def test_readlinkat(self): |
| 722 | os.symlink(support.TESTFN, support.TESTFN + 'link') |
| 723 | f = posix.open(posix.getcwd(), posix.O_RDONLY) |
| 724 | try: |
| 725 | self.assertEqual(posix.readlink(support.TESTFN + 'link'), |
| 726 | posix.readlinkat(f, support.TESTFN + 'link')) |
| 727 | finally: |
| 728 | support.unlink(support.TESTFN + 'link') |
| 729 | posix.close(f) |
| 730 | |
| 731 | @unittest.skipUnless(hasattr(posix, 'renameat'), "test needs posix.renameat()") |
| 732 | def test_renameat(self): |
| 733 | support.unlink(support.TESTFN) |
| 734 | open(support.TESTFN + 'ren', 'w').close() |
| 735 | f = posix.open(posix.getcwd(), posix.O_RDONLY) |
| 736 | try: |
| 737 | posix.renameat(f, support.TESTFN + 'ren', f, support.TESTFN) |
| 738 | except: |
| 739 | posix.rename(support.TESTFN + 'ren', support.TESTFN) |
| 740 | raise |
| 741 | else: |
| 742 | posix.stat(support.TESTFN) # should not throw exception |
| 743 | finally: |
| 744 | posix.close(f) |
| 745 | |
| 746 | @unittest.skipUnless(hasattr(posix, 'symlinkat'), "test needs posix.symlinkat()") |
| 747 | def test_symlinkat(self): |
| 748 | f = posix.open(posix.getcwd(), posix.O_RDONLY) |
| 749 | try: |
| 750 | posix.symlinkat(support.TESTFN, f, support.TESTFN + 'link') |
| 751 | self.assertEqual(posix.readlink(support.TESTFN + 'link'), support.TESTFN) |
| 752 | finally: |
| 753 | posix.close(f) |
| 754 | support.unlink(support.TESTFN + 'link') |
| 755 | |
| 756 | @unittest.skipUnless(hasattr(posix, 'unlinkat'), "test needs posix.unlinkat()") |
| 757 | def test_unlinkat(self): |
| 758 | f = posix.open(posix.getcwd(), posix.O_RDONLY) |
| 759 | open(support.TESTFN + 'del', 'w').close() |
| 760 | posix.stat(support.TESTFN + 'del') # should not throw exception |
| 761 | try: |
| 762 | posix.unlinkat(f, support.TESTFN + 'del') |
| 763 | except: |
| 764 | support.unlink(support.TESTFN + 'del') |
| 765 | raise |
| 766 | else: |
| 767 | self.assertRaises(OSError, posix.stat, support.TESTFN + 'link') |
| 768 | finally: |
| 769 | posix.close(f) |
| 770 | |
| 771 | @unittest.skipUnless(hasattr(posix, 'utimensat'), "test needs posix.utimensat()") |
| 772 | def test_utimensat(self): |
| 773 | f = posix.open(posix.getcwd(), posix.O_RDONLY) |
| 774 | try: |
| 775 | now = time.time() |
| 776 | posix.utimensat(f, support.TESTFN, None, None) |
| 777 | self.assertRaises(TypeError, posix.utimensat, f, support.TESTFN, (None, None), (None, None)) |
| 778 | self.assertRaises(TypeError, posix.utimensat, f, support.TESTFN, (now, 0), None) |
| 779 | self.assertRaises(TypeError, posix.utimensat, f, support.TESTFN, None, (now, 0)) |
| 780 | posix.utimensat(f, support.TESTFN, (int(now), int((now - int(now)) * 1e9)), |
| 781 | (int(now), int((now - int(now)) * 1e9))) |
| 782 | finally: |
| 783 | posix.close(f) |
| 784 | |
| 785 | @unittest.skipUnless(hasattr(posix, 'mkfifoat'), "don't have mkfifoat()") |
| 786 | def test_mkfifoat(self): |
| 787 | support.unlink(support.TESTFN) |
| 788 | f = posix.open(posix.getcwd(), posix.O_RDONLY) |
| 789 | try: |
| 790 | posix.mkfifoat(f, support.TESTFN, stat.S_IRUSR | stat.S_IWUSR) |
| 791 | self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode)) |
| 792 | finally: |
| 793 | posix.close(f) |
| 794 | |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 795 | class PosixGroupsTester(unittest.TestCase): |
| 796 | |
| 797 | def setUp(self): |
| 798 | if posix.getuid() != 0: |
| 799 | raise unittest.SkipTest("not enough privileges") |
| 800 | if not hasattr(posix, 'getgroups'): |
| 801 | raise unittest.SkipTest("need posix.getgroups") |
| 802 | if sys.platform == 'darwin': |
| 803 | raise unittest.SkipTest("getgroups(2) is broken on OSX") |
| 804 | self.saved_groups = posix.getgroups() |
| 805 | |
| 806 | def tearDown(self): |
| 807 | if hasattr(posix, 'setgroups'): |
| 808 | posix.setgroups(self.saved_groups) |
| 809 | elif hasattr(posix, 'initgroups'): |
| 810 | name = pwd.getpwuid(posix.getuid()).pw_name |
| 811 | posix.initgroups(name, self.saved_groups[0]) |
| 812 | |
| 813 | @unittest.skipUnless(hasattr(posix, 'initgroups'), |
| 814 | "test needs posix.initgroups()") |
| 815 | def test_initgroups(self): |
| 816 | # find missing group |
| 817 | |
Antoine Pitrou | e5a9101 | 2010-09-04 17:32:06 +0000 | [diff] [blame] | 818 | g = max(self.saved_groups) + 1 |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 819 | name = pwd.getpwuid(posix.getuid()).pw_name |
| 820 | posix.initgroups(name, g) |
| 821 | self.assertIn(g, posix.getgroups()) |
| 822 | |
| 823 | @unittest.skipUnless(hasattr(posix, 'setgroups'), |
| 824 | "test needs posix.setgroups()") |
| 825 | def test_setgroups(self): |
Antoine Pitrou | e5a9101 | 2010-09-04 17:32:06 +0000 | [diff] [blame] | 826 | for groups in [[0], list(range(16))]: |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 827 | posix.setgroups(groups) |
| 828 | self.assertListEqual(groups, posix.getgroups()) |
| 829 | |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 830 | |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 831 | def test_main(): |
Antoine Pitrou | 68c9592 | 2011-03-20 17:33:57 +0100 | [diff] [blame] | 832 | try: |
| 833 | support.run_unittest(PosixTester, PosixGroupsTester) |
| 834 | finally: |
| 835 | support.reap_children() |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 836 | |
| 837 | if __name__ == '__main__': |
| 838 | test_main() |