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