#!/bin/sh
# Copyright (c) 2017 Oracle and/or its affiliates. All Rights Reserved.
# Copyright (c) International Business Machines  Corp., 2000
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it would be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

TCID="rpcinfo01"
TST_TOTAL=16
TST_CLEANUP=tst_rmdir

TST_USE_LEGACY_API=1
. tst_net.sh

do_setup()
{
	tst_resm TINFO "Checking for portmap or rpcbind"

	if pgrep portmap > /dev/null; then
		PORTMAPPER="portmap"
	else
		pgrep rpcbind > /dev/null && PORTMAPPER="rpcbind" || \
			tst_brkm TCONF "portmap or rpcbind is not running"
	fi

	tst_tmpdir

	# Create file with 1 tcp and 1 udp line. Use for variable assignments.
	rpcinfo -p $(tst_ipaddr) | grep tcp | sed -n 2p > rpc_out
	rpcinfo -p $(tst_ipaddr) | grep udp | sed -n 2p >> rpc_out

	wc -l rpc_out | grep "2" > /dev/null || \
		tst_brkm TBROK "Not enough programs registered on $(tst_ipaddr)"

	# Using "rpc_out" file created above, assign variables for rpcinfo opts
	TPNUM=`grep tcp rpc_out | awk '{print $1}'`
	TVNUM=`grep tcp rpc_out | awk '{print $2}'`
	TCPNAME=`grep tcp rpc_out | awk '{print $5}'`
	UPNUM=`grep udp rpc_out | awk '{print $1}'`
	UVNUM=`grep udp rpc_out | awk '{print $2}'`
	UDPNAME=`grep udp rpc_out | awk '{print $5}'`
}

do_test()
{
	local thost="$(tst_ipaddr)"

	EXPECT_RHOST_PASS rpcinfo -p $thost | grep -q portmapper
	EXPECT_RHOST_PASS rpcinfo -t $thost $TPNUM
	EXPECT_RHOST_PASS rpcinfo -t $thost $TPNUM $TVNUM
	EXPECT_RHOST_PASS rpcinfo -t $thost $TCPNAME
	EXPECT_RHOST_PASS rpcinfo -t $thost $TCPNAME $TVNUM
	EXPECT_RHOST_PASS rpcinfo -u $thost 100000
	EXPECT_RHOST_PASS rpcinfo -u $thost 100000 2
	EXPECT_RHOST_PASS rpcinfo -u $thost portmapper
	EXPECT_RHOST_PASS rpcinfo -u $thost portmapper 2

	tst_resm TINFO "Test rpcinfo with missing or bad options"
	EXPECT_RHOST_FAIL rpcinfo -p bogushost
	EXPECT_RHOST_FAIL rpcinfo -bogusflag
	EXPECT_RHOST_FAIL rpcinfo -t $thost
	EXPECT_RHOST_FAIL rpcinfo -u $thost
	EXPECT_RHOST_FAIL rpcinfo -u $thost bogusprog
	EXPECT_RHOST_FAIL rpcinfo -u $thost 11579
	EXPECT_RHOST_FAIL rpcinfo -u $thost 100000 5
}

do_setup
do_test

tst_exit