xref: /aosp_15_r20/kernel/tests/net/test/util.py (revision 2f2c4c7ab4226c71756b9c31670392fdd6887c4f)
1*2f2c4c7aSAndroid Build Coastguard Worker# Copyright 2017 The Android Open Source Project
2*2f2c4c7aSAndroid Build Coastguard Worker#
3*2f2c4c7aSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*2f2c4c7aSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*2f2c4c7aSAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*2f2c4c7aSAndroid Build Coastguard Worker#
7*2f2c4c7aSAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0
8*2f2c4c7aSAndroid Build Coastguard Worker#
9*2f2c4c7aSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*2f2c4c7aSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*2f2c4c7aSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*2f2c4c7aSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*2f2c4c7aSAndroid Build Coastguard Worker# limitations under the License.
14*2f2c4c7aSAndroid Build Coastguard Worker
15*2f2c4c7aSAndroid Build Coastguard Worker"""Utilities for kernel net tests."""
16*2f2c4c7aSAndroid Build Coastguard Worker
17*2f2c4c7aSAndroid Build Coastguard Workerimport ctypes
18*2f2c4c7aSAndroid Build Coastguard Worker
19*2f2c4c7aSAndroid Build Coastguard Worker
20*2f2c4c7aSAndroid Build Coastguard Workerdef GetSysprop(name):
21*2f2c4c7aSAndroid Build Coastguard Worker  PROP_VALUE_MAX = 92
22*2f2c4c7aSAndroid Build Coastguard Worker  libc = ctypes.CDLL(ctypes.util.find_library("c"), use_errno=True)
23*2f2c4c7aSAndroid Build Coastguard Worker  name = ctypes.create_string_buffer(name)
24*2f2c4c7aSAndroid Build Coastguard Worker  value = ctypes.create_string_buffer(PROP_VALUE_MAX)
25*2f2c4c7aSAndroid Build Coastguard Worker  libc.__system_property_get(name, value)
26*2f2c4c7aSAndroid Build Coastguard Worker  return value.value
27*2f2c4c7aSAndroid Build Coastguard Worker
28*2f2c4c7aSAndroid Build Coastguard Worker
29*2f2c4c7aSAndroid Build Coastguard Workerdef VendorApiLevelIsAtLeast(min_level):
30*2f2c4c7aSAndroid Build Coastguard Worker  try:
31*2f2c4c7aSAndroid Build Coastguard Worker    level = int(GetSysprop(b"ro.vendor.api_level"))
32*2f2c4c7aSAndroid Build Coastguard Worker  except AttributeError:
33*2f2c4c7aSAndroid Build Coastguard Worker    return True
34*2f2c4c7aSAndroid Build Coastguard Worker  return level >= min_level
35*2f2c4c7aSAndroid Build Coastguard Worker
36*2f2c4c7aSAndroid Build Coastguard Worker
37*2f2c4c7aSAndroid Build Coastguard Workerdef GetPadLength(block_size, length):
38*2f2c4c7aSAndroid Build Coastguard Worker  return (block_size - (length % block_size)) % block_size
39*2f2c4c7aSAndroid Build Coastguard Worker
40*2f2c4c7aSAndroid Build Coastguard Worker
41*2f2c4c7aSAndroid Build Coastguard Workerdef InjectParameterizedTest(cls, param_list, name_generator):
42*2f2c4c7aSAndroid Build Coastguard Worker  """Injects parameterized tests into the provided class
43*2f2c4c7aSAndroid Build Coastguard Worker
44*2f2c4c7aSAndroid Build Coastguard Worker  This method searches for all tests that start with the name "ParamTest",
45*2f2c4c7aSAndroid Build Coastguard Worker  and injects a test method for each set of parameters in param_list. Names
46*2f2c4c7aSAndroid Build Coastguard Worker  are generated via the use of the name_generator.
47*2f2c4c7aSAndroid Build Coastguard Worker
48*2f2c4c7aSAndroid Build Coastguard Worker  Args:
49*2f2c4c7aSAndroid Build Coastguard Worker    cls: the class for which to inject all parameterized tests
50*2f2c4c7aSAndroid Build Coastguard Worker    param_list: a list of tuples, where each tuple is a combination of
51*2f2c4c7aSAndroid Build Coastguard Worker        of parameters to test (i.e. representing a single test case)
52*2f2c4c7aSAndroid Build Coastguard Worker    name_generator: A function that takes a combination of parameters and
53*2f2c4c7aSAndroid Build Coastguard Worker        returns a string that identifies the test case.
54*2f2c4c7aSAndroid Build Coastguard Worker  """
55*2f2c4c7aSAndroid Build Coastguard Worker  param_test_names = [name for name in dir(cls) if name.startswith("ParamTest")]
56*2f2c4c7aSAndroid Build Coastguard Worker
57*2f2c4c7aSAndroid Build Coastguard Worker  # Force param_list to an actual list; otherwise itertools.Product will hit
58*2f2c4c7aSAndroid Build Coastguard Worker  # the end, resulting in only the first ParamTest* method actually being
59*2f2c4c7aSAndroid Build Coastguard Worker  # parameterized
60*2f2c4c7aSAndroid Build Coastguard Worker  param_list = list(param_list)
61*2f2c4c7aSAndroid Build Coastguard Worker
62*2f2c4c7aSAndroid Build Coastguard Worker  # Parameterize each test method starting with "ParamTest"
63*2f2c4c7aSAndroid Build Coastguard Worker  for test_name in param_test_names:
64*2f2c4c7aSAndroid Build Coastguard Worker    func = getattr(cls, test_name)
65*2f2c4c7aSAndroid Build Coastguard Worker
66*2f2c4c7aSAndroid Build Coastguard Worker    for params in param_list:
67*2f2c4c7aSAndroid Build Coastguard Worker      # Give the test method a readable, debuggable name.
68*2f2c4c7aSAndroid Build Coastguard Worker      param_string = name_generator(*params)
69*2f2c4c7aSAndroid Build Coastguard Worker      new_name = "%s_%s" % (func.__name__.replace("ParamTest", "test"),
70*2f2c4c7aSAndroid Build Coastguard Worker                            param_string)
71*2f2c4c7aSAndroid Build Coastguard Worker      new_name = new_name.replace("(", "-").replace(")", "")  # remove parens
72*2f2c4c7aSAndroid Build Coastguard Worker
73*2f2c4c7aSAndroid Build Coastguard Worker      # Inject the test method
74*2f2c4c7aSAndroid Build Coastguard Worker      setattr(cls, new_name, _GetTestClosure(func, params))
75*2f2c4c7aSAndroid Build Coastguard Worker
76*2f2c4c7aSAndroid Build Coastguard Worker
77*2f2c4c7aSAndroid Build Coastguard Workerdef _GetTestClosure(func, params):
78*2f2c4c7aSAndroid Build Coastguard Worker  """ Creates a no-argument test method for the given function and parameters.
79*2f2c4c7aSAndroid Build Coastguard Worker
80*2f2c4c7aSAndroid Build Coastguard Worker  This is required to be separate from the InjectParameterizedTest method, due
81*2f2c4c7aSAndroid Build Coastguard Worker  to some interesting scoping issues with internal function declarations. If
82*2f2c4c7aSAndroid Build Coastguard Worker  left in InjectParameterizedTest, all the tests end up using the same
83*2f2c4c7aSAndroid Build Coastguard Worker  instance of TestClosure
84*2f2c4c7aSAndroid Build Coastguard Worker
85*2f2c4c7aSAndroid Build Coastguard Worker  Args:
86*2f2c4c7aSAndroid Build Coastguard Worker    func: the function for which this test closure should run
87*2f2c4c7aSAndroid Build Coastguard Worker    params: the parameters for the run of this test function
88*2f2c4c7aSAndroid Build Coastguard Worker  """
89*2f2c4c7aSAndroid Build Coastguard Worker
90*2f2c4c7aSAndroid Build Coastguard Worker  def TestClosure(self):
91*2f2c4c7aSAndroid Build Coastguard Worker    func(self, *params)
92*2f2c4c7aSAndroid Build Coastguard Worker
93*2f2c4c7aSAndroid Build Coastguard Worker  return TestClosure
94