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 https://opensource.org/licenses/CDDL-1.0.
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# Copyright (c) 2023 by Klara Inc.
30#
31
32. $STF_SUITE/include/libtest.shlib
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    '}' ';' '"' '<' ',' '>' '?' '/' ' '
39set -A ALL_CHAR ${VALID_NAME_CHAR[*]} ${INVALID_NAME_CHAR[*]}
40
41#
42# Cleanup all the user properties of the pool.
43#
44# $1 pool name
45#
46function cleanup_user_prop
47{
48	typeset pool=$1
49
50	typeset user_prop
51	user_prop=$(zpool get -H -o property all $pool | grep ":")
52
53	typeset prop
54	for prop in $user_prop; do
55		zpool set $prop="" $pool ||
56			log_must zpool set $prop="" $pool
57	done
58}
59
60#
61# Random select character from the specified character set and combine into a
62# random string
63#
64# $1 character set name
65# $2 String length
66#
67function random_string
68{
69	typeset char_set=${1:-VALID_NAME_CHAR}
70	typeset -i len=${2:-5}
71
72	eval typeset -i count=\${#$char_set[@]}
73
74	# No consumers want an empty string.
75	((len == 0)) && len=3
76
77	typeset str
78	typeset -i i=0
79	while ((i < len)); do
80		typeset -i ind
81		((ind = RANDOM % count))
82		eval str=\${str}\${$char_set[\$ind]}
83
84		((i += 1))
85	done
86
87	echo "$str"
88}
89
90#
91# Get valid user-defined property name
92#
93# $1 user-defined property name length
94#
95function valid_user_property
96{
97	typeset -i sumlen=${1:-10}
98	((sumlen < 2 )) && sumlen=2
99	typeset -i len
100	((len = RANDOM % sumlen))
101	typeset part1 part2
102
103	while true; do
104		part1="$(random_string VALID_NAME_CHAR $len)"
105		if [[ "$part1" == "-"* ]]; then
106			continue
107		fi
108		break
109	done
110	((len = sumlen - (len + 1)))
111
112	while true; do
113		part2="$(random_string VALID_NAME_CHAR $len)"
114		if [[ -z $part1 && -z $part2 ]]; then
115			continue
116		fi
117		break
118	done
119
120	echo "${part1}:${part2}"
121}
122
123#
124# Get invalid user-defined property name
125#
126# $1 user-defined property name length
127#
128function invalid_user_property
129{
130	typeset -i sumlen=${1:-10}
131	((sumlen == 0)) && sumlen=1
132	typeset -i len
133	((len = RANDOM % sumlen))
134
135	typeset part1 part2
136	while true; do
137		part1="$(random_string VALID_NAME_CHAR $len)"
138		((len = sumlen - len))
139		part2="$(random_string INVALID_NAME_CHAR $len)"
140
141		# Avoid $part1 is *:* and $part2 is "=*"
142		if [[ "$part1" == *":"* && "$part2" == "="* ]]; then
143			continue
144		fi
145		break
146	done
147
148	echo "${part1}${part2}"
149}
150
151#
152# Get user-defined property value
153#
154# $1 user-defined property name length
155#
156function user_property_value
157{
158	typeset -i len=${1:-100}
159
160	random_string ALL_CHAR $len
161}
162
163#
164# Check if the user-defined property is identical to the expected value.
165#
166# $1 pool
167# $2 user property
168# $3 expected value
169#
170function check_user_prop
171{
172	typeset pool=$1
173	typeset user_prop="$2"
174	typeset expect_value="$3"
175	typeset value=$(zpool get -p -H -o value "$user_prop" $pool 2>&1)
176
177	[ "$expect_value" = "$value" ]
178}
179