1*2f2c4c7aSAndroid Build Coastguard Worker#!/usr/bin/python3 2*2f2c4c7aSAndroid Build Coastguard Worker# 3*2f2c4c7aSAndroid Build Coastguard Worker# Copyright 2018 The Android Open Source Project 4*2f2c4c7aSAndroid Build Coastguard Worker# 5*2f2c4c7aSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 6*2f2c4c7aSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 7*2f2c4c7aSAndroid Build Coastguard Worker# You may obtain a copy of the License at 8*2f2c4c7aSAndroid Build Coastguard Worker# 9*2f2c4c7aSAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 10*2f2c4c7aSAndroid Build Coastguard Worker# 11*2f2c4c7aSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 12*2f2c4c7aSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 13*2f2c4c7aSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*2f2c4c7aSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 15*2f2c4c7aSAndroid Build Coastguard Worker# limitations under the License. 16*2f2c4c7aSAndroid Build Coastguard Worker 17*2f2c4c7aSAndroid Build Coastguard Workerimport ctypes 18*2f2c4c7aSAndroid Build Coastguard Workerimport importlib 19*2f2c4c7aSAndroid Build Coastguard Workerimport os 20*2f2c4c7aSAndroid Build Coastguard Workerimport sys 21*2f2c4c7aSAndroid Build Coastguard Workerimport unittest 22*2f2c4c7aSAndroid Build Coastguard Worker 23*2f2c4c7aSAndroid Build Coastguard Workerimport gki 24*2f2c4c7aSAndroid Build Coastguard Workerimport namespace 25*2f2c4c7aSAndroid Build Coastguard Workerimport net_test 26*2f2c4c7aSAndroid Build Coastguard Worker 27*2f2c4c7aSAndroid Build Coastguard Worker# man 2 personality 28*2f2c4c7aSAndroid Build Coastguard Workerpersonality = ctypes.CDLL(None).personality 29*2f2c4c7aSAndroid Build Coastguard Workerpersonality.restype = ctypes.c_int 30*2f2c4c7aSAndroid Build Coastguard Workerpersonality.argtypes = [ctypes.c_ulong] 31*2f2c4c7aSAndroid Build Coastguard Worker 32*2f2c4c7aSAndroid Build Coastguard Worker# From Linux kernel's include/uapi/linux/personality.h 33*2f2c4c7aSAndroid Build Coastguard WorkerPER_QUERY = 0xFFFFFFFF 34*2f2c4c7aSAndroid Build Coastguard WorkerPER_LINUX = 0 35*2f2c4c7aSAndroid Build Coastguard WorkerPER_LINUX32 = 8 36*2f2c4c7aSAndroid Build Coastguard Worker 37*2f2c4c7aSAndroid Build Coastguard Workerall_test_modules = [ 38*2f2c4c7aSAndroid Build Coastguard Worker 'anycast_test', 39*2f2c4c7aSAndroid Build Coastguard Worker 'bpf_test', 40*2f2c4c7aSAndroid Build Coastguard Worker 'csocket_test', 41*2f2c4c7aSAndroid Build Coastguard Worker 'cstruct_test', 42*2f2c4c7aSAndroid Build Coastguard Worker 'kernel_feature_test', 43*2f2c4c7aSAndroid Build Coastguard Worker 'leak_test', 44*2f2c4c7aSAndroid Build Coastguard Worker 'multinetwork_test', 45*2f2c4c7aSAndroid Build Coastguard Worker 'neighbour_test', 46*2f2c4c7aSAndroid Build Coastguard Worker 'netlink_test', 47*2f2c4c7aSAndroid Build Coastguard Worker 'nf_test', 48*2f2c4c7aSAndroid Build Coastguard Worker 'parameterization_test', 49*2f2c4c7aSAndroid Build Coastguard Worker 'ping6_test', 50*2f2c4c7aSAndroid Build Coastguard Worker 'policy_crash_test', 51*2f2c4c7aSAndroid Build Coastguard Worker 'resilient_rs_test', 52*2f2c4c7aSAndroid Build Coastguard Worker 'sock_diag_test', 53*2f2c4c7aSAndroid Build Coastguard Worker 'srcaddr_selection_test', 54*2f2c4c7aSAndroid Build Coastguard Worker 'sysctls_test', 55*2f2c4c7aSAndroid Build Coastguard Worker 'tcp_fastopen_test', 56*2f2c4c7aSAndroid Build Coastguard Worker 'tcp_nuke_addr_test', 57*2f2c4c7aSAndroid Build Coastguard Worker 'tcp_repair_test', 58*2f2c4c7aSAndroid Build Coastguard Worker 'xfrm_algorithm_test', 59*2f2c4c7aSAndroid Build Coastguard Worker 'xfrm_test', 60*2f2c4c7aSAndroid Build Coastguard Worker 'xfrm_tunnel_test', 61*2f2c4c7aSAndroid Build Coastguard Worker] 62*2f2c4c7aSAndroid Build Coastguard Worker 63*2f2c4c7aSAndroid Build Coastguard Worker 64*2f2c4c7aSAndroid Build Coastguard Workerdef RunTests(modules_to_test): 65*2f2c4c7aSAndroid Build Coastguard Worker uname = os.uname() 66*2f2c4c7aSAndroid Build Coastguard Worker linux = uname.sysname 67*2f2c4c7aSAndroid Build Coastguard Worker kver = uname.release 68*2f2c4c7aSAndroid Build Coastguard Worker arch = uname.machine 69*2f2c4c7aSAndroid Build Coastguard Worker p = personality(PER_LINUX) 70*2f2c4c7aSAndroid Build Coastguard Worker true_arch = os.uname().machine 71*2f2c4c7aSAndroid Build Coastguard Worker personality(p) 72*2f2c4c7aSAndroid Build Coastguard Worker print('Running on %s %s %s %s/%s-%sbit%s%s' 73*2f2c4c7aSAndroid Build Coastguard Worker % (linux, kver, net_test.LINUX_VERSION, true_arch, arch, 74*2f2c4c7aSAndroid Build Coastguard Worker '64' if sys.maxsize > 0x7FFFFFFF else '32', 75*2f2c4c7aSAndroid Build Coastguard Worker ' GKI' if gki.IS_GKI else '', ' GSI' if net_test.IS_GSI else ''), 76*2f2c4c7aSAndroid Build Coastguard Worker file=sys.stderr) 77*2f2c4c7aSAndroid Build Coastguard Worker namespace.EnterNewNetworkNamespace() 78*2f2c4c7aSAndroid Build Coastguard Worker 79*2f2c4c7aSAndroid Build Coastguard Worker # First, run InjectTests on all modules, to ensure that any parameterized 80*2f2c4c7aSAndroid Build Coastguard Worker # tests in those modules are injected. 81*2f2c4c7aSAndroid Build Coastguard Worker for name in modules_to_test: 82*2f2c4c7aSAndroid Build Coastguard Worker importlib.import_module(name) 83*2f2c4c7aSAndroid Build Coastguard Worker if hasattr(sys.modules[name], 'InjectTests'): 84*2f2c4c7aSAndroid Build Coastguard Worker sys.modules[name].InjectTests() 85*2f2c4c7aSAndroid Build Coastguard Worker 86*2f2c4c7aSAndroid Build Coastguard Worker test_suite = unittest.defaultTestLoader.loadTestsFromNames(modules_to_test) 87*2f2c4c7aSAndroid Build Coastguard Worker 88*2f2c4c7aSAndroid Build Coastguard Worker assert test_suite.countTestCases() > 0, ( 89*2f2c4c7aSAndroid Build Coastguard Worker 'Inconceivable: no tests found! Command line: %s' % ' '.join(sys.argv)) 90*2f2c4c7aSAndroid Build Coastguard Worker 91*2f2c4c7aSAndroid Build Coastguard Worker runner = unittest.TextTestRunner(verbosity=2) 92*2f2c4c7aSAndroid Build Coastguard Worker result = runner.run(test_suite) 93*2f2c4c7aSAndroid Build Coastguard Worker sys.exit(not result.wasSuccessful()) 94*2f2c4c7aSAndroid Build Coastguard Worker 95*2f2c4c7aSAndroid Build Coastguard Worker 96*2f2c4c7aSAndroid Build Coastguard Workerif __name__ == '__main__': 97*2f2c4c7aSAndroid Build Coastguard Worker # If one or more tests were passed in on the command line, only run those. 98*2f2c4c7aSAndroid Build Coastguard Worker if len(sys.argv) > 1: 99*2f2c4c7aSAndroid Build Coastguard Worker RunTests(sys.argv[1:]) 100*2f2c4c7aSAndroid Build Coastguard Worker else: 101*2f2c4c7aSAndroid Build Coastguard Worker RunTests(all_test_modules) 102