10 from xattr import NS_USER, XATTR_CREATE, XATTR_REPLACE
12 TEST_DIR = os.environ.get("TESTDIR", ".")
15 class xattrTest(unittest.TestCase):
17 USER_ATTR = "%s.%s" % (NS_USER, USER_NN)
19 MANYOPS_COUNT = 131072
27 """tear down function"""
28 for fname in self.rmfiles:
31 except EnvironmentError:
33 for dname in self.rmdirs:
36 except EnvironmentError:
40 """create a temp file"""
41 fh, fname = tempfile.mkstemp(".test", "xattr-", TEST_DIR)
42 self.rmfiles.append(fname)
46 """create a temp dir"""
47 dname = tempfile.mkdtemp(".test", "xattr-", TEST_DIR)
48 self.rmdirs.append(dname)
51 def _getsymlink(self, dangling=True):
52 """create a symlink"""
53 fh, fname = self._getfile()
57 sname = fname + ".symlink"
58 os.symlink(fname, sname)
59 self.rmfiles.append(sname)
62 def _checkDeprecated(self, item, symlink=False):
63 """check deprecated list, set, get operations against an item"""
64 self.failUnlessEqual(xattr.listxattr(item, symlink), [])
65 self.failUnlessRaises(EnvironmentError, xattr.setxattr, item,
66 self.USER_ATTR, self.USER_VAL,
69 xattr.setxattr(item, self.USER_ATTR, self.USER_VAL, 0, symlink)
71 if err.errno == errno.EPERM and symlink:
72 # symlinks may fail, in which case we abort the rest
73 # of the test for this case
76 self.failUnlessRaises(EnvironmentError, xattr.setxattr, item,
77 self.USER_ATTR, self.USER_VAL,
79 self.failUnlessEqual(xattr.listxattr(item, symlink), [self.USER_ATTR])
80 self.failUnlessEqual(xattr.getxattr(item, self.USER_ATTR, symlink),
82 self.failUnlessEqual(xattr.get_all(item, nofollow=symlink),
83 [(self.USER_ATTR, self.USER_VAL)])
84 xattr.removexattr(item, self.USER_ATTR)
85 self.failUnlessEqual(xattr.listxattr(item, symlink), [])
86 self.failUnlessEqual(xattr.get_all(item, nofollow=symlink), [])
87 self.failUnlessRaises(EnvironmentError, xattr.removexattr,
90 def _checkListSetGet(self, item, symlink=False, use_ns=False):
91 """check list, set, get operations against an item"""
92 self.failUnlessEqual(xattr.list(item, symlink), [])
93 self.failUnlessRaises(EnvironmentError, xattr.set, item,
94 self.USER_ATTR, self.USER_VAL,
96 self.failUnlessRaises(EnvironmentError, xattr.set, item,
97 self.USER_NN, self.USER_VAL,
102 xattr.set(item, self.USER_NN, self.USER_VAL,
106 xattr.set(item, self.USER_ATTR, self.USER_VAL,
109 if err.errno == errno.EPERM and symlink:
110 # symlinks may fail, in which case we abort the rest
111 # of the test for this case
114 self.failUnlessRaises(EnvironmentError, xattr.set, item,
115 self.USER_ATTR, self.USER_VAL,
117 self.failUnlessRaises(EnvironmentError, xattr.set, item,
118 self.USER_NN, self.USER_VAL,
121 self.failUnlessEqual(xattr.list(item, nofollow=symlink),
123 self.failUnlessEqual(xattr.list(item, namespace=NS_USER,
126 self.failUnlessEqual(xattr.get(item, self.USER_ATTR, nofollow=symlink),
128 self.failUnlessEqual(xattr.get(item, self.USER_NN, nofollow=symlink,
131 self.failUnlessEqual(xattr.get_all(item, nofollow=symlink),
132 [(self.USER_ATTR, self.USER_VAL)])
133 self.failUnlessEqual(xattr.get_all(item, nofollow=symlink,
135 [(self.USER_NN, self.USER_VAL)])
137 xattr.remove(item, self.USER_NN, namespace=NS_USER)
139 xattr.remove(item, self.USER_ATTR)
140 self.failUnlessEqual(xattr.list(item, symlink), [])
141 self.failUnlessEqual(xattr.get_all(item, nofollow=symlink), [])
142 self.failUnlessRaises(EnvironmentError, xattr.remove,
143 item, self.USER_ATTR, nofollow=symlink)
144 self.failUnlessRaises(EnvironmentError, xattr.remove,
145 item, self.USER_NN, namespace=NS_USER,
148 def testNoXattrDeprecated(self):
149 """test no attributes (deprecated functions)"""
150 fh, fname = self._getfile()
151 self.failUnlessEqual(xattr.listxattr(fname), [])
152 self.failUnlessEqual(xattr.get_all(fname), [])
153 dname = self._getdir()
154 self.failUnlessEqual(xattr.listxattr(dname), [])
155 self.failUnlessEqual(xattr.get_all(dname), [])
156 _, sname = self._getsymlink()
157 self.failUnlessEqual(xattr.listxattr(sname, True), [])
158 self.failUnlessEqual(xattr.get_all(sname, nofollow=True), [])
160 def testNoXattr(self):
161 """test no attributes"""
162 fh, fname = self._getfile()
163 self.failUnlessEqual(xattr.list(fname), [])
164 self.failUnlessEqual(xattr.list(fname, namespace=NS_USER), [])
165 self.failUnlessEqual(xattr.get_all(fname), [])
166 self.failUnlessEqual(xattr.get_all(fname, namespace=NS_USER), [])
167 dname = self._getdir()
168 self.failUnlessEqual(xattr.list(dname), [])
169 self.failUnlessEqual(xattr.list(dname, namespace=NS_USER), [])
170 self.failUnlessEqual(xattr.get_all(dname), [])
171 self.failUnlessEqual(xattr.get_all(dname, namespace=NS_USER), [])
172 _, sname = self._getsymlink()
173 self.failUnlessEqual(xattr.list(sname, nofollow=True), [])
174 self.failUnlessEqual(xattr.list(sname, nofollow=True,
175 namespace=NS_USER), [])
176 self.failUnlessEqual(xattr.get_all(sname, nofollow=True), [])
177 self.failUnlessEqual(xattr.get_all(sname, nofollow=True,
178 namespace=NS_USER), [])
180 def testFileByNameDeprecated(self):
181 """test set and retrieve one attribute by file name (deprecated)"""
182 fh, fname = self._getfile()
183 self._checkDeprecated(fname)
186 def testFileByName(self):
187 """test set and retrieve one attribute by file name"""
188 fh, fname = self._getfile()
189 self._checkListSetGet(fname)
190 self._checkListSetGet(fname, use_ns=True)
193 def testFileByDescriptorDeprecated(self):
194 """test file descriptor operations (deprecated functions)"""
195 fh, fname = self._getfile()
196 self._checkDeprecated(fh)
199 def testFileByDescriptor(self):
200 """test file descriptor operations"""
201 fh, fname = self._getfile()
202 self._checkListSetGet(fh)
203 self._checkListSetGet(fh, use_ns=True)
206 def testFileByObjectDeprecated(self):
207 """test file descriptor operations (deprecated functions)"""
208 fh, fname = self._getfile()
210 self._checkDeprecated(fo)
213 def testFileByObject(self):
214 """test file descriptor operations"""
215 fh, fname = self._getfile()
217 self._checkListSetGet(fo)
218 self._checkListSetGet(fo, use_ns=True)
221 def testMixedAccessDeprecated(self):
222 """test mixed access to file (deprecated functions)"""
223 fh, fname = self._getfile()
225 self.failUnlessEqual(xattr.listxattr(fname), [])
226 xattr.setxattr(fname, self.USER_ATTR, self.USER_VAL)
227 self.failUnlessEqual(xattr.listxattr(fh), [self.USER_ATTR])
228 self.failUnlessEqual(xattr.getxattr(fo, self.USER_ATTR),
230 self.failUnlessEqual(xattr.get_all(fo),
231 [(self.USER_ATTR, self.USER_VAL)])
232 self.failUnlessEqual(xattr.get_all(fname),
233 [(self.USER_ATTR, self.USER_VAL)])
235 def testMixedAccess(self):
236 """test mixed access to file"""
237 fh, fname = self._getfile()
239 self.failUnlessEqual(xattr.list(fname), [])
240 xattr.set(fname, self.USER_ATTR, self.USER_VAL)
241 self.failUnlessEqual(xattr.list(fh), [self.USER_ATTR])
242 self.failUnlessEqual(xattr.list(fh, namespace=NS_USER),
244 self.failUnlessEqual(xattr.get(fo, self.USER_ATTR),
246 self.failUnlessEqual(xattr.get(fo, self.USER_NN, namespace=NS_USER),
248 self.failUnlessEqual(xattr.get_all(fo),
249 [(self.USER_ATTR, self.USER_VAL)])
250 self.failUnlessEqual(xattr.get_all(fo, namespace=NS_USER),
251 [(self.USER_NN, self.USER_VAL)])
252 self.failUnlessEqual(xattr.get_all(fname),
253 [(self.USER_ATTR, self.USER_VAL)])
254 self.failUnlessEqual(xattr.get_all(fname, namespace=NS_USER),
255 [(self.USER_NN, self.USER_VAL)])
257 def testDirOpsDeprecated(self):
258 """test attribute setting on directories (deprecated functions)"""
259 dname = self._getdir()
260 self._checkDeprecated(dname)
262 def testDirOps(self):
263 """test attribute setting on directories"""
264 dname = self._getdir()
265 self._checkListSetGet(dname)
266 self._checkListSetGet(dname, use_ns=True)
268 def testSymlinkOpsDeprecated(self):
269 """test symlink operations (deprecated functions)"""
270 _, sname = self._getsymlink()
271 self.failUnlessRaises(EnvironmentError, xattr.listxattr, sname)
272 self._checkDeprecated(sname, symlink=True)
273 target, sname = self._getsymlink(dangling=False)
274 xattr.setxattr(target, self.USER_ATTR, self.USER_VAL)
275 self.failUnlessEqual(xattr.listxattr(target), [self.USER_ATTR])
276 self.failUnlessEqual(xattr.listxattr(sname, True), [])
277 self.failUnlessRaises(EnvironmentError, xattr.removexattr, sname,
278 self.USER_ATTR, True)
279 xattr.removexattr(sname, self.USER_ATTR, False)
281 def testSymlinkOps(self):
282 """test symlink operations"""
283 _, sname = self._getsymlink()
284 self.failUnlessRaises(EnvironmentError, xattr.list, sname)
285 self._checkListSetGet(sname, symlink=True)
286 self._checkListSetGet(sname, symlink=True, use_ns=True)
287 target, sname = self._getsymlink(dangling=False)
288 xattr.set(target, self.USER_ATTR, self.USER_VAL)
289 self.failUnlessEqual(xattr.list(target), [self.USER_ATTR])
290 self.failUnlessEqual(xattr.list(sname, nofollow=True), [])
291 self.failUnlessRaises(EnvironmentError, xattr.remove, sname,
292 self.USER_ATTR, nofollow=True)
293 xattr.remove(sname, self.USER_ATTR, nofollow=False)
295 def testBinaryPayloadDeprecated(self):
296 """test binary values (deprecated functions)"""
297 fh, fname = self._getfile()
299 BINVAL = "abc" + '\0' + "def"
300 xattr.setxattr(fname, self.USER_ATTR, BINVAL)
301 self.failUnlessEqual(xattr.listxattr(fname), [self.USER_ATTR])
302 self.failUnlessEqual(xattr.getxattr(fname, self.USER_ATTR), BINVAL)
303 self.failUnlessEqual(xattr.get_all(fname), [(self.USER_ATTR, BINVAL)])
304 xattr.removexattr(fname, self.USER_ATTR)
306 def testBinaryPayload(self):
307 """test binary values"""
308 fh, fname = self._getfile()
310 BINVAL = "abc" + '\0' + "def"
311 xattr.set(fname, self.USER_ATTR, BINVAL)
312 self.failUnlessEqual(xattr.list(fname), [self.USER_ATTR])
313 self.failUnlessEqual(xattr.list(fname, namespace=NS_USER),
315 self.failUnlessEqual(xattr.get(fname, self.USER_ATTR), BINVAL)
316 self.failUnlessEqual(xattr.get(fname, self.USER_NN,
317 namespace=NS_USER), BINVAL)
318 self.failUnlessEqual(xattr.get_all(fname), [(self.USER_ATTR, BINVAL)])
319 self.failUnlessEqual(xattr.get_all(fname, namespace=NS_USER),
320 [(self.USER_NN, BINVAL)])
321 xattr.remove(fname, self.USER_ATTR)
323 def testManyOpsDeprecated(self):
324 """test many ops (deprecated functions)"""
325 fh, fname = self._getfile()
326 xattr.setxattr(fh, self.USER_ATTR, self.USER_VAL)
327 VL = [self.USER_ATTR]
328 for i in range(self.MANYOPS_COUNT):
329 self.failUnlessEqual(xattr.listxattr(fh), VL)
330 for i in range(self.MANYOPS_COUNT):
331 self.failUnlessEqual(xattr.getxattr(fh, self.USER_ATTR),
333 for i in range(self.MANYOPS_COUNT):
334 self.failUnlessEqual(xattr.get_all(fh),
335 [(self.USER_ATTR, self.USER_VAL)])
337 def testManyOps(self):
339 fh, fname = self._getfile()
340 xattr.set(fh, self.USER_ATTR, self.USER_VAL)
341 VL = [self.USER_ATTR]
343 for i in range(self.MANYOPS_COUNT):
344 self.failUnlessEqual(xattr.list(fh), VL)
345 self.failUnlessEqual(xattr.list(fh, namespace=NS_USER), VN)
346 for i in range(self.MANYOPS_COUNT):
347 self.failUnlessEqual(xattr.get(fh, self.USER_ATTR),
349 self.failUnlessEqual(xattr.get(fh, self.USER_NN,
352 for i in range(self.MANYOPS_COUNT):
353 self.failUnlessEqual(xattr.get_all(fh),
354 [(self.USER_ATTR, self.USER_VAL)])
355 self.failUnlessEqual(xattr.get_all(fh, namespace=NS_USER),
356 [(self.USER_NN, self.USER_VAL)])