]> git.k1024.org Git - pyxattr.git/blob - test/test_xattr.py
Tests: small py3 cleanup
[pyxattr.git] / test / test_xattr.py
1 #
2 #
3
4 import sys
5 import tempfile
6 import os
7 import errno
8 import pytest
9
10 import xattr
11 from xattr import NS_USER, XATTR_CREATE, XATTR_REPLACE
12
13 NAMESPACE = os.environ.get("NAMESPACE", NS_USER)
14
15 EMPTY_NS = bytes()
16
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 = []
21 else:
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])
26
27 USER_NN = "test"
28 USER_ATTR = NAMESPACE.decode() + "." + USER_NN
29 USER_VAL = "abc"
30 EMPTY_VAL = ""
31 LARGE_VAL = "x" * 2048
32 MANYOPS_COUNT = 16384
33
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()
39
40 # Helper functions
41
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]
46
47 def ignore(attrs):
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]
51
52 def lists_equal(attrs, value):
53     """Helper to check list equivalence, skipping TEST_IGNORE_XATTRS."""
54     assert ignore(attrs) == value
55
56 def tuples_equal(attrs, value):
57     """Helper to check list equivalence, skipping TEST_IGNORE_XATTRS."""
58     assert ignore_tuples(attrs) == value
59
60 # Fixtures and helpers
61
62 @pytest.fixture
63 def testdir():
64     """per-test temp dir based in TEST_DIR"""
65     with tempfile.TemporaryDirectory(dir=TEST_DIR) as dname:
66         yield dname
67
68 def get_file(path):
69     fh, fname = tempfile.mkstemp(".test", "xattr-", path)
70     return fh, fname
71
72 def get_file_name(path):
73     fh, fname = get_file(path)
74     os.close(fh)
75     return fname
76
77 def get_file_fd(path):
78     return get_file(path)[0]
79
80 def get_file_object(path):
81     fd = get_file(path)[0]
82     return os.fdopen(fd)
83
84 def get_dir(path):
85     return tempfile.mkdtemp(".test", "xattr-", path)
86
87 def get_symlink(path, dangling=True):
88     """create a symlink"""
89     fh, fname = get_file(path)
90     os.close(fh)
91     if dangling:
92         os.unlink(fname)
93     sname = fname + ".symlink"
94     os.symlink(fname, sname)
95     return fname, sname
96
97 def get_valid_symlink(path):
98     return get_symlink(path, dangling=False)[1]
99
100 def get_dangling_symlink(path):
101     return get_symlink(path, dangling=True)[1]
102
103 # Note: user attributes are only allowed on files and directories, so
104 # we have to skip the symlinks here. See xattr(7).
105 ITEMS_P = [
106     (get_file_name, False),
107     (get_file_fd, False),
108     (get_file_object, False),
109     (get_dir, False),
110     (get_valid_symlink, False),
111 ]
112
113 ITEMS_D = [
114     "file name",
115     "file FD",
116     "file object",
117     "directory",
118     "file via symlink",
119 ]
120
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"]
124
125 @pytest.fixture(params=ITEMS_P, ids=ITEMS_D)
126 def subject(testdir, request):
127     return request.param[0](testdir), request.param[1]
128
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]
132
133 @pytest.fixture(params=[True, False], ids=["with namespace", "no namespace"])
134 def use_ns(request):
135     return request.param
136
137 @pytest.fixture(params=[True, False], ids=["dangling", "valid"])
138 def use_dangling(request):
139     return request.param
140
141 ### Test functions
142
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
147
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
152
153
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)])
165
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)]
183
184 def test_replace_on_missing(subject, use_ns):
185     item = subject[0]
186     lists_equal(xattr.list(item), [])
187     with pytest.raises(EnvironmentError):
188         if use_ns:
189             xattr.set(item, USER_NN, USER_VAL, flags=XATTR_REPLACE,
190                       namespace=NAMESPACE)
191         else:
192             xattr.set(item, USER_ATTR, USER_VAL, flags=XATTR_REPLACE)
193
194 def test_create_on_existing(subject, use_ns):
195     item = subject[0]
196     lists_equal(xattr.list(item), [])
197     if use_ns:
198         xattr.set(item, USER_NN, USER_VAL,
199                   namespace=NAMESPACE)
200     else:
201         xattr.set(item, USER_ATTR, USER_VAL)
202     with pytest.raises(EnvironmentError):
203         if use_ns:
204             xattr.set(item, USER_NN, USER_VAL,
205                       flags=XATTR_CREATE, namespace=NAMESPACE)
206         else:
207             xattr.set(item, USER_ATTR, USER_VAL, flags=XATTR_CREATE)
208
209 def test_remove_on_missing(subject, use_ns):
210     item = subject[0]
211     lists_equal(xattr.list(item), [])
212     with pytest.raises(EnvironmentError):
213         if use_ns:
214             xattr.remove(item, USER_NN, namespace=NAMESPACE)
215         else:
216             xattr.remove(item, USER_ATTR)
217
218 def test_set_get_remove(subject, use_ns):
219     item = subject[0]
220     lists_equal(xattr.list(item), [])
221     if use_ns:
222         xattr.set(item, USER_NN, USER_VAL,
223                   namespace=NAMESPACE)
224     else:
225         xattr.set(item, USER_ATTR, USER_VAL)
226     if use_ns:
227         assert xattr.list(item, namespace=NAMESPACE) == [USER_NN]
228     else:
229         lists_equal(xattr.list(item), [USER_ATTR])
230         lists_equal(xattr.list(item, namespace=EMPTY_NS),
231                     [USER_ATTR])
232     if use_ns:
233         assert xattr.get(item, USER_NN, namespace=NAMESPACE) == USER_VAL
234     else:
235         assert xattr.get(item, USER_ATTR) == USER_VAL
236     if use_ns:
237         assert xattr.get_all(item, namespace=NAMESPACE) == \
238             [(USER_NN, USER_VAL)]
239     else:
240         tuples_equal(xattr.get_all(item),
241                      [(USER_ATTR, USER_VAL)])
242     if use_ns:
243         xattr.remove(item, USER_NN, namespace=NAMESPACE)
244     else:
245         xattr.remove(item, USER_ATTR)
246     lists_equal(xattr.list(item), [])
247     tuples_equal(xattr.get_all(item), [])
248
249 def test_replace_on_missing_deprecated(subject):
250     item = subject[0]
251     lists_equal(xattr.listxattr(item), [])
252     with pytest.raises(EnvironmentError):
253         xattr.setxattr(item, USER_ATTR, USER_VAL, XATTR_REPLACE)
254
255 def test_create_on_existing_deprecated(subject):
256     item = subject[0]
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)
261
262 def test_remove_on_missing_deprecated(subject):
263     """check deprecated list, set, get operations against an item"""
264     item = subject[0]
265     lists_equal(xattr.listxattr(item), [])
266     with pytest.raises(EnvironmentError):
267         xattr.removexattr(item, USER_ATTR)
268
269 def test_set_get_remove_deprecated(subject):
270     """check deprecated list, set, get operations against an item"""
271     item = subject[0]
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), [])
280
281 def test_many_ops(subject):
282     """test many ops"""
283     item = subject[0]
284     xattr.set(item, USER_ATTR, USER_VAL)
285     VL = [USER_ATTR]
286     VN = [USER_NN]
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)]
299
300 def test_many_ops_deprecated(subject):
301     """test many ops (deprecated functions)"""
302     item = subject[0]
303     xattr.setxattr(item, USER_ATTR, USER_VAL)
304     VL = [USER_ATTR]
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)])
312
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)
320
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,
332                   namespace=NAMESPACE)
333
334 def test_binary_payload_deprecated(subject):
335     """test binary values (deprecated functions)"""
336     item = subject[0]
337     BINVAL = b"abc\0def"
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)
343
344 def test_binary_payload(subject):
345     """test binary values"""
346     item = subject[0]
347     BINVAL = b"abc\0def"
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)
356
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,
363                   nofollow=True)
364     with pytest.raises(IOError):
365         xattr.setxattr(sname, USER_ATTR, USER_VAL, XATTR_CREATE, True)
366
367 def test_none_namespace(subject):
368     with pytest.raises(TypeError):
369         xattr.get(subject[0], USER_ATTR, namespace=None)
370
371 @pytest.mark.parametrize(
372     "call",
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):
379         call()
380
381 @pytest.mark.parametrize(
382     "call, args", [(xattr.get, [USER_ATTR]),
383                    (xattr.listxattr, []),
384                    (xattr.list, []),
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)