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)
240 def test_replace_on_missing_deprecated(subject):
242 lists_equal(xattr.listxattr(item), [])
243 with pytest.raises(EnvironmentError):
244 xattr.setxattr(item, USER_ATTR, USER_VAL, XATTR_REPLACE)
246 def test_create_on_existing_deprecated(subject):
248 lists_equal(xattr.listxattr(item), [])
249 xattr.setxattr(item, USER_ATTR, USER_VAL, 0)
250 with pytest.raises(EnvironmentError):
251 xattr.setxattr(item, USER_ATTR, USER_VAL, XATTR_CREATE)
253 def test_remove_on_missing_deprecated(subject):
254 """check deprecated list, set, get operations against an item"""
256 lists_equal(xattr.listxattr(item), [])
257 with pytest.raises(EnvironmentError):
258 xattr.removexattr(item, USER_ATTR)
260 def test_set_get_remove_deprecated(subject):
261 """check deprecated list, set, get operations against an item"""
263 lists_equal(xattr.listxattr(item), [])
264 xattr.setxattr(item, USER_ATTR, USER_VAL, 0)
265 lists_equal(xattr.listxattr(item), [USER_ATTR])
266 assert xattr.getxattr(item, USER_ATTR) == USER_VAL
267 tuples_equal(xattr.get_all(item), [(USER_ATTR, USER_VAL)])
268 xattr.removexattr(item, USER_ATTR)
269 lists_equal(xattr.listxattr(item), [])
270 tuples_equal(xattr.get_all(item), [])
272 def test_many_ops(subject):
275 xattr.set(item, USER_ATTR, USER_VAL)
278 for i in range(MANYOPS_COUNT):
279 lists_equal(xattr.list(item), VL)
280 lists_equal(xattr.list(item, namespace=EMPTY_NS), VL)
281 assert xattr.list(item, namespace=NAMESPACE) == VN
282 for i in range(MANYOPS_COUNT):
283 assert xattr.get(item, USER_ATTR) == USER_VAL
284 assert xattr.get(item, USER_NN, namespace=NAMESPACE) == USER_VAL
285 for i in range(MANYOPS_COUNT):
286 tuples_equal(xattr.get_all(item),
287 [(USER_ATTR, USER_VAL)])
288 assert xattr.get_all(item, namespace=NAMESPACE) == \
289 [(USER_NN, USER_VAL)]
291 def test_many_ops_deprecated(subject):
292 """test many ops (deprecated functions)"""
294 xattr.setxattr(item, USER_ATTR, USER_VAL)
296 for i in range(MANYOPS_COUNT):
297 lists_equal(xattr.listxattr(item), VL)
298 for i in range(MANYOPS_COUNT):
299 assert xattr.getxattr(item, USER_ATTR) == USER_VAL
300 for i in range(MANYOPS_COUNT):
301 tuples_equal(xattr.get_all(item),
302 [(USER_ATTR, USER_VAL)])
304 def test_no_attributes_deprecated(any_subject):
305 """test no attributes (deprecated functions)"""
306 item, nofollow = any_subject
307 lists_equal(xattr.listxattr(item, True), [])
308 tuples_equal(xattr.get_all(item, True), [])
309 with pytest.raises(EnvironmentError):
310 xattr.getxattr(item, USER_ATTR, True)
312 def test_no_attributes(any_subject):
313 """test no attributes"""
314 item, nofollow = any_subject
315 lists_equal(xattr.list(item, nofollow=nofollow), [])
316 assert xattr.list(item, nofollow=nofollow,
317 namespace=NAMESPACE) == []
318 tuples_equal(xattr.get_all(item, nofollow=nofollow), [])
319 assert xattr.get_all(item, nofollow=nofollow,
320 namespace=NAMESPACE) == []
321 with pytest.raises(EnvironmentError):
322 xattr.get(item, USER_NN, nofollow=nofollow,
325 def test_binary_payload_deprecated(subject):
326 """test binary values (deprecated functions)"""
329 xattr.setxattr(item, USER_ATTR, BINVAL)
330 lists_equal(xattr.listxattr(item), [USER_ATTR])
331 assert xattr.getxattr(item, USER_ATTR) == BINVAL
332 tuples_equal(xattr.get_all(item), [(USER_ATTR, BINVAL)])
333 xattr.removexattr(item, USER_ATTR)
335 def test_binary_payload(subject):
336 """test binary values"""
339 xattr.set(item, USER_ATTR, BINVAL)
340 lists_equal(xattr.list(item), [USER_ATTR])
341 assert xattr.list(item, namespace=NAMESPACE) == [USER_NN]
342 assert xattr.get(item, USER_ATTR) == BINVAL
343 assert xattr.get(item, USER_NN, namespace=NAMESPACE) == BINVAL
344 tuples_equal(xattr.get_all(item), [(USER_ATTR, BINVAL)])
345 assert xattr.get_all(item, namespace=NAMESPACE) == [(USER_NN, BINVAL)]
346 xattr.remove(item, USER_ATTR)
348 def test_symlinks_user_fail(testdir, use_dangling):
349 _, sname = get_symlink(testdir, dangling=use_dangling)
350 with pytest.raises(IOError):
351 xattr.set(sname, USER_ATTR, USER_VAL, nofollow=True)
352 with pytest.raises(IOError):
353 xattr.set(sname, USER_NN, USER_VAL, namespace=NAMESPACE,
355 with pytest.raises(IOError):
356 xattr.setxattr(sname, USER_ATTR, USER_VAL, XATTR_CREATE, True)
358 def test_none_namespace(subject):
359 with pytest.raises(TypeError):
360 xattr.get(subject[0], USER_ATTR, namespace=None)
362 @pytest.mark.parametrize(
364 [xattr.get, xattr.list, xattr.listxattr,
365 xattr.remove, xattr.removexattr,
366 xattr.set, xattr.setxattr,
367 xattr.get, xattr.getxattr])
368 def test_wrong_call(call):
369 with pytest.raises(TypeError):
372 @pytest.mark.parametrize(
373 "call, args", [(xattr.get, [USER_ATTR]),
374 (xattr.listxattr, []),
376 (xattr.remove, [USER_ATTR]),
377 (xattr.removexattr, [USER_ATTR]),
378 (xattr.get, [USER_ATTR]),
379 (xattr.getxattr, [USER_ATTR]),
380 (xattr.set, [USER_ATTR, USER_VAL]),
381 (xattr.setxattr, [USER_ATTR, USER_VAL])])
382 def test_wrong_argument_type(call, args):
383 with pytest.raises(TypeError):
384 call(object(), *args)