test_stat.py 8.42 KB
Newer Older
1 2
import unittest
import os
3
import socket
4
import sys
5
from test.support import TESTFN, import_fresh_module
6 7 8

c_stat = import_fresh_module('stat', fresh=['_stat'])
py_stat = import_fresh_module('stat', blocked=['_stat'])
9

10
class TestFilemode:
11 12
    statmod = None

13 14 15
    file_flags = {'SF_APPEND', 'SF_ARCHIVED', 'SF_IMMUTABLE', 'SF_NOUNLINK',
                  'SF_SNAPSHOT', 'UF_APPEND', 'UF_COMPRESSED', 'UF_HIDDEN',
                  'UF_IMMUTABLE', 'UF_NODUMP', 'UF_NOUNLINK', 'UF_OPAQUE'}
16

17 18
    formats = {'S_IFBLK', 'S_IFCHR', 'S_IFDIR', 'S_IFIFO', 'S_IFLNK',
               'S_IFREG', 'S_IFSOCK'}
19

20 21
    format_funcs = {'S_ISBLK', 'S_ISCHR', 'S_ISDIR', 'S_ISFIFO', 'S_ISLNK',
                    'S_ISREG', 'S_ISSOCK'}
22

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    stat_struct = {
        'ST_MODE': 0,
        'ST_INO': 1,
        'ST_DEV': 2,
        'ST_NLINK': 3,
        'ST_UID': 4,
        'ST_GID': 5,
        'ST_SIZE': 6,
        'ST_ATIME': 7,
        'ST_MTIME': 8,
        'ST_CTIME': 9}

    # permission bit value are defined by POSIX
    permission_bits = {
        'S_ISUID': 0o4000,
        'S_ISGID': 0o2000,
        'S_ENFMT': 0o2000,
        'S_ISVTX': 0o1000,
        'S_IRWXU': 0o700,
        'S_IRUSR': 0o400,
        'S_IREAD': 0o400,
        'S_IWUSR': 0o200,
        'S_IWRITE': 0o200,
        'S_IXUSR': 0o100,
        'S_IEXEC': 0o100,
        'S_IRWXG': 0o070,
        'S_IRGRP': 0o040,
        'S_IWGRP': 0o020,
        'S_IXGRP': 0o010,
        'S_IRWXO': 0o007,
        'S_IROTH': 0o004,
        'S_IWOTH': 0o002,
        'S_IXOTH': 0o001}
56

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    # defined by the Windows API documentation
    file_attributes = {
        'FILE_ATTRIBUTE_ARCHIVE': 32,
        'FILE_ATTRIBUTE_COMPRESSED': 2048,
        'FILE_ATTRIBUTE_DEVICE': 64,
        'FILE_ATTRIBUTE_DIRECTORY': 16,
        'FILE_ATTRIBUTE_ENCRYPTED': 16384,
        'FILE_ATTRIBUTE_HIDDEN': 2,
        'FILE_ATTRIBUTE_INTEGRITY_STREAM': 32768,
        'FILE_ATTRIBUTE_NORMAL': 128,
        'FILE_ATTRIBUTE_NOT_CONTENT_INDEXED': 8192,
        'FILE_ATTRIBUTE_NO_SCRUB_DATA': 131072,
        'FILE_ATTRIBUTE_OFFLINE': 4096,
        'FILE_ATTRIBUTE_READONLY': 1,
        'FILE_ATTRIBUTE_REPARSE_POINT': 1024,
        'FILE_ATTRIBUTE_SPARSE_FILE': 512,
        'FILE_ATTRIBUTE_SYSTEM': 4,
        'FILE_ATTRIBUTE_TEMPORARY': 256,
        'FILE_ATTRIBUTE_VIRTUAL': 65536}

77 78 79 80 81 82 83 84 85 86
    def setUp(self):
        try:
            os.remove(TESTFN)
        except OSError:
            try:
                os.rmdir(TESTFN)
            except OSError:
                pass
    tearDown = setUp

87 88 89 90 91
    def get_mode(self, fname=TESTFN, lstat=True):
        if lstat:
            st_mode = os.lstat(fname).st_mode
        else:
            st_mode = os.stat(fname).st_mode
92
        modestr = self.statmod.filemode(st_mode)
93 94 95 96
        return st_mode, modestr

    def assertS_IS(self, name, mode):
        # test format, lstrip is for S_IFIFO
97 98
        fmt = getattr(self.statmod, "S_IF" + name.lstrip("F"))
        self.assertEqual(self.statmod.S_IFMT(mode), fmt)
99 100 101
        # test that just one function returns true
        testname = "S_IS" + name
        for funcname in self.format_funcs:
102
            func = getattr(self.statmod, funcname, None)
103 104 105 106 107 108 109 110 111
            if func is None:
                if funcname == testname:
                    raise ValueError(funcname)
                continue
            if funcname == testname:
                self.assertTrue(func(mode))
            else:
                self.assertFalse(func(mode))

112 113 114
    def test_mode(self):
        with open(TESTFN, 'w'):
            pass
115 116
        if os.name == 'posix':
            os.chmod(TESTFN, 0o700)
117 118 119
            st_mode, modestr = self.get_mode()
            self.assertEqual(modestr, '-rwx------')
            self.assertS_IS("REG", st_mode)
120 121
            self.assertEqual(self.statmod.S_IMODE(st_mode),
                             self.statmod.S_IRWXU)
122

123
            os.chmod(TESTFN, 0o070)
