11 TEST_DIR=os.environ.get("TESTDIR", ".")
13 BASIC_ACL_TEXT="u::rw,g::r,o::-"
16 """Wrapper to skip a test"""
17 new_fn = lambda x: None
18 new_fn.__doc__ = "SKIPPED %s" % fn.__doc__
22 def has_ext(extension):
23 """Decorator to skip tests based on platform support"""
31 """Support functions ACLs"""
39 """tear down function"""
40 for fname in self.rmfiles:
42 for dname in self.rmdirs:
46 """create a temp file"""
47 fh, fname = tempfile.mkstemp(".test", "xattr-", TEST_DIR)
48 self.rmfiles.append(fname)
52 """create a temp dir"""
53 dname = tempfile.mkdtemp(".test", "xattr-", TEST_DIR)
54 self.rmdirs.append(dname)
57 def _getsymlink(self):
58 """create a symlink"""
59 fh, fname = self._getfile()
62 os.symlink(fname + ".non-existent", fname)
66 class LoadTests(aclTest, unittest.TestCase):
67 """Load/create tests"""
68 def testFromFile(self):
69 """Test loading ACLs from a file"""
70 _, fname = self._getfile()
71 acl1 = posix1e.ACL(file=fname)
72 self.failUnless(acl1.valid(), "ACL read from file should be valid")
74 def testFromDir(self):
75 """Test loading ACLs from a directory"""
76 dname = self._getdir()
77 acl1 = posix1e.ACL(file=dname)
78 acl2 = posix1e.ACL(filedef=dname)
79 self.failUnless(acl1.valid(),
80 "ACL read from directory should be valid")
81 # default ACLs might or might not be valid; missing ones are
82 # not valid, so we don't test acl2 for validity
85 """Test loading ACLs from a file descriptor"""
86 fd, _ = self._getfile()
87 acl1 = posix1e.ACL(fd=fd)
88 self.failUnless(acl1.valid(), "ACL read from fd should be valid")
90 def testFromEmpty(self):
91 """Test creating an empty ACL"""
93 self.failIf(acl1.valid(), "Empty ACL should not be valid")
95 def testFromText(self):
96 """Test creating an ACL from text"""
97 acl1 = posix1e.ACL(text=BASIC_ACL_TEXT)
98 self.failUnless(acl1.valid(),
99 "ACL based on standard description should be valid")
101 class AclExtensions(aclTest, unittest.TestCase):
102 """ACL extensions checks"""
104 @has_ext(HAS_ACL_FROM_MODE)
105 def testFromMode(self):
106 """Test loading ACLs from an octal mode"""
107 acl1 = posix1e.ACL(mode=0644)
108 self.failUnless(acl1.valid(),
109 "ACL created via octal mode shoule be valid")
111 @has_ext(HAS_ACL_CHECK)
112 def testAclCheck(self):
113 """Test the acl_check method"""
114 acl1 = posix1e.ACL(text=BASIC_ACL_TEXT)
115 self.failIf(acl1.check(), "ACL is not valid")
117 self.failUnless(acl2.check(), "Empty ACL should not be valid")
120 class WriteTests(aclTest, unittest.TestCase):
123 def testDeleteDefault(self):
124 """Test removing the default ACL"""
125 dname = self._getdir()
126 posix1e.delete_default(dname)
128 def testReapply(self):
129 """Test re-applying an ACL"""
130 fd, fname = self._getfile()
131 acl1 = posix1e.ACL(fd=fd)
134 dname = self._getdir()
135 acl2 = posix1e.ACL(file=fname)
139 class ModificationTests(aclTest, unittest.TestCase):
140 """ACL modification tests"""
142 @has_ext(HAS_ACL_ENTRY)
143 def testAppend(self):
144 """Test append a new Entry to the ACL"""
147 e.tag_type = posix1e.ACL_OTHER
150 @has_ext(HAS_ACL_ENTRY)
151 def testDelete(self):
152 """Test delete Entry from the ACL"""
155 e.tag_type = posix1e.ACL_OTHER
160 @has_ext(HAS_ACL_ENTRY)
161 def testDoubleEntries(self):
162 """Test double entries"""
163 acl = posix1e.ACL(text=BASIC_ACL_TEXT)
164 self.failUnless(acl.valid(), "ACL is not valid")
165 for tag_type in (posix1e.ACL_USER_OBJ, posix1e.ACL_GROUP_OBJ,
168 e.tag_type = tag_type
170 self.failIf(acl.valid(),
171 "ACL containing duplicate entries should not be valid")
174 @has_ext(HAS_ACL_ENTRY)
175 def testMultipleGoodEntries(self):
176 """Test multiple valid entries"""
177 acl = posix1e.ACL(text=BASIC_ACL_TEXT)
178 self.failUnless(acl.valid(), "ACL is not valid")
179 for tag_type in (posix1e.ACL_USER,
181 for obj_id in range(5):
183 e.tag_type = tag_type
187 self.failUnless(acl.valid(),
188 "ACL should be able to hold multiple"
189 " user/group entries")
191 @has_ext(HAS_ACL_ENTRY)
192 def testMultipleBadEntries(self):
193 """Test multiple invalid entries"""
194 acl = posix1e.ACL(text=BASIC_ACL_TEXT)
195 self.failUnless(acl.valid(), "ACL built from standard description"
197 for tag_type in (posix1e.ACL_USER,
200 e1.tag_type = tag_type
204 self.failUnless(acl.valid(), "ACL should be able to add a"
207 e2.tag_type = tag_type
211 self.failIf(acl.valid(), "ACL should not validate when"
212 " containing two duplicate entries")
216 @has_ext(HAS_ACL_ENTRY)
217 def testPermset(self):
218 """Test permissions"""
224 posix1e.ACL_READ: "read",
225 posix1e.ACL_WRITE: "write",
226 posix1e.ACL_EXECUTE: "execute",
229 self.failIf(ps.test(perm), "Empty permission set should not"
230 " have permission '%s'" % pmap[perm])
232 self.failUnless(ps.test(perm), "Permission '%s' should exist"
233 " after addition" % pmap[perm])
235 self.failIf(ps.test(perm), "Permission '%s' should not exist"
236 " after deletion" % pmap[perm])
239 if __name__ == "__main__":