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		wait_freeing
97
98		if [[ "$expect_value" != "" && "$cur_value" != "$old_value" ]];
99		then
100			log_fail "The '$dataset' '$prop' value '$cur_value'" \
101				"should equal '$old_value'."
102		fi
103	fi
104}
105
106#
107# Cleanup all the user properties of the pool and the dataset reside it.
108#
109# $1 pool name
110#
111function cleanup_user_prop
112{
113	typeset pool=$1
114	typeset dtst=$(zfs list -H -r -o name -t filesystem,volume $pool)
115
116	typeset user_prop
117	for dt in $dtst; do
118		user_prop=$(zfs get -H -o property all $dtst | grep ":")
119
120		typeset prop
121		for prop in $user_prop; do
122			zfs inherit $prop $dt ||
123				log_must zfs inherit $prop $dt
124		done
125	done
126}
127
128#
129# Random select character from the specified character set and combine into a
130# random string
131#
132# $1 character set name
133# $2 String length
134#
135function random_string
136{
137	typeset char_set=${1:-VALID_NAME_CHAR}
138	typeset -i len=${2:-5}
139
140	eval typeset -i count=\${#$char_set[@]}
141
142	# No consumers want an empty string.
143	((len == 0)) && len=3
144
145	typeset str
146	typeset -i i=0
147	while ((i < len)); do
148		typeset -i ind
149		((ind = RANDOM % count))
150		eval str=\${str}\${$char_set[\$ind]}
151
152		((i += 1))
153	done
154
155	echo "$str"
156}
157
158#
159# Get valid user defined property name
160#
161# $1 user defined property name length
162#
163function valid_user_property
164{
165	typeset -i sumlen=${1:-10}
166	((sumlen < 2 )) && sumlen=2
167	typeset -i len
168	((len = RANDOM % sumlen))
169	typeset part1 part2
170
171	while true; do
172		part1="$(random_string VALID_NAME_CHAR $len)"
173		if [[ "$part1" == "-"* ]]; then
174			continue
175		fi
176		break
177	done
178	((len = sumlen - (len + 1)))
179
180	while true; do
181		part2="$(random_string VALID_NAME_CHAR $len)"
182		if [[ -z $part1 && -z $part2 ]]; then
183			continue
184		fi
185		break
186	done
187
188	echo "${part1}:${part2}"
189}
190
191#
192# Get invalid user defined property name
193#
194# $1 user defined property name length
195#
196function invalid_user_property
197{
198	typeset -i sumlen=${1:-10}
199	((sumlen == 0)) && sumlen=1
200	typeset -i len
201	((len = RANDOM % sumlen))
202
203	typeset part1 part2
204	while true; do
205		part1="$(random_string VALID_NAME_CHAR $len)"
206		((len = sumlen - len))
207		part2="$(random_string INVALID_NAME_CHAR $len)"
208
209		# Avoid $part1 is *:* and $part2 is "=*"
210		if [[ "$part1" == *":"* && "$part2" == "="* ]]; then
211			continue
212		fi
213		break
214	done
215
216	echo "${part1}${part2}"
217}
218
219#
220# Get user property value
221#
222# $1 user defined property name length
223#
224function user_property_value
225{
226	typeset -i len=${1:-100}
227
228	random_string ALL_CHAR $len
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	[ "$expect_value" = "$value" ]
246}
247
248#
249# Get source of the dataset
250#
251function get_source
252{
253	typeset prop=$1
254	typeset dataset=$2
255
256	zfs get -H -o source $prop $dataset ||
257                log_fail "Unable to get $prop source for dataset $dataset"
258}
259
260#
261# Verify property $2 is set from source $4 on dataset $1 and has value $3.
262#
263# $1 checked dataset
264# $2 user property
265# $3 property value
266# $4 source
267#
268# Returns: 0 if both expected source and value match, 1 otherwise
269#
270function check_prop_source
271{
272        typeset dataset="$1"
273        typeset prop="$2"
274        typeset value="$3"
275        typeset source="$4"
276        typeset chk_value=$(get_prop "$prop" "$dataset")
277        typeset chk_source=$(get_source "$prop" "$dataset")
278
279	if [[ "$chk_value" != "$value" || "$chk_source" != "$source" ]]
280	then
281		log_note "expected (value '$value', source '$source')," \
282			"got (value '$chk_value', source '$chk_source')"
283		return 1
284	else
285		return 0
286	fi
287}
288
289#
290# Verify target dataset $1 inherit property $2 from dataset $3.
291#
292# $1 checked dataset
293# $2 property
294# $3 inherited dataset
295#
296# Returns: 0 if property has expected value and is inherited, 1 otherwise
297#
298function check_prop_inherit
299{
300        typeset checked_dtst="$1"
301        typeset prop="$2"
302        typeset inherited_dtst="$3"
303        typeset inherited_value=$(get_prop "$prop" "$inherited_dtst")
304        typeset value=$(get_prop "$prop" "$checked_dtst")
305        typeset source=$(get_source "$prop" "$checked_dtst")
306
307        [ "$value" = "$inherited_value" ] &&
308            [ "$source" = "inherited from $inherited_dtst" ]
309}
310
311#
312# Verify property $2 received value on dataset $1 has value $3
313#
314# $1 checked dataset
315# $2 property name
316# $3 checked value
317#
318# Returns: 0 if property has expected value and is received, 1 otherwise
319#
320function check_prop_received
321{
322        typeset dataset="$1"
323        typeset prop="$2"
324        typeset value="$3"
325
326        received=$(zfs get -H -o received "$prop" "$dataset") ||
327                log_fail "Unable to get $prop received value for dataset $dataset"
328        [ "$received" = "$value" ]
329}
330
331#
332# Verify user property $2 is not set on dataset $1
333#
334# $1 checked dataset
335# $2 property name
336#
337# Returns: 0 if property is missing (not set), 1 otherwise
338#
339function check_prop_missing
340{
341        typeset dataset="$1"
342        typeset prop="$2"
343
344        value=$(zfs get -H -o value "$prop" "$dataset") ||
345                log_fail "Unable to get $prop value for dataset $dataset"
346        [ "$value" = "-" ]
347}
348