11 from xattr import NS_USER, XATTR_CREATE, XATTR_REPLACE
13 if sys.hexversion >= 0x03000000:
20 TEST_DIR = os.environ.get("TESTDIR", ".")
23 class xattrTest(unittest.TestCase):
25 USER_ATTR = NS_USER.decode() + "." + USER_NN
27 MANYOPS_COUNT = 131072
30 USER_NN = USER_NN.encode()
31 USER_VAL = USER_VAL.encode()
32 USER_ATTR = USER_ATTR.encode()
40 """tear down function"""
41 for fname in self.rmfiles:
44 except EnvironmentError:
46 for dname in self.rmdirs:
49 except EnvironmentError:
53 """create a temp file"""
54 fh, fname = tempfile.mkstemp(".test", "xattr-", TEST_DIR)
55 self.rmfiles.append(fname)
59 """create a temp dir"""
60 dname = tempfile.mkdtemp(".test", "xattr-", TEST_DIR)
61 self.rmdirs.append(dname)
64 def _getsymlink(self, dangling=True):
65 """create a symlink"""
66 fh, fname = self._getfile()
70 sname = fname + ".symlink"
71 os.symlink(fname, sname)
72 self.rmfiles.append(sname)
75 def _checkDeprecated(self, item, symlink=False):
76 """check deprecated list, set, get operations against an item"""
77 self.assertEqual(xattr.listxattr(item, symlink), [])
78 self.assertRaises(EnvironmentError, xattr.setxattr, item,
79 self.USER_ATTR, self.USER_VAL,
82 xattr.setxattr(item, self.USER_ATTR, self.USER_VAL, 0, symlink)
84 err = sys.exc_info()[1]
85 if err.errno == errno.EPERM and symlink:
86 # symlinks may fail, in which case we abort the rest
87 # of the test for this case
90 self.assertRaises(EnvironmentError, xattr.setxattr, item,
91 self.USER_ATTR, self.USER_VAL, XATTR_CREATE)
92 self.assertEqual(xattr.listxattr(item, symlink), [self.USER_ATTR])
93 self.assertEqual(xattr.getxattr(item, self.USER_ATTR, symlink),
95 self.assertEqual(xattr.get_all(item, nofollow=symlink),
96 [(self.USER_ATTR, self.USER_VAL)])
97 xattr.removexattr(item, self.USER_ATTR)
98 self.assertEqual(xattr.listxattr(item, symlink), [])
99 self.assertEqual(xattr.get_all(item, nofollow=symlink), [])
100 self.assertRaises(EnvironmentError, xattr.removexattr,
101 item, self.USER_ATTR)
103 def _checkListSetGet(self, item, symlink=False, use_ns=False):
104 """check list, set, get operations against an item"""
105 self.assertEqual(xattr.list(item, symlink), [])
106 self.assertRaises(EnvironmentError, xattr.set, item,
107 self.USER_ATTR, self.USER_VAL, flags=XATTR_REPLACE)
108 self.assertRaises(EnvironmentError, xattr.set, item,
109 self.USER_NN, self.USER_VAL, flags=XATTR_REPLACE,
113 xattr.set(item, self.USER_NN, self.USER_VAL,
117 xattr.set(item, self.USER_ATTR, self.USER_VAL,
120 err = sys.exc_info()[1]
121 if err.errno == errno.EPERM and symlink:
122 # symlinks may fail, in which case we abort the rest
123 # of the test for this case
126 self.assertRaises(EnvironmentError, xattr.set, item,
127 self.USER_ATTR, self.USER_VAL, flags=XATTR_CREATE)
128 self.assertRaises(EnvironmentError, xattr.set, item,
129 self.USER_NN, self.USER_VAL,
130 flags=XATTR_CREATE, namespace=NS_USER)
131 self.assertEqual(xattr.list(item, nofollow=symlink),
133 self.assertEqual(xattr.list(item, nofollow=symlink,
136 self.assertEqual(xattr.list(item, namespace=NS_USER, nofollow=symlink),
138 self.assertEqual(xattr.get(item, self.USER_ATTR, nofollow=symlink),
140 self.assertEqual(xattr.get(item, self.USER_NN, nofollow=symlink,
141 namespace=NS_USER), self.USER_VAL)
142 self.assertEqual(xattr.get_all(item, nofollow=symlink),
143 [(self.USER_ATTR, self.USER_VAL)])
144 self.assertEqual(xattr.get_all(item, nofollow=symlink,
146 [(self.USER_NN, self.USER_VAL)])
148 xattr.remove(item, self.USER_NN, namespace=NS_USER)
150 xattr.remove(item, self.USER_ATTR)
151 self.assertEqual(xattr.list(item, symlink), [])
152 self.assertEqual(xattr.get_all(item, nofollow=symlink), [])
153 self.assertRaises(EnvironmentError, xattr.remove,
154 item, self.USER_ATTR, nofollow=symlink)
155 self.assertRaises(EnvironmentError, xattr.remove, item,
156 self.USER_NN, namespace=NS_USER, nofollow=symlink)
158 def testNoXattrDeprecated(self):
159 """test no attributes (deprecated functions)"""
160 fh, fname = self._getfile()
161 self.assertEqual(xattr.listxattr(fname), [])
162 self.assertEqual(xattr.get_all(fname), [])
163 dname = self._getdir()
164 self.assertEqual(xattr.listxattr(dname), [])
165 self.assertEqual(xattr.get_all(dname), [])
166 _, sname = self._getsymlink()
167 self.assertEqual(xattr.listxattr(sname, True), [])
168 self.assertEqual(xattr.get_all(sname, nofollow=True), [])
170 def testNoXattr(self):
171 """test no attributes"""
172 fh, fname = self._getfile()
173 self.assertEqual(xattr.list(fname), [])
174 self.assertEqual(xattr.list(fname, namespace=NS_USER), [])
175 self.assertEqual(xattr.get_all(fname), [])
176 self.assertEqual(xattr.get_all(fname, namespace=NS_USER), [])
177 dname = self._getdir()
178 self.assertEqual(xattr.list(dname), [])
179 self.assertEqual(xattr.list(dname, namespace=NS_USER), [])
180 self.assertEqual(xattr.get_all(dname), [])
181 self.assertEqual(xattr.get_all(dname, namespace=NS_USER), [])
182 _, sname = self._getsymlink()
183 self.assertEqual(xattr.list(sname, nofollow=True), [])
184 self.assertEqual(xattr.list(sname, nofollow=True,
185 namespace=NS_USER), [])
186 self.assertEqual(xattr.get_all(sname, nofollow=True), [])
187 self.assertEqual(xattr.get_all(sname, nofollow=True,
188 namespace=NS_USER), [])
190 def testFileByNameDeprecated(self):
191 """test set and retrieve one attribute by file name (deprecated)"""
192 fh, fname = self._getfile()
193 self._checkDeprecated(fname)
196 def testFileByName(self):
197 """test set and retrieve one attribute by file name"""
198 fh, fname = self._getfile()
199 self._checkListSetGet(fname)
200 self._checkListSetGet(fname, use_ns=True)
203 def testFileByDescriptorDeprecated(self):
204 """test file descriptor operations (deprecated functions)"""
205 fh, fname = self._getfile()
206 self._checkDeprecated(fh)
209 def testFileByDescriptor(self):
210 """test file descriptor operations"""
211 fh, fname = self._getfile()
212 self._checkListSetGet(fh)
213 self._checkListSetGet(fh, use_ns=True)
216 def testFileByObjectDeprecated(self):
217 """test file descriptor operations (deprecated functions)"""
218 fh, fname = self._getfile()
220 self._checkDeprecated(fo)
223 def testFileByObject(self):
224 """test file descriptor operations"""
225 fh, fname = self._getfile()
227 self._checkListSetGet(fo)
228 self._checkListSetGet(fo, use_ns=True)
231 def testMixedAccessDeprecated(self):
232 """test mixed access to file (deprecated functions)"""
233 fh, fname = self._getfile()
235 self.assertEqual(xattr.listxattr(fname), [])
236 xattr.setxattr(fname, self.USER_ATTR, self.USER_VAL)
237 self.assertEqual(xattr.listxattr(fh), [self.USER_ATTR])
238 self.assertEqual(xattr.getxattr(fo, self.USER_ATTR), self.USER_VAL)
239 self.assertEqual(xattr.get_all(fo), [(self.USER_ATTR, self.USER_VAL)])
240 self.assertEqual(xattr.get_all(fname),
241 [(self.USER_ATTR, self.USER_VAL)])
244 def testMixedAccess(self):
245 """test mixed access to file"""
246 fh, fname = self._getfile()
248 self.assertEqual(xattr.list(fname), [])
249 xattr.set(fname, self.USER_ATTR, self.USER_VAL)
250 self.assertEqual(xattr.list(fh), [self.USER_ATTR])
251 self.assertEqual(xattr.list(fh, namespace=NS_USER), [self.USER_NN])
252 self.assertEqual(xattr.get(fo, self.USER_ATTR), self.USER_VAL)
253 self.assertEqual(xattr.get(fo, self.USER_NN, namespace=NS_USER),
255 self.assertEqual(xattr.get_all(fo), [(self.USER_ATTR, self.USER_VAL)])
256 self.assertEqual(xattr.get_all(fo, namespace=NS_USER),
257 [(self.USER_NN, self.USER_VAL)])
258 self.assertEqual(xattr.get_all(fname),
259 [(self.USER_ATTR, self.USER_VAL)])
260 self.assertEqual(xattr.get_all(fname, namespace=NS_USER),
261 [(self.USER_NN, self.USER_VAL)])
264 def testDirOpsDeprecated(self):
265 """test attribute setting on directories (deprecated functions)"""
266 dname = self._getdir()
267 self._checkDeprecated(dname)
269 def testDirOps(self):
270 """test attribute setting on directories"""
271 dname = self._getdir()
272 self._checkListSetGet(dname)
273 self._checkListSetGet(dname, use_ns=True)
275 def testSymlinkOpsDeprecated(self):
276 """test symlink operations (deprecated functions)"""
277 _, sname = self._getsymlink()
278 self.assertRaises(EnvironmentError, xattr.listxattr, sname)
279 self._checkDeprecated(sname, symlink=True)
280 target, sname = self._getsymlink(dangling=False)
281 xattr.setxattr(target, self.USER_ATTR, self.USER_VAL)
282 self.assertEqual(xattr.listxattr(target), [self.USER_ATTR])
283 self.assertEqual(xattr.listxattr(sname, True), [])
284 self.assertRaises(EnvironmentError, xattr.removexattr, sname,
285 self.USER_ATTR, True)
286 xattr.removexattr(sname, self.USER_ATTR, False)
288 def testSymlinkOps(self):
289 """test symlink operations"""
290 _, sname = self._getsymlink()
291 self.assertRaises(EnvironmentError, xattr.list, sname)
292 self._checkListSetGet(sname, symlink=True)
293 self._checkListSetGet(sname, symlink=True, use_ns=True)
294 target, sname = self._getsymlink(dangling=False)
295 xattr.set(target, self.USER_ATTR, self.USER_VAL)
296 self.assertEqual(xattr.list(target), [self.USER_ATTR])
297 self.assertEqual(xattr.list(sname, nofollow=True), [])
298 self.assertRaises(EnvironmentError, xattr.remove, sname,
299 self.USER_ATTR, nofollow=True)
300 xattr.remove(sname, self.USER_ATTR, nofollow=False)
302 def testBinaryPayloadDeprecated(self):
303 """test binary values (deprecated functions)"""
304 fh, fname = self._getfile()
306 BINVAL = "abc" + '\0' + "def"
308 BINVAL = BINVAL.encode()
309 xattr.setxattr(fname, self.USER_ATTR, BINVAL)
310 self.assertEqual(xattr.listxattr(fname), [self.USER_ATTR])
311 self.assertEqual(xattr.getxattr(fname, self.USER_ATTR), BINVAL)
312 self.assertEqual(xattr.get_all(fname), [(self.USER_ATTR, BINVAL)])
313 xattr.removexattr(fname, self.USER_ATTR)
315 def testBinaryPayload(self):
316 """test binary values"""
317 fh, fname = self._getfile()
319 BINVAL = "abc" + '\0' + "def"
321 BINVAL = BINVAL.encode()
322 xattr.set(fname, self.USER_ATTR, BINVAL)
323 self.assertEqual(xattr.list(fname), [self.USER_ATTR])
324 self.assertEqual(xattr.list(fname, namespace=NS_USER), [self.USER_NN])
325 self.assertEqual(xattr.get(fname, self.USER_ATTR), BINVAL)
326 self.assertEqual(xattr.get(fname, self.USER_NN,
327 namespace=NS_USER), BINVAL)
328 self.assertEqual(xattr.get_all(fname), [(self.USER_ATTR, BINVAL)])
329 self.assertEqual(xattr.get_all(fname, namespace=NS_USER),
330 [(self.USER_NN, BINVAL)])
331 xattr.remove(fname, self.USER_ATTR)
333 def testManyOpsDeprecated(self):
334 """test many ops (deprecated functions)"""
335 fh, fname = self._getfile()
336 xattr.setxattr(fh, self.USER_ATTR, self.USER_VAL)
337 VL = [self.USER_ATTR]
338 for i in range(self.MANYOPS_COUNT):
339 self.assertEqual(xattr.listxattr(fh), VL)
340 for i in range(self.MANYOPS_COUNT):
341 self.assertEqual(xattr.getxattr(fh, self.USER_ATTR), self.USER_VAL)
342 for i in range(self.MANYOPS_COUNT):
343 self.assertEqual(xattr.get_all(fh),
344 [(self.USER_ATTR, self.USER_VAL)])
346 def testManyOps(self):
348 fh, fname = self._getfile()
349 xattr.set(fh, self.USER_ATTR, self.USER_VAL)
350 VL = [self.USER_ATTR]
352 for i in range(self.MANYOPS_COUNT):
353 self.assertEqual(xattr.list(fh), VL)
354 self.assertEqual(xattr.list(fh, namespace=EMPTY_NS), VL)
355 self.assertEqual(xattr.list(fh, namespace=NS_USER), VN)
356 for i in range(self.MANYOPS_COUNT):
357 self.assertEqual(xattr.get(fh, self.USER_ATTR), self.USER_VAL)
358 self.assertEqual(xattr.get(fh, self.USER_NN, namespace=NS_USER),
360 for i in range(self.MANYOPS_COUNT):
361 self.assertEqual(xattr.get_all(fh),
362 [(self.USER_ATTR, self.USER_VAL)])
363 self.assertEqual(xattr.get_all(fh, namespace=NS_USER),
364 [(self.USER_NN, self.USER_VAL)])
366 def testNoneNamespace(self):
367 fh, fname = self._getfile()
368 self.assertRaises(TypeError, xattr.get, fh, self.USER_ATTR,
372 if __name__ == "__main__":