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#
24# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26
27. $STF_SUITE/include/libtest.kshlib
28
29set -A VALID_NAME_CHAR a b c d e f g h i j k l m n o p q r s t u v w x y z \
30     		       0 1 2 3 4 5 6 7 8 9 ':' '-' '.' '_'
31set -A INVALID_NAME_CHAR A B C D E F G H I J K L M N O P Q R S T U V W X Y Z \
32		         '`' '~' '!' '@' '#' '$' '%' '^' '&' '(' ')' '+' '=' \
33			 '|' '{' '[' ']' '}' ';' '"' '<' ',' '>' '?' '/' \
34			 ' '
35set -A ALL_CHAR ${VALID_NAME_CHAR[*]} ${INVALID_NAME_CHAR[*]}
36
37#
38# Firstly, set the property value to dataset. Then checking if the property
39# value is equal with the expected value, according to the expected result.
40#
41# $1 property value
42# $2 property name
43# $3 dataset
44# $4 expected result
45#
46function set_n_check_prop
47{
48	typeset expect_value=$1
49	typeset prop=$2
50	typeset dataset=$3
51	typeset expect_result=${4:-true}
52	typeset old_value=""
53	typeset cur_value=""
54
55	[ -n "$prop" ] && old_value=$(get_prop $prop $dataset)
56	if [ "$expect_result" = "true" ]; then
57		log_must $ZFS set $prop=$expect_value $dataset
58	else
59		log_mustnot $ZFS set $prop=$expect_value $dataset
60	fi
61	[ -n "$prop" ] && cur_value=$(get_prop $prop $dataset)
62
63	err="ERROR: Dataset '$dataset': '$prop' value '$cur_value'"
64	if [ "$expect_result" = "true" ]; then
65		[ "$expect_value" = "gzip-6" ] && expect_value="gzip"
66		case "$prop" in
67		reservation|reserv|quota)
68			if [ "$expect_value" = "none" -a "$cur_value" != "0" ]; then
69				err="$err should not be set!"
70				log_fail "$err"
71				return
72			fi
73			;;
74		esac
75		if [ "$cur_value" != "$expect_value" ]; then
76			err="$err should have changed to '$expect_value'!"
77			log_fail "$err"
78		fi
79	else
80		if [ "$expect_value" != "" -a "$cur_value" != "$old_value" ]; then
81			err="$err should be unchanged at '$old_value'!"
82			log_fail "$err"
83		fi
84	fi
85}
86
87#
88# Cleanup all the user properties of the pool and the dataset reside it.
89#
90# $1 pool name
91#
92function cleanup_user_prop
93{
94	typeset pool=$1
95	typeset dtst=$($ZFS list -H -r -o name -t filesystem,volume $pool)
96
97	typeset user_prop
98	for dt in $dtst; do
99		user_prop=$($ZFS get -H -o property all $dtst | grep ":")
100
101		typeset prop
102		for prop in $user_prop; do
103			$ZFS inherit $prop $dt
104			(($? != 0)) && log_must $ZFS inherit $prop $dt
105		done
106	done
107}
108
109#
110# Random select charactor from the specified charactor set and combine into a
111# random string
112#
113# $1 character set name
114# $2 String length
115#
116function random_string
117{
118	typeset char_set=${1:-VALID_NAME_CHAR}
119	typeset -i len=${2:-5}
120
121	eval typeset -i count=\${#$char_set[@]}
122
123	typeset str
124	typeset -i i=0
125	while ((i < len)); do
126		typeset -i ind
127		((ind = RANDOM % count))
128		eval str=\${str}\${$char_set[\$ind]}
129
130		((i += 1))
131	done
132
133	$ECHO "$str"
134}
135
136#
137# Get vaild user defined property name
138#
139# $1 user defined property name length
140#
141function valid_user_property
142{
143	typeset -i sumlen=${1:-10}
144	((sumlen < 2 )) && sumlen=2
145	typeset -i len
146	((len = RANDOM % sumlen))
147	typeset part1 part2
148
149	while true; do
150		part1="$(random_string VALID_NAME_CHAR $len)"
151		if [[ "$part1" == "-"* ]]; then
152			continue
153		fi
154		break
155	done
156	((len = sumlen - (len + 1)))
157
158	while true; do
159		part2="$(random_string VALID_NAME_CHAR $len)"
160		if [[ -z $part1 && -z $part2 ]]; then
161			continue
162		fi
163		break
164	done
165
166	$ECHO "${part1}:${part2}"
167}
168
169#
170# Get invaild user defined property name
171#
172# $1 user defined property name length
173#
174function invalid_user_property
175{
176	typeset -i sumlen=${1:-10}
177	((sumlen == 0)) && sumlen=1
178	typeset -i len
179	((len = RANDOM % sumlen))
180
181	typeset part1 part2
182	while true; do
183		part1="$(random_string VALID_NAME_CHAR $len)"
184		((len = sumlen - len))
185		part2="$(random_string INVALID_NAME_CHAR $len)"
186
187		# Avoid $part1 is *:* and $part2 is "=*"
188		if [[ "$part1" == *":"* && "$part2" == "="* ]]; then
189			continue
190		fi
191		break
192	done
193
194	$ECHO "${part1}${part2}"
195}
196
197#
198# Get user property value
199#
200# $1 user defined property name length
201#
202function user_property_value
203{
204	typeset -i len=${1:-100}
205	((len < 1 )) && len=1
206
207	typeset value=$(random_string ALL_CHAR $len)
208
209	$ECHO "$value"
210}
211
212#
213# Check if the user property is identical to the expected value.
214#
215# $1 dataset
216# $2 user property
217# $3 expected value
218#
219function check_user_prop
220{
221	typeset dtst=$1
222	typeset user_prop="$2"
223	typeset expect_value="$3"
224	typeset value=$($ZFS get -p -H -o value "$user_prop" $dtst 2>&1)
225
226	if [[ "$expect_value" == "$value" ]]; then
227		return 0
228	else
229		return 1
230	fi
231}
232
233#
234# Get source of the dataset
235#
236function get_source
237{
238	typeset prop=$1
239	typeset dataset=$2
240	typeset source
241
242	source=$($ZFS get -H -o source $prop $dataset)
243        if (($? != 0)); then
244                log_fail "Unable to get $prop source for dataset $dataset"
245        fi
246
247	$ECHO "$source"
248}
249