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 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#
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
66typeset exclude=`eval echo \"'(${KEEP})'\"`
67for pool in $(zpool list -H -o name | \
68	egrep -v "$exclude" | \
69	grep -v "$TESTPOOL" | \
70	egrep -v "$NO_POOLS"); do
71	log_must zpool destroy $pool
72done
73
74DISK=${DISKS%% *}
75if [[ ! -e $TESTDIR ]]; then
76        log_must mkdir $TESTDIR
77fi
78
79log_note "Ensure invalid characters fail"
80for POOLNAME in "!" "\"" "#" "$" "%" "&" "'" "(" ")" \
81    "\*" "+" "," "-" "\." "/" "\\" \
82    0 1 2 3 4 5 6 7 8 9 \
83    ":" ";" "<" "=" ">" "\?" "@" \
84    "[" "]" "^" "_" "\`" "{" "|" "}" "~"
85do
86	log_mustnot zpool create -m $TESTDIR $POOLNAME $DISK
87        if poolexists $POOLNAME; then
88                log_fail "Unexpectedly created pool: '$POOLNAME'"
89        fi
90
91	log_mustnot zpool destroy $POOLNAME
92done
93
94log_note "Check that invalid octal values fail"
95for oct in "\000" "\001" "\002" "\003" "\004" "\005" "\006" "\007" \
96    "\010" "\011" "\012" "\013" "\014" "\015" "\017" \
97    "\020" "\021" "\022" "\023" "\024" "\025" "\026" "\027" \
98    "\030" "\031" "\032" "\033" "\034" "\035" "\036" "\037" \
99    "\040" "\177"
100do
101	POOLNAME=`eval "echo x | tr 'x' '$oct'"`
102	log_mustnot zpool create -m $TESTDIR $POOLNAME $DISK
103        if poolexists $POOLNAME; then
104                log_fail "Unexpectedly created pool: '$POOLNAME'"
105        fi
106
107	log_mustnot zpool destroy $POOLNAME
108done
109
110log_note "Verify invalid pool names fail"
111set -A POOLNAME "c0t0d0s0" "c0t0d0" "c0t0d19" "c0t50000E0108D279d0" \
112    "mirror" "raidz" ",," ",,,,,,,,,,,,,,,,,,,,,,,,," \
113    "2222222222222222222" "mirror_pool" "raidz_pool" \
114    "mirror-pool" "raidz-pool" "spare" "spare_pool" \
115    "spare-pool" "raidz1-" "raidz2:" ":aaa" "-bbb" "_ccc" ".ddd"
116if verify_slog_support ; then
117	POOLNAME[${#POOLNAME[@]}]='log'
118fi
119typeset -i i=0
120while ((i < ${#POOLNAME[@]})); do
121	log_mustnot zpool create -m $TESTDIR ${POOLNAME[$i]} $DISK
122        if poolexists ${POOLNAME[$i]}; then
123                log_fail "Unexpectedly created pool: '${POOLNAME[$i]}'"
124        fi
125
126	log_mustnot zpool destroy ${POOLNAME[$i]}
127
128	((i += 1))
129done
130
131log_pass "Invalid names and characters were caught correctly"
132