blob: 5a0028c870c7cad2547d9ec31761fccb77236b39 [file] [log] [blame]
Neal Norwitze241ce82003-02-17 18:17:05 +00001"Test posix functions"
2
Tim Peters003eb302003-02-17 21:48:48 +00003from test.test_support import TestSkipped, TestFailed, TESTFN, run_suite
Neal Norwitze241ce82003-02-17 18:17:05 +00004
5try:
6 import posix
7except ImportError:
8 raise TestSkipped, "posix is not available"
9
10import time
11import os
12import sys
13import unittest
14import warnings
15warnings.filterwarnings('ignore', '.* potential security risk .*',
16 RuntimeWarning)
17
18class PosixTester(unittest.TestCase):
19
20 def setUp(self):
21 # create empty file
22 fp = open(TESTFN, 'w+')
23 fp.close()
24
25 def tearDown(self):
26 os.unlink(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",
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'):
49 fp = open(TESTFN)
50 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'):
57 fp = open(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(TESTFN)
69 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
76 def test_dup2(self):
77 if hasattr(posix, 'dup2'):
78 fp1 = open(TESTFN)
79 fp2 = open(TESTFN)
80 try:
81 posix.dup2(fp1.fileno(), fp2.fileno())
82 finally:
83 fp1.close()
84 fp2.close()
85
86 def fdopen_helper(self, *args):
87 fd = os.open(TESTFN, os.O_RDONLY)
88 fp2 = posix.fdopen(fd, *args)
89 fp2.close()
90
91 def test_fdopen(self):
92 if hasattr(posix, 'fdopen'):
93 self.fdopen_helper()
94 self.fdopen_helper('r')
95 self.fdopen_helper('r', 100)
96
97 def test_fstat(self):
98 if hasattr(posix, 'fstat'):
99 fp = open(TESTFN)
100 try:
Neal Norwitz2ff51a82003-02-17 22:40:31 +0000101 self.assert_(posix.fstat(fp.fileno()))
Neal Norwitze241ce82003-02-17 18:17:05 +0000102 finally:
103 fp.close()
104
105 def test_stat(self):
106 if hasattr(posix, 'stat'):
Neal Norwitz2ff51a82003-02-17 22:40:31 +0000107 self.assert_(posix.stat(TESTFN))
Neal Norwitze241ce82003-02-17 18:17:05 +0000108
109 def test_chdir(self):
110 if hasattr(posix, 'chdir'):
111 posix.chdir(os.curdir)
Neal Norwitz2ff51a82003-02-17 22:40:31 +0000112 self.assertRaises(OSError, posix.chdir, TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000113
114 def test_lsdir(self):
115 if hasattr(posix, 'lsdir'):
Neal Norwitz2ff51a82003-02-17 22:40:31 +0000116 self.assert_(TESTFN in posix.lsdir(os.curdir))
Neal Norwitze241ce82003-02-17 18:17:05 +0000117
118 def test_access(self):
119 if hasattr(posix, 'access'):
Neal Norwitz2ff51a82003-02-17 22:40:31 +0000120 self.assert_(posix.access(TESTFN, os.R_OK))
Neal Norwitze241ce82003-02-17 18:17:05 +0000121
122 def test_umask(self):
123 if hasattr(posix, 'umask'):
124 old_mask = posix.umask(0)
Neal Norwitz2ff51a82003-02-17 22:40:31 +0000125 self.assert_(isinstance(old_mask, int))
Neal Norwitze241ce82003-02-17 18:17:05 +0000126 posix.umask(old_mask)
127
128 def test_strerror(self):
129 if hasattr(posix, 'strerror'):
Neal Norwitz2ff51a82003-02-17 22:40:31 +0000130 self.assert_(posix.strerror(0))
Neal Norwitze241ce82003-02-17 18:17:05 +0000131
132 def test_pipe(self):
133 if hasattr(posix, 'pipe'):
134 reader, writer = posix.pipe()
135 os.close(reader)
136 os.close(writer)
137
138 def test_tempnam(self):
139 if hasattr(posix, 'tempnam'):
Neal Norwitz2ff51a82003-02-17 22:40:31 +0000140 self.assert_(posix.tempnam())
141 self.assert_(posix.tempnam(os.curdir))
142 self.assert_(posix.tempnam(os.curdir, 'blah'))
Neal Norwitze241ce82003-02-17 18:17:05 +0000143
144 def test_tmpfile(self):
145 if hasattr(posix, 'tmpfile'):
146 fp = posix.tmpfile()
147 fp.close()
148
149 def test_utime(self):
150 if hasattr(posix, 'utime'):
151 now = time.time()
152 posix.utime(TESTFN, None)
153 posix.utime(TESTFN, (now, now))
154
155def test_main():
156 suite = unittest.TestSuite()
157 suite.addTest(unittest.makeSuite(PosixTester))
158 run_suite(suite)
159
160if __name__ == '__main__':
161 test_main()