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 29def has_hwservicemanager(): 30 # if the property is set, or hwservicemanager is missing, then we don't have 31 # hwservicemanger running. 32 return 0 != run_cmd("echo '[[ \"$(getprop hwservicemanager.disabled)\" == \"true\" ]] || " + 33 "[[ ! -f /system/bin/hwservicemanager ]]' | adb shell sh", ignore_error=True) 34 35@unittest.skipUnless(has_hwservicemanager(), "no hwservicemanager") 36class TestHidl(unittest.TestCase): 37 pass 38 39def make_test(client, server): 40 def test(self): 41 try: 42 run_cmd("adb shell killall %s >/dev/null 2>&1" % client, ignore_error=True) 43 run_cmd("adb shell killall %s >/dev/null 2>&1" % server, ignore_error=True) 44 run_cmd("adb shell \"( %s ) </dev/null >/dev/null 2>&1 &\"" % server) 45 run_cmd("adb shell %s" % client) 46 finally: 47 run_cmd("adb shell killall %s >/dev/null 2>&1" % client, ignore_error=True) 48 run_cmd("adb shell killall %s >/dev/null 2>&1" % server, ignore_error=True) 49 return test 50 51def has_bitness(bitness): 52 return 0 == run_cmd("echo '[[ \"$(getprop ro.product.cpu.abilist%s)\" != \"\" ]]' | adb shell sh" % bitness, ignore_error=True) 53 54if __name__ == '__main__': 55 clients = [] 56 servers = [] 57 58 if has_bitness(32): 59 clients += ["/data/nativetest/hidl_test_client/hidl_test_client32"] 60 servers += ["/data/nativetest/hidl_test_servers/hidl_test_servers32"] 61 62 if has_bitness(64): 63 clients += ["/data/nativetest64/hidl_test_client/hidl_test_client64"] 64 servers += ["/data/nativetest64/hidl_test_servers/hidl_test_servers64"] 65 66 assert len(clients) > 0 67 assert len(servers) > 0 68 69 def short_name(binary): 70 if "64" in binary: 71 return "64" 72 return "32" 73 74 for client in clients: 75 for server in servers: 76 test_name = 'test_%s_to_%s' % (short_name(client), short_name(server)) 77 test = make_test(client, server) 78 setattr(TestHidl, test_name, test) 79 80 suite = unittest.TestLoader().loadTestsFromTestCase(TestHidl) 81 unittest.TextTestRunner(verbosity=2).run(suite) 82