1#!/bin/ksh -p
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 https://opensource.org/licenses/CDDL-1.0.
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 2008 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27
28#
29# Copyright (c) 2013, 2016 by Delphix. All rights reserved.
30#
31
32. $STF_SUITE/include/libtest.shlib
33
34#
35# DESCRIPTION:
36#
37# Ensure that a set of invalid names cannot be used to create pools.
38#
39# STRATEGY:
40# 1) For each invalid character in the character set, try to create
41# and destroy the pool. Verify it fails.
42# 2) Given a list of invalid pool names, ensure the pools are not
43# created.
44#
45
46verify_runnable "global"
47
48log_assert "Ensure that a set of invalid names cannot be used to create pools."
49
50# Global variable use to cleanup failures.
51POOLNAME=""
52
53function cleanup
54{
55	if poolexists $POOLNAME; then
56		log_must zpool destroy $POOLNAME
57	fi
58
59	if [[ -d $TESTDIR ]]; then
60		log_must rm -rf $TESTDIR
61	fi
62}
63
64log_onexit cleanup
65
66for pool in $(get_all_pools); do
67	if [[ "$pool" != "$TESTPOOL" ]]; then
68		log_must zpool destroy $pool
69	fi
70done
71
72DISK=${DISKS%% *}
73if [[ ! -e $TESTDIR ]]; then
74        log_must mkdir $TESTDIR
75fi
76
77log_note "Ensure invalid characters fail"
78for POOLNAME in "!" "\"" "#" "$" "%" "&" "'" "(" ")" \
79    "\*" "+" "," "-" "\." "/" "\\" \
80    0 1 2 3 4 5 6 7 8 9 \
81    ":" ";" "<" "=" ">" "\?" "@" \
82    "[" "]" "^" "_" "\`" "{" "|" "}" "~"
83do
84	log_mustnot zpool create -m $TESTDIR $POOLNAME $DISK
85        if poolexists $POOLNAME; then
86                log_fail "Unexpectedly created pool: '$POOLNAME'"
87        fi
88
89	log_mustnot zpool destroy $POOLNAME
90done
91
92log_note "Check that invalid octal values fail"
93for oct in "\000" "\001" "\002" "\003" "\004" "\005" "\006" "\007" \
94    "\010" "\011" "\012" "\013" "\014" "\015" "\017" \
95    "\020" "\021" "\022" "\023" "\024" "\025" "\026" "\027" \
96    "\030" "\031" "\032" "\033" "\034" "\035" "\036" "\037" \
97    "\040" "\177"
98do
99	POOLNAME=`eval "echo x | tr 'x' '$oct'"`
100	log_mustnot zpool create -m $TESTDIR $POOLNAME $DISK
101        if poolexists $POOLNAME; then
102                log_fail "Unexpectedly created pool: '$POOLNAME'"
103        fi
104
105	log_mustnot zpool destroy $POOLNAME
106done
107
108log_note "Verify invalid pool names fail"
109set -A POOLNAME \
110    "mirror" "raidz" ",," ",,,,,,,,,,,,,,,,,,,,,,,,," \
111    "2222222222222222222" "mirror_pool" "raidz_pool" \
112    "mirror-pool" "raidz-pool" "spare" "spare_pool" \
113    "spare-pool" "raidz1-" "raidz2:" ":aaa" "-bbb" "_ccc" ".ddd"
114
115POOLNAME[${#POOLNAME[@]}]='log'
116
117typeset -i i=0
118while ((i < ${#POOLNAME[@]})); do
119	log_mustnot zpool create -m $TESTDIR ${POOLNAME[$i]} $DISK
120        if poolexists ${POOLNAME[$i]}; then
121                log_fail "Unexpectedly created pool: '${POOLNAME[$i]}'"
122        fi
123
124	log_mustnot zpool destroy ${POOLNAME[$i]}
125
126	((i += 1))
127done
128
129log_pass "Invalid names and characters were caught correctly"
130