|
1 "Test posix functions" |
|
2 |
|
3 from test import test_support |
|
4 |
|
5 try: |
|
6 import posix |
|
7 except ImportError: |
|
8 raise test_support.TestSkipped, "posix is not available" |
|
9 |
|
10 import time |
|
11 import os |
|
12 import sys |
|
13 import unittest |
|
14 import warnings |
|
15 warnings.filterwarnings('ignore', '.* potential security risk .*', |
|
16 RuntimeWarning) |
|
17 |
|
18 class PosixTester(unittest.TestCase): |
|
19 |
|
20 def setUp(self): |
|
21 # create empty file |
|
22 fp = open(test_support.TESTFN, 'w+') |
|
23 fp.close() |
|
24 |
|
25 def tearDown(self): |
|
26 os.unlink(test_support.TESTFN) |
|
27 |
|
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", |
|
32 "times", "getloadavg", "tmpnam", |
|
33 "getegid", "geteuid", "getgid", "getgroups", |
|
34 "getpid", "getpgrp", "getppid", "getuid", |
|
35 ] |
|
36 |
|
37 for name in NO_ARG_FUNCTIONS: |
|
38 posix_func = getattr(posix, name, None) |
|
39 if posix_func is not None: |
|
40 posix_func() |
|
41 self.assertRaises(TypeError, posix_func, 1) |
|
42 |
|
43 def test_statvfs(self): |
|
44 if hasattr(posix, 'statvfs'): |
|
45 self.assert_(posix.statvfs(os.curdir)) |
|
46 |
|
47 def test_fstatvfs(self): |
|
48 if hasattr(posix, 'fstatvfs'): |
|
49 fp = open(test_support.TESTFN) |
|
50 try: |
|
51 self.assert_(posix.fstatvfs(fp.fileno())) |
|
52 finally: |
|
53 fp.close() |
|
54 |
|
55 def test_ftruncate(self): |
|
56 if hasattr(posix, 'ftruncate'): |
|
57 fp = open(test_support.TESTFN, 'w+') |
|
58 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'): |
|
68 fp = open(test_support.TESTFN) |
|
69 try: |
|
70 fd = posix.dup(fp.fileno()) |
|
71 self.assert_(isinstance(fd, int)) |
|
72 os.close(fd) |
|
73 finally: |
|
74 fp.close() |
|
75 |
|
76 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 |
|
81 def test_dup2(self): |
|
82 if hasattr(posix, 'dup2'): |
|
83 fp1 = open(test_support.TESTFN) |
|
84 fp2 = open(test_support.TESTFN) |
|
85 try: |
|
86 posix.dup2(fp1.fileno(), fp2.fileno()) |
|
87 finally: |
|
88 fp1.close() |
|
89 fp2.close() |
|
90 |
|
91 def fdopen_helper(self, *args): |
|
92 fd = os.open(test_support.TESTFN, os.O_RDONLY) |
|
93 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 |
|
102 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 |
|
133 def test_fstat(self): |
|
134 if hasattr(posix, 'fstat'): |
|
135 fp = open(test_support.TESTFN) |
|
136 try: |
|
137 self.assert_(posix.fstat(fp.fileno())) |
|
138 finally: |
|
139 fp.close() |
|
140 |
|
141 def test_stat(self): |
|
142 if hasattr(posix, 'stat'): |
|
143 self.assert_(posix.stat(test_support.TESTFN)) |
|
144 |
|
145 def test_chdir(self): |
|
146 if hasattr(posix, 'chdir'): |
|
147 posix.chdir(os.curdir) |
|
148 self.assertRaises(OSError, posix.chdir, test_support.TESTFN) |
|
149 |
|
150 def test_lsdir(self): |
|
151 if hasattr(posix, 'lsdir'): |
|
152 self.assert_(test_support.TESTFN in posix.lsdir(os.curdir)) |
|
153 |
|
154 def test_access(self): |
|
155 if hasattr(posix, 'access'): |
|
156 self.assert_(posix.access(test_support.TESTFN, os.R_OK)) |
|
157 |
|
158 def test_umask(self): |
|
159 if hasattr(posix, 'umask'): |
|
160 old_mask = posix.umask(0) |
|
161 self.assert_(isinstance(old_mask, int)) |
|
162 posix.umask(old_mask) |
|
163 |
|
164 def test_strerror(self): |
|
165 if hasattr(posix, 'strerror'): |
|
166 self.assert_(posix.strerror(0)) |
|
167 |
|
168 def test_pipe(self): |
|
169 if hasattr(posix, 'pipe'): |
|
170 reader, writer = posix.pipe() |
|
171 os.close(reader) |
|
172 os.close(writer) |
|
173 |
|
174 def test_tempnam(self): |
|
175 if hasattr(posix, 'tempnam'): |
|
176 self.assert_(posix.tempnam()) |
|
177 self.assert_(posix.tempnam(os.curdir)) |
|
178 self.assert_(posix.tempnam(os.curdir, 'blah')) |
|
179 |
|
180 def test_tmpfile(self): |
|
181 if hasattr(posix, 'tmpfile'): |
|
182 fp = posix.tmpfile() |
|
183 fp.close() |
|
184 |
|
185 def test_utime(self): |
|
186 if hasattr(posix, 'utime'): |
|
187 now = time.time() |
|
188 posix.utime(test_support.TESTFN, None) |
|
189 self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, None)) |
|
190 self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (now, None)) |
|
191 self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, now)) |
|
192 posix.utime(test_support.TESTFN, (int(now), int(now))) |
|
193 posix.utime(test_support.TESTFN, (now, now)) |
|
194 |
|
195 def test_main(): |
|
196 test_support.run_unittest(PosixTester) |
|
197 |
|
198 if __name__ == '__main__': |
|
199 test_main() |