blob: f3edf33fb612dacf91aeb62c8ff829237cdef31d [file] [log] [blame]
Neal Norwitze241ce82003-02-17 18:17:05 +00001"Test posix functions"
2
Benjamin Petersonee8712c2008-05-20 21:35:26 +00003from test import support
R. David Murrayeb3615d2009-04-22 02:24:39 +00004
5# Skip these tests if there is no posix module.
6posix = support.import_module('posix')
Neal Norwitze241ce82003-02-17 18:17:05 +00007
Neal Norwitze241ce82003-02-17 18:17:05 +00008import time
9import os
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000010import pwd
Benjamin Petersondcf97b92008-07-02 17:30:14 +000011import shutil
Neal Norwitze241ce82003-02-17 18:17:05 +000012import unittest
13import warnings
R. David Murraya21e4ca2009-03-31 23:16:50 +000014
Neal Norwitze241ce82003-02-17 18:17:05 +000015warnings.filterwarnings('ignore', '.* potential security risk .*',
16 RuntimeWarning)
17
18class PosixTester(unittest.TestCase):
19
20 def setUp(self):
21 # create empty file
Benjamin Petersonee8712c2008-05-20 21:35:26 +000022 fp = open(support.TESTFN, 'w+')
Neal Norwitze241ce82003-02-17 18:17:05 +000023 fp.close()
24
25 def tearDown(self):
Neal Norwitzc34177c2008-08-25 01:04:16 +000026 support.unlink(support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +000027
28 def testNoArgFunctions(self):
29 # test posix functions which take no arguments and have
30 # no side-effects which we need to cleanup (e.g., fork, wait, abort)
Guido van Rossumf0af3e32008-10-02 18:55:37 +000031 NO_ARG_FUNCTIONS = [ "ctermid", "getcwd", "getcwdb", "uname",
Guido van Rossum687b9c02007-10-25 23:18:51 +000032 "times", "getloadavg",
Neal Norwitze241ce82003-02-17 18:17:05 +000033 "getegid", "geteuid", "getgid", "getgroups",
34 "getpid", "getpgrp", "getppid", "getuid",
35 ]
Neal Norwitz71b13e82003-02-23 22:12:24 +000036
Neal Norwitze241ce82003-02-17 18:17:05 +000037 for name in NO_ARG_FUNCTIONS:
38 posix_func = getattr(posix, name, None)
39 if posix_func is not None:
40 posix_func()
Neal Norwitz2ff51a82003-02-17 22:40:31 +000041 self.assertRaises(TypeError, posix_func, 1)
Neal Norwitze241ce82003-02-17 18:17:05 +000042
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000043 if hasattr(posix, 'getresuid'):
44 def test_getresuid(self):
45 user_ids = posix.getresuid()
46 self.assertEqual(len(user_ids), 3)
47 for val in user_ids:
48 self.assertGreaterEqual(val, 0)
49
50 if hasattr(posix, 'getresgid'):
51 def test_getresgid(self):
52 group_ids = posix.getresgid()
53 self.assertEqual(len(group_ids), 3)
54 for val in group_ids:
55 self.assertGreaterEqual(val, 0)
56
57 if hasattr(posix, 'setresuid'):
58 def test_setresuid(self):
59 current_user_ids = posix.getresuid()
60 self.assertIsNone(posix.setresuid(*current_user_ids))
61 # -1 means don't change that value.
62 self.assertIsNone(posix.setresuid(-1, -1, -1))
63
64 def test_setresuid_exception(self):
65 # Don't do this test if someone is silly enough to run us as root.
66 current_user_ids = posix.getresuid()
67 if 0 not in current_user_ids:
68 new_user_ids = (current_user_ids[0]+1, -1, -1)
69 self.assertRaises(OSError, posix.setresuid, *new_user_ids)
70
71 if hasattr(posix, 'setresgid'):
72 def test_setresgid(self):
73 current_group_ids = posix.getresgid()
74 self.assertIsNone(posix.setresgid(*current_group_ids))
75 # -1 means don't change that value.
76 self.assertIsNone(posix.setresgid(-1, -1, -1))
77
78 def test_setresgid_exception(self):
79 # Don't do this test if someone is silly enough to run us as root.
80 current_group_ids = posix.getresgid()
81 if 0 not in current_group_ids:
82 new_group_ids = (current_group_ids[0]+1, -1, -1)
83 self.assertRaises(OSError, posix.setresgid, *new_group_ids)
84
Neal Norwitze241ce82003-02-17 18:17:05 +000085 def test_statvfs(self):
86 if hasattr(posix, 'statvfs'):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000087 self.assertTrue(posix.statvfs(os.curdir))
Neal Norwitze241ce82003-02-17 18:17:05 +000088
89 def test_fstatvfs(self):
90 if hasattr(posix, 'fstatvfs'):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000091 fp = open(support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +000092 try:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000093 self.assertTrue(posix.fstatvfs(fp.fileno()))
Neal Norwitze241ce82003-02-17 18:17:05 +000094 finally:
95 fp.close()
96
97 def test_ftruncate(self):
98 if hasattr(posix, 'ftruncate'):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000099 fp = open(support.TESTFN, 'w+')
Neal Norwitze241ce82003-02-17 18:17:05 +0000100 try:
101 # we need to have some data to truncate
102 fp.write('test')
103 fp.flush()
104 posix.ftruncate(fp.fileno(), 0)
105 finally:
106 fp.close()
107
108 def test_dup(self):
109 if hasattr(posix, 'dup'):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000110 fp = open(support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000111 try:
112 fd = posix.dup(fp.fileno())
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000113 self.assertTrue(isinstance(fd, int))
Neal Norwitze241ce82003-02-17 18:17:05 +0000114 os.close(fd)
115 finally:
116 fp.close()
117
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000118 def test_confstr(self):
119 if hasattr(posix, 'confstr'):
120 self.assertRaises(ValueError, posix.confstr, "CS_garbage")
121 self.assertEqual(len(posix.confstr("CS_PATH")) > 0, True)
122
Neal Norwitze241ce82003-02-17 18:17:05 +0000123 def test_dup2(self):
124 if hasattr(posix, 'dup2'):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000125 fp1 = open(support.TESTFN)
126 fp2 = open(support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000127 try:
128 posix.dup2(fp1.fileno(), fp2.fileno())
129 finally:
130 fp1.close()
131 fp2.close()
132
Skip Montanaro98470002005-06-17 01:14:49 +0000133 def test_osexlock(self):
134 if hasattr(posix, "O_EXLOCK"):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000135 fd = os.open(support.TESTFN,
Skip Montanaro98470002005-06-17 01:14:49 +0000136 os.O_WRONLY|os.O_EXLOCK|os.O_CREAT)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000137 self.assertRaises(OSError, os.open, support.TESTFN,
Skip Montanaro98470002005-06-17 01:14:49 +0000138 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
139 os.close(fd)
140
141 if hasattr(posix, "O_SHLOCK"):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000142 fd = os.open(support.TESTFN,
Skip Montanaro98470002005-06-17 01:14:49 +0000143 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000144 self.assertRaises(OSError, os.open, support.TESTFN,
Skip Montanaro98470002005-06-17 01:14:49 +0000145 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
146 os.close(fd)
147
148 def test_osshlock(self):
149 if hasattr(posix, "O_SHLOCK"):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000150 fd1 = os.open(support.TESTFN,
Skip Montanaro98470002005-06-17 01:14:49 +0000151 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000152 fd2 = os.open(support.TESTFN,
Skip Montanaro98470002005-06-17 01:14:49 +0000153 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
154 os.close(fd2)
155 os.close(fd1)
156
157 if hasattr(posix, "O_EXLOCK"):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000158 fd = os.open(support.TESTFN,
Skip Montanaro98470002005-06-17 01:14:49 +0000159 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000160 self.assertRaises(OSError, os.open, support.TESTFN,
Skip Montanaro98470002005-06-17 01:14:49 +0000161 os.O_RDONLY|os.O_EXLOCK|os.O_NONBLOCK)
162 os.close(fd)
163
Neal Norwitze241ce82003-02-17 18:17:05 +0000164 def test_fstat(self):
165 if hasattr(posix, 'fstat'):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000166 fp = open(support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000167 try:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000168 self.assertTrue(posix.fstat(fp.fileno()))
Neal Norwitze241ce82003-02-17 18:17:05 +0000169 finally:
170 fp.close()
171
172 def test_stat(self):
173 if hasattr(posix, 'stat'):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000174 self.assertTrue(posix.stat(support.TESTFN))
Neal Norwitze241ce82003-02-17 18:17:05 +0000175
Christian Heimesd5e2b6f2008-03-19 21:50:51 +0000176 if hasattr(posix, 'chown'):
177 def test_chown(self):
178 # raise an OSError if the file does not exist
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000179 os.unlink(support.TESTFN)
180 self.assertRaises(OSError, posix.chown, support.TESTFN, -1, -1)
Christian Heimesd5e2b6f2008-03-19 21:50:51 +0000181
182 # re-create the file
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000183 open(support.TESTFN, 'w').close()
Christian Heimesd5e2b6f2008-03-19 21:50:51 +0000184 if os.getuid() == 0:
185 try:
186 # Many linux distros have a nfsnobody user as MAX_UID-2
187 # that makes a good test case for signedness issues.
188 # http://bugs.python.org/issue1747858
189 # This part of the test only runs when run as root.
190 # Only scary people run their tests as root.
191 ent = pwd.getpwnam('nfsnobody')
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000192 posix.chown(support.TESTFN, ent.pw_uid, ent.pw_gid)
Christian Heimesd5e2b6f2008-03-19 21:50:51 +0000193 except KeyError:
194 pass
195 else:
196 # non-root cannot chown to root, raises OSError
197 self.assertRaises(OSError, posix.chown,
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000198 support.TESTFN, 0, 0)
Christian Heimesd5e2b6f2008-03-19 21:50:51 +0000199
200 # test a successful chown call
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000201 posix.chown(support.TESTFN, os.getuid(), os.getgid())
Christian Heimesd5e2b6f2008-03-19 21:50:51 +0000202
Neal Norwitze241ce82003-02-17 18:17:05 +0000203 def test_chdir(self):
204 if hasattr(posix, 'chdir'):
205 posix.chdir(os.curdir)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000206 self.assertRaises(OSError, posix.chdir, support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000207
208 def test_lsdir(self):
209 if hasattr(posix, 'lsdir'):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000210 self.assertTrue(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'):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000214 self.assertTrue(posix.access(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)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000219 self.assertTrue(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'):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000224 self.assertTrue(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
Neal Norwitze241ce82003-02-17 18:17:05 +0000232 def test_utime(self):
233 if hasattr(posix, 'utime'):
234 now = time.time()
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000235 posix.utime(support.TESTFN, None)
236 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, None))
237 self.assertRaises(TypeError, posix.utime, support.TESTFN, (now, None))
238 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, now))
239 posix.utime(support.TESTFN, (int(now), int(now)))
240 posix.utime(support.TESTFN, (now, now))
Neal Norwitze241ce82003-02-17 18:17:05 +0000241
Thomas Wouterscf297e42007-02-23 15:07:44 +0000242 def test_chflags(self):
243 if hasattr(posix, 'chflags'):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000244 st = os.stat(support.TESTFN)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000245 if hasattr(st, 'st_flags'):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000246 posix.chflags(support.TESTFN, st.st_flags)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000247
248 def test_lchflags(self):
249 if hasattr(posix, 'lchflags'):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000250 st = os.stat(support.TESTFN)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000251 if hasattr(st, 'st_flags'):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000252 posix.lchflags(support.TESTFN, st.st_flags)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000253
Guido van Rossum98297ee2007-11-06 21:34:58 +0000254 def test_environ(self):
255 for k, v in posix.environ.items():
256 self.assertEqual(type(k), str)
257 self.assertEqual(type(v), str)
258
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000259 def test_getcwd_long_pathnames(self):
260 if hasattr(posix, 'getcwd'):
261 dirname = 'getcwd-test-directory-0123456789abcdef-01234567890abcdef'
262 curdir = os.getcwd()
263 base_path = os.path.abspath(support.TESTFN) + '.getcwd'
264
265 try:
266 os.mkdir(base_path)
267 os.chdir(base_path)
268 except:
Benjamin Petersone549ead2009-03-28 21:42:05 +0000269# Just returning nothing instead of the SkipTest exception,
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000270# because the test results in Error in that case.
271# Is that ok?
Benjamin Petersone549ead2009-03-28 21:42:05 +0000272# raise unittest.SkipTest("cannot create directory for testing")
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000273 return
274
275 def _create_and_do_getcwd(dirname, current_path_length = 0):
276 try:
277 os.mkdir(dirname)
278 except:
Benjamin Petersone549ead2009-03-28 21:42:05 +0000279 raise unittest.SkipTest("mkdir cannot create directory sufficiently deep for getcwd test")
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000280
281 os.chdir(dirname)
282 try:
283 os.getcwd()
284 if current_path_length < 1027:
285 _create_and_do_getcwd(dirname, current_path_length + len(dirname) + 1)
286 finally:
287 os.chdir('..')
288 os.rmdir(dirname)
289
290 _create_and_do_getcwd(dirname)
291
292 finally:
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000293 os.chdir(curdir)
R. David Murray414c91f2009-07-09 20:12:31 +0000294 support.rmtree(base_path)
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000295
296
Neal Norwitze241ce82003-02-17 18:17:05 +0000297def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000298 support.run_unittest(PosixTester)
Neal Norwitze241ce82003-02-17 18:17:05 +0000299
300if __name__ == '__main__':
301 test_main()