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