#!/bin/sh

source dlog_test_header

trap cleanup 0

export DLOG_CONFIG_PATH="/usr/share/dlog-$type.conf.test"

if [ $LOGGER -eq -1 ]; then
	dlog_logger -t 0 &
	LOGGER=$!
	sleep 1
fi

LOG_DETAILS="testing if pid filtering works"
dlogsend -b main -t DLOG_TESTSUITE pidTEST &
sleep 1
line=$(dlogutil -v raw -d --pid $!)
[ "$line" = "pidTEST" ] && ok || fail

LOG_DETAILS="testing if tid filtering works"
dlogsend -b main -t DLOG_TESTSUITE tidTEST &
sleep 1
line=$(dlogutil -v raw -d --tid $!) #dlogsend is a single threaded app so tid is the same as pid
[ "$line" = "tidTEST" ] && ok || fail

mv $DLOG_CONFIG_PATH $DLOG_CONFIG_PATH.1
LOG_DETAILS="testing if secure logging works (1/2)"
grep -v enable_secure_logs $DLOG_CONFIG_PATH.1 > $DLOG_CONFIG_PATH
echo "enable_secure_logs=1" >> $DLOG_CONFIG_PATH
dlogutil -c
dlogsend -zb main -t DLOG_TESTSUITE secure &
sleep 1
dlogsend -b main -t DLOG_TESTSUITE insecure &
sleep 1
line=$(dlogutil -v raw -d | head -n1)
[ "$line" = "secure" ] && ok || fail
LOG_DETAILS="testing if secure logging works (2/2)"
grep -v enable_secure_logs $DLOG_CONFIG_PATH.1 > $DLOG_CONFIG_PATH
echo "enable_secure_logs=0" >> $DLOG_CONFIG_PATH
dlogutil -c
dlogsend -zb main -t DLOG_TESTSUITE secure &
sleep 1
dlogsend -b main -t DLOG_TESTSUITE insecure &
sleep 1
line=$(dlogutil -v raw -d | head -n1)
[ "$line" = "insecure" ] && ok || fail
mv $DLOG_CONFIG_PATH.1 $DLOG_CONFIG_PATH

if [ $LOGGER -ne -1 ]; then
	kill $LOGGER > /dev/null
	sleep 1
fi
