Feature: write summary with negative cases
[platform/upstream/SSAT.git] / ssat-api.sh
index b4ef5a7..2b11b08 100644 (file)
@@ -1,11 +1,10 @@
 #!/usr/bin/env bash
 ##
-# @file ssat-api.sh
-# @author MyungJoo Ham <myungjoo.ham@gmail.com>
-# @date Jun 22 2018
-# @license Apache-2.0
-# @brief This is API set for SSAT (Shell Script Automated Tester)
-#
+## @file ssat-api.sh
+## @author MyungJoo Ham <myungjoo.ham@gmail.com>
+## @date Jun 22 2018
+## @brief This is API set for SSAT (Shell Script Automated Tester)
+##
 
 if [[ "$nocolor" != "1" ]]
 then
@@ -49,7 +48,21 @@ fi
 
 ResultLog=""
 
-function writef {
+# Platform dependent variables
+KernelName=$(uname -s)
+if [[ "${KernelName}" == "Darwin" ]]; then
+       StatCmd_GetSize="stat -f %z"
+       SO_EXT="dylib"
+else
+       StatCmd_GetSize="stat --printf=%s"
+       SO_EXT="so"
+fi
+
+## @fn writef()
+## @private
+## @param $1 the string to be printed.
+## @brief Prepare report result
+function writef() {
        if [[ "${SILENT}" == "0" ]]
        then
                printf "$1\n"
@@ -57,11 +70,10 @@ function writef {
        ResultLog="$ResultLog$1\n"
 }
 
-##
-# @brief Report results of a test group (a "runTest.sh" in a testee directory)
-#
-function report {
-       if (( ${_fail} == 0 ))
+## @fn report()
+## @brief Report results of a test group (a "runTest.sh" in a testee directory)
+function report() {
+       if (( ${_fail} == 0 && ${_criticalFail} == 0 ))
        then
                writef "${Green}==================================================${NC}"
                writef "${LightGreen}[PASSED]${NC} Test Group $_group ${Green}Passed${NC}"
@@ -72,7 +84,7 @@ function report {
                        writef "${Red}[FAILED]${NC} Test Group $_group has ${Red}failed cases ($_fail)${NC}"
                else
                        writef "${Green}==================================================${NC}"
-                       writef "${LightGreen}[PASSED]${NC} Test Group $_group has ${Red}failed cases ($_fail), but they are not critical.${NC}"
+                       writef "${LightGreen}[PASSED]${NC} Test Group $_group has ${Red}failed cases ($_fail), but they are ignorable cases and not critical.${NC}"
                fi
        fi
 
@@ -81,9 +93,16 @@ function report {
        # do nothing
                echo ""
        else
-               writef "${_cases},${_pass},${_fail}"
+               _ignore=$((_fail-_criticalFail))
+               _fail=${_criticalFail}
+               if [[ "${COUNTNEGATIVE}" -eq "1" ]]
+               then
+                       writef "${_cases},${_pass},${_fail},${_ignore},${_neg}"
+               else
+                       writef "${_cases},${_pass},${_fail},${_ignore}"
+               fi
                echo "${ResultLog}" > ${_filename}
-               printf "${_filename}\n"
+               printf "\n${_filename}\n"
        fi
 
        if (( ${_criticalFail} > 0 ))
@@ -94,11 +113,14 @@ function report {
        fi
 }
 
-function testInit {
+## @fn testInit()
+## @brief Initialize runTest.sh shell test case
+function testInit() {
        _pass=0
        _fail=0
        _criticalFail=0
        _cases=0
+       _neg=0
        _filename=$(mktemp)
        _group=`basename "$1"`
        if [[ "${#_group}" -eq "0" ]]
@@ -110,14 +132,19 @@ function testInit {
        writef "    Test Group ${Green}$_group${NC} Starts."
 }
 
-##
-# @brief Write Test Log
-# @param $1 1 = success / 0 = fail ($5 is not 1)
-# @param $2 test case ID (short string)
-# @param $3 test case description
-# @param $4 set 1 if this is not critical (don't care if it's pass or fail)_
-# @param $5 set 1 if $1==0 is success and $1!=0 is fail.
-function testResult {
+## @fn testResult()
+## @brief Write Test Log
+## @param $1 1 = success / 0 = fail ($5 is not 1)
+## @param $2 test case ID (short string)
+## @param $3 test case description
+## @param $4 set 1 if this is not critical (don't care if it's pass or fail)_
+## @param $5 set 1 if $1==0 is success and $1!=0 is fail.
+function testResult() {
+       if [[ "${PROGRESS}" -eq "1" ]]
+       then
+               echo "Case ${2}(${3}) report ${1}" > /dev/stderr
+       fi
+
        _cases=$((_cases+1))
        _good=0
        if [[ "${5}" -eq "1" ]]; then
@@ -130,6 +157,14 @@ function testResult {
                fi
        fi
 
+       if [[ "${COUNTNEGATIVE}" -eq "1" ]]
+       then
+               if [[ "${2}\n" =~ "${COUNTNEGATIVEPOSTFIX}\n" ]]
+               then
+                       _neg=$((_neg+1))
+               fi
+       fi
+
        if [[ "${_good}" -eq "1" ]]
        then
                writef "${LightGreen}[PASSED]${NC} ${Green}$2${NC}:$3${NC}"
@@ -138,7 +173,7 @@ function testResult {
                _fail=$((_fail+1))
                if [[ "${4}" == "1" ]]
                then
-                       writef "${Purple}[FAILED][Ignorable] $2${NC}:${Purple}$3${NC}"
+                       writef "${Purple}[IGNORED] $2${NC}:${Purple}$3${NC}"
                else
                        writef "${Red}[FAILED][Critical] $2${NC}:${Purple}$3${NC}"
                        _criticalFail=$((_criticalFail+1))
@@ -146,14 +181,14 @@ function testResult {
        fi
 }
 
-##
-# @brief Call Test Case (a shell script), expected exit = 0
-# @param $1 Full path to the executable (e.g., ~/script/a1.sh)
-# @param $2 Full string of the arguments to $1 (e.g., "-q -n --dryrun")
-# @param $3 test case ID
-# @param $4 test case description
-# @param $5 set 1 if this is not critical (don't care if it's pass or fail)_
-function callTestSuccess {
+## @fn callTestSuccess()
+## @brief Call Test Case (a shell script), expected exit = 0
+## @param $1 Full path to the executable (e.g., ~/script/a1.sh)
+## @param $2 Full string of the arguments to $1 (e.g., "-q -n --dryrun")
+## @param $3 test case ID
+## @param $4 test case description
+## @param $5 set 1 if this is not critical (don't care if it's pass or fail)_
+function callTestSuccess() {
        callutput=$(. $1 $2)
        retcode=$?
        if (( ${retcode} == 0 ))
@@ -164,14 +199,14 @@ function callTestSuccess {
        fi
 }
 
-##
-# @brief Call Test Case (a shell script), expected exit != 0
-# @param $1 Full path to the executable (e.g., ~/script/a1.sh)
-# @param $2 Full string of the arguments to $1 (e.g., "-q -n --dryrun")
-# @param $3 test case ID
-# @param $4 test case description
-# @param $5 set 1 if this is not critical (don't care if it's pass or fail)_
-function callTestFail {
+## @fn callTestFail()
+## @brief Call Test Case (a shell script), expected exit != 0
+## @param $1 Full path to the executable (e.g., ~/script/a1.sh)
+## @param $2 Full string of the arguments to $1 (e.g., "-q -n --dryrun")
+## @param $3 test case ID
+## @param $4 test case description
+## @param $5 set 1 if this is not critical (don't care if it's pass or fail)_
+function callTestFail() {
        callutput=$(. $1 $2)
        retcode=$?
        if (( ${retcode} != 0 ))
@@ -182,15 +217,15 @@ function callTestFail {
        fi
 }
 
-##
-# @brief Call Test Case (a shell script), expected exit == $5
-# @param $1 Full path to the executable (e.g., ~/script/a1.sh)
-# @param $2 Full string of the arguments to $1 (e.g., "-q -n --dryrun")
-# @param $3 test case ID
-# @param $4 test case description
-# @param $5 Expected exit code.
-# @param $6 set 1 if this is not critical (don't care if it's pass or fail)_
-function callTestExitEq {
+## @fn callTestExitEq()
+## @brief Call Test Case (a shell script), expected exit == $5
+## @param $1 Full path to the executable (e.g., ~/script/a1.sh)
+## @param $2 Full string of the arguments to $1 (e.g., "-q -n --dryrun")
+## @param $3 test case ID
+## @param $4 test case description
+## @param $5 Expected exit code.
+## @param $6 set 1 if this is not critical (don't care if it's pass or fail)_
+function callTestExitEq() {
        callutput=$(. $1 $2)
        retcode=$?
        if (( ${retcode} == $5 ))
@@ -201,37 +236,37 @@ function callTestExitEq {
        fi
 }
 
-##
-# @brief Compare two result files expected to be equal
-# @param $1 Path to result 1 (golden)
-# @param $2 Path to result 2 (test run)
-# @param $3 test case ID
-# @param $4 test case description
-# @param $5 0 if the size is expected to be equal as well. 1 if golden (result 1) might be smaller (will ignore rest of result 2). 2 if the opposite of 1. If $5 > 2, it denotes the max size of compared bytes. (compare the first $5 bytes only)
-# @param $6 set 1 if this is not critical (don't care if it's pass or fail)_
-function callCompareTest {
+## @fn callCompareTest()
+## @brief Compare two result files expected to be equal
+## @param $1 Path to result 1 (golden)
+## @param $2 Path to result 2 (test run)
+## @param $3 test case ID
+## @param $4 test case description
+## @param $5 0 if the size is expected to be equal as well. 1 if golden (result 1) might be smaller (will ignore rest of result 2). 2 if the opposite of 1. If $5 > 2, it denotes the max size of compared bytes. (compare the first $5 bytes only)
+## @param $6 set 1 if this is not critical (don't care if it's pass or fail)_
+function callCompareTest() {
        # Try cmp.
-       command -v cmp
        output=0
-       if (( $? == 0 ))
+       command -v cmp
+       # If cmp is symlink, then it could be from busybox and it does not support "-n" option
+       if [[ $? == 0 && ! -L $(which cmp) ]]
        then
                # use cmp
-               echo NYI
                if (( $5 == 0 )); then
                        # Size should be same as well.
                        cmp $1 $2
                        output=$?
                elif (( $5 == 1 )); then
                        # Compare up to the size of golden
-                       cmp -n `stat --printf="%s" $1` $1 $2
+                       cmp -n `${StatCmd_GetSize} $1` $1 $2
                        output=$?
                elif (( $5 == 2 )); then
                        # Compare up to the size of test-run
-                       cmp -n `stat --printf="%s" $2` $1 $2
+                       cmp -n `${StatCmd_GetSize} $2` $1 $2
                        output=$?
                else
                        # Compare up to $5 bytes.
-                       cmp -n `stat --printf="%s" $5` $1 $2
+                       cmp -n `${StatCmd_GetSize} $5` $1 $2
                        output=$?
                fi
                if (( ${output} == 0 )); then
@@ -241,26 +276,42 @@ function callCompareTest {
                fi
                testResult $output "$3" "$4" $6
        else
-               # use internal logic (slower!)
-               echo NYI
-               testResult 0 "$3" "Cannot test. cmp not found." $6
+           # use internal logic (slower!)
+           bufsize=`${StatCmd_GetSize} $1`
+           if (( $5 == 2 )); then
+               bufsize=`${StatCmd_GetSize} $2`
+           else
+               bufsize=$5
+           fi
+           diff <(dd bs=1 count=$bufsize if=$1 &>/dev/null) <(dd bs=1 count=$bufsize if=$2 &>/dev/null)
+           output=$?
+           if (( ${output} == 0 )); then
+               output=1
+           else
+               output=0
+           fi
+           testResult $output "$3" "$4" $6
        fi
 }
 
-########################################################
-## EXTENSION. GStreamer
-## @todo How to separate such "plugins"?
-########################################################
+## @fn gstTest()
+## @brief Execute gst-launch with given arguments
+## @todo Separate this function to "gstreamer extension plugin"
+## @param $1 gst-launch-1.0 Arguments
+## @param $2 test case ID
+## @param $3 set 1 if this is not critical (don't care if it's pass or fail)
+## @param $4 set 1 if this passes if gstLaunch fails.
+## @param $5 set 1 to enable PERFORMANCE test.
+function gstTest() {
+       if [[ "$VALGRIND" -eq "1" ]]; then
+               calloutputprefix='valgrind --track-origins=yes'
+       fi
+       if [[ "${SILENT}" -eq "1" ]]; then
+               calloutput=$(eval $calloutputprefix gst-launch-1.0 -f -q $1 &> /dev/null)
+       else
+               calloutput=$(eval $calloutputprefix gst-launch-1.0 -f -q $1)
+       fi
 
-##
-# @brief Execute gst-launch with given arguments
-# @param $1 gst-launch-1.0 Arguments
-# @param $2 test case ID
-# @param $3 set 1 if this is not critical (don't care if it's pass or fail)
-# @param $4 set 1 if this passes if gstLaunch fails.
-# @param $5 set 1 to enable PERFORMANCE test.
-function gstTest {
-       calloutput=$(gst-launch-1.0 -q $1)
        retcode=$?
        desired=0
        if [[ "${4}" -eq "1" ]]; then
@@ -290,11 +341,11 @@ function gstTest {
        fi
 }
 
-##
-# @brief Convert all *.bmp to *.png
-#
-# @todo macronice "bmp2png" searching.
-function convertBMP2PNG {
+## @fn convertBMP2PNG()
+## @brief Convert all *.bmp to *.png in the current directory
+## @todo macronice "bmp2png" searching.
+## @todo Separate this function to "gstreamer extension plugin"
+function convertBMP2PNG() {
        tool="bmp2png"
        if [ -x bmp2png ]; then
                tool="bmp2png"