#!/bin/sh 

rm -rf allout.cn allout.sha

echo 'running ABAC testing...'
echo '  ---with sha'
env ./runall run 1> allout.sha 2>/dev/null
echo '  ---with cn'
env ABAC_CN=1 ./runall run 1> allout.cn 2>/dev/null
result=`diff allout.cn allout.save`

if [ "$result" = "" ]; then
   echo "   PASSED"
else
   echo "   ERROR:got changes!!!"
fi
echo "  "

## special case, run python script in one of the directory
## to test the python/swig/libabac linkup
echo "running special python testing in access_rt2_typed..."

cd access_rt2_typed
rm -rf python.cn

g_cmd_sha=`grep "cmd1:" good_query.py |sed "s/cmd1://g" `
g_cmd_cn=`grep "cmd2:" good_query.py |sed "s/cmd2://g" `
b_cmd_sha=`grep "cmd1:" bad_query.py |sed "s/cmd1://g" `
b_cmd_cn=`grep "cmd2:" bad_query.py |sed "s/cmd2://g" `

echo '  ---good query with sh'
eval $g_cmd_sha 2>/dev/null 1| grep 'YAP query succeed' >/dev/null
if [ $? -eq 1 ]; then
   echo  "   ERROR:python test failed for ($g_cmd_sha)"
else
   echo "   PASSED"
fi

echo '  ---good query with cn'
eval $g_cmd_cn 2>/dev/null 1| tee python.cn | grep -c 'YAP query succeed' >/dev/null
if [ $? -eq 0 ]; then
   result=`diff python.cn python.save`
   if [ "$result" = "" ]; then
       echo "   PASSED"
   else
       echo "   ERROR:python test result differs!!!"
   fi
fi

echo '  ---bad query with sh'
eval $b_cmd_sha 2>/dev/null 1| grep 'YAP query failed' >/dev/null
if [ $? -eq 1 ]; then
   echo  "   ERROR:python test expected to fail but did not ($b_cmd_sha)"
else
   echo  "   PASSED"
fi

echo '  ---bad query with cn'
eval $b_cmd_cn 2>/dev/null 1| grep 'YAP query failed' >/dev/null
if [ $? -eq 1 ]; then
   echo  "   ERROR:python test expected to fail but did not ($b_cmd_cn)"
else
   echo  "   PASSED"
fi
