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