#! /bin/bash
# FS QA Test No. 097. Modifed from UDFQA test 033.
#
# simple attr tests for EAs:
#  - set
#  - get
#  - list
#  - remove
# Basic testing.
#
#-----------------------------------------------------------------------
# Copyright (c) 2000-2004 Silicon Graphics, Inc.  All Rights Reserved.
# Copyright (c) 2017 Google, Inc.  All Rights Reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
#-----------------------------------------------------------------------
#

seq=`basename $0`
seqres=$RESULT_DIR/$seq
echo "QA output created by $seq"

here=`pwd`
tmp=/tmp/$$
status=1	# failure is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

file=$TEST_DIR/foo

_cleanup()
{
	rm -f $tmp.* $file
}

getfattr()
{
	$GETFATTR_PROG --absolute-names "$@" |& _filter_test_dir
}

setfattr()
{
	$SETFATTR_PROG "$@" |& _filter_test_dir
}

# get standard environment, filters and checks
. ./common/rc
. ./common/attr
. ./common/filter

# real QA test starts here
_supported_fs generic
_supported_os Linux

_require_test
_require_attrs

echo -e "\ncreate file foo"
rm -f $file
touch $file

echo -e "\nshould be no EAs for foo:"
getfattr -d $file

echo -e "\nset EA <noise,woof>:"
setfattr -n user.noise -v woof $file

echo -e "\nset EA <colour,blue>:"
setfattr -n user.colour -v blue $file

echo -e "\nset EA <size,small>:"
setfattr -n user.size -v small $file

echo -e "\nlist the EAs for foo: noise, colour, size"
getfattr -d $file

echo -e "\ncheck the list again for foo"
getfattr -d $file

echo -e "\nunmount the FS and see if EAs are persistent"
_test_cycle_mount

echo -e "\ncheck the list again for foo after umount/mount"
getfattr -d $file

echo -e "\nremove the colour EA on foo"
setfattr -x user.colour $file

echo -e "\nlist EAs for foo: noise, size"
getfattr -d $file

echo -e "\nget the value of the noise EA"
getfattr -n user.noise $file

echo -e "\nget the value of the colour EA which was removed earlier"
getfattr -n user.colour $file

echo -e "\nget the value of the size EA"
getfattr -n user.size $file

echo -e "\nlist all the EAs again: noise, size"
getfattr -d $file

echo -e "\nchange the value of the size EA from small to huge"
setfattr -n user.size -v huge $file

echo -e "\nget the size EA which should now have value huge"
getfattr -n user.size $file

echo -e "\nlist EAs: noise, size"
getfattr -d $file

echo -e "\nremove the size EA from foo"
setfattr -x user.size $file

echo -e "\nlist EAs: noise (size EA has been removed)"
getfattr -d $file

echo -e "\ntry removing non-existent EA named woof"
setfattr -x user.woof $file

echo -e "\ntry removing already removed EA size"
setfattr -x user.size $file

echo -e "\nlist EAs: noise"
getfattr -d $file

echo -e "\ntry removing already removed EA colour"
setfattr -x user.colour $file

echo -e "\nlist EAs: noise"
getfattr -d $file

echo -e "\nremove remaining EA noise"
setfattr -x user.noise $file

echo -e "\nlist EAs: should be no EAs left now"
getfattr -d $file

echo -e "\nunmount the FS and see if EAs are persistent"
_test_cycle_mount

echo -e "\nlist EAs: should still be no EAs left"
getfattr -d $file

echo -e "\n*** Test out the trusted namespace ***"

echo -e "\nset EA <trusted:colour,marone>:"
setfattr -n trusted.colour -v marone $file

echo -e "\nset EA <user:colour,beige>:"
setfattr -n user.colour -v beige $file

echo -e "\nset EA <user:vomit,pizza>:"
setfattr -n user.vomit -v pizza $file

echo -e "\nset EA <trusted:noise,whack>:"
setfattr -n trusted.noise -v whack $file

echo -e "\nlist trusted EAs: <trusted:colour,noise>:"
getfattr -d -m '^trusted\.' $file

echo -e "\nlist user EAs: <user:colour,vomit>:"
getfattr -d $file

echo -e "\nget trusted EA colour: marone"
getfattr -n trusted.colour $file

echo -e "\nget trusted EA noise: whack"
getfattr -n trusted.noise $file

echo -e "\nget trusted EA vomit which is a user EA => find nothing"
getfattr -n trusted.vomit $file

echo -e "\nunmount the FS and see if EAs are persistent"
_test_cycle_mount

echo -e "\nget trusted EA colour: marone"
getfattr -n trusted.colour $file

echo -e "\nget trusted EA noise: whack"
getfattr -n trusted.noise $file

echo -e "\nget user EA vomit: pizza"
getfattr -n user.vomit $file

echo -e "\nremove the trusted colour EA"
setfattr -x trusted.colour $file

echo -e "\nlist trusted EAs: <trusted:noise>:"
getfattr -d -m '^trusted\.' $file

echo -e "\nlist user EAs: <user:colour,vomit>:"
getfattr -d $file

echo -e "\nremove the final trusted EA noise"
setfattr -x trusted.noise $file

echo -e "\nlist trusted EAs: none"
getfattr -d -m '^trusted\.' $file

# success, all done
status=0
exit
