]> git.k1024.org Git - pyxattr.git/blob - test/test_xattr.py
Introduce an any_subject fixture which includes symlinks
[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 if sys.hexversion >= 0x03000000:
16     PY3K = True
17     EMPTY_NS = bytes()
18 else:
19     PY3K = False
20     EMPTY_NS = ''
21
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 = []
26 else:
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])
31
32 USER_NN = "test"
33 USER_ATTR = NAMESPACE.decode() + "." + USER_NN
34 USER_VAL = "abc"
35 EMPTY_VAL = ""
36 LARGE_VAL = "x" * 2048
37 MANYOPS_COUNT = 16384
38
39 if PY3K:
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()
45
46 # Helper functions
47
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]
52
53 def ignore(attrs):
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]
57
58 def lists_equal(attrs, value):
59     """Helper to check list equivalence, skipping TEST_IGNORE_XATTRS."""
60     assert ignore(attrs) == value
61
62 def tuples_equal(attrs, value):
63     """Helper to check list equivalence, skipping TEST_IGNORE_XATTRS."""
64     assert ignore_tuples(attrs) == value
65
66 # Fixtures and helpers
67
68 @pytest.fixture
69 def testdir():
70     """per-test temp dir based in TEST_DIR"""
71     with tempfile.TemporaryDirectory(dir=TEST_DIR) as dname:
72         yield dname
73
74 def get_file(path):
75     fh, fname = tempfile.mkstemp(".test", "xattr-", path)
76     return fh, fname
77
78 def get_file_name(path):
79     fh, fname = get_file(path)
80     os.close(fh)
81     return fname
82
83 def get_file_fd(path):
84     return get_file(path)[0]
85
86 def get_file_object(path):
87     fd = get_file(path)[0]
88     return os.fdopen(fd)
89
90 def get_dir(path):
91     return tempfile.mkdtemp(".test", "xattr-", path)
92
93 def get_symlink(path, dangling=True):
94     """create a symlink"""
95     fh, fname = get_file(path)
96     os.close(fh)
97     if dangling:
98         os.unlink(fname)
99     sname = fname + ".symlink"
100     os.symlink(fname, sname)
101     return fname, sname
102
103 def get_valid_symlink(path):
104     return get_symlink(path, dangling=False)[1]
105
106 def get_dangling_symlink(path):
107     return get_symlink(path, dangling=True)[1]
108
109 # Note: user attributes are only allowed on files and directories, so
110 # we have to skip the symlinks here. See xattr(7).
111 ITEMS_P = [
112     (get_file_name, False),
113     (get_file_fd, False),
114     (get_file_object, False),
115     (get_dir, False),
116     (get_valid_symlink, False),
117 ]
118
119 ITEMS_D = [
120     "file name",
121     "file FD",
122     "file object",
123     "directory",
124     "file via symlink",
125 ]
126
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"]
130
131 @pytest.fixture(params=ITEMS_P, ids=ITEMS_D)
132 def subject(testdir, request):
133     return request.param[0](testdir), request.param[1]
134
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]
138
139 @pytest.fixture(params=[True, False], ids=["with namespace", "no namespace"])
140 def use_ns(request):
141     return request.param
142
143 @pytest.fixture(params=[True, False], ids=["dangling", "valid"])
144 def use_dangling(request):
145     return request.param
146
147 ### Test functions
148
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
153
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
158
159
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)])
171
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)]
189
190 def test_ListSetGet(subject, use_ns):
191     """check list, set, get operations against an item"""
192     item = subject[0]
193     lists_equal(xattr.list(item), [])
194     with pytest.raises(EnvironmentError):
195         if use_ns:
196             xattr.set(item, USER_NN, USER_VAL, flags=XATTR_REPLACE,
197                       namespace=NAMESPACE)
198         else:
199             xattr.set(item, USER_ATTR, USER_VAL, flags=XATTR_REPLACE)
200     if use_ns:
201         xattr.set(item, USER_NN, USER_VAL,
202                   namespace=NAMESPACE)
203     else:
204         xattr.set(item, USER_ATTR, USER_VAL)
205     with pytest.raises(EnvironmentError):
206         if use_ns:
207             xattr.set(item, USER_NN, USER_VAL,
208                       flags=XATTR_CREATE, namespace=NAMESPACE)
209         else:
210             xattr.set(item, USER_ATTR, USER_VAL, flags=XATTR_CREATE)
211     if use_ns:
212         assert xattr.list(item, namespace=NAMESPACE) == [USER_NN]
213     else:
214         lists_equal(xattr.list(item), [USER_ATTR])
215         lists_equal(xattr.list(item, namespace=EMPTY_NS),
216                     [USER_ATTR])
217     if use_ns:
218         assert xattr.get(item, USER_NN, namespace=NAMESPACE) == USER_VAL
219     else:
220         assert xattr.get(item, USER_ATTR) == USER_VAL
221     if use_ns:
222         assert xattr.get_all(item, namespace=NAMESPACE) == \
223             [(USER_NN, USER_VAL)]
224     else:
225         tuples_equal(xattr.get_all(item),
226                      [(USER_ATTR, USER_VAL)])
227     if use_ns:
228         xattr.remove(item, USER_NN, namespace=NAMESPACE)
229     else:
230         xattr.remove(item, USER_ATTR)
231     lists_equal(xattr.list(item), [])
232     tuples_equal(xattr.get_all(item), [])
233     with pytest.raises(EnvironmentError):
234         if use_ns:
235             xattr.remove(item, USER_NN, namespace=NAMESPACE)
236         else:
237             xattr.remove(item, USER_ATTR)
238
239 def test_ListSetGetDeprecated(subject):
240     """check deprecated list, set, get operations against an item"""
241     item = subject[0]
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)
256
257 def test_many_ops(subject):
258     """test many ops"""
259     item = subject[0]
260     xattr.set(item, USER_ATTR, USER_VAL)
261     VL = [USER_ATTR]
262     VN = [USER_NN]
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)]
275
276 def test_many_ops_deprecated(subject):
277     """test many ops (deprecated functions)"""
278     item = subject[0]
279     xattr.setxattr(item, USER_ATTR, USER_VAL)
280     VL = [USER_ATTR]
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)])
288
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)
296
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,
308                   namespace=NAMESPACE)
309
310 def test_binary_payload_deprecated(subject):
311     """test binary values (deprecated functions)"""
312     item = subject[0]
313     BINVAL = b"abc\0def"
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)
319
320 def test_binary_payload(subject):
321     """test binary values"""
322     item = subject[0]
323     BINVAL = b"abc\0def"
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)
332
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,
339                   nofollow=True)
340     with pytest.raises(IOError):
341         xattr.setxattr(sname, USER_ATTR, USER_VAL, XATTR_CREATE, True)
342
343 def test_none_namespace(subject):
344     with pytest.raises(TypeError):
345         xattr.get(subject[0], USER_ATTR, namespace=None)
346
347 @pytest.mark.parametrize(
348     "call",
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):
355         call()
356
357 @pytest.mark.parametrize(
358     "call, args", [(xattr.get, [USER_ATTR]),
359                    (xattr.listxattr, []),
360                    (xattr.list, []),
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)