• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import unittest
2import os
3import sys
4from test.support import TESTFN, import_fresh_module
5
6c_stat = import_fresh_module('stat', fresh=['_stat'])
7py_stat = import_fresh_module('stat', blocked=['_stat'])
8
9class TestFilemode:
10    statmod = None
11
12    file_flags = {'SF_APPEND', 'SF_ARCHIVED', 'SF_IMMUTABLE', 'SF_NOUNLINK',
13                  'SF_SNAPSHOT', 'UF_APPEND', 'UF_COMPRESSED', 'UF_HIDDEN',
14                  'UF_IMMUTABLE', 'UF_NODUMP', 'UF_NOUNLINK', 'UF_OPAQUE'}
15
16    formats = {'S_IFBLK', 'S_IFCHR', 'S_IFDIR', 'S_IFIFO', 'S_IFLNK',
17               'S_IFREG', 'S_IFSOCK'}
18
19    format_funcs = {'S_ISBLK', 'S_ISCHR', 'S_ISDIR', 'S_ISFIFO', 'S_ISLNK',
20                    'S_ISREG', 'S_ISSOCK'}
21
22    stat_struct = {
23        'ST_MODE': 0,
24        'ST_INO': 1,
25        'ST_DEV': 2,
26        'ST_NLINK': 3,
27        'ST_UID': 4,
28        'ST_GID': 5,
29        'ST_SIZE': 6,
30        'ST_ATIME': 7,
31        'ST_MTIME': 8,
32        'ST_CTIME': 9}
33
34    # permission bit value are defined by POSIX
35    permission_bits = {
36        'S_ISUID': 0o4000,
37        'S_ISGID': 0o2000,
38        'S_ENFMT': 0o2000,
39        'S_ISVTX': 0o1000,
40        'S_IRWXU': 0o700,
41        'S_IRUSR': 0o400,
42        'S_IREAD': 0o400,
43        'S_IWUSR': 0o200,
44        'S_IWRITE': 0o200,
45        'S_IXUSR': 0o100,
46        'S_IEXEC': 0o100,
47        'S_IRWXG': 0o070,
48        'S_IRGRP': 0o040,
49        'S_IWGRP': 0o020,
50        'S_IXGRP': 0o010,
51        'S_IRWXO': 0o007,
52        'S_IROTH': 0o004,
53        'S_IWOTH': 0o002,
54        'S_IXOTH': 0o001}
55
56    # defined by the Windows API documentation
57    file_attributes = {
58        'FILE_ATTRIBUTE_ARCHIVE': 32,
59        'FILE_ATTRIBUTE_COMPRESSED': 2048,
60        'FILE_ATTRIBUTE_DEVICE': 64,
61        'FILE_ATTRIBUTE_DIRECTORY': 16,
62        'FILE_ATTRIBUTE_ENCRYPTED': 16384,
63        'FILE_ATTRIBUTE_HIDDEN': 2,
64        'FILE_ATTRIBUTE_INTEGRITY_STREAM': 32768,
65        'FILE_ATTRIBUTE_NORMAL': 128,
66        'FILE_ATTRIBUTE_NOT_CONTENT_INDEXED': 8192,
67        'FILE_ATTRIBUTE_NO_SCRUB_DATA': 131072,
68        'FILE_ATTRIBUTE_OFFLINE': 4096,
69        'FILE_ATTRIBUTE_READONLY': 1,
70        'FILE_ATTRIBUTE_REPARSE_POINT': 1024,
71        'FILE_ATTRIBUTE_SPARSE_FILE': 512,
72        'FILE_ATTRIBUTE_SYSTEM': 4,
73        'FILE_ATTRIBUTE_TEMPORARY': 256,
74        'FILE_ATTRIBUTE_VIRTUAL': 65536}
75
76    def setUp(self):
77        try:
78            os.remove(TESTFN)
79        except OSError:
80            try:
81                os.rmdir(TESTFN)
82            except OSError:
83                pass
84    tearDown = setUp
85
86    def get_mode(self, fname=TESTFN, lstat=True):
87        if lstat:
88            st_mode = os.lstat(fname).st_mode
89        else:
90            st_mode = os.stat(fname).st_mode
91        modestr = self.statmod.filemode(st_mode)
92        return st_mode, modestr
93
94    def assertS_IS(self, name, mode):
95        # test format, lstrip is for S_IFIFO
96        fmt = getattr(self.statmod, "S_IF" + name.lstrip("F"))
97        self.assertEqual(self.statmod.S_IFMT(mode), fmt)
98        # test that just one function returns true
99        testname = "S_IS" + name
100        for funcname in self.format_funcs:
101            func = getattr(self.statmod, funcname, None)
102            if func is None:
103                if funcname == testname:
104                    raise ValueError(funcname)
105                continue
106            if funcname == testname:
107                self.assertTrue(func(mode))
108            else:
109                self.assertFalse(func(mode))
110
111    def test_mode(self):
112        with open(TESTFN, 'w'):
113            pass
114        if os.name == 'posix':
115            os.chmod(TESTFN, 0o700)
116            st_mode, modestr = self.get_mode()
117            self.assertEqual(modestr, '-rwx------')
118            self.assertS_IS("REG", st_mode)
119            self.assertEqual(self.statmod.S_IMODE(st_mode),
120                             self.statmod.S_IRWXU)
121
122            os.chmod(TESTFN, 0o070)
123            st_mode, modestr = self.get_mode()
124            self.assertEqual(modestr, '----rwx---')
125            self.assertS_IS("REG", st_mode)
126            self.assertEqual(self.statmod.S_IMODE(st_mode),
127                             self.statmod.S_IRWXG)
128
129            os.chmod(TESTFN, 0o007)
130            st_mode, modestr = self.get_mode()
131            self.assertEqual(modestr, '-------rwx')
132            self.assertS_IS("REG", st_mode)
133            self.assertEqual(self.statmod.S_IMODE(st_mode),
134                             self.statmod.S_IRWXO)
135
136            os.chmod(TESTFN, 0o444)
137            st_mode, modestr = self.get_mode()
138            self.assertS_IS("REG", st_mode)
139            self.assertEqual(modestr, '-r--r--r--')
140            self.assertEqual(self.statmod.S_IMODE(st_mode), 0o444)
141        else:
142            os.chmod(TESTFN, 0o700)
143            st_mode, modestr = self.get_mode()
144            self.assertEqual(modestr[:3], '-rw')
145            self.assertS_IS("REG", st_mode)
146            self.assertEqual(self.statmod.S_IFMT(st_mode),
147                             self.statmod.S_IFREG)
148
149    def test_directory(self):
150        os.mkdir(TESTFN)
151        os.chmod(TESTFN, 0o700)
152        st_mode, modestr = self.get_mode()
153        self.assertS_IS("DIR", st_mode)
154        if os.name == 'posix':
155            self.assertEqual(modestr, 'drwx------')
156        else:
157            self.assertEqual(modestr[0], 'd')
158
159    @unittest.skipUnless(hasattr(os, 'symlink'), 'os.symlink not available')
160    def test_link(self):
161        try:
162            os.symlink(os.getcwd(), TESTFN)
163        except (OSError, NotImplementedError) as err:
164            raise unittest.SkipTest(str(err))
165        else:
166            st_mode, modestr = self.get_mode()
167            self.assertEqual(modestr[0], 'l')
168            self.assertS_IS("LNK", st_mode)
169
170    @unittest.skipUnless(hasattr(os, 'mkfifo'), 'os.mkfifo not available')
171    def test_fifo(self):
172        try:
173            os.mkfifo(TESTFN, 0o700)
174        except PermissionError as e:
175            self.skipTest('os.mkfifo(): %s' % e)
176        st_mode, modestr = self.get_mode()
177        self.assertEqual(modestr, 'prwx------')
178        self.assertS_IS("FIFO", st_mode)
179
180    @unittest.skipUnless(os.name == 'posix', 'requires Posix')
181    def test_devices(self):
182        if os.path.exists(os.devnull):
183            st_mode, modestr = self.get_mode(os.devnull, lstat=False)
184            self.assertEqual(modestr[0], 'c')
185            self.assertS_IS("CHR", st_mode)
186        # Linux block devices, BSD has no block devices anymore
187        for blockdev in ("/dev/sda", "/dev/hda"):
188            if os.path.exists(blockdev):
189                st_mode, modestr = self.get_mode(blockdev, lstat=False)
190                self.assertEqual(modestr[0], 'b')
191                self.assertS_IS("BLK", st_mode)
192                break
193
194    def test_module_attributes(self):
195        for key, value in self.stat_struct.items():
196            modvalue = getattr(self.statmod, key)
197            self.assertEqual(value, modvalue, key)
198        for key, value in self.permission_bits.items():
199            modvalue = getattr(self.statmod, key)
200            self.assertEqual(value, modvalue, key)
201        for key in self.file_flags:
202            modvalue = getattr(self.statmod, key)
203            self.assertIsInstance(modvalue, int)
204        for key in self.formats:
205            modvalue = getattr(self.statmod, key)
206            self.assertIsInstance(modvalue, int)
207        for key in self.format_funcs:
208            func = getattr(self.statmod, key)
209            self.assertTrue(callable(func))
210            self.assertEqual(func(0), 0)
211
212    @unittest.skipUnless(sys.platform == "win32",
213                         "FILE_ATTRIBUTE_* constants are Win32 specific")
214    def test_file_attribute_constants(self):
215        for key, value in sorted(self.file_attributes.items()):
216            self.assertTrue(hasattr(self.statmod, key), key)
217            modvalue = getattr(self.statmod, key)
218            self.assertEqual(value, modvalue, key)
219
220
221class TestFilemodeCStat(TestFilemode, unittest.TestCase):
222    statmod = c_stat
223
224    formats = TestFilemode.formats | {'S_IFDOOR', 'S_IFPORT', 'S_IFWHT'}
225    format_funcs = TestFilemode.format_funcs | {'S_ISDOOR', 'S_ISPORT',
226                                                'S_ISWHT'}
227
228
229class TestFilemodePyStat(TestFilemode, unittest.TestCase):
230    statmod = py_stat
231
232
233if __name__ == '__main__':
234    unittest.main()
235