#!/bin/sh
#  Copyright (c) International Business Machines  Corp., 2002
#
#  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 will 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, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# 12/05/02  Port to bash -Robbie Williamson <robbiew@us.ibm.com>
# 02/05/03  Modified - Manoj Iyer <manjo@mail.utexas.edu> use USCTEST macros
#	   fixed bugs.
# 07/26/05  Michael Reed  <mreedltp@vnet.ibm.com>
#	   Made changes to account for the replacement of syslogd
#	   with syslog-ng
#
##################################################################
# case 10: Test setlogmask() with LOG_MASK macro.		#
#								#
#	 o Use setlogmask() with LOG_MASK macro to set an       #
#		individual priority level.			  #
#	 o Send the message of above prority and expect it to   #
#	   be logged.					   #
#	 o Send message which is below the priority level to    #
#	   the one set above, which should not be logged.       #
##################################################################

. syslog-lib.sh || exit 1

syslog_case10()
{
	tst_resm TINFO "syslog: Testing setlogmask() with LOG_MASK macro..."

	# Create the configuration file specific to this test case.
	case "$CONFIG_FILE" in
	/etc/syslog.conf|/etc/rsyslog.conf)
		echo "$RSYSLOG_CONFIG" > $CONFIG_FILE
		echo "user.debug       /var/log/messages" >> $CONFIG_FILE
		;;

	/etc/syslog-ng/syslog-ng.conf)
		echo "source src{ internal(); unix-dgram(\"/dev/log\"); udp(ip(\"0.0.0.0\") port(514)); };" > $CONFIG_FILE
		echo "filter f_syslog_debug{ facility(user); };" >> $CONFIG_FILE
		echo "destination syslog_messages { file(\"/var/log/messages\");};" >> $CONFIG_FILE
		echo "log { source(src); filter(f_syslog_debug); destination(syslog_messages); };" >> $CONFIG_FILE
		;;

	esac

	restart_syslog_daemon

	if [ -e /var/log/messages ]; then
		allow1=`grep -c "syslogtst:10 error level is logged" /var/log/messages`
		donot_allow1=`grep -c "syslogtst:10 warning level not to be logged" /var/log/messages`
	else
		allow1=0
		donot_allow1=0
	fi

	if ! syslogtst 10 2>/dev/null; then
		status_flag=1
		return
	fi
	sleep 2

	# check if /var/log/messages script exists
	if [ ! -e /var/log/messages ]; then
		tst_resm TBROK "/var/log/messages no such log file"
		cleanup 1
	fi

	allow2=`grep -c "syslogtst:10 error level is logged" /var/log/messages`
	donot_allow2=`grep -c "syslogtst:10 warning level not to be logged" /var/log/messages`

	diff1=$(( $allow2 - $allow1 ))
	if [ $diff1 -ne 1 ]; then
		tst_resm TFAIL "Expected message was not logged...."
		status_flag=1
		return
	fi

	diff2=$(( $donot_allow2 - $donot_allow1 ))
	if [ $diff2 -ne 0 ]; then
		tst_resm TFAIL "Unexpected message was logged..."
		status_flag=1
	fi

}

tst_resm TINFO " Test setlogmask() with LOG_MASK macro."
tst_resm TINFO " o Use setlogmask() with LOG_MASK macro to set an"
tst_resm TINFO "   individual priority level."
tst_resm TINFO " o Send the message of above prority and expect it to be"
tst_resm TINFO "   logged."
tst_resm TINFO " o Send message which is at other priority level to"
tst_resm TINFO "   the one set above, which should not be logged."

setup
syslog_case10
cleanup ${status_flag:=0}