blob: d2c768afca4b04bb0c2bccc156a14c54f45d4bf8 [file] [log] [blame]
Neal Norwitze241ce82003-02-17 18:17:05 +00001"Test posix functions"
2
Walter Dörwald21d3a322003-05-01 17:45:56 +00003from test import test_support
Neal Norwitze241ce82003-02-17 18:17:05 +00004
R. David Murray95fb46c2009-04-21 13:06:04 +00005# Skip these tests if there is no posix module.
6posix = test_support.import_module('posix')
Neal Norwitze241ce82003-02-17 18:17:05 +00007
Antoine Pitrou30b3b352009-12-02 20:37:54 +00008import errno
Neal Norwitze241ce82003-02-17 18:17:05 +00009import time
10import os
Gregory P. Smithf48da8f2008-03-18 19:05:32 +000011import pwd
Facundo Batista5596b0c2008-06-22 13:36:20 +000012import shutil
Stefan Krah182ae642010-07-13 19:17:08 +000013import sys
Neal Norwitze241ce82003-02-17 18:17:05 +000014import unittest
15import warnings
R. David Murray59beec32009-03-30 19:04:00 +000016
R. David Murray59beec32009-03-30 19:04:00 +000017
Neal Norwitze241ce82003-02-17 18:17:05 +000018warnings.filterwarnings('ignore', '.* potential security risk .*',
19 RuntimeWarning)
20
21class PosixTester(unittest.TestCase):
22
23 def setUp(self):
24 # create empty file
Walter Dörwald21d3a322003-05-01 17:45:56 +000025 fp = open(test_support.TESTFN, 'w+')
Neal Norwitze241ce82003-02-17 18:17:05 +000026 fp.close()
27
28 def tearDown(self):
Walter Dörwald21d3a322003-05-01 17:45:56 +000029 os.unlink(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +000030
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 Norwitz71b13e82003-02-23 22:12:24 +000035 "times", "getloadavg", "tmpnam",
Neal Norwitze241ce82003-02-17 18:17:05 +000036 "getegid", "geteuid", "getgid", "getgroups",
37 "getpid", "getpgrp", "getppid", "getuid",
38 ]
Neal Norwitz71b13e82003-02-23 22:12:24 +000039
Neal Norwitze241ce82003-02-17 18:17:05 +000040 for name in NO_ARG_FUNCTIONS:
41 posix_func = getattr(posix, name, None)
42 if posix_func is not None:
43 posix_func()
Neal Norwitz2ff51a82003-02-17 22:40:31 +000044 self.assertRaises(TypeError, posix_func, 1)
Neal Norwitze241ce82003-02-17 18:17:05 +000045
Martin v. Löwis50ea4562009-11-27 13:56:01 +000046 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 Pitrou30b3b352009-12-02 20:37:54 +000088 @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 Norwitze241ce82003-02-17 18:17:05 +0000109 def test_statvfs(self):
110 if hasattr(posix, 'statvfs'):
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000111 self.assertTrue(posix.statvfs(os.curdir))
Neal Norwitze241ce82003-02-17 18:17:05 +0000112
113 def test_fstatvfs(self):
114 if hasattr(posix, 'fstatvfs'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000115 fp = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000116 try:
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000117 self.assertTrue(posix.fstatvfs(fp.fileno()))
Neal Norwitze241ce82003-02-17 18:17:05 +0000118 finally:
119 fp.close()
120
121 def test_ftruncate(self):
122 if hasattr(posix, 'ftruncate'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000123 fp = open(test_support.TESTFN, 'w+')
Neal Norwitze241ce82003-02-17 18:17:05 +0000124 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örwald21d3a322003-05-01 17:45:56 +0000134 fp = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000135 try:
136 fd = posix.dup(fp.fileno())
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000137 self.assertIsInstance(fd, int)
Neal Norwitze241ce82003-02-17 18:17:05 +0000138 os.close(fd)
139 finally:
140 fp.close()
141
Skip Montanaro94785ef2006-04-20 01:29:48 +0000142 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 Norwitze241ce82003-02-17 18:17:05 +0000147 def test_dup2(self):
148 if hasattr(posix, 'dup2'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000149 fp1 = open(test_support.TESTFN)
150 fp2 = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000151 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örwald21d3a322003-05-01 17:45:56 +0000158 fd = os.open(test_support.TESTFN, os.O_RDONLY)
Neal Norwitze241ce82003-02-17 18:17:05 +0000159 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 Montanaro98470002005-06-17 01:14:49 +0000168 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 Norwitze241ce82003-02-17 18:17:05 +0000199 def test_fstat(self):
200 if hasattr(posix, 'fstat'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000201 fp = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000202 try:
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000203 self.assertTrue(posix.fstat(fp.fileno()))
Neal Norwitze241ce82003-02-17 18:17:05 +0000204 finally:
205 fp.close()
206
207 def test_stat(self):
208 if hasattr(posix, 'stat'):
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000209 self.assertTrue(posix.stat(test_support.TESTFN))
Neal Norwitze241ce82003-02-17 18:17:05 +0000210
Gregory P. Smith9f12d462009-12-23 09:31:11 +0000211 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. Smithf48da8f2008-03-18 19:05:32 +0000228
Gregory P. Smith9f12d462009-12-23 09:31:11 +0000229 # test a successful chown call
230 chown_func(first_param, os.getuid(), os.getgid())
Gregory P. Smithf48da8f2008-03-18 19:05:32 +0000231
Gregory P. Smith9f12d462009-12-23 09:31:11 +0000232 @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. Smithf48da8f2008-03-18 19:05:32 +0000260
Neal Norwitze241ce82003-02-17 18:17:05 +0000261 def test_chdir(self):
262 if hasattr(posix, 'chdir'):
263 posix.chdir(os.curdir)
Walter Dörwald21d3a322003-05-01 17:45:56 +0000264 self.assertRaises(OSError, posix.chdir, test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000265
266 def test_lsdir(self):
267 if hasattr(posix, 'lsdir'):
Ezio Melottiaa980582010-01-23 23:04:36 +0000268 self.assertIn(test_support.TESTFN, posix.lsdir(os.curdir))
Neal Norwitze241ce82003-02-17 18:17:05 +0000269
270 def test_access(self):
271 if hasattr(posix, 'access'):
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000272 self.assertTrue(posix.access(test_support.TESTFN, os.R_OK))
Neal Norwitze241ce82003-02-17 18:17:05 +0000273
274 def test_umask(self):
275 if hasattr(posix, 'umask'):
276 old_mask = posix.umask(0)
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000277 self.assertIsInstance(old_mask, int)
Neal Norwitze241ce82003-02-17 18:17:05 +0000278 posix.umask(old_mask)
279
280 def test_strerror(self):
281 if hasattr(posix, 'strerror'):
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000282 self.assertTrue(posix.strerror(0))
Neal Norwitze241ce82003-02-17 18:17:05 +0000283
284 def test_pipe(self):
285 if hasattr(posix, 'pipe'):
Antoine Pitroubba8f2d2010-04-10 23:32:12 +0000286 reader, writer = posix.pipe()
287 os.close(reader)
288 os.close(writer)
Neal Norwitze241ce82003-02-17 18:17:05 +0000289
290 def test_tempnam(self):
291 if hasattr(posix, 'tempnam'):
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000292 self.assertTrue(posix.tempnam())
293 self.assertTrue(posix.tempnam(os.curdir))
294 self.assertTrue(posix.tempnam(os.curdir, 'blah'))
Neal Norwitze241ce82003-02-17 18:17:05 +0000295
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örwald21d3a322003-05-01 17:45:56 +0000304 posix.utime(test_support.TESTFN, None)
Neal Norwitzc28e7ad2004-06-06 20:27:05 +0000305 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örwald21d3a322003-05-01 17:45:56 +0000309 posix.utime(test_support.TESTFN, (now, now))
Neal Norwitze241ce82003-02-17 18:17:05 +0000310
Martin v. Löwis382abef2007-02-19 10:55:19 +0000311 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 Batista5596b0c2008-06-22 13:36:20 +0000323 def test_getcwd_long_pathnames(self):
324 if hasattr(posix, 'getcwd'):
325 dirname = 'getcwd-test-directory-0123456789abcdef-01234567890abcdef'
326 curdir = os.getcwd()
Facundo Batista96f3dc32008-06-22 18:23:55 +0000327 base_path = os.path.abspath(test_support.TESTFN) + '.getcwd'
Facundo Batista5596b0c2008-06-22 13:36:20 +0000328
329 try:
330 os.mkdir(base_path)
331 os.chdir(base_path)
Facundo Batista96f3dc32008-06-22 18:23:55 +0000332 except:
Benjamin Peterson888a39b2009-03-26 20:48:25 +0000333# Just returning nothing instead of the SkipTest exception,
Facundo Batista2694eb02008-06-22 19:35:24 +0000334# because the test results in Error in that case.
335# Is that ok?
Benjamin Peterson888a39b2009-03-26 20:48:25 +0000336# raise unittest.SkipTest, "cannot create directory for testing"
Facundo Batista2694eb02008-06-22 19:35:24 +0000337 return
Facundo Batista5596b0c2008-06-22 13:36:20 +0000338
Facundo Batista96f3dc32008-06-22 18:23:55 +0000339 try:
Facundo Batista5596b0c2008-06-22 13:36:20 +0000340 def _create_and_do_getcwd(dirname, current_path_length = 0):
341 try:
342 os.mkdir(dirname)
343 except:
Benjamin Peterson888a39b2009-03-26 20:48:25 +0000344 raise unittest.SkipTest, "mkdir cannot create directory sufficiently deep for getcwd test"
Facundo Batista5596b0c2008-06-22 13:36:20 +0000345
346 os.chdir(dirname)
347 try:
348 os.getcwd()
Stefan Krah182ae642010-07-13 19:17:08 +0000349 if current_path_length < 4099:
Facundo Batista5596b0c2008-06-22 13:36:20 +0000350 _create_and_do_getcwd(dirname, current_path_length + len(dirname) + 1)
Stefan Krah182ae642010-07-13 19:17:08 +0000351 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 Batista5596b0c2008-06-22 13:36:20 +0000356 finally:
357 os.chdir('..')
358 os.rmdir(dirname)
359
360 _create_and_do_getcwd(dirname)
361
362 finally:
Facundo Batista5596b0c2008-06-22 13:36:20 +0000363 os.chdir(curdir)
R. David Murrayb0c828a2009-07-09 18:41:03 +0000364 shutil.rmtree(base_path)
Facundo Batista5596b0c2008-06-22 13:36:20 +0000365
366
Neal Norwitze241ce82003-02-17 18:17:05 +0000367def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000368 test_support.run_unittest(PosixTester)
Neal Norwitze241ce82003-02-17 18:17:05 +0000369
370if __name__ == '__main__':
371 test_main()