#!/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 route messages to the chosen destination.

testname="Destination test"
echo $testname

failcount=0
tempfile=@abs_builddir@/destination_test_tempfile_$$
destfile1_tmp=@abs_builddir@/destination_test_destfile_1_tmp_$$
destfile2_tmp=@abs_builddir@/destination_test_destfile_2_tmp_$$
destfile1=@abs_builddir@/destination_test_destfile_1_$$
destfile2=@abs_builddir@/destination_test_destfile_2_$$

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

echo "1. One logger, multiple destinations:"
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
.
rm -f $destfile1 $destfile2
./logger_example -s error -f $destfile1_tmp -f $destfile2_tmp

# strip the pids
sed -e 's/\[\([a-z0-9\.]\{1,\}\)\/\([0-9]\{1,\}\)\]/[\1]/' < $destfile1_tmp > $destfile1
sed -e 's/\[\([a-z0-9\.]\{1,\}\)\/\([0-9]\{1,\}\)\]/[\1]/' < $destfile2_tmp > $destfile2

echo -n  "   - destination 1:"
cut -d' ' -f3- $destfile1 | diff $tempfile -
passfail $?

echo -n  "   - destination 2:"
cut -d' ' -f3- $destfile2 | diff $tempfile -
passfail $?

# Tidy up.
rm -f $tempfile $destfile1_tmp $destfile2_tmp $destfile1 $destfile2

echo     "2. Two loggers, different destinations and severities"
rm -f $destfile1 $destfile2
./logger_example -l example -s info -f $destfile1_tmp -l alpha -s warn -f $destfile2_tmp

# strip the pids
sed -e 's/\[\([a-z0-9\.]\{1,\}\)\/\([0-9]\{1,\}\)\]/[\1]/' < $destfile1_tmp > $destfile1
sed -e 's/\[\([a-z0-9\.]\{1,\}\)\/\([0-9]\{1,\}\)\]/[\1]/' < $destfile2_tmp > $destfile2

# All output for example and example.beta should have gone to destfile1.
# Output for example.alpha should have done to destfile2.

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
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
.
echo -n  "   - destination 1:"
cut -d' ' -f3- $destfile1 | diff $tempfile -
passfail $?

echo -n  "   - destination 2:"
cat > $tempfile << .
WARN  [example.alpha] LOG_READ_ERROR error reading from message file a.txt: dummy reason
.
cut -d' ' -f3- $destfile2 | 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 $destfile1_tmp $destfile2_tmp $destfile1 $destfile2

exit $failcount
