blob: 5f40060a6b521603073f460cb4ac4b0c5b550c17 [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
Neal Norwitze241ce82003-02-17 18:17:05 +000014import unittest
15import warnings
16warnings.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
44 def test_statvfs(self):
45 if hasattr(posix, 'statvfs'):
Neal Norwitz2ff51a82003-02-17 22:40:31 +000046 self.assert_(posix.statvfs(os.curdir))
Neal Norwitze241ce82003-02-17 18:17:05 +000047
48 def test_fstatvfs(self):
49 if hasattr(posix, 'fstatvfs'):
Walter Dörwald21d3a322003-05-01 17:45:56 +000050 fp = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +000051 try:
Neal Norwitz2ff51a82003-02-17 22:40:31 +000052 self.assert_(posix.fstatvfs(fp.fileno()))
Neal Norwitze241ce82003-02-17 18:17:05 +000053 finally:
54 fp.close()
55
56 def test_ftruncate(self):
57 if hasattr(posix, 'ftruncate'):
Walter Dörwald21d3a322003-05-01 17:45:56 +000058 fp = open(test_support.TESTFN, 'w+')
Neal Norwitze241ce82003-02-17 18:17:05 +000059 try:
60 # we need to have some data to truncate
61 fp.write('test')
62 fp.flush()
63 posix.ftruncate(fp.fileno(), 0)
64 finally:
65 fp.close()
66
67 def test_dup(self):
68 if hasattr(posix, 'dup'):
Walter Dörwald21d3a322003-05-01 17:45:56 +000069 fp = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +000070 try:
71 fd = posix.dup(fp.fileno())
Neal Norwitz2ff51a82003-02-17 22:40:31 +000072 self.assert_(isinstance(fd, int))
Neal Norwitze241ce82003-02-17 18:17:05 +000073 os.close(fd)
74 finally:
75 fp.close()
76
Skip Montanaro94785ef2006-04-20 01:29:48 +000077 def test_confstr(self):
78 if hasattr(posix, 'confstr'):
79 self.assertRaises(ValueError, posix.confstr, "CS_garbage")
80 self.assertEqual(len(posix.confstr("CS_PATH")) > 0, True)
81
Neal Norwitze241ce82003-02-17 18:17:05 +000082 def test_dup2(self):
83 if hasattr(posix, 'dup2'):
Walter Dörwald21d3a322003-05-01 17:45:56 +000084 fp1 = open(test_support.TESTFN)
85 fp2 = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +000086 try:
87 posix.dup2(fp1.fileno(), fp2.fileno())
88 finally:
89 fp1.close()
90 fp2.close()
91
92 def fdopen_helper(self, *args):
Walter Dörwald21d3a322003-05-01 17:45:56 +000093 fd = os.open(test_support.TESTFN, os.O_RDONLY)
Neal Norwitze241ce82003-02-17 18:17:05 +000094 fp2 = posix.fdopen(fd, *args)
95 fp2.close()
96
97 def test_fdopen(self):
98 if hasattr(posix, 'fdopen'):
99 self.fdopen_helper()
100 self.fdopen_helper('r')
101 self.fdopen_helper('r', 100)
102
Skip Montanaro98470002005-06-17 01:14:49 +0000103 def test_osexlock(self):
104 if hasattr(posix, "O_EXLOCK"):
105 fd = os.open(test_support.TESTFN,
106 os.O_WRONLY|os.O_EXLOCK|os.O_CREAT)
107 self.assertRaises(OSError, os.open, test_support.TESTFN,
108 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
109 os.close(fd)
110
111 if hasattr(posix, "O_SHLOCK"):
112 fd = os.open(test_support.TESTFN,
113 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
114 self.assertRaises(OSError, os.open, test_support.TESTFN,
115 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
116 os.close(fd)
117
118 def test_osshlock(self):
119 if hasattr(posix, "O_SHLOCK"):
120 fd1 = os.open(test_support.TESTFN,
121 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
122 fd2 = os.open(test_support.TESTFN,
123 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
124 os.close(fd2)
125 os.close(fd1)
126
127 if hasattr(posix, "O_EXLOCK"):
128 fd = os.open(test_support.TESTFN,
129 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
130 self.assertRaises(OSError, os.open, test_support.TESTFN,
131 os.O_RDONLY|os.O_EXLOCK|os.O_NONBLOCK)
132 os.close(fd)
133
Neal Norwitze241ce82003-02-17 18:17:05 +0000134 def test_fstat(self):
135 if hasattr(posix, 'fstat'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000136 fp = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000137 try:
Neal Norwitz2ff51a82003-02-17 22:40:31 +0000138 self.assert_(posix.fstat(fp.fileno()))
Neal Norwitze241ce82003-02-17 18:17:05 +0000139 finally:
140 fp.close()
141
142 def test_stat(self):
143 if hasattr(posix, 'stat'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000144 self.assert_(posix.stat(test_support.TESTFN))
Neal Norwitze241ce82003-02-17 18:17:05 +0000145
Gregory P. Smithf48da8f2008-03-18 19:05:32 +0000146 if hasattr(posix, 'chown'):
147 def test_chown(self):
148 # raise an OSError if the file does not exist
149 os.unlink(test_support.TESTFN)
150 self.assertRaises(OSError, posix.chown, test_support.TESTFN, -1, -1)
151
152 # re-create the file
153 open(test_support.TESTFN, 'w').close()
154 if os.getuid() == 0:
155 try:
156 # Many linux distros have a nfsnobody user as MAX_UID-2
157 # that makes a good test case for signedness issues.
158 # http://bugs.python.org/issue1747858
159 # This part of the test only runs when run as root.
160 # Only scary people run their tests as root.
161 ent = pwd.getpwnam('nfsnobody')
162 posix.chown(test_support.TESTFN, ent.pw_uid, ent.pw_gid)
163 except KeyError:
164 pass
165 else:
166 # non-root cannot chown to root, raises OSError
167 self.assertRaises(OSError, posix.chown,
168 test_support.TESTFN, 0, 0)
169
170 # test a successful chown call
171 posix.chown(test_support.TESTFN, os.getuid(), os.getgid())
172
Neal Norwitze241ce82003-02-17 18:17:05 +0000173 def test_chdir(self):
174 if hasattr(posix, 'chdir'):
175 posix.chdir(os.curdir)
Walter Dörwald21d3a322003-05-01 17:45:56 +0000176 self.assertRaises(OSError, posix.chdir, test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000177
178 def test_lsdir(self):
179 if hasattr(posix, 'lsdir'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000180 self.assert_(test_support.TESTFN in posix.lsdir(os.curdir))
Neal Norwitze241ce82003-02-17 18:17:05 +0000181
182 def test_access(self):
183 if hasattr(posix, 'access'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000184 self.assert_(posix.access(test_support.TESTFN, os.R_OK))
Neal Norwitze241ce82003-02-17 18:17:05 +0000185
186 def test_umask(self):
187 if hasattr(posix, 'umask'):
188 old_mask = posix.umask(0)
Neal Norwitz2ff51a82003-02-17 22:40:31 +0000189 self.assert_(isinstance(old_mask, int))
Neal Norwitze241ce82003-02-17 18:17:05 +0000190 posix.umask(old_mask)
191
192 def test_strerror(self):
193 if hasattr(posix, 'strerror'):
Neal Norwitz2ff51a82003-02-17 22:40:31 +0000194 self.assert_(posix.strerror(0))
Neal Norwitze241ce82003-02-17 18:17:05 +0000195
196 def test_pipe(self):
197 if hasattr(posix, 'pipe'):
198 reader, writer = posix.pipe()
199 os.close(reader)
200 os.close(writer)
201
202 def test_tempnam(self):
203 if hasattr(posix, 'tempnam'):
Neal Norwitz2ff51a82003-02-17 22:40:31 +0000204 self.assert_(posix.tempnam())
205 self.assert_(posix.tempnam(os.curdir))
206 self.assert_(posix.tempnam(os.curdir, 'blah'))
Neal Norwitze241ce82003-02-17 18:17:05 +0000207
208 def test_tmpfile(self):
209 if hasattr(posix, 'tmpfile'):
210 fp = posix.tmpfile()
211 fp.close()
212
213 def test_utime(self):
214 if hasattr(posix, 'utime'):
215 now = time.time()
Walter Dörwald21d3a322003-05-01 17:45:56 +0000216 posix.utime(test_support.TESTFN, None)
Neal Norwitzc28e7ad2004-06-06 20:27:05 +0000217 self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, None))
218 self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (now, None))
219 self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, now))
220 posix.utime(test_support.TESTFN, (int(now), int(now)))
Walter Dörwald21d3a322003-05-01 17:45:56 +0000221 posix.utime(test_support.TESTFN, (now, now))
Neal Norwitze241ce82003-02-17 18:17:05 +0000222
Martin v. Löwis382abef2007-02-19 10:55:19 +0000223 def test_chflags(self):
224 if hasattr(posix, 'chflags'):
225 st = os.stat(test_support.TESTFN)
226 if hasattr(st, 'st_flags'):
227 posix.chflags(test_support.TESTFN, st.st_flags)
228
229 def test_lchflags(self):
230 if hasattr(posix, 'lchflags'):
231 st = os.stat(test_support.TESTFN)
232 if hasattr(st, 'st_flags'):
233 posix.lchflags(test_support.TESTFN, st.st_flags)
234
Facundo Batista5596b0c2008-06-22 13:36:20 +0000235 def test_getcwd_long_pathnames(self):
236 if hasattr(posix, 'getcwd'):
237 dirname = 'getcwd-test-directory-0123456789abcdef-01234567890abcdef'
238 curdir = os.getcwd()
Facundo Batista96f3dc32008-06-22 18:23:55 +0000239 base_path = os.path.abspath(test_support.TESTFN) + '.getcwd'
Facundo Batista5596b0c2008-06-22 13:36:20 +0000240
241 try:
242 os.mkdir(base_path)
243 os.chdir(base_path)
Facundo Batista96f3dc32008-06-22 18:23:55 +0000244 except:
Facundo Batista2694eb02008-06-22 19:35:24 +0000245# Just returning nothing instead of the TestSkipped exception,
246# because the test results in Error in that case.
247# Is that ok?
248# raise test_support.TestSkipped, "cannot create directory for testing"
249 return
Facundo Batista5596b0c2008-06-22 13:36:20 +0000250
Facundo Batista96f3dc32008-06-22 18:23:55 +0000251 try:
Facundo Batista5596b0c2008-06-22 13:36:20 +0000252 def _create_and_do_getcwd(dirname, current_path_length = 0):
253 try:
254 os.mkdir(dirname)
255 except:
256 raise test_support.TestSkipped, "mkdir cannot create directory sufficiently deep for getcwd test"
257
258 os.chdir(dirname)
259 try:
260 os.getcwd()
261 if current_path_length < 1027:
262 _create_and_do_getcwd(dirname, current_path_length + len(dirname) + 1)
263 finally:
264 os.chdir('..')
265 os.rmdir(dirname)
266
267 _create_and_do_getcwd(dirname)
268
269 finally:
270 shutil.rmtree(base_path)
271 os.chdir(curdir)
272
273
Neal Norwitze241ce82003-02-17 18:17:05 +0000274def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000275 test_support.run_unittest(PosixTester)
Neal Norwitze241ce82003-02-17 18:17:05 +0000276
277if __name__ == '__main__':
278 test_main()