Home | History | Annotate | Download | only in selinux
      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 
     18 import logging
     19 
     20 from vts.runners.host import asserts
     21 from vts.runners.host import base_test
     22 from vts.runners.host import const
     23 from vts.runners.host import test_runner
     24 from vts.testcases.kernel.api.selinux import SelinuxCheckReqProtTest
     25 from vts.testcases.kernel.api.selinux import SelinuxPolicyTest
     26 from vts.testcases.kernel.api.selinux import SelinuxNullTest
     27 from vts.utils.python.controllers import android_device
     28 from vts.utils.python.file import target_file_utils
     29 
     30 TEST_OBJECTS = {
     31     SelinuxCheckReqProtTest.SelinuxCheckReqProt(),
     32     SelinuxPolicyTest.SelinuxPolicy(), SelinuxNullTest.SelinuxNull()
     33 }
     34 
     35 
     36 class VtsKernelSelinuxFileApiTest(base_test.BaseTestClass):
     37     """Test cases which check content of selinuxfs files.
     38     """
     39 
     40     def setUpClass(self):
     41         self.dut = self.registerController(android_device)[0]
     42         self.dut.shell.InvokeTerminal(
     43             "KernelSelinuxFileApiTest")  # creates a remote shell instance.
     44         self.shell = self.dut.shell.KernelSelinuxFileApiTest
     45 
     46     def runSelinuxFileTest(self, test_object):
     47         """Reads the file and checks that its content and permissions are valid.
     48 
     49         Args:
     50             test_object: inherits KernelSelinuxFileTestBase, contains the test functions
     51         """
     52         logging.info("Testing existence of %s" % (test_object.get_path()))
     53 
     54         asserts.assertTrue(
     55             target_file_utils.Exists(test_object.get_path(), self.shell),
     56             "%s: File does not exist." % test_object.get_path())
     57 
     58         logging.info("Testing permissions of %s" % (test_object.get_path()))
     59         try:
     60             permissions = target_file_utils.GetPermission(
     61                 test_object.get_path(), self.shell)
     62             asserts.assertTrue(
     63                 test_object.get_permission_checker()(permissions),
     64                 "%s: File has invalid permissions (%s)" %
     65                 (test_object.get_path(), permissions))
     66         except (ValueError, IOError) as e:
     67             asserts.fail("Failed to assert permissions: %s" % str(e))
     68 
     69         logging.info("Testing format of %s" % (test_object.get_path()))
     70         file_content = target_file_utils.ReadFileContent(
     71             test_object.get_path(), self.shell)
     72         asserts.assertTrue(
     73             test_object.result_correct(file_content), "Results not valid!")
     74 
     75     def generateProcFileTests(self):
     76         """Run all selinux file tests."""
     77         self.runGeneratedTests(
     78             test_func=self.runSelinuxFileTest,
     79             settings=TEST_OBJECTS,
     80             name_func=lambda test_obj: "test" + test_obj.__class__.__name__)
     81 
     82 
     83 if __name__ == "__main__":
     84     test_runner.main()
     85