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