1#!/bin/bash
2# Copyright (c) 2021 Huawei Device Co., Ltd.
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15# This Script used to push test data to devices
16# Usage:
17# ./prepare_testdata.sh path
18# path is the rootdir of ohos projects.
19set -e
20
21if [ $# -lt 1 ];then
22    echo "Usage $0 <product name>"
23    echo "example $0 rk3568"
24    exit 1
25fi
26
27function hdc_shell_cmd() {
28    # do nothing if there are not any arguments
29    if [ $# -eq 0 ];then
30        return;
31    fi
32    echo "Running command $@"
33    hdc shell $@
34}
35
36function get_root_dir() {
37    local cur_path=$(pwd)
38    while [ "${cur_path}" != "" ]
39    do
40        cur_path=${cur_path%/*}
41        if [ "${cur_path}" == "" ];then
42            echo "[error] get code root dir fail"
43            exit 1
44        fi
45        if [ "$(basename ${cur_path})" == "base" ]; then
46            ohos_root=${cur_path%/*}
47            return
48        fi
49    done
50}
51
52get_root_dir
53product_name=$1
54
55if [ ! -d "${ohos_root}/out/${product_name}" ]; then
56    echo "product ${product_name} not exist"
57    exit 1
58fi
59
60hdc target mount
61sleep 0.2
62hdc_shell_cmd "mount -o remount,rw /"
63ut_target_path="/data/init_ut"
64echo "Remove ${ut_target_path}"
65hdc_shell_cmd "rm -rf ${ut_target_path}"
66hdc_shell_cmd "rm /bin/init_unittest"
67
68echo "Create ${ut_target_path}"
69hdc_shell_cmd "umask 022"
70hdc_shell_cmd "mkdir -p ${ut_target_path}"
71hdc_shell_cmd "mkdir -p ${ut_target_path}/proc"
72
73ohos_init="${ohos_root}/base/startup"
74
75hdc_shell_cmd "mkdir -p ${ut_target_path}/coverage"
76sleep 0.25
77
78# copy file to test
79hdc file send ${ohos_root}/out/${product_name}/tests/unittest/startup/init/init_unittest /data/init_ut/init_unittest
80sleep 0.25
81hdc_shell_cmd "cp /data/init_ut/init_unittest /bin/init_unittest"
82
83hdc_shell_cmd "chmod 777 /data/init_ut/* -R"
84sleep 0.2
85hdc_shell_cmd "chmod 777 /bin/init_unittest"
86
87hdc_shell_cmd "export GCOV_PREFIX=${ut_target_path}/coverage&&export GCOV_PREFIX_STRIP=15&&init_unittest"
88sleep 0.2
89
90if [ $? -ne 0 ]; then
91    echo "Execute init_unittest in device failed. please check the log"
92fi
93echo "Running init unittests end..."
94echo "Ready to generate coverage..."
95pushd ${ohos_init}
96rm -rf ./g.sh
97rm -rf *.gcno
98rm -rf *.gcda
99echo "Copy .gcta files to ${ohos_init}}"
100
101for file in $(hdc_shell_cmd ls /data/init_ut/coverage/*.gcda); do
102    hdc file recv ${file}  ${ohos_init}/${file:23}
103    chmod 777 ${ohos_init}/${file:23}
104done
105
106
107echo "Find out all gcno files and copy to ${ohos_init}"
108find ${ohos_root}/out/${product_name}/obj/base/startup/ -name "*.gcno" -type f -exec cp {} . \;
109if [ $? -ne 0 ]; then
110    echo "find gcno failed."
111    popd 2>&1 > /dev/null
112    exit 1
113fi
114
115if [ ! -f "${ohos_init}/g.sh" ]; then
116    echo "create g.sh"
117    touch ${ohos_init}/g.sh
118    echo "${ohos_root}/prebuilts/clang/ohos/linux-x86_64/llvm/bin/llvm-cov gcov \$@" > ${ohos_init}/g.sh
119    chmod 755 ${ohos_init}/g.sh
120fi
121
122echo "Running command lcov"
123lcov -d . -o "${ohos_init}/init_ut_tmp.info" -b . -c --gcov-tool ${ohos_init}/g.sh
124
125if [ $? -ne 0 ]; then
126    echo "Run command lcov failed"
127    popd 2>&1 > /dev/null
128fi
129
130echo "Filter out don\'t cared dir"
131lcov --remove init_ut_tmp.info "*foundation*" "*init/adapter/init_adapter.c*" "*third_party*" \
132    "*device.c*" "*prebuilts*" "*test/unittest/*"  "*utils/native/*" "*utils/system/*" \
133    "*init.c*" "*init_signal_handler.c*" "*ueventd.c*" \
134    "*ueventd_device_handler.c*" "*ueventd_firmware_handler.c*" "*ueventd_socket.c*" -o ${ohos_init}/init_ut.info
135
136genhtml -o ${HOME}/init_coverage init_ut.info
137if [ $? -ne 0 ]; then
138    echo "Run genhtml failed."
139    popd 2>&1 > /dev/null
140    exit 1
141fi
142echo "Clear tmp files"
143rm -rf ./g.sh *.gcno *.gcda init_ut.info init_ut_tmp.info
144hdc_shell_cmd "rm -rf ${ut_target_path}"
145echo
146echo "Generate init ut coverage done."
147echo "Check coverage in ${HOME}/init_coverage."
148echo
149popd 2>&1 > /dev/null
150