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