124 125 126
            st_mode, modestr = self.get_mode()
            self.assertEqual(modestr, '----rwx---')
            self.assertS_IS("REG", st_mode)
127 128
            self.assertEqual(self.statmod.S_IMODE(st_mode),
                             self.statmod.S_IRWXG)
129

130
            os.chmod(TESTFN, 0o007)
131 132 133
            st_mode, modestr = self.get_mode()
            self.assertEqual(modestr, '-------rwx')
            self.assertS_IS("REG", st_mode)
134 135
            self.assertEqual(self.statmod.S_IMODE(st_mode),
                             self.statmod.S_IRWXO)
136

137
            os.chmod(TESTFN, 0o444)
138 139 140
            st_mode, modestr = self.get_mode()
            self.assertS_IS("REG", st_mode)
            self.assertEqual(modestr, '-r--r--r--')
141
            self.assertEqual(self.statmod.S_IMODE(st_mode), 0o444)
142 143
        else:
            os.chmod(TESTFN, 0o700)
144 145 146
            st_mode, modestr = self.get_mode()
            self.assertEqual(modestr[:3], '-rw')
            self.assertS_IS("REG", st_mode)
147 148
            self.assertEqual(self.statmod.S_IFMT(st_mode),
                             self.statmod.S_IFREG)
149 150 151 152

    def test_directory(self):
        os.mkdir(TESTFN)
        os.chmod(TESTFN, 0o700)
153 154
        st_mode, modestr = self.get_mode()
        self.assertS_IS("DIR", st_mode)
155
        if os.name == 'posix':
156
            self.assertEqual(modestr, 'drwx------')
157
        else:
158
            self.assertEqual(modestr[0], 'd')
159 160 161

    @unittest.skipUnless(hasattr(os, 'symlink'), 'os.symlink not available')
    def test_link(self):
162 163 164 165 166
        try:
            os.symlink(os.getcwd(), TESTFN)
        except (OSError, NotImplementedError) as err:
            raise unittest.SkipTest(str(err))
        else:
167 168 169
            st_mode, modestr = self.get_mode()
            self.assertEqual(modestr[0], 'l')
            self.assertS_IS("LNK", st_mode)
170 171 172

    @unittest.skipUnless(hasattr(os, 'mkfifo'), 'os.mkfifo not available')
    def test_fifo(self):
173 174 175 176
        try:
            os.mkfifo(TESTFN, 0o700)
        except PermissionError as e:
            self.skipTest('os.mkfifo(): %s' % e)
177 178 179 180 181 182 183
        st_mode, modestr = self.get_mode()
        self.assertEqual(modestr, 'prwx------')
        self.assertS_IS("FIFO", st_mode)

    @unittest.skipUnless(os.name == 'posix', 'requires Posix')
    def test_devices(self):
        if os.path.exists(os.devnull):
184
            st_mode, modestr = self.get_mode(os.devnull, lstat=False)
185 186
            self.assertEqual(modestr[0], 'c')
            self.assertS_IS("CHR", st_mode)
187
        # Linux block devices, BSD has no block devices anymore
188
        for blockdev in ("/dev/sda", "/dev/hda"):
189
            if os.path.exists(blockdev):
190
                st_mode, modestr = self.get_mode(blockdev, lstat=False)
191 192 193 194
                self.assertEqual(modestr[0], 'b')
                self.assertS_IS("BLK", st_mode)
                break

195 196 197 198 199 200 201 202
    @unittest.skipUnless(hasattr(socket, 'AF_UNIX'), 'requires unix socket')
    def test_socket(self):
        with socket.socket(socket.AF_UNIX) as s:
            s.bind(TESTFN)
            st_mode, modestr = self.get_mode()
            self.assertEqual(modestr[0], 's')
            self.assertS_IS("SOCK", st_mode)

203 204
    def test_module_attributes(self):
        for key, value in self.stat_struct.items():
205
            modvalue = getattr(self.statmod, key)
206 207
            self.assertEqual(value, modvalue, key)
        for key, value in self.permission_bits.items():
208
            modvalue = getattr(self.statmod, key)
209 210
            self.assertEqual(value, modvalue, key)
        for key in self.file_flags:
211
            modvalue = getattr(self.statmod, key)
212 213
            self.assertIsInstance(modvalue, int)
        for key in self.formats:
214
            modvalue = getattr(self.statmod, key)
215 216
            self.assertIsInstance(modvalue, int)
        for key in self.format_funcs:
217
            func = getattr(self.statmod, key)
218 219
            self.assertTrue(callable(func))
            self.assertEqual(func(0), 0)
220

221 222 223 224 225 226 227 228
    @unittest.skipUnless(sys.platform == "win32",
                         "FILE_ATTRIBUTE_* constants are Win32 specific")
    def test_file_attribute_constants(self):
        for key, value in sorted(self.file_attributes.items()):
            self.assertTrue(hasattr(self.statmod, key), key)
            modvalue = getattr(self.statmod, key)
            self.assertEqual(value, modvalue, key)

229

230
class TestFilemodeCStat(TestFilemode, unittest.TestCase):
231 232 233
    statmod = c_stat

    formats = TestFilemode.formats | {'S_IFDOOR', 'S_IFPORT', 'S_IFWHT'}
234 235
    format_funcs = TestFilemode.format_funcs | {'S_ISDOOR', 'S_ISPORT',
                                                'S_ISWHT'}
236 237


238
class TestFilemodePyStat(TestFilemode, unittest.TestCase):
239 240 241
    statmod = py_stat


242
if __name__ == '__main__':
243
    unittest.main()