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 target_file_utils 29 30TEST_OBJECTS = { 31 SelinuxCheckReqProtTest.SelinuxCheckReqProt(), 32 SelinuxPolicyTest.SelinuxPolicy(), SelinuxNullTest.SelinuxNull() 33} 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.android_devices[0] 42 self.shell = self.dut.shell 43 44 def runSelinuxFileTest(self, test_object): 45 """Reads the file and checks that its content and permissions are valid. 46 47 Args: 48 test_object: inherits KernelSelinuxFileTestBase, contains the test functions 49 """ 50 logging.info("Testing existence of %s" % (test_object.get_path())) 51 52 asserts.assertTrue( 53 target_file_utils.Exists(test_object.get_path(), self.shell), 54 "%s: File does not exist." % test_object.get_path()) 55 56 logging.info("Testing permissions of %s" % (test_object.get_path())) 57 try: 58 permissions = target_file_utils.GetPermission( 59 test_object.get_path(), self.shell) 60 asserts.assertTrue( 61 test_object.get_permission_checker()(permissions), 62 "%s: File has invalid permissions (%s)" % 63 (test_object.get_path(), permissions)) 64 except (ValueError, IOError) as e: 65 asserts.fail("Failed to assert permissions: %s" % str(e)) 66 67 logging.info("Testing format of %s" % (test_object.get_path())) 68 file_content = target_file_utils.ReadFileContent( 69 test_object.get_path(), self.shell) 70 asserts.assertTrue( 71 test_object.result_correct(file_content), "Results not valid!") 72 73 def generateProcFileTests(self): 74 """Run all selinux file tests.""" 75 self.runGeneratedTests( 76 test_func=self.runSelinuxFileTest, 77 settings=TEST_OBJECTS, 78 name_func=lambda test_obj: "test" + test_obj.__class__.__name__) 79 80 81if __name__ == "__main__": 82 test_runner.main() 83