xref: /illumos-gate/usr/src/test/smbclient-tests/include/xattr_common.ksh (revision 142d813a06c6f9a6142e2c276b62129a17a31a65)
1#
2# CDDL HEADER START
3#
4# The contents of this file are subject to the terms of the
5# Common Development and Distribution License (the "License").
6# You may not use this file except in compliance with the License.
7#
8# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9# or http://www.opensolaris.org/os/licensing.
10# See the License for the specific language governing permissions
11# and limitations under the License.
12#
13# When distributing Covered Code, include this CDDL HEADER in each
14# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15# If applicable, add the following below this CDDL HEADER, with the
16# fields enclosed by brackets "[]" replaced with your own identifying
17# information: Portions Copyright [yyyy] [name of copyright owner]
18#
19# CDDL HEADER END
20#
21
22#
23# Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24#
25
26# a function that takes a file, then creates and verifies
27# an xattr on that file. The xattr_contents is the file
28# that should appear in the xattr namespace.
29
30function create_xattr
31{       # filename xattr_name xattr_contents
32	typeset FILE=$1
33	typeset XATTR_NAME=$2
34	typeset XATTR_CONTENTS=$3
35
36	# read any empty xattr dir on that file
37	cti_execute_cmd "runat $FILE ls"
38
39	# create the xattr
40	cti_execute FAIL "runat $FILE cp $XATTR_CONTENTS $XATTR_NAME"
41	if [[ $? != 0 ]]; then
42	        cti_fail "FAIL:In function create_xattr: "\
43			"create xattr-of-$FILE failed unexpectedly"
44	        return
45	fi
46
47	verify_xattr $FILE $XATTR_NAME $XATTR_CONTENTS
48}
49
50# a function that compares the a single xattr between two files
51# and checks to see if their contents are identical
52
53function compare_xattrs
54{       # filename1 filename2 xattr_name
55	typeset FILE1=$1
56	typeset FILE2=$2
57	typeset XATTR_NAME=$3
58
59	cti_execute_cmd "runat $FILE1 cat $XATTR_NAME > /tmp/file1.$$"
60	cti_execute_cmd "runat $FILE2 cat $XATTR_NAME > /tmp/file2.$$"
61	cti_execute_cmd "diff /tmp/file1.$$ /tmp/file2.$$ \
62		>> /tmp/diffout.$$ 2>&1"
63	if [[ $? != 0 ]]; then
64	        cti_fail "FAIL:In function compare_xattrs: "\
65			"compare xattr-of-$FILE1 with xattr-of-$FILE2 "\
66			"failed unexpectedly"
67	        cti_report "diff xattrs-of-$FILE1 xattrs-of-$FILE2 "\
68			"printed you can see the file /tmp/diffout.$$"
69	        return
70	else
71	        cti_report "PASS:In function compare_xattrs: "\
72			"compare xattr-of-$FILE1 with xattr-of-$FILE2 "\
73			"succeeded as expected"
74	fi
75
76	cti_execute_cmd "rm /tmp/file1.$$ /tmp/file2.$$"
77	if [[ $? != 0 ]]; then
78	        cti_fail "FAIL:In function compare_xattrs: "\
79			"rm temp file: /tmp/file1.$$ /tmp/file2.$$ "\
80			"failed unexpectedly"
81	        return
82	else
83	        cti_report "PASS:In function compare_xattrs: "\
84			"rm temp file: /tmp/file1.$$ /tmp/file2.$$ "\
85			"succeeded as expected"
86	fi
87}
88
89# verify xattr exists and has content matching xattr_contents
90
91function verify_xattr
92{       # filename xattr_name xattr_contents
93	typeset FILE=$1
94	typeset XATTR_NAME=$2
95	typeset XATTR_CONTENTS=$3
96
97	cti_execute_cmd "runat $FILE diff $XATTR_NAME $XATTR_CONTENTS"
98	if [[ $? != 0 ]]; then
99	        cti_fail "FAIL:In function verify_xattr: "\
100			"verify xattr-of-$FILE failed unexpectedly"
101	        return
102	fi
103}
104
105function delete_xattr
106{       # filename xattr_name
107	typeset FILE=$1
108	typeset XATTR_NAME=$2
109
110	# delete the xattr
111	cti_execute_cmd "runat $FILE rm $XATTR_NAME"
112	if [[ $? != 0 ]]; then
113	        cti_fail "FAIL:In function delete_xattr: "\
114			"delete xattr-of-$FILE failed unexpectedly"
115	        return
116	else
117	        cti_report "PASS:In function delete_xattr: "\
118			"delete xattr-of-$FILE succeeded as expected"
119	fi
120
121	# make sure it's gone (ls should fail)
122	cti_execute PASS "runat $FILE ls $XATTR_NAME"
123	if [[ $? == 0 ]]; then
124	        cti_fail "FAIL:In function delete_xattr: "\
125			"$FILE has xattr named $XATTR_NAME unexpectedly"
126	        return
127	else
128	        cti_report "PASS:In function delete_xattr: "\
129			"$FILE does not have xattr named "\
130			"$XATTR_NAME as expected"
131	fi
132
133}
134
135# not sure about this : really this should be testing write/append
136
137function verify_write_xattr
138{       # filename xattr_name
139	typeset FILE=$1
140	typeset XATTR_NAME=$2
141
142	cti_execute_cmd "runat $FILE dd if=/etc/passwd of=$XATTR_NAME"
143	if [[ $? != 0 ]]; then
144	        cti_fail "FAIL:In function verify_write_xattr: "\
145			"create xattr-of-$FILE named $XATTR_NAME "\
146			"failed unexpectedly"
147	        return
148	else
149	cti_report "PASS:In function verify_write_xattr: "\
150		"create xattr-of-$FILE named $XATTR_NAME succeeded"
151	fi
152
153	cti_execute_cmd "runat $FILE cat $XATTR_NAME \
154		> /tmp/$XATTR_NAME.$$ 2>&1"
155	if [[ $? != 0 ]]; then
156	        cti_fail "FAIL:In function verify_write_xattr: "\
157			"cat xattr-of-$FILE named $XATTR_NAME "\
158			"failed unexpectedly"
159	        return
160	else
161	        cti_report "PASS:In function verify_write_xattr: "\
162			"cat xattr-of-$FILE named $XATTR_NAME succeeded"
163	fi
164
165	cti_execute_cmd "dd if=/etc/passwd of=/tmp/passwd_dd.$$"
166	cti_execute_cmd "diff /tmp/passwd_dd.$$ /tmp/$XATTR_NAME.$$"
167	if [[ $? != 0 ]]; then
168	        cti_fail "FAIL:In function verify_write_xattr: "\
169			"diff xattr-of-$FILE named $XATTR_NAME failed"
170	        return
171	else
172	        cti_report "PASS:In function verify_write_xattr: "\
173			"diff xattr-of-$FILE named $XATTR_NAME succeeded"
174	fi
175
176	cti_execute_cmd "rm /tmp/passwd_dd.$$ /tmp/$XATTR_NAME.$$"
177}
178
179# this function is to create the expected output
180
181function create_expected_output
182{       # expected_output_file  contents_of_the_output
183	typeset FILE=$1
184	shift
185	if [[ -e $FILE ]]; then
186	        cti_execute_cmd "rm $FILE"
187	fi
188
189	for line in $@
190	do
191	        cti_execute_cmd "echo $line >> $FILE"
192	done
193 }
194