xref: /aosp_15_r20/test/vts/tests/kernel_proc_file_api_test/proc_tests/ProcModulesTest.py (revision 9a74111979c139a065a9a7e4d45972320c5732c7)
1#
2# Copyright (C) 2020 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17import re
18from proc_tests import KernelProcFileTestBase
19
20
21class ProcModulesTest(KernelProcFileTestBase.KernelProcFileTestBase):
22    '''/proc/modules contains information about loaded kernel modules.'''
23
24    def prepare_test(self, dut):
25        api_level = dut.getLaunchApiLevel(strict=False)
26        self.require_module = False
27        return True
28
29    def parse_contents(self, contents):
30        module_present = False
31        # MODULE_NAME SIZE REFERENCE_COUNT USER1,USER2, STATE BASE_ADDRESS TAINT_FLAG
32        # MODULE_NAME is a string
33        # SIZE is an integer
34        # REFERENCE_COUNT is an integer or -
35        # USER1,USER2, is a list of modules using this module with a trailing comma.
36        #   If no modules are using this module or if modules cannot be unloaded then
37        #   - will appear. If this mdoule cannot be unloaded then [permanent] will be
38        #   the last entry.
39        # STATE is either Unloading, Loading, or Live
40        # BASE_ADDRESS is a memory address
41        # TAINT_FLAG is optional and if present, has characters between ( and )
42        test_re = re.compile(r"^\w+ \d+ (\d+|-) (((\w+,)*(\[permanent\],)?)|-) (Unloading|Loading|Live) 0x[0-9a-f]+( \(\w+\))?")
43        for line in contents.splitlines():
44            if not re.match(test_re, line):
45                raise SyntaxError("Malformed entry in /proc/modules: %s" % line)
46            else:
47                module_present = True
48        if self.require_module and not module_present:
49            raise SyntaxError("There must be at least one entry in /proc/modules")
50
51        return ''
52
53    def result_correct(self, parse_result):
54        return True
55
56    def get_path(self):
57        return "/proc/modules"
58