xref: /freebsd/usr.sbin/extattr/tests/extattr_test.sh (revision ee5cf11617a9b7f034d95c639bd4d27d1f09e848)
1#
2# Copyright (c) 2016 Spectra Logic Corp
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8# 1. Redistributions of source code must retain the above copyright
9#    notice, this list of conditions and the following disclaimer.
10# 2. Redistributions in binary form must reproduce the above copyright
11#    notice, this list of conditions and the following disclaimer in the
12#    documentation and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24# SUCH DAMAGE.
25#
26# $FreeBSD$
27
28atf_test_case bad_namespace
29bad_namespace_head() {
30	atf_set "descr" "Can't set attributes for nonexistent namespaces"
31}
32bad_namespace_body() {
33	touch foo
34	atf_check -s not-exit:0 -e match:"Invalid argument" \
35		setextattr badnamespace myattr X foo
36	atf_check -s not-exit:0 -e match:"Invalid argument" \
37		lsextattr -q badnamespace foo
38}
39
40atf_test_case hex
41hex_head() {
42	atf_set "descr" "Set and get attribute values in hexadecimal"
43}
44hex_body() {
45	touch foo
46	atf_check -s exit:0 -o empty setextattr user myattr XYZ foo
47	atf_check -s exit:0 -o inline:"58 59 5a\n" \
48		getextattr -qx user myattr foo
49}
50
51atf_test_case hex_nonascii
52hex_nonascii_head() {
53	atf_set "descr" "Get binary attribute values in hexadecimal"
54}
55hex_nonascii_body() {
56	touch foo
57	BINSTUFF=`echo $'\x20\x30\x40\x55\x66\x70\x81\xa2\xb3\xee\xff'`
58	atf_check -s exit:0 -o empty setextattr user myattr "$BINSTUFF" foo
59	getextattr user myattr foo
60	atf_check -s exit:0 -o inline:"20 30 40 55 66 70 81 a2 b3 ee ff\n" \
61		getextattr -qx user myattr foo
62}
63
64atf_test_case long_name
65long_name_head() {
66	atf_set "descr" "A maximum length attribute name"
67}
68long_name_body() {
69	# https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=208965
70	atf_expect_fail "BUG 208965 extattr(2) doesn't allow maxlen attr names"
71
72	ATTRNAME=`jot -b X -s "" 255 0`
73	touch foo
74	atf_check -s exit:0 -o empty setextattr user $ATTRNAME myvalue foo
75	atf_check -s exit:0 -o inline:"${ATTRNAME}\n" lsextattr -q user foo
76	atf_check -s exit:0 -o inline:"myvalue\n" \
77		getextattr -q user ${ATTRNAME} foo
78	atf_check -s exit:0 -o empty rmextattr user ${ATTRNAME} foo
79	atf_check -s exit:0 -o empty lsextattr -q user foo
80}
81
82atf_test_case loud
83loud_head() {
84	atf_set "descr" "Loud (non -q) output for each command"
85}
86loud_body() {
87	touch foo
88	# setextattr(8) and friends print hard tabs.  Use printf to convert
89	# them to spaces before checking the output.
90	atf_check -s exit:0 -o empty setextattr user myattr myvalue foo
91	atf_check -s exit:0 -o inline:"foo myattr" \
92		printf "%s %s" $(lsextattr user foo)
93	atf_check -s exit:0 -o inline:"foo myvalue" \
94		printf "%s %s" $(getextattr user myattr foo)
95	atf_check -s exit:0 -o empty rmextattr user myattr foo
96	atf_check -s exit:0 -o inline:"foo" printf %s $(lsextattr user foo)
97}
98
99atf_test_case noattrs
100noattrs_head() {
101	atf_set "descr" "A file with no extended attributes"
102}
103noattrs_body() {
104	touch foo
105	atf_check -s exit:0 -o empty lsextattr -q user foo
106}
107
108atf_test_case nonexistent_file
109nonexistent_file_head() {
110	atf_set "descr" "A file that does not exist"
111}
112nonexistent_file_body() {
113	atf_check -s exit:1 -e match:"No such file or directory" \
114		lsextattr user foo
115	atf_check -s exit:1 -e match:"No such file or directory" \
116		setextattr user myattr myvalue foo
117	atf_check -s exit:1 -e match:"No such file or directory" \
118		getextattr user myattr foo
119	atf_check -s exit:1 -e match:"No such file or directory" \
120		rmextattr user myattr foo
121}
122
123atf_test_case null
124null_head() {
125	atf_set "descr" "NUL-terminate an attribute value"
126}
127null_body() {
128	touch foo
129	atf_check -s exit:0 -o empty setextattr -n user myattr myvalue foo
130	atf_check -s exit:0 -o inline:"myvalue\0\n" getextattr -q user myattr foo
131}
132
133atf_test_case one_user_attr
134one_user_attr_head() {
135	atf_set "descr" "A file with one extended attribute"
136}
137one_user_attr_body() {
138	touch foo
139	atf_check -s exit:0 -o empty setextattr user myattr myvalue foo
140	atf_check -s exit:0 -o inline:"myattr\n" lsextattr -q user foo
141	atf_check -s exit:0 -o inline:"myvalue\n" getextattr -q user myattr foo
142	atf_check -s exit:0 -o empty rmextattr user myattr foo
143	atf_check -s exit:0 -o empty lsextattr -q user foo
144}
145
146atf_test_case one_system_attr
147one_system_attr_head() {
148	atf_set "descr" "A file with one extended attribute"
149	atf_set "require.user" "root"
150}
151one_system_attr_body() {
152	touch foo
153	atf_check -s exit:0 -o empty setextattr system myattr myvalue foo
154	atf_check -s exit:0 -o inline:"myattr\n" lsextattr -q system foo
155	atf_check -s exit:0 -o inline:"myvalue\n" getextattr -q system myattr foo
156	atf_check -s exit:0 -o empty rmextattr system myattr foo
157	atf_check -s exit:0 -o empty lsextattr -q system foo
158}
159
160atf_test_case stdin
161stdin_head() {
162	atf_set "descr" "Set attribute value from stdin"
163}
164stdin_body() {
165	dd if=/dev/random of=infile bs=1k count=8
166	touch foo
167	setextattr -i user myattr foo < infile || atf_fail "setextattr failed"
168	atf_check -s exit:0 -o inline:"myattr\n" lsextattr -q user foo
169	getextattr -qq user myattr foo > outfile || atf_fail "getextattr failed"
170	atf_check -s exit:0 cmp -s infile outfile
171}
172
173atf_test_case stringify
174stringify_head() {
175	atf_set "descr" "Stringify the output of getextattr"
176}
177stringify_body() {
178	touch foo
179	atf_check -s exit:0 -o empty setextattr user myattr "my value" foo
180	atf_check -s exit:0 -o inline:"\"my\\\040value\"\n" \
181		getextattr -qs user myattr foo
182}
183
184atf_test_case symlink
185symlink_head() {
186	atf_set "descr" "A symlink to an ordinary file"
187}
188symlink_body() {
189	touch foo
190	ln -s foo foolink
191	atf_check -s exit:0 -o empty setextattr user myattr myvalue foolink
192	atf_check -s exit:0 -o inline:"myvalue\n" \
193		getextattr -q user myattr foolink
194	atf_check -s exit:0 -o inline:"myvalue\n" getextattr -q user myattr foo
195}
196
197atf_test_case symlink_nofollow
198symlink_nofollow_head() {
199	atf_set "descr" "Operating directly on a symlink"
200}
201symlink_nofollow_body() {
202	touch foo
203	ln -s foo foolink
204	# Check that with -h we can operate directly on the link
205	atf_check -s exit:0 -o empty setextattr -h user myattr myvalue foolink
206	atf_check -s exit:0 -o inline:"myvalue\n" \
207		getextattr -qh user myattr foolink
208	atf_check -s exit:1 -e match:"Attribute not found" \
209		getextattr user myattr foolink
210	atf_check -s exit:1 -e match:"Attribute not found" \
211		getextattr user myattr foo
212
213	# Check that with -h we cannot operate on the destination file
214	atf_check -s exit:0 -o empty setextattr user otherattr othervalue foo
215	atf_check -s exit:1 getextattr -qh user otherattr foolink
216}
217
218atf_test_case system_and_user_attrs
219system_and_user_attrs_head() {
220	atf_set "descr" "A file with both system and user extended attributes"
221	atf_set "require.user" "root"
222}
223system_and_user_attrs_body() {
224	touch foo
225	atf_check -s exit:0 -o empty setextattr user userattr userval foo
226	atf_check -s exit:0 -o empty setextattr system sysattr sysval foo
227	atf_check -s exit:0 -o inline:"userattr\n" lsextattr -q user foo
228	atf_check -s exit:0 -o inline:"sysattr\n" lsextattr -q system foo
229
230	atf_check -s exit:0 -o inline:"userval\n" getextattr -q user userattr foo
231	atf_check -s exit:0 -o inline:"sysval\n" getextattr -q system sysattr foo
232	atf_check -s exit:0 -o empty rmextattr user userattr foo
233	atf_check -s exit:0 -o empty rmextattr system sysattr foo
234	atf_check -s exit:0 -o empty lsextattr -q user foo
235	atf_check -s exit:0 -o empty lsextattr -q system foo
236}
237
238atf_test_case two_files
239two_files_head() {
240	atf_set "descr" "Manipulate two files"
241}
242two_files_body() {
243	touch foo bar
244	atf_check -s exit:0 -o empty setextattr user myattr myvalue foo bar
245	atf_check -s exit:0 -o inline:"foo\tmyattr\nbar\tmyattr\n" \
246		lsextattr user foo bar
247	atf_check -s exit:0 \
248		-o inline:"foo\tmyvalue\nbar\tmyvalue\n" \
249		getextattr user myattr foo bar
250	atf_check -s exit:0 -o empty rmextattr user myattr foo bar
251	atf_check -s exit:0 -o empty lsextattr -q user foo bar
252}
253
254atf_test_case two_files_force
255two_files_force_head() {
256	atf_set "descr" "Manipulate two files.  The first does not exist"
257}
258two_files_force_body() {
259	touch bar
260	atf_check -s exit:1 -e match:"No such file or directory" \
261		setextattr user myattr myvalue foo bar
262	atf_check -s exit:0 -e ignore setextattr -f user myattr myvalue foo bar
263	atf_check -s exit:1 -e match:"No such file or directory" \
264		lsextattr user foo bar
265	atf_check -s exit:0 -e ignore -o inline:"bar\tmyattr\n" \
266		lsextattr -f user foo bar
267	atf_check -s exit:1 -e match:"No such file or directory" \
268		getextattr user myattr foo bar
269	atf_check -s exit:0 -e ignore \
270		-o inline:"bar\tmyvalue\n" \
271		getextattr -f user myattr foo bar
272	atf_check -s exit:1 -e match:"No such file or directory" \
273		rmextattr user myattr foo bar
274	atf_check -s exit:0 -e ignore \
275		rmextattr -f user myattr foo bar
276	atf_check -s exit:0 -o empty lsextattr -q user bar
277}
278
279atf_test_case two_user_attrs
280two_user_attrs_head() {
281	atf_set "descr" "A file with two extended attributes"
282}
283two_user_attrs_body() {
284	touch foo
285	atf_check -s exit:0 -o empty setextattr user myattr1 myvalue1 foo
286	atf_check -s exit:0 -o empty setextattr user myattr2 myvalue2 foo
287	# lsextattr could return the attributes in any order, so we must be
288	# careful how we compare them.
289	raw_output=`lsextattr -q user foo` || atf_fail "lsextattr failed"
290	tabless_output=`printf "%s %s" ${raw_output}`
291	if [ "myattr1 myattr2" != "${tabless_output}" -a \
292	     "myattr2 myattr1" != "${tabless_output}" ]; then
293		atf_fail "lsextattr printed ${tabless_output}"
294	fi
295	atf_check -s exit:0 -o inline:"myvalue1\n" getextattr -q user myattr1 foo
296	atf_check -s exit:0 -o inline:"myvalue2\n" getextattr -q user myattr2 foo
297	atf_check -s exit:0 -o empty rmextattr user myattr2 foo
298	atf_check -s exit:0 -o empty rmextattr user myattr1 foo
299	atf_check -s exit:0 -o empty lsextattr -q user foo
300}
301
302atf_test_case unprivileged_user_cannot_set_system_attr
303unprivileged_user_cannot_set_system_attr_head() {
304	atf_set "descr" "Unprivileged users can't set system attributes"
305        atf_set "require.user" "unprivileged"
306}
307unprivileged_user_cannot_set_system_attr_body() {
308	touch foo
309	atf_check -s exit:1 -e match:"Operation not permitted" \
310		setextattr system myattr myvalue foo
311}
312
313
314atf_init_test_cases() {
315	atf_add_test_case bad_namespace
316	atf_add_test_case hex
317	atf_add_test_case hex_nonascii
318	atf_add_test_case long_name
319	atf_add_test_case loud
320	atf_add_test_case noattrs
321	atf_add_test_case nonexistent_file
322	atf_add_test_case null
323	atf_add_test_case symlink_nofollow
324	atf_add_test_case one_user_attr
325	atf_add_test_case one_system_attr
326	atf_add_test_case stdin
327	atf_add_test_case stringify
328	atf_add_test_case symlink
329	atf_add_test_case symlink_nofollow
330	atf_add_test_case system_and_user_attrs
331	atf_add_test_case two_files
332	atf_add_test_case two_files_force
333	atf_add_test_case two_user_attrs
334	atf_add_test_case unprivileged_user_cannot_set_system_attr
335}
336