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