Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1 | "Test posix functions" |
| 2 | |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 3 | from test import test_support |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4 | |
| 5 | try: |
| 6 | import posix |
| 7 | except ImportError: |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 8 | raise test_support.TestSkipped, "posix is not available" |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 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 |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 22 | fp = open(test_support.TESTFN, 'w+') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 23 | fp.close() |
| 24 | |
| 25 | def tearDown(self): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 26 | os.unlink(test_support.TESTFN) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 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 Norwitz | 71b13e8 | 2003-02-23 22:12:24 +0000 | [diff] [blame] | 32 | "times", "getloadavg", "tmpnam", |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 33 | "getegid", "geteuid", "getgid", "getgroups", |
| 34 | "getpid", "getpgrp", "getppid", "getuid", |
| 35 | ] |
Neal Norwitz | 71b13e8 | 2003-02-23 22:12:24 +0000 | [diff] [blame] | 36 | |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 37 | for name in NO_ARG_FUNCTIONS: |
| 38 | posix_func = getattr(posix, name, None) |
| 39 | if posix_func is not None: |
| 40 | posix_func() |
Neal Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 41 | self.assertRaises(TypeError, posix_func, 1) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 42 | |
| 43 | def test_statvfs(self): |
| 44 | if hasattr(posix, 'statvfs'): |
Neal Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 45 | self.assert_(posix.statvfs(os.curdir)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 46 | |
| 47 | def test_fstatvfs(self): |
| 48 | if hasattr(posix, 'fstatvfs'): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 49 | fp = open(test_support.TESTFN) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 50 | try: |
Neal Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 51 | self.assert_(posix.fstatvfs(fp.fileno())) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 52 | finally: |
| 53 | fp.close() |
| 54 | |
| 55 | def test_ftruncate(self): |
| 56 | if hasattr(posix, 'ftruncate'): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 57 | fp = open(test_support.TESTFN, 'w+') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 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'): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 68 | fp = open(test_support.TESTFN) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 69 | try: |
| 70 | fd = posix.dup(fp.fileno()) |
Neal Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 71 | self.assert_(isinstance(fd, int)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 72 | os.close(fd) |
| 73 | finally: |
| 74 | fp.close() |
| 75 | |
| 76 | def test_dup2(self): |
| 77 | if hasattr(posix, 'dup2'): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 78 | fp1 = open(test_support.TESTFN) |
| 79 | fp2 = open(test_support.TESTFN) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 80 | try: |
| 81 | posix.dup2(fp1.fileno(), fp2.fileno()) |
| 82 | finally: |
| 83 | fp1.close() |
| 84 | fp2.close() |
| 85 | |
| 86 | def fdopen_helper(self, *args): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 87 | fd = os.open(test_support.TESTFN, os.O_RDONLY) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 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 | |
Skip Montanaro | 9847000 | 2005-06-17 01:14:49 +0000 | [diff] [blame^] | 97 | def test_osexlock(self): |
| 98 | if hasattr(posix, "O_EXLOCK"): |
| 99 | fd = os.open(test_support.TESTFN, |
| 100 | os.O_WRONLY|os.O_EXLOCK|os.O_CREAT) |
| 101 | self.assertRaises(OSError, os.open, test_support.TESTFN, |
| 102 | os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK) |
| 103 | os.close(fd) |
| 104 | |
| 105 | if hasattr(posix, "O_SHLOCK"): |
| 106 | fd = os.open(test_support.TESTFN, |
| 107 | os.O_WRONLY|os.O_SHLOCK|os.O_CREAT) |
| 108 | self.assertRaises(OSError, os.open, test_support.TESTFN, |
| 109 | os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK) |
| 110 | os.close(fd) |
| 111 | |
| 112 | def test_osshlock(self): |
| 113 | if hasattr(posix, "O_SHLOCK"): |
| 114 | fd1 = os.open(test_support.TESTFN, |
| 115 | os.O_WRONLY|os.O_SHLOCK|os.O_CREAT) |
| 116 | fd2 = os.open(test_support.TESTFN, |
| 117 | os.O_WRONLY|os.O_SHLOCK|os.O_CREAT) |
| 118 | os.close(fd2) |
| 119 | os.close(fd1) |
| 120 | |
| 121 | if hasattr(posix, "O_EXLOCK"): |
| 122 | fd = os.open(test_support.TESTFN, |
| 123 | os.O_WRONLY|os.O_SHLOCK|os.O_CREAT) |
| 124 | self.assertRaises(OSError, os.open, test_support.TESTFN, |
| 125 | os.O_RDONLY|os.O_EXLOCK|os.O_NONBLOCK) |
| 126 | os.close(fd) |
| 127 | |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 128 | def test_fstat(self): |
| 129 | if hasattr(posix, 'fstat'): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 130 | fp = open(test_support.TESTFN) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 131 | try: |
Neal Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 132 | self.assert_(posix.fstat(fp.fileno())) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 133 | finally: |
| 134 | fp.close() |
| 135 | |
| 136 | def test_stat(self): |
| 137 | if hasattr(posix, 'stat'): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 138 | self.assert_(posix.stat(test_support.TESTFN)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 139 | |
| 140 | def test_chdir(self): |
| 141 | if hasattr(posix, 'chdir'): |
| 142 | posix.chdir(os.curdir) |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 143 | self.assertRaises(OSError, posix.chdir, test_support.TESTFN) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 144 | |
| 145 | def test_lsdir(self): |
| 146 | if hasattr(posix, 'lsdir'): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 147 | self.assert_(test_support.TESTFN in posix.lsdir(os.curdir)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 148 | |
| 149 | def test_access(self): |
| 150 | if hasattr(posix, 'access'): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 151 | self.assert_(posix.access(test_support.TESTFN, os.R_OK)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 152 | |
| 153 | def test_umask(self): |
| 154 | if hasattr(posix, 'umask'): |
| 155 | old_mask = posix.umask(0) |
Neal Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 156 | self.assert_(isinstance(old_mask, int)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 157 | posix.umask(old_mask) |
| 158 | |
| 159 | def test_strerror(self): |
| 160 | if hasattr(posix, 'strerror'): |
Neal Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 161 | self.assert_(posix.strerror(0)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 162 | |
| 163 | def test_pipe(self): |
| 164 | if hasattr(posix, 'pipe'): |
| 165 | reader, writer = posix.pipe() |
| 166 | os.close(reader) |
| 167 | os.close(writer) |
| 168 | |
| 169 | def test_tempnam(self): |
| 170 | if hasattr(posix, 'tempnam'): |
Neal Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 171 | self.assert_(posix.tempnam()) |
| 172 | self.assert_(posix.tempnam(os.curdir)) |
| 173 | self.assert_(posix.tempnam(os.curdir, 'blah')) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 174 | |
| 175 | def test_tmpfile(self): |
| 176 | if hasattr(posix, 'tmpfile'): |
| 177 | fp = posix.tmpfile() |
| 178 | fp.close() |
| 179 | |
| 180 | def test_utime(self): |
| 181 | if hasattr(posix, 'utime'): |
| 182 | now = time.time() |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 183 | posix.utime(test_support.TESTFN, None) |
Neal Norwitz | c28e7ad | 2004-06-06 20:27:05 +0000 | [diff] [blame] | 184 | self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, None)) |
| 185 | self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (now, None)) |
| 186 | self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, now)) |
| 187 | posix.utime(test_support.TESTFN, (int(now), int(now))) |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 188 | posix.utime(test_support.TESTFN, (now, now)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 189 | |
| 190 | def test_main(): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 191 | test_support.run_unittest(PosixTester) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 192 | |
| 193 | if __name__ == '__main__': |
| 194 | test_main() |