Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1 | "Test posix functions" |
| 2 | |
Tim Peters | 003eb30 | 2003-02-17 21:48:48 +0000 | [diff] [blame] | 3 | from test.test_support import TestSkipped, TestFailed, TESTFN, run_suite |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 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", |
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'): |
| 49 | fp = open(TESTFN) |
| 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'): |
| 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 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'): |
| 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 Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 101 | self.assert_(posix.fstat(fp.fileno())) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 102 | finally: |
| 103 | fp.close() |
| 104 | |
| 105 | def test_stat(self): |
| 106 | if hasattr(posix, 'stat'): |
Neal Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 107 | self.assert_(posix.stat(TESTFN)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 108 | |
| 109 | def test_chdir(self): |
| 110 | if hasattr(posix, 'chdir'): |
| 111 | posix.chdir(os.curdir) |
Neal Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 112 | self.assertRaises(OSError, posix.chdir, TESTFN) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 113 | |
| 114 | def test_lsdir(self): |
| 115 | if hasattr(posix, 'lsdir'): |
Neal Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 116 | self.assert_(TESTFN in posix.lsdir(os.curdir)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 117 | |
| 118 | def test_access(self): |
| 119 | if hasattr(posix, 'access'): |
Neal Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 120 | self.assert_(posix.access(TESTFN, os.R_OK)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 121 | |
| 122 | def test_umask(self): |
| 123 | if hasattr(posix, 'umask'): |
| 124 | old_mask = posix.umask(0) |
Neal Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 125 | self.assert_(isinstance(old_mask, int)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 126 | posix.umask(old_mask) |
| 127 | |
| 128 | def test_strerror(self): |
| 129 | if hasattr(posix, 'strerror'): |
Neal Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 130 | self.assert_(posix.strerror(0)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 131 | |
| 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 Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 140 | self.assert_(posix.tempnam()) |
| 141 | self.assert_(posix.tempnam(os.curdir)) |
| 142 | self.assert_(posix.tempnam(os.curdir, 'blah')) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 143 | |
| 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 | |
| 155 | def test_main(): |
| 156 | suite = unittest.TestSuite() |
| 157 | suite.addTest(unittest.makeSuite(PosixTester)) |
| 158 | run_suite(suite) |
| 159 | |
| 160 | if __name__ == '__main__': |
| 161 | test_main() |