xref: /illumos-gate/usr/src/test/zfs-tests/tests/functional/xattr/xattr_common.kshlib (revision 66582b606a8194f7f3ba5b3a3a6dca5b0d346361)
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 2008 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26
27#
28# Copyright (c) 2013, 2016 by Delphix. All rights reserved.
29#
30
31. $STF_SUITE/include/libtest.shlib
32
33# a function that takes a file, then creates and verifies
34# an xattr on that file. The xattr_contents is the file
35# that should appear in the xattr namespace.
36function create_xattr { # filename xattr_name xattr_contents
37	typeset FILE=$1
38	typeset XATTR_NAME=$2
39	typeset XATTR_CONTENTS=$3
40
41	# read any empty xattr on that file
42	log_must runat $FILE ls
43	# create the xattr
44	log_must runat $FILE cp $XATTR_CONTENTS $XATTR_NAME
45
46	verify_xattr $FILE $XATTR_NAME $XATTR_CONTENTS
47}
48
49# a function that compares the a single xattr between two files
50# and checks to see if their contents are identical
51function compare_xattrs { # filename1 filename2 xattr_name
52	typeset FILE1=$1
53	typeset FILE2=$2
54	typeset XATTR_NAME=$3
55
56	runat $FILE1 cat $XATTR_NAME > /tmp/file1.$$
57	runat $FILE2 cat $XATTR_NAME > /tmp/file2.$$
58
59	log_must diff /tmp/file1.$$ /tmp/file2.$$
60	log_must rm /tmp/file1.$$ /tmp/file2.$$
61}
62
63function verify_xattr { # filename xattr_name xattr_contents
64	typeset FILE=$1
65	typeset XATTR_NAME=$2
66	typeset XATTR_CONTENTS=$3
67
68	# read the xattr, writing it to a temp file
69	log_must eval "runat $FILE cat $XATTR_NAME > /tmp/$XATTR_NAME.$$ 2>&1"
70	log_must diff $XATTR_CONTENTS /tmp/$XATTR_NAME.$$
71	rm /tmp/$XATTR_NAME.$$
72}
73
74function delete_xattr { # filename xattr_name
75        typeset FILE=$1
76        typeset XATTR_NAME=$2
77
78        # delete the xattr
79        log_must runat $FILE rm $XATTR_NAME
80        log_mustnot eval "runat $FILE ls $XATTR_NAME > /dev/null 2>&1"
81}
82
83# not sure about this : really this should be testing write/append
84function verify_write_xattr { # filename xattr_name
85        typeset FILE=$1
86        typeset XATTR_NAME=$2
87
88        log_must eval "runat $FILE dd if=/etc/passwd of=$XATTR_NAME"
89        log_must eval "runat $FILE cat $XATTR_NAME > /tmp/$XATTR_NAME.$$ 2>&1"
90        log_must dd if=/etc/passwd of=/tmp/passwd_dd.$$
91        log_must diff /tmp/passwd_dd.$$ /tmp/$XATTR_NAME.$$
92        log_must rm /tmp/passwd_dd.$$ /tmp/$XATTR_NAME.$$
93}
94
95# this function is to create the expected output
96function create_expected_output  { # expected_output_file  contents_of_the_output
97   typeset FILE=$1
98   shift
99   if [[ -f $FILE ]]; then
100      log_must rm $FILE
101   fi
102
103   for line in $@
104   do
105      log_must eval "echo $line >> $FILE"
106   done
107 }
108