11 from xattr import NS_USER, XATTR_CREATE, XATTR_REPLACE
13 NAMESPACE = os.environ.get("NAMESPACE", NS_USER)
15 if sys.hexversion >= 0x03000000:
22 TEST_DIR = os.environ.get("TEST_DIR", ".")
23 TEST_IGNORE_XATTRS = os.environ.get("TEST_IGNORE_XATTRS", "")
24 if TEST_IGNORE_XATTRS == "":
25 TEST_IGNORE_XATTRS = []
27 TEST_IGNORE_XATTRS = TEST_IGNORE_XATTRS.split(",")
28 # The following has to be a list comprehension, not a generator, to
29 # avoid weird consequences of lazy evaluation.
30 TEST_IGNORE_XATTRS.extend([a.encode() for a in TEST_IGNORE_XATTRS])
33 USER_ATTR = NAMESPACE.decode() + "." + USER_NN
36 LARGE_VAL = "x" * 2048
40 USER_NN = USER_NN.encode()
41 USER_VAL = USER_VAL.encode()
42 USER_ATTR = USER_ATTR.encode()
43 EMPTY_VAL = EMPTY_VAL.encode()
44 LARGE_VAL = LARGE_VAL.encode()
48 def ignore_tuples(attrs):
49 """Remove ignored attributes from the output of xattr.get_all."""
50 return [attr for attr in attrs
51 if attr[0] not in TEST_IGNORE_XATTRS]
54 """Remove ignored attributes from the output of xattr.list"""
55 return [attr for attr in attrs
56 if attr not in TEST_IGNORE_XATTRS]
58 def lists_equal(attrs, value):
59 """Helper to check list equivalence, skipping TEST_IGNORE_XATTRS."""
60 assert ignore(attrs) == value
62 def tuples_equal(attrs, value):
63 """Helper to check list equivalence, skipping TEST_IGNORE_XATTRS."""
64 assert ignore_tuples(attrs) == value
66 # Fixtures and helpers
70 """per-test temp dir based in TEST_DIR"""
71 with tempfile.TemporaryDirectory(dir=TEST_DIR) as dname:
75 fh, fname = tempfile.mkstemp(".test", "xattr-", path)
78 def get_file_name(path):
79 fh, fname = get_file(path)
83 def get_file_fd(path):
84 return get_file(path)[0]
86 def get_file_object(path):
87 fd = get_file(path)[0]
91 return tempfile.mkdtemp(".test", "xattr-", path)
93 def get_symlink(path, dangling=True):
94 """create a symlink"""
95 fh, fname = get_file(path)
99 sname = fname + ".symlink"
100 os.symlink(fname, sname)
103 def get_valid_symlink(path):
104 return get_symlink(path, dangling=False)[1]
106 def get_dangling_symlink(path):
107 return get_symlink(path, dangling=True)[1]
109 # Note: user attributes are only allowed on files and directories, so
110 # we have to skip the symlinks here. See xattr(7).
112 (get_file_name, False),
113 (get_file_fd, False),
114 (get_file_object, False),
116 (get_valid_symlink, False),
127 ALL_ITEMS_P = ITEMS_P + [ (get_valid_symlink, True),
128 (get_dangling_symlink, True)]
129 ALL_ITEMS_D = ITEMS_D + ["valid symlink", "dangling symlink"]
131 @pytest.fixture(params=ITEMS_P, ids=ITEMS_D)
132 def subject(testdir, request):
133 return request.param[0](testdir), request.param[1]
135 @pytest.fixture(params=ALL_ITEMS_P, ids=ALL_ITEMS_D)
136 def any_subject(testdir, request):
137 return request.param[0](testdir), request.param[1]
139 @pytest.fixture(params=[True, False], ids=["with namespace", "no namespace"])
143 @pytest.fixture(params=[True, False], ids=["dangling", "valid"])
144 def use_dangling(request):
149 def test_empty_value(subject):
150 item, nofollow = subject
151 xattr.set(item, USER_ATTR, EMPTY_VAL, nofollow=nofollow)
152 assert xattr.get(item, USER_ATTR, nofollow=nofollow) == EMPTY_VAL
154 def test_large_value(subject):
155 item, nofollow = subject
156 xattr.set(item, USER_ATTR, LARGE_VAL)
157 assert xattr.get(item, USER_ATTR, nofollow=nofollow) == LARGE_VAL
160 def test_file_mixed_access_deprecated(testdir):
161 """test mixed access to file (deprecated functions)"""
162 fh, fname = get_file(testdir)
163 with os.fdopen(fh) as fo:
164 lists_equal(xattr.listxattr(fname), [])
165 xattr.setxattr(fname, USER_ATTR, USER_VAL)
166 lists_equal(xattr.listxattr(fh), [USER_ATTR])
167 assert xattr.getxattr(fo, USER_ATTR) == USER_VAL
168 tuples_equal(xattr.get_all(fo), [(USER_ATTR, USER_VAL)])
169 tuples_equal(xattr.get_all(fname),
170 [(USER_ATTR, USER_VAL)])
172 def test_file_mixed_access(testdir):
173 """test mixed access to file"""
174 fh, fname = get_file(testdir)
175 with os.fdopen(fh) as fo:
176 lists_equal(xattr.list(fname), [])
177 xattr.set(fname, USER_ATTR, USER_VAL)
178 lists_equal(xattr.list(fh), [USER_ATTR])
179 assert xattr.list(fh, namespace=NAMESPACE) == [USER_NN]
180 assert xattr.get(fo, USER_ATTR) == USER_VAL
181 assert xattr.get(fo, USER_NN, namespace=NAMESPACE) == USER_VAL
182 tuples_equal(xattr.get_all(fo),
183 [(USER_ATTR, USER_VAL)])
184 assert xattr.get_all(fo, namespace=NAMESPACE) == \
185 [(USER_NN, USER_VAL)]
186 tuples_equal(xattr.get_all(fname), [(USER_ATTR, USER_VAL)])
187 assert xattr.get_all(fname, namespace=NAMESPACE) == \
188 [(USER_NN, USER_VAL)]
190 def test_ListSetGet(subject, use_ns):
191 """check list, set, get operations against an item"""
193 lists_equal(xattr.list(item), [])
194 with pytest.raises(EnvironmentError):
196 xattr.set(item, USER_NN, USER_VAL, flags=XATTR_REPLACE,
199 xattr.set(item, USER_ATTR, USER_VAL, flags=XATTR_REPLACE)
201 xattr.set(item, USER_NN, USER_VAL,
204 xattr.set(item, USER_ATTR, USER_VAL)
205 with pytest.raises(EnvironmentError):
207 xattr.set(item, USER_NN, USER_VAL,
208 flags=XATTR_CREATE, namespace=NAMESPACE)
210 xattr.set(item, USER_ATTR, USER_VAL, flags=XATTR_CREATE)
212 assert xattr.list(item, namespace=NAMESPACE) == [USER_NN]
214 lists_equal(xattr.list(item), [USER_ATTR])
215 lists_equal(xattr.list(item, namespace=EMPTY_NS),
218 assert xattr.get(item, USER_NN, namespace=NAMESPACE) == USER_VAL
220 assert xattr.get(item, USER_ATTR) == USER_VAL
222 assert xattr.get_all(item, namespace=NAMESPACE) == \
223 [(USER_NN, USER_VAL)]
225 tuples_equal(xattr.get_all(item),
226 [(USER_ATTR, USER_VAL)])
228 xattr.remove(item, USER_NN, namespace=NAMESPACE)
230 xattr.remove(item, USER_ATTR)
231 lists_equal(xattr.list(item), [])
232 tuples_equal(xattr.get_all(item), [])
233 with pytest.raises(EnvironmentError):
235 xattr.remove(item, USER_NN, namespace=NAMESPACE)
237 xattr.remove(item, USER_ATTR)
239 def test_ListSetGetDeprecated(subject):
240 """check deprecated list, set, get operations against an item"""
242 lists_equal(xattr.listxattr(item), [])
243 with pytest.raises(EnvironmentError):
244 xattr.setxattr(item, USER_ATTR, USER_VAL, XATTR_REPLACE)
245 xattr.setxattr(item, USER_ATTR, USER_VAL, 0)
246 with pytest.raises(EnvironmentError):
247 xattr.setxattr(item, USER_ATTR, USER_VAL, XATTR_CREATE)
248 lists_equal(xattr.listxattr(item), [USER_ATTR])
249 assert xattr.getxattr(item, USER_ATTR) == USER_VAL
250 tuples_equal(xattr.get_all(item), [(USER_ATTR, USER_VAL)])
251 xattr.removexattr(item, USER_ATTR)
252 lists_equal(xattr.listxattr(item), [])
253 tuples_equal(xattr.get_all(item), [])
254 with pytest.raises(EnvironmentError):
255 xattr.removexattr(item, USER_ATTR)
257 def test_many_ops(subject):
260 xattr.set(item, USER_ATTR, USER_VAL)
263 for i in range(MANYOPS_COUNT):
264 lists_equal(xattr.list(item), VL)
265 lists_equal(xattr.list(item, namespace=EMPTY_NS), VL)
266 assert xattr.list(item, namespace=NAMESPACE) == VN
267 for i in range(MANYOPS_COUNT):
268 assert xattr.get(item, USER_ATTR) == USER_VAL
269 assert xattr.get(item, USER_NN, namespace=NAMESPACE) == USER_VAL
270 for i in range(MANYOPS_COUNT):
271 tuples_equal(xattr.get_all(item),
272 [(USER_ATTR, USER_VAL)])
273 assert xattr.get_all(item, namespace=NAMESPACE) == \
274 [(USER_NN, USER_VAL)]
276 def test_many_ops_deprecated(subject):
277 """test many ops (deprecated functions)"""
279 xattr.setxattr(item, USER_ATTR, USER_VAL)
281 for i in range(MANYOPS_COUNT):
282 lists_equal(xattr.listxattr(item), VL)
283 for i in range(MANYOPS_COUNT):
284 assert xattr.getxattr(item, USER_ATTR) == USER_VAL
285 for i in range(MANYOPS_COUNT):
286 tuples_equal(xattr.get_all(item),
287 [(USER_ATTR, USER_VAL)])
289 def test_no_attributes_deprecated(any_subject):
290 """test no attributes (deprecated functions)"""
291 item, nofollow = any_subject
292 lists_equal(xattr.listxattr(item, True), [])
293 tuples_equal(xattr.get_all(item, True), [])
294 with pytest.raises(EnvironmentError):
295 xattr.getxattr(item, USER_ATTR, True)
297 def test_no_attributes(any_subject):
298 """test no attributes"""
299 item, nofollow = any_subject
300 lists_equal(xattr.list(item, nofollow=nofollow), [])
301 assert xattr.list(item, nofollow=nofollow,
302 namespace=NAMESPACE) == []
303 tuples_equal(xattr.get_all(item, nofollow=nofollow), [])
304 assert xattr.get_all(item, nofollow=nofollow,
305 namespace=NAMESPACE) == []
306 with pytest.raises(EnvironmentError):
307 xattr.get(item, USER_NN, nofollow=nofollow,
310 def test_binary_payload_deprecated(subject):
311 """test binary values (deprecated functions)"""
314 xattr.setxattr(item, USER_ATTR, BINVAL)
315 lists_equal(xattr.listxattr(item), [USER_ATTR])
316 assert xattr.getxattr(item, USER_ATTR) == BINVAL
317 tuples_equal(xattr.get_all(item), [(USER_ATTR, BINVAL)])
318 xattr.removexattr(item, USER_ATTR)
320 def test_binary_payload(subject):
321 """test binary values"""
324 xattr.set(item, USER_ATTR, BINVAL)
325 lists_equal(xattr.list(item), [USER_ATTR])
326 assert xattr.list(item, namespace=NAMESPACE) == [USER_NN]
327 assert xattr.get(item, USER_ATTR) == BINVAL
328 assert xattr.get(item, USER_NN, namespace=NAMESPACE) == BINVAL
329 tuples_equal(xattr.get_all(item), [(USER_ATTR, BINVAL)])
330 assert xattr.get_all(item, namespace=NAMESPACE) == [(USER_NN, BINVAL)]
331 xattr.remove(item, USER_ATTR)
333 def test_symlinks_user_fail(testdir, use_dangling):
334 _, sname = get_symlink(testdir, dangling=use_dangling)
335 with pytest.raises(IOError):
336 xattr.set(sname, USER_ATTR, USER_VAL, nofollow=True)
337 with pytest.raises(IOError):
338 xattr.set(sname, USER_NN, USER_VAL, namespace=NAMESPACE,
340 with pytest.raises(IOError):
341 xattr.setxattr(sname, USER_ATTR, USER_VAL, XATTR_CREATE, True)
343 def test_none_namespace(subject):
344 with pytest.raises(TypeError):
345 xattr.get(subject[0], USER_ATTR, namespace=None)
347 @pytest.mark.parametrize(
349 [xattr.get, xattr.list, xattr.listxattr,
350 xattr.remove, xattr.removexattr,
351 xattr.set, xattr.setxattr,
352 xattr.get, xattr.getxattr])
353 def test_wrong_call(call):
354 with pytest.raises(TypeError):
357 @pytest.mark.parametrize(
358 "call, args", [(xattr.get, [USER_ATTR]),
359 (xattr.listxattr, []),
361 (xattr.remove, [USER_ATTR]),
362 (xattr.removexattr, [USER_ATTR]),
363 (xattr.get, [USER_ATTR]),
364 (xattr.getxattr, [USER_ATTR]),
365 (xattr.set, [USER_ATTR, USER_VAL]),
366 (xattr.setxattr, [USER_ATTR, USER_VAL])])
367 def test_wrong_argument_type(call, args):
368 with pytest.raises(TypeError):
369 call(object(), *args)