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 2009 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26
27#
28# Copyright (c) 2014, 2016 by Delphix. All rights reserved.
29#
30
31. $STF_SUITE/include/libtest.shlib
32
33set -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 \
34    0 1 2 3 4 5 6 7 8 9 ':' '-' '.' '_'
35set -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 \
36    '`' '~' '!' '@' '#' '$' '%' '^' '&' '(' ')' '+' '=' '|' "\\" '{' '[' ']' \
37    '}' ';' '"' '<' ',' '>' '?' '/' ' '
38set -A ALL_CHAR ${VALID_NAME_CHAR[*]} ${INVALID_NAME_CHAR[*]}
39
40#
41# Firstly, set the property value to dataset. Then checking if the property
42# value is equal with the expected value, according to the expected result.
43#
44# $1 property value
45# $2 property name
46# $3 dataset
47# $4 expected result
48#
49function set_n_check_prop
50{
51	typeset expect_value=$1
52	typeset prop=$2
53	typeset dataset=$3
54	typeset expect_result=${4:-true}
55
56	typeset old_value=""
57	typeset cur_value=""
58
59	[[ -n $prop ]] && old_value=$(get_prop $prop $dataset)
60
61	if [[ $expect_result == true ]]; then
62		[[ -z $prop || -z $dataset ]] && \
63			log_fail "property or dataset isn't defined."
64
65		log_must zfs set $prop=$expect_value $dataset
66		if [[ $expect_value == "gzip-6" ]]; then
67			expect_value="gzip"
68		fi
69
70		[[ -n $prop ]] && cur_value=$(get_prop $prop $dataset)
71
72		case $prop in
73			reservation|reserv|quota )
74				if [[ $expect_value == "none" ]]; then
75					[[ $cur_value != "0" ]] && \
76						log_fail "The '$dataset' '$prop' value \
77						'$cur_value' is not expected."
78				elif [[ $cur_value != $expect_value ]]; then
79					log_fail "The '$dataset' '$prop' value '$cur_value' \
80					does not equal the expected value '$expect_value'."
81				fi
82				;;
83			* )
84				if [[ $cur_value != $expect_value ]]; then
85					log_fail "The '$dataset' '$prop' value '$cur_value' \
86					does not equal the expected value '$expect_value'."
87				fi
88				;;
89		esac
90
91	else
92		log_mustnot zfs set $prop=$expect_value $dataset
93
94		[[ -n $prop ]] && cur_value=$(get_prop $prop $dataset)
95
96		if [[ "$expect_value" != "" && "$cur_value" != "$old_value" ]];
97		then
98			log_fail "The '$dataset' '$prop' value '$cur_value' \
99				should equal with '$old_value'."
100		fi
101	fi
102}
103
104#
105# Cleanup all the user properties of the pool and the dataset reside it.
106#
107# $1 pool name
108#
109function cleanup_user_prop
110{
111	typeset pool=$1
112	typeset dtst=$(zfs list -H -r -o name -t filesystem,volume $pool)
113
114	typeset user_prop
115	for dt in $dtst; do
116		user_prop=$(zfs get -H -o property all $dtst | grep ":")
117
118		typeset prop
119		for prop in $user_prop; do
120			zfs inherit $prop $dt
121			(($? != 0)) && log_must zfs inherit $prop $dt
122		done
123	done
124}
125
126#
127# Random select charactor from the specified charactor set and combine into a
128# random string
129#
130# $1 character set name
131# $2 String length
132#
133function random_string
134{
135	typeset char_set=${1:-VALID_NAME_CHAR}
136	typeset -i len=${2:-5}
137
138	eval typeset -i count=\${#$char_set[@]}
139
140	# No consumers want an empty string.
141	((len == 0)) && len=3
142
143	typeset str
144	typeset -i i=0
145	while ((i < len)); do
146		typeset -i ind
147		((ind = RANDOM % count))
148		eval str=\${str}\${$char_set[\$ind]}
149
150		((i += 1))
151	done
152
153	echo "$str"
154}
155
156#
157# Get vaild user defined property name
158#
159# $1 user defined property name length
160#
161function valid_user_property
162{
163	typeset -i sumlen=${1:-10}
164	((sumlen < 2 )) && sumlen=2
165	typeset -i len
166	((len = RANDOM % sumlen))
167	typeset part1 part2
168
169	while true; do
170		part1="$(random_string VALID_NAME_CHAR $len)"
171		if [[ "$part1" == "-"* ]]; then
172			continue
173		fi
174		break
175	done
176	((len = sumlen - (len + 1)))
177
178	while true; do
179		part2="$(random_string VALID_NAME_CHAR $len)"
180		if [[ -z $part1 && -z $part2 ]]; then
181			continue
182		fi
183		break
184	done
185
186	echo "${part1}:${part2}"
187}
188
189#
190# Get invaild user defined property name
191#
192# $1 user defined property name length
193#
194function invalid_user_property
195{
196	typeset -i sumlen=${1:-10}
197	((sumlen == 0)) && sumlen=1
198	typeset -i len
199	((len = RANDOM % sumlen))
200
201	typeset part1 part2
202	while true; do
203		part1="$(random_string VALID_NAME_CHAR $len)"
204		((len = sumlen - len))
205		part2="$(random_string INVALID_NAME_CHAR $len)"
206
207		# Avoid $part1 is *:* and $part2 is "=*"
208		if [[ "$part1" == *":"* && "$part2" == "="* ]]; then
209			continue
210		fi
211		break
212	done
213
214	echo "${part1}${part2}"
215}
216
217#
218# Get user property value
219#
220# $1 user defined property name length
221#
222function user_property_value
223{
224	typeset -i len=${1:-100}
225
226	typeset value=$(random_string ALL_CHAR $len)
227
228	echo "$value"
229}
230
231#
232# Check if the user property is identical to the expected value.
233#
234# $1 dataset
235# $2 user property
236# $3 expected value
237#
238function check_user_prop
239{
240	typeset dtst=$1
241	typeset user_prop="$2"
242	typeset expect_value="$3"
243	typeset value=$(zfs get -p -H -o value "$user_prop" $dtst 2>&1)
244
245	if [[ "$expect_value" == "$value" ]]; then
246		return 0
247	else
248		return 1
249	fi
250}
251
252#
253# Get source of the dataset
254#
255function get_source
256{
257	typeset prop=$1
258	typeset dataset=$2
259	typeset source
260
261	source=$(zfs get -H -o source $prop $dataset)
262        if (($? != 0)); then
263                log_fail "Unable to get $prop source for dataset $dataset"
264        fi
265
266	echo "$source"
267}
268
269#
270# Verify property $2 is set from source $4 on dataset $1 and has value $3.
271#
272# $1 checked dataset
273# $2 user property
274# $3 property value
275# $4 source
276#
277# Returns: 0 if both expected source and value match, 1 otherwise
278#
279function check_prop_source
280{
281        typeset dataset="$1"
282        typeset prop="$2"
283        typeset value="$3"
284        typeset source="$4"
285        typeset chk_value=$(get_prop "$prop" "$dataset")
286        typeset chk_source=$(get_source "$prop" "$dataset")
287
288        if [[ "$chk_value" != "$value" || "$chk_source" != "$4" ]]
289        then
290                return 1
291        else
292                return 0
293        fi
294}
295
296#
297# Verify target dataset $1 inherit property $2 from dataset $3.
298#
299# $1 checked dataset
300# $2 property
301# $3 inherited dataset
302#
303# Returns: 0 if property has expected value and is inherited, 1 otherwise
304#
305function check_prop_inherit
306{
307        typeset checked_dtst="$1"
308        typeset prop="$2"
309        typeset inherited_dtst="$3"
310        typeset inherited_value=$(get_prop "$prop" "$inherited_dtst")
311        typeset value=$(get_prop "$prop" "$checked_dtst")
312        typeset source=$(get_source "$prop" "$checked_dtst")
313
314        if [[ "$value" != "$inherited_value" || \
315            "$source" != "inherited from $inherited_dtst" ]]
316        then
317                return 1
318        else
319                return 0
320        fi
321}
322
323#
324# Verify property $2 received value on dataset $1 has value $3
325#
326# $1 checked dataset
327# $2 property name
328# $3 checked value
329#
330# Returns: 0 if property has expected value and is received, 1 otherwise
331#
332function check_prop_received
333{
334        typeset dataset="$1"
335        typeset prop="$2"
336        typeset value="$3"
337
338        received=$(zfs get -H -o received "$prop" "$dataset")
339        if (($? != 0)); then
340                log_fail "Unable to get $prop received value for dataset " \
341                    "$dataset"
342        fi
343        if [[ "$received" == "$value" ]]
344        then
345                return 0
346        else
347                return 1
348        fi
349}
350
351#
352# Verify user property $2 is not set on dataset $1
353#
354# $1 checked dataset
355# $2 property name
356#
357# Returns: 0 if property is missing (not set), 1 otherwise
358#
359function check_prop_missing
360{
361        typeset dataset="$1"
362        typeset prop="$2"
363
364        value=$(zfs get -H -o value "$prop" "$dataset")
365        if (($? != 0)); then
366                log_fail "Unable to get $prop value for dataset $dataset"
367        fi
368        if [[ "-" == "$value" ]]
369        then
370                return 0
371        else
372                return 1
373        fi
374}
375