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