11 class xattrTest(unittest.TestCase):
12 USER_ATTR = "user.test"
21 """tear down function"""
22 for fname in self.rmfiles:
24 for dname in self.rmdirs:
28 """create a temp file"""
29 fh, fname = tempfile.mkstemp(".test", "xattr-", ".")
30 self.rmfiles.append(fname)
34 """create a temp dir"""
35 dname = tempfile.mkdtemp(".test", "xattr-", ".")
36 self.rmdirs.append(dname)
39 def _getsymlink(self):
40 """create a symlink"""
41 fh, fname = self._getfile()
44 os.symlink(fname + ".non-existent", fname)
47 def _checkListSetGet(self, item, symlink=False):
48 """check list, set, get operations against an item"""
49 self.failUnlessEqual(xattr.listxattr(item, symlink), [])
50 self.failUnlessRaises(EnvironmentError, xattr.setxattr, item,
51 self.USER_ATTR, self.USER_VAL,
54 xattr.setxattr(item, self.USER_ATTR, self.USER_VAL, 0, symlink)
56 if err.errno == errno.EPERM and symlink:
57 # symlinks may fail, in which case we abort the rest
58 # of the test for this case
61 self.failUnlessRaises(EnvironmentError, xattr.setxattr, item,
62 self.USER_ATTR, self.USER_VAL,
64 self.failUnlessEqual(xattr.listxattr(item, symlink), [self.USER_ATTR])
65 self.failUnlessEqual(xattr.getxattr(item, self.USER_ATTR, symlink),
67 xattr.removexattr(item, self.USER_ATTR)
68 self.failUnlessEqual(xattr.listxattr(item, symlink), [])
69 self.failUnlessRaises(EnvironmentError, xattr.removexattr,
72 def testNoXattr(self):
73 """test no attributes"""
74 fh, fname = self._getfile()
75 self.failUnlessEqual(xattr.listxattr(fname), [])
76 dname = self._getdir()
77 self.failUnlessEqual(xattr.listxattr(dname), [])
78 sname = self._getsymlink()
79 self.failUnlessEqual(xattr.listxattr(sname, True), [])
81 def testFileByName(self):
82 """test set and retrieve one attribute by file name"""
83 fh, fname = self._getfile()
84 self._checkListSetGet(fname)
87 def testFileByDescriptor(self):
88 """test file descriptor operations"""
89 fh, fname = self._getfile()
90 self._checkListSetGet(fh)
93 def testFileByObject(self):
94 """test file descriptor operations"""
95 fh, fname = self._getfile()
97 self._checkListSetGet(fo)
100 def testMixedAccess(self):
101 """test mixed access to file"""
102 fh, fname = self._getfile()
104 self.failUnlessEqual(xattr.listxattr(fname), [])
105 xattr.setxattr(fname, self.USER_ATTR, self.USER_VAL)
106 self.failUnlessEqual(xattr.listxattr(fh), [self.USER_ATTR])
107 self.failUnlessEqual(xattr.getxattr(fo, self.USER_ATTR),
110 def testDirOps(self):
111 """test attribute setting on directories"""
112 dname = self._getdir()
113 self._checkListSetGet(dname)
115 def testSymlinkOps(self):
116 """test symlink operations"""
117 sname = self._getsymlink()
118 self.failUnlessRaises(EnvironmentError, xattr.listxattr, sname)
119 self._checkListSetGet(sname, symlink=True)
121 def testBinaryPayload(self):
122 """test binary values"""
123 fh, fname = self._getfile()
125 BINVAL = "abc" + '\0' + "def"
126 xattr.setxattr(fname, self.USER_ATTR, BINVAL)
127 self.failUnlessEqual(xattr.listxattr(fname), [self.USER_ATTR])
128 self.failUnlessEqual(xattr.getxattr(fname, self.USER_ATTR), BINVAL)
129 xattr.removexattr(fname, self.USER_ATTR)
131 def testManyOps(self):
133 fh, fname = self._getfile()
134 xattr.setxattr(fh, self.USER_ATTR, self.USER_VAL)
135 VL = [self.USER_ATTR]
136 for i in range(131072):
137 self.failUnlessEqual(xattr.listxattr(fh), VL)
138 for i in range(131072):
139 self.failUnlessEqual(xattr.getxattr(fh, self.USER_ATTR),