1# vim: filetype=sh
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23# $FreeBSD$
24
25#
26# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
27# Use is subject to license terms.
28#
29# ident	"@(#)xattr_common.kshlib	1.3	08/02/27 SMI"
30#
31
32. $STF_SUITE/include/libtest.kshlib
33
34# a function that takes a file, then creates and verifies
35# an xattr on that file. The xattr_contents is the file
36# that should appear in the xattr namespace.
37function create_xattr { # filename xattr_name xattr_contents
38	typeset FILE=$1
39	typeset XATTR_NAME=$2
40	typeset XATTR_CONTENTS=$3
41
42	# read any empty xattr on that file
43	log_must $RUNAT $FILE $LS
44	# create the xattr
45	log_must $RUNAT $FILE $CP $XATTR_CONTENTS $XATTR_NAME
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
52function compare_xattrs { # filename1 filename2 xattr_name
53	typeset FILE1=$1
54	typeset FILE2=$2
55	typeset XATTR_NAME=$3
56
57	$RUNAT $FILE1 $CAT $XATTR_NAME > $TMPDIR/file1.${TESTCASE_ID}
58	$RUNAT $FILE2 $CAT $XATTR_NAME > $TMPDIR/file2.${TESTCASE_ID}
59
60	log_must $DIFF $TMPDIR/file1.${TESTCASE_ID} $TMPDIR/file2.${TESTCASE_ID}
61	log_must $RM $TMPDIR/file1.${TESTCASE_ID} $TMPDIR/file2.${TESTCASE_ID}
62}
63
64function verify_xattr { # filename xattr_name xattr_contents
65	typeset FILE=$1
66	typeset XATTR_NAME=$2
67	typeset XATTR_CONTENTS=$3
68
69	# read the xattr, writing it to a temp file
70	log_must eval "$RUNAT $FILE $CAT $XATTR_NAME > $TMPDIR/$XATTR_NAME.${TESTCASE_ID} 2>&1"
71	log_must $DIFF $XATTR_CONTENTS $TMPDIR/$XATTR_NAME.${TESTCASE_ID}
72	$RM $TMPDIR/$XATTR_NAME.${TESTCASE_ID}
73}
74
75function delete_xattr { # filename xattr_name
76        typeset FILE=$1
77        typeset XATTR_NAME=$2
78
79        # delete the xattr
80        log_must $RUNAT $FILE $RM $XATTR_NAME
81        log_mustnot eval "$RUNAT $FILE $LS $XATTR_NAME > /dev/null 2>&1"
82}
83
84# not sure about this : really this should be testing write/append
85function verify_write_xattr { # filename xattr_name
86        typeset FILE=$1
87        typeset XATTR_NAME=$2
88
89        log_must eval "$RUNAT $FILE $DD if=/etc/passwd of=$XATTR_NAME"
90        log_must eval "$RUNAT $FILE $CAT $XATTR_NAME > $TMPDIR/$XATTR_NAME.${TESTCASE_ID} 2>&1"
91        log_must $DD if=/etc/passwd of=$TMPDIR/passwd_dd.${TESTCASE_ID}
92        log_must $DIFF $TMPDIR/passwd_dd.${TESTCASE_ID} $TMPDIR/$XATTR_NAME.${TESTCASE_ID}
93        log_must $RM $TMPDIR/passwd_dd.${TESTCASE_ID} $TMPDIR/$XATTR_NAME.${TESTCASE_ID}
94}
95
96# this function is to create the expected output
97function create_expected_output  { # expected_output_file  contents_of_the_output
98   typeset FILE=$1
99   shift
100   if [[ -f $FILE ]]; then
101      log_must $RM $FILE
102   fi
103
104   for line in $@
105   do
106      log_must eval "$ECHO $line >> $FILE"
107   done
108 }
109