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 24from vts.testcases.kernel.api.selinux import SelinuxCheckReqProtTest 25from vts.testcases.kernel.api.selinux import SelinuxPolicyTest 26from vts.testcases.kernel.api.selinux import SelinuxNullTest 27from vts.utils.python.controllers import android_device 28from vts.utils.python.file import file_utils 29 30TEST_OBJECTS = { 31 SelinuxCheckReqProtTest.SelinuxCheckReqProt(), 32 SelinuxPolicyTest.SelinuxPolicy(), 33 SelinuxNullTest.SelinuxNull() 34} 35 36class 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 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 = file_utils.GetPermission( 61 test_object.get_path(), self.shell) 62 asserts.assertTrue(test_object.get_permission_checker()(permissions), 63 "%s: File has invalid permissions (%s)" % 64 (test_object.get_path(), permissions)) 65 except (ValueError, IOError) as e: 66 asserts.fail("Failed to assert permissions: %s" % str(e)) 67 68 logging.info("Testing format of %s" % (test_object.get_path())) 69 file_content = file_utils.ReadFileContent( 70 test_object.get_path(), self.shell) 71 asserts.assertTrue( 72 test_object.result_correct(file_content), "Results not valid!") 73 74 def generateProcFileTests(self): 75 """Run all selinux file tests.""" 76 self.runGeneratedTests(test_func=self.runSelinuxFileTest, 77 settings=TEST_OBJECTS, 78 name_func=lambda test_obj: "test" + test_obj.__class__.__name__) 79 80if __name__ == "__main__": 81 test_runner.main() 82