1#!/usr/local/bin/ksh93 -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 2007 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26
27. $STF_SUITE/tests/cli_root/cli_common.kshlib
28
29#################################################################################
30#
31# __stc_assertion_start
32#
33# ID: zfs_receive_009_neg
34#
35# DESCRIPTION:
36#	Verify 'zfs receive' fails with bad options, missing argument or too many
37#	arguments.
38#
39# STRATEGY:
40#	1. Set a array of illegal arguments
41#	2. Execute 'zfs receive' with illegal arguments
42#	3. Verify the command should be failed
43#
44# TESTABILITY: explicit
45#
46# TEST_AUTOMATION_LEVEL: automated
47#
48# CODING_STATUS: COMPLETED (2007-06-20)
49#
50# __stc_assertion_end
51#
52################################################################################
53
54verify_runnable "both"
55
56function cleanup
57{
58	typeset ds
59
60	if snapexists $snap; then
61		log_must $ZFS destroy $snap
62	fi
63	for ds in $ctr1 $ctr2 $fs1; do
64		if datasetexists $ds; then
65			log_must $ZFS destroy -rf $ds
66		fi
67	done
68	if [[ -d $TESTDIR2 ]]; then
69		$RM -rf $TESTDIR2
70	fi
71}
72
73log_assert "Verify 'zfs receive' fails with bad option, missing or too many arguments"
74log_onexit cleanup
75
76set -A badopts "v" "n" "F" "d" "-V" "-N" "-f" "-D" "-VNfD" "-vNFd" "-vnFD" "-dVnF" \
77		"-vvvNfd" "-blah" "-12345" "-?" "-*" "-%"
78set -A validopts "" "-v" "-n" "-F" "-vn" "-nF" "-vnF" "-vd" "-nd" "-Fd" "-vnFd"
79
80ctr1=$TESTPOOL/$TESTCTR1
81ctr2=$TESTPOOL/$TESTCTR2
82fs1=$TESTPOOL/$TESTFS1
83fs2=$TESTPOOL/$TESTFS2
84fs3=$TESTPOOL/$TESTFS3
85snap=$TESTPOOL/$TESTFS@$TESTSNAP
86bkup=$TESTDIR2/bkup.${TESTCASE_ID}
87
88# Preparations for negative testing
89for ctr in $ctr1 $ctr2; do
90	log_must $ZFS create $ctr
91done
92if [[ -d $TESTDIR2 ]]; then
93	$RM -rf $TESTDIR2
94fi
95log_must $ZFS create -o mountpoint=$TESTDIR2 $fs1
96log_must $ZFS snapshot $snap
97log_must eval "$ZFS send $snap > $bkup"
98
99#Testing zfs receive fails with input from terminal
100log_mustnot eval "$ZFS recv $fs3 </dev/console"
101
102# Testing with missing argument and too many arguments
103typeset -i i=0
104while (( i < ${#validopts[*]} )); do
105	log_mustnot eval "$ZFS recv < $bkup"
106
107	$ECHO ${validopts[i]} | $GREP "d" >/dev/null 2>&1
108	if (( $? != 0 )); then
109		log_mustnot eval "$ZFS recv ${validopts[i]} $fs2 $fs3 < $bkup"
110	else
111		log_mustnot eval "$ZFS recv ${validopts[i]} $ctr1 $ctr2 < $bkup"
112	fi
113
114	(( i += 1 ))
115done
116
117# Testing with bad options
118i=0
119while (( i < ${#badopts[*]} ))
120do
121	log_mustnot eval "$ZFS recv ${badopts[i]} $ctr1 < $bkup"
122	log_mustnot eval "$ZFS recv ${badopts[i]} $fs2 < $bkup"
123
124	(( i = i + 1 ))
125done
126
127log_pass "'zfs receive' as expected with bad options, missing or too many arguments."
128