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