blob: aaa384124c990181b056cee67daf6595dbc188a0 [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
8import time
9import os
Gregory P. Smithf48da8f2008-03-18 19:05:32 +000010import pwd
Facundo Batista5596b0c2008-06-22 13:36:20 +000011import shutil
Neal Norwitze241ce82003-02-17 18:17:05 +000012import unittest
13import warnings
R. David Murray59beec32009-03-30 19:04:00 +000014
R. David Murray59beec32009-03-30 19:04:00 +000015
Neal Norwitze241ce82003-02-17 18:17:05 +000016warnings.filterwarnings('ignore', '.* potential security risk .*',
17 RuntimeWarning)
18
19class PosixTester(unittest.TestCase):
20
21 def setUp(self):
22 # create empty file
Walter Dörwald21d3a322003-05-01 17:45:56 +000023 fp = open(test_support.TESTFN, 'w+')
Neal Norwitze241ce82003-02-17 18:17:05 +000024 fp.close()
25
26 def tearDown(self):
Walter Dörwald21d3a322003-05-01 17:45:56 +000027 os.unlink(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +000028
29 def testNoArgFunctions(self):
30 # test posix functions which take no arguments and have
31 # no side-effects which we need to cleanup (e.g., fork, wait, abort)
32 NO_ARG_FUNCTIONS = [ "ctermid", "getcwd", "getcwdu", "uname",
Neal Norwitz71b13e82003-02-23 22:12:24 +000033 "times", "getloadavg", "tmpnam",
Neal Norwitze241ce82003-02-17 18:17:05 +000034 "getegid", "geteuid", "getgid", "getgroups",
35 "getpid", "getpgrp", "getppid", "getuid",
36 ]
Neal Norwitz71b13e82003-02-23 22:12:24 +000037
Neal Norwitze241ce82003-02-17 18:17:05 +000038 for name in NO_ARG_FUNCTIONS:
39 posix_func = getattr(posix, name, None)
40 if posix_func is not None:
41 posix_func()
Neal Norwitz2ff51a82003-02-17 22:40:31 +000042 self.assertRaises(TypeError, posix_func, 1)
Neal Norwitze241ce82003-02-17 18:17:05 +000043
Martin v. Löwis50ea4562009-11-27 13:56:01 +000044 if hasattr(posix, 'getresuid'):
45 def test_getresuid(self):
46 user_ids = posix.getresuid()
47 self.assertEqual(len(user_ids), 3)
48 for val in user_ids:
49 self.assertGreaterEqual(val, 0)
50
51 if hasattr(posix, 'getresgid'):
52 def test_getresgid(self):
53 group_ids = posix.getresgid()
54 self.assertEqual(len(group_ids), 3)
55 for val in group_ids:
56 self.assertGreaterEqual(val, 0)
57
58 if hasattr(posix, 'setresuid'):
59 def test_setresuid(self):
60 current_user_ids = posix.getresuid()
61 self.assertIsNone(posix.setresuid(*current_user_ids))
62 # -1 means don't change that value.
63 self.assertIsNone(posix.setresuid(-1, -1, -1))
64
65 def test_setresuid_exception(self):
66 # Don't do this test if someone is silly enough to run us as root.
67 current_user_ids = posix.getresuid()
68 if 0 not in current_user_ids:
69 new_user_ids = (current_user_ids[0]+1, -1, -1)
70 self.assertRaises(OSError, posix.setresuid, *new_user_ids)
71
72 if hasattr(posix, 'setresgid'):
73 def test_setresgid(self):
74 current_group_ids = posix.getresgid()
75 self.assertIsNone(posix.setresgid(*current_group_ids))
76 # -1 means don't change that value.
77 self.assertIsNone(posix.setresgid(-1, -1, -1))
78
79 def test_setresgid_exception(self):
80 # Don't do this test if someone is silly enough to run us as root.
81 current_group_ids = posix.getresgid()
82 if 0 not in current_group_ids:
83 new_group_ids = (current_group_ids[0]+1, -1, -1)
84 self.assertRaises(OSError, posix.setresgid, *new_group_ids)
85
Neal Norwitze241ce82003-02-17 18:17:05 +000086 def test_statvfs(self):
87 if hasattr(posix, 'statvfs'):
Benjamin Peterson5c8da862009-06-30 22:57:08 +000088 self.assertTrue(posix.statvfs(os.curdir))
Neal Norwitze241ce82003-02-17 18:17:05 +000089
90 def test_fstatvfs(self):
91 if hasattr(posix, 'fstatvfs'):
Walter Dörwald21d3a322003-05-01 17:45:56 +000092 fp = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +000093 try:
Benjamin Peterson5c8da862009-06-30 22:57:08 +000094 self.assertTrue(posix.fstatvfs(fp.fileno()))
Neal Norwitze241ce82003-02-17 18:17:05 +000095 finally:
96 fp.close()
97
98 def test_ftruncate(self):
99 if hasattr(posix, 'ftruncate'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000100 fp = open(test_support.TESTFN, 'w+')
Neal Norwitze241ce82003-02-17 18:17:05 +0000101 try:
102 # we need to have some data to truncate
103 fp.write('test')
104 fp.flush()
105 posix.ftruncate(fp.fileno(), 0)
106 finally:
107 fp.close()
108
109 def test_dup(self):
110 if hasattr(posix, 'dup'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000111 fp = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000112 try:
113 fd = posix.dup(fp.fileno())
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000114 self.assertTrue(isinstance(fd, int))
Neal Norwitze241ce82003-02-17 18:17:05 +0000115 os.close(fd)
116 finally:
117 fp.close()
118
Skip Montanaro94785ef2006-04-20 01:29:48 +0000119 def test_confstr(self):
120 if hasattr(posix, 'confstr'):
121 self.assertRaises(ValueError, posix.confstr, "CS_garbage")
122 self.assertEqual(len(posix.confstr("CS_PATH")) > 0, True)
123
Neal Norwitze241ce82003-02-17 18:17:05 +0000124 def test_dup2(self):
125 if hasattr(posix, 'dup2'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000126 fp1 = open(test_support.TESTFN)
127 fp2 = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000128 try:
129 posix.dup2(fp1.fileno(), fp2.fileno())
130 finally:
131 fp1.close()
132 fp2.close()
133
134 def fdopen_helper(self, *args):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000135 fd = os.open(test_support.TESTFN, os.O_RDONLY)
Neal Norwitze241ce82003-02-17 18:17:05 +0000136 fp2 = posix.fdopen(fd, *args)
137 fp2.close()
138
139 def test_fdopen(self):
140 if hasattr(posix, 'fdopen'):
141 self.fdopen_helper()
142 self.fdopen_helper('r')
143 self.fdopen_helper('r', 100)
144
Skip Montanaro98470002005-06-17 01:14:49 +0000145 def test_osexlock(self):
146 if hasattr(posix, "O_EXLOCK"):
147 fd = os.open(test_support.TESTFN,
148 os.O_WRONLY|os.O_EXLOCK|os.O_CREAT)
149 self.assertRaises(OSError, os.open, test_support.TESTFN,
150 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
151 os.close(fd)
152
153 if hasattr(posix, "O_SHLOCK"):
154 fd = os.open(test_support.TESTFN,
155 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
156 self.assertRaises(OSError, os.open, test_support.TESTFN,
157 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
158 os.close(fd)
159
160 def test_osshlock(self):
161 if hasattr(posix, "O_SHLOCK"):
162 fd1 = os.open(test_support.TESTFN,
163 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
164 fd2 = os.open(test_support.TESTFN,
165 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
166 os.close(fd2)
167 os.close(fd1)
168
169 if hasattr(posix, "O_EXLOCK"):
170 fd = os.open(test_support.TESTFN,
171 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
172 self.assertRaises(OSError, os.open, test_support.TESTFN,
173 os.O_RDONLY|os.O_EXLOCK|os.O_NONBLOCK)
174 os.close(fd)
175
Neal Norwitze241ce82003-02-17 18:17:05 +0000176 def test_fstat(self):
177 if hasattr(posix, 'fstat'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000178 fp = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000179 try:
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000180 self.assertTrue(posix.fstat(fp.fileno()))
Neal Norwitze241ce82003-02-17 18:17:05 +0000181 finally:
182 fp.close()
183
184 def test_stat(self):
185 if hasattr(posix, 'stat'):
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000186 self.assertTrue(posix.stat(test_support.TESTFN))
Neal Norwitze241ce82003-02-17 18:17:05 +0000187
Gregory P. Smithf48da8f2008-03-18 19:05:32 +0000188 if hasattr(posix, 'chown'):
189 def test_chown(self):
190 # raise an OSError if the file does not exist
191 os.unlink(test_support.TESTFN)
192 self.assertRaises(OSError, posix.chown, test_support.TESTFN, -1, -1)
193
194 # re-create the file
195 open(test_support.TESTFN, 'w').close()
196 if os.getuid() == 0:
197 try:
198 # Many linux distros have a nfsnobody user as MAX_UID-2
199 # that makes a good test case for signedness issues.
200 # http://bugs.python.org/issue1747858
201 # This part of the test only runs when run as root.
202 # Only scary people run their tests as root.
203 ent = pwd.getpwnam('nfsnobody')
204 posix.chown(test_support.TESTFN, ent.pw_uid, ent.pw_gid)
205 except KeyError:
206 pass
207 else:
208 # non-root cannot chown to root, raises OSError
209 self.assertRaises(OSError, posix.chown,
210 test_support.TESTFN, 0, 0)
211
212 # test a successful chown call
213 posix.chown(test_support.TESTFN, os.getuid(), os.getgid())
214
Neal Norwitze241ce82003-02-17 18:17:05 +0000215 def test_chdir(self):
216 if hasattr(posix, 'chdir'):
217 posix.chdir(os.curdir)
Walter Dörwald21d3a322003-05-01 17:45:56 +0000218 self.assertRaises(OSError, posix.chdir, test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000219
220 def test_lsdir(self):
221 if hasattr(posix, 'lsdir'):
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000222 self.assertTrue(test_support.TESTFN in posix.lsdir(os.curdir))
Neal Norwitze241ce82003-02-17 18:17:05 +0000223
224 def test_access(self):
225 if hasattr(posix, 'access'):
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000226 self.assertTrue(posix.access(test_support.TESTFN, os.R_OK))
Neal Norwitze241ce82003-02-17 18:17:05 +0000227
228 def test_umask(self):
229 if hasattr(posix, 'umask'):
230 old_mask = posix.umask(0)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000231 self.assertTrue(isinstance(old_mask, int))
Neal Norwitze241ce82003-02-17 18:17:05 +0000232 posix.umask(old_mask)
233
234 def test_strerror(self):
235 if hasattr(posix, 'strerror'):
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000236 self.assertTrue(posix.strerror(0))
Neal Norwitze241ce82003-02-17 18:17:05 +0000237
238 def test_pipe(self):
239 if hasattr(posix, 'pipe'):
240 reader, writer = posix.pipe()
241 os.close(reader)
242 os.close(writer)
243
244 def test_tempnam(self):
245 if hasattr(posix, 'tempnam'):
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000246 self.assertTrue(posix.tempnam())
247 self.assertTrue(posix.tempnam(os.curdir))
248 self.assertTrue(posix.tempnam(os.curdir, 'blah'))
Neal Norwitze241ce82003-02-17 18:17:05 +0000249
250 def test_tmpfile(self):
251 if hasattr(posix, 'tmpfile'):
252 fp = posix.tmpfile()
253 fp.close()
254
255 def test_utime(self):
256 if hasattr(posix, 'utime'):
257 now = time.time()
Walter Dörwald21d3a322003-05-01 17:45:56 +0000258 posix.utime(test_support.TESTFN, None)
Neal Norwitzc28e7ad2004-06-06 20:27:05 +0000259 self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, None))
260 self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (now, None))
261 self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, now))
262 posix.utime(test_support.TESTFN, (int(now), int(now)))
Walter Dörwald21d3a322003-05-01 17:45:56 +0000263 posix.utime(test_support.TESTFN, (now, now))
Neal Norwitze241ce82003-02-17 18:17:05 +0000264
Martin v. Löwis382abef2007-02-19 10:55:19 +0000265 def test_chflags(self):
266 if hasattr(posix, 'chflags'):
267 st = os.stat(test_support.TESTFN)
268 if hasattr(st, 'st_flags'):
269 posix.chflags(test_support.TESTFN, st.st_flags)
270
271 def test_lchflags(self):
272 if hasattr(posix, 'lchflags'):
273 st = os.stat(test_support.TESTFN)
274 if hasattr(st, 'st_flags'):
275 posix.lchflags(test_support.TESTFN, st.st_flags)
276
Facundo Batista5596b0c2008-06-22 13:36:20 +0000277 def test_getcwd_long_pathnames(self):
278 if hasattr(posix, 'getcwd'):
279 dirname = 'getcwd-test-directory-0123456789abcdef-01234567890abcdef'
280 curdir = os.getcwd()
Facundo Batista96f3dc32008-06-22 18:23:55 +0000281 base_path = os.path.abspath(test_support.TESTFN) + '.getcwd'
Facundo Batista5596b0c2008-06-22 13:36:20 +0000282
283 try:
284 os.mkdir(base_path)
285 os.chdir(base_path)
Facundo Batista96f3dc32008-06-22 18:23:55 +0000286 except:
Benjamin Peterson888a39b2009-03-26 20:48:25 +0000287# Just returning nothing instead of the SkipTest exception,
Facundo Batista2694eb02008-06-22 19:35:24 +0000288# because the test results in Error in that case.
289# Is that ok?
Benjamin Peterson888a39b2009-03-26 20:48:25 +0000290# raise unittest.SkipTest, "cannot create directory for testing"
Facundo Batista2694eb02008-06-22 19:35:24 +0000291 return
Facundo Batista5596b0c2008-06-22 13:36:20 +0000292
Facundo Batista96f3dc32008-06-22 18:23:55 +0000293 try:
Facundo Batista5596b0c2008-06-22 13:36:20 +0000294 def _create_and_do_getcwd(dirname, current_path_length = 0):
295 try:
296 os.mkdir(dirname)
297 except:
Benjamin Peterson888a39b2009-03-26 20:48:25 +0000298 raise unittest.SkipTest, "mkdir cannot create directory sufficiently deep for getcwd test"
Facundo Batista5596b0c2008-06-22 13:36:20 +0000299
300 os.chdir(dirname)
301 try:
302 os.getcwd()
303 if current_path_length < 1027:
304 _create_and_do_getcwd(dirname, current_path_length + len(dirname) + 1)
305 finally:
306 os.chdir('..')
307 os.rmdir(dirname)
308
309 _create_and_do_getcwd(dirname)
310
311 finally:
Facundo Batista5596b0c2008-06-22 13:36:20 +0000312 os.chdir(curdir)
R. David Murrayb0c828a2009-07-09 18:41:03 +0000313 shutil.rmtree(base_path)
Facundo Batista5596b0c2008-06-22 13:36:20 +0000314
315
Neal Norwitze241ce82003-02-17 18:17:05 +0000316def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000317 test_support.run_unittest(PosixTester)
Neal Norwitze241ce82003-02-17 18:17:05 +0000318
319if __name__ == '__main__':
320 test_main()