2012-05-15 10:30:25 -03:00
|
|
|
import unittest
|
|
|
|
import os
|
2018-08-10 02:12:08 -03:00
|
|
|
import socket
|
2014-06-19 11:46:37 -03:00
|
|
|
import sys
|
2020-12-01 04:20:50 -04:00
|
|
|
from test.support import os_helper
|
2020-04-25 04:06:29 -03:00
|
|
|
from test.support import socket_helper
|
2020-08-03 13:49:18 -03:00
|
|
|
from test.support.import_helper import import_fresh_module
|
|
|
|
from test.support.os_helper import TESTFN
|
|
|
|
|
2013-06-22 16:05:02 -03:00
|
|
|
|
|
|
|
c_stat = import_fresh_module('stat', fresh=['_stat'])
|
|
|
|
py_stat = import_fresh_module('stat', blocked=['_stat'])
|
2012-05-15 10:30:25 -03:00
|
|
|
|
2013-06-29 07:58:57 -03:00
|
|
|
class TestFilemode:
|
2013-06-22 16:05:02 -03:00
|
|
|
statmod = None
|
|
|
|
|
2013-06-21 13:25:56 -03:00
|
|
|
file_flags = {'SF_APPEND', 'SF_ARCHIVED', 'SF_IMMUTABLE', 'SF_NOUNLINK',
|
|
|
|
'SF_SNAPSHOT', 'UF_APPEND', 'UF_COMPRESSED', 'UF_HIDDEN',
|
|
|
|
'UF_IMMUTABLE', 'UF_NODUMP', 'UF_NOUNLINK', 'UF_OPAQUE'}
|
2012-05-15 10:30:25 -03:00
|
|
|
|
2013-06-21 13:25:56 -03:00
|
|
|
formats = {'S_IFBLK', 'S_IFCHR', 'S_IFDIR', 'S_IFIFO', 'S_IFLNK',
|
2019-10-10 04:34:46 -03:00
|
|
|
'S_IFREG', 'S_IFSOCK', 'S_IFDOOR', 'S_IFPORT', 'S_IFWHT'}
|
2012-05-15 10:30:25 -03:00
|
|
|
|
2013-06-21 13:25:56 -03:00
|
|
|
format_funcs = {'S_ISBLK', 'S_ISCHR', 'S_ISDIR', 'S_ISFIFO', 'S_ISLNK',
|
2019-10-10 04:34:46 -03:00
|
|
|
'S_ISREG', 'S_ISSOCK', 'S_ISDOOR', 'S_ISPORT', 'S_ISWHT'}
|
2012-05-15 10:30:25 -03:00
|
|
|
|
2013-06-21 13:25:56 -03:00
|
|
|
stat_struct = {
|
|
|
|
'ST_MODE': 0,
|
|
|
|
'ST_INO': 1,
|
|
|
|
'ST_DEV': 2,
|
|
|
|
'ST_NLINK': 3,
|
|
|
|
'ST_UID': 4,
|
|
|
|
'ST_GID': 5,
|
|
|
|
'ST_SIZE': 6,
|
|
|
|
'ST_ATIME': 7,
|
|
|
|
'ST_MTIME': 8,
|
|
|
|
'ST_CTIME': 9}
|
|
|
|
|
|
|
|
# permission bit value are defined by POSIX
|
|
|
|
permission_bits = {
|
|
|
|
'S_ISUID': 0o4000,
|
|
|
|
'S_ISGID': 0o2000,
|
|
|
|
'S_ENFMT': 0o2000,
|
|
|
|
'S_ISVTX': 0o1000,
|
|
|
|
'S_IRWXU': 0o700,
|
|
|
|
'S_IRUSR': 0o400,
|
|
|
|
'S_IREAD': 0o400,
|
|
|
|
'S_IWUSR': 0o200,
|
|
|
|
'S_IWRITE': 0o200,
|
|
|
|
'S_IXUSR': 0o100,
|
|
|
|
'S_IEXEC': 0o100,
|
|
|
|
'S_IRWXG': 0o070,
|
|
|
|
'S_IRGRP': 0o040,
|
|
|
|
'S_IWGRP': 0o020,
|
|
|
|
'S_IXGRP': 0o010,
|
|
|
|
'S_IRWXO': 0o007,
|
|
|
|
'S_IROTH': 0o004,
|
|
|
|
'S_IWOTH': 0o002,
|
|
|
|
'S_IXOTH': 0o001}
|
2012-05-15 10:30:25 -03:00
|
|
|
|
2014-06-19 11:46:37 -03:00
|
|
|
# defined by the Windows API documentation
|
|
|
|
file_attributes = {
|
|
|
|
'FILE_ATTRIBUTE_ARCHIVE': 32,
|
|
|
|
'FILE_ATTRIBUTE_COMPRESSED': 2048,
|
|
|
|
'FILE_ATTRIBUTE_DEVICE': 64,
|
|
|
|
'FILE_ATTRIBUTE_DIRECTORY': 16,
|
|
|
|
'FILE_ATTRIBUTE_ENCRYPTED': 16384,
|
|
|
|
'FILE_ATTRIBUTE_HIDDEN': 2,
|
|
|
|
'FILE_ATTRIBUTE_INTEGRITY_STREAM': 32768,
|
|
|
|
'FILE_ATTRIBUTE_NORMAL': 128,
|
|
|
|
'FILE_ATTRIBUTE_NOT_CONTENT_INDEXED': 8192,
|
|
|
|
'FILE_ATTRIBUTE_NO_SCRUB_DATA': 131072,
|
|
|
|
'FILE_ATTRIBUTE_OFFLINE': 4096,
|
|
|
|
'FILE_ATTRIBUTE_READONLY': 1,
|
|
|
|
'FILE_ATTRIBUTE_REPARSE_POINT': 1024,
|
|
|
|
'FILE_ATTRIBUTE_SPARSE_FILE': 512,
|
|
|
|
'FILE_ATTRIBUTE_SYSTEM': 4,
|
|
|
|
'FILE_ATTRIBUTE_TEMPORARY': 256,
|
|
|
|
'FILE_ATTRIBUTE_VIRTUAL': 65536}
|
|
|
|
|
2012-05-15 10:30:25 -03:00
|
|
|
def setUp(self):
|
|
|
|
try:
|
|
|
|
os.remove(TESTFN)
|
|
|
|
except OSError:
|
|
|
|
try:
|
|
|
|
os.rmdir(TESTFN)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
tearDown = setUp
|
|
|
|
|
2013-06-23 11:10:29 -03:00
|
|
|
def get_mode(self, fname=TESTFN, lstat=True):
|
|
|
|
if lstat:
|
|
|
|
st_mode = os.lstat(fname).st_mode
|
|
|
|
else:
|
|
|
|
st_mode = os.stat(fname).st_mode
|
2013-06-22 16:05:02 -03:00
|
|
|
modestr = self.statmod.filemode(st_mode)
|
2013-06-21 13:25:56 -03:00
|
|
|
return st_mode, modestr
|
|
|
|
|
|
|
|
def assertS_IS(self, name, mode):
|
|
|
|
# test format, lstrip is for S_IFIFO
|
2013-06-22 16:05:02 -03:00
|
|
|
fmt = getattr(self.statmod, "S_IF" + name.lstrip("F"))
|
|
|
|
self.assertEqual(self.statmod.S_IFMT(mode), fmt)
|
2013-06-21 13:25:56 -03:00
|
|
|
# test that just one function returns true
|
|
|
|
testname = "S_IS" + name
|
|
|
|
for funcname in self.format_funcs:
|
2013-06-22 16:05:02 -03:00
|
|
|
func = getattr(self.statmod, funcname, None)
|
2013-06-21 13:25:56 -03:00
|
|
|
if func is None:
|
|
|
|
if funcname == testname:
|
|
|
|
raise ValueError(funcname)
|
|
|
|
continue
|
|
|
|
if funcname == testname:
|
|
|
|
self.assertTrue(func(mode))
|
|
|
|
else:
|
|
|
|
self.assertFalse(func(mode))
|
|
|
|
|
2022-06-06 14:24:11 -03:00
|
|
|
@os_helper.skip_unless_working_chmod
|
2012-05-15 10:30:25 -03:00
|
|
|
def test_mode(self):
|
|
|
|
with open(TESTFN, 'w'):
|
|
|
|
pass
|
2012-05-16 11:01:59 -03:00
|
|
|
if os.name == 'posix':
|
|
|
|
os.chmod(TESTFN, 0o700)
|
2013-06-21 13:25:56 -03:00
|
|
|
st_mode, modestr = self.get_mode()
|
|
|
|
self.assertEqual(modestr, '-rwx------')
|
|
|
|
self.assertS_IS("REG", st_mode)
|
2013-06-22 16:05:02 -03:00
|
|
|
self.assertEqual(self.statmod.S_IMODE(st_mode),
|
|
|
|
self.statmod.S_IRWXU)
|
2013-06-21 13:25:56 -03:00
|
|
|
|
2012-05-16 11:01:59 -03:00
|
|
|
os.chmod(TESTFN, 0o070)
|
2013-06-21 13:25:56 -03:00
|
|
|
st_mode, modestr = self.get_mode()
|
|
|
|
self.assertEqual(modestr, '----rwx---')
|
|
|
|
self.assertS_IS("REG", st_mode)
|
2013-06-22 16:05:02 -03:00
|
|
|
self.assertEqual(self.statmod.S_IMODE(st_mode),
|
|
|
|
self.statmod.S_IRWXG)
|
2013-06-21 13:25:56 -03:00
|
|
|
|
2012-05-16 11:01:59 -03:00
|
|
|
os.chmod(TESTFN, 0o007)
|
2013-06-21 13:25:56 -03:00
|
|
|
st_mode, modestr = self.get_mode()
|
|
|
|
self.assertEqual(modestr, '-------rwx')
|
|
|
|
self.assertS_IS("REG", st_mode)
|
2013-06-22 16:05:02 -03:00
|
|
|
self.assertEqual(self.statmod.S_IMODE(st_mode),
|
|
|
|
self.statmod.S_IRWXO)
|
2013-06-21 13:25:56 -03:00
|
|
|
|
2012-05-16 11:01:59 -03:00
|
|
|
os.chmod(TESTFN, 0o444)
|
2013-06-21 13:25:56 -03:00
|
|
|
st_mode, modestr = self.get_mode()
|
|
|
|
self.assertS_IS("REG", st_mode)
|
|
|
|
self.assertEqual(modestr, '-r--r--r--')
|
2013-06-22 16:05:02 -03:00
|
|
|
self.assertEqual(self.statmod.S_IMODE(st_mode), 0o444)
|
2012-05-16 11:01:59 -03:00
|
|
|
else:
|
|
|
|
os.chmod(TESTFN, 0o700)
|
2013-06-21 13:25:56 -03:00
|
|
|
st_mode, modestr = self.get_mode()
|
|
|
|
self.assertEqual(modestr[:3], '-rw')
|
|
|
|
self.assertS_IS("REG", st_mode)
|
2013-06-22 16:05:02 -03:00
|
|
|
self.assertEqual(self.statmod.S_IFMT(st_mode),
|
|
|
|
self.statmod.S_IFREG)
|
2012-05-15 10:30:25 -03:00
|
|
|
|
2022-06-06 14:24:11 -03:00
|
|
|
@os_helper.skip_unless_working_chmod
|
2012-05-15 10:30:25 -03:00
|
|
|
def test_directory(self):
|
|
|
|
os.mkdir(TESTFN)
|
|
|
|
os.chmod(TESTFN, 0o700)
|
2013-06-21 13:25:56 -03:00
|
|
|
st_mode, modestr = self.get_mode()
|
|
|
|
self.assertS_IS("DIR", st_mode)
|
2012-05-16 11:01:59 -03:00
|
|
|
if os.name == 'posix':
|
2013-06-21 13:25:56 -03:00
|
|
|
self.assertEqual(modestr, 'drwx------')
|
2012-05-16 11:01:59 -03:00
|
|
|
else:
|
2013-06-21 13:25:56 -03:00
|
|
|
self.assertEqual(modestr[0], 'd')
|
2012-05-15 10:30:25 -03:00
|
|
|
|
2022-06-05 04:59:47 -03:00
|
|
|
@os_helper.skip_unless_symlink
|
2012-05-15 10:30:25 -03:00
|
|
|
def test_link(self):
|
2012-05-16 11:01:59 -03:00
|
|
|
try:
|
|
|
|
os.symlink(os.getcwd(), TESTFN)
|
|
|
|
except (OSError, NotImplementedError) as err:
|
|
|
|
raise unittest.SkipTest(str(err))
|
|
|
|
else:
|
2013-06-21 13:25:56 -03:00
|
|
|
st_mode, modestr = self.get_mode()
|
|
|
|
self.assertEqual(modestr[0], 'l')
|
|
|
|
self.assertS_IS("LNK", st_mode)
|
2012-05-15 10:30:25 -03:00
|
|
|
|
|
|
|
@unittest.skipUnless(hasattr(os, 'mkfifo'), 'os.mkfifo not available')
|
|
|
|
def test_fifo(self):
|
2020-12-01 04:20:50 -04:00
|
|
|
if sys.platform == "vxworks":
|
|
|
|
fifo_path = os.path.join("/fifos/", TESTFN)
|
|
|
|
else:
|
|
|
|
fifo_path = TESTFN
|
|
|
|
self.addCleanup(os_helper.unlink, fifo_path)
|
2017-11-12 12:31:07 -04:00
|
|
|
try:
|
2020-12-01 04:20:50 -04:00
|
|
|
os.mkfifo(fifo_path, 0o700)
|
2017-11-12 12:31:07 -04:00
|
|
|
except PermissionError as e:
|
|
|
|
self.skipTest('os.mkfifo(): %s' % e)
|
2020-12-01 04:20:50 -04:00
|
|
|
st_mode, modestr = self.get_mode(fifo_path)
|
2013-06-21 13:25:56 -03:00
|
|
|
self.assertEqual(modestr, 'prwx------')
|
|
|
|
self.assertS_IS("FIFO", st_mode)
|
|
|
|
|
|
|
|
@unittest.skipUnless(os.name == 'posix', 'requires Posix')
|
|
|
|
def test_devices(self):
|
|
|
|
if os.path.exists(os.devnull):
|
2013-06-23 11:10:29 -03:00
|
|
|
st_mode, modestr = self.get_mode(os.devnull, lstat=False)
|
2013-06-21 13:25:56 -03:00
|
|
|
self.assertEqual(modestr[0], 'c')
|
|
|
|
self.assertS_IS("CHR", st_mode)
|
2013-06-22 09:48:32 -03:00
|
|
|
# Linux block devices, BSD has no block devices anymore
|
2013-06-21 13:53:13 -03:00
|
|
|
for blockdev in ("/dev/sda", "/dev/hda"):
|
2013-06-21 13:25:56 -03:00
|
|
|
if os.path.exists(blockdev):
|
2013-06-23 11:10:29 -03:00
|
|
|
st_mode, modestr = self.get_mode(blockdev, lstat=False)
|
2013-06-21 13:25:56 -03:00
|
|
|
self.assertEqual(modestr[0], 'b')
|
|
|
|
self.assertS_IS("BLK", st_mode)
|
|
|
|
break
|
|
|
|
|
2020-04-25 04:06:29 -03:00
|
|
|
@socket_helper.skip_unless_bind_unix_socket
|
2018-08-10 02:12:08 -03:00
|
|
|
def test_socket(self):
|
|
|
|
with socket.socket(socket.AF_UNIX) as s:
|
|
|
|
s.bind(TESTFN)
|
|
|
|
st_mode, modestr = self.get_mode()
|
|
|
|
self.assertEqual(modestr[0], 's')
|
|
|
|
self.assertS_IS("SOCK", st_mode)
|
|
|
|
|
2013-06-21 13:25:56 -03:00
|
|
|
def test_module_attributes(self):
|
|
|
|
for key, value in self.stat_struct.items():
|
2013-06-22 16:05:02 -03:00
|
|
|
modvalue = getattr(self.statmod, key)
|
2013-06-21 13:25:56 -03:00
|
|
|
self.assertEqual(value, modvalue, key)
|
|
|
|
for key, value in self.permission_bits.items():
|
2013-06-22 16:05:02 -03:00
|
|
|
modvalue = getattr(self.statmod, key)
|
2013-06-21 13:25:56 -03:00
|
|
|
self.assertEqual(value, modvalue, key)
|
|
|
|
for key in self.file_flags:
|
2013-06-22 16:05:02 -03:00
|
|
|
modvalue = getattr(self.statmod, key)
|
2013-06-21 13:25:56 -03:00
|
|
|
self.assertIsInstance(modvalue, int)
|
|
|
|
for key in self.formats:
|
2013-06-22 16:05:02 -03:00
|
|
|
modvalue = getattr(self.statmod, key)
|
2013-06-21 13:25:56 -03:00
|
|
|
self.assertIsInstance(modvalue, int)
|
|
|
|
for key in self.format_funcs:
|
2013-06-22 16:05:02 -03:00
|
|
|
func = getattr(self.statmod, key)
|
2013-06-21 13:25:56 -03:00
|
|
|
self.assertTrue(callable(func))
|
|
|
|
self.assertEqual(func(0), 0)
|
2012-05-15 10:30:25 -03:00
|
|
|
|
2014-06-19 11:46:37 -03:00
|
|
|
@unittest.skipUnless(sys.platform == "win32",
|
|
|
|
"FILE_ATTRIBUTE_* constants are Win32 specific")
|
|
|
|
def test_file_attribute_constants(self):
|
|
|
|
for key, value in sorted(self.file_attributes.items()):
|
|
|
|
self.assertTrue(hasattr(self.statmod, key), key)
|
|
|
|
modvalue = getattr(self.statmod, key)
|
|
|
|
self.assertEqual(value, modvalue, key)
|
|
|
|
|
2012-05-15 10:30:25 -03:00
|
|
|
|
2013-06-29 07:58:57 -03:00
|
|
|
class TestFilemodeCStat(TestFilemode, unittest.TestCase):
|
2013-06-22 16:05:02 -03:00
|
|
|
statmod = c_stat
|
|
|
|
|
|
|
|
|
2013-06-29 07:58:57 -03:00
|
|
|
class TestFilemodePyStat(TestFilemode, unittest.TestCase):
|
2013-06-22 16:05:02 -03:00
|
|
|
statmod = py_stat
|
|
|
|
|
|
|
|
|
2012-05-15 10:30:25 -03:00
|
|
|
if __name__ == '__main__':
|
2013-06-29 07:58:57 -03:00
|
|
|
unittest.main()
|