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 subprocess
19import unittest
20
21def run_cmd(cmd, ignore_error=False):
22    print("Running command:", cmd)
23    p = subprocess.Popen(cmd, shell=True)
24    p.communicate()
25    if not ignore_error and p.returncode:
26        raise subprocess.CalledProcessError(p.returncode, cmd)
27    return p.returncode
28
29class TestHidl(unittest.TestCase):
30    pass
31
32def make_test(client, server):
33    def test(self):
34        try:
35            run_cmd("adb shell killall %s >/dev/null 2>&1" % client, ignore_error=True)
36            run_cmd("adb shell killall %s >/dev/null 2>&1" % server, ignore_error=True)
37            run_cmd("adb shell \"( %s ) </dev/null >/dev/null 2>&1 &\"" % server)
38            run_cmd("adb shell %s" % client)
39        finally:
40            run_cmd("adb shell killall %s >/dev/null 2>&1" % client, ignore_error=True)
41            run_cmd("adb shell killall %s >/dev/null 2>&1" % server, ignore_error=True)
42    return test
43
44def has_bitness(bitness):
45    return 0 == run_cmd("echo '[[ \"$(getprop ro.product.cpu.abilist%s)\" != \"\" ]]' | adb shell sh" % bitness, ignore_error=True)
46
47if __name__ == '__main__':
48    clients = []
49    servers = []
50
51    if has_bitness(32):
52        clients += ["/data/nativetest/hidl_test_client/hidl_test_client"]
53        servers += ["/data/nativetest/hidl_test_servers/hidl_test_servers"]
54
55    if has_bitness(64):
56        clients += ["/data/nativetest64/hidl_test_client/hidl_test_client"]
57        servers += ["/data/nativetest64/hidl_test_servers/hidl_test_servers"]
58
59    assert len(clients) > 0
60    assert len(servers) > 0
61
62    def short_name(binary):
63        if "64" in binary:
64            return "64"
65        return "32"
66
67    for client in clients:
68        for server in servers:
69            test_name = 'test_%s_to_%s' % (short_name(client), short_name(server))
70            test = make_test(client, server)
71            setattr(TestHidl, test_name, test)
72
73    suite = unittest.TestLoader().loadTestsFromTestCase(TestHidl)
74    unittest.TextTestRunner(verbosity=2).run(suite)
75