#!/bin/sh
# Copyright (C) 2011  Internet Systems Consortium, Inc. ("ISC")
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.

# Checks that the logger will limit the output of messages less severe than
# the severity/debug setting.

testname="Severity test"
echo $testname

failcount=0
tempfile=@abs_builddir@/severity_test_tempfile_$$

passfail() {
    if [ $1 -eq 0 ]; then
        echo " pass"
    else
        echo " FAIL"
        failcount=`expr $failcount + $1`
    fi
}

echo -n "1. Default parameters:"
cat > $tempfile << .
FATAL [example] LOG_WRITE_ERROR error writing to test1: 42
ERROR [example] LOG_READING_LOCAL_FILE reading local message file dummy/file
WARN  [example] LOG_BAD_STREAM bad log console output stream: example
WARN  [example.alpha] LOG_READ_ERROR error reading from message file a.txt: dummy reason
INFO  [example.alpha] LOG_INPUT_OPEN_FAIL unable to open message file example.msg for input: dummy reason
FATAL [example.beta] LOG_BAD_SEVERITY unrecognized log severity: beta_fatal
ERROR [example.beta] LOG_BAD_DESTINATION unrecognized log destination: beta_error
WARN  [example.beta] LOG_BAD_STREAM bad log console output stream: beta_warn
INFO  [example.beta] LOG_READ_ERROR error reading from message file beta: info
.
./logger_example -c stdout | cut -d' ' -f3- | diff $tempfile -
passfail $?

echo -n "2. Severity filter:"
cat > $tempfile << .
FATAL [example] LOG_WRITE_ERROR error writing to test1: 42
ERROR [example] LOG_READING_LOCAL_FILE reading local message file dummy/file
FATAL [example.beta] LOG_BAD_SEVERITY unrecognized log severity: beta_fatal
ERROR [example.beta] LOG_BAD_DESTINATION unrecognized log destination: beta_error
.
./logger_example -c stdout -s error | cut -d' ' -f3- | diff $tempfile -
passfail $?

echo -n "3. Debug level:"
cat > $tempfile << .
FATAL [example] LOG_WRITE_ERROR error writing to test1: 42
ERROR [example] LOG_READING_LOCAL_FILE reading local message file dummy/file
WARN  [example] LOG_BAD_STREAM bad log console output stream: example
WARN  [example.alpha] LOG_READ_ERROR error reading from message file a.txt: dummy reason
INFO  [example.alpha] LOG_INPUT_OPEN_FAIL unable to open message file example.msg for input: dummy reason
DEBUG [example] LOG_READING_LOCAL_FILE reading local message file example/0
DEBUG [example] LOG_READING_LOCAL_FILE reading local message file example/24
DEBUG [example] LOG_READING_LOCAL_FILE reading local message file example/25
FATAL [example.beta] LOG_BAD_SEVERITY unrecognized log severity: beta_fatal
ERROR [example.beta] LOG_BAD_DESTINATION unrecognized log destination: beta_error
WARN  [example.beta] LOG_BAD_STREAM bad log console output stream: beta_warn
INFO  [example.beta] LOG_READ_ERROR error reading from message file beta: info
DEBUG [example.beta] LOG_BAD_SEVERITY unrecognized log severity: beta/25
.
./logger_example -c stdout -s debug -d 25 | cut -d' ' -f3- | diff $tempfile -
passfail $?

if [ $failcount -eq 0 ]; then
    echo "PASS: $testname"
elif [ $failcount -eq 1 ]; then
    echo "FAIL: $testname - 1 test failed"
else
    echo "FAIL: $testname - $failcount tests failed"
fi

# Tidy up
rm -f $tempfile

exit $failcount
