1#!/bin/bash
2
3# See hal_hidl_gtest.py
4
5THREADS=
6CHECKER=vts_testability_checker
7CHECKER_DEVICE_PATH="/data/local/tmp/${CHECKER}"
8PRINT_COMMANDS=
9
10function run() {
11    if [ "${PRINT_COMMANDS}" = true ] ; then
12        >&2 echo "*** $@"
13    fi
14    $@
15}
16
17function make_modules() {
18    if [ "${THREADS}" != "0" ] ; then
19        run make -j${THREADS} -C ${ANDROID_BUILD_TOP} -f build/core/main.mk $@
20    fi
21}
22
23function push_checker() {
24    run adb push ${OUT}/system/bin/${CHECKER} ${CHECKER_DEVICE_PATH}
25}
26
27function push_test() {
28    local module=$1
29    for test_dir in nativetest nativetest64 ; do
30        local test_file=/data/${test_dir}/${module}/${module}
31        run adb push ${OUT}${test_file} ${test_file}
32    done
33}
34
35function read_checker_output() {
36    python -c 'import json,sys;obj=json.load(sys.stdin);sys.stdout.write("%s\n"%obj["Testable"]);map(lambda i:sys.stdout.write("%s\n"%i),obj["instances"])'
37}
38
39function run_test() {
40    local module=$1
41    local status=0
42
43    for test_dir in nativetest nativetest64 ; do
44        local test_file=/data/${test_dir}/${module}/${module}
45        local interfaces=$(run adb shell ${test_file} --list_registered_services \
46            | sed -n 's/^hal_service: \(.*\)$/\1/p')
47        if [ -z "$interfaces" ]; then
48            run adb shell ${test_file} || status=$?
49        else
50            for interface in ${interfaces} ; do
51                local output=$(run adb shell ${CHECKER_DEVICE_PATH} -c ${interface} | read_checker_output)
52                local testable=$(echo "${output}" | head -n1)
53                local instances=$(echo "${output}" | tail -n+2)
54
55                if [ "${testable}" == "True" ] ; then
56                    for instance in ${instances} ; do
57                        run adb shell ${test_file} --hal_service_instance="${interface}/${instance}" || status=$?
58                    done
59                fi
60            done
61        fi
62    done
63    return ${status}
64}
65
66function usage() {
67    echo "usage: $0 -m <module_name> [-m <module_name>[...]] [-j <jobs>] [-p]"
68    echo "          -m <module_name>: name of test (e.g. VtsHalHealthV2_0TargetTest)"
69    echo "          -p: print commands"
70    echo "          -j <jobs>: # jobs in make. "
71    echo "                     -j0 skips making any modules."
72    echo "                     If not present, use infinite number of jobs."
73
74    exit 1
75}
76
77function main() {
78    local modules=
79
80    while getopts "m:j:p" option ; do
81        case "${option}" in
82            m)
83                [ ! -z ${OPTARG} ] || usage
84                modules="${modules} ${OPTARG}"
85                ;;
86            j)
87                THREADS=${OPTARG}
88                ;;
89            p)
90                PRINT_COMMANDS=true
91                ;;
92            *)
93                usage
94                ;;
95        esac
96    done
97
98    set -e
99    make_modules ${CHECKER} ${modules}
100    run adb root
101    push_checker
102    for module in ${modules} ; do
103        push_test ${module}
104    done
105
106    set +e
107    local status=0
108    for module in ${modules} ; do
109        run_test ${module} || status=$?
110    done
111    return ${status}
112}
113
114main $@
115