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 self.failUnlessEqual(xattr.get_all(item, nofollow=symlink),
68 [(self.USER_ATTR, self.USER_VAL)])
69 xattr.removexattr(item, self.USER_ATTR)
70 self.failUnlessEqual(xattr.listxattr(item, symlink), [])
71 self.failUnlessEqual(xattr.get_all(item, nofollow=symlink), [])
72 self.failUnlessRaises(EnvironmentError, xattr.removexattr,
75 def testNoXattr(self):
76 """test no attributes"""
77 fh, fname = self._getfile()
78 self.failUnlessEqual(xattr.listxattr(fname), [])
79 self.failUnlessEqual(xattr.get_all(fname), [])
80 dname = self._getdir()
81 self.failUnlessEqual(xattr.listxattr(dname), [])
82 self.failUnlessEqual(xattr.get_all(dname), [])
83 sname = self._getsymlink()
84 self.failUnlessEqual(xattr.listxattr(sname, True), [])
85 self.failUnlessEqual(xattr.get_all(sname, nofollow=True), [])
87 def testFileByName(self):
88 """test set and retrieve one attribute by file name"""
89 fh, fname = self._getfile()
90 self._checkListSetGet(fname)
93 def testFileByDescriptor(self):
94 """test file descriptor operations"""
95 fh, fname = self._getfile()
96 self._checkListSetGet(fh)
99 def testFileByObject(self):
100 """test file descriptor operations"""
101 fh, fname = self._getfile()
103 self._checkListSetGet(fo)
106 def testMixedAccess(self):
107 """test mixed access to file"""
108 fh, fname = self._getfile()
110 self.failUnlessEqual(xattr.listxattr(fname), [])
111 xattr.setxattr(fname, self.USER_ATTR, self.USER_VAL)
112 self.failUnlessEqual(xattr.listxattr(fh), [self.USER_ATTR])
113 self.failUnlessEqual(xattr.getxattr(fo, self.USER_ATTR),
115 self.failUnlessEqual(xattr.get_all(fo),
116 [(self.USER_ATTR, self.USER_VAL)])
117 self.failUnlessEqual(xattr.get_all(fname),
118 [(self.USER_ATTR, self.USER_VAL)])
120 def testDirOps(self):
121 """test attribute setting on directories"""
122 dname = self._getdir()
123 self._checkListSetGet(dname)
125 def testSymlinkOps(self):
126 """test symlink operations"""
127 sname = self._getsymlink()
128 self.failUnlessRaises(EnvironmentError, xattr.listxattr, sname)
129 self._checkListSetGet(sname, symlink=True)
131 def testBinaryPayload(self):
132 """test binary values"""
133 fh, fname = self._getfile()
135 BINVAL = "abc" + '\0' + "def"
136 xattr.setxattr(fname, self.USER_ATTR, BINVAL)
137 self.failUnlessEqual(xattr.listxattr(fname), [self.USER_ATTR])
138 self.failUnlessEqual(xattr.getxattr(fname, self.USER_ATTR), BINVAL)
139 self.failUnlessEqual(xattr.get_all(fname), [(self.USER_ATTR, BINVAL)])
140 xattr.removexattr(fname, self.USER_ATTR)
142 def testManyOps(self):
144 fh, fname = self._getfile()
145 xattr.setxattr(fh, self.USER_ATTR, self.USER_VAL)
146 VL = [self.USER_ATTR]
147 for i in range(131072):
148 self.failUnlessEqual(xattr.listxattr(fh), VL)
149 for i in range(131072):
150 self.failUnlessEqual(xattr.getxattr(fh, self.USER_ATTR),
152 for i in range(131072):
153 self.failUnlessEqual(xattr.get_all(fh),
154 [(self.USER_ATTR, self.USER_VAL)])