blob: fb830ae9ece15c4a4c267c51ee101a313e24c3d0 [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
5try:
6 import posix
7except ImportError:
Walter Dörwald21d3a322003-05-01 17:45:56 +00008 raise test_support.TestSkipped, "posix is not available"
Neal Norwitze241ce82003-02-17 18:17:05 +00009
Stefan Krah200888f2010-07-19 18:15:41 +000010import errno
Ronald Oussorenac08e302010-07-24 10:05:19 +000011import sys
Neal Norwitze241ce82003-02-17 18:17:05 +000012import time
13import os
Gregory P. Smithf48da8f2008-03-18 19:05:32 +000014import pwd
Facundo Batista5596b0c2008-06-22 13:36:20 +000015import shutil
Stefan Krah36db84d2010-07-19 15:43:23 +000016import sys
Neal Norwitze241ce82003-02-17 18:17:05 +000017import unittest
18import warnings
19warnings.filterwarnings('ignore', '.* potential security risk .*',
20 RuntimeWarning)
21
22class PosixTester(unittest.TestCase):
23
24 def setUp(self):
25 # create empty file
Walter Dörwald21d3a322003-05-01 17:45:56 +000026 fp = open(test_support.TESTFN, 'w+')
Neal Norwitze241ce82003-02-17 18:17:05 +000027 fp.close()
28
29 def tearDown(self):
Walter Dörwald21d3a322003-05-01 17:45:56 +000030 os.unlink(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +000031
32 def testNoArgFunctions(self):
33 # test posix functions which take no arguments and have
34 # no side-effects which we need to cleanup (e.g., fork, wait, abort)
35 NO_ARG_FUNCTIONS = [ "ctermid", "getcwd", "getcwdu", "uname",
Neal Norwitz71b13e82003-02-23 22:12:24 +000036 "times", "getloadavg", "tmpnam",
Neal Norwitze241ce82003-02-17 18:17:05 +000037 "getegid", "geteuid", "getgid", "getgroups",
38 "getpid", "getpgrp", "getppid", "getuid",
39 ]
Neal Norwitz71b13e82003-02-23 22:12:24 +000040
Neal Norwitze241ce82003-02-17 18:17:05 +000041 for name in NO_ARG_FUNCTIONS:
42 posix_func = getattr(posix, name, None)
43 if posix_func is not None:
44 posix_func()
Neal Norwitz2ff51a82003-02-17 22:40:31 +000045 self.assertRaises(TypeError, posix_func, 1)
Neal Norwitze241ce82003-02-17 18:17:05 +000046
47 def test_statvfs(self):
48 if hasattr(posix, 'statvfs'):
Neal Norwitz2ff51a82003-02-17 22:40:31 +000049 self.assert_(posix.statvfs(os.curdir))
Neal Norwitze241ce82003-02-17 18:17:05 +000050
51 def test_fstatvfs(self):
52 if hasattr(posix, 'fstatvfs'):
Walter Dörwald21d3a322003-05-01 17:45:56 +000053 fp = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +000054 try:
Neal Norwitz2ff51a82003-02-17 22:40:31 +000055 self.assert_(posix.fstatvfs(fp.fileno()))
Neal Norwitze241ce82003-02-17 18:17:05 +000056 finally:
57 fp.close()
58
59 def test_ftruncate(self):
60 if hasattr(posix, 'ftruncate'):
Walter Dörwald21d3a322003-05-01 17:45:56 +000061 fp = open(test_support.TESTFN, 'w+')
Neal Norwitze241ce82003-02-17 18:17:05 +000062 try:
63 # we need to have some data to truncate
64 fp.write('test')
65 fp.flush()
66 posix.ftruncate(fp.fileno(), 0)
67 finally:
68 fp.close()
69
70 def test_dup(self):
71 if hasattr(posix, 'dup'):
Walter Dörwald21d3a322003-05-01 17:45:56 +000072 fp = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +000073 try:
74 fd = posix.dup(fp.fileno())
Neal Norwitz2ff51a82003-02-17 22:40:31 +000075 self.assert_(isinstance(fd, int))
Neal Norwitze241ce82003-02-17 18:17:05 +000076 os.close(fd)
77 finally:
78 fp.close()
79
Skip Montanaro94785ef2006-04-20 01:29:48 +000080 def test_confstr(self):
81 if hasattr(posix, 'confstr'):
82 self.assertRaises(ValueError, posix.confstr, "CS_garbage")
83 self.assertEqual(len(posix.confstr("CS_PATH")) > 0, True)
84
Neal Norwitze241ce82003-02-17 18:17:05 +000085 def test_dup2(self):
86 if hasattr(posix, 'dup2'):
Walter Dörwald21d3a322003-05-01 17:45:56 +000087 fp1 = open(test_support.TESTFN)
88 fp2 = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +000089 try:
90 posix.dup2(fp1.fileno(), fp2.fileno())
91 finally:
92 fp1.close()
93 fp2.close()
94
95 def fdopen_helper(self, *args):
Walter Dörwald21d3a322003-05-01 17:45:56 +000096 fd = os.open(test_support.TESTFN, os.O_RDONLY)
Neal Norwitze241ce82003-02-17 18:17:05 +000097 fp2 = posix.fdopen(fd, *args)
98 fp2.close()
99
100 def test_fdopen(self):
101 if hasattr(posix, 'fdopen'):
102 self.fdopen_helper()
103 self.fdopen_helper('r')
104 self.fdopen_helper('r', 100)
105
Skip Montanaro98470002005-06-17 01:14:49 +0000106 def test_osexlock(self):
107 if hasattr(posix, "O_EXLOCK"):
108 fd = os.open(test_support.TESTFN,
109 os.O_WRONLY|os.O_EXLOCK|os.O_CREAT)
110 self.assertRaises(OSError, os.open, test_support.TESTFN,
111 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
112 os.close(fd)
113
114 if hasattr(posix, "O_SHLOCK"):
115 fd = os.open(test_support.TESTFN,
116 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
117 self.assertRaises(OSError, os.open, test_support.TESTFN,
118 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
119 os.close(fd)
120
121 def test_osshlock(self):
122 if hasattr(posix, "O_SHLOCK"):
123 fd1 = os.open(test_support.TESTFN,
124 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
125 fd2 = os.open(test_support.TESTFN,
126 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
127 os.close(fd2)
128 os.close(fd1)
129
130 if hasattr(posix, "O_EXLOCK"):
131 fd = os.open(test_support.TESTFN,
132 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
133 self.assertRaises(OSError, os.open, test_support.TESTFN,
134 os.O_RDONLY|os.O_EXLOCK|os.O_NONBLOCK)
135 os.close(fd)
136
Neal Norwitze241ce82003-02-17 18:17:05 +0000137 def test_fstat(self):
138 if hasattr(posix, 'fstat'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000139 fp = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000140 try:
Neal Norwitz2ff51a82003-02-17 22:40:31 +0000141 self.assert_(posix.fstat(fp.fileno()))
Neal Norwitze241ce82003-02-17 18:17:05 +0000142 finally:
143 fp.close()
144
145 def test_stat(self):
146 if hasattr(posix, 'stat'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000147 self.assert_(posix.stat(test_support.TESTFN))
Neal Norwitze241ce82003-02-17 18:17:05 +0000148
Gregory P. Smith21c134d2009-12-23 09:46:53 +0000149 def _test_all_chown_common(self, chown_func, first_param):
150 """Common code for chown, fchown and lchown tests."""
151 if os.getuid() == 0:
152 try:
153 # Many linux distros have a nfsnobody user as MAX_UID-2
154 # that makes a good test case for signedness issues.
155 # http://bugs.python.org/issue1747858
156 # This part of the test only runs when run as root.
157 # Only scary people run their tests as root.
158 ent = pwd.getpwnam('nfsnobody')
159 chown_func(first_param, ent.pw_uid, ent.pw_gid)
160 except KeyError:
161 pass
162 else:
163 # non-root cannot chown to root, raises OSError
164 self.assertRaises(OSError, chown_func,
165 first_param, 0, 0)
166
167 # test a successful chown call
168 chown_func(first_param, os.getuid(), os.getgid())
169
170 def _test_chown(self):
171 # raise an OSError if the file does not exist
172 os.unlink(test_support.TESTFN)
173 self.assertRaises(OSError, posix.chown, test_support.TESTFN, -1, -1)
174
175 # re-create the file
176 open(test_support.TESTFN, 'w').close()
177 self._test_all_chown_common(posix.chown, test_support.TESTFN)
178
Gregory P. Smithf48da8f2008-03-18 19:05:32 +0000179 if hasattr(posix, 'chown'):
Gregory P. Smith21c134d2009-12-23 09:46:53 +0000180 test_chown = _test_chown
Gregory P. Smithf48da8f2008-03-18 19:05:32 +0000181
Gregory P. Smith21c134d2009-12-23 09:46:53 +0000182 def _test_fchown(self):
183 os.unlink(test_support.TESTFN)
Gregory P. Smithf48da8f2008-03-18 19:05:32 +0000184
Gregory P. Smith21c134d2009-12-23 09:46:53 +0000185 # re-create the file
186 test_file = open(test_support.TESTFN, 'w')
187 try:
188 fd = test_file.fileno()
189 self._test_all_chown_common(posix.fchown, fd)
190 finally:
191 test_file.close()
192
193 if hasattr(posix, 'fchown'):
194 test_fchown = _test_fchown
195
196 def _test_lchown(self):
197 os.unlink(test_support.TESTFN)
198 # create a symlink
199 os.symlink('/tmp/dummy-symlink-target', test_support.TESTFN)
200 self._test_all_chown_common(posix.lchown, test_support.TESTFN)
201
202 if hasattr(posix, 'lchown'):
203 test_lchown = _test_lchown
Gregory P. Smithf48da8f2008-03-18 19:05:32 +0000204
Neal Norwitze241ce82003-02-17 18:17:05 +0000205 def test_chdir(self):
206 if hasattr(posix, 'chdir'):
207 posix.chdir(os.curdir)
Walter Dörwald21d3a322003-05-01 17:45:56 +0000208 self.assertRaises(OSError, posix.chdir, test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000209
210 def test_lsdir(self):
211 if hasattr(posix, 'lsdir'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000212 self.assert_(test_support.TESTFN in posix.lsdir(os.curdir))
Neal Norwitze241ce82003-02-17 18:17:05 +0000213
214 def test_access(self):
215 if hasattr(posix, 'access'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000216 self.assert_(posix.access(test_support.TESTFN, os.R_OK))
Neal Norwitze241ce82003-02-17 18:17:05 +0000217
218 def test_umask(self):
219 if hasattr(posix, 'umask'):
220 old_mask = posix.umask(0)
Neal Norwitz2ff51a82003-02-17 22:40:31 +0000221 self.assert_(isinstance(old_mask, int))
Neal Norwitze241ce82003-02-17 18:17:05 +0000222 posix.umask(old_mask)
223
224 def test_strerror(self):
225 if hasattr(posix, 'strerror'):
Neal Norwitz2ff51a82003-02-17 22:40:31 +0000226 self.assert_(posix.strerror(0))
Neal Norwitze241ce82003-02-17 18:17:05 +0000227
228 def test_pipe(self):
229 if hasattr(posix, 'pipe'):
230 reader, writer = posix.pipe()
231 os.close(reader)
232 os.close(writer)
233
234 def test_tempnam(self):
235 if hasattr(posix, 'tempnam'):
Neal Norwitz2ff51a82003-02-17 22:40:31 +0000236 self.assert_(posix.tempnam())
237 self.assert_(posix.tempnam(os.curdir))
238 self.assert_(posix.tempnam(os.curdir, 'blah'))
Neal Norwitze241ce82003-02-17 18:17:05 +0000239
240 def test_tmpfile(self):
241 if hasattr(posix, 'tmpfile'):
242 fp = posix.tmpfile()
243 fp.close()
244
245 def test_utime(self):
246 if hasattr(posix, 'utime'):
247 now = time.time()
Walter Dörwald21d3a322003-05-01 17:45:56 +0000248 posix.utime(test_support.TESTFN, None)
Neal Norwitzc28e7ad2004-06-06 20:27:05 +0000249 self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, None))
250 self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (now, None))
251 self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, now))
252 posix.utime(test_support.TESTFN, (int(now), int(now)))
Walter Dörwald21d3a322003-05-01 17:45:56 +0000253 posix.utime(test_support.TESTFN, (now, now))
Neal Norwitze241ce82003-02-17 18:17:05 +0000254
Martin v. Löwis382abef2007-02-19 10:55:19 +0000255 def test_chflags(self):
256 if hasattr(posix, 'chflags'):
257 st = os.stat(test_support.TESTFN)
258 if hasattr(st, 'st_flags'):
259 posix.chflags(test_support.TESTFN, st.st_flags)
260
261 def test_lchflags(self):
262 if hasattr(posix, 'lchflags'):
263 st = os.stat(test_support.TESTFN)
264 if hasattr(st, 'st_flags'):
265 posix.lchflags(test_support.TESTFN, st.st_flags)
266
Facundo Batista5596b0c2008-06-22 13:36:20 +0000267 def test_getcwd_long_pathnames(self):
268 if hasattr(posix, 'getcwd'):
269 dirname = 'getcwd-test-directory-0123456789abcdef-01234567890abcdef'
270 curdir = os.getcwd()
Facundo Batista96f3dc32008-06-22 18:23:55 +0000271 base_path = os.path.abspath(test_support.TESTFN) + '.getcwd'
Facundo Batista5596b0c2008-06-22 13:36:20 +0000272
273 try:
274 os.mkdir(base_path)
275 os.chdir(base_path)
Facundo Batista96f3dc32008-06-22 18:23:55 +0000276 except:
Facundo Batista2694eb02008-06-22 19:35:24 +0000277# Just returning nothing instead of the TestSkipped exception,
278# because the test results in Error in that case.
279# Is that ok?
280# raise test_support.TestSkipped, "cannot create directory for testing"
281 return
Facundo Batista5596b0c2008-06-22 13:36:20 +0000282
Facundo Batista96f3dc32008-06-22 18:23:55 +0000283 try:
Facundo Batista5596b0c2008-06-22 13:36:20 +0000284 def _create_and_do_getcwd(dirname, current_path_length = 0):
285 try:
286 os.mkdir(dirname)
287 except:
288 raise test_support.TestSkipped, "mkdir cannot create directory sufficiently deep for getcwd test"
289
290 os.chdir(dirname)
291 try:
292 os.getcwd()
Stefan Krah36db84d2010-07-19 15:43:23 +0000293 if current_path_length < 4099:
Facundo Batista5596b0c2008-06-22 13:36:20 +0000294 _create_and_do_getcwd(dirname, current_path_length + len(dirname) + 1)
Stefan Krah36db84d2010-07-19 15:43:23 +0000295 except OSError as e:
296 expected_errno = errno.ENAMETOOLONG
297 if 'sunos' in sys.platform or 'openbsd' in sys.platform:
298 expected_errno = errno.ERANGE # Issue 9185
299 self.assertEqual(e.errno, expected_errno)
Facundo Batista5596b0c2008-06-22 13:36:20 +0000300 finally:
301 os.chdir('..')
302 os.rmdir(dirname)
303
304 _create_and_do_getcwd(dirname)
305
306 finally:
Facundo Batista5596b0c2008-06-22 13:36:20 +0000307 os.chdir(curdir)
R. David Murray8bb57b72009-07-09 20:13:41 +0000308 shutil.rmtree(base_path)
Facundo Batista5596b0c2008-06-22 13:36:20 +0000309
Ronald Oussorenac08e302010-07-24 10:05:19 +0000310 def test_getgroups(self):
Ronald Oussoren1fa5e052010-08-01 18:46:05 +0000311 with os.popen('id -G 2>/dev/null') as idg:
Ronald Oussorenac08e302010-07-24 10:05:19 +0000312 groups = idg.read().strip()
313
314 if not groups:
Ronald Oussoren1fa5e052010-08-01 18:46:05 +0000315 # This test needs 'id -G'
316 return
Ronald Oussorenac08e302010-07-24 10:05:19 +0000317
Ronald Oussorenbb4d9f62010-08-03 08:07:18 +0000318 # 'id -G' and 'os.getgroups()' should return the same
319 # groups, ignoring order and duplicates.
Ronald Oussoren7c973452010-07-24 14:23:23 +0000320 self.assertEqual(
Ronald Oussorenbb4d9f62010-08-03 08:07:18 +0000321 set([int(x) for x in groups.split()]),
322 set(posix.getgroups()))
Ronald Oussorenac08e302010-07-24 10:05:19 +0000323
324class PosixGroupsTester(unittest.TestCase):
325 if posix.getuid() == 0 and hasattr(posix, 'getgroups') and sys.platform != 'darwin':
326
327 def setUp(self):
328 self.saved_groups = posix.getgroups()
329
330 def tearDown(self):
331 if hasattr(posix, 'setgroups'):
332 posix.setgroups(self.saved_groups)
333 elif hasattr(posix, 'initgroups'):
334 name = pwd.getpwuid(posix.getuid()).pw_name
335 posix.initgroups(name, self.saved_groups[0])
336
337 if hasattr(posix, 'initgroups'):
338 def test_initgroups(self):
339 # find missing group
340
341 groups = sorted(self.saved_groups)
342 for g1,g2 in zip(groups[:-1], groups[1:]):
343 g = g1 + 1
344 if g < g2:
345 break
346 else:
347 g = g2 + 1
348 name = pwd.getpwuid(posix.getuid()).pw_name
349 posix.initgroups(name, g)
350 self.assertIn(g, posix.getgroups())
351
352 if hasattr(posix, 'setgroups'):
353 def test_setgroups(self):
354 for groups in [[0], range(16)]:
355 posix.setgroups(groups)
356 self.assertListEqual(groups, posix.getgroups())
357
Facundo Batista5596b0c2008-06-22 13:36:20 +0000358
Neal Norwitze241ce82003-02-17 18:17:05 +0000359def test_main():
Ronald Oussorenac08e302010-07-24 10:05:19 +0000360 test_support.run_unittest(PosixTester, PosixGroupsTester)
Neal Norwitze241ce82003-02-17 18:17:05 +0000361
362if __name__ == '__main__':
363 test_main()