xref: /aosp_15_r20/system/sepolicy/tests/policy_test.py (revision e4a36f4174b17bbab9dc043f4a65dc8d87377290)
1*e4a36f41SAndroid Build Coastguard Worker# Copyright 2023 The Android Open Source Project
2*e4a36f41SAndroid Build Coastguard Worker#
3*e4a36f41SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*e4a36f41SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*e4a36f41SAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*e4a36f41SAndroid Build Coastguard Worker#
7*e4a36f41SAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
8*e4a36f41SAndroid Build Coastguard Worker#
9*e4a36f41SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*e4a36f41SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*e4a36f41SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*e4a36f41SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*e4a36f41SAndroid Build Coastguard Worker# limitations under the License.
14*e4a36f41SAndroid Build Coastguard Worker"""Tests for policy"""
15*e4a36f41SAndroid Build Coastguard Worker
16*e4a36f41SAndroid Build Coastguard Workerimport unittest
17*e4a36f41SAndroid Build Coastguard Workerfrom policy import MatchPathPrefix
18*e4a36f41SAndroid Build Coastguard Worker
19*e4a36f41SAndroid Build Coastguard Worker# pylint: disable=missing-docstring
20*e4a36f41SAndroid Build Coastguard Workerclass PolicyTests(unittest.TestCase):
21*e4a36f41SAndroid Build Coastguard Worker    def assertMatches(self, path, prefix):
22*e4a36f41SAndroid Build Coastguard Worker        self.assertTrue(MatchPathPrefix(path, prefix))
23*e4a36f41SAndroid Build Coastguard Worker
24*e4a36f41SAndroid Build Coastguard Worker    def assertDoesNotMatch(self, path, prefix):
25*e4a36f41SAndroid Build Coastguard Worker        self.assertFalse(MatchPathPrefix(path, prefix))
26*e4a36f41SAndroid Build Coastguard Worker
27*e4a36f41SAndroid Build Coastguard Worker    # tests
28*e4a36f41SAndroid Build Coastguard Worker
29*e4a36f41SAndroid Build Coastguard Worker    def test_match_path_prefix(self):
30*e4a36f41SAndroid Build Coastguard Worker        # check common prefix heuristics
31*e4a36f41SAndroid Build Coastguard Worker        self.assertMatches("/(vendor|system/vendor)/bin/sh", "/vendor/bin")
32*e4a36f41SAndroid Build Coastguard Worker        self.assertMatches("/(vendor|system/vendor)/bin/sh", "/system/vendor/bin"),
33*e4a36f41SAndroid Build Coastguard Worker        self.assertMatches("/(odm|vendor/odm)/etc/selinux", "/odm/etc"),
34*e4a36f41SAndroid Build Coastguard Worker        self.assertMatches("/(odm|vendor/odm)/etc/selinux", "/vendor/odm/etc"),
35*e4a36f41SAndroid Build Coastguard Worker        self.assertMatches("/(system_ext|system/system_ext)/bin/foo", "/system_ext/bin"),
36*e4a36f41SAndroid Build Coastguard Worker        self.assertMatches("/(system_ext|system/system_ext)/bin/foo", "/system/system_ext/bin"),
37*e4a36f41SAndroid Build Coastguard Worker        self.assertMatches("/(product|system/product)/lib/libc.so", "/product/lib"),
38*e4a36f41SAndroid Build Coastguard Worker        self.assertMatches("/(product|system/product)/lib/libc.so", "/system/product/lib"),
39*e4a36f41SAndroid Build Coastguard Worker        self.assertDoesNotMatch("/(vendor|system/vendor)/bin/sh", "/system/bin"),
40*e4a36f41SAndroid Build Coastguard Worker        self.assertDoesNotMatch("/(odm|vendor/odm)/etc/selinux", "/vendor/etc"),
41*e4a36f41SAndroid Build Coastguard Worker        self.assertDoesNotMatch("/(system_ext|system/system_ext)/bin/foo", "/system/bin"),
42*e4a36f41SAndroid Build Coastguard Worker        self.assertDoesNotMatch("/(product|system/product)/lib/libc.so", "/system/lib"),
43*e4a36f41SAndroid Build Coastguard Worker
44*e4a36f41SAndroid Build Coastguard Worker        # check generic regex
45*e4a36f41SAndroid Build Coastguard Worker        self.assertMatches("(/.*)+", "/system/etc/vintf")
46*e4a36f41SAndroid Build Coastguard Worker        self.assertDoesNotMatch("(/.*)+", "foo/bar/baz")
47*e4a36f41SAndroid Build Coastguard Worker
48*e4a36f41SAndroid Build Coastguard Worker        self.assertMatches("/(system|product)/lib(64)?(/.*)+.*\.so", "/system/lib/hw/libbaz.so")
49*e4a36f41SAndroid Build Coastguard Worker        self.assertMatches("/(system|product)/lib(64)?(/.*)+.*\.so", "/system/lib64/")
50*e4a36f41SAndroid Build Coastguard Worker        self.assertMatches("/(system|product)/lib(64)?(/.*)+.*\.so", "/product/lib/hw/libbaz.so")
51*e4a36f41SAndroid Build Coastguard Worker        self.assertMatches("/(system|product)/lib(64)?(/.*)+.*\.so", "/product/lib64/")
52*e4a36f41SAndroid Build Coastguard Worker        self.assertDoesNotMatch("/(system|product)/lib(64)?(/.*)+.*\.so", "/vendor/lib/hw/libbaz.so")
53*e4a36f41SAndroid Build Coastguard Worker        self.assertDoesNotMatch("/(system|product)/lib(64)?(/.*)+.*\.so", "/odm/lib64/")
54*e4a36f41SAndroid Build Coastguard Worker
55*e4a36f41SAndroid Build Coastguard Workerif __name__ == '__main__':
56*e4a36f41SAndroid Build Coastguard Worker    unittest.main(verbosity=2)
57