11 from xattr import NS_USER, XATTR_CREATE, XATTR_REPLACE
13 NAMESPACE = os.environ.get("NAMESPACE", NS_USER)
17 TEST_DIR = os.environ.get("TEST_DIR", ".")
18 TEST_IGNORE_XATTRS = os.environ.get("TEST_IGNORE_XATTRS", "")
19 if TEST_IGNORE_XATTRS == "":
20 TEST_IGNORE_XATTRS = []
22 TEST_IGNORE_XATTRS = TEST_IGNORE_XATTRS.split(",")
23 # The following has to be a list comprehension, not a generator, to
24 # avoid weird consequences of lazy evaluation.
25 TEST_IGNORE_XATTRS.extend([a.encode() for a in TEST_IGNORE_XATTRS])
28 USER_ATTR = NAMESPACE.decode() + "." + USER_NN
31 LARGE_VAL = "x" * 2048
34 USER_NN = USER_NN.encode()
35 USER_VAL = USER_VAL.encode()
36 USER_ATTR = USER_ATTR.encode()
37 EMPTY_VAL = EMPTY_VAL.encode()
38 LARGE_VAL = LARGE_VAL.encode()
42 def ignore_tuples(attrs):
43 """Remove ignored attributes from the output of xattr.get_all."""
44 return [attr for attr in attrs
45 if attr[0] not in TEST_IGNORE_XATTRS]
48 """Remove ignored attributes from the output of xattr.list"""
49 return [attr for attr in attrs
50 if attr not in TEST_IGNORE_XATTRS]
52 def lists_equal(attrs, value):
53 """Helper to check list equivalence, skipping TEST_IGNORE_XATTRS."""
54 assert ignore(attrs) == value
56 def tuples_equal(attrs, value):
57 """Helper to check list equivalence, skipping TEST_IGNORE_XATTRS."""
58 assert ignore_tuples(attrs) == value
60 # Fixtures and helpers
64 """per-test temp dir based in TEST_DIR"""
65 with tempfile.TemporaryDirectory(dir=TEST_DIR) as dname:
69 fh, fname = tempfile.mkstemp(".test", "xattr-", path)
72 def get_file_name(path):
73 fh, fname = get_file(path)
77 def get_file_fd(path):
78 return get_file(path)[0]
80 def get_file_object(path):
81 fd = get_file(path)[0]
85 return tempfile.mkdtemp(".test", "xattr-", path)
87 def get_symlink(path, dangling=True):
88 """create a symlink"""
89 fh, fname = get_file(path)
93 sname = fname + ".symlink"
94 os.symlink(fname, sname)
97 def get_valid_symlink(path):
98 return get_symlink(path, dangling=False)[1]
100 def get_dangling_symlink(path):
101 return get_symlink(path, dangling=True)[1]
103 # Note: user attributes are only allowed on files and directories, so
104 # we have to skip the symlinks here. See xattr(7).
106 (get_file_name, False),
107 (get_file_fd, False),
108 (get_file_object, False),
110 (get_valid_symlink, False),
121 ALL_ITEMS_P = ITEMS_P + [ (get_valid_symlink, True),
122 (get_dangling_symlink, True)]
123 ALL_ITEMS_D = ITEMS_D + ["valid symlink", "dangling symlink"]
125 @pytest.fixture(params=ITEMS_P, ids=ITEMS_D)
126 def subject(testdir, request):
127 return request.param[0](testdir), request.param[1]
129 @pytest.fixture(params=ALL_ITEMS_P, ids=ALL_ITEMS_D)
130 def any_subject(testdir, request):
131 return request.param[0](testdir), request.param[1]
133 @pytest.fixture(params=[True, False], ids=["with namespace", "no namespace"])
137 @pytest.fixture(params=[True, False], ids=["dangling", "valid"])
138 def use_dangling(request):
143 def test_empty_value(subject):
144 item, nofollow = subject
145 xattr.set(item, USER_ATTR, EMPTY_VAL, nofollow=nofollow)
146 assert xattr.get(item, USER_ATTR, nofollow=nofollow) == EMPTY_VAL
148 def test_large_value(subject):
149 item, nofollow = subject
150 xattr.set(item, USER_ATTR, LARGE_VAL)
151 assert xattr.get(item, USER_ATTR, nofollow=nofollow) == LARGE_VAL
154 def test_file_mixed_access_deprecated(testdir):
155 """test mixed access to file (deprecated functions)"""
156 fh, fname = get_file(testdir)
157 with os.fdopen(fh) as fo:
158 lists_equal(xattr.listxattr(fname), [])
159 xattr.setxattr(fname, USER_ATTR, USER_VAL)
160 lists_equal(xattr.listxattr(fh), [USER_ATTR])
161 assert xattr.getxattr(fo, USER_ATTR) == USER_VAL
162 tuples_equal(xattr.get_all(fo), [(USER_ATTR, USER_VAL)])
163 tuples_equal(xattr.get_all(fname),
164 [(USER_ATTR, USER_VAL)])
166 def test_file_mixed_access(testdir):
167 """test mixed access to file"""
168 fh, fname = get_file(testdir)
169 with os.fdopen(fh) as fo:
170 lists_equal(xattr.list(fname), [])
171 xattr.set(fname, USER_ATTR, USER_VAL)
172 lists_equal(xattr.list(fh), [USER_ATTR])
173 assert xattr.list(fh, namespace=NAMESPACE) == [USER_NN]
174 assert xattr.get(fo, USER_ATTR) == USER_VAL
175 assert xattr.get(fo, USER_NN, namespace=NAMESPACE) == USER_VAL
176 tuples_equal(xattr.get_all(fo),
177 [(USER_ATTR, USER_VAL)])
178 assert xattr.get_all(fo, namespace=NAMESPACE) == \
179 [(USER_NN, USER_VAL)]
180 tuples_equal(xattr.get_all(fname), [(USER_ATTR, USER_VAL)])
181 assert xattr.get_all(fname, namespace=NAMESPACE) == \
182 [(USER_NN, USER_VAL)]
184 def test_replace_on_missing(subject, use_ns):
186 lists_equal(xattr.list(item), [])
187 with pytest.raises(EnvironmentError):
189 xattr.set(item, USER_NN, USER_VAL, flags=XATTR_REPLACE,
192 xattr.set(item, USER_ATTR, USER_VAL, flags=XATTR_REPLACE)
194 def test_create_on_existing(subject, use_ns):
196 lists_equal(xattr.list(item), [])
198 xattr.set(item, USER_NN, USER_VAL,
201 xattr.set(item, USER_ATTR, USER_VAL)
202 with pytest.raises(EnvironmentError):
204 xattr.set(item, USER_NN, USER_VAL,
205 flags=XATTR_CREATE, namespace=NAMESPACE)
207 xattr.set(item, USER_ATTR, USER_VAL, flags=XATTR_CREATE)
209 def test_remove_on_missing(subject, use_ns):
211 lists_equal(xattr.list(item), [])
212 with pytest.raises(EnvironmentError):
214 xattr.remove(item, USER_NN, namespace=NAMESPACE)
216 xattr.remove(item, USER_ATTR)
218 def test_set_get_remove(subject, use_ns):
220 lists_equal(xattr.list(item), [])
222 xattr.set(item, USER_NN, USER_VAL,
225 xattr.set(item, USER_ATTR, USER_VAL)
227 assert xattr.list(item, namespace=NAMESPACE) == [USER_NN]
229 lists_equal(xattr.list(item), [USER_ATTR])
230 lists_equal(xattr.list(item, namespace=EMPTY_NS),
233 assert xattr.get(item, USER_NN, namespace=NAMESPACE) == USER_VAL
235 assert xattr.get(item, USER_ATTR) == USER_VAL
237 assert xattr.get_all(item, namespace=NAMESPACE) == \
238 [(USER_NN, USER_VAL)]
240 tuples_equal(xattr.get_all(item),
241 [(USER_ATTR, USER_VAL)])
243 xattr.remove(item, USER_NN, namespace=NAMESPACE)
245 xattr.remove(item, USER_ATTR)
246 lists_equal(xattr.list(item), [])
247 tuples_equal(xattr.get_all(item), [])
249 def test_replace_on_missing_deprecated(subject):
251 lists_equal(xattr.listxattr(item), [])
252 with pytest.raises(EnvironmentError):
253 xattr.setxattr(item, USER_ATTR, USER_VAL, XATTR_REPLACE)
255 def test_create_on_existing_deprecated(subject):
257 lists_equal(xattr.listxattr(item), [])
258 xattr.setxattr(item, USER_ATTR, USER_VAL, 0)
259 with pytest.raises(EnvironmentError):
260 xattr.setxattr(item, USER_ATTR, USER_VAL, XATTR_CREATE)
262 def test_remove_on_missing_deprecated(subject):
263 """check deprecated list, set, get operations against an item"""
265 lists_equal(xattr.listxattr(item), [])
266 with pytest.raises(EnvironmentError):
267 xattr.removexattr(item, USER_ATTR)
269 def test_set_get_remove_deprecated(subject):
270 """check deprecated list, set, get operations against an item"""
272 lists_equal(xattr.listxattr(item), [])
273 xattr.setxattr(item, USER_ATTR, USER_VAL, 0)
274 lists_equal(xattr.listxattr(item), [USER_ATTR])
275 assert xattr.getxattr(item, USER_ATTR) == USER_VAL
276 tuples_equal(xattr.get_all(item), [(USER_ATTR, USER_VAL)])
277 xattr.removexattr(item, USER_ATTR)
278 lists_equal(xattr.listxattr(item), [])
279 tuples_equal(xattr.get_all(item), [])
281 def test_many_ops(subject):
284 xattr.set(item, USER_ATTR, USER_VAL)
287 for i in range(MANYOPS_COUNT):
288 lists_equal(xattr.list(item), VL)
289 lists_equal(xattr.list(item, namespace=EMPTY_NS), VL)
290 assert xattr.list(item, namespace=NAMESPACE) == VN
291 for i in range(MANYOPS_COUNT):
292 assert xattr.get(item, USER_ATTR) == USER_VAL
293 assert xattr.get(item, USER_NN, namespace=NAMESPACE) == USER_VAL
294 for i in range(MANYOPS_COUNT):
295 tuples_equal(xattr.get_all(item),
296 [(USER_ATTR, USER_VAL)])
297 assert xattr.get_all(item, namespace=NAMESPACE) == \
298 [(USER_NN, USER_VAL)]
300 def test_many_ops_deprecated(subject):
301 """test many ops (deprecated functions)"""
303 xattr.setxattr(item, USER_ATTR, USER_VAL)
305 for i in range(MANYOPS_COUNT):
306 lists_equal(xattr.listxattr(item), VL)
307 for i in range(MANYOPS_COUNT):
308 assert xattr.getxattr(item, USER_ATTR) == USER_VAL
309 for i in range(MANYOPS_COUNT):
310 tuples_equal(xattr.get_all(item),
311 [(USER_ATTR, USER_VAL)])
313 def test_no_attributes_deprecated(any_subject):
314 """test no attributes (deprecated functions)"""
315 item, nofollow = any_subject
316 lists_equal(xattr.listxattr(item, True), [])
317 tuples_equal(xattr.get_all(item, True), [])
318 with pytest.raises(EnvironmentError):
319 xattr.getxattr(item, USER_ATTR, True)
321 def test_no_attributes(any_subject):
322 """test no attributes"""
323 item, nofollow = any_subject
324 lists_equal(xattr.list(item, nofollow=nofollow), [])
325 assert xattr.list(item, nofollow=nofollow,
326 namespace=NAMESPACE) == []
327 tuples_equal(xattr.get_all(item, nofollow=nofollow), [])
328 assert xattr.get_all(item, nofollow=nofollow,
329 namespace=NAMESPACE) == []
330 with pytest.raises(EnvironmentError):
331 xattr.get(item, USER_NN, nofollow=nofollow,
334 def test_binary_payload_deprecated(subject):
335 """test binary values (deprecated functions)"""
338 xattr.setxattr(item, USER_ATTR, BINVAL)
339 lists_equal(xattr.listxattr(item), [USER_ATTR])
340 assert xattr.getxattr(item, USER_ATTR) == BINVAL
341 tuples_equal(xattr.get_all(item), [(USER_ATTR, BINVAL)])
342 xattr.removexattr(item, USER_ATTR)
344 def test_binary_payload(subject):
345 """test binary values"""
348 xattr.set(item, USER_ATTR, BINVAL)
349 lists_equal(xattr.list(item), [USER_ATTR])
350 assert xattr.list(item, namespace=NAMESPACE) == [USER_NN]
351 assert xattr.get(item, USER_ATTR) == BINVAL
352 assert xattr.get(item, USER_NN, namespace=NAMESPACE) == BINVAL
353 tuples_equal(xattr.get_all(item), [(USER_ATTR, BINVAL)])
354 assert xattr.get_all(item, namespace=NAMESPACE) == [(USER_NN, BINVAL)]
355 xattr.remove(item, USER_ATTR)
357 def test_symlinks_user_fail(testdir, use_dangling):
358 _, sname = get_symlink(testdir, dangling=use_dangling)
359 with pytest.raises(IOError):
360 xattr.set(sname, USER_ATTR, USER_VAL, nofollow=True)
361 with pytest.raises(IOError):
362 xattr.set(sname, USER_NN, USER_VAL, namespace=NAMESPACE,
364 with pytest.raises(IOError):
365 xattr.setxattr(sname, USER_ATTR, USER_VAL, XATTR_CREATE, True)
367 def test_none_namespace(subject):
368 with pytest.raises(TypeError):
369 xattr.get(subject[0], USER_ATTR, namespace=None)
371 @pytest.mark.parametrize(
373 [xattr.get, xattr.list, xattr.listxattr,
374 xattr.remove, xattr.removexattr,
375 xattr.set, xattr.setxattr,
376 xattr.get, xattr.getxattr])
377 def test_wrong_call(call):
378 with pytest.raises(TypeError):
381 @pytest.mark.parametrize(
382 "call, args", [(xattr.get, [USER_ATTR]),
383 (xattr.listxattr, []),
385 (xattr.remove, [USER_ATTR]),
386 (xattr.removexattr, [USER_ATTR]),
387 (xattr.get, [USER_ATTR]),
388 (xattr.getxattr, [USER_ATTR]),
389 (xattr.set, [USER_ATTR, USER_VAL]),
390 (xattr.setxattr, [USER_ATTR, USER_VAL])])
391 def test_wrong_argument_type(call, args):
392 with pytest.raises(TypeError):
393 call(object(), *args)