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#
17from abc import ABCMeta
18from abc import abstractmethod
19
20from vts.utils.python.file import target_file_utils
21
22
23class KernelSelinuxFileTestBase(object):
24    """Abstract test for the formatting of a selinux file.
25
26    Individual files can inherit from this class and define the correct path,
27    file content, and permissions.
28    """
29    __metaclass__ = ABCMeta
30
31    @abstractmethod
32    def get_path(self):
33        """Return the full path of this selinux file."""
34        pass
35
36    def result_correct(self, file_contents):
37        """Return True if the file contents are correct.
38
39        Subclasses define the requirements for the selinux file and validate
40        that the contents of a file are correct.
41
42        Args:
43            file_contents: String, the contents of an selinux file
44
45        Returns:
46            True if the contents are correct, False otherwise.
47        """
48        return True
49
50    def get_permission_checker(self):
51        """Gets the function handle to use for validating file permissions.
52
53        Return the function that will check if the permissions are correct.
54        By default, return the IsReadOnly function from target_file_utils.
55
56        Returns:
57            function which takes one argument (the unix file permission bits
58            in octal format) and returns True if the permissions are correct,
59            False otherwise.
60        """
61        return target_file_utils.IsReadOnly
62