• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2
3# This file is part of the openHiTLS project.
4#
5# openHiTLS is licensed under the Mulan PSL v2.
6# You can use this software according to the terms and conditions of the Mulan PSL v2.
7# You may obtain a copy of Mulan PSL v2 at:
8#
9#     http://license.coscl.org.cn/MulanPSL2
10#
11# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
12# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
13# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
14# See the Mulan PSL v2 for more details.
15elapsed=0
16
17cd ../../
18HITLS_ROOT_DIR=`pwd`
19
20paramList=$@
21paramNum=$#
22is_concurrent=1
23need_run_all=1
24threadsNum=$(grep -c ^processor /proc/cpuinfo)
25testsuite_array=()
26testcase_array=()
27export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:$(realpath ${HITLS_ROOT_DIR}/build):$(realpath ${HITLS_ROOT_DIR}/platform/Secure_C/lib)
28
29# Check whether an ASAN alarm is generated.
30generate_asan_log() {
31    ASAN_LOG=$(find ../output -name "asan.log*")
32    if [ ! -z "$ASAN_LOG" ]; then
33        for i in $ASAN_LOG
34        do
35            if grep -q "ASan doesn't fully support makecontext/swapcontext" $i
36            then
37                line_count=$(wc -l < "$i")
38                if [ "$line_count" -eq 1 ]; then
39                    echo "The ASAN log contains only ucontext warning content. Ignore it."
40                else
41                    echo "ASAN ERROR. Exit with ucontext check failure."
42                    cat ${i}
43                    exit 1
44                fi
45                continue
46            else
47                echo "ASAN ERROR. Exit with failure."
48                cat ${i}
49                exit 1
50            fi
51        done
52    fi
53}
54# Run the specified test suites or test cases in the output directory.
55run_test() {
56    cd ${HITLS_ROOT_DIR}/testcode/output
57    export ASAN_OPTIONS=detect_stack_use_after_return=1:strict_string_checks=1:detect_leaks=1:halt_on_error=0:detect_odr_violation=0:log_path=asan.log
58
59    echo ""
60    echo "Begin Test"
61    echo ".................................................."
62    start_time=$(date +%s)
63
64    # Run the specified test suite.
65    if [ ${#testsuite_array[*]} -ne 0 ] && [ ${#testcase_array[*]} -eq 0 ];then
66        for i in ${testsuite_array[@]}
67        do
68            if [ "${i}" = "test_suite_sdv_eal_provider_load" ]; then
69                # 针对特定测试套件设置 LD_LIBRARY_PATH
70                echo "Running ${i} with LD_LIBRARY_PATH set to ../testdata/provider/path1"
71                env LD_LIBRARY_PATH="../testdata/provider/path1:${LD_LIBRARY_PATH}" ./${i} NO_DETAIL
72            else
73                # 其他测试套件正常运行
74                ./${i} NO_DETAIL
75            fi
76        done
77    fi
78
79    # Run the specified test case.
80    if [ ${#testcase_array[*]} -ne 0 ];then
81        num=0
82        for i in ${testcase_array[@]}
83        do
84            ./${testsuite_array[num]} ${i}
85            let num+=1
86        done
87    fi
88
89    end_time=$(date +%s)
90    elapsed=$((end_time - start_time))
91
92    generate_asan_log
93}
94
95gen_test_report()
96{
97    cd ${HITLS_ROOT_DIR}/testcode/output
98    ./gen_testcase GenReport
99    testcase_num=0
100    pass_num=0
101    skip_num=0
102    while read line
103    do
104        array=(${line})
105        last_index=$((${#array[@]}-1))
106        if [ "${array[last_index]}" = "PASS" ]; then
107            let pass_num+=1
108        elif [ "${array[last_index]}" = "SKIP" ]; then
109            let skip_num+=1
110        fi
111        let testcase_num+=1
112    done < result.log
113    fail_num=`expr $testcase_num - $pass_num - $skip_num`
114    SumTime=`echo "$elapsed 60" |awk '{printf("%.2f",$1/$2)}'`
115    echo "SumTime is ${SumTime} mintues TestCase Num is ${testcase_num} Pass is ${pass_num} Skip is ${skip_num} Fail is ${fail_num}"
116    if [ ${fail_num} -ne 0 ]; then
117        exit 1
118    fi
119}
120
121# Run all tests in the output directory.
122run_all() {
123    start_time=$(date +%s)
124    echo "Test: $1" >> ${HITLS_ROOT_DIR}/testcode/output/time.txt
125    echo "Start: $(date)" >> ${HITLS_ROOT_DIR}/testcode/output/time.txt
126
127    cd ${HITLS_ROOT_DIR}/testcode/output
128    SUITES=$(ls ./ | grep .datax | sed -e "s/.datax//")
129    export ASAN_OPTIONS=detect_stack_use_after_return=1:strict_string_checks=1:detect_leaks=1:halt_on_error=0:detect_odr_violation=0:log_path=asan.log
130
131    echo ""
132    echo "Begin Test"
133    echo ".................................................."
134    if [ $is_concurrent = 1 ]; then
135        mkfifo tmppipe
136        exec 5<>tmppipe
137        rm -f tmppipe
138        echo "threadsNum = $threadsNum"
139        # procNum indicates the maximum number of concurrent processes.
140        for ((i=1;i<=$threadsNum;i++)); do
141            echo >&5
142        done
143        retPipe=$tmpPipe.ret
144        mkfifo $retPipe
145        exec 8<>$retPipe
146        rm -f $retPipe
147        echo "0" >&8
148        for i in $SUITES
149        do
150            # Run tests in parallel.
151            read -u5
152            {
153                if [ "${i}" = "test_suite_sdv_eal_provider_load" ]; then
154                    echo "Running ${i} with LD_LIBRARY_PATH set to ../testdata/provider/path1"
155                    env LD_LIBRARY_PATH="../testdata/provider/path1:${LD_LIBRARY_PATH}" ./${i} NO_DETAIL || (read -u8 && echo "1 $i" >&8)
156                else
157                    ./${i} NO_DETAIL || (read -u8 && echo "1 $i" >&8)
158                fi
159                echo >&5
160            } &
161        done
162        wait
163
164        exec 5>&-
165        exec 5<&-
166        read -u8 ret
167        exec 8<&-
168        if [ "$ret" != "0" ];then
169            echo "some case failed $ret"
170            gen_test_report
171            generate_asan_log
172            exit 1
173        fi
174    else
175        for i in $SUITES
176        do
177            if [ "${i}" = "test_suite_sdv_eal_provider_load" ]; then
178                echo "Running ${i} with LD_LIBRARY_PATH set to ../testdata/provider/path1"
179                env LD_LIBRARY_PATH="../testdata/provider/path1:${LD_LIBRARY_PATH}" ./${i} NO_DETAIL
180            else
181                ./${i} NO_DETAIL
182            fi
183        done
184    fi
185
186    end_time=$(date +%s)
187    echo "End: $(date)" >> time.txt
188    elapsed=$((end_time - start_time))
189    eval "echo Elapsed time: $(date -ud "@$elapsed" +'$((%s/3600/24)) days %H hr %M min %S sec') >> time.txt"
190
191    generate_asan_log
192}
193
194parse_testsuite_testcase()
195{
196    cd ${HITLS_ROOT_DIR}/testcode/output
197    testsuite_name="test_suite"
198    if [[ "$1" == *$testsuite_name* ]]; then
199        if [ -f "$1" ]; then
200            testsuite_array[${#testsuite_array[*]}]=$i
201            return 1
202        fi
203        return 0
204    else
205        testsuite=`grep -l $1 *.c`
206        if [ "${testsuite}" = "" ]; then
207            return 0
208        else
209            array=(${testsuite//./ })
210            testsuite_array[${#testcase_array[*]}]="${array[0]}"
211            testcase_array[${#testcase_array[*]}]="$1"
212            return 1
213        fi
214    fi
215}
216
217parse_option()
218{
219    for i in $paramList
220    do
221        case "$i" in
222            "help")
223                printf "Note: Before Run <sh ${BASH_SOURCE[0]}>, Please Fisrt Run <sh build_hitls.sh && sh build_sdv.sh>"
224                printf "%-50s %-30s\n" "Run All Testsuites Of The Output"     "sh ${BASH_SOURCE[0]}"
225                printf "%-50s %-30s\n" "Run The Specified Testsuite"          "sh ${BASH_SOURCE[0]} test_suites_xxx test_suites_xxx"
226                printf "%-50s %-30s\n" "Run The Specified Testcase"           "sh ${BASH_SOURCE[0]} UT_CRYPTO_xxx SDV_CRYPTO_xxx"
227                printf "%-50s %-30s\n" "Set Thread Pool Size"                 "sh ${BASH_SOURCE[0]} threads=N"
228                printf "%-50s %-30s\n" "Example: Run with 4 threads"          "sh ${BASH_SOURCE[0]} threads=4"
229                exit 0
230                ;;
231            "threads"*)
232                threads_num=${i#*=}
233                threadsNum=$threads_num
234                ;;
235            *)
236                parse_testsuite_testcase $i
237                if [ $? -eq 0 ]; then
238                    echo "Not Find This Testsuite or Testcase : ${i}"
239                    exit 1
240                fi
241                need_run_all=0
242                ;;
243        esac
244    done
245}
246
247run_demos()
248{
249    exit_code=$?
250    pushd ${HITLS_ROOT_DIR}/testcode/demo/build
251    executales=$(find ./ -maxdepth 1 -type f -perm -a=x )
252    for e in $executales
253    do
254        if [[ ! "$e" == *"client"* ]] && [[ ! "$e" == *"server"* ]]; then
255            echo "${e} start"
256            eval "${e}"
257            if [ $exit_code -ne 0 ]; then
258                echo "Demo ${e} failed"
259                exit 1
260            fi
261        fi
262    done
263
264    # run server and client in order.
265    ./server &
266    if [ $exit_code -ne 0 ]; then
267        echo "Demo ${e} failed"
268        exit 1
269    fi
270    sleep 1
271    ./client
272    if [ $exit_code -ne 0 ]; then
273        echo "Demo ${e} failed"
274        exit 1
275    fi
276    popd
277}
278
279clean()
280{
281    rm -rf ${HITLS_ROOT_DIR}/testcode/output/log/*
282    rm -rf ${HITLS_ROOT_DIR}/testcode/output/result.log
283    rm -rf ${HITLS_ROOT_DIR}/testcode/output/*.sock*
284    rm -rf ${HITLS_ROOT_DIR}/testcode/output/asan*
285}
286
287clean
288parse_option
289if [ ${need_run_all} -eq 1 ]; then
290    run_all
291else
292    run_test
293fi
294run_demos
295gen_test_report
296