blob: af6ced4204041038d5991a06a2045973abc138a6 [file] [log] [blame]
Giampaolo Rodola'ffa1d0b2012-05-15 15:30:25 +02001import unittest
2import os
Antoine Pitrou3a5053b2013-06-29 12:58:57 +02003from test.support import TESTFN, import_fresh_module
Christian Heimesc77d9f32013-06-22 21:05:02 +02004
5c_stat = import_fresh_module('stat', fresh=['_stat'])
6py_stat = import_fresh_module('stat', blocked=['_stat'])
Giampaolo Rodola'ffa1d0b2012-05-15 15:30:25 +02007
Antoine Pitrou3a5053b2013-06-29 12:58:57 +02008class TestFilemode:
Christian Heimesc77d9f32013-06-22 21:05:02 +02009 statmod = None
10
Christian Heimesf678b312013-06-21 18:25:56 +020011 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'ffa1d0b2012-05-15 15:30:25 +020054
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 Heimes36a7e4f2013-06-23 16:10:29 +020065 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 Heimesc77d9f32013-06-22 21:05:02 +020070 modestr = self.statmod.filemode(st_mode)
Christian Heimesf678b312013-06-21 18:25:56 +020071 return st_mode, modestr
72
73 def assertS_IS(self, name, mode):
74 # test format, lstrip is for S_IFIFO
Christian Heimesc77d9f32013-06-22 21:05:02 +020075 fmt = getattr(self.statmod, "S_IF" + name.lstrip("F"))
76 self.assertEqual(self.statmod.S_IFMT(mode), fmt)
Christian Heimesf678b312013-06-21 18:25:56 +020077 # test that just one function returns true
78 testname = "S_IS" + name
79 for funcname in self.format_funcs:
Christian Heimesc77d9f32013-06-22 21:05:02 +020080 func = getattr(self.statmod, funcname, None)
Christian Heimesf678b312013-06-21 18:25:56 +020081 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'ffa1d0b2012-05-15 15:30:25 +020090 def test_mode(self):
91 with open(TESTFN, 'w'):
92 pass
Giampaolo Rodola'e1266782012-05-16 16:01:59 +020093 if os.name == 'posix':
94 os.chmod(TESTFN, 0o700)
Christian Heimesf678b312013-06-21 18:25:56 +020095 st_mode, modestr = self.get_mode()
96 self.assertEqual(modestr, '-rwx------')
97 self.assertS_IS("REG", st_mode)
Christian Heimesc77d9f32013-06-22 21:05:02 +020098 self.assertEqual(self.statmod.S_IMODE(st_mode),
99 self.statmod.S_IRWXU)
Christian Heimesf678b312013-06-21 18:25:56 +0200100
Giampaolo Rodola'e1266782012-05-16 16:01:59 +0200101 os.chmod(TESTFN, 0o070)
Christian Heimesf678b312013-06-21 18:25:56 +0200102 st_mode, modestr = self.get_mode()
103 self.assertEqual(modestr, '----rwx---')
104 self.assertS_IS("REG", st_mode)
Christian Heimesc77d9f32013-06-22 21:05:02 +0200105 self.assertEqual(self.statmod.S_IMODE(st_mode),
106 self.statmod.S_IRWXG)
Christian Heimesf678b312013-06-21 18:25:56 +0200107
Giampaolo Rodola'e1266782012-05-16 16:01:59 +0200108 os.chmod(TESTFN, 0o007)
Christian Heimesf678b312013-06-21 18:25:56 +0200109 st_mode, modestr = self.get_mode()
110 self.assertEqual(modestr, '-------rwx')
111 self.assertS_IS("REG", st_mode)
Christian Heimesc77d9f32013-06-22 21:05:02 +0200112 self.assertEqual(self.statmod.S_IMODE(st_mode),
113 self.statmod.S_IRWXO)
Christian Heimesf678b312013-06-21 18:25:56 +0200114
Giampaolo Rodola'e1266782012-05-16 16:01:59 +0200115 os.chmod(TESTFN, 0o444)
Christian Heimesf678b312013-06-21 18:25:56 +0200116 st_mode, modestr = self.get_mode()
117 self.assertS_IS("REG", st_mode)
118 self.assertEqual(modestr, '-r--r--r--')
Christian Heimesc77d9f32013-06-22 21:05:02 +0200119 self.assertEqual(self.statmod.S_IMODE(st_mode), 0o444)
Giampaolo Rodola'e1266782012-05-16 16:01:59 +0200120 else:
121 os.chmod(TESTFN, 0o700)
Christian Heimesf678b312013-06-21 18:25:56 +0200122 st_mode, modestr = self.get_mode()
123 self.assertEqual(modestr[:3], '-rw')
124 self.assertS_IS("REG", st_mode)
Christian Heimesc77d9f32013-06-22 21:05:02 +0200125 self.assertEqual(self.statmod.S_IFMT(st_mode),
126 self.statmod.S_IFREG)
Giampaolo Rodola'ffa1d0b2012-05-15 15:30:25 +0200127
128 def test_directory(self):
129 os.mkdir(TESTFN)
130 os.chmod(TESTFN, 0o700)
Christian Heimesf678b312013-06-21 18:25:56 +0200131 st_mode, modestr = self.get_mode()
132 self.assertS_IS("DIR", st_mode)
Giampaolo Rodola'e1266782012-05-16 16:01:59 +0200133 if os.name == 'posix':
Christian Heimesf678b312013-06-21 18:25:56 +0200134 self.assertEqual(modestr, 'drwx------')
Giampaolo Rodola'e1266782012-05-16 16:01:59 +0200135 else:
Christian Heimesf678b312013-06-21 18:25:56 +0200136 self.assertEqual(modestr[0], 'd')
Giampaolo Rodola'ffa1d0b2012-05-15 15:30:25 +0200137
138 @unittest.skipUnless(hasattr(os, 'symlink'), 'os.symlink not available')
139 def test_link(self):
Giampaolo Rodola'e1266782012-05-16 16:01:59 +0200140 try:
141 os.symlink(os.getcwd(), TESTFN)
142 except (OSError, NotImplementedError) as err:
143 raise unittest.SkipTest(str(err))
144 else:
Christian Heimesf678b312013-06-21 18:25:56 +0200145 st_mode, modestr = self.get_mode()
146 self.assertEqual(modestr[0], 'l')
147 self.assertS_IS("LNK", st_mode)
Giampaolo Rodola'ffa1d0b2012-05-15 15:30:25 +0200148
149 @unittest.skipUnless(hasattr(os, 'mkfifo'), 'os.mkfifo not available')
150 def test_fifo(self):
151 os.mkfifo(TESTFN, 0o700)
Christian Heimesf678b312013-06-21 18:25:56 +0200152 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 Heimes36a7e4f2013-06-23 16:10:29 +0200159 st_mode, modestr = self.get_mode(os.devnull, lstat=False)
Christian Heimesf678b312013-06-21 18:25:56 +0200160 self.assertEqual(modestr[0], 'c')
161 self.assertS_IS("CHR", st_mode)
Christian Heimes45d94932013-06-22 14:48:32 +0200162 # Linux block devices, BSD has no block devices anymore
Christian Heimes60a95932013-06-21 18:53:13 +0200163 for blockdev in ("/dev/sda", "/dev/hda"):
Christian Heimesf678b312013-06-21 18:25:56 +0200164 if os.path.exists(blockdev):
Christian Heimes36a7e4f2013-06-23 16:10:29 +0200165 st_mode, modestr = self.get_mode(blockdev, lstat=False)
Christian Heimesf678b312013-06-21 18:25:56 +0200166 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 Heimesc77d9f32013-06-22 21:05:02 +0200172 modvalue = getattr(self.statmod, key)
Christian Heimesf678b312013-06-21 18:25:56 +0200173 self.assertEqual(value, modvalue, key)
174 for key, value in self.permission_bits.items():
Christian Heimesc77d9f32013-06-22 21:05:02 +0200175 modvalue = getattr(self.statmod, key)
Christian Heimesf678b312013-06-21 18:25:56 +0200176 self.assertEqual(value, modvalue, key)
177 for key in self.file_flags:
Christian Heimesc77d9f32013-06-22 21:05:02 +0200178 modvalue = getattr(self.statmod, key)
Christian Heimesf678b312013-06-21 18:25:56 +0200179 self.assertIsInstance(modvalue, int)
180 for key in self.formats:
Christian Heimesc77d9f32013-06-22 21:05:02 +0200181 modvalue = getattr(self.statmod, key)
Christian Heimesf678b312013-06-21 18:25:56 +0200182 self.assertIsInstance(modvalue, int)
183 for key in self.format_funcs:
Christian Heimesc77d9f32013-06-22 21:05:02 +0200184 func = getattr(self.statmod, key)
Christian Heimesf678b312013-06-21 18:25:56 +0200185 self.assertTrue(callable(func))
186 self.assertEqual(func(0), 0)
Giampaolo Rodola'ffa1d0b2012-05-15 15:30:25 +0200187
188
Antoine Pitrou3a5053b2013-06-29 12:58:57 +0200189class TestFilemodeCStat(TestFilemode, unittest.TestCase):
Christian Heimesc77d9f32013-06-22 21:05:02 +0200190 statmod = c_stat
191
192 formats = TestFilemode.formats | {'S_IFDOOR', 'S_IFPORT', 'S_IFWHT'}
Antoine Pitrou3a5053b2013-06-29 12:58:57 +0200193 format_funcs = TestFilemode.format_funcs | {'S_ISDOOR', 'S_ISPORT',
194 'S_ISWHT'}
Christian Heimesc77d9f32013-06-22 21:05:02 +0200195
196
Antoine Pitrou3a5053b2013-06-29 12:58:57 +0200197class TestFilemodePyStat(TestFilemode, unittest.TestCase):
Christian Heimesc77d9f32013-06-22 21:05:02 +0200198 statmod = py_stat
199
200
Giampaolo Rodola'ffa1d0b2012-05-15 15:30:25 +0200201if __name__ == '__main__':
Antoine Pitrou3a5053b2013-06-29 12:58:57 +0200202 unittest.main()