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 2007 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27
28#
29# Copyright (c) 2016 by Delphix. All rights reserved.
30#
31
32. $STF_SUITE/tests/functional/cli_root/cli_common.kshlib
33
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
45verify_runnable "both"
46
47function cleanup
48{
49	typeset ds
50
51	if snapexists $snap; then
52		log_must zfs destroy $snap
53	fi
54	for ds in $ctr1 $ctr2 $fs1; do
55		if datasetexists $ds; then
56			log_must zfs destroy -rf $ds
57		fi
58	done
59	if [[ -d $TESTDIR2 ]]; then
60		rm -rf $TESTDIR2
61	fi
62}
63
64log_assert "Verify 'zfs receive' fails with bad option, missing or too many arguments"
65log_onexit cleanup
66
67set -A badopts "v" "n" "F" "d" "-V" "-N" "-f" "-D" "-VNfD" "-vNFd" "-vnFD" "-dVnF" \
68		"-vvvNfd" "-blah" "-12345" "-?" "-*" "-%"
69set -A validopts "" "-v" "-n" "-F" "-vn" "-nF" "-vnF" "-vd" "-nd" "-Fd" "-vnFd"
70
71ctr1=$TESTPOOL/$TESTCTR1
72ctr2=$TESTPOOL/$TESTCTR2
73fs1=$TESTPOOL/$TESTFS1
74fs2=$TESTPOOL/$TESTFS2
75fs3=$TESTPOOL/$TESTFS3
76snap=$TESTPOOL/$TESTFS@$TESTSNAP
77bkup=$TESTDIR2/bkup.$$
78
79# Preparations for negative testing
80for ctr in $ctr1 $ctr2; do
81	log_must zfs create $ctr
82done
83if [[ -d $TESTDIR2 ]]; then
84	rm -rf $TESTDIR2
85fi
86log_must zfs create -o mountpoint=$TESTDIR2 $fs1
87log_must zfs snapshot $snap
88log_must eval "zfs send $snap > $bkup"
89
90#Testing zfs receive fails with input from terminal
91log_mustnot eval "zfs recv $fs3 </dev/console"
92
93# Testing with missing argument and too many arguments
94typeset -i i=0
95while (( i < ${#validopts[*]} )); do
96	log_mustnot eval "zfs recv < $bkup"
97
98	echo ${validopts[i]} | grep "d" >/dev/null 2>&1
99	if (( $? != 0 )); then
100		log_mustnot eval "zfs recv ${validopts[i]} $fs2 $fs3 < $bkup"
101	else
102		log_mustnot eval "zfs recv ${validopts[i]} $ctr1 $ctr2 < $bkup"
103	fi
104
105	(( i += 1 ))
106done
107
108# Testing with bad options
109i=0
110while (( i < ${#badopts[*]} ))
111do
112	log_mustnot eval "zfs recv ${badopts[i]} $ctr1 < $bkup"
113	log_mustnot eval "zfs recv ${badopts[i]} $fs2 < $bkup"
114
115	(( i = i + 1 ))
116done
117
118log_pass "'zfs receive' as expected with bad options, missing or too many arguments."
119