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