11 from xattr import NS_USER, XATTR_CREATE, XATTR_REPLACE
13 if sys.hexversion >= 0x03000000:
18 TEST_DIR = os.environ.get("TESTDIR", ".")
21 class xattrTest(unittest.TestCase):
23 USER_ATTR = NS_USER.decode() + "." + USER_NN
25 MANYOPS_COUNT = 131072
28 USER_NN = USER_NN.encode()
29 USER_VAL = USER_VAL.encode()
30 USER_ATTR = USER_ATTR.encode()
38 """tear down function"""
39 for fname in self.rmfiles:
42 except EnvironmentError:
44 for dname in self.rmdirs:
47 except EnvironmentError:
51 """create a temp file"""
52 fh, fname = tempfile.mkstemp(".test", "xattr-", TEST_DIR)
53 self.rmfiles.append(fname)
57 """create a temp dir"""
58 dname = tempfile.mkdtemp(".test", "xattr-", TEST_DIR)
59 self.rmdirs.append(dname)
62 def _getsymlink(self, dangling=True):
63 """create a symlink"""
64 fh, fname = self._getfile()
68 sname = fname + ".symlink"
69 os.symlink(fname, sname)
70 self.rmfiles.append(sname)
73 def _checkDeprecated(self, item, symlink=False):
74 """check deprecated list, set, get operations against an item"""
75 self.failUnlessEqual(xattr.listxattr(item, symlink), [])
76 self.failUnlessRaises(EnvironmentError, xattr.setxattr, item,
77 self.USER_ATTR, self.USER_VAL,
80 xattr.setxattr(item, self.USER_ATTR, self.USER_VAL, 0, symlink)
82 err = sys.exc_info()[1]
83 if err.errno == errno.EPERM and symlink:
84 # symlinks may fail, in which case we abort the rest
85 # of the test for this case
88 self.failUnlessRaises(EnvironmentError, xattr.setxattr, item,
89 self.USER_ATTR, self.USER_VAL,
91 self.failUnlessEqual(xattr.listxattr(item, symlink), [self.USER_ATTR])
92 self.failUnlessEqual(xattr.getxattr(item, self.USER_ATTR, symlink),
94 self.failUnlessEqual(xattr.get_all(item, nofollow=symlink),
95 [(self.USER_ATTR, self.USER_VAL)])
96 xattr.removexattr(item, self.USER_ATTR)
97 self.failUnlessEqual(xattr.listxattr(item, symlink), [])
98 self.failUnlessEqual(xattr.get_all(item, nofollow=symlink), [])
99 self.failUnlessRaises(EnvironmentError, xattr.removexattr,
100 item, self.USER_ATTR)
102 def _checkListSetGet(self, item, symlink=False, use_ns=False):
103 """check list, set, get operations against an item"""
104 self.failUnlessEqual(xattr.list(item, symlink), [])
105 self.failUnlessRaises(EnvironmentError, xattr.set, item,
106 self.USER_ATTR, self.USER_VAL,
108 self.failUnlessRaises(EnvironmentError, xattr.set, item,
109 self.USER_NN, self.USER_VAL,
114 xattr.set(item, self.USER_NN, self.USER_VAL,
118 xattr.set(item, self.USER_ATTR, self.USER_VAL,
121 err = sys.exc_info()[1]
122 if err.errno == errno.EPERM and symlink:
123 # symlinks may fail, in which case we abort the rest
124 # of the test for this case
127 self.failUnlessRaises(EnvironmentError, xattr.set, item,
128 self.USER_ATTR, self.USER_VAL,
130 self.failUnlessRaises(EnvironmentError, xattr.set, item,
131 self.USER_NN, self.USER_VAL,
134 self.failUnlessEqual(xattr.list(item, nofollow=symlink),
136 self.failUnlessEqual(xattr.list(item, namespace=NS_USER,
139 self.failUnlessEqual(xattr.get(item, self.USER_ATTR, nofollow=symlink),
141 self.failUnlessEqual(xattr.get(item, self.USER_NN, nofollow=symlink,
144 self.failUnlessEqual(xattr.get_all(item, nofollow=symlink),
145 [(self.USER_ATTR, self.USER_VAL)])
146 self.failUnlessEqual(xattr.get_all(item, nofollow=symlink,
148 [(self.USER_NN, self.USER_VAL)])
150 xattr.remove(item, self.USER_NN, namespace=NS_USER)
152 xattr.remove(item, self.USER_ATTR)
153 self.failUnlessEqual(xattr.list(item, symlink), [])
154 self.failUnlessEqual(xattr.get_all(item, nofollow=symlink), [])
155 self.failUnlessRaises(EnvironmentError, xattr.remove,
156 item, self.USER_ATTR, nofollow=symlink)
157 self.failUnlessRaises(EnvironmentError, xattr.remove,
158 item, self.USER_NN, namespace=NS_USER,
161 def testNoXattrDeprecated(self):
162 """test no attributes (deprecated functions)"""
163 fh, fname = self._getfile()
164 self.failUnlessEqual(xattr.listxattr(fname), [])
165 self.failUnlessEqual(xattr.get_all(fname), [])
166 dname = self._getdir()
167 self.failUnlessEqual(xattr.listxattr(dname), [])
168 self.failUnlessEqual(xattr.get_all(dname), [])
169 _, sname = self._getsymlink()
170 self.failUnlessEqual(xattr.listxattr(sname, True), [])
171 self.failUnlessEqual(xattr.get_all(sname, nofollow=True), [])
173 def testNoXattr(self):
174 """test no attributes"""
175 fh, fname = self._getfile()
176 self.failUnlessEqual(xattr.list(fname), [])
177 self.failUnlessEqual(xattr.list(fname, namespace=NS_USER), [])
178 self.failUnlessEqual(xattr.get_all(fname), [])
179 self.failUnlessEqual(xattr.get_all(fname, namespace=NS_USER), [])
180 dname = self._getdir()
181 self.failUnlessEqual(xattr.list(dname), [])
182 self.failUnlessEqual(xattr.list(dname, namespace=NS_USER), [])
183 self.failUnlessEqual(xattr.get_all(dname), [])
184 self.failUnlessEqual(xattr.get_all(dname, namespace=NS_USER), [])
185 _, sname = self._getsymlink()
186 self.failUnlessEqual(xattr.list(sname, nofollow=True), [])
187 self.failUnlessEqual(xattr.list(sname, nofollow=True,
188 namespace=NS_USER), [])
189 self.failUnlessEqual(xattr.get_all(sname, nofollow=True), [])
190 self.failUnlessEqual(xattr.get_all(sname, nofollow=True,
191 namespace=NS_USER), [])
193 def testFileByNameDeprecated(self):
194 """test set and retrieve one attribute by file name (deprecated)"""
195 fh, fname = self._getfile()
196 self._checkDeprecated(fname)
199 def testFileByName(self):
200 """test set and retrieve one attribute by file name"""
201 fh, fname = self._getfile()
202 self._checkListSetGet(fname)
203 self._checkListSetGet(fname, use_ns=True)
206 def testFileByDescriptorDeprecated(self):
207 """test file descriptor operations (deprecated functions)"""
208 fh, fname = self._getfile()
209 self._checkDeprecated(fh)
212 def testFileByDescriptor(self):
213 """test file descriptor operations"""
214 fh, fname = self._getfile()
215 self._checkListSetGet(fh)
216 self._checkListSetGet(fh, use_ns=True)
219 def testFileByObjectDeprecated(self):
220 """test file descriptor operations (deprecated functions)"""
221 fh, fname = self._getfile()
223 self._checkDeprecated(fo)
226 def testFileByObject(self):
227 """test file descriptor operations"""
228 fh, fname = self._getfile()
230 self._checkListSetGet(fo)
231 self._checkListSetGet(fo, use_ns=True)
234 def testMixedAccessDeprecated(self):
235 """test mixed access to file (deprecated functions)"""
236 fh, fname = self._getfile()
238 self.failUnlessEqual(xattr.listxattr(fname), [])
239 xattr.setxattr(fname, self.USER_ATTR, self.USER_VAL)
240 self.failUnlessEqual(xattr.listxattr(fh), [self.USER_ATTR])
241 self.failUnlessEqual(xattr.getxattr(fo, self.USER_ATTR),
243 self.failUnlessEqual(xattr.get_all(fo),
244 [(self.USER_ATTR, self.USER_VAL)])
245 self.failUnlessEqual(xattr.get_all(fname),
246 [(self.USER_ATTR, self.USER_VAL)])
248 def testMixedAccess(self):
249 """test mixed access to file"""
250 fh, fname = self._getfile()
252 self.failUnlessEqual(xattr.list(fname), [])
253 xattr.set(fname, self.USER_ATTR, self.USER_VAL)
254 self.failUnlessEqual(xattr.list(fh), [self.USER_ATTR])
255 self.failUnlessEqual(xattr.list(fh, namespace=NS_USER),
257 self.failUnlessEqual(xattr.get(fo, self.USER_ATTR),
259 self.failUnlessEqual(xattr.get(fo, self.USER_NN, namespace=NS_USER),
261 self.failUnlessEqual(xattr.get_all(fo),
262 [(self.USER_ATTR, self.USER_VAL)])
263 self.failUnlessEqual(xattr.get_all(fo, namespace=NS_USER),
264 [(self.USER_NN, self.USER_VAL)])
265 self.failUnlessEqual(xattr.get_all(fname),
266 [(self.USER_ATTR, self.USER_VAL)])
267 self.failUnlessEqual(xattr.get_all(fname, namespace=NS_USER),
268 [(self.USER_NN, self.USER_VAL)])
270 def testDirOpsDeprecated(self):
271 """test attribute setting on directories (deprecated functions)"""
272 dname = self._getdir()
273 self._checkDeprecated(dname)
275 def testDirOps(self):
276 """test attribute setting on directories"""
277 dname = self._getdir()
278 self._checkListSetGet(dname)
279 self._checkListSetGet(dname, use_ns=True)
281 def testSymlinkOpsDeprecated(self):
282 """test symlink operations (deprecated functions)"""
283 _, sname = self._getsymlink()
284 self.failUnlessRaises(EnvironmentError, xattr.listxattr, sname)
285 self._checkDeprecated(sname, symlink=True)
286 target, sname = self._getsymlink(dangling=False)
287 xattr.setxattr(target, self.USER_ATTR, self.USER_VAL)
288 self.failUnlessEqual(xattr.listxattr(target), [self.USER_ATTR])
289 self.failUnlessEqual(xattr.listxattr(sname, True), [])
290 self.failUnlessRaises(EnvironmentError, xattr.removexattr, sname,
291 self.USER_ATTR, True)
292 xattr.removexattr(sname, self.USER_ATTR, False)
294 def testSymlinkOps(self):
295 """test symlink operations"""
296 _, sname = self._getsymlink()
297 self.failUnlessRaises(EnvironmentError, xattr.list, sname)
298 self._checkListSetGet(sname, symlink=True)
299 self._checkListSetGet(sname, symlink=True, use_ns=True)
300 target, sname = self._getsymlink(dangling=False)
301 xattr.set(target, self.USER_ATTR, self.USER_VAL)
302 self.failUnlessEqual(xattr.list(target), [self.USER_ATTR])
303 self.failUnlessEqual(xattr.list(sname, nofollow=True), [])
304 self.failUnlessRaises(EnvironmentError, xattr.remove, sname,
305 self.USER_ATTR, nofollow=True)
306 xattr.remove(sname, self.USER_ATTR, nofollow=False)
308 def testBinaryPayloadDeprecated(self):
309 """test binary values (deprecated functions)"""
310 fh, fname = self._getfile()
312 BINVAL = "abc" + '\0' + "def"
314 BINVAL = BINVAL.encode()
315 xattr.setxattr(fname, self.USER_ATTR, BINVAL)
316 self.failUnlessEqual(xattr.listxattr(fname), [self.USER_ATTR])
317 self.failUnlessEqual(xattr.getxattr(fname, self.USER_ATTR), BINVAL)
318 self.failUnlessEqual(xattr.get_all(fname), [(self.USER_ATTR, BINVAL)])
319 xattr.removexattr(fname, self.USER_ATTR)
321 def testBinaryPayload(self):
322 """test binary values"""
323 fh, fname = self._getfile()
325 BINVAL = "abc" + '\0' + "def"
327 BINVAL = BINVAL.encode()
328 xattr.set(fname, self.USER_ATTR, BINVAL)
329 self.failUnlessEqual(xattr.list(fname), [self.USER_ATTR])
330 self.failUnlessEqual(xattr.list(fname, namespace=NS_USER),
332 self.failUnlessEqual(xattr.get(fname, self.USER_ATTR), BINVAL)
333 self.failUnlessEqual(xattr.get(fname, self.USER_NN,
334 namespace=NS_USER), BINVAL)
335 self.failUnlessEqual(xattr.get_all(fname), [(self.USER_ATTR, BINVAL)])
336 self.failUnlessEqual(xattr.get_all(fname, namespace=NS_USER),
337 [(self.USER_NN, BINVAL)])
338 xattr.remove(fname, self.USER_ATTR)
340 def testManyOpsDeprecated(self):
341 """test many ops (deprecated functions)"""
342 fh, fname = self._getfile()
343 xattr.setxattr(fh, self.USER_ATTR, self.USER_VAL)
344 VL = [self.USER_ATTR]
345 for i in range(self.MANYOPS_COUNT):
346 self.failUnlessEqual(xattr.listxattr(fh), VL)
347 for i in range(self.MANYOPS_COUNT):
348 self.failUnlessEqual(xattr.getxattr(fh, self.USER_ATTR),
350 for i in range(self.MANYOPS_COUNT):
351 self.failUnlessEqual(xattr.get_all(fh),
352 [(self.USER_ATTR, self.USER_VAL)])
354 def testManyOps(self):
356 fh, fname = self._getfile()
357 xattr.set(fh, self.USER_ATTR, self.USER_VAL)
358 VL = [self.USER_ATTR]
360 for i in range(self.MANYOPS_COUNT):
361 self.failUnlessEqual(xattr.list(fh), VL)
362 self.failUnlessEqual(xattr.list(fh, namespace=NS_USER), VN)
363 for i in range(self.MANYOPS_COUNT):
364 self.failUnlessEqual(xattr.get(fh, self.USER_ATTR),
366 self.failUnlessEqual(xattr.get(fh, self.USER_NN,
369 for i in range(self.MANYOPS_COUNT):
370 self.failUnlessEqual(xattr.get_all(fh),
371 [(self.USER_ATTR, self.USER_VAL)])
372 self.failUnlessEqual(xattr.get_all(fh, namespace=NS_USER),
373 [(self.USER_NN, self.USER_VAL)])
375 if __name__ == "__main__":