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