blob: ac3dbb832d7315ff551e9e7bfaf52989aede6955 [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
10import time
11import os
Gregory P. Smithf48da8f2008-03-18 19:05:32 +000012import pwd
Facundo Batista5596b0c2008-06-22 13:36:20 +000013import shutil
Stefan Krah36db84d2010-07-19 15:43:23 +000014import sys
Neal Norwitze241ce82003-02-17 18:17:05 +000015import unittest
16import warnings
17warnings.filterwarnings('ignore', '.* potential security risk .*',
18 RuntimeWarning)
19
20class PosixTester(unittest.TestCase):
21
22 def setUp(self):
23 # create empty file
Walter Dörwald21d3a322003-05-01 17:45:56 +000024 fp = open(test_support.TESTFN, 'w+')
Neal Norwitze241ce82003-02-17 18:17:05 +000025 fp.close()
26
27 def tearDown(self):
Walter Dörwald21d3a322003-05-01 17:45:56 +000028 os.unlink(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +000029
30 def testNoArgFunctions(self):
31 # test posix functions which take no arguments and have
32 # no side-effects which we need to cleanup (e.g., fork, wait, abort)
33 NO_ARG_FUNCTIONS = [ "ctermid", "getcwd", "getcwdu", "uname",
Neal Norwitz71b13e82003-02-23 22:12:24 +000034 "times", "getloadavg", "tmpnam",
Neal Norwitze241ce82003-02-17 18:17:05 +000035 "getegid", "geteuid", "getgid", "getgroups",
36 "getpid", "getpgrp", "getppid", "getuid",
37 ]
Neal Norwitz71b13e82003-02-23 22:12:24 +000038
Neal Norwitze241ce82003-02-17 18:17:05 +000039 for name in NO_ARG_FUNCTIONS:
40 posix_func = getattr(posix, name, None)
41 if posix_func is not None:
42 posix_func()
Neal Norwitz2ff51a82003-02-17 22:40:31 +000043 self.assertRaises(TypeError, posix_func, 1)
Neal Norwitze241ce82003-02-17 18:17:05 +000044
45 def test_statvfs(self):
46 if hasattr(posix, 'statvfs'):
Neal Norwitz2ff51a82003-02-17 22:40:31 +000047 self.assert_(posix.statvfs(os.curdir))
Neal Norwitze241ce82003-02-17 18:17:05 +000048
49 def test_fstatvfs(self):
50 if hasattr(posix, 'fstatvfs'):
Walter Dörwald21d3a322003-05-01 17:45:56 +000051 fp = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +000052 try:
Neal Norwitz2ff51a82003-02-17 22:40:31 +000053 self.assert_(posix.fstatvfs(fp.fileno()))
Neal Norwitze241ce82003-02-17 18:17:05 +000054 finally:
55 fp.close()
56
57 def test_ftruncate(self):
58 if hasattr(posix, 'ftruncate'):
Walter Dörwald21d3a322003-05-01 17:45:56 +000059 fp = open(test_support.TESTFN, 'w+')
Neal Norwitze241ce82003-02-17 18:17:05 +000060 try:
61 # we need to have some data to truncate
62 fp.write('test')
63 fp.flush()
64 posix.ftruncate(fp.fileno(), 0)
65 finally:
66 fp.close()
67
68 def test_dup(self):
69 if hasattr(posix, 'dup'):
Walter Dörwald21d3a322003-05-01 17:45:56 +000070 fp = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +000071 try:
72 fd = posix.dup(fp.fileno())
Neal Norwitz2ff51a82003-02-17 22:40:31 +000073 self.assert_(isinstance(fd, int))
Neal Norwitze241ce82003-02-17 18:17:05 +000074 os.close(fd)
75 finally:
76 fp.close()
77
Skip Montanaro94785ef2006-04-20 01:29:48 +000078 def test_confstr(self):
79 if hasattr(posix, 'confstr'):
80 self.assertRaises(ValueError, posix.confstr, "CS_garbage")
81 self.assertEqual(len(posix.confstr("CS_PATH")) > 0, True)
82
Neal Norwitze241ce82003-02-17 18:17:05 +000083 def test_dup2(self):
84 if hasattr(posix, 'dup2'):
Walter Dörwald21d3a322003-05-01 17:45:56 +000085 fp1 = open(test_support.TESTFN)
86 fp2 = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +000087 try:
88 posix.dup2(fp1.fileno(), fp2.fileno())
89 finally:
90 fp1.close()
91 fp2.close()
92
93 def fdopen_helper(self, *args):
Walter Dörwald21d3a322003-05-01 17:45:56 +000094 fd = os.open(test_support.TESTFN, os.O_RDONLY)
Neal Norwitze241ce82003-02-17 18:17:05 +000095 fp2 = posix.fdopen(fd, *args)
96 fp2.close()
97
98 def test_fdopen(self):
99 if hasattr(posix, 'fdopen'):
100 self.fdopen_helper()
101 self.fdopen_helper('r')
102 self.fdopen_helper('r', 100)
103
Skip Montanaro98470002005-06-17 01:14:49 +0000104 def test_osexlock(self):
105 if hasattr(posix, "O_EXLOCK"):
106 fd = os.open(test_support.TESTFN,
107 os.O_WRONLY|os.O_EXLOCK|os.O_CREAT)
108 self.assertRaises(OSError, os.open, test_support.TESTFN,
109 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
110 os.close(fd)
111
112 if hasattr(posix, "O_SHLOCK"):
113 fd = os.open(test_support.TESTFN,
114 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
115 self.assertRaises(OSError, os.open, test_support.TESTFN,
116 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
117 os.close(fd)
118
119 def test_osshlock(self):
120 if hasattr(posix, "O_SHLOCK"):
121 fd1 = os.open(test_support.TESTFN,
122 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
123 fd2 = os.open(test_support.TESTFN,
124 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
125 os.close(fd2)
126 os.close(fd1)
127
128 if hasattr(posix, "O_EXLOCK"):
129 fd = os.open(test_support.TESTFN,
130 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
131 self.assertRaises(OSError, os.open, test_support.TESTFN,
132 os.O_RDONLY|os.O_EXLOCK|os.O_NONBLOCK)
133 os.close(fd)
134
Neal Norwitze241ce82003-02-17 18:17:05 +0000135 def test_fstat(self):
136 if hasattr(posix, 'fstat'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000137 fp = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000138 try:
Neal Norwitz2ff51a82003-02-17 22:40:31 +0000139 self.assert_(posix.fstat(fp.fileno()))
Neal Norwitze241ce82003-02-17 18:17:05 +0000140 finally:
141 fp.close()
142
143 def test_stat(self):
144 if hasattr(posix, 'stat'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000145 self.assert_(posix.stat(test_support.TESTFN))
Neal Norwitze241ce82003-02-17 18:17:05 +0000146
Gregory P. Smith21c134d2009-12-23 09:46:53 +0000147 def _test_all_chown_common(self, chown_func, first_param):
148 """Common code for chown, fchown and lchown tests."""
149 if os.getuid() == 0:
150 try:
151 # Many linux distros have a nfsnobody user as MAX_UID-2
152 # that makes a good test case for signedness issues.
153 # http://bugs.python.org/issue1747858
154 # This part of the test only runs when run as root.
155 # Only scary people run their tests as root.
156 ent = pwd.getpwnam('nfsnobody')
157 chown_func(first_param, ent.pw_uid, ent.pw_gid)
158 except KeyError:
159 pass
160 else:
161 # non-root cannot chown to root, raises OSError
162 self.assertRaises(OSError, chown_func,
163 first_param, 0, 0)
164
165 # test a successful chown call
166 chown_func(first_param, os.getuid(), os.getgid())
167
168 def _test_chown(self):
169 # raise an OSError if the file does not exist
170 os.unlink(test_support.TESTFN)
171 self.assertRaises(OSError, posix.chown, test_support.TESTFN, -1, -1)
172
173 # re-create the file
174 open(test_support.TESTFN, 'w').close()
175 self._test_all_chown_common(posix.chown, test_support.TESTFN)
176
Gregory P. Smithf48da8f2008-03-18 19:05:32 +0000177 if hasattr(posix, 'chown'):
Gregory P. Smith21c134d2009-12-23 09:46:53 +0000178 test_chown = _test_chown
Gregory P. Smithf48da8f2008-03-18 19:05:32 +0000179
Gregory P. Smith21c134d2009-12-23 09:46:53 +0000180 def _test_fchown(self):
181 os.unlink(test_support.TESTFN)
Gregory P. Smithf48da8f2008-03-18 19:05:32 +0000182
Gregory P. Smith21c134d2009-12-23 09:46:53 +0000183 # re-create the file
184 test_file = open(test_support.TESTFN, 'w')
185 try:
186 fd = test_file.fileno()
187 self._test_all_chown_common(posix.fchown, fd)
188 finally:
189 test_file.close()
190
191 if hasattr(posix, 'fchown'):
192 test_fchown = _test_fchown
193
194 def _test_lchown(self):
195 os.unlink(test_support.TESTFN)
196 # create a symlink
197 os.symlink('/tmp/dummy-symlink-target', test_support.TESTFN)
198 self._test_all_chown_common(posix.lchown, test_support.TESTFN)
199
200 if hasattr(posix, 'lchown'):
201 test_lchown = _test_lchown
Gregory P. Smithf48da8f2008-03-18 19:05:32 +0000202
Neal Norwitze241ce82003-02-17 18:17:05 +0000203 def test_chdir(self):
204 if hasattr(posix, 'chdir'):
205 posix.chdir(os.curdir)
Walter Dörwald21d3a322003-05-01 17:45:56 +0000206 self.assertRaises(OSError, posix.chdir, test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000207
208 def test_lsdir(self):
209 if hasattr(posix, 'lsdir'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000210 self.assert_(test_support.TESTFN in posix.lsdir(os.curdir))
Neal Norwitze241ce82003-02-17 18:17:05 +0000211
212 def test_access(self):
213 if hasattr(posix, 'access'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000214 self.assert_(posix.access(test_support.TESTFN, os.R_OK))
Neal Norwitze241ce82003-02-17 18:17:05 +0000215
216 def test_umask(self):
217 if hasattr(posix, 'umask'):
218 old_mask = posix.umask(0)
Neal Norwitz2ff51a82003-02-17 22:40:31 +0000219 self.assert_(isinstance(old_mask, int))
Neal Norwitze241ce82003-02-17 18:17:05 +0000220 posix.umask(old_mask)
221
222 def test_strerror(self):
223 if hasattr(posix, 'strerror'):
Neal Norwitz2ff51a82003-02-17 22:40:31 +0000224 self.assert_(posix.strerror(0))
Neal Norwitze241ce82003-02-17 18:17:05 +0000225
226 def test_pipe(self):
227 if hasattr(posix, 'pipe'):
228 reader, writer = posix.pipe()
229 os.close(reader)
230 os.close(writer)
231
232 def test_tempnam(self):
233 if hasattr(posix, 'tempnam'):
Neal Norwitz2ff51a82003-02-17 22:40:31 +0000234 self.assert_(posix.tempnam())
235 self.assert_(posix.tempnam(os.curdir))
236 self.assert_(posix.tempnam(os.curdir, 'blah'))
Neal Norwitze241ce82003-02-17 18:17:05 +0000237
238 def test_tmpfile(self):
239 if hasattr(posix, 'tmpfile'):
240 fp = posix.tmpfile()
241 fp.close()
242
243 def test_utime(self):
244 if hasattr(posix, 'utime'):
245 now = time.time()
Walter Dörwald21d3a322003-05-01 17:45:56 +0000246 posix.utime(test_support.TESTFN, None)
Neal Norwitzc28e7ad2004-06-06 20:27:05 +0000247 self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, None))
248 self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (now, None))
249 self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, now))
250 posix.utime(test_support.TESTFN, (int(now), int(now)))
Walter Dörwald21d3a322003-05-01 17:45:56 +0000251 posix.utime(test_support.TESTFN, (now, now))
Neal Norwitze241ce82003-02-17 18:17:05 +0000252
Martin v. Löwis382abef2007-02-19 10:55:19 +0000253 def test_chflags(self):
254 if hasattr(posix, 'chflags'):
255 st = os.stat(test_support.TESTFN)
256 if hasattr(st, 'st_flags'):
257 posix.chflags(test_support.TESTFN, st.st_flags)
258
259 def test_lchflags(self):
260 if hasattr(posix, 'lchflags'):
261 st = os.stat(test_support.TESTFN)
262 if hasattr(st, 'st_flags'):
263 posix.lchflags(test_support.TESTFN, st.st_flags)
264
Facundo Batista5596b0c2008-06-22 13:36:20 +0000265 def test_getcwd_long_pathnames(self):
266 if hasattr(posix, 'getcwd'):
267 dirname = 'getcwd-test-directory-0123456789abcdef-01234567890abcdef'
268 curdir = os.getcwd()
Facundo Batista96f3dc32008-06-22 18:23:55 +0000269 base_path = os.path.abspath(test_support.TESTFN) + '.getcwd'
Facundo Batista5596b0c2008-06-22 13:36:20 +0000270
271 try:
272 os.mkdir(base_path)
273 os.chdir(base_path)
Facundo Batista96f3dc32008-06-22 18:23:55 +0000274 except:
Facundo Batista2694eb02008-06-22 19:35:24 +0000275# Just returning nothing instead of the TestSkipped exception,
276# because the test results in Error in that case.
277# Is that ok?
278# raise test_support.TestSkipped, "cannot create directory for testing"
279 return
Facundo Batista5596b0c2008-06-22 13:36:20 +0000280
Facundo Batista96f3dc32008-06-22 18:23:55 +0000281 try:
Facundo Batista5596b0c2008-06-22 13:36:20 +0000282 def _create_and_do_getcwd(dirname, current_path_length = 0):
283 try:
284 os.mkdir(dirname)
285 except:
286 raise test_support.TestSkipped, "mkdir cannot create directory sufficiently deep for getcwd test"
287
288 os.chdir(dirname)
289 try:
290 os.getcwd()
Stefan Krah36db84d2010-07-19 15:43:23 +0000291 if current_path_length < 4099:
Facundo Batista5596b0c2008-06-22 13:36:20 +0000292 _create_and_do_getcwd(dirname, current_path_length + len(dirname) + 1)
Stefan Krah36db84d2010-07-19 15:43:23 +0000293 except OSError as e:
294 expected_errno = errno.ENAMETOOLONG
295 if 'sunos' in sys.platform or 'openbsd' in sys.platform:
296 expected_errno = errno.ERANGE # Issue 9185
297 self.assertEqual(e.errno, expected_errno)
Facundo Batista5596b0c2008-06-22 13:36:20 +0000298 finally:
299 os.chdir('..')
300 os.rmdir(dirname)
301
302 _create_and_do_getcwd(dirname)
303
304 finally:
Facundo Batista5596b0c2008-06-22 13:36:20 +0000305 os.chdir(curdir)
R. David Murray8bb57b72009-07-09 20:13:41 +0000306 shutil.rmtree(base_path)
Facundo Batista5596b0c2008-06-22 13:36:20 +0000307
308
Neal Norwitze241ce82003-02-17 18:17:05 +0000309def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000310 test_support.run_unittest(PosixTester)
Neal Norwitze241ce82003-02-17 18:17:05 +0000311
312if __name__ == '__main__':
313 test_main()