1#!/usr/bin/env python3
2#
3# Copyright (C) 2020 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18import os
19import subprocess
20import unittest
21import time
22
23def run_cmd(cmd, ignore_error=False):
24    print("Running command:", cmd)
25    p = subprocess.Popen(cmd, shell=True)
26    p.communicate()
27    if not ignore_error and p.returncode:
28        raise subprocess.CalledProcessError(p.returncode, cmd)
29    return p.returncode
30
31
32def has_hwservicemanager():
33   # if the property is set, or hwservicemanager is missing, then we don't have
34    # hwservicemanger running.
35    return 0 != run_cmd("echo '[[ \"$(getprop hwservicemanager.disabled)\" == \"true\" ]] || " +
36                        "[[ ! -f /system/bin/hwservicemanager ]]' | adb shell sh", ignore_error=True)
37
38@unittest.skipUnless(has_hwservicemanager(), "no hwservicemanager")
39class TestHidlJava(unittest.TestCase):
40    pass
41
42def cleanup(cmd):
43    binary = cmd.split()[0]
44    run_cmd("adb shell killall %s >/dev/null 2>&1" % binary, ignore_error=True)
45
46def make_test(client, server):
47    def test(self):
48        try:
49            env = "CLASSPATH=/data/framework/hidl_test_java_java.jar"
50
51            cleanup(client)
52            cleanup(server)
53            run_cmd("adb shell \"( %s %s -s ) </dev/null >/dev/null 2>&1 &\"" % (env, server))
54            time.sleep(2)
55            run_cmd("adb shell %s %s -c" % (env, client))
56        finally:
57            cleanup(client)
58            cleanup(server)
59    return test
60
61def has_bitness(bitness):
62    return 0 == run_cmd("echo '[[ \"$(getprop ro.product.cpu.abilist%s)\" != \"\" ]]' | adb shell sh" % bitness, ignore_error=True)
63
64if __name__ == '__main__':
65    cmds = ["app_process /data/framework com.android.commands.hidl_test_java.HidlTestJava"]
66
67    if has_bitness(32):
68        cmds += ["/data/nativetest/hidl_test_java_native/hidl_test_java_native"]
69
70    if has_bitness(64):
71        cmds += ["/data/nativetest64/hidl_test_java_native/hidl_test_java_native"]
72
73    assert len(cmds) >= 2
74
75    def short_name(cmd):
76        if "app" in cmd:
77            return "java"
78        if "64" in cmd:
79            return "64"
80        return "32"
81
82    for client in cmds:
83        for server in cmds:
84            test_name = 'test_%s_to_%s' % (short_name(client), short_name(server))
85            test = make_test(client, server)
86            setattr(TestHidlJava, test_name, test)
87
88    suite = unittest.TestLoader().loadTestsFromTestCase(TestHidlJava)
89    unittest.TextTestRunner(verbosity=2).run(suite)
90