Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1 | "Test posix functions" |
| 2 | |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 3 | from test import test_support |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4 | |
| 5 | try: |
| 6 | import posix |
| 7 | except ImportError: |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 8 | raise test_support.TestSkipped, "posix is not available" |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 9 | |
| 10 | import time |
| 11 | import os |
Gregory P. Smith | f48da8f | 2008-03-18 19:05:32 +0000 | [diff] [blame] | 12 | import pwd |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 13 | import shutil |
Stefan Krah | 36db84d | 2010-07-19 15:43:23 +0000 | [diff] [blame^] | 14 | import sys |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 15 | import unittest |
| 16 | import warnings |
| 17 | warnings.filterwarnings('ignore', '.* potential security risk .*', |
| 18 | RuntimeWarning) |
| 19 | |
| 20 | class PosixTester(unittest.TestCase): |
| 21 | |
| 22 | def setUp(self): |
| 23 | # create empty file |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 24 | fp = open(test_support.TESTFN, 'w+') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 25 | fp.close() |
| 26 | |
| 27 | def tearDown(self): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 28 | os.unlink(test_support.TESTFN) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 29 | |
| 30 | def testNoArgFunctions(self): |
| 31 | # test posix functions which take no arguments and have |
| 32 | # no side-effects which we need to cleanup (e.g., fork, wait, abort) |
| 33 | NO_ARG_FUNCTIONS = [ "ctermid", "getcwd", "getcwdu", "uname", |
Neal Norwitz | 71b13e8 | 2003-02-23 22:12:24 +0000 | [diff] [blame] | 34 | "times", "getloadavg", "tmpnam", |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 35 | "getegid", "geteuid", "getgid", "getgroups", |
| 36 | "getpid", "getpgrp", "getppid", "getuid", |
| 37 | ] |
Neal Norwitz | 71b13e8 | 2003-02-23 22:12:24 +0000 | [diff] [blame] | 38 | |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 39 | for name in NO_ARG_FUNCTIONS: |
| 40 | posix_func = getattr(posix, name, None) |
| 41 | if posix_func is not None: |
| 42 | posix_func() |
Neal Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 43 | self.assertRaises(TypeError, posix_func, 1) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 44 | |
| 45 | def test_statvfs(self): |
| 46 | if hasattr(posix, 'statvfs'): |
Neal Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 47 | self.assert_(posix.statvfs(os.curdir)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 48 | |
| 49 | def test_fstatvfs(self): |
| 50 | if hasattr(posix, 'fstatvfs'): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 51 | fp = open(test_support.TESTFN) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 52 | try: |
Neal Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 53 | self.assert_(posix.fstatvfs(fp.fileno())) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 54 | finally: |
| 55 | fp.close() |
| 56 | |
| 57 | def test_ftruncate(self): |
| 58 | if hasattr(posix, 'ftruncate'): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 59 | fp = open(test_support.TESTFN, 'w+') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 60 | try: |
| 61 | # we need to have some data to truncate |
| 62 | fp.write('test') |
| 63 | fp.flush() |
| 64 | posix.ftruncate(fp.fileno(), 0) |
| 65 | finally: |
| 66 | fp.close() |
| 67 | |
| 68 | def test_dup(self): |
| 69 | if hasattr(posix, 'dup'): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 70 | fp = open(test_support.TESTFN) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 71 | try: |
| 72 | fd = posix.dup(fp.fileno()) |
Neal Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 73 | self.assert_(isinstance(fd, int)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 74 | os.close(fd) |
| 75 | finally: |
| 76 | fp.close() |
| 77 | |
Skip Montanaro | 94785ef | 2006-04-20 01:29:48 +0000 | [diff] [blame] | 78 | def test_confstr(self): |
| 79 | if hasattr(posix, 'confstr'): |
| 80 | self.assertRaises(ValueError, posix.confstr, "CS_garbage") |
| 81 | self.assertEqual(len(posix.confstr("CS_PATH")) > 0, True) |
| 82 | |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 83 | def test_dup2(self): |
| 84 | if hasattr(posix, 'dup2'): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 85 | fp1 = open(test_support.TESTFN) |
| 86 | fp2 = open(test_support.TESTFN) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 87 | try: |
| 88 | posix.dup2(fp1.fileno(), fp2.fileno()) |
| 89 | finally: |
| 90 | fp1.close() |
| 91 | fp2.close() |
| 92 | |
| 93 | def fdopen_helper(self, *args): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 94 | fd = os.open(test_support.TESTFN, os.O_RDONLY) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 95 | fp2 = posix.fdopen(fd, *args) |
| 96 | fp2.close() |
| 97 | |
| 98 | def test_fdopen(self): |
| 99 | if hasattr(posix, 'fdopen'): |
| 100 | self.fdopen_helper() |
| 101 | self.fdopen_helper('r') |
| 102 | self.fdopen_helper('r', 100) |
| 103 | |
Skip Montanaro | 9847000 | 2005-06-17 01:14:49 +0000 | [diff] [blame] | 104 | def test_osexlock(self): |
| 105 | if hasattr(posix, "O_EXLOCK"): |
| 106 | fd = os.open(test_support.TESTFN, |
| 107 | os.O_WRONLY|os.O_EXLOCK|os.O_CREAT) |
| 108 | self.assertRaises(OSError, os.open, test_support.TESTFN, |
| 109 | os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK) |
| 110 | os.close(fd) |
| 111 | |
| 112 | if hasattr(posix, "O_SHLOCK"): |
| 113 | fd = os.open(test_support.TESTFN, |
| 114 | os.O_WRONLY|os.O_SHLOCK|os.O_CREAT) |
| 115 | self.assertRaises(OSError, os.open, test_support.TESTFN, |
| 116 | os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK) |
| 117 | os.close(fd) |
| 118 | |
| 119 | def test_osshlock(self): |
| 120 | if hasattr(posix, "O_SHLOCK"): |
| 121 | fd1 = os.open(test_support.TESTFN, |
| 122 | os.O_WRONLY|os.O_SHLOCK|os.O_CREAT) |
| 123 | fd2 = os.open(test_support.TESTFN, |
| 124 | os.O_WRONLY|os.O_SHLOCK|os.O_CREAT) |
| 125 | os.close(fd2) |
| 126 | os.close(fd1) |
| 127 | |
| 128 | if hasattr(posix, "O_EXLOCK"): |
| 129 | fd = os.open(test_support.TESTFN, |
| 130 | os.O_WRONLY|os.O_SHLOCK|os.O_CREAT) |
| 131 | self.assertRaises(OSError, os.open, test_support.TESTFN, |
| 132 | os.O_RDONLY|os.O_EXLOCK|os.O_NONBLOCK) |
| 133 | os.close(fd) |
| 134 | |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 135 | def test_fstat(self): |
| 136 | if hasattr(posix, 'fstat'): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 137 | fp = open(test_support.TESTFN) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 138 | try: |
Neal Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 139 | self.assert_(posix.fstat(fp.fileno())) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 140 | finally: |
| 141 | fp.close() |
| 142 | |
| 143 | def test_stat(self): |
| 144 | if hasattr(posix, 'stat'): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 145 | self.assert_(posix.stat(test_support.TESTFN)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 146 | |
Gregory P. Smith | 21c134d | 2009-12-23 09:46:53 +0000 | [diff] [blame] | 147 | def _test_all_chown_common(self, chown_func, first_param): |
| 148 | """Common code for chown, fchown and lchown tests.""" |
| 149 | if os.getuid() == 0: |
| 150 | try: |
| 151 | # Many linux distros have a nfsnobody user as MAX_UID-2 |
| 152 | # that makes a good test case for signedness issues. |
| 153 | # http://bugs.python.org/issue1747858 |
| 154 | # This part of the test only runs when run as root. |
| 155 | # Only scary people run their tests as root. |
| 156 | ent = pwd.getpwnam('nfsnobody') |
| 157 | chown_func(first_param, ent.pw_uid, ent.pw_gid) |
| 158 | except KeyError: |
| 159 | pass |
| 160 | else: |
| 161 | # non-root cannot chown to root, raises OSError |
| 162 | self.assertRaises(OSError, chown_func, |
| 163 | first_param, 0, 0) |
| 164 | |
| 165 | # test a successful chown call |
| 166 | chown_func(first_param, os.getuid(), os.getgid()) |
| 167 | |
| 168 | def _test_chown(self): |
| 169 | # raise an OSError if the file does not exist |
| 170 | os.unlink(test_support.TESTFN) |
| 171 | self.assertRaises(OSError, posix.chown, test_support.TESTFN, -1, -1) |
| 172 | |
| 173 | # re-create the file |
| 174 | open(test_support.TESTFN, 'w').close() |
| 175 | self._test_all_chown_common(posix.chown, test_support.TESTFN) |
| 176 | |
Gregory P. Smith | f48da8f | 2008-03-18 19:05:32 +0000 | [diff] [blame] | 177 | if hasattr(posix, 'chown'): |
Gregory P. Smith | 21c134d | 2009-12-23 09:46:53 +0000 | [diff] [blame] | 178 | test_chown = _test_chown |
Gregory P. Smith | f48da8f | 2008-03-18 19:05:32 +0000 | [diff] [blame] | 179 | |
Gregory P. Smith | 21c134d | 2009-12-23 09:46:53 +0000 | [diff] [blame] | 180 | def _test_fchown(self): |
| 181 | os.unlink(test_support.TESTFN) |
Gregory P. Smith | f48da8f | 2008-03-18 19:05:32 +0000 | [diff] [blame] | 182 | |
Gregory P. Smith | 21c134d | 2009-12-23 09:46:53 +0000 | [diff] [blame] | 183 | # re-create the file |
| 184 | test_file = open(test_support.TESTFN, 'w') |
| 185 | try: |
| 186 | fd = test_file.fileno() |
| 187 | self._test_all_chown_common(posix.fchown, fd) |
| 188 | finally: |
| 189 | test_file.close() |
| 190 | |
| 191 | if hasattr(posix, 'fchown'): |
| 192 | test_fchown = _test_fchown |
| 193 | |
| 194 | def _test_lchown(self): |
| 195 | os.unlink(test_support.TESTFN) |
| 196 | # create a symlink |
| 197 | os.symlink('/tmp/dummy-symlink-target', test_support.TESTFN) |
| 198 | self._test_all_chown_common(posix.lchown, test_support.TESTFN) |
| 199 | |
| 200 | if hasattr(posix, 'lchown'): |
| 201 | test_lchown = _test_lchown |
Gregory P. Smith | f48da8f | 2008-03-18 19:05:32 +0000 | [diff] [blame] | 202 | |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 203 | def test_chdir(self): |
| 204 | if hasattr(posix, 'chdir'): |
| 205 | posix.chdir(os.curdir) |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 206 | self.assertRaises(OSError, posix.chdir, test_support.TESTFN) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 207 | |
| 208 | def test_lsdir(self): |
| 209 | if hasattr(posix, 'lsdir'): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 210 | self.assert_(test_support.TESTFN in posix.lsdir(os.curdir)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 211 | |
| 212 | def test_access(self): |
| 213 | if hasattr(posix, 'access'): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 214 | self.assert_(posix.access(test_support.TESTFN, os.R_OK)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 215 | |
| 216 | def test_umask(self): |
| 217 | if hasattr(posix, 'umask'): |
| 218 | old_mask = posix.umask(0) |
Neal Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 219 | self.assert_(isinstance(old_mask, int)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 220 | posix.umask(old_mask) |
| 221 | |
| 222 | def test_strerror(self): |
| 223 | if hasattr(posix, 'strerror'): |
Neal Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 224 | self.assert_(posix.strerror(0)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 225 | |
| 226 | def test_pipe(self): |
| 227 | if hasattr(posix, 'pipe'): |
| 228 | reader, writer = posix.pipe() |
| 229 | os.close(reader) |
| 230 | os.close(writer) |
| 231 | |
| 232 | def test_tempnam(self): |
| 233 | if hasattr(posix, 'tempnam'): |
Neal Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 234 | self.assert_(posix.tempnam()) |
| 235 | self.assert_(posix.tempnam(os.curdir)) |
| 236 | self.assert_(posix.tempnam(os.curdir, 'blah')) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 237 | |
| 238 | def test_tmpfile(self): |
| 239 | if hasattr(posix, 'tmpfile'): |
| 240 | fp = posix.tmpfile() |
| 241 | fp.close() |
| 242 | |
| 243 | def test_utime(self): |
| 244 | if hasattr(posix, 'utime'): |
| 245 | now = time.time() |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 246 | posix.utime(test_support.TESTFN, None) |
Neal Norwitz | c28e7ad | 2004-06-06 20:27:05 +0000 | [diff] [blame] | 247 | self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, None)) |
| 248 | self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (now, None)) |
| 249 | self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, now)) |
| 250 | posix.utime(test_support.TESTFN, (int(now), int(now))) |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 251 | posix.utime(test_support.TESTFN, (now, now)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 252 | |
Martin v. Löwis | 382abef | 2007-02-19 10:55:19 +0000 | [diff] [blame] | 253 | def test_chflags(self): |
| 254 | if hasattr(posix, 'chflags'): |
| 255 | st = os.stat(test_support.TESTFN) |
| 256 | if hasattr(st, 'st_flags'): |
| 257 | posix.chflags(test_support.TESTFN, st.st_flags) |
| 258 | |
| 259 | def test_lchflags(self): |
| 260 | if hasattr(posix, 'lchflags'): |
| 261 | st = os.stat(test_support.TESTFN) |
| 262 | if hasattr(st, 'st_flags'): |
| 263 | posix.lchflags(test_support.TESTFN, st.st_flags) |
| 264 | |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 265 | def test_getcwd_long_pathnames(self): |
| 266 | if hasattr(posix, 'getcwd'): |
| 267 | dirname = 'getcwd-test-directory-0123456789abcdef-01234567890abcdef' |
| 268 | curdir = os.getcwd() |
Facundo Batista | 96f3dc3 | 2008-06-22 18:23:55 +0000 | [diff] [blame] | 269 | base_path = os.path.abspath(test_support.TESTFN) + '.getcwd' |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 270 | |
| 271 | try: |
| 272 | os.mkdir(base_path) |
| 273 | os.chdir(base_path) |
Facundo Batista | 96f3dc3 | 2008-06-22 18:23:55 +0000 | [diff] [blame] | 274 | except: |
Facundo Batista | 2694eb0 | 2008-06-22 19:35:24 +0000 | [diff] [blame] | 275 | # Just returning nothing instead of the TestSkipped exception, |
| 276 | # because the test results in Error in that case. |
| 277 | # Is that ok? |
| 278 | # raise test_support.TestSkipped, "cannot create directory for testing" |
| 279 | return |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 280 | |
Facundo Batista | 96f3dc3 | 2008-06-22 18:23:55 +0000 | [diff] [blame] | 281 | try: |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 282 | def _create_and_do_getcwd(dirname, current_path_length = 0): |
| 283 | try: |
| 284 | os.mkdir(dirname) |
| 285 | except: |
| 286 | raise test_support.TestSkipped, "mkdir cannot create directory sufficiently deep for getcwd test" |
| 287 | |
| 288 | os.chdir(dirname) |
| 289 | try: |
| 290 | os.getcwd() |
Stefan Krah | 36db84d | 2010-07-19 15:43:23 +0000 | [diff] [blame^] | 291 | if current_path_length < 4099: |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 292 | _create_and_do_getcwd(dirname, current_path_length + len(dirname) + 1) |
Stefan Krah | 36db84d | 2010-07-19 15:43:23 +0000 | [diff] [blame^] | 293 | except OSError as e: |
| 294 | expected_errno = errno.ENAMETOOLONG |
| 295 | if 'sunos' in sys.platform or 'openbsd' in sys.platform: |
| 296 | expected_errno = errno.ERANGE # Issue 9185 |
| 297 | self.assertEqual(e.errno, expected_errno) |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 298 | finally: |
| 299 | os.chdir('..') |
| 300 | os.rmdir(dirname) |
| 301 | |
| 302 | _create_and_do_getcwd(dirname) |
| 303 | |
| 304 | finally: |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 305 | os.chdir(curdir) |
R. David Murray | 8bb57b7 | 2009-07-09 20:13:41 +0000 | [diff] [blame] | 306 | shutil.rmtree(base_path) |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 307 | |
| 308 | |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 309 | def test_main(): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 310 | test_support.run_unittest(PosixTester) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 311 | |
| 312 | if __name__ == '__main__': |
| 313 | test_main() |