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