blob: a7255c41c567d31ce5869698e03cc1eab5c9c5d4 [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
Neal Norwitze241ce82003-02-17 18:17:05 +00005
6import time
7import os
Gregory P. Smithf48da8f2008-03-18 19:05:32 +00008import pwd
Facundo Batista5596b0c2008-06-22 13:36:20 +00009import shutil
Neal Norwitze241ce82003-02-17 18:17:05 +000010import unittest
11import warnings
R. David Murray59beec32009-03-30 19:04:00 +000012
13posix = test_support.import_module('posix')
14
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
Walter Dörwald21d3a322003-05-01 17:45:56 +000022 fp = open(test_support.TESTFN, 'w+')
Neal Norwitze241ce82003-02-17 18:17:05 +000023 fp.close()
24
25 def tearDown(self):
Walter Dörwald21d3a322003-05-01 17:45:56 +000026 os.unlink(test_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)
31 NO_ARG_FUNCTIONS = [ "ctermid", "getcwd", "getcwdu", "uname",
Neal Norwitz71b13e82003-02-23 22:12:24 +000032 "times", "getloadavg", "tmpnam",
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
43 def test_statvfs(self):
44 if hasattr(posix, 'statvfs'):
Neal Norwitz2ff51a82003-02-17 22:40:31 +000045 self.assert_(posix.statvfs(os.curdir))
Neal Norwitze241ce82003-02-17 18:17:05 +000046
47 def test_fstatvfs(self):
48 if hasattr(posix, 'fstatvfs'):
Walter Dörwald21d3a322003-05-01 17:45:56 +000049 fp = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +000050 try:
Neal Norwitz2ff51a82003-02-17 22:40:31 +000051 self.assert_(posix.fstatvfs(fp.fileno()))
Neal Norwitze241ce82003-02-17 18:17:05 +000052 finally:
53 fp.close()
54
55 def test_ftruncate(self):
56 if hasattr(posix, 'ftruncate'):
Walter Dörwald21d3a322003-05-01 17:45:56 +000057 fp = open(test_support.TESTFN, 'w+')
Neal Norwitze241ce82003-02-17 18:17:05 +000058 try:
59 # we need to have some data to truncate
60 fp.write('test')
61 fp.flush()
62 posix.ftruncate(fp.fileno(), 0)
63 finally:
64 fp.close()
65
66 def test_dup(self):
67 if hasattr(posix, 'dup'):
Walter Dörwald21d3a322003-05-01 17:45:56 +000068 fp = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +000069 try:
70 fd = posix.dup(fp.fileno())
Neal Norwitz2ff51a82003-02-17 22:40:31 +000071 self.assert_(isinstance(fd, int))
Neal Norwitze241ce82003-02-17 18:17:05 +000072 os.close(fd)
73 finally:
74 fp.close()
75
Skip Montanaro94785ef2006-04-20 01:29:48 +000076 def test_confstr(self):
77 if hasattr(posix, 'confstr'):
78 self.assertRaises(ValueError, posix.confstr, "CS_garbage")
79 self.assertEqual(len(posix.confstr("CS_PATH")) > 0, True)
80
Neal Norwitze241ce82003-02-17 18:17:05 +000081 def test_dup2(self):
82 if hasattr(posix, 'dup2'):
Walter Dörwald21d3a322003-05-01 17:45:56 +000083 fp1 = open(test_support.TESTFN)
84 fp2 = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +000085 try:
86 posix.dup2(fp1.fileno(), fp2.fileno())
87 finally:
88 fp1.close()
89 fp2.close()
90
91 def fdopen_helper(self, *args):
Walter Dörwald21d3a322003-05-01 17:45:56 +000092 fd = os.open(test_support.TESTFN, os.O_RDONLY)
Neal Norwitze241ce82003-02-17 18:17:05 +000093 fp2 = posix.fdopen(fd, *args)
94 fp2.close()
95
96 def test_fdopen(self):
97 if hasattr(posix, 'fdopen'):
98 self.fdopen_helper()
99 self.fdopen_helper('r')
100 self.fdopen_helper('r', 100)
101
Skip Montanaro98470002005-06-17 01:14:49 +0000102 def test_osexlock(self):
103 if hasattr(posix, "O_EXLOCK"):
104 fd = os.open(test_support.TESTFN,
105 os.O_WRONLY|os.O_EXLOCK|os.O_CREAT)
106 self.assertRaises(OSError, os.open, test_support.TESTFN,
107 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
108 os.close(fd)
109
110 if hasattr(posix, "O_SHLOCK"):
111 fd = os.open(test_support.TESTFN,
112 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
113 self.assertRaises(OSError, os.open, test_support.TESTFN,
114 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
115 os.close(fd)
116
117 def test_osshlock(self):
118 if hasattr(posix, "O_SHLOCK"):
119 fd1 = os.open(test_support.TESTFN,
120 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
121 fd2 = os.open(test_support.TESTFN,
122 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
123 os.close(fd2)
124 os.close(fd1)
125
126 if hasattr(posix, "O_EXLOCK"):
127 fd = os.open(test_support.TESTFN,
128 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
129 self.assertRaises(OSError, os.open, test_support.TESTFN,
130 os.O_RDONLY|os.O_EXLOCK|os.O_NONBLOCK)
131 os.close(fd)
132
Neal Norwitze241ce82003-02-17 18:17:05 +0000133 def test_fstat(self):
134 if hasattr(posix, 'fstat'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000135 fp = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000136 try:
Neal Norwitz2ff51a82003-02-17 22:40:31 +0000137 self.assert_(posix.fstat(fp.fileno()))
Neal Norwitze241ce82003-02-17 18:17:05 +0000138 finally:
139 fp.close()
140
141 def test_stat(self):
142 if hasattr(posix, 'stat'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000143 self.assert_(posix.stat(test_support.TESTFN))
Neal Norwitze241ce82003-02-17 18:17:05 +0000144
Gregory P. Smithf48da8f2008-03-18 19:05:32 +0000145 if hasattr(posix, 'chown'):
146 def test_chown(self):
147 # raise an OSError if the file does not exist
148 os.unlink(test_support.TESTFN)
149 self.assertRaises(OSError, posix.chown, test_support.TESTFN, -1, -1)
150
151 # re-create the file
152 open(test_support.TESTFN, 'w').close()
153 if os.getuid() == 0:
154 try:
155 # Many linux distros have a nfsnobody user as MAX_UID-2
156 # that makes a good test case for signedness issues.
157 # http://bugs.python.org/issue1747858
158 # This part of the test only runs when run as root.
159 # Only scary people run their tests as root.
160 ent = pwd.getpwnam('nfsnobody')
161 posix.chown(test_support.TESTFN, ent.pw_uid, ent.pw_gid)
162 except KeyError:
163 pass
164 else:
165 # non-root cannot chown to root, raises OSError
166 self.assertRaises(OSError, posix.chown,
167 test_support.TESTFN, 0, 0)
168
169 # test a successful chown call
170 posix.chown(test_support.TESTFN, os.getuid(), os.getgid())
171
Neal Norwitze241ce82003-02-17 18:17:05 +0000172 def test_chdir(self):
173 if hasattr(posix, 'chdir'):
174 posix.chdir(os.curdir)
Walter Dörwald21d3a322003-05-01 17:45:56 +0000175 self.assertRaises(OSError, posix.chdir, test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000176
177 def test_lsdir(self):
178 if hasattr(posix, 'lsdir'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000179 self.assert_(test_support.TESTFN in posix.lsdir(os.curdir))
Neal Norwitze241ce82003-02-17 18:17:05 +0000180
181 def test_access(self):
182 if hasattr(posix, 'access'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000183 self.assert_(posix.access(test_support.TESTFN, os.R_OK))
Neal Norwitze241ce82003-02-17 18:17:05 +0000184
185 def test_umask(self):
186 if hasattr(posix, 'umask'):
187 old_mask = posix.umask(0)
Neal Norwitz2ff51a82003-02-17 22:40:31 +0000188 self.assert_(isinstance(old_mask, int))
Neal Norwitze241ce82003-02-17 18:17:05 +0000189 posix.umask(old_mask)
190
191 def test_strerror(self):
192 if hasattr(posix, 'strerror'):
Neal Norwitz2ff51a82003-02-17 22:40:31 +0000193 self.assert_(posix.strerror(0))
Neal Norwitze241ce82003-02-17 18:17:05 +0000194
195 def test_pipe(self):
196 if hasattr(posix, 'pipe'):
197 reader, writer = posix.pipe()
198 os.close(reader)
199 os.close(writer)
200
201 def test_tempnam(self):
202 if hasattr(posix, 'tempnam'):
Neal Norwitz2ff51a82003-02-17 22:40:31 +0000203 self.assert_(posix.tempnam())
204 self.assert_(posix.tempnam(os.curdir))
205 self.assert_(posix.tempnam(os.curdir, 'blah'))
Neal Norwitze241ce82003-02-17 18:17:05 +0000206
207 def test_tmpfile(self):
208 if hasattr(posix, 'tmpfile'):
209 fp = posix.tmpfile()
210 fp.close()
211
212 def test_utime(self):
213 if hasattr(posix, 'utime'):
214 now = time.time()
Walter Dörwald21d3a322003-05-01 17:45:56 +0000215 posix.utime(test_support.TESTFN, None)
Neal Norwitzc28e7ad2004-06-06 20:27:05 +0000216 self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, None))
217 self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (now, None))
218 self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, now))
219 posix.utime(test_support.TESTFN, (int(now), int(now)))
Walter Dörwald21d3a322003-05-01 17:45:56 +0000220 posix.utime(test_support.TESTFN, (now, now))
Neal Norwitze241ce82003-02-17 18:17:05 +0000221
Martin v. Löwis382abef2007-02-19 10:55:19 +0000222 def test_chflags(self):
223 if hasattr(posix, 'chflags'):
224 st = os.stat(test_support.TESTFN)
225 if hasattr(st, 'st_flags'):
226 posix.chflags(test_support.TESTFN, st.st_flags)
227
228 def test_lchflags(self):
229 if hasattr(posix, 'lchflags'):
230 st = os.stat(test_support.TESTFN)
231 if hasattr(st, 'st_flags'):
232 posix.lchflags(test_support.TESTFN, st.st_flags)
233
Facundo Batista5596b0c2008-06-22 13:36:20 +0000234 def test_getcwd_long_pathnames(self):
235 if hasattr(posix, 'getcwd'):
236 dirname = 'getcwd-test-directory-0123456789abcdef-01234567890abcdef'
237 curdir = os.getcwd()
Facundo Batista96f3dc32008-06-22 18:23:55 +0000238 base_path = os.path.abspath(test_support.TESTFN) + '.getcwd'
Facundo Batista5596b0c2008-06-22 13:36:20 +0000239
240 try:
241 os.mkdir(base_path)
242 os.chdir(base_path)
Facundo Batista96f3dc32008-06-22 18:23:55 +0000243 except:
Benjamin Peterson888a39b2009-03-26 20:48:25 +0000244# Just returning nothing instead of the SkipTest exception,
Facundo Batista2694eb02008-06-22 19:35:24 +0000245# because the test results in Error in that case.
246# Is that ok?
Benjamin Peterson888a39b2009-03-26 20:48:25 +0000247# raise unittest.SkipTest, "cannot create directory for testing"
Facundo Batista2694eb02008-06-22 19:35:24 +0000248 return
Facundo Batista5596b0c2008-06-22 13:36:20 +0000249
Facundo Batista96f3dc32008-06-22 18:23:55 +0000250 try:
Facundo Batista5596b0c2008-06-22 13:36:20 +0000251 def _create_and_do_getcwd(dirname, current_path_length = 0):
252 try:
253 os.mkdir(dirname)
254 except:
Benjamin Peterson888a39b2009-03-26 20:48:25 +0000255 raise unittest.SkipTest, "mkdir cannot create directory sufficiently deep for getcwd test"
Facundo Batista5596b0c2008-06-22 13:36:20 +0000256
257 os.chdir(dirname)
258 try:
259 os.getcwd()
260 if current_path_length < 1027:
261 _create_and_do_getcwd(dirname, current_path_length + len(dirname) + 1)
262 finally:
263 os.chdir('..')
264 os.rmdir(dirname)
265
266 _create_and_do_getcwd(dirname)
267
268 finally:
269 shutil.rmtree(base_path)
270 os.chdir(curdir)
271
272
Neal Norwitze241ce82003-02-17 18:17:05 +0000273def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000274 test_support.run_unittest(PosixTester)
Neal Norwitze241ce82003-02-17 18:17:05 +0000275
276if __name__ == '__main__':
277 test_main()