• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2#
3# Copyright (C) 2017 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 logging
19
20from vts.runners.host import asserts
21from vts.runners.host import base_test
22from vts.runners.host import const
23from vts.runners.host import test_runner
24
25from vts.testcases.kernel.api.proc import ProcCmdlineTest
26from vts.testcases.kernel.api.proc import ProcCpuInfoTest
27from vts.testcases.kernel.api.proc import ProcKmsgTest
28from vts.testcases.kernel.api.proc import ProcMapsTest
29from vts.testcases.kernel.api.proc import ProcMemInfoTest
30from vts.testcases.kernel.api.proc import ProcModulesTest
31from vts.testcases.kernel.api.proc import ProcMountsTest
32from vts.testcases.kernel.api.proc import ProcQtaguidCtrlTest
33from vts.testcases.kernel.api.proc import ProcRemoveUidRangeTest
34from vts.testcases.kernel.api.proc import ProcSimpleFileTests
35from vts.testcases.kernel.api.proc import ProcShowUidStatTest
36from vts.testcases.kernel.api.proc import ProcStatTest
37from vts.testcases.kernel.api.proc import ProcVersionTest
38from vts.testcases.kernel.api.proc import ProcVmallocInfoTest
39from vts.testcases.kernel.api.proc import ProcZoneInfoTest
40
41from vts.utils.python.controllers import android_device
42from vts.utils.python.file import file_utils
43
44TEST_OBJECTS = {
45    ProcCmdlineTest.ProcCmdlineTest(),
46    ProcCpuInfoTest.ProcCpuInfoTest(),
47    ProcKmsgTest.ProcKmsgTest(),
48    ProcSimpleFileTests.ProcKptrRestrictTest(),
49    ProcMapsTest.ProcMapsTest(),
50    ProcMemInfoTest.ProcMemInfoTest(),
51    ProcSimpleFileTests.ProcMmapMinAddrTest(),
52    ProcSimpleFileTests.ProcMmapRndBitsTest(),
53    ProcModulesTest.ProcModulesTest(),
54    ProcMountsTest.ProcMountsTest(),
55    ProcSimpleFileTests.ProcOverCommitMemoryTest(),
56    ProcQtaguidCtrlTest.ProcQtaguidCtrlTest(),
57    ProcSimpleFileTests.ProcRandomizeVaSpaceTest(),
58    ProcRemoveUidRangeTest.ProcRemoveUidRangeTest(),
59    ProcShowUidStatTest.ProcShowUidStatTest(),
60    ProcStatTest.ProcStatTest(),
61    ProcVersionTest.ProcVersionTest(),
62    ProcVmallocInfoTest.ProcVmallocInfoTest(),
63    ProcZoneInfoTest.ProcZoneInfoTest(),
64}
65
66TEST_OBJECTS_64 = {
67    ProcSimpleFileTests.ProcMmapRndCompatBitsTest(),
68}
69
70
71class KernelProcFileApiTest(base_test.BaseTestClass):
72    """Test cases which check content of proc files."""
73
74    def setUpClass(self):
75        self.dut = self.registerController(android_device)[0]
76        self.dut.shell.InvokeTerminal(
77            "KernelApiTest")  # creates a remote shell instance.
78        self.shell = self.dut.shell.KernelApiTest
79
80    def runProcFileTest(self, test_object):
81        """Reads from the file and checks that it parses and the content is valid.
82
83        Args:
84            test_object: inherits KernelProcFileTestBase, contains the test functions
85        """
86        asserts.skipIf(test_object in TEST_OBJECTS_64 and not self.dut.is64Bit,
87                       "Skip test for 64-bit kernel.")
88        filepath = test_object.get_path()
89        file_utils.assertPermissionsAndExistence(
90            self.shell, filepath, test_object.get_permission_checker())
91
92        logging.info("Testing format of %s", filepath)
93
94        asserts.assertTrue(
95            test_object.prepare_test(self.shell), "Setup failed!")
96
97        if not test_object.test_format():
98            return
99
100        file_content = self.ReadFileContent(filepath)
101        try:
102            parse_result = test_object.parse_contents(file_content)
103        except (SyntaxError, ValueError, IndexError) as e:
104            asserts.fail("Failed to parse! " + str(e))
105        asserts.assertTrue(
106            test_object.result_correct(parse_result), "Results not valid!")
107
108    def generateProcFileTests(self):
109        """Run all proc file tests."""
110        self.runGeneratedTests(
111            test_func=self.runProcFileTest,
112            settings=TEST_OBJECTS.union(TEST_OBJECTS_64),
113            name_func=lambda test_obj: "test" + test_obj.__class__.__name__)
114
115    def ReadFileContent(self, filepath):
116        """Read the content of a file and perform assertions.
117
118        Args:
119            filepath: string, path to file
120
121        Returns:
122            string, content of file
123        """
124        cmd = "cat %s" % filepath
125        results = self.shell.Execute(cmd)
126
127        # checks the exit code
128        asserts.assertEqual(
129            results[const.EXIT_CODE][0], 0,
130            "%s: Error happened while reading the file." % filepath)
131
132        return results[const.STDOUT][0]
133
134
135if __name__ == "__main__":
136    test_runner.main()
137