[macOS/API] Use option depending on system to get file size using stat
[platform/upstream/SSAT.git] / ssat-api.sh
1 #!/usr/bin/env bash
2 ##
3 ## @file ssat-api.sh
4 ## @author MyungJoo Ham <myungjoo.ham@gmail.com>
5 ## @date Jun 22 2018
6 ## @brief This is API set for SSAT (Shell Script Automated Tester)
7 ##
8
9 if [[ "$nocolor" != "1" ]]
10 then
11         Black='\033[0;30m'
12         DarkGray='\033[1;30m'
13         Red='\033[0;31m'
14         LightRed='\033[1;31m'
15         Green='\033[0;32m'
16         LightGreen='\033[1;32m'
17         Orange='\033[0;33m'
18         Yellow='\033[1;33m'
19         Blue='\033[0;34m'
20         LightBlue='\033[1;34m'
21         Purple='\033[0;35m'
22         LightPurple='\033[1;35m'
23         Cyan='\033[0;36m'
24         LightCyan='\033[1;36m'
25         LightGray='\033[0;37m'
26         White='\033[1;37m'
27         NC='\033[0m'
28 else
29         Black=''
30         DarkGray=''
31         Red=''
32         LightRed=''
33         Green=''
34         LightGreen=''
35         Orange=''
36         Yellow=''
37         Blue=''
38         LightBlue=''
39         Purple=''
40         LightPurple=''
41         Cyan=''
42         LightCyan=''
43         LightGray=''
44         White=''
45         NC=''
46 fi
47
48
49 ResultLog=""
50
51 # Platform dependent variables
52 KernelName=$(uname -s)
53 if [[ "${KernelName}" == "Darwin" ]]; then
54         StatCmd_GetSize="stat -f %z"
55 else
56         StatCmd_GetSize="stat --printf=%s"
57 fi
58
59 ## @fn writef()
60 ## @private
61 ## @param $1 the string to be printed.
62 ## @brief Prepare report result
63 function writef() {
64         if [[ "${SILENT}" == "0" ]]
65         then
66                 printf "$1\n"
67         fi
68         ResultLog="$ResultLog$1\n"
69 }
70
71 ## @fn report()
72 ## @brief Report results of a test group (a "runTest.sh" in a testee directory)
73 function report() {
74         if (( ${_fail} == 0 && ${_criticalFail} == 0 ))
75         then
76                 writef "${Green}==================================================${NC}"
77                 writef "${LightGreen}[PASSED]${NC} Test Group $_group ${Green}Passed${NC}"
78         else
79                 if (( ${_criticalFail} > 0 ))
80                 then
81                         writef "${Green}==================================================${NC}"
82                         writef "${Red}[FAILED]${NC} Test Group $_group has ${Red}failed cases ($_fail)${NC}"
83                 else
84                         writef "${Green}==================================================${NC}"
85                         writef "${LightGreen}[PASSED]${NC} Test Group $_group has ${Red}failed cases ($_fail), but they are not critical.${NC}"
86                 fi
87         fi
88
89         if [[ "$INDEPENDENT" -eq "1" ]]
90         then
91         # do nothing
92                 echo ""
93         else
94                 writef "${_cases},${_pass},${_fail}"
95                 echo "${ResultLog}" > ${_filename}
96                 printf "\n${_filename}\n"
97         fi
98
99         if (( ${_criticalFail} > 0 ))
100         then
101                 exit 1
102         else
103                 exit 0
104         fi
105 }
106
107 ## @fn testInit()
108 ## @brief Initialize runTest.sh shell test case
109 function testInit() {
110         _pass=0
111         _fail=0
112         _criticalFail=0
113         _cases=0
114         _filename=$(mktemp)
115         _group=`basename "$1"`
116         if [[ "${#_group}" -eq "0" ]]
117         then
118                 _group="(Unspecified)"
119         fi
120
121         writef "${Green}==================================================${NC}"
122         writef "    Test Group ${Green}$_group${NC} Starts."
123 }
124
125 ## @fn testResult()
126 ## @brief Write Test Log
127 ## @param $1 1 = success / 0 = fail ($5 is not 1)
128 ## @param $2 test case ID (short string)
129 ## @param $3 test case description
130 ## @param $4 set 1 if this is not critical (don't care if it's pass or fail)_
131 ## @param $5 set 1 if $1==0 is success and $1!=0 is fail.
132 function testResult() {
133         if [[ "${PROGRESS}" -eq "1" ]]
134         then
135                 echo "Case ${2}(${3}) report ${1}" > /dev/stderr
136         fi
137
138         _cases=$((_cases+1))
139         _good=0
140         if [[ "${5}" -eq "1" ]]; then
141                 if [[ "${1}" -eq "0" ]]; then
142                         _good=1
143                 fi
144         else
145                 if [[ "${1}" -eq "1" ]]; then
146                         _good=1
147                 fi
148         fi
149
150         if [[ "${_good}" -eq "1" ]]
151         then
152                 writef "${LightGreen}[PASSED]${NC} ${Green}$2${NC}:$3${NC}"
153                 _pass=$((_pass+1))
154         else
155                 _fail=$((_fail+1))
156                 if [[ "${4}" == "1" ]]
157                 then
158                         writef "${Purple}[FAILED][Ignorable] $2${NC}:${Purple}$3${NC}"
159                 else
160                         writef "${Red}[FAILED][Critical] $2${NC}:${Purple}$3${NC}"
161                         _criticalFail=$((_criticalFail+1))
162                 fi
163         fi
164 }
165
166 ## @fn callTestSuccess()
167 ## @brief Call Test Case (a shell script), expected exit = 0
168 ## @param $1 Full path to the executable (e.g., ~/script/a1.sh)
169 ## @param $2 Full string of the arguments to $1 (e.g., "-q -n --dryrun")
170 ## @param $3 test case ID
171 ## @param $4 test case description
172 ## @param $5 set 1 if this is not critical (don't care if it's pass or fail)_
173 function callTestSuccess() {
174         callutput=$(. $1 $2)
175         retcode=$?
176         if (( ${retcode} == 0 ))
177         then
178                 testResult 1 "$3" "$4" $5
179         else
180                 testResult 0 "$3" "$4 ret($retcode)" $5
181         fi
182 }
183
184 ## @fn callTestFail()
185 ## @brief Call Test Case (a shell script), expected exit != 0
186 ## @param $1 Full path to the executable (e.g., ~/script/a1.sh)
187 ## @param $2 Full string of the arguments to $1 (e.g., "-q -n --dryrun")
188 ## @param $3 test case ID
189 ## @param $4 test case description
190 ## @param $5 set 1 if this is not critical (don't care if it's pass or fail)_
191 function callTestFail() {
192         callutput=$(. $1 $2)
193         retcode=$?
194         if (( ${retcode} != 0 ))
195         then
196                 testResult 1 "$3" "$4 ret($retcode)" $5
197         else
198                 testResult 0 "$3" "$4" $5
199         fi
200 }
201
202 ## @fn callTestExitEq()
203 ## @brief Call Test Case (a shell script), expected exit == $5
204 ## @param $1 Full path to the executable (e.g., ~/script/a1.sh)
205 ## @param $2 Full string of the arguments to $1 (e.g., "-q -n --dryrun")
206 ## @param $3 test case ID
207 ## @param $4 test case description
208 ## @param $5 Expected exit code.
209 ## @param $6 set 1 if this is not critical (don't care if it's pass or fail)_
210 function callTestExitEq() {
211         callutput=$(. $1 $2)
212         retcode=$?
213         if (( ${retcode} == $5 ))
214         then
215                 testResult 1 "$3" "$4" $6
216         else
217                 testResult 0 "$3" "$4 ret($retcode)" $6
218         fi
219 }
220
221 ## @fn callCompareTest()
222 ## @brief Compare two result files expected to be equal
223 ## @param $1 Path to result 1 (golden)
224 ## @param $2 Path to result 2 (test run)
225 ## @param $3 test case ID
226 ## @param $4 test case description
227 ## @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)
228 ## @param $6 set 1 if this is not critical (don't care if it's pass or fail)_
229 function callCompareTest() {
230         # Try cmp.
231         output=0
232         command -v cmp
233         # If cmp is symlink, then it could be from busybox and it does not support "-n" option
234         if [[ $? == 0 && ! -L $(which cmp) ]]
235         then
236                 # use cmp
237                 if (( $5 == 0 )); then
238                         # Size should be same as well.
239                         cmp $1 $2
240                         output=$?
241                 elif (( $5 == 1 )); then
242                         # Compare up to the size of golden
243                         cmp -n `${StatCmd_GetSize} $1` $1 $2
244                         output=$?
245                 elif (( $5 == 2 )); then
246                         # Compare up to the size of test-run
247                         cmp -n `${StatCmd_GetSize} $2` $1 $2
248                         output=$?
249                 else
250                         # Compare up to $5 bytes.
251                         cmp -n `${StatCmd_GetSize} $5` $1 $2
252                         output=$?
253                 fi
254                 if (( ${output} == 0 )); then
255                         output=1
256                 else
257                         output=0
258                 fi
259                 testResult $output "$3" "$4" $6
260         else
261             # use internal logic (slower!)
262             bufsize=`${StatCmd_GetSize} $1`
263             if (( $5 == 2 )); then
264                 bufsize=`${StatCmd_GetSize} $2`
265             else
266                 bufsize=$5
267             fi
268             diff <(dd bs=1 count=$bufsize if=$1 &>/dev/null) <(dd bs=1 count=$bufsize if=$2 &>/dev/null)
269             output=$?
270             if (( ${output} == 0 )); then
271                 output=1
272             else
273                 output=0
274             fi
275             testResult $output "$3" "$4" $6
276         fi
277 }
278
279 ## @fn gstTest()
280 ## @brief Execute gst-launch with given arguments
281 ## @todo Separate this function to "gstreamer extension plugin"
282 ## @param $1 gst-launch-1.0 Arguments
283 ## @param $2 test case ID
284 ## @param $3 set 1 if this is not critical (don't care if it's pass or fail)
285 ## @param $4 set 1 if this passes if gstLaunch fails.
286 ## @param $5 set 1 to enable PERFORMANCE test.
287 function gstTest() {
288         if [[ "$VALGRIND" -eq "1" ]]; then
289                 calloutputprefix='valgrind --track-origins=yes'
290         fi
291         calloutput=$(eval $calloutputprefix gst-launch-1.0 -f -q $1)
292
293         retcode=$?
294         desired=0
295         if [[ "${4}" -eq "1" ]]; then
296                 if [[ "${retcode}" -ne "0" ]]; then
297                         desired=1
298                 fi
299         else
300                 if [[ "${retcode}" -eq "0" ]]; then
301                         desired=1
302                 fi
303         fi
304
305         if [[ "$desired" -eq "1" ]]; then
306                 testResult 1 "$2" "gst-launch of case $2" $3
307         else
308                 testResult 0 "$2" "gst-launch of case $2" $3
309         fi
310
311         if [[ "$5" -eq "1" ]]; then
312                 if (( ${#GST_DEBUG_DUMP_DOT_DIR} -le 1 )); then
313                         GST_DEBUG_DUMP_DOT_DIR="./performance"
314                 fi
315                 dot -Tpng $GST_DEBUG_DUMP_DOT_DIR/*.PLAYING_PAUSED.dot > $GST_DEBUG_DUMP_DOT_DIR/debug/$base/$2.png
316                 gst-report-1.0 --dot $GST_DEBUG_DUMP_DOT_DIR/*.gsttrace | dot -Tsvg > $GST_DEBUG_DUMP_DOT_DIR/profile/$base/$2.svg
317                 rm -f $GST_DEBUG_DUMP_DOT_DIR/*.dot
318                 rm -f $GST_DEBUG_DUMP_DOT_DIR/*.gsttrace
319         fi
320 }
321
322 ## @fn convertBMP2PNG()
323 ## @brief Convert all *.bmp to *.png in the current directory
324 ## @todo macronice "bmp2png" searching.
325 ## @todo Separate this function to "gstreamer extension plugin"
326 function convertBMP2PNG() {
327         tool="bmp2png"
328         if [ -x bmp2png ]; then
329                 tool="bmp2png"
330         else
331                 if [ -x ../bmp2png ]; then
332                         tool="../bmp2png"
333                 else
334                         if [ -x ../../bmp2png ]; then
335                                 tool="../../bmp2png"
336                         else
337                                 tool="../../../bmp2png"
338                                 # Try this and die if fails
339                         fi
340                 fi
341         fi
342         for X in `ls *.bmp`
343         do
344                 if [[ $X  = *"GRAY8"* ]]; then
345                         $tool $X --GRAY8
346                 else
347                         $tool $X
348                 fi
349         done
350 }
351
352 SSATAPILOADED=1