blob: a2eba89cf3c3a358c12d4eaf4a9cca3b4cae14c2 [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
Neal Norwitze241ce82003-02-17 18:17:05 +000011import time
12import os
Gregory P. Smithf48da8f2008-03-18 19:05:32 +000013import pwd
Facundo Batista5596b0c2008-06-22 13:36:20 +000014import shutil
Stefan Krah36db84d2010-07-19 15:43:23 +000015import sys
Neal Norwitze241ce82003-02-17 18:17:05 +000016import unittest
17import warnings
18warnings.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
46 def test_statvfs(self):
47 if hasattr(posix, 'statvfs'):
Neal Norwitz2ff51a82003-02-17 22:40:31 +000048 self.assert_(posix.statvfs(os.curdir))
Neal Norwitze241ce82003-02-17 18:17:05 +000049
50 def test_fstatvfs(self):
51 if hasattr(posix, 'fstatvfs'):
Walter Dörwald21d3a322003-05-01 17:45:56 +000052 fp = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +000053 try:
Neal Norwitz2ff51a82003-02-17 22:40:31 +000054 self.assert_(posix.fstatvfs(fp.fileno()))
Neal Norwitze241ce82003-02-17 18:17:05 +000055 finally:
56 fp.close()
57
58 def test_ftruncate(self):
59 if hasattr(posix, 'ftruncate'):
Walter Dörwald21d3a322003-05-01 17:45:56 +000060 fp = open(test_support.TESTFN, 'w+')
Neal Norwitze241ce82003-02-17 18:17:05 +000061 try:
62 # we need to have some data to truncate
63 fp.write('test')
64 fp.flush()
65 posix.ftruncate(fp.fileno(), 0)
66 finally:
67 fp.close()
68
69 def test_dup(self):
70 if hasattr(posix, 'dup'):
Walter Dörwald21d3a322003-05-01 17:45:56 +000071 fp = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +000072 try:
73 fd = posix.dup(fp.fileno())
Neal Norwitz2ff51a82003-02-17 22:40:31 +000074 self.assert_(isinstance(fd, int))
Neal Norwitze241ce82003-02-17 18:17:05 +000075 os.close(fd)
76 finally:
77 fp.close()
78
Skip Montanaro94785ef2006-04-20 01:29:48 +000079 def test_confstr(self):
80 if hasattr(posix, 'confstr'):
81 self.assertRaises(ValueError, posix.confstr, "CS_garbage")
82 self.assertEqual(len(posix.confstr("CS_PATH")) > 0, True)
83
Neal Norwitze241ce82003-02-17 18:17:05 +000084 def test_dup2(self):
85 if hasattr(posix, 'dup2'):
Walter Dörwald21d3a322003-05-01 17:45:56 +000086 fp1 = open(test_support.TESTFN)
87 fp2 = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +000088 try:
89 posix.dup2(fp1.fileno(), fp2.fileno())
90 finally:
91 fp1.close()
92 fp2.close()
93
94 def fdopen_helper(self, *args):
Walter Dörwald21d3a322003-05-01 17:45:56 +000095 fd = os.open(test_support.TESTFN, os.O_RDONLY)
Neal Norwitze241ce82003-02-17 18:17:05 +000096 fp2 = posix.fdopen(fd, *args)
97 fp2.close()
98
99 def test_fdopen(self):
100 if hasattr(posix, 'fdopen'):
101 self.fdopen_helper()
102 self.fdopen_helper('r')
103 self.fdopen_helper('r', 100)
104
Skip Montanaro98470002005-06-17 01:14:49 +0000105 def test_osexlock(self):
106 if hasattr(posix, "O_EXLOCK"):
107 fd = os.open(test_support.TESTFN,
108 os.O_WRONLY|os.O_EXLOCK|os.O_CREAT)
109 self.assertRaises(OSError, os.open, test_support.TESTFN,
110 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
111 os.close(fd)
112
113 if hasattr(posix, "O_SHLOCK"):
114 fd = os.open(test_support.TESTFN,
115 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
116 self.assertRaises(OSError, os.open, test_support.TESTFN,
117 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
118 os.close(fd)
119
120 def test_osshlock(self):
121 if hasattr(posix, "O_SHLOCK"):
122 fd1 = os.open(test_support.TESTFN,
123 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
124 fd2 = os.open(test_support.TESTFN,
125 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
126 os.close(fd2)
127 os.close(fd1)
128
129 if hasattr(posix, "O_EXLOCK"):
130 fd = os.open(test_support.TESTFN,
131 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
132 self.assertRaises(OSError, os.open, test_support.TESTFN,
133 os.O_RDONLY|os.O_EXLOCK|os.O_NONBLOCK)
134 os.close(fd)
135
Neal Norwitze241ce82003-02-17 18:17:05 +0000136 def test_fstat(self):
137 if hasattr(posix, 'fstat'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000138 fp = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000139 try:
Neal Norwitz2ff51a82003-02-17 22:40:31 +0000140 self.assert_(posix.fstat(fp.fileno()))
Neal Norwitze241ce82003-02-17 18:17:05 +0000141 finally:
142 fp.close()
143
144 def test_stat(self):
145 if hasattr(posix, 'stat'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000146 self.assert_(posix.stat(test_support.TESTFN))
Neal Norwitze241ce82003-02-17 18:17:05 +0000147
Gregory P. Smith21c134d2009-12-23 09:46:53 +0000148 def _test_all_chown_common(self, chown_func, first_param):
149 """Common code for chown, fchown and lchown tests."""
150 if os.getuid() == 0:
151 try:
152 # Many linux distros have a nfsnobody user as MAX_UID-2
153 # that makes a good test case for signedness issues.
154 # http://bugs.python.org/issue1747858
155 # This part of the test only runs when run as root.
156 # Only scary people run their tests as root.
157 ent = pwd.getpwnam('nfsnobody')
158 chown_func(first_param, ent.pw_uid, ent.pw_gid)
159 except KeyError:
160 pass
161 else:
162 # non-root cannot chown to root, raises OSError
163 self.assertRaises(OSError, chown_func,
164 first_param, 0, 0)
165
166 # test a successful chown call
167 chown_func(first_param, os.getuid(), os.getgid())
168
169 def _test_chown(self):
170 # raise an OSError if the file does not exist
171 os.unlink(test_support.TESTFN)
172 self.assertRaises(OSError, posix.chown, test_support.TESTFN, -1, -1)
173
174 # re-create the file
175 open(test_support.TESTFN, 'w').close()
176 self._test_all_chown_common(posix.chown, test_support.TESTFN)
177
Gregory P. Smithf48da8f2008-03-18 19:05:32 +0000178 if hasattr(posix, 'chown'):
Gregory P. Smith21c134d2009-12-23 09:46:53 +0000179 test_chown = _test_chown
Gregory P. Smithf48da8f2008-03-18 19:05:32 +0000180
Gregory P. Smith21c134d2009-12-23 09:46:53 +0000181 def _test_fchown(self):
182 os.unlink(test_support.TESTFN)
Gregory P. Smithf48da8f2008-03-18 19:05:32 +0000183
Gregory P. Smith21c134d2009-12-23 09:46:53 +0000184 # re-create the file
185 test_file = open(test_support.TESTFN, 'w')
186 try:
187 fd = test_file.fileno()
188 self._test_all_chown_common(posix.fchown, fd)
189 finally:
190 test_file.close()
191
192 if hasattr(posix, 'fchown'):
193 test_fchown = _test_fchown
194
195 def _test_lchown(self):
196 os.unlink(test_support.TESTFN)
197 # create a symlink
198 os.symlink('/tmp/dummy-symlink-target', test_support.TESTFN)
199 self._test_all_chown_common(posix.lchown, test_support.TESTFN)
200
201 if hasattr(posix, 'lchown'):
202 test_lchown = _test_lchown
Gregory P. Smithf48da8f2008-03-18 19:05:32 +0000203
Neal Norwitze241ce82003-02-17 18:17:05 +0000204 def test_chdir(self):
205 if hasattr(posix, 'chdir'):
206 posix.chdir(os.curdir)
Walter Dörwald21d3a322003-05-01 17:45:56 +0000207 self.assertRaises(OSError, posix.chdir, test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000208
209 def test_lsdir(self):
210 if hasattr(posix, 'lsdir'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000211 self.assert_(test_support.TESTFN in posix.lsdir(os.curdir))
Neal Norwitze241ce82003-02-17 18:17:05 +0000212
213 def test_access(self):
214 if hasattr(posix, 'access'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000215 self.assert_(posix.access(test_support.TESTFN, os.R_OK))
Neal Norwitze241ce82003-02-17 18:17:05 +0000216
217 def test_umask(self):
218 if hasattr(posix, 'umask'):
219 old_mask = posix.umask(0)
Neal Norwitz2ff51a82003-02-17 22:40:31 +0000220 self.assert_(isinstance(old_mask, int))
Neal Norwitze241ce82003-02-17 18:17:05 +0000221 posix.umask(old_mask)
222
223 def test_strerror(self):
224 if hasattr(posix, 'strerror'):
Neal Norwitz2ff51a82003-02-17 22:40:31 +0000225 self.assert_(posix.strerror(0))
Neal Norwitze241ce82003-02-17 18:17:05 +0000226
227 def test_pipe(self):
228 if hasattr(posix, 'pipe'):
229 reader, writer = posix.pipe()
230 os.close(reader)
231 os.close(writer)
232
233 def test_tempnam(self):
234 if hasattr(posix, 'tempnam'):
Neal Norwitz2ff51a82003-02-17 22:40:31 +0000235 self.assert_(posix.tempnam())
236 self.assert_(posix.tempnam(os.curdir))
237 self.assert_(posix.tempnam(os.curdir, 'blah'))
Neal Norwitze241ce82003-02-17 18:17:05 +0000238
239 def test_tmpfile(self):
240 if hasattr(posix, 'tmpfile'):
241 fp = posix.tmpfile()
242 fp.close()
243
244 def test_utime(self):
245 if hasattr(posix, 'utime'):
246 now = time.time()
Walter Dörwald21d3a322003-05-01 17:45:56 +0000247 posix.utime(test_support.TESTFN, None)
Neal Norwitzc28e7ad2004-06-06 20:27:05 +0000248 self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, None))
249 self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (now, None))
250 self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, now))
251 posix.utime(test_support.TESTFN, (int(now), int(now)))
Walter Dörwald21d3a322003-05-01 17:45:56 +0000252 posix.utime(test_support.TESTFN, (now, now))
Neal Norwitze241ce82003-02-17 18:17:05 +0000253
Martin v. Löwis382abef2007-02-19 10:55:19 +0000254 def test_chflags(self):
255 if hasattr(posix, 'chflags'):
256 st = os.stat(test_support.TESTFN)
257 if hasattr(st, 'st_flags'):
258 posix.chflags(test_support.TESTFN, st.st_flags)
259
260 def test_lchflags(self):
261 if hasattr(posix, 'lchflags'):
262 st = os.stat(test_support.TESTFN)
263 if hasattr(st, 'st_flags'):
264 posix.lchflags(test_support.TESTFN, st.st_flags)
265
Facundo Batista5596b0c2008-06-22 13:36:20 +0000266 def test_getcwd_long_pathnames(self):
267 if hasattr(posix, 'getcwd'):
268 dirname = 'getcwd-test-directory-0123456789abcdef-01234567890abcdef'
269 curdir = os.getcwd()
Facundo Batista96f3dc32008-06-22 18:23:55 +0000270 base_path = os.path.abspath(test_support.TESTFN) + '.getcwd'
Facundo Batista5596b0c2008-06-22 13:36:20 +0000271
272 try:
273 os.mkdir(base_path)
274 os.chdir(base_path)
Facundo Batista96f3dc32008-06-22 18:23:55 +0000275 except:
Facundo Batista2694eb02008-06-22 19:35:24 +0000276# Just returning nothing instead of the TestSkipped exception,
277# because the test results in Error in that case.
278# Is that ok?
279# raise test_support.TestSkipped, "cannot create directory for testing"
280 return
Facundo Batista5596b0c2008-06-22 13:36:20 +0000281
Facundo Batista96f3dc32008-06-22 18:23:55 +0000282 try:
Facundo Batista5596b0c2008-06-22 13:36:20 +0000283 def _create_and_do_getcwd(dirname, current_path_length = 0):
284 try:
285 os.mkdir(dirname)
286 except:
287 raise test_support.TestSkipped, "mkdir cannot create directory sufficiently deep for getcwd test"
288
289 os.chdir(dirname)
290 try:
291 os.getcwd()
Stefan Krah36db84d2010-07-19 15:43:23 +0000292 if current_path_length < 4099:
Facundo Batista5596b0c2008-06-22 13:36:20 +0000293 _create_and_do_getcwd(dirname, current_path_length + len(dirname) + 1)
Stefan Krah36db84d2010-07-19 15:43:23 +0000294 except OSError as e:
295 expected_errno = errno.ENAMETOOLONG
296 if 'sunos' in sys.platform or 'openbsd' in sys.platform:
297 expected_errno = errno.ERANGE # Issue 9185
298 self.assertEqual(e.errno, expected_errno)
Facundo Batista5596b0c2008-06-22 13:36:20 +0000299 finally:
300 os.chdir('..')
301 os.rmdir(dirname)
302
303 _create_and_do_getcwd(dirname)
304
305 finally:
Facundo Batista5596b0c2008-06-22 13:36:20 +0000306 os.chdir(curdir)
R. David Murray8bb57b72009-07-09 20:13:41 +0000307 shutil.rmtree(base_path)
Facundo Batista5596b0c2008-06-22 13:36:20 +0000308
309
Neal Norwitze241ce82003-02-17 18:17:05 +0000310def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000311 test_support.run_unittest(PosixTester)
Neal Norwitze241ce82003-02-17 18:17:05 +0000312
313if __name__ == '__main__':
314 test_main()