|
| 1 | +"Test posix functions" |
| 2 | + |
| 3 | +from test_support import TestSkipped, TestFailed, TESTFN, run_suite |
| 4 | + |
| 5 | +try: |
| 6 | + import posix |
| 7 | +except ImportError: |
| 8 | + raise 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(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", |
| 32 | + "times", "getlogin", "getloadavg", "tmpnam", |
| 33 | + "getegid", "geteuid", "getgid", "getgroups", |
| 34 | + "getpid", "getpgrp", "getppid", "getuid", |
| 35 | + ] |
| 36 | + for name in NO_ARG_FUNCTIONS: |
| 37 | + posix_func = getattr(posix, name, None) |
| 38 | + if posix_func is not None: |
| 39 | + posix_func() |
| 40 | + try: |
| 41 | + posix_func(1) |
| 42 | + except TypeError: |
| 43 | + pass |
| 44 | + else: |
| 45 | + raise TestFailed, '%s should take no arguments' % name |
| 46 | + |
| 47 | + def test_statvfs(self): |
| 48 | + if hasattr(posix, 'statvfs'): |
| 49 | + posix.statvfs(os.curdir) |
| 50 | + |
| 51 | + def test_fstatvfs(self): |
| 52 | + if hasattr(posix, 'fstatvfs'): |
| 53 | + fp = open(TESTFN) |
| 54 | + try: |
| 55 | + posix.fstatvfs(fp.fileno()) |
| 56 | + finally: |
| 57 | + fp.close() |
| 58 | + |
| 59 | + def test_ftruncate(self): |
| 60 | + if hasattr(posix, 'ftruncate'): |
| 61 | + fp = open(TESTFN, 'w+') |
| 62 | + try: |
| 63 | + # we need to have some data to truncate |
| 64 | + fp.write('test') |
| 65 | + fp.flush() |
| 66 | + posix.ftruncate(fp.fileno(), 0) |
| 67 | + finally: |
| 68 | + fp.close() |
| 69 | + |
| 70 | + def test_dup(self): |
| 71 | + if hasattr(posix, 'dup'): |
| 72 | + fp = open(TESTFN) |
| 73 | + try: |
| 74 | + fd = posix.dup(fp.fileno()) |
| 75 | + os.close(fd) |
| 76 | + finally: |
| 77 | + fp.close() |
| 78 | + |
| 79 | + def test_dup2(self): |
| 80 | + if hasattr(posix, 'dup2'): |
| 81 | + fp1 = open(TESTFN) |
| 82 | + fp2 = open(TESTFN) |
| 83 | + try: |
| 84 | + posix.dup2(fp1.fileno(), fp2.fileno()) |
| 85 | + finally: |
| 86 | + fp1.close() |
| 87 | + fp2.close() |
| 88 | + |
| 89 | + def fdopen_helper(self, *args): |
| 90 | + fd = os.open(TESTFN, os.O_RDONLY) |
| 91 | + fp2 = posix.fdopen(fd, *args) |
| 92 | + fp2.close() |
| 93 | + |
| 94 | + def test_fdopen(self): |
| 95 | + if hasattr(posix, 'fdopen'): |
| 96 | + self.fdopen_helper() |
| 97 | + self.fdopen_helper('r') |
| 98 | + self.fdopen_helper('r', 100) |
| 99 | + |
| 100 | + def test_fstat(self): |
| 101 | + if hasattr(posix, 'fstat'): |
| 102 | + fp = open(TESTFN) |
| 103 | + try: |
| 104 | + posix.fstat(fp.fileno()) |
| 105 | + finally: |
| 106 | + fp.close() |
| 107 | + |
| 108 | + def test_stat(self): |
| 109 | + if hasattr(posix, 'stat'): |
| 110 | + posix.stat(TESTFN) |
| 111 | + |
| 112 | + def test_chdir(self): |
| 113 | + if hasattr(posix, 'chdir'): |
| 114 | + posix.chdir(os.curdir) |
| 115 | + try: |
| 116 | + posix.chdir(TESTFN) |
| 117 | + except OSError: |
| 118 | + pass |
| 119 | + else: |
| 120 | + raise TestFailed, \ |
| 121 | + 'should not be able to change directory to a file' |
| 122 | + |
| 123 | + def test_lsdir(self): |
| 124 | + if hasattr(posix, 'lsdir'): |
| 125 | + if TESTFN not in posix.lsdir(os.curdir): |
| 126 | + raise TestFailed, \ |
| 127 | + '%s should exist in current directory' % TESTFN |
| 128 | + |
| 129 | + def test_access(self): |
| 130 | + if hasattr(posix, 'access'): |
| 131 | + if not posix.access(TESTFN, os.R_OK): |
| 132 | + raise TestFailed, 'should have read access to: %s' % TESTFN |
| 133 | + |
| 134 | + def test_umask(self): |
| 135 | + if hasattr(posix, 'umask'): |
| 136 | + old_mask = posix.umask(0) |
| 137 | + posix.umask(old_mask) |
| 138 | + |
| 139 | + def test_strerror(self): |
| 140 | + if hasattr(posix, 'strerror'): |
| 141 | + posix.strerror(0) |
| 142 | + |
| 143 | + def test_pipe(self): |
| 144 | + if hasattr(posix, 'pipe'): |
| 145 | + reader, writer = posix.pipe() |
| 146 | + os.close(reader) |
| 147 | + os.close(writer) |
| 148 | + |
| 149 | + def test_tempnam(self): |
| 150 | + if hasattr(posix, 'tempnam'): |
| 151 | + posix.tempnam() |
| 152 | + posix.tempnam(os.curdir) |
| 153 | + posix.tempnam(os.curdir, 'blah') |
| 154 | + |
| 155 | + def test_tmpfile(self): |
| 156 | + if hasattr(posix, 'tmpfile'): |
| 157 | + fp = posix.tmpfile() |
| 158 | + fp.close() |
| 159 | + |
| 160 | + def test_utime(self): |
| 161 | + if hasattr(posix, 'utime'): |
| 162 | + now = time.time() |
| 163 | + posix.utime(TESTFN, None) |
| 164 | + posix.utime(TESTFN, (now, now)) |
| 165 | + |
| 166 | +def test_main(): |
| 167 | + suite = unittest.TestSuite() |
| 168 | + suite.addTest(unittest.makeSuite(PosixTester)) |
| 169 | + run_suite(suite) |
| 170 | + |
| 171 | +if __name__ == '__main__': |
| 172 | + test_main() |
0 commit comments